Loop over parameters to find maximized welfare

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 :frowning:

I have found two topic related to my question:

and

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

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 (623 Bytes)
welfare_TRY2.mod (8.1 KB)
welfare_TRY2_steadystate.m (5.7 KB)
COMPUTE_WELFARE.m (996 Bytes)

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

3 Likes
  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


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 (5.7 KB)
welfare_TRY2.mod (8.1 KB)
Run_welfare_TRY_outside2.m (677 Bytes)

1 Like

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 ," Review of Economic Dynamics, 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:

     shocks;
     var eps_me; stderr sigma_ME ;
     var eps_z; stderr sigma_Z ;
     var eps_R; stderr exp(sigma_R);
     end;
    

    It’s it okay?

  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.

     if info;
         disp('Computation fails for rho = ' num2str(rho)]);
     end;
    

    But these code doesn’t work. Once I run a .m file included the above code, it will show an error message: “Unexpected MATLAB expression.”. Is there something wrong?

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

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.

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. :stuck_out_tongue_winking_eye:

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?

Hi Mechelle,

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

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.

2 Likes

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! :yum:

@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


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)).

Hi Prof. Jpfeifer,

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

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)

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 !

What is the full error message? Please provide the codes

Hi Jpfeifer,

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

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 (582 Bytes)
housing.mod (7.6 KB)

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

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

cannot work as var_list_ is empty. You need

welfare_pos=strmatch('v',M_.endo_names,'exact');
1 Like

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.

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

Hi Grant,

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

[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

1 Like

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.

2 Likes