Problem with fminunc, why does the parameters not change?

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.

 채택된 답변

Matt J
Matt J 2014년 5월 7일
편집: Matt J 2014년 5월 7일
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

Matt J
Matt J 2014년 5월 7일
편집: Matt J 2014년 5월 7일
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);

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Barbara
Barbara 2014년 5월 14일

0 개 추천

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

태그

질문:

2014년 5월 7일

답변:

2014년 5월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by