MATLAB: User defined hessian in fminunc

fminuncuser-defined-hessian

I need to use a specific hessian [HB] in optimization using fminunc. I understand that this can possibly done by specific option: ''If set to 'objective', fminunc uses a user-defined Hessian for the objective function. The Hessian is the third output of the objective function (see fun). For optimset, the name is HessFcn. ''
What confuses me is how to call the specific hessian I'd like to use.
My objective function is:
function [f,g,HB]=lik_lambdaCUEB(y,x,z,alpha,beta)
vy=y-z*(z\y);
vx=x-z*(z\x);
e=z.*((y-x*beta)*ones(1,length(z(1,:))));
eB=z.*((vy-vx*beta)*ones(1,length(z(1,:))));
w=1+(e*alpha)+(1/2)*(alpha'*(e'*e)*alpha);
wB=1+(e*alpha)+(1/2)*(alpha'*(eB'*eB)*alpha);
f=-sum(1./w);
if nargout > 1
g = sum((alpha'*z')'.*(y-x*beta)); %not sure about the modification for CUE, check
%H = (-w.*z*(y-x*beta).^2);
HB=(z'*(vy-vx*beta)*(vy-vx*beta)'*z);
end
Which I am calling as below to optimize:
options = optimset('MaxFunEvals',1000,'TolFun',1.0000e-010,'TolX',1.0000e-020,'MaxIter',1000,'FunValCheck', 'on','HessFcn','objective');
[alpha]=fminunc(@(alpha)lik_lambdaCUEB(y,x,z,alpha,beta),(0*ones(kz,1)),options);
I am not sure where can I specify the hessian HB? How can I do this?

Best Answer

The documentation on writing gradients and Hessians is here. The specifics for setting options and includiing the Hessian is here. In particular, you also need to include the option that you are specifying the gradient:
options = optimoptions('fminunc','Algorithm','trust-region',...
'SpecifyObjectiveGradient',true,'HessianFcn','objective');
Alan Weiss
MATLAB mathematical toolbox documentation