[R] optim

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Aug 4 12:23:08 CEST 2005


On Thu, 4 Aug 2005, nwew wrote:

> Dear R-helpers,
>
> The function optim implements algorithms that I would like to use.

They are available to you as part of the R API at C level.

> I have function implemented in R, which given the parameters of which
> minimization is to take place returns a scalar as well as the gradient.
>
> Unfortunately optim requires two function _fn_ and _gr_ where fn returns the
> function value and gr the gradient. Splitting my function in two functions
> would be easy, however I am wondering if evaluating both is not doubling the
> the very high computational costs. Most of the computational intensive
> operations are identical if computing the function value and gradient.

That is an unusual situation.

> Question: is there a way to tweek optim that only one function evaluation is
> necessary? Are ther other implementations of the algorithm, which do assume
> that the function to be minimized returns the function value and the gradient
> as well?

You can of course write your function to cache the work and check if the
parameter value is unchanged from the last call.  Then if the optimizer 
calls for the gradient after the function value in the same place (and 
most methods will) you can just do the additional work for the gradient.

That is what nnet does, at C level.

-- 
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




More information about the R-help mailing list