Dynare Intro
Dynare Intro
Matteo Coronese
April 22, 2021
Scuola Superiore Sant’Anna
What is Dynare?
2
Getting Started
• 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
yt =at kα
t
(2)
it =yt − ct
ln at = ρ ln at−1 + εt (3)
5
A Simple RBC Model II
c−σ
t = βEt c−σ
t+1 (αat+1 kt+1 + (1 − δ))
α−1
(4)
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.
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.
1 1 1
ln ȳ + (yt − ȳ) = ln ā + (at − ā) + α ln k̄ + α (kt − k̄) →
ȳ ā k̄
yt − ȳ at − ā kt − k̄
= +α →
ȳ ā k̄
ŷt =ât + αk̂t
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̄) →
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
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
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:
• 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
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:
16
The .mod file - Stochastic Simulation II
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
where
yt =Vector of endogenous variables
ut =Vector of exogenous shocks
θ =Vector of parameters
19
Making Sense of Output III
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
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̄
x̄
, some back-of-the-envelope algebra yields
Further outputs:
23
Where is the output stored?
You can find all your output in Matlab workspace, under different objects
24