fminunc works, fmincon does not
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I hope someone can help explaining why fminunc works (it does!), and why fmincon does not, in the code below. The estimates returned by fminunc are within the boundaries given to fmincon. The motivation for getting fmincon to work is the need to exchange the data, which causes fminunc to vomit (the parameters gets outside the boundaries and the objective function explodes).
Code
clear all
MyDataSet=importdata('MyDataSet.mat');
% Y and RK
RK=cell2mat(MyDataSet(2:end,5)); RK(RK==0)=100000*eps;
Y=cell2mat(MyDataSet(2:end,35));
%%Unconstrained Optimization
par=[0.5;0.1;4;0.3;0.3;0.01;0.7;0.05;0.05;0.05;0.05];
fminuncDNF = true;
while fminuncDNF
try
[x,fval] = fminunc(@(P) -fLogLik(P,Y,RK),par,options);
fminuncDNF = false;
catch
disp('fminunc failed, changing start guess');
a=rand(1,1);
par=[rand(3,1);a;0.95-a;randn(6,1)];
end
end
%%Constrained optimization
par=[0.5;0.1;4;0.3;0.3;0.01;0.7;0.05;0.05;0.05;0.05];
A=[zeros(4,2) [-eye(3);0 1 1] zeros(4,6)];
b=[zeros(3,1);1];
b=b-[zeros(3,1);]*2*optimget(options,'TolCon',1e-6);
fminconDNF = true;
while fminconDNF
try
[x,fval] = fmincon('-fLogLik(P)',par,A,b,[],[],[],[],[],options,Y,RK);
fminconDNF = false;
catch
disp('fmincon failed, changing start guess');
a=rand(1,1);
par=[rand(3,1);a;0.95-a;randn(6,1)];
end
end
----
function S=fLogLik(P,Y,RK)
h_u=fFilter_realLogGARCH(P,Y,RK);
mu0=P(2);
sig2_u=mean(h_u(2:end,2).^2);
tmp1=log(h_u(:,1));
tmp2=(Y-mu0).^2;
tmp2=tmp2./h_u(:,1);
tmp3=log(sig2_u);
l=-1*(tmp1+tmp2+tmp3+1);
S=sum(l(2:end,1));
end
function h_u=fFilter_realLogGARCH(P,Y,RK)
T=length(Y); h=zeros(T,1); u=h; h(1,1)=exp(P(1));
for t=2:T
z_tm1=(Y(t-1,1)-P(2))/sqrt(h(t-1,1));
tmp1=P(4)*log(h(t-1,1));
tmp2=P(5)*log(RK(t-1,1));
tmp3=P(6)*z_tm1;
tmp4=P(7)*(z_tm1^2-1);
h(t,1)=exp(P(3)+tmp1+tmp2+tmp3+tmp4);
z_t=(Y(t,1)-P(2))/sqrt(h(t,1));
tmp1=log(RK(t,1))-P(8);
tmp2=P(9)*log(h(t,1));
tmp3=P(10)*z_t;
tmp4=P(11)*(z_t^2-1);
u(t,1)=tmp1-tmp2-tmp3-tmp4;
end
h_u=[h,u];
end
Best, Kasper
0 Kommentare
Antworten (1)
Steve Grikschat
am 28 Feb. 2011
Hi Kasper,
It's hard to tell what's going on from the code, so I'll just offer some generalities.
Each solver and algorithm is different and will take different paths to (hopefully) reach the solution. Perhaps, putting constraints on the problem will guide the constrained algorithm into a difficult spot from which it can't recover. It's hard to say from this vantage point. You are starting some of the parameters close to the bounds.
On a related note, you should restate the first 3 linear inequalities as bounds. This has some distinct advantages: - the finite-difference derivative estimates will stay within the bounds - the interior-point and sqp algorithms of fmincon will honor the bounds at each step
Since you've mentioned that veering outside the constraints causes the objective to blow up, this seems important.
2 Kommentare
Steve Grikschat
am 1 Mär. 2011
Jumping to catch means there is an error from fmincon or from the problem set up. You need to debug this before you can say whether or not fmincon "works".
My initial reaction is to the specification of the objective:
fmincon('-fLogLik(P)',...
is probably not gonna work. Try making this like your call to fminunc:
fmincon(@(P)-fLogLik(P,Y,RK),...
If that doesn't work, use the debugger to find out the problem.
Regards
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!