Problem with fminunc, why does the parameters not change?

2 views (last 30 days)
Hi,
may be is a very simple mistake but I cant see it. I have a simple model to try how fminunc works and the thing is I dont get different parameters from the initial ones.
this is my code:
function main2()
pars0=[0.2 0.3 0.4]';
objective=@(x)li2(x);
[x,fval,exitflag,output,grad,hessian]= fminunc(objective,pars0);
disp(x);
disp(fval);
disp(exitflag);
end
function [ilik]=li2(x);
mData=randn(50,1);
iT=size(mData,1);
lik=zeros(iT,1);
hxt=zeros(iT,1);
Yt=0;
xt=0;
for i = 1 : iT
Yt=mData(i,1);
F=x(3,1)*Yt^2+x(1,1)*Yt+x(2,1);
hxt(i,1)=F;
lik(i,1) = -(0.5)*iT*log(2*pi)-0.5*F;
end
ilik = -sum(lik);
end
Thanks a lot in advance.

Accepted Answer

Matt J
Matt J on 7 May 2014
Edited: Matt J on 7 May 2014
That is not how your code behaves for me. I do see a small change between pars0 and the final solution:
>> x-pars0
ans =
1.0e-05 *
0.0872
-0.0150
0.1102
However, it makes no sense to have randn() statements in your objective function. Doing so means the objective function is not a deterministic function of the input x. How can the iterative minimization algorithm know what to minimize if the definition of the function is changing randomly with every iteration?
  1 Comment
Matt J
Matt J on 7 May 2014
Edited: Matt J on 7 May 2014
You probably meant to do something like the following. Notice that it keeps the random data fixed while the minimization is in progress. However, without constraints, the minimum is simply -Inf.
pars0=[0.2 0.3 0.4]';
mData=randn(50,1);
objective=@(x)li2(x,mData);
[x,fval,exitflag,output,grad,hessian]= fminunc(objective,pars0);
disp(x);
disp(fval);
disp(exitflag);
keyboard
function [ilik]=li2(x,mData)
iT=size(mData,1);
lik=zeros(iT,1);
hxt=zeros(iT,1);
Yt=0;
xt=0;
for i = 1 : iT
Yt=mData(i,1);
F=x(3,1)*Yt^2+x(1,1)*Yt+x(2,1);
hxt(i,1)=F;
lik(i,1) = -(0.5)*iT*log(2*pi)-0.5*F;
end
ilik = -sum(lik);

Sign in to comment.

More Answers (1)

Barbara
Barbara on 14 May 2014
Dear Matt J
Yes the mistake was that I put the random generator of the data inside the function. I see it now. Thanks!

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!