Loop over parameters

Dear Pfeifer,

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?

Thanks!

Sanga

I am not sure I can follow. Whenever you loop over resol, the steady state is recomputed. What is the error message you get?

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 :slight_smile:

1 Like

@cmarch Given that you use

options_.noprint=1;

it will never happen that stoch_simul throws out and error. So your try-catch-statement is redundant.

Good morning Prof. Pfeifer,

That is correct. I correct the code chunk. :slightly_smiling_face:

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
1 Like

4 posts were merged into an existing topic: Adaptive Learning Loop

6 posts were split to a new topic: Parameter loop does not correctly update results

2 posts were split to a new topic: Adaptive Learning Loop

10 posts were split to a new topic: Parameter to loop to retrieve standard deviations of endogenous variables

6 posts were split to a new topic: Parameter loop in open economy model

5 posts were split to a new topic: Loop to get welfare

12 posts were split to a new topic: Loop with updating of non-diagonal covariance matrix

4 posts were split to a new topic: Parameter loop in Dynare 5.x