[R] optim and "the function should not" advice

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Sep 20 11:11:33 CEST 2012


On 20/09/2012 09:24, Gildas Mazo wrote:
> Dear R users,
>
> I'm using optim to optimize a pretty complicated function. This function takes the parameter vector "theta" and within its body I use instructions like
>
> sigma<-theta[a:b]; computations with sigma...
> out<-c()
> for (i in 1:d){
> a<-theta[(3*d+i):c]
> out[i]<-evaluation of an expression involving 'a' (I use symbolic differentiation)
> }
>
> Unfortunately for certain problems 'optim' returns a parameter vector which didn't move at all from the initial parameters, and the output says that although the function has been evaluated a high number of times, the gradient (which I fed the function with) has been evaluated only one time. I used the BFGS method.

On face value that means it is unable to find a small step that goes 
downhill consistent with the gradient, and usually indicates an error in 
the gradient function or using numerical derivatives on a 
non-differentiable function.

> By chance I looked at the help and I read "The parameter vector passed to fn has special semantics and may be shared between calls: the function should not change or copy it" . Could the instructions above be the cause of the failure? If so, how to deal with symbolic differentation?

None of the code you show us changes 'theta'.  It would be a very 
unusual thing to do, but has happened in error when people have used 
compiled code.

> Thanks in advance,
> Gildas


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