Running a loop on a parameter

Dear Prof. Pfeifer,
Forum loop.zip (1.5 MB)

I would like to run the transition.mod file for different values of the parameter ‘phiP’. As you will see in the mod file, all the parameters are set from calibration, steadyInitial and steadyFinal.

However, for phiP I would like to run transition.mod for different values of it, and get it to save the value of welfare (calculated in welfare.m) for each iteration. I tried the following as a starting point based on one of your examples, I get the error ‘Struct contents reference from a non-struct array object.’

phi = 0:0.5:1.05;
first_time = 1;
for i=1:length(phi)
if first_time
set_param_value(‘phiP’,phi(i));
dynare transition.mod noclearall;
first_time = 0;
else
set_param_value(‘phiP’,phi(i));
info = stoch_simul(var_list_);
if info;
disp(['Computation fails for phiP = ’ num2str(phiP)]);
end;
end
end

In order to run transition.mod (where phiP is just set as a parameter saved in calibration.mat), you can do this by running run.m where the options have been set so that it will run transition.mod.

I would really appreciate your help on this issue. Many thanks in advance.

You cannot call
set_param_value('phiP',phi(i));
before the first call to Dynare, because the model has not been set up yet. You should set phiP to the first value within the mod-file.

Dear Prof. Pfeifer,

As I am a truly beginner, I have a basic problem: loop over parameters. I know that this problem was discussed many times since I found some topics connected with it, but I didn’t find any solution for my problem.
I combined two model: DSGE and human capital model. The model’s most important parameter is named by „x”: x=private investment into human capital/total investment into human capital, 1-x=government investment into human capital/ total investment into human capital. I would like to let that „x” parameter vary.
I’ve already tried the given .m code, but it does not work properly (for example it could not recognise „var_list_”, or I get the following error: „Index exceeds matrix dimensions”). What should I write after stoc_simul (or how should I write my variables after stoch_simul)? Or has my Dynare file another problem?
I would be thankful for any help.
Dora
dsge_humancapital.m (417 Bytes)
dsge_humancapital.mod (1.1 KB)

Dear Prof. Pfeifer,

Many thanks for your reply. I am still experiencing further problems.

I have set the value of the parameter ‘phiP’ in the calibration mat file, instead of the mod file, because it is used in one of the m files (newsteady), so if I don’t do this I get an error.

I have also included the code for running the loop in the run.m file (lines 82-97). I changed the line in the code ‘info = stoch_simul(var_list_);’ to info = simul(var_list_); I am not sure whether this is correct. I did this because in the transition.mod file, I use the ‘simul’ command and not ‘stoch_simul’. When I run the code, I get the following error:

Undefined function or variable ‘var_list_’.

Error in run (line 91)
info = simul(var_list_);

Just to summarize my queries:

  1. Should I replace ‘stoch_simul(var_list_)’ with some other command in the code for running the loop, given that I am working with a perfect foresight model, and what should this be?
  2. How to I deal with the error I get when I run the run.m file telling me that the variable ‘var_list_’ is undefined.

Please find the updated codes attached.

Many thanks in advance.Forum loop2.zip (1.5 MB)

In Dynare 4.5, if you do perfect foresight simulations, you need to loop over

perfect_foresight_setup
perfect_foresight_solver

instead of stoch_simul. Both functions do not take input arguments. Note that the error code is now not info~=0, but

oo_.deterministic_simulation.status ~= 1

Dear Prof. Pfeifer,

Many thanks, but I am still experiencing problems. I tried the following based on your suggestions but I am not sure what I did was entirely correct as I still get an error message.

dynare transition nostrict
phi = 0:1:3;
first_time = 1;
for i=1:length(phi)
if first_time
set_param_value(‘phiP’,phi(i));
dynare transition nostrict;
first_time = 0;
else
set_param_value(‘phiP’,phi(i));
info = perfect_foresight_setup;
info = perfect_foresight_solver;
if oo_.deterministic_simulation.status ~= 1;
disp([‘Computation fails for phiP =’ num2str(phiP)]);
end;
end
end

I get the following error:

Error using perfect_foresight_setup
Too many output arguments.

Error in run (line 92)
info = perfect_foresight_setup;

Can you please help with what the issue might be. Many thanks

It should be

perfect_foresight_setup;
perfect_foresight_solver;

These functions have no output argument

Many thanks Prof. Pfeifer! It works.

One other thing. I would like to run welfare.m for each value of phiP and save the calculations for ‘Cons_Equivalent’ for all iterations (in a matrix), but I am not sure how to go about it. I tried including the following code after ‘for i=1:length(phi)’:

       welfare
       save welfphi Cons_Equivalent

but I think this only saves Cons_Equivalent for the first iteration. Can you please help on how to go about it? Many thanks

That way you will always overwrite the last value. You need to condition the storage on the index variable i, something like

welfare_vector(i)=Cons_Equivalent

if Cons_Equivalent is a scalar storing the consumption equivalent for the current value of phi

Thank you so much Prof. Pfeifer!

Dear Prof. Pfeifer,

I am experiencing some issues with running the loop with the code above.

So, the loop is being run over the parameter ‘phiP’ which appears in calibration.m (where its starting value is saved). It also appears in run.m where the looping code appears (from line 82).

The issue is that the values in the welfare_vector are different for the same value of phiP when the initial value of phiP is changed. To be clear, if I set the initial value of phiP to -10 in calibration.m, the corresponding Cons_Equivalent value for phiP=-6 (third column in welfare_vector) is different from the value I get if I set the initial value of phiP to - 6 (which will now be the first column of welfare_vector).

This should not be. The value for Cons_Equivalent for phiP=-6 should be the same regardless of the starting point, but I can’t work out where things go wrong.

I would greatly appreciate your help on this issue. Please find the files attached. Please use the rum.m file to run the code. And the initial value of phiP is set in calibration.m (line 11).

Many thanks in advance. Forum loop3.zip (1.5 MB)

What will be wrong is the first value of the loop. The reason is that you are executing

               set_param_value('phiP',phi(i));
               dynare transition nostrict;

Within your mod-file, you load the file calibration.mat, which will overwrite any value you set before.

Dear Prof. Pfeifer,

Many thanks for your reply. I have included some code in calibration.m (lines 14-23) to try to mimic the loop in run.m but have been unable to get it to run properly.

Also, I think I may have partly pinpointed the issue. it seems, based on the values I get in welfare_vector when I run run.m, that the starting value if phiP is always zero, regardless of the values I specify at the start of the code (line 82). I think this may be linked to:

first_time = 0; (line 90)

in the code but I’m not certain.

Could you please tell me how to fix this issue, so I can get a welfare_vector which reflects the phiP vector accurately? Please find the updated code attached.

Many thanks in advance.

Forum loop4.zip (1.5 MB)