# Help looping over two parameters

**URL:** <https://forum.dynare.org/t/help-looping-over-two-parameters/4637>\
**Category:** Stochastic simulations\
**Created:** [19 April 2015 04:19 UTC](https://forum.dynare.org/t/help-looping-over-two-parameters/4637 "2015-04-19T04:19:09Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![hakeru7](https://forum.dynare.org/letter_avatar_proxy/v4/letter/h/e19adc/32.png) [@hakeru7](https://forum.dynare.org/u/hakeru7)\
**Post date:** [19 April 2015 04:19 UTC](https://forum.dynare.org/t/help-looping-over-two-parameters/4637/1 "2015-04-19T04:19:09Z")

</div>

Hi all,

I’m trying to loop over two parameters by first running dynare and then looping over the stoch\_simul command. Before each stoch\_simul loop, I load in two new parameters.

For some reason, there are no errors and matlab takes into both new parameters each time, but I can see from the results that only one of the parameter change is being taken into account (ppsipi). The other parameter (ppibar) is being updated, but the I keep getting the same results implying that it is in fact not being updated…

Any help would be greatly appreciated.

Jae

---

<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:** [19 April 2015 09:03 UTC](https://forum.dynare.org/t/help-looping-over-two-parameters/4637/2 "2015-04-19T09:03:07Z")

</div>

You are not taking parameter dependence into account. That’s why you should use model-local variables (the ones with the pound operator) or a steady state file. See Remark 4 (Parameter dependence and the use of model-local variables) in Pfeifer (2013): [“A Guide to Specifying Observation Equations for the Estimation of DSGE Models”](https://drive.google.com/file/d/1r89OU5OE3CBa6tOlj6l3hNVWEaRH5Anv/view?usp=sharing)

---

<div class="post-metadata">

**Author:** ![hakeru7](https://forum.dynare.org/letter_avatar_proxy/v4/letter/h/e19adc/32.png) [@hakeru7](https://forum.dynare.org/u/hakeru7)\
**Post date:** [19 April 2015 17:13 UTC](https://forum.dynare.org/t/help-looping-over-two-parameters/4637/3 "2015-04-19T17:13:28Z")

</div>

Thanks for the reply Johannes.

Just trying to understand what dynare does…in my loop I update my parameter of choice, then run an m-file that re-defines the rest of the parameters based on the changes and saves it, then dynare runs my mod file which loads and sets all the parameters each time.

Why does this logic not work if I am re-uploading the whole set of parameters each time…? Is because I am running stoch\_simul instead of the whole dynare?

---

<div class="post-metadata">

**Author:** ![hakeru7](https://forum.dynare.org/letter_avatar_proxy/v4/letter/h/e19adc/32.png) [@hakeru7](https://forum.dynare.org/u/hakeru7)\
**Post date:** [19 April 2015 17:26 UTC](https://forum.dynare.org/t/help-looping-over-two-parameters/4637/4 "2015-04-19T17:26:35Z")

</div>

Also, I tried following your suggestion of creating a steady state file. I think I’m struggling to wrap my head around how to do this when I recall steady state values in my model block (like in my Taylor rule).

I’m using the attached version and getting a “steadystate file did not compute the steadystate” error…  
[MSDSGE\_level\_loop\_short\_steadystate.m](https://forum.dynare.org/uploads/default/original/2X/9/9bedb8524c41d180910867af6ce8067c0d14b91d.m) (1.9 KB)

---

<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:** [20 April 2015 21:25 UTC](https://forum.dynare.org/t/help-looping-over-two-parameters/4637/5 "2015-04-20T21:25:02Z")

</div>

Where do you all get these old, outdated, and error-prone steady state files from? Its structure is deprecated since ancient times and an up-to-date example is delivered with every Dynare installation in the examples folder (NK\_baseline\_steady\_state.m). But in your case, this is unnecessary as shown below.

To your question. The problem is the following. You have e.g.

```auto
parameters eeta Y_SS
```

and

`set_param_value('eeta',eeta);
...
// Analytic Steady State
Y_SS = ((eeta-1)/eeta)^(1/ttau)*gbar;
`  
Obviously, the parameter Y\_SS depends on eeta. But when you change eeta, you do not automatically change Y\_SS with it. This is why you should use model-local variables or a steady state file to handle this dependence. In your case, I would recommend using a simple steady\_state\_model block, which automatically creates the corresponding steady state file. You simply need to move the I attach the corresponding file.

Lastly, one remark: you are using a linearized model. order=2 will be useless in this case and welfare evaluations usually wrong.  
[MSDSGE\_level\_loop\_short.mod](https://forum.dynare.org/uploads/default/original/2X/4/482d8e93ba5d22e5a91676baa75d5cb38aafa0b7.mod) (4.24 KB)

---

<div class="post-metadata">

**Author:** ![hakeru7](https://forum.dynare.org/letter_avatar_proxy/v4/letter/h/e19adc/32.png) [@hakeru7](https://forum.dynare.org/u/hakeru7)\
**Post date:** [21 April 2015 15:39 UTC](https://forum.dynare.org/t/help-looping-over-two-parameters/4637/6 "2015-04-21T15:39:46Z")

</div>

thanks again for your reply. I didn’t realize these steady state files were outdated. I got them from Chris Sim’s lecture notes.

Would you be able to elaborate on your last remark there? What do you mean by my using a linearized model and order=2 being useless? I did want to do welfare evaluations eventually.

---

<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:** [21 April 2015 16:43 UTC](https://forum.dynare.org/t/help-looping-over-two-parameters/4637/7 "2015-04-21T16:43:28Z")

</div>

You cannot do welfare computations on a purely linearized model. You should use a second order approximation. At a minimum, if certain conditions are met (see Woodford (2002)), a linear-quadratic approach might be sufficient.

---

<div class="post-metadata">

**Author:** ![hakeru7](https://forum.dynare.org/letter_avatar_proxy/v4/letter/h/e19adc/32.png) [@hakeru7](https://forum.dynare.org/u/hakeru7)\
**Post date:** [21 April 2015 17:19 UTC](https://forum.dynare.org/t/help-looping-over-two-parameters/4637/8 "2015-04-21T17:19:50Z")

</div>

Sorry Johannes, I don’t think I follow.

I thought with order=2 dynare is doing a second order approximation already. It was my understanding that inserting a welfare function into the model and using order=2 to do welfare analysis is common practice.

Am I missing something here? what is the difference between dynare order=2 and your suggestion of “use a second order approximation”?

---

<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:** [21 April 2015 17:30 UTC](https://forum.dynare.org/t/help-looping-over-two-parameters/4637/9 "2015-04-21T17:30:46Z")

</div>

If your model is already linearized, a second order approximation of these equations will be equal to a linear approximation, because you lost the second order terms already when entering the equations linearly. You can still append the objective function nonlinearly and you will get a second order approximation of the objective. But this linear quadratic approach is only valid under particular restrictions (e.g. undistorted steady state). In general, it will be false.

---

<div class="post-metadata">

**Author:** ![hakeru7](https://forum.dynare.org/letter_avatar_proxy/v4/letter/h/e19adc/32.png) [@hakeru7](https://forum.dynare.org/u/hakeru7)\
**Post date:** [21 April 2015 17:56 UTC](https://forum.dynare.org/t/help-looping-over-two-parameters/4637/10 "2015-04-21T17:56:02Z")

</div>

Right. That makes sense.

What I’m confused about is why you think my model is already linearized. The model block contains my FOCs in its raw form.

---

<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:** [21 April 2015 19:52 UTC](https://forum.dynare.org/t/help-looping-over-two-parameters/4637/11 "2015-04-21T19:52:59Z")

</div>

Sorry, my mistake. I only read the heading

[quote]// model in logs  
[/quote]

in your file and presumed it was log-linearized. I should have looked more closely.

---

<div class="post-metadata">

**Author:** ![hakeru7](https://forum.dynare.org/letter_avatar_proxy/v4/letter/h/e19adc/32.png) [@hakeru7](https://forum.dynare.org/u/hakeru7)\
**Post date:** [21 April 2015 21:10 UTC](https://forum.dynare.org/t/help-looping-over-two-parameters/4637/12 "2015-04-21T21:10:52Z")

</div>

Sorry for the confusion. My file could have been neater…

Thanks so much for the all your comments and suggestions!

---

<div class="post-metadata">

**Author:** ![pito](https://forum.dynare.org/letter_avatar_proxy/v4/letter/p/e68b1a/32.png) [@pito](https://forum.dynare.org/u/pito)\
**Post date:** [23 April 2015 21:00 UTC](https://forum.dynare.org/t/help-looping-over-two-parameters/4637/13 "2015-04-23T21:00:36Z")

</div>

Hi everyone,  
Let me continue on this topic, as I used the same approach to loop over 2 parameters, yet in a different model.  
My question is what exactly stoch\_simul(var\_list\_) does. Because my output matrix X delivers the wrong results, except the very first iteration (column 1 of X). Two parameters, which I am looping over (phiinf, phier), do not affect other parameters like it was in the previous case. Yet it is obvious that the results of matrix X (rows 3-14 starting from 2nd column) are not updated, if I run it separately for each pair of looped parameters as a check.  
Would appreciate any reply.  
Thank you,  
Aliya  
[Model10\_rer\_short.mod](https://forum.dynare.org/uploads/default/original/2X/5/5f59022a7c54322e737185f840798f9f22cd0b74.mod) (9.78 KB)  
[search.m](https://forum.dynare.org/uploads/default/original/2X/a/a21b898b950ee98df1ce2a91bb3d39f9e767af54.m) (31.4 KB)

---

<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:** [24 April 2015 07:48 UTC](https://forum.dynare.org/t/help-looping-over-two-parameters/4637/14 "2015-04-24T07:48:40Z")

</div>

As shown above and in numerous posts, you must set the structural parameters you want to vary with

`set_param_value('phiinf',X(1,i));
set_param_value('phier',X(2,i));
`  
You cannot simply use

`phiinf=X(1,i);
phier=X(2,i);
`  
as you did.

---

<div class="post-metadata">

**Author:** ![pito](https://forum.dynare.org/letter_avatar_proxy/v4/letter/p/e68b1a/32.png) [@pito](https://forum.dynare.org/u/pito)\
**Post date:** [24 April 2015 08:15 UTC](https://forum.dynare.org/t/help-looping-over-two-parameters/4637/15 "2015-04-24T08:15:02Z")

</div>

Still it doesn’t solve the problem. The matrix X has wrong results. Because the cell (3,2) of X has a number -3.6626e+10, while the correct one should be -2.2986e+11 if a first iteration is that corresponding pair of looped parameters: phiinf=0.5, phier=0.

---

<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:** [24 April 2015 08:30 UTC](https://forum.dynare.org/t/help-looping-over-two-parameters/4637/16 "2015-04-24T08:30:05Z")

</div>

Your X is

[quote]X =

```
     0 0.5000 1.0000 0 0.5000 1.0000 0 0.5000 1.0000
     0 0 0 0.5000 0.5000 0.5000 1.0000 1.0000 1.0000
     0 0 0 0 0 0 0 0 0
     0 0 0 0 0 0 0 0 0
     0 0 0 0 0 0 0 0 0
     0 0 0 0 0 0 0 0 0
     0 0 0 0 0 0 0 0 0
     0 0 0 0 0 0 0 0 0
     0 0 0 0 0 0 0 0 0
     0 0 0 0 0 0 0 0 0
     0 0 0 0 0 0 0 0 0
     0 0 0 0 0 0 0 0 0
     0 0 0 0 0 0 0 0 0
     0 0 0 0 0 0 0 0 0

```

[/quote]

Using X(1,i) and X(2,i) with i=1 uses phiinf=0, phier=0. Maybe you are confusing rows and columns.

---

<div class="post-metadata">

**Author:** ![pito](https://forum.dynare.org/letter_avatar_proxy/v4/letter/p/e68b1a/32.png) [@pito](https://forum.dynare.org/u/pito)\
**Post date:** [24 April 2015 08:55 UTC](https://forum.dynare.org/t/help-looping-over-two-parameters/4637/17 "2015-04-24T08:55:46Z")

</div>

I am not confusing rows and columns. My matrix X is this up to 3rd row after loop is made, I am not showing other rows, sorry for bad formatting. But you see the cell (3,2) which is -36626165419.0826 under phiinf=0.5, phier=0.

0 0.5 1 0 0.5 1 0 0.5 1  
0 0 0 0.5 0.5 0.5 1 1 1  
-430628478299.734 -36626165419.0826 -5438549262240.73 -1.75290881843188e+15 -916705959054391 -2.86385007585544e+15 -716948367993135 -654920144499515 -2.23270232124789e+15

Now let’s change my looping range as if my first iteration starts from that phiinf=0.5, phier=0. The three rows of matrix X will look like this then, showing that the correct number should be -229862535943.196.

0.5 0.75 1 0.5 0.75 1 0.5 0.75 1  
0 0 0 0.5 0.5 0.5 1 1 1  
-229862535943.196 -181712327817.607 -5438549262240.73 -3.82907414291894e+15 -1.48502741146982e+15 -2.86385007585544e+15 -1.10183224485801e+15 -829356745047734 -2.23270232124789e+15

---

<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:** [24 April 2015 09:26 UTC](https://forum.dynare.org/t/help-looping-over-two-parameters/4637/18 "2015-04-24T09:26:48Z")

</div>

You rely on stochastic simulations. Without fixing a seed with

` set_dynare_seed('default')
`  
before calling stoch\_simul, you will be using different random numbers. The reason is that when calling Dynare, a seed is set. When only calling stoch\_simul, this is not the case. You need to do this manually.

---

<div class="post-metadata">

**Author:** ![pito](https://forum.dynare.org/letter_avatar_proxy/v4/letter/p/e68b1a/32.png) [@pito](https://forum.dynare.org/u/pito)\
**Post date:** [24 April 2015 10:08 UTC](https://forum.dynare.org/t/help-looping-over-two-parameters/4637/19 "2015-04-24T10:08:36Z")

</div>

I got it now, thank you so much! Such a great opportunity to clarify things on this forum. Success in your further work!

---

<div class="post-metadata">

**Author:** ![pito](https://forum.dynare.org/letter_avatar_proxy/v4/letter/p/e68b1a/32.png) [@pito](https://forum.dynare.org/u/pito)\
**Post date:** [30 June 2015 15:36 UTC](https://forum.dynare.org/t/help-looping-over-two-parameters/4637/20 "2015-06-30T15:36:20Z")

</div>

Hi everyone,

Let me continue on this, but in a perfect foresight model. I remember that set\_dynare\_seed command, as you’ve suggested earlier, helped me to fix my issue in a stochastic model.

Now I am looping over one parameter which affects my exogenous shocks path that I am feeding into a perfect foresight model. I observe that shocks path is replaced properly, yet the results of simulation are not correct after the second iteration of a parameter, if I check just the way I described in my previous post here. Obviously, set\_dynare\_seed is not a solution as before. What should I control for to deal with basically the same problem, but in a perfect foresight framework?  
Would appreciate any helpful reply.

Thank you,  
Aliya

[Next page](https://forum.dynare.org/t/help-looping-over-two-parameters/4637.md?page=2)
