Optimal policy parameters in a non-linear model

Hi Aida,

Thanks for your help. Do you mean that I should put that code right before the “stoch_simul” command? If so, what’s the difference if I put the maximization code after the “stoch_simul” command?

Besides, where are the values of maximized welfare and the optimal parameters stored?

  1. You should put it after a stoch_simul-command as that one is needed to initialize all the structures used.
  2. The output of the optimizer
    stores the welfare in fhat and the parameter values in x_opt_hat
1 Like

Dear Prof Pfeifer,

Many thanks for your reply. Can I have other questions please:

  • If i changed the x_start values or lower bound values, sometimes it did not work. The message is: ‘Undefined function or variable ‘par_value_lambda’.’ What could be the problem?
  • Can I put negative numbers in the x_start and/or lower bound values?
  • Sometimes, I get the optimal parameters equal to the initial values. What seems to be the problem here? In order to allow a possibility that the optimal parameters are negative, I tried to lower the x_start and/or lower bound values to negative numbers, but it didn’t work.

Thank you very much.

  1. The first one is due to a bug in the above code that I fixed.
  2. Yes, negative numbers are allowed as long as the bounds are consistent with this
  3. Having optimal parameters equal to the starting values is usually a sign of numerical problems. It’s often hard to know what happens. But often using a different optimizer or different starting values helps.
1 Like

Thanks for your kind reply Prof Jpfeifer,

I made my code work. writing the code after the stoch_simul command.
I have a similar problem than Ratih, with some starting values it gives me optimized values but with others sets it reports the initial numbers.
At the moment I am playing around with different values but could you suggest a different optimizer, please?
to have in mind an alternative option in case I cannot get optimized values under certain scenarios.
Thank you very much!

The cmaes optimizer seems to work pretty well in practice, but it takes quite long to run. You would need something along the lines of

    %set CMAES options
    cmaesOptions = options_.cmaes;
    cmaesOptions.LBounds = [-1000;-1000];
    cmaesOptions.UBounds = [1000;1000];
    [x_opt_hat, fhat, COUNTEVAL, STOPFLAG, OUT, BESTEVER] = cmaes('welfare_objective',x_start,H0,cmaesOptions,x_opt_name);

Dear Prof Jpfeifer,

Thank you for your reply. I have tried cmaes, it solves the optimization problems regardless of the scenario. I believe it is more powerful perhaps under certain circumstances.

I understand that “bestever” is the best result in all trials of the maximization problem whereas fmin is the best in the last generation. It seems to make sense that “bestever” is the best choice, am I right? If so, do you know if there is a case where it would be better to pick fmin? a rule of thumb or criteria, just to know.

Could you kindly advice me how to use “plotcmaesdat.m”, I read we can get the plot of the welfare to show the max point. I have seen I should write cmaesOptions.LogModulo different from zero, but I am not sure how to write that code. Any help would great.

Again, thank you very much for your help!!

  1. We are almost always interested in the global maximum, so bestever seems appropriate
  2. Regarding plotcmaesdat.m: the Dynare-packaged cmaes.m does not support this feature. You would need to download and use the original cmaes.m.

Ok, I understand Prof Jpfeifer. I will try to have a look at cmaes.m for plots then.
Thank you very much for your help and time.

Prof. Jpfeifer,

Thanks for your help.

  1. Since cmaes is a global optimizer, I want to ask that in your posted code cmaesOptions.LBounds = [-1000;-1000]; cmaesOptions.UBounds = [1000;1000]; do the number of semicolon refer to the number of parameters which need to optimize the welfare in the x_opt_name? Say, if I have 3 parameters in x_opt_name, I should put cmaesOptions.LBounds = [-1000;-1000;-1000]; cmaesOptions.UBounds = [1000;-1000;1000]. Am I right?

  2. Also, do cmaesOptions.LBounds and cmaesOptions.UBounds refer to the upper and lower bound of the parameter range? If so, what’s the difference with the parameter range of 0~inf in your posted code %define parameters to be optimized and their upper and lower bound x_opt_name={'alpha_le',0,Inf 'alpha_qky',0,Inf 'alpha_ve',0,Inf 'alpha_cg',0,Inf }; ?

  3. Moreover, what does x_start mean? Why do you put 0.01 in it? I tried to put the parameter names in it, instead of 0.01, the code still can run.

I am looking forward to your reply.

  1. Yes, the bounds need to be a column vector with the number of rows conforming to the number of estimated parameters.
  2. csminwel does not allow setting bounds for the optimizer. cmaes does. As nobody specified the actual bounds to be used, I just picked some arbitrary numbers. That’s why the two specifications are not equivalent.
  3. x_start is the starting values. It should not work with a string instead of a number.

Prof. Jpfeifer,

Many thanks for your reply.

As you said, only cmaes allow setting bounds for the optimizer, since we have already set the bounds in your posted code


, which is from 0 to infinite, and also have the bounds in

cmaesOptions.LBounds = [-1000;-1000]; 
cmaesOptions.UBounds = [1000;1000]; 

which one should be the bounds that cmaes ues? I am confused about it.
Moreover, if

cmaesOptions.LBounds = [-1000;-1000]; 
cmaesOptions.UBounds = [1000;1000];

is the bounds that cmaes uses, how should I set parameters to be optimized in front of the optimizer in the mod-file?
When I set the parameter names in x_start as x_start=[rho_r, rho_pi, rho_y]', the code indeed do run. It can do work with a string instead of a number. Please see the attached code. Could you please help me check whether there exist some problems?
When I set the bounds as

cmaesOptions.LBounds = [0;1.1;0]; 
cmaesOptions.UBounds = [1;5;3];

, I have the error message:

In an assignment A(:slight_smile: = B, the number of elements in A and B must be the same.
Error in dyn_first_order_solver (line 251)
info(2) = temp’*temp;

Error in stochastic_solvers (line 267)
[dr,info] = dyn_first_order_solver(jacobia_,M_,dr,options_,task);

Error in resol (line 144)
[dr,info] = stochastic_solvers(dr,check_flag,M,options,oo);

Error in conditional_welfare_objective (line 17)
[oo_.dr,info,M_,options_,oo_] = resol(0,M_,options_,oo_); %get decision rules

Error in cmaes (line 948)
fitness.raw(k) = feval(fitfun, arxvalid(:,k), varargin{:});

Error in housing (line 346)
[x_opt_hat, fhat, COUNTEVAL, STOPFLAG, OUT, BESTEVER] = cmaes(‘conditional_welfare_objective’,x_start,H0,cmaesOptions,x_opt_name);

Error in dynare (line 223)
evalin(‘base’,fname) ;`

Could you please help me what the problem is? How to fix it?

Here attached the code. I am looking forward to your reply. conditional_welfare_objective.m (1.1 KB)
housing.mod (8.5 KB)

  1. The code part relating to x_opt_name was not written for use with CMAES. If you keep it in your code, make sure that the bounds set are consistent with the ones in cmaesOptions. How you actually set those bounds depends on your model. I just picked some numbers here as I don’t know the model.
  2. Regarding

This is not a string. The assignment uses the calibrated parameter values stored in e.g. the variable rho_r as the starting value.
3. The error message you get comes from options_.qz_criterium not being set and the solver triggering a case where it is needed. I changed the code at https://github.com/JohannesPfeifer/DSGE_mod/commit/c7e066a275ffc10f51eee27acba789b17d002315 to make it robust against this.

Prof. Jpfeifer,

Thanks for your reply. As you posted that the code part relating to x_opt_name was not written for use with CMAES, but in the code of CMAES, it is written as [x_opt_hat, fhat, COUNTEVAL, STOPFLAG, OUT, BESTEVER] = cmaes(‘welfare_objective’,x_start,H0,cmaesOptions,x_opt_name); which indeed includes the x_opt_name.

So if I don’t put parameter bounds in x_opt_name, how should I define the parameter names and x_opt_name? I have tried x_opt_name={‘rho_r’ ‘rho_pi’ ‘rho_y’}’, it doesn’t work. Could you please help with this question? I am looking forward to your reply.

If you are not adjusting the underlying objective function, which requires x_opt_name as an input, you need to leave it exactly as defined for csminwel. In addition, you need to set the bounds to the same value as in cmaesOptions

Dear prof. Pfeifer,

how can I tell Dynare to use the optimal parameters derived in this way to run the simulations (as the osr command would do)?

(P.S. I need to: loop over a parameter–> find conditional optimal parameters --> run simulated data (2nd order) with this set of parameters (the looped one and the conditionally optimal ones))

You can use the set_param_value-command for that. Search the forum

Dear Prof,

I am trying to do this and I am using the same codes for maximising the objective function. As far as I have understood I first need to run the .m file but when I try to do so I get this error:

welfare_objective(x_opt, x_opt_name)
Undefined function or variable ‘x_opt’.

How can I solve this? I guess I missed one step.


Drawing extensively on @jpfeifer’s code provided at Discounting under Optimal Monetary Policy, you can place the attached files in your folder and run

dynare fixednew_camilo

fixednew_camilo.mod (7.0 KB)
get_minus_welfare_objective.m (1.7 KB).

Advice 1: explore the sensitivity of your results by using several starting values for the parameters you are optimizing over.
Advice 2: try and impose sensible parameter bounds.



This is great. Thank you so much. i tried with different initial values and bounds and my results now seem to be sensible to different combinations.