# Using dynare for a solver for OSR and calibration

**URL:** <https://forum.dynare.org/t/using-dynare-for-a-solver-for-osr-and-calibration/4231>\
**Category:** Optimal Policy\
**Created:** [17 October 2014 10:21 UTC](https://forum.dynare.org/t/using-dynare-for-a-solver-for-osr-and-calibration/4231 "2014-10-17T10:21:32Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![dominik](https://forum.dynare.org/letter_avatar_proxy/v4/letter/d/ee7513/32.png) [@dominik](https://forum.dynare.org/u/dominik)\
**Post date:** [17 October 2014 10:21 UTC](https://forum.dynare.org/t/using-dynare-for-a-solver-for-osr-and-calibration/4231/1 "2014-10-17T10:21:32Z")

</div>

Hi

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?

Thanks  
Dominik

---

<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:** [17 October 2014 18:50 UTC](https://forum.dynare.org/t/using-dynare-for-a-solver-for-osr-and-calibration/4231/2 "2014-10-17T18:50:44Z")

</div>

See [[Initial values](https://forum.dynare.org/t/initial-values/4230/1)) and the links therein

---

<div class="post-metadata">

**Author:** ![dominik](https://forum.dynare.org/letter_avatar_proxy/v4/letter/d/ee7513/32.png) [@dominik](https://forum.dynare.org/u/dominik)\
**Post date:** [20 October 2014 13:33 UTC](https://forum.dynare.org/t/using-dynare-for-a-solver-for-osr-and-calibration/4231/3 "2014-10-20T13:33:20Z")

</div>

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

---

<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:** [20 October 2014 14:00 UTC](https://forum.dynare.org/t/using-dynare-for-a-solver-for-osr-and-calibration/4231/4 "2014-10-20T14:00:23Z")

</div>

You are right. I meant to link to [[Loop over parameters](https://forum.dynare.org/t/loop-over-parameters/3335/1)) and an example linked at [[Prevent dynare from storing results](https://forum.dynare.org/t/prevent-dynare-from-storing-results/4234/1))

---

<div class="post-metadata">

**Author:** ![dominik](https://forum.dynare.org/letter_avatar_proxy/v4/letter/d/ee7513/32.png) [@dominik](https://forum.dynare.org/u/dominik)\
**Post date:** [20 October 2014 15:25 UTC](https://forum.dynare.org/t/using-dynare-for-a-solver-for-osr-and-calibration/4231/5 "2014-10-20T15:25:16Z")

</div>

Thanks!  
I found the example from your paper the most instructive. [sites.google.com/site/pfeiferec … edirects=0](https://sites.google.com/site/pfeiferecon/20140525_replication_codes.zip?attredirects=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

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

%% set parameter for use in Dynare
set_param_value('phi1',xopt(1));

[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 
else
       % 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
end
```

---

<div class="post-metadata">

**Author:** ![dominik](https://forum.dynare.org/letter_avatar_proxy/v4/letter/d/ee7513/32.png) [@dominik](https://forum.dynare.org/u/dominik)\
**Post date:** [27 October 2014 11:09 UTC](https://forum.dynare.org/t/using-dynare-for-a-solver-for-osr-and-calibration/4231/6 "2014-10-27T11:09:32Z")

</div>

Hi,

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:

```auto
if isempty(options_.qz_criterium)
    options_.qz_criterium = 1+1e-6;
end  
options_.noprint=1;
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 ;  
and  
uA by uA\*sigma\_A

---

<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:** [30 October 2014 09:11 UTC](https://forum.dynare.org/t/using-dynare-for-a-solver-for-osr-and-calibration/4231/7 "2014-10-30T09:11:25Z")

</div>

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
`

---

<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:** [2 November 2014 12:24 UTC](https://forum.dynare.org/t/using-dynare-for-a-solver-for-osr-and-calibration/4231/8 "2014-11-02T12:24:30Z")

</div>

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

```auto
set_param_value('nu',nu);
oo_.osr = osr(var_list_,osr_params_,obj_var_,optim_weights_);
```
