From c7b47c349d052b622ef2f450b703eccf074a4465 Mon Sep 17 00:00:00 2001 From: Marco Ratto <marco.ratto@ec.europa.eu> Date: Thu, 14 Dec 2023 19:08:26 +0100 Subject: [PATCH] initialize robust to false and fixed indentation of mr_gstep.m --- matlab/default_option_values.m | 2 +- matlab/optimization/mr_gstep.m | 80 +++++++++++++++++----------------- 2 files changed, 41 insertions(+), 41 deletions(-) diff --git a/matlab/default_option_values.m b/matlab/default_option_values.m index 87721dfad9..2ca5507d4e 100644 --- a/matlab/default_option_values.m +++ b/matlab/default_option_values.m @@ -564,7 +564,7 @@ options_.csminwel=csminwel; %newrat optimization routine newrat.hess=1; % dynare numerical hessian -newrat.robust=0; +newrat.robust=false; newrat.tolerance.f=1e-5; newrat.tolerance.f_analytic=1e-7; newrat.tolerance.gstep = NaN; diff --git a/matlab/optimization/mr_gstep.m b/matlab/optimization/mr_gstep.m index 02ff7ac529..a119dee390 100644 --- a/matlab/optimization/mr_gstep.m +++ b/matlab/optimization/mr_gstep.m @@ -75,11 +75,11 @@ while i<n if gg(i)*(hh(i)*gg(i))/2 > htol(i) [ff, xx,fcount,retcode] = csminit1(func0,x,penalty,f0,gg,0,diag(hh),Verbose,varargin{:}); if retcode && robust -% do_nothing=true; + if abs(x(i))<1.e-6 - xa=transpose(linspace(x(i)/2, sign(x(i))*1.e-6*3/2, 7)); + xa=transpose(linspace(x(i)/2, sign(x(i))*1.e-6*3/2, 7)); else - xa=transpose(linspace(x(i)/2, x(i)*3/2, 7)); + xa=transpose(linspace(x(i)/2, x(i)*3/2, 7)); end for k=1:7 xh1(i)=xa(k); @@ -100,48 +100,48 @@ while i<n end ig(i)=1; if robust - if not(isequal(xx , check_bounds(xx,bounds))) - xx = check_bounds(xx,bounds); - if xx(i)<x(i) - % lower bound - xx(i) = min(xx(i)+h1(i), 0.5*(xx(i)+x(i))); + if not(isequal(xx , check_bounds(xx,bounds))) + xx = check_bounds(xx,bounds); + if xx(i)<x(i) + % lower bound + xx(i) = min(xx(i)+h1(i), 0.5*(xx(i)+x(i))); + else + % upper bound + xx(i) = max(xx(i)-h1(i), 0.5*(xx(i)+x(i))); + end + [ff,exit_flag]=penalty_objective_function(xx,func0,penalty,varargin{:}); + if exit_flag~=1 + disp('last step exited with bad status!') + elseif ff<f0 + f0=ff; + x=xx; + end else - % upper bound - xx(i) = max(xx(i)-h1(i), 0.5*(xx(i)+x(i))); - end - [ff,exit_flag]=penalty_objective_function(xx,func0,penalty,varargin{:}); - if exit_flag~=1 - disp('last step exited with bad status!') - elseif ff<f0 - f0=ff; - x=xx; - end - else - % check improvement wrt predicted one - if abs(f0-ff) < abs(gg(i)*(hh(i)*gg(i))/2/100) || abs(x(i)-xx(i))<1.e-10 - [ff1, xx1, fcount, retcode] = csminit1(func0,x,penalty,f0,-gg,0,diag(hh),Verbose,varargin{:}); - if not(isequal(xx1 , check_bounds(xx1,bounds))) - xx1 = check_bounds(xx1,bounds); - if xx1(i)<x(i) - % lower bound - xx1(i) = min(xx1(i)+h1(i), 0.5*(xx1(i)+x(i))); - else - % upper bound - xx1(i) = max(xx1(i)-h1(i), 0.5*(xx1(i)+x(i))); + % check improvement wrt predicted one + if abs(f0-ff) < abs(gg(i)*(hh(i)*gg(i))/2/100) || abs(x(i)-xx(i))<1.e-10 + [ff1, xx1, fcount, retcode] = csminit1(func0,x,penalty,f0,-gg,0,diag(hh),Verbose,varargin{:}); + if not(isequal(xx1 , check_bounds(xx1,bounds))) + xx1 = check_bounds(xx1,bounds); + if xx1(i)<x(i) + % lower bound + xx1(i) = min(xx1(i)+h1(i), 0.5*(xx1(i)+x(i))); + else + % upper bound + xx1(i) = max(xx1(i)-h1(i), 0.5*(xx1(i)+x(i))); + end + [ff1,exit_flag]=penalty_objective_function(xx1,func0,penalty,varargin{:}); + if exit_flag~=1 + disp('last step exited with bad status!') + end end - [ff1,exit_flag]=penalty_objective_function(xx1,func0,penalty,varargin{:}); - if exit_flag~=1 - disp('last step exited with bad status!') + if ff1<ff + ff=ff1; + xx=xx1; end end - if ff1<ff - ff=ff1; - xx=xx1; - end + f0=ff; + x=xx; end - f0=ff; - x=xx; - end else f0=ff; x=xx; -- GitLab