Optimal policy error

I am trying to find the optimal variables of monetary policy that maximize welfare. I have taken codes
(i.e., commands at the end of mod file after stoch_simul, and welfare_objective.m) from the forum. They work with some other mod files provided in different topics in this forum. However, when I run the code with my mod file it gives me the following error:

Unable to perform assignment because the left and right sides have a different number of elements.
Error in dyn_first_order_solver (line 227)
info(2) = temp’*temp;
Error in stochastic_solvers (line 262)
[dr,info] = dyn_first_order_solver(jacobia_,M_,dr,options_,task);
Error in resol (line 120)
[dr,info] = stochastic_solvers(dr,check_flag,M,options,oo);
Error in welfare_objective (line 17)
[oo_.dr,info,M_,options_,oo_] = resol(0,M_,options_,oo_); %get decision rules
Error in csminwel (line 62)
f0 = fcn(x0,varargin{:});
Error in codenew.driver (line 436)
[fhat,x_opt_hat] = csminwel(@welfare_objective,x_start,H0,[],crit,nit,x_opt_name);
Error in dynare (line 293)
evalin(‘base’,[fname ‘.driver’])

I do not understand where the problem comes from. I will be thankful if someone can help me with this issue. I have attached the mod file and the welfare function. Two other files compute the steady state values.

codenew.mod (7.3 KB)
codenew_steadystate.m (1.7 KB)
fun_code.m (1.8 KB)
welfare_objective.m (1.1 KB)

In Dynare 5.1, you need to use
welfare_objective.m (1.1 KB)

The problem was the missing setting of qz_criterium.

1 Like

Thank you, professor.

It works fine now. However, whenever I change the initial values in x_start, I get different optimized parameters (i.e., in x_opt_hat). Moreover, when I set the same initial value for the parameters, I always get the same optimized values for the parameters. Does this imply a problem in the code?

Does the code even work for your model? When I ran it, it always triggered an error code. Does a simulated version of the model work?

Yes, it is working. When I ran it with Dynare 4.6.4, it didn’t work. I installed Dynare 5.1 and then I got the results without the error.

But the value of the objective function was the 1e6 that indicates a problem in the model.

I cannot understand which variable indicates the value of the objective function equal to 1e6.

I have tried the same code with a simpler mod file. The simulated version works without any error; however, I still face the same values for optimized parameters. I still get the same results even when I change the value that is supposed to be optimized inside the welfare_objective_1.m. So the results are the same no matter what I change inside welfare_objective_1.m.

I have another question too, do I need to make the parameters I am searching for equal to zero in the parameters block?

welfare_objective_1.m (1.1 KB)
fun_megabe.m (2.2 KB)
giowithouttax_steadystate.m (1.7 KB)
giowithouttax.mod (8.2 KB)


Your starting value is invalid. Try

x_start=[1.5, 0.25]';
1 Like

Thank you. It is working with these starting values-just one more questions: I get a very large value for the parameter responding to inflation in the monetary rule, e.g., 500 or so. Does it make any sense theoretically?

Have a look at


Dear Professor,

Thank you again. I have some follow-up questions regarding conditional and unconditional welfare. As far as I know and have read throughout the forum, unconditional welfare is the mean of a simulated series of welfare. Is this right?

Second, is the welfare maximized by welfare_objective.m (mentioned in the previous comments), conditional or unconditional?

I am also confused about the sign of welfare because the value given by fhat from this function is always positive when I use it in my code. However, the mean simulated welfare that I suppose to be unconditional is always negative. If I plot the value of welfare when a parameter changes using the following loop:

clear all
dynare giowithouttax 
% get position of welfare in the variable list.
%% rho_r

 grid = 0.0:0.1:0.9;
 % mean of welfare
 % matrix for parameters values and correspondoing welfare values

first_time = 1;

for i = 1:length(grid)
    if first_time           
        dynare giowithouttax noclearall;
        first_time = 0;
        [info, oo_] = stoch_simul(M_, options_, oo_, var_list_);
        if info
          disp(['Computation fails for rho_r = ' num2str(rho_r)]);

The values of parameters maximizing the welfare are more or less similar to those given by x_opt_hat, but the welfare value is always negative. Is there any problem in the code or maybe in my understanding?

  1. Yes, unconditional welfare can be computed as the mean in a very long simulation.
  2. The welfare_objective above compute conditional welfare at the steady state.
  3. The sign of lifetime welfare is not determined. It can be positive or negative. We only know that higher is better.
1 Like

Thank you and sorry for too many questions.

Searching for the parameters maximizing welfare is sensitive to the size of shocks, and the values of the optimized parameters change every time I change the shock size. How should I know the true value of shocks? For the first-order, I choose 1. Does using 0.01 as the standard error of the Taylor rule’s shock make sense for the second-order approximation and welfare analysis?

Moreover, if I have a log utility function and two different (optimal) policies, can I simply take the mean of unconditional welfare from each policy and calculate consumption equivalence based on the following equation?

CE = exp ((1-BETA) * (mean.W(alternative) - mean.W(benchmark))) - 1

If this method is valid for unconditional welfare, are these results equivalent to those we might get from conditional welfare?

I mean, can I take fhat from welfare_objective of two different optimized policies and calculate CE based on the equation above?

  1. Yes, shock sizes matter for welfare. You need to find a realistic calibration. 1 is almost always wrong as it implies 100 percent, which is a problem with higher order approximations.
  2. Yes, with log utility that should work.
  3. No, conditional and unconditional welfare are different concepts. You can compute the consumption equivalent with the same formula by plugging in the respective numbers, but you cannot infer one from the other.
1 Like

Dear Prof Pfeifer,
I have a simple question but I am not sure. Is it different if we do all the above steps with 2 shocks vs 1 shock in the shock sector:

var shock_R;
stderr 1;
var shock_A;
stderr 1;


%var shock_R;
%stderr 1;
var shock_A;
stderr 1;

Instead of productivity and interest rate shock, we only have TFP shock.
Because the variance is different but I am not sure if it affects the mean of welfare. Thank you so much.

Yes, welfare depends on higher order properties like variances. Thus, it matters which shocks can arise over time.