Dear all, I am using the functions 'optim' and 'nlminb'. For both, you can provide a function which computes the gradient of the objective function (to enhance speed and precision). In my case, both the objective function and the gradient take time to be computed and share many common computations (similar matrix, products, etc...). Therefore, I have to compute these quantities twice which slows down the procedure. How can I get rid of that in a similar way as with 'nlmin' which allows the attribute 'gradient' in the output of the objective function? Thanks a lot for your answer -- David Ardia H-building, room 11-26 Econometric Institute Erasmus University PO Box 1738 NL 3000 DR Rotterdam The Netherlands Phone: +31 (0)10 408 2269
Check out: http://finzi.psych.upenn.edu/R/Rhelp02a/archive/83547.html On Tue, Apr 22, 2008 at 5:50 AM, Ardia David <david.ardia at unifr.ch> wrote:> Dear all, > I am using the functions 'optim' and 'nlminb'. For both, you can provide > a function which computes the gradient of the objective function (to > enhance speed and precision). In my case, both the objective function > and the gradient take time to be computed and share many common > computations (similar matrix, products, etc...). Therefore, I have to > compute these quantities twice which slows down the procedure. How can I > get rid of that in a similar way as with 'nlmin' which allows the > attribute 'gradient' in the output of the objective function? > Thanks a lot for your answer > -- > David Ardia > H-building, room 11-26 > Econometric Institute > Erasmus University > PO Box 1738 > NL 3000 DR Rotterdam > The Netherlands > Phone: +31 (0)10 408 2269 > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
You can cache the results for reuse in the gradient call (most easily by sharing an environment between them). I don't think nlmin exists in R (nlm does), but doing it that way wastes gradient computations where they would not be used. Your usage may be exceptional, but for most usages the gradient computations are markedly more expensive, and these functions were designed for such problems. On Tue, 22 Apr 2008, Ardia David wrote:> Dear all, > I am using the functions 'optim' and 'nlminb'. For both, you can provide > a function which computes the gradient of the objective function (to > enhance speed and precision). In my case, both the objective function > and the gradient take time to be computed and share many common > computations (similar matrix, products, etc...). Therefore, I have to > compute these quantities twice which slows down the procedure. How can I > get rid of that in a similar way as with 'nlmin' which allows the > attribute 'gradient' in the output of the objective function? > Thanks a lot for your answer > -- > David Ardia > H-building, room 11-26 > Econometric Institute > Erasmus University > PO Box 1738 > NL 3000 DR Rotterdam > The Netherlands > Phone: +31 (0)10 408 2269 > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595