From 1fc33a4c63926dc88e6145eb96613800a3bc3eed Mon Sep 17 00:00:00 2001 From: Johannes Pfeifer Date: Thu, 21 Jan 2021 19:34:47 +0100 Subject: [PATCH 1/9] newrat.m: check for valid initial parameter --- matlab/optimization/newrat.m | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/matlab/optimization/newrat.m b/matlab/optimization/newrat.m index e94d56554..05adce69c 100644 --- a/matlab/optimization/newrat.m +++ b/matlab/optimization/newrat.m @@ -84,6 +84,10 @@ end % func0 = str2func([func2str(func0),'_hh']); % func0 = func0; [fval0,exit_flag,gg,hh]=penalty_objective_function(x,func0,penalty,varargin{:}); +if ~exit_flag + disp_verbose('Bad initial parameter.',Verbose) + return +end fval=fval0; % initialize mr_gstep and mr_hessian From 96b1ca3bee12646c7f6c9a76f7be2fed4a2d6f74 Mon Sep 17 00:00:00 2001 From: Johannes Pfeifer Date: Thu, 21 Jan 2021 21:26:06 +0100 Subject: [PATCH 2/9] mr_hessian.m: honor Save_files option --- matlab/dynare_estimation_1.m | 2 +- matlab/optimization/mr_hessian.m | 11 +++++++---- matlab/optimization/newrat.m | 15 ++++++++------- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/matlab/dynare_estimation_1.m b/matlab/dynare_estimation_1.m index e24ee6d6f..cda0ecb02 100644 --- a/matlab/dynare_estimation_1.m +++ b/matlab/dynare_estimation_1.m @@ -274,7 +274,7 @@ if ~isequal(options_.mode_compute,0) && ~options_.mh_posterior_mode_estimation if compute_hessian crit = options_.newrat.tolerance.f; newratflag = newratflag>0; - hh = reshape(mr_hessian(xparam1,objective_function,fval,newratflag,crit,new_rat_hess_info,[bounds.lb bounds.ub],bayestopt_.p2,dataset_, dataset_info, options_,M_,estim_params_,bayestopt_,bounds,oo_), nx, nx); + hh = reshape(mr_hessian(xparam1,objective_function,fval,newratflag,crit,new_rat_hess_info,[bounds.lb bounds.ub],bayestopt_.p2,0,dataset_, dataset_info, options_,M_,estim_params_,bayestopt_,bounds,oo_), nx, nx); end options_.kalman_algo = kalman_algo0; end diff --git a/matlab/optimization/mr_hessian.m b/matlab/optimization/mr_hessian.m index d1ab01e7b..873e0184f 100644 --- a/matlab/optimization/mr_hessian.m +++ b/matlab/optimization/mr_hessian.m @@ -1,15 +1,15 @@ -function [hessian_mat, gg, htol1, ihh, hh_mat0, hh1, hess_info] = mr_hessian(x,func,penalty,hflag,htol0,hess_info,bounds,prior_std,varargin) -% function [hessian_mat, gg, htol1, ihh, hh_mat0, hh1, hess_info] = mr_hessian(x,func,penalty,hflag,htol0,hess_info,bounds,prior_std,varargin) +function [hessian_mat, gg, htol1, ihh, hh_mat0, hh1, hess_info] = mr_hessian(x,func,penalty,hflag,htol0,hess_info,bounds,prior_std,Save_files,varargin) +% function [hessian_mat, gg, htol1, ihh, hh_mat0, hh1, hess_info] = mr_hessian(x,func,penalty,hflag,htol0,hess_info,bounds,prior_std,Save_files,varargin) % numerical gradient and Hessian, with 'automatic' check of numerical % error % % adapted from Michel Juillard original routine hessian.m % % Inputs: +% - x parameter values % - func function handle. The function must give two outputs: % the log-likelihood AND the single contributions at times t=1,...,T % of the log-likelihood to compute outer product gradient -% - x parameter values % - penalty penalty due to error code % - hflag 0: Hessian computed with outer product gradient, one point % increments for partial derivatives in gradients @@ -26,6 +26,7 @@ function [hessian_mat, gg, htol1, ihh, hh_mat0, hh1, hess_info] = mr_hessian(x,f % computation of Hessian % - bounds prior bounds of parameters % - prior_std prior standard devation of parameters (can be NaN) +% - Save_files indicator whether files should be saved % - varargin other inputs % e.g. in dsge_likelihood % varargin{1} --> DynareDataset @@ -267,7 +268,9 @@ if outer_product_gradient hessian_mat=hh_mat0(:); end hh1=hess_info.h1; - save hess.mat hessian_mat + if Save_files + save('hess.mat','hessian_mat') + end else hessian_mat=[]; ihh=[]; diff --git a/matlab/optimization/newrat.m b/matlab/optimization/newrat.m index 05adce69c..a49d0e584 100644 --- a/matlab/optimization/newrat.m +++ b/matlab/optimization/newrat.m @@ -94,7 +94,7 @@ fval=fval0; outer_product_gradient=1; if isempty(hh) - [dum, gg, htol0, igg, hhg, h1, hess_info]=mr_hessian(x,func0,penalty,flagit,htol,hess_info,bounds,prior_std,varargin{:}); + [dum, gg, htol0, igg, hhg, h1, hess_info]=mr_hessian(x,func0,penalty,flagit,htol,hess_info,bounds,prior_std,Save_files,varargin{:}); if isempty(dum) outer_product_gradient=0; igg = 1e-4*eye(nx); @@ -160,16 +160,17 @@ while norm(gg)>gtol && check==0 && jitgtol && check==0 && jit0 - [dum, gg, htol0, igg, hhg, h1, hess_info]=mr_hessian(xparam1,func0,penalty,flagg,ftol0,hess_info,bounds,prior_std,varargin{:}); + [dum, gg, htol0, igg, hhg, h1, hess_info]=mr_hessian(xparam1,func0,penalty,flagg,ftol0,hess_info,bounds,prior_std,Save_files,varargin{:}); if flagg==2 hh = reshape(dum,nx,nx); ee=eig(hh); @@ -252,7 +253,7 @@ while norm(gg)>gtol && check==0 && jit Date: Thu, 21 Jan 2021 21:43:36 +0100 Subject: [PATCH 3/9] newrat.m: only find index once --- matlab/optimization/csminit1.m | 1 - 1 file changed, 1 deletion(-) diff --git a/matlab/optimization/csminit1.m b/matlab/optimization/csminit1.m index 22391027e..ec088edc7 100644 --- a/matlab/optimization/csminit1.m +++ b/matlab/optimization/csminit1.m @@ -129,7 +129,6 @@ else done=0; factor=3; shrink=1; - lambdaMin=0; lambdaMax=inf; lambdaPeak=0; fPeak=f0; From ef14cdb1e975cefc7b36ebb7cf7a35a3ee96b25a Mon Sep 17 00:00:00 2001 From: Johannes Pfeifer Date: Wed, 27 Jan 2021 21:42:48 +0100 Subject: [PATCH 4/9] mr_hessian.m: remove redundant lines and try-catch statement --- matlab/optimization/mr_hessian.m | 20 +++----------------- 1 file changed, 3 insertions(+), 17 deletions(-) diff --git a/matlab/optimization/mr_hessian.m b/matlab/optimization/mr_hessian.m index 873e0184f..b7ee3c113 100644 --- a/matlab/optimization/mr_hessian.m +++ b/matlab/optimization/mr_hessian.m @@ -100,11 +100,7 @@ while i(3*hess_info.htol) hess_info.h1(i)= hess_info.htol/abs(dx(it))*hess_info.h1(i); hess_info.h1(i) = max(hess_info.h1(i),1e-10); xh1(i)=x(i)+hess_info.h1(i); - try - [fx,exit_flag,ffx]=penalty_objective_function(xh1,func,penalty,varargin{:}); - catch - fx=1.e8; - end + [fx,exit_flag,ffx]=penalty_objective_function(xh1,func,penalty,varargin{:}); iter=0; while (fx-f0)==0 && iter<50 hess_info.h1(i)= hess_info.h1(i)*2; @@ -238,9 +226,7 @@ if outer_product_gradient sd0=sqrt(diag(hh0)); %rescaled 'standard errors' using second order derivatives sd=sqrt(diag(hh_mat)); %rescaled 'standard errors' using outer product hh_mat=hh_mat./(sd*sd').*(sd0*sd0'); %rescaled inverse outer product with 'true' std's - igg=inv(hh_mat); % rescaled outer product hessian with 'true' std's ihh=A'*(hh_mat\A); % inverted outer product hessian - hh_mat0=inv(A)'*hh_mat*inv(A); % outer product hessian with 'true' std's sd=sqrt(diag(ihh)); %standard errors sdh=sqrt(1./diag(hh)); %diagonal standard errors for j=1:length(sd) From 7df35bca35b3ea9819bc84a2f4bc1e52fd7f54e5 Mon Sep 17 00:00:00 2001 From: Johannes Pfeifer Date: Wed, 27 Jan 2021 22:02:45 +0100 Subject: [PATCH 5/9] newrat.m: only compute eig(hh) if output is actually required --- matlab/optimization/newrat.m | 61 +++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 26 deletions(-) diff --git a/matlab/optimization/newrat.m b/matlab/optimization/newrat.m index a49d0e584..0d7ece4fe 100644 --- a/matlab/optimization/newrat.m +++ b/matlab/optimization/newrat.m @@ -70,7 +70,7 @@ nx=length(x); xparam1=x; %ftol0=1.e-6; htol_base = max(1.e-7, ftol0); -flagit=0; % mode of computation of hessian in each iteration +flagit=0; % mode of computation of hessian in each iteration; hard-coded outer-product of gradients as it performed best in tests ftol=ftol0; gtol=1.e-3; htol=htol_base; @@ -121,15 +121,16 @@ else h1=[]; end H = igg; -disp_verbose(['Gradient norm ',num2str(norm(gg))],Verbose) -ee=eig(hh); -disp_verbose(['Minimum Hessian eigenvalue ',num2str(min(ee))],Verbose) -disp_verbose(['Maximum Hessian eigenvalue ',num2str(max(ee))],Verbose) +if Verbose + disp_eigenvalues_gradient(gg,hh); +end g=gg; check=0; -if max(eig(hh))<0 - disp_verbose('Negative definite Hessian! Local maximum!',Verbose) - pause +if Verbose + if max(eig(hh))<0 + disp('Negative definite Hessian! Local maximum!') + pause + end end if Save_files save('m1.mat','x','hh','g','hhg','igg','fval0') @@ -139,7 +140,9 @@ igrad=1; igibbs=1; inx=eye(nx); jit=0; -nig=[]; +if Save_files + nig=[]; +end ig=ones(nx,1); ggx=zeros(nx,1); while norm(gg)>gtol && check==0 && jitgtol && check==0 && jitgtol && check==0 && jitgtol && check==0 && jit Date: Wed, 27 Jan 2021 22:13:15 +0100 Subject: [PATCH 6/9] mr_hessian.m: add comments and remove redundant line --- matlab/optimization/mr_hessian.m | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/matlab/optimization/mr_hessian.m b/matlab/optimization/mr_hessian.m index b7ee3c113..8e41cfccb 100644 --- a/matlab/optimization/mr_hessian.m +++ b/matlab/optimization/mr_hessian.m @@ -177,7 +177,7 @@ gg=(f1'-f_1')./(2.*hess_info.h1); if outer_product_gradient if hflag==2 - gg=(f1'-f_1')./(2.*hess_info.h1); + % full numerical Hessian hessian_mat = zeros(size(f0,1),n*n); for i=1:n if i > 1 @@ -203,6 +203,7 @@ if outer_product_gradient i=i+1; end elseif hflag==1 + % full numerical 2nd order derivs only in diagonal hessian_mat = zeros(size(f0,1),n*n); for i=1:n dum = (f1(:,i)+f_1(:,i)-2*f0)./(hess_info.h1(i)*h_1(i)); @@ -219,17 +220,19 @@ if outer_product_gradient hh_mat0=ggh'*ggh; % outer product hessian A=diag(2.*hess_info.h1); % rescaling matrix % igg=inv(hh_mat); % inverted rescaled outer product hessian - ihh=A'*(hh_mat\A); % inverted outer product hessian + ihh=A'*(hh_mat\A); % inverted outer product hessian (based on rescaling) if hflag>0 && min(eig(reshape(hessian_mat,n,n)))>0 hh0 = A*reshape(hessian_mat,n,n)*A'; %rescaled second order derivatives - hh = reshape(hessian_mat,n,n); %rescaled second order derivatives + hh = reshape(hessian_mat,n,n); %second order derivatives sd0=sqrt(diag(hh0)); %rescaled 'standard errors' using second order derivatives sd=sqrt(diag(hh_mat)); %rescaled 'standard errors' using outer product hh_mat=hh_mat./(sd*sd').*(sd0*sd0'); %rescaled inverse outer product with 'true' std's - ihh=A'*(hh_mat\A); % inverted outer product hessian + ihh=A'*(hh_mat\A); % update inverted outer product hessian with 'true' std's sd=sqrt(diag(ihh)); %standard errors sdh=sqrt(1./diag(hh)); %diagonal standard errors for j=1:length(sd) + % some heuristic normalizations of the standard errors that + % avoid numerical issues in outer product sd0(j,1)=min(prior_std(j), sd(j)); %prior std sd0(j,1)=10^(0.5*(log10(sd0(j,1))+log10(sdh(j,1)))); end From 1ca5aad981a4d1dc2fb13c9cd527eb346ad3507e Mon Sep 17 00:00:00 2001 From: Johannes Pfeifer Date: Thu, 28 Jan 2021 14:45:13 +0100 Subject: [PATCH 7/9] mr_hessian.m: account for non-scalar output --- matlab/optimization/mr_hessian.m | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/matlab/optimization/mr_hessian.m b/matlab/optimization/mr_hessian.m index 8e41cfccb..2f6299229 100644 --- a/matlab/optimization/mr_hessian.m +++ b/matlab/optimization/mr_hessian.m @@ -207,11 +207,10 @@ if outer_product_gradient hessian_mat = zeros(size(f0,1),n*n); for i=1:n dum = (f1(:,i)+f_1(:,i)-2*f0)./(hess_info.h1(i)*h_1(i)); - if dum>eps - hessian_mat(:,(i-1)*n+i)=dum; - else - hessian_mat(:,(i-1)*n+i)=max(eps, gg(i)^2); - end + hessian_mat(:,(i-1)*n+i)=dum; + if any(dum<=eps) + hessian_mat(dum<=eps,(i-1)*n+i)=max(eps, gg(i)^2); + end end end From b56b7760b11863b105c5a850e7c2938418327275 Mon Sep 17 00:00:00 2001 From: Johannes Pfeifer Date: Thu, 28 Jan 2021 15:06:32 +0100 Subject: [PATCH 8/9] mr_hessian.m: don't manually increase run index of loop --- matlab/optimization/mr_hessian.m | 2 -- 1 file changed, 2 deletions(-) diff --git a/matlab/optimization/mr_hessian.m b/matlab/optimization/mr_hessian.m index 2f6299229..a2b7c90aa 100644 --- a/matlab/optimization/mr_hessian.m +++ b/matlab/optimization/mr_hessian.m @@ -198,9 +198,7 @@ if outer_product_gradient xh1(j)=x(j); xh_1(i)=x(i); xh_1(j)=x(j); - j=j+1; end - i=i+1; end elseif hflag==1 % full numerical 2nd order derivs only in diagonal From ce56305318fcf617fc418413cdb913c00f89af78 Mon Sep 17 00:00:00 2001 From: Johannes Pfeifer Date: Thu, 28 Jan 2021 15:07:05 +0100 Subject: [PATCH 9/9] mr_hessian.m: only compute inverse of A once --- matlab/optimization/mr_hessian.m | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/matlab/optimization/mr_hessian.m b/matlab/optimization/mr_hessian.m index a2b7c90aa..1012ee2fd 100644 --- a/matlab/optimization/mr_hessian.m +++ b/matlab/optimization/mr_hessian.m @@ -233,10 +233,11 @@ if outer_product_gradient sd0(j,1)=min(prior_std(j), sd(j)); %prior std sd0(j,1)=10^(0.5*(log10(sd0(j,1))+log10(sdh(j,1)))); end + inv_A=inv(A); ihh=ihh./(sd*sd').*(sd0*sd0'); %inverse outer product with modified std's - igg=inv(A)'*ihh*inv(A); % inverted rescaled outer product hessian with modified std's - hh_mat=inv(igg); % outer product rescaled hessian with modified std's - hh_mat0=inv(A)'*hh_mat*inv(A); % outer product hessian with modified std's + igg=inv_A'*ihh*inv_A; % inverted rescaled outer product hessian with modified std's + % hh_mat=inv(igg); % outer product rescaled hessian with modified std's + hh_mat0=inv_A'/igg*inv_A; % outer product hessian with modified std's % sd0=sqrt(1./diag(hh0)); %rescaled 'standard errors' using second order derivatives % sd=sqrt(diag(igg)); %rescaled 'standard errors' using outer product % igg=igg./(sd*sd').*(sd0*sd0'); %rescaled inverse outer product with 'true' std's