From 02c74460e891e334f85cf80f0944698df9121ec5 Mon Sep 17 00:00:00 2001 From: Marco Ratto <marco.ratto@ec.europa.eu> Date: Tue, 14 Feb 2023 17:22:59 +0100 Subject: [PATCH] distinguish between TolFun (optimizer termination criterion) and TolGstep/TolGstepRel, used for tuning gradient step. So, we can keep for example TolFun=1.e-5, while allowing coarser values for TolGstep. this may help when maximizing non smooth surfaces (e.g. PKF or very large models), where numerical noise may count. By default TolGstep=TolFun as in usual historical behavior. --- matlab/optimization/dynare_minimize_objective.m | 12 +++++++++++- matlab/optimization/mr_hessian.m | 9 ++++++++- matlab/optimization/newrat.m | 4 ++-- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/matlab/optimization/dynare_minimize_objective.m b/matlab/optimization/dynare_minimize_objective.m index e611bed46d..0f82b81c02 100644 --- a/matlab/optimization/dynare_minimize_objective.m +++ b/matlab/optimization/dynare_minimize_objective.m @@ -289,6 +289,8 @@ switch minimizer_algorithm end nit=options_.newrat.maxiter; epsilon = options_.gradient_epsilon; + gstep_crit = NaN; + gstep_crit_rel = NaN; robust = false; Verbose = options_.newrat.verbosity; Save_files = options_.newrat.Save_files; @@ -311,6 +313,10 @@ switch minimizer_algorithm robust = options_list{i,2}; case 'TolFun' crit = options_list{i,2}; + case 'TolGstep' + gstep_crit = options_list{i,2}; + case 'TolGstepRel' + gstep_crit_rel = options_list{i,2}; case 'verbosity' Verbose = options_list{i,2}; case 'SaveFiles' @@ -324,8 +330,12 @@ switch minimizer_algorithm Save_files = 0; Verbose = 0; end + if isnan(gstep_crit) + gstep_crit = crit; + end hess_info.gstep=options_.gstep; - hess_info.htol = 1.e-4; + hess_info.htol = gstep_crit; + hess_info.htol_rel = gstep_crit_rel; hess_info.h1=epsilon*ones(n_params,1); hess_info.robust=robust; % here we force 7th input argument (flagg) to be 0, since outer product diff --git a/matlab/optimization/mr_hessian.m b/matlab/optimization/mr_hessian.m index c17b161b67..b763b70c87 100644 --- a/matlab/optimization/mr_hessian.m +++ b/matlab/optimization/mr_hessian.m @@ -83,6 +83,11 @@ hess_info.h1 = min(hess_info.h1,0.9.*hmax); if htol0<hess_info.htol hess_info.htol=htol0; end +if not(isnan(hess_info.htol_rel)) + htol1=hess_info.htol; + hess_info.htol=abs(hess_info.htol_rel*f0); +end + xh1=x; f1=zeros(size(f0,1),n); f_1=f1; @@ -298,4 +303,6 @@ else hh1 = []; end -htol1=hhtol; +if isnan(hess_info.htol_rel) + htol1=hhtol; +end diff --git a/matlab/optimization/newrat.m b/matlab/optimization/newrat.m index 68a9898792..2106ae0e49 100644 --- a/matlab/optimization/newrat.m +++ b/matlab/optimization/newrat.m @@ -69,7 +69,7 @@ icount=0; nx=length(x); xparam1=x; %ftol0=1.e-6; -htol_base = max(1.e-7, ftol0); +htol_base = max(1.e-7, hess_info.htol); 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; @@ -232,7 +232,7 @@ while norm(gg)>gtol && check==0 && jit<nit if flagit==2 hh=hh0; elseif flagg>0 - [dum, gg, htol0, igg, hhg, h1, hess_info]=mr_hessian(xparam1,func0,penalty,flagg,ftol0,hess_info,bounds,prior_std,Save_files,varargin{:}); + [dum, gg, htol0, igg, hhg, h1, hess_info]=mr_hessian(xparam1,func0,penalty,flagg,htol_base,hess_info,bounds,prior_std,Save_files,varargin{:}); if flagg==2 hh = reshape(dum,nx,nx); ee=eig(hh); -- GitLab