# Save results from stoch\_simul() in a Loop

**URL:** <https://forum.dynare.org/t/save-results-from-stoch-simul-in-a-loop/17212>\
**Category:** Stochastic simulations\
**Created:** [30 December 2020 02:05 UTC](https://forum.dynare.org/t/save-results-from-stoch-simul-in-a-loop/17212 "2020-12-30T02:05:26Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ecofai](https://forum.dynare.org/letter_avatar_proxy/v4/letter/e/f14d63/32.png) [@Ecofai](https://forum.dynare.org/u/Ecofai)\
**Post date:** [30 December 2020 02:05 UTC](https://forum.dynare.org/t/save-results-from-stoch-simul-in-a-loop/17212/1 "2020-12-30T02:05:26Z")

</div>

Hi all;  
I use the code below to loop over a parameter. However, in each iteration I end up with the same value in the vector std\_y. How can I fix this problem?  
I use the Dynare 4.6.3 version.

Best regards,

```
> std_y=[];
> rho = 0.5:0.05:1;
> first_time = 1;
> for ii=1:length(rho)
> if first_time
> dynare model noclearall;
> set_param_value('rho_a',rho(ii));
> first_time = 0;
> std_y(ii)=sqrt(oo_.var(1));
> else
> set_param_value('rho_a',rho(ii));
> info = stoch_simul(M_, options_, oo_, var_list_);
> std_y(ii)= sqrt(oo_.var(1));
> if info
> disp('Computation fails for rho = ', num2str(rho_a));
> end
> end
> end
```

---

<div class="post-metadata">

**Author:** ![jpfeifer](https://forum.dynare.org/user_avatar/forum.dynare.org/jpfeifer/32/5044_2.png) [@jpfeifer](https://forum.dynare.org/u/jpfeifer)\
**Post date:** [30 December 2020 08:47 UTC](https://forum.dynare.org/t/save-results-from-stoch-simul-in-a-loop/17212/2 "2020-12-30T08:47:09Z")

</div>

Without the full codes, it is impossible to tell

---

<div class="post-metadata">

**Author:** ![Ecofai](https://forum.dynare.org/letter_avatar_proxy/v4/letter/e/f14d63/32.png) [@Ecofai](https://forum.dynare.org/u/Ecofai)\
**Post date:** [30 December 2020 11:23 UTC](https://forum.dynare.org/t/save-results-from-stoch-simul-in-a-loop/17212/3 "2020-12-30T11:23:41Z")

</div>

Thank you for the reply, here are the files.

[mymodel.mod](https://forum.dynare.org/uploads/short-url/4uXrd8qFtn5RDjomaPZmYHAGSlF.mod) (471 Bytes) [test.m](https://forum.dynare.org/uploads/short-url/37sHMiz6EM1QgkhPBl0SVmOAtEM.m) (593 Bytes)

---

<div class="post-metadata">

**Author:** ![Max1](https://forum.dynare.org/letter_avatar_proxy/v4/letter/m/e99b99/32.png) [@Max1](https://forum.dynare.org/u/Max1)\
**Post date:** [30 December 2020 13:54 UTC](https://forum.dynare.org/t/save-results-from-stoch-simul-in-a-loop/17212/4 "2020-12-30T13:54:09Z")

</div>

As far as I can see there is no issue with your .m-file. [test.m](https://forum.dynare.org/uploads/short-url/3ZCGOhdugk1ldOwBXO2IRNmiz8Y.m) (1007 Bytes)  
You may like to use the resol command instead of stoch\_simul.

The AR(1) parameter is updated in each iteration, but due to the model structure and the very small variance of the innovation the impact on the variance of output seems to be negligible.

You may double check whether your implementation of the TFP shock is correct.

---

<div class="post-metadata">

**Author:** ![Ecofai](https://forum.dynare.org/letter_avatar_proxy/v4/letter/e/f14d63/32.png) [@Ecofai](https://forum.dynare.org/u/Ecofai)\
**Post date:** [30 December 2020 14:52 UTC](https://forum.dynare.org/t/save-results-from-stoch-simul-in-a-loop/17212/5 "2020-12-30T14:52:53Z")

</div>

@Max1 Thanks for your reply,  
1- Using resol() instead of stoch\_simul() doesn’t solve the problem.  
2- I disagree, the problem is not coming from the variance of the innovation, you can check the vector y\_std and you’ll see that the same value is repeated in each iteration. Everything happen as if the only value reported in y\_std in each iteration is the one coming from the first iteration.  
3- I think my implementation of the TFP is correct since running the model outside the loop everything seems to work perfectly.

---

<div class="post-metadata">

**Author:** ![Ecofai](https://forum.dynare.org/letter_avatar_proxy/v4/letter/e/f14d63/32.png) [@Ecofai](https://forum.dynare.org/u/Ecofai)\
**Post date:** [30 December 2020 15:14 UTC](https://forum.dynare.org/t/save-results-from-stoch-simul-in-a-loop/17212/6 "2020-12-30T15:14:04Z")

</div>

I run the code on Dynare 4.5.7 version, with a slight change of  
`info = stoch_simul(M_, options_, oo_, var_list_)`  
to  
`info = stoch_simul(var_list_).`  
And I got the values I expected for std\_y (no repeated value anymore).

My question now is why the code doesn’t run properly on 4.6.3 version ? What changes should I make ?

---

<div class="post-metadata">

**Author:** ![Max1](https://forum.dynare.org/letter_avatar_proxy/v4/letter/m/e99b99/32.png) [@Max1](https://forum.dynare.org/u/Max1)\
**Post date:** [30 December 2020 15:55 UTC](https://forum.dynare.org/t/save-results-from-stoch-simul-in-a-loop/17212/7 "2020-12-30T15:55:17Z")

</div>

[test.m](https://forum.dynare.org/uploads/short-url/ynjzWwsb9gOpUOpLP3QizC38uXI.m) (1.1 KB)  
I disagree, if I use stoch\_simul under Dynare 4.6.3 it works as expected.

@jpfeifer is resol not updating `oo_.var` ?

---

<div class="post-metadata">

**Author:** ![jpfeifer](https://forum.dynare.org/user_avatar/forum.dynare.org/jpfeifer/32/5044_2.png) [@jpfeifer](https://forum.dynare.org/u/jpfeifer)\
**Post date:** [30 December 2020 16:20 UTC](https://forum.dynare.org/t/save-results-from-stoch-simul-in-a-loop/17212/8 "2020-12-30T16:20:49Z")

</div>

1. `resol` does not compute variances. You need to call `stoch_simul`.
2. When calling `stoch_simul` in Dynare 4.6.3. you do not just have to alter the inputs, but also the outputs:

```auto
addpath c:\dynare\4.6.3\matlab
clear all
close all
clc
std_y=[];
rho = 0.5:0.05:1;
first_time = 1;
%dynare mymodel
for ii=1:length(rho)
    if first_time      
        dynare mymodel noclearall;
        set_param_value('rho_a',rho(ii));
        first_time = 0;
        std_y(ii)=sqrt(oo_.var(1));
    else
        set_param_value('rho_a',rho(ii));
        [info, oo_, options_, M_] = stoch_simul(M_, options_, oo_, var_list_);
        std_y(ii)= sqrt(oo_.var(1));
        if info
          disp('Computation fails for rho = ', num2str(rho_a));
        end
    end
end

plot(rho,std_y)

```

The reason you are getting the same result all of the time is that you do not request `oo_` as an output.

---

<div class="post-metadata">

**Author:** ![Max1](https://forum.dynare.org/letter_avatar_proxy/v4/letter/m/e99b99/32.png) [@Max1](https://forum.dynare.org/u/Max1)\
**Post date:** [30 December 2020 16:32 UTC](https://forum.dynare.org/t/save-results-from-stoch-simul-in-a-loop/17212/9 "2020-12-30T16:32:40Z")

</div>

@jpfeifer Thanks for making this clear. `resol` only updates the decision rule and hence the steady state but not the moments.

@Ecofai Please take into consideration that the (theoretical) variances will not be computed under a unit root (if. rho =1). Hence, the last entry in std\_y will be NaN, if you don’t use the periods option.

---

<div class="post-metadata">

**Author:** ![Ecofai](https://forum.dynare.org/letter_avatar_proxy/v4/letter/e/f14d63/32.png) [@Ecofai](https://forum.dynare.org/u/Ecofai)\
**Post date:** [30 December 2020 22:08 UTC](https://forum.dynare.org/t/save-results-from-stoch-simul-in-a-loop/17212/10 "2020-12-30T22:08:28Z")

</div>

Everything is clear know. Thanks to both of you @jpfeifer @Max1 .
