# Loop over parameters to find maximized welfare

**URL:** <https://forum.dynare.org/t/loop-over-parameters-to-find-maximized-welfare/12358>\
**Category:** Optimal Policy\
**Created:** [3 September 2018 14:11 UTC](https://forum.dynare.org/t/loop-over-parameters-to-find-maximized-welfare/12358 "2018-09-03T14:11:55Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![MichelleHuang](https://forum.dynare.org/letter_avatar_proxy/v4/letter/m/bc79bd/32.png) [@MichelleHuang](https://forum.dynare.org/u/MichelleHuang)\
**Post date:** [3 September 2018 14:11 UTC](https://forum.dynare.org/t/loop-over-parameters-to-find-maximized-welfare/12358/1 "2018-09-03T14:11:55Z")

</div>

Dear all,

here are so many similar questions on this forum, but I couldn’t find out an identical question here, I really need a help ☹

I have found two topic related to my question:

> [@Loop over parameters](https://forum.dynare.org/t/loop-over-parameters/3335):
>
> Hi, I know that this problem was broached on this forum many times since I found some topics connected with it but I didn’t find any solution for my problem. I want to look at some statistics of variables in the model when some parameters change. The steady state is calculated by hand and included in ‘initval’. For creating the loop over parameters, I used simple syntax like this: rhos = 0.8:0.05:1.05; for i=1:length(rhos); rho = rhos(i); stoch\_simul(order=1); if info; disp('Computation fails…

and

> [@Optimal policy parameters in a non-linear model](https://forum.dynare.org/t/optimal-policy-parameters-in-a-non-linear-model/11772):
>
> Dear all, I am trying to find out the optimal policy parameters for macroprudential policy in my model: taue = 0.9\*taue(-1) + 0.1\*(alpha\_le\*(leverage\_e/leverage\_e(-1) - 1) + alpha\_qky\*((qe\*ke/ye)/(qe(-1)\*ke(-1)/ye(-1)) -1) + alpha\_ve\*(ve/ve(-1) - 1) + alpha\_cg\*(cg - 1)); 0.9 is the persistence parameter. What I am trying to find out is the optimal value of alpha\_le, alpha\_qky, alpha\_ve, and alpha\_cg that optimizes the welfare. This is how I calculate the welfare (omega\_e): util\_e = log(c…

My questions are as below.

**First of all, what is the difference between these two topics? which one could loop over parameters to find out the corresponding maximized welfare?**

//

Next, follow the suggestion from the first forum topic, I build a .m file to find out my target welfare.

```
dynare welfare_TRY2.mod
rhogrid = 0.6:0.2:1.0;

results_cell = zeros(length(rhogrid),1);
for i = 1:length(rhogrid)

    set_param_value('rho',rhogrid(i))

    info = stoch_simul(var_list_)

    if info ==0
        results_cell(i) = oo_.steady_state(44);
    else 
        fprintf('Here is an error!');
    end
end

```

where oo\_.steady\_state(44) is referred to the **conditional welfare** which is derived according to the **get\_consumption\_equivalent\_conditional\_welfare.m** from

> [@Welfare and Pruning](https://forum.dynare.org/t/welfare-and-pruning/11470):
>
> Hi! I am working with a medium-scale two-agent New Keynesian model with government spending and want to analyse the welfare effects of a 1% increase in fiscal spending using the methodology set out in Schmitt-Grohe and Uribe (2004). The approach I’m using is summarised below and the associated Dynare code is as follows: VRold=(1/(1-betta))(ln(STEADY\_STATE(CR))-nu(STEADY\_STATE(NR)^(1+varphi)/(1+varphi))); VLold=(1/(1-betta))(ln(STEADY\_STATE(CL))-nu(STEADY\_STATE(NL)^(1+varphi)/(1+varphi))); V…

**My second question is that, does oo\_.steady\_state(44) really capture the corresponding welfare given after-looped parameters?**

And, this .m file works with something wrong.  
The content of results\_cell are all the same and identical to the steady state value of welfare from running my dynare.mod file separately! I have tried to loop another parameter, while the result doesn’t change. **Does it mean that my loop doesn’t work in fact? How could I fix it?**

As a matter of fact, my .m file doesn’t work well for every iteration. In the last iteration, i=3 and rho=1.0, the info =[4.0 1.0]. From the error message, I know that my model violate Blanchard & Kahn conditions. **My question is that, since the .m file still run without just stopping, does my model structure need an overall modification? or could I think my model works well but the parameter looping range should be modified?**

//

The followings are my .m and .mod files.  
[Run\_welfare\_TRY\_outside2.m](https://forum.dynare.org/uploads/default/original/2X/9/9807a9c6ca814050d968796ff6f1d755aa7d8499.m) (623 Bytes)  
[welfare\_TRY2.mod](https://forum.dynare.org/uploads/default/original/2X/6/60f40c4dead76cfebb5907024afcd59e14093127.mod) (8.1 KB)  
[welfare\_TRY2\_steadystate.m](https://forum.dynare.org/uploads/default/original/2X/3/3a66bdc9ee83accae7d43f33b135bab9bc9773e2.m) (5.7 KB)  
[COMPUTE\_WELFARE.m](https://forum.dynare.org/uploads/default/original/2X/a/a862c14d3d5ba66ba6dff4e5560dad5a10440a85.m) (996 Bytes)

I am so sorry and embarrassed to ask so many questions, I would be thankful for any help.  
Regards

---

<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:** [4 September 2018 06:26 UTC](https://forum.dynare.org/t/loop-over-parameters-to-find-maximized-welfare/12358/2 "2018-09-04T06:26:25Z")

</div>

1. In your case, you only need to read out the welfare. As you are not computing a consumption-equivalent, you do not need the function I write.
2. Welfare is a stochastic property of the model, but you are reading out the steady state instead of the unconditional mean. The steady state will always be the same. You need to read out the respective element of `oo_.mean` and for that cannot use `nomoments`. Also, you need to add `welfare` to the variables after `stoch_simul` to get its moments.
3. Are you sure your shock size is correct. It seems you multiply by 100 to have them in percentages. But at second order you cannot do that as the model solution is not certainty equivalent anymore. You will get wrong results. A 1 percent shock is 0.01, not 1.

Use

```auto

dynare welfare_TRY2.mod
rhogrid = 0.6:0.2:1.0;
% result_y = ones(length(rhogrid),1);
welfare_pos=strmatch('welfare',var_list_,'exact');
results_cell = zeros(length(rhogrid),1);
for i = 1:length(rhogrid)

    set_param_value('rho',rhogrid(i))

    info = stoch_simul(var_list_);
    
    if info ==0
        results_cell(i) = oo_.mean(welfare_pos);
    else 
        fprintf('Here is an error!\n');
    end
end

```

[welfare\_TRY2\_steadystate.m](https://forum.dynare.org/uploads/default/original/2X/3/3a66bdc9ee83accae7d43f33b135bab9bc9773e2.m) (5.7 KB)  
[welfare\_TRY2.mod](https://forum.dynare.org/uploads/default/original/2X/4/4cc28e2dac266421d234b54c97cfcca38bfbadf3.mod) (8.1 KB)  
[Run\_welfare\_TRY\_outside2.m](https://forum.dynare.org/uploads/default/original/2X/5/59e66441eeef0e45872356713f9fbe9694884a83.m) (677 Bytes)

---

<div class="post-metadata">

**Author:** ![MichelleHuang](https://forum.dynare.org/letter_avatar_proxy/v4/letter/m/bc79bd/32.png) [@MichelleHuang](https://forum.dynare.org/u/MichelleHuang)\
**Post date:** [4 September 2018 08:18 UTC](https://forum.dynare.org/t/loop-over-parameters-to-find-maximized-welfare/12358/3 "2018-09-04T08:18:33Z")

</div>

Thank you for your quick reply!!! It’s real helpful!!

But I still have some questions:

1. done

2. I am not sure whether my shock size is correct… I just follow the .mod file from  
Matteo Iacoviello, 2015. " **[Financial Business Cycles](https://ideas.repec.org/a/red/issued/14-27.html)** ," [Review of Economic Dynamics](https://ideas.repec.org/s/red/issued.html), Elsevier for the Society for Economic Dynamics, vol. 18(1), pages 140-164, January.  
And my model structure is basically modified from it. But I don’t know what’s the effect of different scale of shock size. Should I choose my shock size based on the parameter values? If not, how should I choose my shock size?  
I change the shock size to an ordinary case as below:

3. There is still an error after I change the shock size. And the error is the same as before. I know that my model violate Blanchard & Kahn conditions when rho=1.0. My question is that, since the .m file still run without just stopping, does my model structure need an overall modification? or could I think my model works well but the parameter looping range should be modified?

4. In the previous forum topics, there is a code to show where the error happens.

Sorry for all these questions, and thank you very much for your time.

---

<div class="post-metadata">

**Author:** ![Grant](https://forum.dynare.org/letter_avatar_proxy/v4/letter/g/a5b964/32.png) [@Grant](https://forum.dynare.org/u/Grant)\
**Post date:** [4 September 2018 09:55 UTC](https://forum.dynare.org/t/loop-over-parameters-to-find-maximized-welfare/12358/4 "2018-09-04T09:55:27Z")

</div>

Hi Michelle,

I think you seem to miss one point. In your case, you are tying to find out the maximized welfare value, unconditional or conditional as you wish, but you did’t write a function which can pin down the maximized welfare during a number of simulation of the loop. From my logic, 1. you should write a loop over the parameter, 2. then run the simulation. After the simulation, 3. you should use a function to find out the maximized welfare value and the corresponding parameter value during the simulations of the loop.

I am not 100% sure about my opinion. Also looking forward to Prof. Jpfeifer’s response.

---

<div class="post-metadata">

**Author:** ![MichelleHuang](https://forum.dynare.org/letter_avatar_proxy/v4/letter/m/bc79bd/32.png) [@MichelleHuang](https://forum.dynare.org/u/MichelleHuang)\
**Post date:** [4 September 2018 12:36 UTC](https://forum.dynare.org/t/loop-over-parameters-to-find-maximized-welfare/12358/5 "2018-09-04T12:36:15Z")

</div>

Hi Grant,

thank you for your joining and opinion!

Yes, I don’t write a function or some code to find out the maximized welfare value, I just write `result_cell` to save all the welfare value.  
If all my code run well and successfully, I will use some simple matlab code like

```
 maximized_welfare = max(result_cell);

```

to find out the maximized welfare. But I still need some other codes to know what’s the corresponding looping parameter value.

Let me leave this question for later. 😜

---

<div class="post-metadata">

**Author:** ![Grant](https://forum.dynare.org/letter_avatar_proxy/v4/letter/g/a5b964/32.png) [@Grant](https://forum.dynare.org/u/Grant)\
**Post date:** [4 September 2018 15:44 UTC](https://forum.dynare.org/t/loop-over-parameters-to-find-maximized-welfare/12358/6 "2018-09-04T15:44:19Z")

</div>

Hi Michelle,

I now get your point about the maximized welfare value. However, you just save the welfare values. How do you pin down the corresponding parameter value which generates the maximized welfare?

---

<div class="post-metadata">

**Author:** ![jojokre](https://forum.dynare.org/letter_avatar_proxy/v4/letter/j/d2c977/32.png) [@jojokre](https://forum.dynare.org/u/jojokre)\
**Post date:** [10 September 2018 14:19 UTC](https://forum.dynare.org/t/loop-over-parameters-to-find-maximized-welfare/12358/7 "2018-09-10T14:19:27Z")

</div>

Hi Mechelle,

```auto
welfare_pos=strmatch('welfare',var_list_,'exact');

```

gives you only the position (=number) of your welfare variable in your .mod file.  
After running a second-order approximation of your model

```auto
oo_.mean(welfare_pos);

```

will give you unconditional welfare.

If you are interested in conditional welfare you need to calculate  
oo\_.dr.ys(welfare\_pos)+0.5\*oo\_.dr.ghs2(oo\_.dr.inv\_order\_var(welfare\_pos))  
after running a second-order approximation of your model.

---

<div class="post-metadata">

**Author:** ![MichelleHuang](https://forum.dynare.org/letter_avatar_proxy/v4/letter/m/bc79bd/32.png) [@MichelleHuang](https://forum.dynare.org/u/MichelleHuang)\
**Post date:** [10 September 2018 14:27 UTC](https://forum.dynare.org/t/loop-over-parameters-to-find-maximized-welfare/12358/8 "2018-09-10T14:27:24Z")

</div>

Hi jojokre,

Thank you for your reply and I have known that

```
oo_.dr.ys(welfare_pos)+0.5*oo_.dr.ghs2(oo_.dr.inv_order_var(welfare_pos))

```

represents the conditional welfare early.

I go modify my questions now! 😋

---

<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:** [13 September 2018 10:00 UTC](https://forum.dynare.org/t/loop-over-parameters-to-find-maximized-welfare/12358/9 "2018-09-13T10:00:07Z")

</div>

@MichelleHuang wants to do a grid search here. I just want to point out for future reference that the conditional welfare measure mentioned in the two previous posts is not general. It is **conditional welfare in the deterministic steady**. If you want to compute conditional welfare in other points, you have to use the `simult_`-function as in

> <https://github.com/JohannesPfeifer/DSGE_mod/blob/master/Born_Pfeifer_2018/Welfare/get_consumption_equivalent_conditional_welfare.m>

  
Now to the questions I did not yet answer above:

1. Iacoviello did only do a first order approximation as he did not conduct a welfare analysis. At first order, there is certainty equivalence so that he could multiply all shocks by 100. That is not allowed at second order. 1% is 0.01.
2. I don’t understand the question about stoppping. The code is designed to filter out error codes.
3. The code for displaying you pasted cannot work, because `rho` is not known. You want to have  
`num2str(rhogrid(i))`.

---

<div class="post-metadata">

**Author:** ![Grant](https://forum.dynare.org/letter_avatar_proxy/v4/letter/g/a5b964/32.png) [@Grant](https://forum.dynare.org/u/Grant)\
**Post date:** [17 September 2018 08:45 UTC](https://forum.dynare.org/t/loop-over-parameters-to-find-maximized-welfare/12358/11 "2018-09-17T08:45:20Z")

</div>

Hi Prof. Jpfeifer,

Given your code, I write my code to do grid search as:

```auto
dynare housing.mod

```

rho\_pi\_grid = 1.001:0.1:3;  
rho\_y\_grid = 0:0.1:1;

welfare\_pos=strmatch(‘v’,var\_list\_,‘exact’);  
results\_cell = zeros(length(rho\_pi\_grid),length(rho\_y\_grid));  
for i = 1:length(rho\_pi\_grid)  
for j = 1:length(rho\_y\_grid)

```auto
set_param_value('rho_pi',rho_pi_grid(i));
set_param_value('rho_y',rho_y_grid(j));

info = stoch_simul(var_list_);

if info ==0
    results_cell(i,j) = oo_.mean(welfare_pos);
else 
    fprintf('Here is an error!\n');
end
end

```

end

I think the difference is that I have two parameters which need to be optimized. However, I have the error message:  
Assignment has more non-singleton rhs dimensions than non-singleton subscripts

Could you please help me check where the problem is? Thank you !

---

<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:** [18 September 2018 08:29 UTC](https://forum.dynare.org/t/loop-over-parameters-to-find-maximized-welfare/12358/12 "2018-09-18T08:29:22Z")

</div>

What is the full error message? Please provide the codes

---

<div class="post-metadata">

**Author:** ![Grant](https://forum.dynare.org/letter_avatar_proxy/v4/letter/g/a5b964/32.png) [@Grant](https://forum.dynare.org/u/Grant)\
**Post date:** [18 September 2018 08:45 UTC](https://forum.dynare.org/t/loop-over-parameters-to-find-maximized-welfare/12358/14 "2018-09-18T08:45:53Z")

</div>

Hi Jpfeifer,

Here attached the grid search code file and the mod-file. After I run the grid search, I got the error message:

```auto
Assignment has more non-singleton rhs dimensions than non-singleton subscripts

```

Error in gridsearch (line 21)  
results\_cell(i,j) = oo\_.mean(welfare\_pos);

Could you please help me fix it? Thank you very much. [gridsearch.m](https://forum.dynare.org/uploads/default/original/2X/6/6cc03328e482b7b243d473538bce969ee62eee1f.m) (582 Bytes)  
[housing.mod](https://forum.dynare.org/uploads/default/original/2X/b/b82ab169093bcbe4db94bbf2cb0f276f841972e3.mod) (7.6 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:** [18 September 2018 09:04 UTC](https://forum.dynare.org/t/loop-over-parameters-to-find-maximized-welfare/12358/15 "2018-09-18T09:04:56Z")

</div>

You did not specify a list of variables in your mod-file. Thus,

```auto
welfare_pos=strmatch('v',var_list_,'exact');

```

cannot work as `var_list_` is empty. You need

```auto
welfare_pos=strmatch('v',M_.endo_names,'exact');

```

---

<div class="post-metadata">

**Author:** ![Grant](https://forum.dynare.org/letter_avatar_proxy/v4/letter/g/a5b964/32.png) [@Grant](https://forum.dynare.org/u/Grant)\
**Post date:** [18 September 2018 09:49 UTC](https://forum.dynare.org/t/loop-over-parameters-to-find-maximized-welfare/12358/17 "2018-09-18T09:49:29Z")

</div>

Prof. Jpfeifer,

Thanks a lot for your reply. I have fixed this problem. However, I also want to pin down the corresponding parameter value which generates the maximized welfare. Could you please show me how to do it? I have no idea about it so far.

I am looking forward to your reply.

---

<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 September 2018 09:10 UTC](https://forum.dynare.org/t/loop-over-parameters-to-find-maximized-welfare/12358/18 "2018-09-24T09:10:55Z")

</div>

You need to use Matlab’s `max`-operator along the grid points.

---

<div class="post-metadata">

**Author:** ![tevy](https://forum.dynare.org/letter_avatar_proxy/v4/letter/t/f6c823/32.png) [@tevy](https://forum.dynare.org/u/tevy)\
**Post date:** [16 January 2019 15:33 UTC](https://forum.dynare.org/t/loop-over-parameters-to-find-maximized-welfare/12358/19 "2019-01-16T15:33:19Z")

</div>

Hi Grant,

I have similar problem with you, and I write this following code to search corresponding value of parameter that generate maximize welfare.

```auto
[max_wel,max_idx] = max(results_cell(:));
[X Y]=ind2sub(size(results_cell),max_idx); % X and Y will give the number of row and column of parameters that generate highest welfare
max_wel % to show the value of maximum welfare
par1_opt=rho_pi_grid(X) % to show the value of the optimal first parameter in row X
par2_opt=xi_y_grid(Y) % to show the value of the optimal second parameter in column Y

```

It works for me. I hope it helps.  
However, my problem is I want to optimize 4 parameters and I don’t know how to expand the code for more than 2 parameters. Anyone can help? Thank you

---

<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:** [18 January 2019 08:07 UTC](https://forum.dynare.org/t/loop-over-parameters-to-find-maximized-welfare/12358/20 "2019-01-18T08:07:58Z")

</div>

With the vectorization `(:)` you move to a one-dimensional vector. Thus, you approach will work with any dimensional problem. The only issue is to get from the sub-indices of the vector back to the original index. But `ind2sub` already allows doing this.
