Re: [R] Gradient function in OPTIMX

2011-08-30 Thread Ravi Varadhan
Hi Kathie,

The gradient check in optimx checks if the user specified gradient (at 
starting parameters) is within roughly 1.e-05 * (1 + fval) of the numerically 
computed gradient. It is likely that you have correctly coded up the gradient, 
but still there can be significant differences b/w numerical and exact 
gradients.  This can happen when the gradients are very large.  

I would check this again separately as follows:

require(numDeriv)

mygrad -  gr.fy(theta0)

numgrad - grad(x=theta0, func=gr.fy)

cbind(mygrad, numgrad)

all.equal(mygrad, numgrad)

Can you report these gradients to us?

In optimx, we should probably change this into a warning rather than a 
stop. 

Ravi.

---
Ravi Varadhan, Ph.D.
Assistant Professor,
Division of Geriatric Medicine and Gerontology School of Medicine Johns Hopkins 
University

Ph. (410) 502-2619
email: rvarad...@jhmi.edu

__
R-help@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.


Re: [R] gradient function in OPTIMX

2011-08-30 Thread Rubén Roa
Hi,

In my package CatDyn, which uses optimx, I included the gradients of 20 version 
of the model involved.
I estimate model parameters with numerical gradients, and at the final 
estimates I calculate the analytical gradients.
In the simplest version of the model the analytical gradients computed post hoc 
are almost identical to the numerical gradients. This shows that the analytical 
gradients (whose formulas were obtained by the CAS Maxima) are correct, at 
least for those simple versions of my model. However, if I try to pass the 
analytical gradients to optimx in a new optimization, I invariably get the 
error message that you got: Gradient function might be wrong - check it!
This happens regardless of the method used (BFGS, spg, Rcgmin).
Same as you, when I try to pass the gradients to optim, instead of optimx, the 
gradients are accepted and computed correctly, but then I cann't use the very 
nice other features of optimx.
I wanted to report this to Ravi and Prof. Nash but I haven't got the time for a 
full report with several examples and variations.
So now that you report it, here I am, seconding you in calling the attention to 
this apparent problem in optimx.

Rubén H. Roa-Ureta, Ph. D.
AZTI Tecnalia, Txatxarramendi Ugartea z/g,
Sukarrieta, Bizkaia, SPAIN

-Original Message-
From: r-help-boun...@r-project.org on behalf of Kathie
Sent: Mon 8/29/2011 11:10 AM
To: r-help@r-project.org
Subject: [R] gradient function in OPTIMX
 
Dear R users

When I use OPTIM with BFGS, I've got a significant result without an error
message.  However, when I use OPTIMX with BFGS( or spg), I've got the
following an error message.



  optimx(par=theta0, fn=obj.fy, gr=gr.fy, method=BFGS,
 control=list(maxit=1))

Error: Gradient function might be wrong - check it! 



I checked and checked my gradient function line by line. I could not find
anything wrong.

Is it a bug or something?  I prefer OPTIMX, so I'd like to know why.

Thanks a lot in advance

Regards,

Kathryn Lord 

--
View this message in context: 
http://r.789695.n4.nabble.com/gradient-function-in-OPTIMX-tp3775791p3775791.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] gradient function in OPTIMX

2011-08-30 Thread John C Nash
optimx uses exactly the same code as optim for BFGS. However, the call to optim 
in optimx
is preceded by a check of the gradient at the starting values supplied using 
numDeriv.

That is, we evaluate the gradient with gr=(user's function for gradient) and 
then with
the grad() function from numDeriv. There are some tolerances used, and 
depending on your
function and its perversities (and most nonlinear functions do have some), 
there won't be
total agreement, so the message can get popped up.

Suggestion: Try evaluating the function and gradient at several sets of inputs 
along with
the numDeriv grad() equivalent and see if they are close enough in your own 
view. If not,
then possibly your gradient code is not quite right.

This suggestion is appropriate generally in building optimization problem 
codes, and is
part of the optimgui package of Yixuan Qui built in the recent Google Summer of 
Code effort.

John Nash


On 08/30/2011 06:00 AM, r-help-requ...@r-project.org wrote:
 Message: 10
 Date: Mon, 29 Aug 2011 02:10:36 -0700 (PDT)
 From: Kathie kathryn.lord2...@gmail.com
 To: r-help@r-project.org
 Subject: [R] gradient function in OPTIMX
 Message-ID: 1314609036951-3775791.p...@n4.nabble.com
 Content-Type: text/plain; charset=us-ascii
 
 Dear R users
 
 
 When I use OPTIM with BFGS, I've got a significant result without an error
 message.  However, when I use OPTIMX with BFGS( or spg), I've got the
 following an error message.
 
 
 
   optimx(par=theta0, fn=obj.fy, gr=gr.fy, method=BFGS,
  control=list(maxit=1))
 Error: Gradient function might be wrong - check it! 
 
 
 
 I checked and checked my gradient function line by line. I could not find
 anything wrong.
 
 Is it a bug or something?  I prefer OPTIMX, so I'd like to know why.
 
 Thanks a lot in advance
 
 Regards,
 
 Kathryn Lord 
 
 --
 View this message in context: 
 http://r.789695.n4.nabble.com/gradient-function-in-OPTIMX-tp3775791p3775791.html
 Sent from the R help mailing list archive at Nabble.com.
 
 
 
 --

__
R-help@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.


[R] gradient function in OPTIMX

2011-08-29 Thread Kathie
Dear R users


When I use OPTIM with BFGS, I've got a significant result without an error
message.  However, when I use OPTIMX with BFGS( or spg), I've got the
following an error message.



  optimx(par=theta0, fn=obj.fy, gr=gr.fy, method=BFGS,
 control=list(maxit=1))

Error: Gradient function might be wrong - check it! 



I checked and checked my gradient function line by line. I could not find
anything wrong.

Is it a bug or something?  I prefer OPTIMX, so I'd like to know why.

Thanks a lot in advance

Regards,

Kathryn Lord 

--
View this message in context: 
http://r.789695.n4.nabble.com/gradient-function-in-OPTIMX-tp3775791p3775791.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.