Eric Simson Dyn Are
Eric Simson Dyn Are
Eric Simson Dyn Are
Spring 2014
1 Introduction
This document will present some simple examples of how to solve and simulate, DSGE models
using Dynare. Dynare can also be used to estimate the parameters of DSGE models via Maximum
Likelihood or Bayesian Maximum Likelihood, but that is beyond the purview of a first year course.
Dynare is not its own program but is rather basically a collection of Matlab codes. To run
Dynare, you must first install it. You can download it from the following website: http://www.
dynare.org/download. I will only support downloads using Matlab. You need to choose a
destination folder. My destination folder is in the C drive and is called “dynare”. I have version
4.4.1. The full path is: “C:\dynare \4.4.1 \matlab ”. You should download the latest stable version
of Dynare.
To run Dynare, you have to create .mod files (you can create these in the m-file editor, but be
sure to save them as .mod, not as .m files). Then, to run your code, simply type “dynare filename”
into the command window (or you can have this command within a seperate m-file which can be
in another directory so long as you call up the Dynare directory). Equivalently, you can “set the
path” in Matlab so that you don’t always have to call up the Dynare directory.
s.t.
1
TFP is assumed to follow a mean zero AR(1) in the log:
ln at = ρ ln at−1 + εt
The first order conditions for this problem can be characterized with three non-linear difference
equations and a transversality condition:
c−σ
t = βEt c−σ α−1
t+1 (αat+1 kt+1 + (1 − δ))
In addition, I have two auxiliary “static” variables that define output from the production
function and investment from the accounting identity:
yt = at ktα
it = yt − ct
The parameters that need to be calibrated are σ, α, δ, β, ρ, and σε . I set the coefficient of
relative risk aversion to 1 (log), capital’s share to 1/3, the depreciation rate to 0.025, the discount
factor to 0.99, the persistence of technology shocks to 0.95, and the standard deviation of technology
shocks to 0.01 (i.e. one percent).
c−σ
t = βEt c−σ α−1
t+1 (αat+1 kt + (1 − δ))
α
kt = at kt−1 − ct + (1 − δ)kt−1
ln at = ρ ln at−1 + εt
lim β t c−σ
t kt = 0
t→∞
α
yt = at kt−1
it = yt − ct
2
In other words, we essentially just lag the capital stock one period in each relevant equation.
kt is just re-interpreted as kt+1 .
c−σ
t = β(1 + rt+1 )Et c−σ
t+1
In the notation I’ve used in class, rt+1 is the interest rate that pays out in period t + 1, but it
is known at time t, so we can bring it outside of the expectation operator. To get Dynare to work
properly, we would need to revise our timing convention where rt is the interest rate known at t
that pays off in period t + 1, so we would write the Euler equation:
c−σ
t = β(1 + rt )Et c−σ
t+1
In terms of the budget constraint, you would just have rt−1 Bt paying the interest earned (or
owed, if Bt < 0) on bonds at time t. Anything determined and therefore known at time t needs
to be dates as such in Dynare notation. Anything pre-determined at time t (known and not the
outcome of equilibrium, like the capital stock) needs to be dated t − 1.
1 var y i k a c;
The next line of code specifies the exogenous variables. These are just the shocks, and in this
model I only have one:
3
1 varexo e;
The next step is to declare the parameters of the model. You simply type “parameters” followed
by parameter names:
Immediately following this command, you need to specify numerical values for these parameters:
1 alpha = 0.33;
2 beta = 0.99;
3 ∆ = 0.025;
4 rho = 0.95;
5 sigma = 1;
6 sigmae = 0.01;
Now that you’ve named variables and specified parameter values, it’s now time to “declare
the model”. To do this, you type in “model;” followed by the first order conditions, constraints,
identities, etc., followed by “end;”. We typically want to approximate the solutions of the natural
logs of the variables, so that impulse responses, etc. are all in percentage terms. Dynare will do
linear approximations of the levels of the variables. To get it to do linear approximation of the
logs of the variables, you need to specify the variables as “exp(x)”. This way the variable x is
interpreted as the log of the variable of interest, while exp(x) is the level (since the exponential
and log are inverse functions), which is what shows up in most of the FOCs. Then you just type
in the first order conditions. If a variable appears dated t, then you just type x. If a variable is
t + 1, you type x(+1); if it’s t − 1, then x(−1). If, for whatever reason, you need variables beyond
one period away to appear, you could define auxiliary state variables to make the system work out
this way. My first order conditions for the above model look like:
1 model;
2
9 end;
Dynare interprets all of the underlying variables here as logs. Since I specified the stochastic
process for at in the log, I do not need to write it using the exponential. Dynare will solve for the
steady state numerically for you. To get it to do this you need to give it guess of the steady state.
If you give it bad guesses, Dynare may not converge and you can get an error. When giving initial
4
values, remember that Dynare is interpreting all the variables as logs. Hence, if you know that
steady state capital is 30, you want to give an initial value of capital somewhere near ln 30, not 30.
you begin this part of the code with the command “initval;”, followed by the initial values/guesses
of all the endogenous variables, capped off with “end”. My code for this part looks like:
1 initval;
2
3 k = log(29);
4 y = log(3);
5 a = 0;
6 c = log(2.5);
7 i = log(1.5);
8
9 end;
The next step is to specify the variance of the shocks. This part of the code starts with “shocks;”,
followed by a specification of the variance (not standard deviation), followed by “end;”:
1 var e = sigmaeˆ2;
In the next step you simply type in “steady;”. This command calculates the steady state values
of the endogenous variables of the model:
1 steady;
The next command is the payoff. It’s the “stoch simul” command, which is what solves the
model, produces the policy functions, and generates impulse responses functions and unconditional
second moments. There are a number of options following this command. If you just type in
“stoch simul;”, it’s going to give you the default output. The default output is: (1) steady state
values of endogenous variables; (2) a model summary, which counts variables by type; (3) covariance
matrix of shocks (which in the example I’m giving is a scalar); (4) the policy and transition functions
(in state space notation); (5) theoretical first and second moments; (6) a theoretical correlation
matrix; (7) theoretical autocovariances up to order 5; and (8) impulse responses. By default,
Dynare does a second order approximation about the steady state; the second order approximation
involves “cross terms” , and so the policy/transition functions look more complicated than you’re
used to seeing.
There are a number of options one can include behind “stoch simul;” (to do this you type
“stoch simul(options);”. There are several of these that you can read about in the manual, but
the more important ones for our purposes are:
hp filter = integer: this will produce theoretical moments (variances, covariances, autocor-
relations) after HP filtering the data (the default is to apply no filter to the data). We
5
typically want to look at HP filtered moments, so this is an option you’ll want to use. The
integer is a number (i.e. 1600) corresponding to the penalty parameter in the HP filter. So,
for example, typing “stoch simul(hp filter=1600);” will produce theoretical (i.e. analytical)
moments of the HP filtered data. The only problem here is that it will not simultaneously do
HP filtering of simulated data; you can either get moments from simulated data or analytical
HP filtered moments
irf = integer: this will change the number of periods plotted in the impulse response functions.
The default value is 40. To suppress printing impulse responses altogether, type in 0 for
the number of horizons. For example, to see impulse responses for only 20 periods, type
“stoch simul(irf=20);”.
nocorr: this will mean it will not print a matrix of correlations in the command window
nofunctions: this will suppress printing the state space coefficients of the solution in the
command window
order = 1 or 2: this tells Dynare the order of the (log) approximation. The default is a second
order approximation. Hence, typing “order=1” will have it do a linear approximation.
drop = integer: You can change the number of observations to drop from the simulations.
For example, typing “stoch simul(drop=0);” will result in no observations being dropped in
the simulations.
simul seed = integer: sets the seed used in the random number generator for the simulations.
6
st = Ast−1 + Bεt
xt = Φst
xt = ΦAst−1 + ΦBεt
It is easily verified that these matrix multiplications are permissable; Φ is n×m and A is m×m.
Hence ΦA, call it C, is n × m. B is m × w; hence ΦB is n × w. Call ΦB = D. Then we can write
out the full system as:
st = Ast−1 + Bεt
xt = Cst−1 + Dεt
We can combine this into one expression by denoting Yt = [st xt ]0 , Ψ = [A C]0 , and Ω =
[B D]0 :
Yt = Ψst−1 + Ωεt
3 % Eric Sims
4 % University of Notre Dame
5 % Spring 2011
6
7 var y i k a c;
8 varexo e;
9
12 alpha = 0.33;
7
13 beta = 0.99;
14 ∆ = 0.025;
15 rho = 0.95;
16 sigma = 1;
17 sigmae = 0.01;
18
19 model;
20
27 end;
28
29 initval;
30
31 k = log(29);
32 y = log(3);
33 a = 0;
34 c = log(2.5);
35 i = log(1.5);
36
37 end;
38
39 shocks;
40 var e = sigmaeˆ2;
41 end;
42
43 steady;
44
4 Running Dynare
To run Dynare you can’t hit F5 or type the name of the .mod file into the command prompt. You
have to type “dynare filename” (without the .mod on the end of the file name). For this to run
you must either be in the same directory where your Dynare files are stored, you must have set
the path of where your Dynare files are stored, or you must type “addpath (directory)” where the
directory is the location of your Dynare files. It is also helpful to add “noclearall” to the declaration
so that you are typing in “dynare filename noclearall”. Otherwise Dynare will automatically erase
all previous output, which you may not want.
So as to do other things, I usually write a .m file to go along with my .mod file. My code for
the .m file is simple:
8
1 % Running Dynare
2
3 addpath c:\dynare\4.1.1\matlab
4
5 dynare basic nc
5 Dynare Output
Dynare will produce a bunch of output in the command window for you (unless you include options
discussed above to suppress it). Below is the output that I get:
STEADY-STATE RESULTS:
y 1.10371
i -0.344308
k 3.34457
a 0
c 0.835782
9
MODEL SUMMARY
Number of variables: 5
Number of stochastic shocks: 1
Number of state variables: 2
Number of jumpers: 2
Number of static variables: 2
Variables e
e 0.000100
Variables y i k a c
y 1.0000 0.9903 0.3618 0.9956 0.9256
i 0.9903 1.0000 0.2285 0.9989 0.8640
k 0.3618 0.2285 1.0000 0.2730 0.6877
10
a 0.9956 0.9989 0.2730 1.0000 0.8862
c 0.9256 0.8640 0.6877 0.8862 1.0000
Order 1 2 3 4 5
y 0.7217 0.4846 0.2868 0.1257 -0.0018
i 0.7107 0.4670 0.2663 0.1050 -0.0208
k 0.9596 0.8619 0.7268 0.5704 0.4058
a 0.7133 0.4711 0.2711 0.1098 -0.0163
c 0.7941 0.5994 0.4206 0.2610 0.1222
Total computing time : 0h00m10s
The “constant” in the policy and transition functions output is just the steady state values;
the coefficients of the (modified) state space representation are easy to understand. In addition,
Dynare produces means (steady state values), volatilities (standard deviations), and variances
for each endogenous variable. It also produces the matrix of correlations as well as coefficients
of autocorrelation at different lags. Given the options I specified, all of these moments are (i)
theoretical (in the sense of being analytical, not based on a simulation) and (ii) based on HP
filtered data.
Dynare also produces impulse responses to the shocks. These are shown here:
y i
0.015 0.04
0.03
0.01
0.02
0.005
0.01
0 0
5 10 15 20 25 30 35 40 5 10 15 20 25 30 35 40
−3
x 10 k a
8 0.015
6
0.01
4
0.005
2
0 0
5 10 15 20 25 30 35 40 5 10 15 20 25 30 35 40
−3
x 10 c
6
0
5 10 15 20 25 30 35 40
There is one interpretive issue in the IRFs of which you need to be aware. Recall that Dynare
interprets kt as kt+1 . Hence, the impulse response of kt that it produces is really the impulse
response of kt+1 . To get the impulse responses of kt , you need to (i) add a zero as the impact
11
response (remember the capital stock can’t react on impact as it is predetermined) and (ii) set the
response of kt at horizon h equal to the response of what Dynare gives as the impulse response in
period h + 1.
1 m = 2; % number of states
2 n = 3; % number of controls
3
4 psi = oo .dr.ghx;
5 omega = oo .dr.ghu;
6 ss = oo .dr.ys;
7
10 es = sigmae*randn(T,1);
11 Xsim = zeros(n+m,T);
12
13 Xsim(:,1) = omega*es(1,1);
14
15 for j = 2:T
16 Xsim(:,j) = psi*Xsim(3:4,j−1) + omega*es(j,1);
17 end
18
19 % add back in steady state values so that these are expressed as log
20 % levels, not log deviatins
21 for j = 1:T
22 Xsim(:,j) = Xsim(:,j) + ss;
23 end
12
Consumption Output
1 1.3
1.25
0.95
1.2
0.9
1.15
0.85
1.1
0.8 1.05
0 50 100 150 200 0 50 100 150 200
−0.1
3.5
−0.2
−0.3 3.45
−0.4 3.4
−0.5
3.35
−0.6
−0.7 3.3
0 50 100 150 200 0 50 100 150 200
The simulations have some of the features we might expect – consumption is “smoother” than
output, which is less volatile than investment. The capital stock evolves very smoothly as well.
Dynare will also directly do simulations for you if you want. For example, if I type in
“stoch simul(order=1,irf=40,periods=200)” it will generate a simulation of 200 periods of the vari-
ables of the model (actually it will produce a simulation of 203 periods; I don’t fully understand
why it adds the three periods). If you use this command then it produces moments (standard
deviations, correlations, and autocorrelations) off of the simulated data.
7 Advanced Topics
In this section I discuss a couple of more “advanced” issues when using Dynare.
13
Suppose you start in a .m file and want to specify the parameters of the model there. I would
write the following code:
1 alpha = 0.33;
2 beta = 0.99;
3 ∆ = 0.025;
4 rho = 0.95;
5 sigma = 1;
6 sigmae = 1;
7
The last command saves the parameters as .mat file. You can modify the .mod file to accept
these parameters as follows:
3 load parameterfile;
4 set param value('alpha',alpha)
5 set param value('beta',beta)
6 set param value('∆',∆)
7 set param value('rho',rho)
8 set param value('sigma',sigma)
9 set param value('sigmae',sigmae)
The .mod file should run using the parameters saved in parameterfile.mat. You can, of course,
override those parameters within the structure of the .mod file. What is nice about doing this is
that, once you’ve run the .mod file once for a given set of parameters, if your code is specified
this way you can run it again for different parameters by just running the generated .m file and it
will use the parameter values. If you aren’t saving the parameters values, if you run the .mod file
once, then change the parameters in the .mod file and then run the .m file, the .m file will use the
original parameters from when the .mod file was first run. This way allows you to get around that.
For example, you can use this to write loops. You can solve the model for a bunch of different
parameters values and save some statistics from each run, for example.
14
can compute the steady state given parameter files. It needs to have the same name as the .mod
file followed by “ steadystate”. So, for example, if my .mod file is called “filename.mod” I need to
name the steady state file “filename steadystate.m”.
The general structure of the steady state file needs to look as follows (all of this information
needs to be there). You also need to eliminate (or comment out) the “initval” component of the
.mod file if you are going to specify your own steady state file.
3 global M
4
5 alpha = M .params(1);
6 beta = M .params(2);
7 ∆ = M .params(3);
8 rho = M .params(4);
9 sigma = M .params(5);
10 sigmae = M .params(6);
11
12 k = (alpha/(1/beta − (1−∆)))ˆ(1/(1−alpha));
13 y = kˆ(alpha);
14 i = ∆*k;
15 c = y − i;
16 a = 1;
17
18 check = 0;
19
20 ys = [log(y)
21 log(i)
22 log(k)
23 log(a)
24 log(c)];
15