Repeating loop with same parameters leads to different results

Dears,
I’m following discussion here and what I’m also doing with grid search is to generate expected welfare for different monetary rules setting.

Here is my example code for loop.

aaa=
    0.1 0.3
    0.1 0.3
    0.1 0.3
    0.1 0.3];
first_time = 1;
welfare=zeros(1,2);
for i1=1:2
    e=aaa(1,i1);
    f=aaa(2,i1);
    g=aaa(3,i1);
    h=aaa(4,i1);

    if first_time
save parameterfile1 e f g h; 
dynare LcpTax.mod noclearall;
       first_time = 0;
    else
set_param_value('sigma1',e);
set_param_value('sigma2',f);
set_param_value('sigmas1',g);
set_param_value('sigmas2',h);

nv=101;
eu_v=zeros(1,nv);
eus_v=zeros(1,nv);
eu_w=zeros(1,nv);

for jj=1:nv;
v=(jj-1)/(nv-1);
info=stoch_simul(var_list_);
eu_v(jj)=oo_.mean(26);
cbar=oo_.steady_state(1);
lbar=oo_.steady_state(3);
eu_v(jj)=(((eu_v(jj)*(1-beta)+eta*lbar)*(1-rho))^(1/(1-rho))/cbar-1)*100;

eus_v(jj)=oo_.mean(27);
csbar=oo_.steady_state(13);
lsbar=oo_.steady_state(15);
eus_v(jj)=(((eus_v(jj)*(1-beta)+eta*lsbar)*(1-rho))^(1/(1-rho))/csbar-1)*100;
eu_w(jj)=n*eu_v(jj)+(1-n)*eus_v(jj);

end;

My questions is:
based on the discussion, we only need to call dynare once and then change different parameter and redo the simulation. I double check the result in this example code, and find that the estimated welfare are different between calling dynare each time and the method mentioned in the previous code.
I believe that calling dynare for each loop will return the correct result, then anyone could suggest what’s the problem with my code listed above?

Just clarifying a doubt with reference to the above topic. Is looping important when we deal with endogenous collateral constraints?

I guess what’s discussed here is on changing the parameters and get moments by redoing simulations. The importance of loop is not related with model setup, which is how to deal with constraints.

@HouseC I would need to see the mod-file you are calling.
@monsoon Looping is somewhat related to how to deal with occasionally binding constraints, because solving this type of model sometimes requires iterating over different parameters/models (see Guerierri/Iacoviello’s Occbin). But there is no general fundamental relation between loops and constraints.

@HouseC Within your mod-file you use the stochastic simulations that rely on random numbers. Dynare, when called, uses

set_dynare_seed('default');
to set the seed of the random number generator. Thus, before you call

info=stoch_simul(var_list_);

you should have

set_dynare_seed('default');
to fix the seed, if this is desired.