# Dynare Julia - Loop over parameters

**URL:** https://forum.dynare.org/t/dynare-julia-loop-over-parameters/22052
**Category:** Dynare Julia
**Created:** [8 March 2023 13:24 UTC](https://forum.dynare.org/t/dynare-julia-loop-over-parameters/22052 "2023-03-08T13:24:06Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![FelipeCavalheiro](https://forum.dynare.org/letter_avatar_proxy/v4/letter/f/96bed5/32.png) [@FelipeCavalheiro](https://forum.dynare.org/u/FelipeCavalheiro)
#### Post date: [8 March 2023 13:24 UTC](https://forum.dynare.org/t/dynare-julia-loop-over-parameters/22052/1 "2023-03-08T13:24:06Z")

</div>

Hi,

I’ve seen some threads about looping over parameters in dynare matlab, but I have no idea how to do that in dynare julia.

I thought about creating modules and trying to do this, but I’m not really sure if this is the right way to go.

Has anyone tried this or know somewhere I can read about it?

Thanks.

---

<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: [8 March 2023 13:38 UTC](https://forum.dynare.org/t/dynare-julia-loop-over-parameters/22052/2 "2023-03-08T13:38:54Z")

</div>

@MichelJuillard may be able to help.

---

<div class="post-metadata">

### Author: ![MichelJuillard](https://forum.dynare.org/user_avatar/forum.dynare.org/micheljuillard/32/5318_2.png) [@MichelJuillard](https://forum.dynare.org/u/MichelJuillard)
#### Post date: [8 March 2023 14:43 UTC](https://forum.dynare.org/t/dynare-julia-loop-over-parameters/22052/3 "2023-03-08T14:43:18Z")

</div>

Felipe, it depends a bit of what you want to do it for and the number of iterations. Could you please post the current state of your \*.mod and explain exactly what you want to do with and how you want to handle the results.  
Best

---

<div class="post-metadata">

### Author: ![FelipeCavalheiro](https://forum.dynare.org/letter_avatar_proxy/v4/letter/f/96bed5/32.png) [@FelipeCavalheiro](https://forum.dynare.org/u/FelipeCavalheiro)
#### Post date: [8 March 2023 16:34 UTC](https://forum.dynare.org/t/dynare-julia-loop-over-parameters/22052/4 "2023-03-08T16:34:53Z")

</div>

[CanonicalNK.mod](https://forum.dynare.org/uploads/short-url/bQTwzA17pdQ9fpAp8DyGqTWPpbp.mod) (1.1 KB)

I want to look at some statistics of variables in the model when some parameters change.

For example, I want to run the model with a specific value of beta, then change the value and compute the statistics without running it all over again.

I believe it would be something similar to this in matlab:

> [@Loop over parameters](https://forum.dynare.org/t/loop-over-parameters/3335/2):
>
> ```auto
> rhos = 0.8:0.05:1.05;
> first_time = 1;
> for i=1:length(rhos)
> if first_time
> set_param_value('rho',rhos(i));
> dynare your_mod_file_here noclearall;
> first_time = 0;
> else
> set_param_value('rho',rhos(i));
> info = stoch_simul(var_list_);
> if info;
> disp('Computation fails for rho = ' num2str(rho)]);
> end;
> end
> end
> 
> ```

---

<div class="post-metadata">

### Author: ![MichelJuillard](https://forum.dynare.org/user_avatar/forum.dynare.org/micheljuillard/32/5318_2.png) [@MichelJuillard](https://forum.dynare.org/u/MichelJuillard)
#### Post date: [9 March 2023 10:36 UTC](https://forum.dynare.org/t/dynare-julia-loop-over-parameters/22052/5 "2023-03-09T10:36:41Z")

</div>

For a few iterations as in your example, using the Dynare macro @#for is the simplest. Dynare macros work in DynareJulia. I attach an example:  
[CanonicalNK\_loo.mod](https://forum.dynare.org/uploads/short-url/kFZtiPZlCMRviXib8mDweK1nhXf.mod) (1.2 KB)

I will also think about a Julia equivalent to your example: calling Dynare first then calling directly stoch\_simul as a function

---

<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: [9 March 2023 13:31 UTC](https://forum.dynare.org/t/dynare-julia-loop-over-parameters/22052/6 "2023-03-09T13:31:50Z")

</div>

@MichelJuillard At least under Matlab/Octave that loop via the preprocessor is strongly discouraged. It does not allow for proper error handling within the loop. In case an error is triggered, you would be wrongly reading out the results from the previous iteration.

---

<div class="post-metadata">

### Author: ![MichelJuillard](https://forum.dynare.org/user_avatar/forum.dynare.org/micheljuillard/32/5318_2.png) [@MichelJuillard](https://forum.dynare.org/u/MichelJuillard)
#### Post date: [9 March 2023 15:37 UTC](https://forum.dynare.org/t/dynare-julia-loop-over-parameters/22052/7 "2023-03-09T15:37:46Z")

</div>

@jpfeifer what alternative do you recommend?

---

<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: [9 March 2023 15:43 UTC](https://forum.dynare.org/t/dynare-julia-loop-over-parameters/22052/8 "2023-03-09T15:43:30Z")

</div>

Looping over the function itself and reading out the `info`-code:

> [@Parameter loop in Dynare 5.x](https://forum.dynare.org/t/parameter-loop-in-dynare-5-x/20293/3):
>
> ```auto
> OMEGARS = 1:1:5;
> first_time = 1;
> 
> for i = 1:length(OMEGARS)
> if first_time           
> dynare rentalhousing noclearall;
> first_time = 0;
> else                 
> set_param_value('OMEGAR',OMEGARS(i));        
> [info, oo_] = stoch_simul(M_, options_, oo_, var_list_);
> if info
> disp(['Computation fails for rho = ' num2str(OMEGARS)]);
> else
> %results_cell{i} = oo_ ;
> plot(oo_.irfs.data_QQ_eps_j);
> hold on;
> disp('I am plotting');
> oo_.irfs.data_QQ_eps_j
> end
> end 
> 
> ```

Under Matlab, you can put the loop (ignoring the first time call) into a `verbatim`-block to simply do the loop.

---

<div class="post-metadata">

### Author: ![FelipeCavalheiro](https://forum.dynare.org/letter_avatar_proxy/v4/letter/f/96bed5/32.png) [@FelipeCavalheiro](https://forum.dynare.org/u/FelipeCavalheiro)
#### Post date: [10 March 2023 11:24 UTC](https://forum.dynare.org/t/dynare-julia-loop-over-parameters/22052/9 "2023-03-10T11:24:13Z")

</div>

Thank you very much!
