Skip to content
Snippets Groups Projects
Commit 32c6c50d authored by Marco Ratto's avatar Marco Ratto
Browse files

Fixed optimizer = 5 for dsge-vars and all other cases that do not allow...

Fixed optimizer = 5 for dsge-vars and all other cases that do not allow computing the outer product of gradient (non-linear optimizers as well).
parent 82e93363
No related branches found
No related tags found
No related merge requests found
...@@ -46,11 +46,11 @@ else ...@@ -46,11 +46,11 @@ else
info = d; info = d;
end end
if DynareOptions.mode_compute==5 % if DynareOptions.mode_compute==5
if ~strcmp(func2str(objective_function),'dsge_likelihood') % if ~strcmp(func2str(objective_function),'dsge_likelihood')
error('Options mode_compute=5 is not compatible with non linear filters or Dsge-VAR models!') % error('Options mode_compute=5 is not compatible with non linear filters or Dsge-VAR models!')
end % end
end % end
if info(1) > 0 if info(1) > 0
disp('Error in computing likelihood for initial parameter values') disp('Error in computing likelihood for initial parameter values')
......
...@@ -55,6 +55,12 @@ end ...@@ -55,6 +55,12 @@ end
h2=BayesInfo.ub-BayesInfo.lb; h2=BayesInfo.ub-BayesInfo.lb;
hmax=BayesInfo.ub-x; hmax=BayesInfo.ub-x;
hmax=min(hmax,x-BayesInfo.lb); hmax=min(hmax,x-BayesInfo.lb);
if isempty(ff0),
outer_product_gradient=0;
else
outer_product_gradient=1;
end
h1 = min(h1,0.5.*hmax); h1 = min(h1,0.5.*hmax);
...@@ -64,9 +70,11 @@ end ...@@ -64,9 +70,11 @@ end
xh1=x; xh1=x;
f1=zeros(size(f0,1),n); f1=zeros(size(f0,1),n);
f_1=f1; f_1=f1;
if outer_product_gradient
ff1=zeros(size(ff0)); ff1=zeros(size(ff0));
ff_1=ff1; ff_1=ff1;
ggh=zeros(size(ff0,1),n); ggh=zeros(size(ff0,1),n);
end
i=0; i=0;
while i<n while i<n
...@@ -124,20 +132,24 @@ while i<n ...@@ -124,20 +132,24 @@ while i<n
end end
end end
f1(:,i)=fx; f1(:,i)=fx;
if outer_product_gradient,
if any(isnan(ffx)) if any(isnan(ffx))
ff1=ones(size(ff0)).*fx/length(ff0); ff1=ones(size(ff0)).*fx/length(ff0);
else else
ff1=ffx; ff1=ffx;
end end
end
xh1(i)=x(i)-h1(i); xh1(i)=x(i)-h1(i);
[fx, ffx]=feval(func,xh1,DynareDataset,DynareOptions,Model,EstimatedParameters,BayesInfo,DynareResults); [fx, ffx]=feval(func,xh1,DynareDataset,DynareOptions,Model,EstimatedParameters,BayesInfo,DynareResults);
f_1(:,i)=fx; f_1(:,i)=fx;
if outer_product_gradient,
if any(isnan(ffx)) if any(isnan(ffx))
ff_1=ones(size(ff0)).*fx/length(ff0); ff_1=ones(size(ff0)).*fx/length(ff0);
else else
ff_1=ffx; ff_1=ffx;
end end
ggh(:,i)=(ff1-ff_1)./(2.*h1(i)); ggh(:,i)=(ff1-ff_1)./(2.*h1(i));
end
xh1(i)=x(i); xh1(i)=x(i);
if hcheck && htol<1 if hcheck && htol<1
htol=min(1,max(min(abs(dx))*2,htol*10)); htol=min(1,max(min(abs(dx))*2,htol*10));
...@@ -152,6 +164,7 @@ xh_1=xh1; ...@@ -152,6 +164,7 @@ xh_1=xh1;
gg=(f1'-f_1')./(2.*h1); gg=(f1'-f_1')./(2.*h1);
if outer_product_gradient,
if hflag==2 if hflag==2
gg=(f1'-f_1')./(2.*h1); gg=(f1'-f_1')./(2.*h1);
hessian_mat = zeros(size(f0,1),n*n); hessian_mat = zeros(size(f0,1),n*n);
...@@ -232,5 +245,12 @@ if any(isnan(hessian_mat)) ...@@ -232,5 +245,12 @@ if any(isnan(hessian_mat))
hessian_mat=hh_mat0(:); hessian_mat=hh_mat0(:);
end end
hh1=h1; hh1=h1;
htol1=htol;
save hess.mat hessian_mat save hess.mat hessian_mat
else
hessian_mat=[];
ihh=[];
hh_mat0 = [];
hh1 = [];
end
htol1=htol;
...@@ -61,9 +61,14 @@ fval=fval0; ...@@ -61,9 +61,14 @@ fval=fval0;
% initialize mr_gstep and mr_hessian % initialize mr_gstep and mr_hessian
mr_hessian(1,x,[],[],[],DynareDataset,DynareOptions,Model,EstimatedParameters,BayesInfo,DynareResults); mr_hessian(1,x,[],[],[],DynareDataset,DynareOptions,Model,EstimatedParameters,BayesInfo,DynareResults);
outer_product_gradient=1;
if isempty(hh) if isempty(hh)
[dum, gg, htol0, igg, hhg, h1]=mr_hessian(0,x,func0,flagit,htol,DynareDataset,DynareOptions,Model,EstimatedParameters,BayesInfo,DynareResults); [dum, gg, htol0, igg, hhg, h1]=mr_hessian(0,x,func0,flagit,htol,DynareDataset,DynareOptions,Model,EstimatedParameters,BayesInfo,DynareResults);
if isempty(dum),
outer_product_gradient=0;
igg = 1e-4*eye(nx);
else
hh0 = reshape(dum,nx,nx); hh0 = reshape(dum,nx,nx);
hh=hhg; hh=hhg;
if min(eig(hh0))<0 if min(eig(hh0))<0
...@@ -72,6 +77,7 @@ if isempty(hh) ...@@ -72,6 +77,7 @@ if isempty(hh)
hh=hh0; hh=hh0;
igg=inv(hh); igg=inv(hh);
end end
end
if htol0>htol if htol0>htol
htol=htol0; htol=htol0;
end end
...@@ -192,6 +198,9 @@ while norm(gg)>gtol && check==0 && jit<nit ...@@ -192,6 +198,9 @@ while norm(gg)>gtol && check==0 && jit<nit
save m1.mat x fval0 nig save m1.mat x fval0 nig
end end
[dum, gg, htol0, igg, hhg, h1]=mr_hessian(0,xparam1,func0,flagit,htol,DynareDataset,DynareOptions,Model,EstimatedParameters,BayesInfo,DynareResults); [dum, gg, htol0, igg, hhg, h1]=mr_hessian(0,xparam1,func0,flagit,htol,DynareDataset,DynareOptions,Model,EstimatedParameters,BayesInfo,DynareResults);
if isempty(dum),
outer_product_gradient=0;
end
if htol0>htol if htol0>htol
htol=htol0; htol=htol0;
disp(' ') disp(' ')
...@@ -199,6 +208,11 @@ while norm(gg)>gtol && check==0 && jit<nit ...@@ -199,6 +208,11 @@ while norm(gg)>gtol && check==0 && jit<nit
disp('Tolerance has to be relaxed') disp('Tolerance has to be relaxed')
disp(' ') disp(' ')
end end
if ~outer_product_gradient,
H = bfgsi1(H,gg-g(:,icount),xparam1-x(:,icount));
hh=inv(H);
hhg=hh;
else
hh0 = reshape(dum,nx,nx); hh0 = reshape(dum,nx,nx);
hh=hhg; hh=hhg;
if flagit==2 if flagit==2
...@@ -209,16 +223,18 @@ while norm(gg)>gtol && check==0 && jit<nit ...@@ -209,16 +223,18 @@ while norm(gg)>gtol && check==0 && jit<nit
igg=inv(hh); igg=inv(hh);
end end
end end
H = igg;
end
end end
disp(['Gradient norm ',num2str(norm(gg))]) disp(['Gradient norm ',num2str(norm(gg))])
ee=eig(hh); ee=eig(hh);
disp(['Minimum Hessian eigenvalue ',num2str(min(ee))]) disp(['Minimum Hessian eigenvalue ',num2str(min(ee))])
disp(['Maximum Hessian eigenvalue ',num2str(max(ee))]) disp(['Maximum Hessian eigenvalue ',num2str(max(ee))])
if max(eig(hh))<0, disp('Negative definite Hessian! Local maximum!'), pause, end, if max(eig(hh))<0, disp('Negative definite Hessian! Local maximum!'), pause(1), end,
t=toc; t=toc;
disp(['Elapsed time for iteration ',num2str(t),' s.']) disp(['Elapsed time for iteration ',num2str(t),' s.'])
g(:,icount+1)=gg; g(:,icount+1)=gg;
H = igg;
save m1.mat x hh g hhg igg fval0 nig H save m1.mat x hh g hhg igg fval0 nig H
end end
end end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment