how to use fminunc to get Hessian matrix
Show older comments
Hi,
I tried to use "fminunc" to get Hessian matrix, but received error information such as - ____________________________________________________________________________
Error using Agarch. Too many output arguments ... Error in rho1_H01_noplots (line 139) [theta,fval,exitflag,output,grad,hessian] = fminunc(@(theta)Agarch(theta,z,zx),theta0,opts);
Caused by: Failure in initial user-supplied objective function evaluation. FMINUNC cannot continue. ____________________________________________________________________________
And I defined "opts" as - ____________________________________________________________________________
opts = optimset('GradObj','on','Hessian','on','Algorithm','sqp','Display', 'off'); ____________________________________________________________________________
I have been reading the online documents relevant to "fminunc" and "hessian" and adjusted my code correspondingly, but still can't make it work. So I am wondering if anyone could help me fix this issue.
Thanks very much!
Xiao
Answers (1)
Alan Weiss
on 2 Sep 2014
0 votes
Your options don't make sense, and may indicate a deeper problem. As explained in the options section of the fminunc documentation, the sqp algorithm does not take an input Hessian; the only algorithm that accepts an input Hessian is the trust-region algorithm. So it is possible that your objective function is trying to return the objective, gradient, and Hessian, but fminunc does not use the Hessian. So if you did not conditionalize your code to only give the number of outputs requested, then you can get this error.
That said, all algorithms can produce an output Hessian estimate.
There might be something else going on. Without more details, I don't know.
Alan Weiss
MATLAB mathematical toolbox documentation
12 Comments
Alan Weiss
on 3 Sep 2014
You really shouldn't set your Hessian option to 'on'. If you want to use the quasi-newton algorithm, then choose it in your options.
opts = optimset('Algorithm','quasi-newton','GradObj','on');
The main issue is your objective function. What does it return? With the GradObj option set to 'on', it should return the objective function f and the gradient of the objective g, and nothing else:
[f,g] = Agarch(theta,z,zx)
I hope you read the section about how to conditionalize your objective function that I pointed you to above.
Alan Weiss
MATLAB mathematical toolbox documentation
So it is possible that your objective function is trying to return the objective, gradient, and Hessian, but fminunc does not use the Hessian. So if you did not conditionalize your code to only give the number of outputs requested, then you can get this error.
Sounds like the opposite to me. fminunc is requesting additional outputs from the objective function like (the gradient and Hessian) required by the algorithm, but the objective function Agarch is not providing them.
Sophia
on 3 Sep 2014
Alan Weiss
on 3 Sep 2014
I am really sorry that you have not understood the documentation. You CAN get a Hessian estimate from fminunc. Do NOT set the Hessian option, which indicates that you are supplying the Hessian!
Let's suppose that your objective function Agarch returns just the value of the objective function.
f = Agarch(theta,z,zx) % I assume that f is a scalar
Then to get an estimated Hessian, do the following.
opts = optimset('Algorithm','quasi-newton');
fun = @(theta)Agarch(theta,z,zx); % I assume z and zx are in your workspace
[theta,fval,exitflag,output,grad,hessian] = fminunc(fun,theta0,opts);
fminunc returns the Hessian matrix.
Alan Weiss
MATLAB mathematical toolbox documentation
Sophia
on 4 Sep 2014
Try instead,
opts = optimoptions(@fminunc,'Algorithm', 'quasi-newton')
in R2013.
Sophia
on 17 Sep 2014
Matt J
on 17 Sep 2014
Is it possible when applying fminunc? If so, what might be the reason(s)?
To study this properly, we would need you to provide code that reproduces it. As a theory, though, if the true Hessian is singular at the solution, then any numerical approximation to it will be vulnerable, due to numerical noise, of becoming slightly non-positive definite.
Also, if I used or use some initial values very close to the "true" values for fminunc, is it reasonable for a simulation study?
We're in no position to tell you what's reasonable for what you're trying to do. However, initializing with true parameter values is, I believe, a good and sensible test of best case conditions.
Yulong Deng
on 28 Oct 2014
I have the same question with you about the same error, while I tried to divide the function by some integers which can let the result show, but I also don't know how to let the original function run without any problem.
Categories
Find more on Solver Outputs and Iterative Display in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!