Loop over parameters

Another question is, how could I save results of every loop, using the above code?
Thanks a lot!

  1. Yes, that is still the proper way to do it.
  2. info is the error code from stoch_simul. If info is not 0, the model could not be solved (for various possible reasons). It’s not necessarily the case that the model is wrong, but rather that the particular parameter draw in the loop is problematic.
  3. var_list_ will typically be created by Dynare in the first run and you do not need to set it in this case. If in your application you need to set it manually, you should be able to set it to an empty character array
var_list_ = char();
  1. That depends on what you want to save. To put the results from different runs into a cell array, you could add
results_cell{i}=oo_;

within the loop.

1 Like

3 posts were split to a new topic: Parameter loop at order=3; resol does not work

Hello

I am simulating a very simple RBC model and would like to let the constant tax rate vary. I can do this manually by just resetting the paramter every time I run the code. However, is it possible to perform this automatically? Say, I want the tax rate to increase by 0.1 every time (from 0 to 0.5).
Can I do this in my mod-file or do I have to write a m-file?

Thanks a lot,
PPilo

What is the command you want to loop over, stoch_simul?

2 posts were split to a new topic: Loop for human capital model

2 posts were merged into an existing topic: Running Dynare produces different results than a parameter loop

3 posts were split to a new topic: Running Dynare produces different results than a parameter loop

Dear Prof. Jpfeifer,

I am also learning the grid search code now. Based on your posted code, I have two command which I cannot understand.

  1. you set “first_time = 1”, what do you mean by “first_time”?
  2. you set “info = stoch_simul(var_list_)”, what do you mean by “info” and “var_list” respectively?

Maybe my questions are very stupid. I am a new learner, could please help me with it?

Thanks.

  1. This is an indicator to check whether you call the first iteration of the loop. In that case, you need to run Dynare once to initialize everything. Only after that happened, you can call set_param_value to change individual parameters.
  2. This is the function call to stoch_simul that the Dynare preprocessor usually creates when you have a stoch_simul-command in your mod-file. Here, I call the function directly. info is the error code returned by stoch_simul. If it’s not 0, then there was a problem. var_list_ is a character array containing the variable names for which you requested moments etc (the ones listed after the stoch_simul-command in your mod-file). Usually, it is already defined from the first iteration of the loop when you called Dynare as a whole.

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