From 0031309d3cae3921e13d9c20439e41e4918869a0 Mon Sep 17 00:00:00 2001 From: Johannes Pfeifer Date: Tue, 12 Mar 2024 17:48:21 +0100 Subject: [PATCH] RBC_IRF_matching.mod: remove global variables --- RBC_IRF_matching/IRF_matching_objective.m | 20 +++++++++----------- RBC_IRF_matching/RBC_IRF_matching.mod | 6 +++--- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/RBC_IRF_matching/IRF_matching_objective.m b/RBC_IRF_matching/IRF_matching_objective.m index 607424d..752200d 100644 --- a/RBC_IRF_matching/IRF_matching_objective.m +++ b/RBC_IRF_matching/IRF_matching_objective.m @@ -1,5 +1,5 @@ -function [fval, IRF_model]=IRF_matching_objective(xopt,IRF_target,weighting_matrix) -% function [fval, IRF_model]=IRF_matching_objective(xopt,IRF_target,weighting_matrix) +function [fval, IRF_model]=IRF_matching_objective(xopt,IRF_target,weighting_matrix,M_,oo_,options_) +% function [fval,IRF_model]=IRF_matching_objective(xopt,IRF_target,weighting_matrix,M_,oo_,options_) % Computes the quadratic deviation of the simulated IRFs from the empirical % ones; does so by calling stoch_simul.m from Dynare to compute IRFs % @@ -22,7 +22,7 @@ % - The empirical IRFs and model IRFs use an impulse size of 1 percent. Thus, there is no uncertainty about the % initial impact. The IRF matching therefore only targets the G-response starting in the second period. -% Copyright (C) 2017 Johannes Pfeifer +% Copyright (C) 2017-2024 Johannes Pfeifer % % This is free software: you can redistribute it and/or modify % it under the terms of the GNU General Public License as published by @@ -36,22 +36,20 @@ % % For a copy of the GNU General Public License, see . -global oo_ M_ options_ %required input structures for call to stoch_simul - %% set parameter for use in Dynare -set_param_value('root_g_1',xopt(1)); % first autoregressive root -set_param_value('root_g_2',xopt(2)); % second autoregressive root +M_.params(strmatch('root_g_1',M_.param_names,'exact'))=xopt(1); % first autoregressive root +M_.params(strmatch('root_g_2',M_.param_names,'exact'))=xopt(2); % second autoregressive root if any(xopt<=-1) || any(xopt(1)>=1) %make sure roots are between 0 and 1 - fval=10e6+sum([xopt].^2); %penalty function + fval=10e6+sum(xopt.^2); %penalty function return end var_list={'ghat','log_y'}; -[info, oo_, options_, M_] = stoch_simul(M_, options_, oo_, var_list); %run stoch_simul to generate IRFs with the options specified in the mod-file +[info, oo_] = stoch_simul(M_, options_, oo_, var_list); %run stoch_simul to generate IRFs with the options specified in the mod-file -if info %solution was not successful - fval=10e6+sum([xopt].^2); %return with penalty +if info(1) %solution was not successful + fval=10e6+sum(xopt.^2); %return with penalty else IRF_model=[oo_.irfs.ghat_eps_g' oo_.irfs.log_y_eps_g']; %select desired IRFs % compute objective function (omitting the impact response of G as that is targeted with the shock size) diff --git a/RBC_IRF_matching/RBC_IRF_matching.mod b/RBC_IRF_matching/RBC_IRF_matching.mod index 8ab6e22..18a013d 100644 --- a/RBC_IRF_matching/RBC_IRF_matching.mod +++ b/RBC_IRF_matching/RBC_IRF_matching.mod @@ -218,19 +218,19 @@ options_.noprint=1; crit = 1e-8; %Tolerance nit = 1000; %Number of iterations - [fhat,x_opt_hat] = csminwel(@IRF_matching_objective,x_start,H0,[],crit,nit,IRF_empirical,IRF_weighting); + [fhat,x_opt_hat] = csminwel(@IRF_matching_objective,x_start,H0,[],crit,nit,IRF_empirical,IRF_weighting,M_,oo_,options_); @#else %set CMAES options H0=0.2*ones(size(x_start,1),1) cmaesOptions = options_.cmaes; cmaesOptions.LBounds = [-1;-1]; cmaesOptions.UBounds = [1;1]; - [x_opt_hat, fhat, COUNTEVAL, STOPFLAG, OUT, BESTEVER] = cmaes('IRF_matching_objective',x_start,H0,cmaesOptions,IRF_empirical,IRF_weighting); + [x_opt_hat, fhat, COUNTEVAL, STOPFLAG, OUT, BESTEVER] = cmaes('IRF_matching_objective',x_start,H0,cmaesOptions,IRF_empirical,IRF_weighting,M_,oo_,options_); x_opt_hat=BESTEVER.x; @#endif %get IRFs at the optimum and plot them -[fval, IRF_model]=IRF_matching_objective(x_opt_hat,IRF_empirical,IRF_weighting); +[fval, IRF_model]=IRF_matching_objective(x_opt_hat,IRF_empirical,IRF_weighting,M_,oo_,options_); figure subplot(2,1,1)