Ways to quickly loop over multiple parameters

Dear all,

I met a problem that there is no the same one on the forum, so I here ask for help :sob:
Thanks everyone in advance!

I posted a topic on the forum asking about how to loop over parameters to find out the maximized conditional welfare: Loop over parameters to find maximized welfare

Following the suggestion there, I wrote my .m file as below to find out my maximized conditional welfare when I tried to loop over more than one parameter.

clear all

dynare FINAL_HDS.mod nostrict noclearall;
pMHy_grid = 0.02: 0.01: 0.05;
pMHq_grid = 0.01: 0.01: 0.02;

%welfare_pos = strmatch('welfare',var_list_,'exact');
welfare_pos = strmatch('welfare',M_.endo_names,'exact');
Vp_pos = strmatch('V_p',M_.endo_names,'exact');
Vi_pos = strmatch('V_i',M_.endo_names,'exact');
Vr_pos = strmatch('V_r',M_.endo_names,'exact');
Ve_pos = strmatch('V_e',M_.endo_names,'exact');

welfare_cell = -inf*ones(length(pMHy_grid) , length(pMHq_grid));

max_welfare_value = -inf;
l_max = 0;
m_max = 0;

for l = 1:length(pMHy_grid)         

    set_param_value('pMHy', pMHy_grid(l))    
for m = 1:length(pMHq_grid)

    set_param_value('pMHq', pMHq_grid(m))
    info = stoch_simul(var_list_);

    if info == 0

        welfare_cell(l,m) = oo_.dr.ys(welfare_pos)+0.5*oo_.dr.ghs2(oo_.dr.inv_order_var(welfare_pos));
        if welfare_cell(l,m) > max_welfare_value

            max_welfare_value = welfare_cell(l,m);
            max_Vp = oo_.dr.ys(Vp_pos)+0.5*oo_.dr.ghs2(oo_.dr.inv_order_var(Vp_pos));
            max_Vi = oo_.dr.ys(Vi_pos)+0.5*oo_.dr.ghs2(oo_.dr.inv_order_var(Vi_pos));
            max_Vr = oo_.dr.ys(Vr_pos)+0.5*oo_.dr.ghs2(oo_.dr.inv_order_var(Vr_pos));
            max_Ve = oo_.dr.ys(Ve_pos)+0.5*oo_.dr.ghs2(oo_.dr.inv_order_var(Ve_pos));
            l_max = l;
            m_max = m;

max_set = [pMHy_grid(l_max), pMHq_grid(m_max)];
save welfare_MA.mat;

Where I generate 4 variables such as Vi_pos in addition to welfare_pos in order to capture the corresponding maximized individual welfare.

The problems I encountered are as follow:

  1. welfare_cell might give a totally identical result for every cell in the lxm matrix in some cases!
    This sounds incredible but it really happened!
    Does it mean that I in fact didn’t reset parameter in every loop?

  2. After running the .m file, the max_welfare_value and all max_Vi might gave me a positive and incredibly large number such as 20000 or even bigger one.

    Is there any wrong of my .m file for looping over parameters?
    Or may there something wrong in my model setting e.g. the definition of welfare? Or a unreasonable grid range?

  3. This .m file really really takes time!

    If I try to set a large grid range with a small gap, which means that there is a lot of value for every parameter to loop, it might take about 48 hours or even longer! :confounded:

    Is there any way to speed up the loop??

I have no idea to fix my code…and I would be thankful for any help.

FINAL_HDS.mod (10.8 KB)
FINAL_HDS_steadystate.m (6.5 KB)
welfare_MA.m (1.6 KB)

1 Like


I have solved the first question!

Now leave the latter two to be solved! I still met the same second problem when I expand my grid range… :sob:

Is it possible that individual or aggregate welfare is positive?
If not, how if I add some restrictions to prevent such situation? Break the loop and go to the next grid when one of these welfare values is bigger than 0?


  1. Make sure you are handling parameter dependence correctly.
  2. You can verify that the uncertainty correction is really large. That may explain the large values.
  3. Shut off the IRF generation. That should speed up things. Also try using a steady_state_model-block instead of a separate file
1 Like

Prof. Jpfeifer,

Thanks a lot for your reply! I have solved the first problem! It exactly resulted from parameter dependence since I wrote a identical grid name…

But I still don’t understand what you exactly meant about the second one…

Do you mean that my oo_.dr.ghs2(oo_.dr.inv_order_var(welfare_pos)) is really large?
Why is it so large? Is my model structure about shocks wrong?
I list my shock setting as below:

Y = exp(A_z)*(k_p(-1)^(alppha*(1-mu)))*(k_e(-1)^(alppha*mu))*(h_e(-1)^nu)*(N_p^((1-alppha-nu)*sigma1))*(N_i^((1-alppha-nu)*sigma2))*(N_r^((1-alppha-nu)*(1-sigma1-sigma2)));
A_z = rho_az*A_z(-1) + eps_z ;
var eps_z; stderr sigma_Z ;

rho_az = 0.990219844054634;     
sigma_Z = 0.00699646242063357;  

The parameters listed above are stem from the estimation result of others’ study since I didn’t do my own estimation.

Is the form wrong? Or do the parameters contribute to the large uncertainty correction?

How could I fix it? :sob:

I am looking forward to your reply. Thank you sooooooooooooo much!

Yes, that is exactly what I meant. The big problem is that the numbers are very hard to interpret as they are in utils, i.e. units of utility. It is more conventional to express everything in percent of steady state consumption.

dear every
this my first reply

i am from china ,

study dsge this year .

dear jpfeifer is dynare team Prof?

my english name: daniel tulips liu