0% found this document useful (0 votes)
36 views24 pages

Dynare Intro

Uploaded by

k60.2112340606
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views24 pages

Dynare Intro

Uploaded by

k60.2112340606
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Introduction to Dynare

Advanced Macroeconomics Tutorials

Matteo Coronese
April 22, 2021
Scuola Superiore Sant’Anna
What is Dynare?

DSGE models can be rather painful (or just impossible) to solve


analytically, as they are typically involve a set of highly non-linear
equations.
Dynare is a set of Matlab functions which solves, simulates and estimates
these models for us.
In a nutshell:

• Solve a model: find the steady state values.


• Simulate a model: obtain time series for each variable or shock the
model and compute Impulse Response Functions (IRFs).
• Estimate a model: feed it with real-world data and obtain estimates
for model parameters.

2
Getting Started

First, tell Matlab where to find Dynare.1

• First option:
• In Windows: addpath c :/ dynare/4.x . y/matlab
• In Linux/GNU: addpath /usr/ lib /dynare/matlab
• In MacOS: addpath / Applications /Dynare/4.x . y/matlab
MATLAB will not remember this setting next time you run it, and you will
have to do it again.
• Second option: Select the “Set Path” entry in the “File” menu, then click on
“Add Folder…”, and select the matlab subdirectory of ‘your Dynare
installation. Apply the settings by clicking on “Save”. MATLAB will
remember this setting next time you run it.

Reference
For every doubt, refer to the comprehensive on-line Manual.
1 Current version to date is 4.6.1
3
How to Run Dynare

To work with Dynare, you typically write your code in a . mod file. You can write
your script as a regular Matlab . m file, just remember to save it as a . mod.
To run your script, just type in your Matlab command line

d y n a r e y o u r s c r i p t . mod

You will visualize results directly in the Matlab command window. Objects created
(e.g. simulated variables, theoretical moments etc) are directly available in your
Matlab workspace.
Clearly, you can create a separate . m file where you invoke different Dynare
scripts, to reproduce a more complex work-flow.

The . mod file has a very simple structure. Let’s go through it with a very simple
example.

4
A Simple RBC Model

Consider the following optimization problem:




c1−σ −1
max E0 βt t
ct
t=0
1−σ (1)
s.t. t − ct + (1 − δ)kt
kt+1 = at kα

with the representative consumer optimizing is life-time utility. The meaning of


the law of motion of capital is simply kt+1 = it + (1 − δ)kt , that is: previous capital
inherited, minus capital depreciation, plus investment. Thus

yt =at kα
t
(2)
it =yt − ct

Finally, TFP is assumed to follow a mean zero AR(1) in logarithms:

ln at = ρ ln at−1 + εt (3)

5
A Simple RBC Model II

F.O.C. from the maximization problem above yields

c−σ
t = βEt c−σ
t+1 (αat+1 kt+1 + (1 − δ))
α−1
(4)

We assume the transversality condition limt→∞ β t c−σ


t kt+1 = 0 holds true. Our
model can thus be expressed as a system with 4 equations and 4 variables.

c−σ
t =βEt c−σ
t+1 (αat+1 kt+1 + (1 − δ))
α−1
Euler Equation
kt+1 =at kα
t − ct + (1 − δ)kt Capital Accumulation
(5)
yt =at kα
t Production Function
ln at =ρ ln at−1 + εt Exogenous Process

6
The .mod file - Declare Variables

We are now ready to write our . mod file. The first thing you need to do is to declare
your endogenous variables, that is yt ,kt ,at and ct . This is done through the
command var:

var y , k , a , c ;

Remember
Always put a semicolon “;” at the end of each line. Otherwise Matlab will throw
an error.

Then declare your exogenous variables, that is those variables which are not
involved in the equilibrium: your shocks, in this case εt . This is done through the
command varexo:

v a r exo e ;

7
The .mod file - Declare Parameters

The next thing you need to declare is the set of model parameters, through the
command parameters. In our case, the set of parameters is given by α, β, δ, ρ, σ and
the standard deviation σ a of the shock εt .
parameters a l p h a b e t a d e l t a rho sigma sigma_a ;

Note
Unless specified, shocks are assumed to be normally distributed. In our case,
εt ∼ N(0, σa2 ).

After you declared your parameters, you can directly type their values below:
alpha = 0 . 3 3 ;
beta = 0 . 9 9 ;
delta = 0.025;
rho = 0 . 9 5 ;
sigma = 2 ;
sigma_a = 0 . 0 1 ;

8
The .mod file - Declare the Model

It’s now time to declare the structure of the model, i.e. its equation.
To to that, start your model section by typing model;, then type your equations and
close the model section through end;. Some timing conventions:

• Write you variables at current time (e.g. ct ) simply through their name (in this
case c).
• If a variable appears lagged (e.g. ct−1 ), write c (−1) .
• If a variable is leading (e.g. Et ct+1 ), write c (+1) .

Dynare requires that predetermined variables (i.e. state variables, like the capital
stock) show up as dated t − 1 in the time t equations and t in the t + 1 equations.
That’s how you indicate to Dynare which are your state variables.

• In our case, just lag capital stock in each equation.

Put another way, for stock variables, the default in Dynare is to use a “stock at the
end of the period” concept, instead of a “stock at the beginning of the period”
convention.

Beside these conventions, shall we write our model in levels or in logs? 9


A Quick Note on Log-Linearization

The process of log-linearization allows us to express variables as percentage


deviation from the steady state, which is quite handy, especially when we visualize
IRFs.
Why logs? Differences in logs can be interpreted as percentage deviations.
Consider a generic variable xt , with a steady state value x̄. Taylor expanding its
logarithm around x̄ yields:
1
ln x ≂ ln x̄ + (x − x̄)

x − x̄
ln x − ln x̄ ≂ ≡ x̂

Let’s try to log-linearize the production function yt = at kα
t . First take logs on both
sides
t →
yt =at kα
t →
ln yt = ln at kα
ln yt = ln at + α ln kt

Then Taylor-expand around steady state values ā,ȳ and k̄.


10
A Quick Note on Log-Linearization II

1 1 1
ln ȳ + (yt − ȳ) = ln ā + (at − ā) + α ln k̄ + α (kt − k̄) →
ȳ ā k̄
yt − ȳ at − ā kt − k̄
= +α →
ȳ ā k̄
ŷt =ât + αk̂t

Log-linearize your model by hand can be painful.


Now, Dynare only linearizes the model for you (i.e solves the model in the
neighborhood of the steady state, where the approximation error is small enough).
Ways to obtain log-linearization:

• As it is always true that exp(xt ) = Z ⇔ ln(Z) = xt , you can write every


variable xt in your model as exp xt . Dynare will interpret xt (the initial
variables you declared) as the log of the variable of interest Z. Remember to
change your initial values accordingly: if x0 = 4, write x0 = ln(4) (more on
that later).
• Just write your model normally, and then add the loglinear option to the
stoch_simul () command (more on that later). 11
A Quick Note on Log-Linearization III

Let’s verify that rewriting our variables will do the trick.

t →
yt =at kα
t ) →
exp (ln yt ) = exp (ln at ) exp (ln kα
exp (ln yt ) = exp (ln at ) exp (α ln kt ) →
1
exp (ln ȳ) + exp (ln ȳ)(yt − ȳ) = . . .

1
· · · = exp(ln ā) exp(α ln k̄) + exp(ln ā) exp(α ln k̄) (at − ā) + . . .

1
. . . + exp(ln ā) exp(α ln k̄) α(kt − k̄) →

[ ] [ ]
yt − ȳ at − ā kt − k̄
exp (ln ȳt ) 1 + = exp(ln ā) exp(α ln k̄) 1 + +α →
ȳ ā k̄
ŷt =ât + αk̂t

12
The .mod file - Declare the Model II

Remind that our equations are:

c−σ
t =βEt c−σ
t+1 (αat+1 kt+1 + (1 − δ))
α−1
Euler Equation
kt+1 =at kα
t − ct + (1 − δ)kt Capital Accumulation
yt =at kα
t Production Function
ln at =ρ ln at−1 + εt Exogenous Process

We thus declare the model as follows:


model ;
exp ( c ) ^( − sigma ) = b e t a ∗ ( exp ( c ( + 1 ) ) ^( − sigma ) ) ∗ ( a l p h a ∗ exp ( a ( + 1 ) ) ∗ ( exp ( k ) ) ^ ( a l p h a − 1 ) + ( 1 − d e l t a ) ) ;
exp ( k ) = exp ( a ) ∗ exp ( k ( − 1 ) ) ^ ( a l p h a ) − exp ( c ) + ( 1 − d e l t a ) ∗ exp ( k ( − 1 ) ) ;
exp ( y ) = exp ( a ) ∗ exp ( k ( − 1 ) ) ^ ( a l p h a ) ;
a = rho ∗ a ( − 1 ) + e ;
end ;

Note that the last equation reports a without exp () , as it was already defined in
logs.

13
The .mod file - The initval Block

Initial values are declared within the initval block, delimited by the commands
initval ; and end;. It has two main purposes:

• In both stochastic and deterministic (i.e. with perfect foresight) simulations,


it provides initial guess values for steady state computations.
• In deterministic simulations, provides guess values for non-linear solvers.

We here focus on stochastic simulations (i.e. simulations corresponding to a


random draw of the shocks).

• If you do not provide an initval block, the solver will use default ones. Bad
initial values might lead, in complicated models, to non-convergence.
• It is not necessary to declare 0 as initial value for exogenous stochastic
variables, since it is the only possible value.
• You have to use your knowledge of the model to write good initial conditions.

14
The .mod file - Declare Shocks

• Through the command steady, we get the (deterministic) steady state.


• In a stochastic framework, we study the effects of random draws of the
shocks. In Dynare, these random values follow a normal distribution with
zero mean, but it belongs to the user to specify the variability of these shocks.
• Non-zero elements of variance-covariance matrix of shocks are entered in the
shocks block (variances of single shocks and, if any, covariances among them).

initval ;
k = log (29) ;
y = log (3) ;
a = 0;
c = log (2.5) ;
end ;

steady ;

shocks ;
v a r e = sigma_a ^ 2 ;
end ;
15
The .mod file - Stochastic Simulation

Finally, trough the command stoch_simul () , we can finally obtain our stochastic
simulation. Default output:

• Steady-state values of endogenous variables.


• Model summary.
• Covariance matrix of shocks.
• Policy and transition functions.
• Theoretical first and second moments.
• Theoretical correlation matrix.
• Theoretical autocovariances up to order 5.
• Impulse responses.

16
The .mod file - Stochastic Simulation II

Some options of the stoch_simul () :

• order: order of Taylor approximation (1, 2, 3).


• k_order_solver: use a C++ solver (more complicated, check manual).
• ar = sets the number of autocorrelations to be computed (default=5)
• irf= sets the number of responses to be computed; if equal to 0 suppresses
plotting of impulse response functions.
• relative_irf: computes normalized impulse responses (w.r.t. shock size).
• periods: species the number of periods to use in simulations. Periods triggers
the computation of moments and correlation using simulated data rather
than the population solution.
• drop = Integer: Number of points dropped at the beginning of simulation
before computing the summary statistics (default = 100).
• loglin = Consider the model in logs.

stoch_simul ( order = 1 , i r f = 40) ;

17
Making Sense of Output

Firs, Dynare prints steady state values (required through steady), the
variance-covariance matrix of shocks (specified in the shocks block) and a model
summary, counting the various “types” of variable detected in the model (states,
jumpers, and static):

• Dynare counts things which show up in the equilibrium conditions just dated
t as static variables (in our case y).
• Things which show up with a t − 1 as states. In our case, k and a.
• Things which show up with a t + 1 as jumpers. In our case, a and c.
• They do not necessarily add up to the number of variables in the model.
Something could show up as both a state and a jumper.
• In our case, a appears lagged (as a state) and with a “+1” in the Euler equation
(jumper).

18
Making Sense of Output II

Then Dynare prints policy and transition functions. How to interpret them?
Remember again our equations (FOCs + equilibrium conditions):

c−σ
t =βEt c−σ
t+1 (αat+1 kt+1 + (1 − δ))
α−1
Euler Equation
kt+1 =at kα
t − ct + (1 − δ)kt Capital Accumulation
yt =at kα
t Production Function
ln at =ρ ln at−1 + εt Exogenous Process

In a matrix notation, our system can be defined as

Et {f(yt+1 , yt , yt−1 , ut , θ)} = 0 (6)

where
yt =Vector of endogenous variables
ut =Vector of exogenous shocks
θ =Vector of parameters

19
Making Sense of Output III

A linearized version of the model in Equation 6 can thus be written as


Π1 yt+1 = Π2 yt + Π3 yt−1 + Γ1 ut (7)
This way to express the model is the so-called state-space form.
Solving a DSGE model means essentially to find the unknown function g(·),
though which we are able to express the current values of your variables as a
function of past values and current shocks:
yt = g(yt−1 , ut )

also know as policy function. If plugged into the original system, it obviously
solves the model. The linearized stochastic solution found by Dynare is of the form
yt = ȳ + A(zt−1 − z̄) + But (8)
What is z? It is a subset of y, containing only state variables. The idea is to
express the evolution of all variables y in terms of past values of state variables
only z (plus currents shocks). More on this during next lecture.

The policy and transition functions gives you the content of matrixes ȳ, A and B.
20
Making Sense of Output IV

Quite obviously, ȳ represents steady state values (the “constant” line in Dynare’s
output.)

Note

• The interpretation of your coefficients varies if you write you model in a


log-form or in levels. In the former case, yt actually represents ln yt .
• Row names should be read as difference with respect to steady state levels.
• For example, 0.33 (row k(−1) , column y) is the effect of (ln kt−1 − ln k̄) on ln yt .

21
Making Sense of Output V

Consider again the effect of capital k(−1) on output y, and look at Equation 8.
Ignoring every other variable, we could write:
ln yt = ln ȳ + 0.33 ∗ (ln kt−1 − ln k̄)
What would show up if we run the level model? Since we know that, at a first linear
approximation, ln(xt ) − ln x̄ = xt −x̄

, some back-of-the-envelope algebra yields

ln yt − ln ȳ =0.33 ∗ (ln kt−1 − ln k̄)


yt − ȳ kt−1 − k̄
=0.33 ∗ ( )
ȳ k̄
[ ]

yt =ȳ + 0.33 ∗ (kt−1 − k̄)

From the table above we know that ln ȳ = 1.10.379 and ln k̄ = 3.344571. Thus we
should expect a value
ȳ exp(1.10379)
0.33 ∗ = 0.33 ∗ = 0.0351
k̄ exp(3.344571)
Check yourselves!
22
Making Sense of Output VI

Further outputs:

• Variable moments (theoretical if periods is not specified, simulated


otherwise). They also depends on your model specification.
• Contemporaneous correlations among variables.
• Autocorrelations of each variable.

Dynare also plots the Impulse Response Functions.

• The Impulse Response Functions (IRFs) represent the trajectories of each


variable when the model is in the steady state and is perturbed by one or
multiple shocks. In our case, we only have one shock. The magnitude of the
shock is equal to one standard deviation (in our case sigma_a).
• If the variance of an exogenous variable is set to zero, this variable will appear
in the report on policy and transition functions, but isn’t used in the
computation of moments and of Impulse Response Functions. Setting a
variance to zero is an easy way of removing an exogenous shock.

23
Where is the output stored?

You can find all your output in Matlab workspace, under different objects

• Impulse Response Functions: reaction of variable x to shock e is recorded in a


the object x_e.
• Steady state values are recorded in the object oo.dr . ys.
• Policy function:
• Coefficients of state variables (the A matrix in Equation 8) are stored
the object oo.dr . ghx.
• Coefficients of shock variables (the B matrix in Equation 8) are stored
the object oo.dr . ghu.
• Simulated variables are recorded in objects with their names (e.g. time series
for ct as c).

24

You might also like