Using dynare for a solver for OSR and calibration


for my project i need to search for the optimal simple monetary policy rule and calibrate some moments using volatilities.
My initial approach for both types of problems was to use a solver like fmincon, since both are problems of the type
max f(x) for x element X
where x is a vector of parameters and f(x) is a subset of the dynare output.

Yet it seems you can not call dynare and access the dynare output inside a matlab function , at least i didnt manage.
Is there a way this can be done? If not is there a generic optimization function included in matlab?


See [Initial values) and the links therein

thanks for the reply, but i believe u posted the wrong link. it leads me to someone with a problem with initial values…

You are right. I meant to link to [Loop over parameters) and an example linked at [Prevent dynare from storing results)

I found the example from your paper the most instructive. … edirects=0

Here is a quick and easy example for anyone who wants to optimize over some output of dynare:
(In this example I optimize over a scalar between 1.1 and 10 using fmincon)

At the end of your usual mod file write

[fhat,xhat] = fmincon(@tominimize,1.5,],],],],1.1,10)

Furthermore save this matlab function in the current folder

global oo_ M_ options_ % get Dynare structures; used to pass them on to resol.m

%% set parameter for use in Dynare

[oo_.dr,info,M_,options_,oo_] = resol(0,M_,options_,oo_); %run model solution in Dynare

if info %solution was not successful
    fval=10e6+sum([xopt(1),xopt(2),xopt(3),log(xopt(4)) ].^2); %return with penalty 
       % compute objective function
    fval=oo_.steady_state(1); %this is an arbitary choice; here you can have e.g. some value function for optimal policy or some model moment for SMM


the above works fine for the OSR purpose (where all relevant statistics are in oo_.dr) if you want to do SMM you need to add some lines because resol doesnt update oo_ completely.
In particular the theoretical variance and mean are not recomputed.

Here is what you need to add to the above code after the resol comand to get the theoretical moments recomputed:

if isempty(options_.qz_criterium)
    options_.qz_criterium = 1+1e-6;
disp_th_moments(oo_.dr, M_.endo_names(1:M_.orig_endo_nbr, :));

Note that you cant change the shock variances with set_param_value. Hence write
var uA = sigma_A^2; by var uA=1 ;
uA by uA*sigma_A

A more general version that also allows for IRF matching would be to use

if isempty(options_.qz_criterium) options_.qz_criterium = 1+1e-6; end options_.noprint=1; set_param_value('nu',nu); % manually set the parameter that is looped over info=stoch_simul(var_list_); % get decision rules by directly calling Dynare routine directly instead of % all of Dynare if info~=0 error('The model did not correctly solve') end

In case of osr running osr with different starting values for a parameter nu, one would need inside of the loop:

oo_.osr = osr(var_list_,osr_params_,obj_var_,optim_weights_);