I also need help regarding a loop over parameters in a specific case. I am not seeing anything here that responds to my problem.
I use to handle loops with linear models, but this time my model is nonlinear for which I have an analytical steady state.
I am using a steady state file for computing the steady state values. My problem is that the loop doen’t work and I guess that this is from the fact that the steady state also has to change over parameter values.
So, please how can i deal with this issue? Do I need to loop over the steady state ? How to do this?
LOOP OVER PARAMETERS WITH PROBLEMATIC PARAMETER COMBINATIONS
I share my small code chunk that allows MATLAB to continue executing a loop over parameters when there are problems related to particular parameter combinations (assuming that you are handling parameter dependence correctly). Especially for complex models, some parameter combinations might turn out to be problematic, yet the loop might remain of interest for you after the problematic combinations are ‘filtered out’. Try and catch proves extremely handy in this case:
I write a simple example with inflation, Calvo parameter \theta, and indexation parameter \chi to illustrate:
stoch_simul(order=2,periods=0,irf=0);
%make sure Dynare does not print out unnecessary output during runs
options_.nomoments=0;
options_.nofunctions=1;
options_.nograph=1;
options_.verbosity=0;
options_.noprint=1;
options_.TeX=0;
% Read out variable position
pi_pos=strmatch('pi',M_.endo_names,'exact');
theta_grid = [0:0.1:1];
chi_grid = [0:0.1:1];
for ii = 1:length(theta_grid)
for jj = 1:length(chi_grid)
set_param_value('theta',theta_grid(ii))
set_param_value('chi',chi_grid(jj))
% Set seed
set_dynare_seed('default');
try
info=stoch_simul(M_.endo_names); % loop over stoch_simul
catch
warning('Unfortunately, there seems to be a problem with this combination of parameters. Assigning a value of NaN to info.');
info = NaN;
end
if info ==0
%read out variance of inflation
variance.pi(ii,jj)=oo_.var(pi_pos,pi_pos);
else
fprintf('Here there is an error with this combination of paramters!\n');
variance.pi(ii,jj)=NaN;
end
end
end
As it proves useful in my loops, I hope it will be so for you as well
stoch_simul(order=2,periods=0,irf=0);
%make sure Dynare does not print out unnecessary output during runs
options_.nomoments=0;
options_.nofunctions=1;
options_.nograph=1;
options_.verbosity=0;
options_.noprint=1; % set noprint option to suppress error
%messages within loop (If you have this option in your
%code you need not use the try-catch statement below)
options_.TeX=0;
% Read out variable position
pi_pos=strmatch('pi',M_.endo_names,'exact');
theta_grid = [0:0.1:1];
chi_grid = [0:0.1:1];
for ii = 1:length(theta_grid)
for jj = 1:length(chi_grid)
set_param_value('theta',theta_grid(ii))
set_param_value('chi',chi_grid(jj))
% Set seed
set_dynare_seed('default'); % (If you have this option in your code you
% need not use the options_.noprint=1 statement above)
try
info=stoch_simul(M_.endo_names); % loop over stoch_simul
catch
warning('Unfortunately, there seems to be a problem with this combination of parameters. Assigning a value of NaN to info.');
info = NaN;
end
if info ==0
%read out variance of inflation
variance.pi(ii,jj)=oo_.var(pi_pos,pi_pos);
else
fprintf('Here there is an error with this combination of paramters!\n');
variance.pi(ii,jj)=NaN;
end
end
end