Dear all,

I know this topic has already been discussed but I still cannot find a solution.

I am solving a simple 4 equations model with perfect foresight and I am trying to loop over three possible values of two deterministic shocks hitting the economy in periods 1 and 2 (totally 3^4=81 iterations). Within the loop I try to update the values by overriding `oo_.exo_simul`

and I am trying to save the path of output taking it from `oo_.endo_simul`

. However, I see that I get the same results for each iterations meaning that the values of the shocks are not updated.

Could you please help me with this?

Thanks

RBC_perfect_foresight.mod (1.1 KB)

USe

```
var y c k h;
varexo e u;
parameters beta rho alpha delta theta psi tau;
alpha = 0.36;
rho = 0.95;
tau = 0.025;
beta = 0.99;
delta = 0.025;
psi = 0;
theta = 2.95;
model;
c*theta*h^(1+psi)=(1-alpha)*y;
k = beta*(((exp(u)*c)/(exp(u(+1))*c(+1)))
*(exp(u(+1))*alpha*y(+1)+(1-delta)*k));
y = exp(e)*(k(-1)^alpha)*(h^(1-alpha));
k = exp(u)*(y-c)+(1-delta)*k(-1);
end;
initval;
y = 1.08068253095672;
c = 0.80359242014163;
h = 0.29175631001732;
k = 11.08360443260358;
e = 0;
u = 0;
end;
steady;
check;
shocks;
var e;
periods 1 2;
values 0.5 0.25;
var u;
periods 1 2;
values 0.5 0.25;
end;
ee1g = 0.1:0.1:0.3;
ee2g = 0.1:0.1:0.3;
uu1g = 0.1:0.1:0.3;
uu2g = 0.1:0.1:0.3;
iterations = length(ee1g)*length(ee2g)*length(uu1g)*length(uu2g);
results = zeros(iterations,152);
numit = 0;
perfect_foresight_setup(periods=150);
perfect_foresight_solver;
for i=1:length(ee1g)
for j=1:length(ee2g)
for q=1:length(uu1g)
for z=1:length(uu2g)
numit = numit + 1;
oo_.exo_simul(2,1) = ee1g(i);
oo_.exo_simul(3,1) = ee2g(j);
oo_.exo_simul(2,2) = uu1g(q);
oo_.exo_simul(3,2) = uu2g(z);
oo_ = perfect_foresight_solver_core(M_,options_,oo_);
if oo_.deterministic_simulation.status
results(numit,:) = oo_.endo_simul(1,:);
end
end
end
end
end
```

1 Like

Thanks a lot for your help. If I run this code, though, the matrix containing the results is populated by zeros only. Does this not happen if you run the code?

Sorry, my mistake. The `~ `

was wrong. I fixed the code above.

Can I ask you what these two lines of code are supposed to do?

```
if oo_.deterministic_simulation.status
results(numit,:) = oo_.endo_simul(1,:);
end
```

Because if I try to apply the same code to a slightly more complicated model this prevents Matlab to populate the matrix results (all zeros).

Thanks

This is a check whether the solver was successful. If the `status`

evaluates to 0, the solver failed and you would be populating the matrix based on the solution of the last successful run.

I understand, thanks. Therefore I was getting all zeros because the solver was failing every time. Now it seems that by using the original values of the shocks as first iteration the loop seems to work.