[R] optim with gradient
Göran Broström
gb at stat.umu.se
Wed Mar 20 15:57:14 CET 2002
On Wed, 20 Mar 2002 ripley at stats.ox.ac.uk wrote:
> On Wed, 20 Mar 2002, [iso-8859-1] Göran Broström wrote:
>
> > I want to maximise a function using 'optim' with a method that requires
> > the gradient, so I supply two functions, 'fun' for the function value
> > and 'd.fun' for its gradient. My question is: Since some calculations are
> > common to the two functions, is it possible to save time by feeding
> > 'd.fun' with some of the calculations made in 'fun'? That would require
> > (at least) that it is guaranteed that each time 'd.fun' is evaluated, it
> > is done immediately after a call to 'fun' at the same parameter value.
> > The help page says:
> >
> > "It is guaranteed that `gr' will be called immediately after a
> > call to `fn' at the same parameter values."
>
> Only if hessian=FALSE, and that phrase is now removed.
>
> > which is not exactly the same. However, look at this trivial example:
> >
> > prov <- function(x.start = 0){
> > fun <- function(x) {
> > e.x <- exp(-x)
> > x * e.x
> > }
> > d.fun <- function(x) {
> > e.x <- exp(-x)
> > e.x * (1 - x)
> > }
> > optim(x.start, fun, d.fun, method = "BFGS",
> > control = list(fnscale = -1))
> > }
> >
> > > prov(2)$counts
> > function gradient
> > 19 9
> >
> > This contradicts the help page info, since that implies that the gradient
> > is called at least as many times as the function.
>
> No, the other way around. It meant `if gr is called, it will be called
> ...'
I guessed that, which means that it would be possible to do what I want,
given that...
[...]
> You need to cache the par values and the results you need, and test both.
... I understand what you mean here. How is it done in my 'trivial
example'?
In my 'real' application, the function and gradient evaluations takes a
long time, but to do both in one call takes almost the same time as just
doing the gradient. So I guess it could be worthwhile to try...
Göran
[...]
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list