Skip to content
Prev 18083 / 398503 Next

optim with gradient

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

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))
}
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. 

Q1: What is the correct guarantee? Is it what I need?

Q2: If so, How do I share calculations between the two functions? In my 
    trivial example, exp(-x) is calculated in both.

Thanks for any enlightenment! (Brian?)

G?ran