Advanced Dynare Handout
Advanced Dynare Handout
Tom Holden
Introduction
This section
• In this section, I will introduce you to some of Dynare’s less well known
features, including:
• The pre-processor.
• Model local variables.
• We will also look at hybrid simulation methods, and assess the accuracy of
Dynare’s different approximations.
1
9/4/2015
• Before the .mod file is passed to the main Dynare processing engine, it first
goes through a pre-processor.
• This parses the original .mod file and generates a new .mod file from it.
2
9/4/2015
• Pre-processor arrays may be dereferenced with MATLAB style syntax, but with
square brackets.
• E.g. MyArray[3] or MyArray[4:8].
• The length operator returns the length of an array.
• E.g. length(MyArray).
• The pre-processor also supports integer variables (not doubles!), which support
standard arithmetic and logical operations.
• Note that negation is ! not ~.
3
9/4/2015
• Model local variables (MLVs) are defined by lines starting with a “#” and
ending with a semi-colon.
• E.g. #PI = exp(pi);.
• Model local variables are interpreted by the main Dynare processor, and they
can only occur within the model block.
• Dynare effectively copies and pastes the contents of the model local variable
into all of the equations in which it is used, surrounding the entire variable
with brackets.
4
9/4/2015
• More generally, we might first linearly transform the source variable, then take
logarithms or logits, then linearly transform the result.
5
9/4/2015
6
9/4/2015
• The original .mod file is NK.mod, and the version using our transformation engine
is in NKTrans.mod.
• In the model block, we just have to perform a search and replace of (+1)
for _LEAD and (-1) for _LAG.
• Note that we had to slightly modify the steady_state_model block
because Dynare does not allow us to introduce new variables in the
steady_state_model block with the same name as MLVs.
7
9/4/2015
• Defining the laws of motion for shock processes is tedious, particularly when
the shocks processes are transformed, as they are here.
Dynare’s internals
Why is understanding Dynare’s internals useful?
• It is also helpful to know Dynare’s internals so that you can better understand
why Dynare acts the way it does.
8
9/4/2015
• evaluate_steady_state.m
• Finds the model’s steady-state, either via calling a user provided
_steadystate.m file, an automatically generated steadystate2.m file, or
via a numerical search using dynare_solve.m.
• dynare_solve.m
• Finds the model’s steady-state via a numerical search.
• It is easy to customise this file to use an alternative solver, such as
CMA-ES.
• resid.m
• Returns the residuals of the model’s static equations at the steady-
state.
• Internally calls the automatically generated _static.m file.
• Useful for debugging a steady-state.
• resol.m
• First finds the model’s steady-state (using evaluate_steady_state.m), then
solves the model, via a call to stochastic_sovers.m.
• Returns the structure dr with the model’s transition matrices, and a flag
info which reports problems solving the model.
• We will modify this file in the non-linear estimation section in order to
implement two alternative solution procedures.
• stochastic_sovers.m
• Does the work of solving the stochastic model.
• Placing breakpoints here can help debug failures of the BK conditions.
• simult.m
• Responsible for simulation of stochastic models.
• Useful to modify this file if you want to use specific shocks for a
simulation.
• The work is done in simult_.m, which we will look at in more detail shortly.
9
9/4/2015
• simul.m
• Responsible for simulation of deterministic models.
• If you develop a new algorithm for solving deterministic models, you
might modify this file.
• Most of the work is done in sim1.m, when using the default algorithm.
• sim1.m
• Does the work of simulating the deterministic model.
• Placing breakpoints here can help understand failures of
convergence.
• Additionally, it sometimes helps to change the line dy = -A\res; to
use a more robust solver.
• The provided dynare repository contains two, lin_solve and
lin_solve_robust which can be used with dy = -
lin_solve(A,res); and dy = -lin_solve_robust(A,res);.
• dsge_likelihood.m
• Returns the negative log-likelihood to a first order approximation to the
model.
• dynare_resolve.m is used to solve the model, which expands the
transition matrices from resol.m into square matrices.
• There is a lot of scope for customising this file, including:
• Modifying the initial covariance matrix of the state, through adding
extra options to the switch DynareOptions.lik_init block.
• Changing what happens when the BK conditions are not satisfied, by
e.g. returning the likelihood under some indeterminate solution.
• Better handle errors using try { } catch { } blocks.
• Adding custom priors by subtracting something from xparam1.
• Adding a custom prior penalising nearly non-stationary models via
subtracting some increasing function of log(1-abs(eigs(T,1))).
• non_linear_dsge_likelihood.m
• Returns the approximate negative log-likelihood of a non-linear model via
the particle filter.
10
9/4/2015
• oo_ contains details about the results of solving the model, including:
• Its steady state in oo_.steady_state, its mean in oo_.mean, and its
covariance in oo_.var.
• All in declaration order.
• The auto-covariance of the model’s variables in oo_.auto_corr.
• Row i of oo_.auto_corr{n} gives the correlation between the i th
endogenous variable and the nth lag of all endogenous variables.
• The steady state of the exogenous variables in
oo_.exo_steady_state for stochastic exogenous variables, and in
oo_.exo_det_steady_state for deterministic exogenous variables.
• The generated simulation paths in oo_.endo_simul (for endogenous
variables), oo_.exo_simul (for stochastic exogenous variables) and
oo_.exo_det_simul (for deterministic exogenous variables).
• Each matrix has columns indexed by time.
• The generated impulse responses in oo_.irfs.
11
9/4/2015
• The most important part of oo_ is oo_.dr which stores the decision rules of the
model.
• The matrices dr.ghx and dr.ghu play a similar role to and in the
VAR(1) ݔ௧ ൌ ݔ௧ିଵ
௧.
• However, dr.ghx is not in fact square, and its rows and columns are not
in declaration order!
• Suppose yOld is the value of all endogenous variables at the end of last period,
in declaration order.
• Then yOldDr = yOld( dr.order_var ); is the values of these old
variables in “dr order”.
• dr.order_var maps dr order to declaration order.
• dr.ghx has as many columns as there are state variables in the model.
• In dr order, all of the state variables are next to each other, immediately
after all of the static variables.
• Using this information, can you produce a vector that indexes all of the
state variables in dr order?
• Have a go at writing code to generate simulation paths for the first order
solution to a model.
12
9/4/2015
• We have already seen that third order approximations can lead to explosive
paths.
• To see the intuition for this, suppose that ݔ௧ evolves according to an equation
of the form ݔ௧ ൌ ⋯ ܣଶ ݔ௧ିଵ ⊗ ݔ௧ିଵ⋯, then from iterating this equation
we have that:
• ݔ௧ ൌ⋯ܣଶ ܣଶ ⊗ܣଶ ݔ௧ିଶ ⊗ݔ௧ିଶ ⊗ݔ௧ିଶ ⊗ݔ௧ିଶ ⋯
• ݔ௧ ൌ ⋯ܣଶ ܣଶ ⊗ ܣଶ ܣଶ ⊗ ܣଶ ⊗ ܣଶ ⊗ ܣଶ ሺݔ௧ିଷ ⊗ ݔ௧ିଷ ⊗ ݔ௧ିଷ ⊗
ݔ௧ିଷ ⊗ ݔ௧ିଷ ⊗ ݔ௧ିଷ ⊗ ݔ௧ିଷ ⊗ ݔ௧ିଷሻ ⋯
• “Pruning” is one solution to this problem, first introduced by Kim et al. (2008).
• There are many different pruned solutions, but all of them are
designed to remove these high order terms in past states.
• Given the multiple ways of performing pruning, any particular pruned solution can
appear a little ad hoc.
• Lan and Meyer-Gohde (2013b) give a proper foundation for a particular type of
pruned solution.
• Suppose we introduce an extra parameter to our model which pre-
multiplies all of the model’s shocks.
• ൌ 1 corresponds to the original solution.
• The traditional unpruned perturbation solution is formed by taking a Taylor
approximation to the (unknown) function ݔ௧ ൌ ܲ , ݔ௧ିଵ, ௧around ൌ 0,
ݔ௧ିଵ ൌ ݔ, ௧ ൌ 0 (e.g. the non-stochastic steady state).
• Lan and Meyer-Gohde (2013b) propose to instead take a Taylor
approximation to the (unknown) function ݔ௧ ൌ ݔ , ௧, ௧ିଵ, … , again
around ൌ 0, ௧ ൌ ௧ିଵ ൌ ⋯ ൌ 0.
• It turns out that the Taylor approximation to this non-linear moving
average representation has a recursive representation of the same broad
form of the pruned solutions previously considered in the literature.
• Lan and Meyer-Gohde (2013a) show that their pruned solution is more
accurate than all of the others considered in the literature.
13
9/4/2015
• Lan and Meyer-Gohde released a Dynare toolkit for their solution method.
• A version of this code is available from
https://github.com/tholden/nlma or from the nlma folder.
• The easiest way to use this toolkit is with the help of another toolkit by me for
handling occasionally binding constraints (OBC) in DSGE models.
• This toolkit is available from https://github.com/tholden/OBCToolkit of
from the OBCToolkit folder.
• The toolkit takes care of changing calls to stoch_simul to calls to the
NLMA toolkit, even in models without OBC.
• To use the OBCToolkit on a model without OBC, it is best to invoke it with:
dynareOBC ModFileName.mod irfsaroundzero.
• It can also handle taking a first order approximation around the risky steady-
state or the mean (using code from Meyer-Gohde (2014)) by setting order=2
or order=3 in the .mod file and then calling: dynareOBC ModFileName.mod
irfsaroundzero firstorderaroundrss or …
firstorderaroundmean.
14
9/4/2015
• If we examine one of these files, we see that it contains code to evaluate the
exact value of each MLV in terms of the function’s inputs.
• Suppose we wish to simulate the MLVs Y, C, PI and R from NKTrans3. Modify the
generated _dynamic.m file so that it returns these four MLVs in a vector.
• Once we have a function that returns the MLVs we need, it is then quite straight-
forward to write code to calculate the path of MLVs along a simulation.
• Code to do this is shown in SimulateMLVs.m.
• As an alternative to manual editing of the _dynamic.m files, the OBC toolkit can
automatically generate simulations for MLVs.
• Just add their names after the call to stoch_simul as you would with an
endogenous variable, and then call dynareOBC with
MLVSimulationMode=1.
• It can also generate average IRFs for model local variables via the
slowirfs option.
• Finally, it can generate simulations for the expected value of a forward
looking model local variable if called with MLVSimulationMode=2 or
MLVSimulationMode=3.
• We will use the Jin and Judd (2002) method for assessing the accuracy of an
approximation.
• For each equation of the form lhs௧ ൌ ॱ௧rhs௧ାଵ, we form the
lhsିॱrhsశభ
normalized, dimension free, error error௧ ൌ
lhs
• More generally for each equation of the form lhs௧ ൌ rhs௧ (where either
or both sides may contain expectations), we form the normalized,
lhsିrhs
dimension free, error error௧ ൌ
lhs
• These should be zero over the entire state space.
• In practice, we just check how close to zero they are over a simulation
path.
• To evaluate these errors we use the capabilities of the author’s OBC toolkit.
• Have a go at modifying NK.mod and NKTrans4.mod to calculate the
error for each of the 16 original equations.
15
9/4/2015
• Our transformation appears to have decreased the accuracy of the Euler equation
and the Taylor rule.
• The nominal interest rate is the obvious suspect.
• In hindsight, it is unsurprising that the ↦ݔlog ݔെ 1 transformation reduced
accuracy.
• The Taylor rule is log-linear when the ZLB does not bind.
• With this transformation, it becomes highly non-linear.
• Imposing the bound this way is not worth the accuracy cost away from the
bound.
• OBC cannot be well approximated by smooth functions!
• Luckily the OBC toolkit is designed to impose OBC in a way that does
preserve accuracy. You will learn more in the OBC section later in the
course.
• If we remove the OBC (or ignore it), how should we modify NKTrans4.mod to
increase its accuracy?
• Can you see any other changes we could make to increase accuracy (and
speed!)?
16
9/4/2015
17