introduction to coding dsge models with dynare

45
Dynare Introduction to Coding DSGE Models with Dynare Macroeconomic Theory (MA1314) Juha Kilponen Bank of Finland Additional Material for Lecture 3

Upload: others

Post on 08-Dec-2021

28 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Introduction to Coding DSGE Models with Dynare

Dynare

Introduction to Coding DSGE Models withDynare

Macroeconomic Theory (MA1314)

Juha Kilponen

Bank of Finland

Additional Material for Lecture 3

Page 2: Introduction to Coding DSGE Models with Dynare

Dynare

DynareI Dynare is preprocessor and a collection of Matlab (and GNUOctave) routines, which solves DSGE models

I Dynare can also be used to estimate DSGE Models and evenMarkov Switching Bayesian VAR models

I The Dynare home page is http://www.dynare.org. You candownload dynare from here. Latest version is 4.4.1.

I The Dynare manual is available athttp://www.dynare.org/documentation-and-support/manual

I Instructions how to install Dynare can be found in the manual(Chapter 2, Installation and configuration)

I See also the Dynare Tutorialhttp://www.dynare.org/documentation-and-support/manual

I WIKI inhttp://www.dynare.org/documentation-and-support/wiki is auseful user forum

Page 3: Introduction to Coding DSGE Models with Dynare

Dynare

The structure of a typical Dynare file

I You tell Dynare what the variables, shocks and parameters(+parameter values) of your model are

I You write down the model equationsI You tell Dynare to find the steady state and check stabilityconditions

I Dynare linearizes the model around the steady stateI ... and solves the recursive equilibrium laws of motion of thelinearized model

I The model is analyzed via impulse responses, stochasticsimulations and moments

Page 4: Introduction to Coding DSGE Models with Dynare

Dynare

The structure of a typical Dynare fileI var;

I Define the ’endogenous’variables of your model

I varexo;I Define the list of shocks in your model

I parameters;I Define the list of parameters and then assign the parametervalues

I you may also find it useful to define some parametertransformations

I e.g. beta is a parameter, and you define steady state realinterest rate rr = 1/beta −1

I more generally, you may give the steady state values ofendogenous variables in terms of parameters

I "var" and "parameters" - blocks end with command "end";

Page 5: Introduction to Coding DSGE Models with Dynare

Dynare

The structure of a typical Dynare file: Writing down themodel

I The model block starts with

model;

I ... and ends withend;

I Between ’model;’and ’end ;’you write down the necessaryequations defining the dynamic equilibrium of the model

I first-order conditions, constraints and any additionaltransformations of variables

I Remember: you need as many equations as there areendogenous variables

I Certain functions are allowed with model command, seeManual for details

Page 6: Introduction to Coding DSGE Models with Dynare

Dynare

I Time indices are given in parenthesisI Xt+1 is written X (+1), (These are forward looking variables)I Xt−1 is written X (−1)I Xt is written X (no time index needed for the current period)

Page 7: Introduction to Coding DSGE Models with Dynare

Dynare

Timing Conventions Cnt’dI In Dynare, the time index refers to the period when the valueof the variable is determined

I The value of the capital stock, which is used in production inperiod t, is predetermined in period t !

I In period t − 1, the agents decide how much to consume andinvest, and they simultaneously determine the size of thecapital stock that will be available in period t

I In a theory model we write the period t production function

Yt = ZtK αt L1−αt

... but in Dynare we would write

Y = Z ∗K(−1)alpha ∗ L(1−alpha)

I Alternatively, you can declare a particular variable aspredetermined using command "predetermined_variables".

Page 8: Introduction to Coding DSGE Models with Dynare

Dynare

Log Linearisation vrs. Linearisation

I Dynare linearizes the model around the steady stateI Remember the advantages of log-linearizations: We aredealing with percentage deviations from the steady state (orthe balanced growth path)

I Log-deviations (or percentage deviations) are easy to interpret(is a deviation ’small’or ’large’?)

I Similar measures are used in empirical examination of the data(e.g. applying HP filter to log(GDP))

I To obtain log linear solution, you can write down the model interms of logarithmic transformations of the original variables

I When Dynare linearizes the model in terms of the logarithmictransformations, it log-linearizes the model in terms of theoriginal variables

Page 9: Introduction to Coding DSGE Models with Dynare

Dynare

ExampleI Adopt the notation

ly = log(Y ), lc = log(C ), lk = log(K ) etc.

I Note: For employment we use the notation

lh = log(L)

where h refers to hours worked (the alternative ll just doesnot look vey nice)

I Also remember the Dynare conventions pertaining to timeindexation

I Then the resource constraint

Yt = Ct +Kt+1 − (1− δ)Kt

can be written as

exp(ly) = exp(lc) + exp(lk)− (1− delta) ∗ exp(lk(−1))

Page 10: Introduction to Coding DSGE Models with Dynare

Dynare

Expectations operatorI The expectation operator Et is not used in Dynare code.

I Dynare ’knows’when one has to take expectations, we do nothave to tell this explicitly.

I Thus the consumption Euler equation

Et

[Ct+1βCt

]= Et

[αZt+1

(Kt+1Lt+1

)α−1+ 1− δ

]or

Et

[Ct+1βCt

]= Et

[αYt+1Kt+1

+ 1− δ

]is written as1beta

∗ exp(lc(+1)− lc) = alfa ∗ (exp(y(+1)− lk) + 1− δ

I Notice: lh = log(L) (h refers to hours worked)I Also notice: In Dynare code, you need the multiplication sign ∗

I Extension: Dynare allows you to change the information setavailable for agents. For instance, writingEXPECTATION(-1)(C(+1)) mean agents use time t-1information to take expectation of variable C for time t+1

Page 11: Introduction to Coding DSGE Models with Dynare

Dynare

Finding the steady stateI It is often necessary to give Dynare an initial guess of thesteady state values, after the command

initval;

I Can be combined with command "endval;" and simulate themodel’s adjustment between initial and endvalues. Typicallyused in deterministic simulations (command: simul;).You tell Dynare to find the steady state with the command

steady;

I Different algorithms to solve the steady state exist, for detailssee the Manua.

I To check the system is stable, you can use command

check;

I Checks the Blanchard Kahn conditions.

Page 12: Introduction to Coding DSGE Models with Dynare

Dynare

Characterizing the shock structure of the model

I shocks; Shock characterization block startsI var e;

I Here we want to shock only one ’variable’(the TFP shock e)

I stderr 0.007;I The standard error of the shock is 0.007I If there are several shocks, say e_z and e_c, you have to bemore careful

I stderr e_z 0.007; stderr e_c 0.002;

I More generally, if there are several shocks, you may want togive the variance-covariance matrix of the shocks; see DynareManual

I end; Shocks charaterisation ends

Page 13: Introduction to Coding DSGE Models with Dynare

Dynare

Analyzing the model via impulse responses, stochasticsimulations and moments

I stoch_simul(order=1,irf=20) ly lh lc li lk z;I order=1 means that Dynare takes a first order Taylorapproximation around the steady state (order=2 => secondorder approximation, order=3 => third order approximation

I irf=20 requests impulse responses for 20 periods. Default is 40I Dynare also computes the moments of the endogenousvariables

I mean, standard deviation, variance, skewness, kurtosisI (contemporaneous) covariance matrixI autocorrelations (default: up to the 5th order)

I The list of variables, we want to Dynare to analyze is ly lh lc lilk z

I Notice that z ≡ log(Zt )− log(Z)= log (Zt ) , since Z = 1

and log(Z)= 0 (a useful normalization).

Page 14: Introduction to Coding DSGE Models with Dynare

Dynare

Analyzing the model via impulse responses, stochasticsimulations and moments

Alternatively we can write:

I stoch_simul(order=1,periods=1000,irf=20) ly lh lk lc lw z;I Now Dynare simulates the model for 1000 periods, andcomputes the moments based on the simulated data

I Or, stoch_simul(order=1,hp_filter=1600, irf=20) ly lh lk lclw z; Now we HP filter the variables before computing themoments

I For additional options in stoch_simul - command, see DynareManual

Page 15: Introduction to Coding DSGE Models with Dynare

Dynare

The structure of the code in a nutshellvar (list of endogenous variables );varexo (list of shocks) ;parameters (parameters + parameter values + transformations);

model;model equations;end;

initval; (initial guesses for computing the steady state)steady; (compute the steady state)

shocks; (the shock structure of the model )var (Variables to be shocked);stderr (the standard error of the shocks);end;stoch_simul(order=1,irf=100) ly lh lc li lk z; (analyzing themodel)

Page 16: Introduction to Coding DSGE Models with Dynare

Dynare

Hansen’s Indivisible Labour RBC model: Equilibriumequations

1 = βEt

[(CtCt+1

)(αZt

(Kt+1Lt+1

)α−1+ 1− δ

)]

ψCt = (1− α)Zt

(KtLt

Yt = Ct +Kt+1 − (1− δ)KtYt = ZtK α

t L1−αt

I = Yt − CtWt = (1− α)

YtLt

logZt = (1− φ) logZ + φ logZt−1 + εt , εt is iid

Page 17: Introduction to Coding DSGE Models with Dynare

Dynare

Dynare file: Hansen’s RBC model

// following are the ’endogenous’variables:var ly, lh, lc, li, lw, lk, z ;

// following are the shocksvarexo e;

parameters beta, fi, alpha, delta, psi;

alpha = 0.33;fi = 0.74;beta = 0.99;delta = 0.024;psi =0.262;

Page 18: Introduction to Coding DSGE Models with Dynare

Dynare

Dynare file: Hansen’s RBC model

// Steady state expressed in terms of parametersrr=1/beta-1;zss=1;css=(1-alpha)*(zss/psi)*(alpha*zss/(rr+delta))^(alpha/(1-alpha));kss=css/((rr+delta)/alpha-delta);hss=(1/psi)*(1-alpha*rr/(rr+(1-alpha)*delta));yss= zss*kss^(alpha)*hss^(1-alpha);iss=delta*kss;wss=(1-alpha)*yss/hss;

Page 19: Introduction to Coding DSGE Models with Dynare

Dynare

Dynare file: Hansen’s RBC modelmodel;

(1/beta)*exp(lc(+1)-lc)= alpha*exp(ly(+1)-lk) + 1 - delta;//Euler equation

psi*exp(lc)= (1-alpha)*exp(ly-lh); //Labour market equilibrium

exp(ly) = exp(lc) + exp(lk) - (1-delta)*exp(lk(-1)); //Resourceconstraint

exp(ly) = exp(z+alpha*lk(-1)+(1-alpha)*lh); //Productionfunction

exp(li)=exp(ly)-exp(lc); // Investment

exp(lw)=(1-alpha)*exp(ly-lh); //Real wage

z = fi*z(-1) + e; //Law of motion of TFP

end;

Page 20: Introduction to Coding DSGE Models with Dynare

Dynare

Dynare file: Hansen’s RBC model

//Initial guesses for the computation of steady state

initval;lc=log(css);lh=log(hss);lk=log(kss);ly=log(yss);li=log(iss);lw=log(wss);z=zss;end;

steady;

Page 21: Introduction to Coding DSGE Models with Dynare

Dynare

Dynare file: Hansen’s RBC model

shocks;var e;stderr 0.007.;end;

stoch_simul(order=1,irf=100) ly lh lc li lk z;

Page 22: Introduction to Coding DSGE Models with Dynare

Dynare

Saving and running the Dynare file

I Save the file as [Name of the file].modI For example rbc.mod

I Run the model by writing in the Matlab command window

dynare rbc.mod

I You may also run the Dynare file from a separate m.-file (thisis practical if you, say, want to solve the model many times,with different parameter values)

Page 23: Introduction to Coding DSGE Models with Dynare

Dynare

I An alternative way to write down the model.I You write down the model in terms of the original variables(not log-transformations)

I Then you add the log-transformations as extra variables

Page 24: Introduction to Coding DSGE Models with Dynare

Dynare

Dynare file: Hansen’s RBC model (alternative form)

// following are the ’endogenous’variables:var Y, L, C, I, W, K, ly, lh, lc, li, lw, lk, z ;

// following are the shocksvarexo e;

parameters beta, fi, alpha, delta, psi;

alpha = 0.33;fi = 0.74;beta = 0.99;delta = 0.024;psi =0.262;

Page 25: Introduction to Coding DSGE Models with Dynare

Dynare

Dynare file: Hansen’s RBC model (alternative form)

I Steady state expressed in terms of parametersI Exactly the same as above, in the main version

Page 26: Introduction to Coding DSGE Models with Dynare

Dynare

Dynare file: Hansen’s RBC model (alternative form)model;

(1/beta)*C(+1)/C= alpha*Y(+1)/K+ 1 - delta;//Euler equation

psi*C= (1-alpha)*Y/H; //Labour market equilibrium

Y = C + K - (1-delta)*K(-1); //Resource constraint

Y = exp(z)*K(-1)^alpha*L^(1-alpha); //Production function

I=Y-C; // Investment

W=(1-alpha)*Y/L; //Real wage

z = fi*z(-1) + e; //Law of motion of TFP

Page 27: Introduction to Coding DSGE Models with Dynare

Dynare

Dynare file: Hansen’s RBC model (alternative form)// model code continues

// define log transformations of the endogenous varialbes

ly=log(Y);

lh=log(L);

lc=log(C);

li=log(I);

lw=log(W);

lk=log(K);

end;

Page 28: Introduction to Coding DSGE Models with Dynare

Dynare

Dynare file: Hansen’s RBC model (alternative form)//Initial guesses for the computation of steady state

initval;C=css;L=hss;K=kss;Y=yss;I=iss;W=wss;lc=log(css);lh=log(hss);lk=log(kss);ly=log(yss);li=log(iss);lw=log(wss);z=zss;end;

steady;

Page 29: Introduction to Coding DSGE Models with Dynare

Dynare

Dynare file: Hansen’s RBC model (alternative form)

shocks;var e;stderr 0.007.;end;

stoch_simul(order=1,irf=100) ly lh lc li lk z;//Notice: Here you ask Dynare to analyze the impulse responses//and the moments of the log-transformations.

Page 30: Introduction to Coding DSGE Models with Dynare

Dynare

Output

Output includes

I Policy and transition functions of the (log)linearized modelI Moments of the endogenous variables

I mean, variance, standard deviation, skewness, kurtosisI matrix of contermporaneous correlationsI coeffi cients of autocorrelation

I Impulse responsesI If if you have included the periods option in

stoch_simul(order=1,periods=1000,irf=100) ly lh lc li lk z;

the output also includes the results (time paths of endogenousvariables) from the stochastic simulation.

Page 31: Introduction to Coding DSGE Models with Dynare

Dynare

Output

I The output is shown on the screen, and in separate figures(impulse responses).

I Output is also stored in a separate structure, called oo_.Other information of the model is saved in structure M_

I The structure oo_ contains (for example)I The steady state (oo_.steady_state )I The variance-covariance matrix (oo_.var)I The autocorrelations (oo_.autocorr)I The impulse responses (oo_.irfs)I The coeffi cients of the policy and transition functions (oo_.dr)I Results (time paths of endogenous variables) from stochasticsimulations (oo_.endo_simul)

I Structure M_ contains add information of the model object.

Page 32: Introduction to Coding DSGE Models with Dynare

Dynare

Output: Policy and transition functions

ly lh lc li lk zConstant 0.019 −1.1 −0.24 −1.44 2.29 0lk(−1) −0.0075 −0.50 0.50 −1.67 0.94 0z(−1) 1.87 1.68 0.18 7.4 0.18 0.74e 2.52 2.28 0.25 10.0 0.24 1

I Transition functions: how the period t values of the statevariables (lk and z) depend on the period t − 1 values of thestate variables, and the shock

I Policy functions: how the period t values of the othervariables depend on the period t − 1 values of the statevariables, and the shock

Page 33: Introduction to Coding DSGE Models with Dynare

Dynare

Output: Policy and transition functions

ly lh lc li lk zConstant 0.019 −1.1 −0.24 −1.44 2.29 0lk(−1) −0.0075 −0.50 0.50 −1.67 0.94 0z(−1) 1.87 1.68 0.18 7.4 0.18 0.74e 2.52 2.28 0.25 10.0 0.24 1

I Rows: period t values of the variablesI Columns: period t − 1 values of the state variables + theshock (e)

I The term ’constant’is the steady state (of log transformation)I E.g. lh = −1.1 and thus L = exp(−1.1) = 0.33

Page 34: Introduction to Coding DSGE Models with Dynare

Dynare

Output: Policy and transition functions

ly lh lc li lk zConstant 0.019 −1.1 −0.24 −1.44 2.29 0lk(−1) −0.0075 −0.50 0.50 −1.67 0.94 0z(−1) 1.87 1.68 0.18 7.4 0.18 0.74e 2.52 2.28 0.25 10.0 0.24 1

I The entries of the table give the coeffi cients of the(log)linearized policy and transition functions. For example(the first column)

ly = 0.019︸ ︷︷ ︸ly

− 0.0075∗(lk (−1)− lk

)+ 1.87∗ z(−1)+ 2.52∗ e

Page 35: Introduction to Coding DSGE Models with Dynare

Dynare

Output: Connection to our earlier results (Lecture 2)

ly = 0.019︸ ︷︷ ︸ly

− 0.0075 ∗(lk (−1)− lk

)+ 1.87 ∗ z(−1) + 2.52 ∗ e

(1)I How to write (1) in terms of log deviations yt , kt and zt?I Remember that

ly − ly = log(Yt )− log(Y)= yt ;

lk (−1)− lk = log(Kt )− log(K ) = ktI Also notice that

2.52 ∗ z = 2.52 ∗φ︷︸︸︷0.74︸ ︷︷ ︸

1.87

∗ z(−1) + 2.52 ∗ e

I Thus (1) can be rewritten as

yt = −0.0075 ∗ kt + 2.52 ∗ zt

Page 36: Introduction to Coding DSGE Models with Dynare

Dynare

Output: Connection to our earlier results (Lecture 2)

ly lh lc li lk zConstant 0.019 −1.1 −0.24 −1.44 2.29 0lk(−1) −0.0075 −0.50 0.50 −1.67 0.94 0z(−1) 1.87 1.68 0.18 7.4 0.18 0.74e 2.52 2.28 0.25 10.0 0.24 1

I More generally, in terms of the notation presented in Lecture2, the transition and policy functions can be re-expressed asfollows

kt+1 = 0.94 ∗ kt + 0.24 ∗ zt ; ct = 0.50 ∗ kt + 0.25 ∗ ztht = −0.50 ∗ kt + 2.28 ∗ zt ; ιt = −1.67 ∗ kt + 10.0 ∗ zt

Page 37: Introduction to Coding DSGE Models with Dynare

Dynare

Solving recursive stochastic linear systems - general caseI In general, analysis of DSGE models involves a solution to thefollowing system

Et (f (yt+1, yt , yt−1, zt ) = 0 (2)

I yt is a vector of endogenous state variables, zt is a vector ofexogenous stochastic shocks. It is often useful to writezt = σεt , E (εt ) = 0, E (εtε′t ) = Σε. σ is then scale variableand you can assume that auxiliary shocks εt has unit variance.

I Notice that f is known.I Policy (solution) function is

yt = g(yt−1, zt , σ)

Thus,

yt+1 = g(yt , zt+1, σ) = g(yt , zt+1, σ) = g(g(yt−1, zt , σ), zt+1, σ)(3)

Page 38: Introduction to Coding DSGE Models with Dynare

Dynare

Solving recursive stochastic linear systems cnt’dI Using (3) in (2), we find that

Et{F (yt−1, zt , zt+1, σ)} = 0 (4)

I A deterministic steady state y satisfies f (y , y , y , 0) = 0 andy = g(y , 0, 0). This is nothing but a non-linear system whichcan easily be solved. (e.g. Matlab, fsolve function). The onlyproblem is that there can be several solutions. We need tochoose only one for approximation.

I There are different methods to deal with (4). For instance,Dynare (1st order approx) computes approximated decisionrules of the form

yt = y + gy yt−1 + gzzt , with y = yt − yI It all eventually collapses into a problem of finding y , gy andgz , such that the system has a saddle path property.

Page 39: Introduction to Coding DSGE Models with Dynare

Dynare

Dynare - first order approximation around y:

Et{F (yt−1, zt , z′, σ)}= (fy ′gygy + fy0gy + fy_)y

+(fy ′gygz + fy0gz + fz )z (5)

+(fy ′gygσ + fy0gσ)σ

= 0

where

y = yt−1 − y , z = zt , z ′ = zt+1fy ′ =

∂f∂y ′, fy0 =

∂f∂y0

, fy_ =∂f

∂yt−1,

fz =∂f∂z, gz =

∂g∂z, gσ =

∂g∂σ, gy =

∂g∂yt−1

I Basic idea: Equation (5) needs to hold for any arbitrary y , zand σ.

Page 40: Introduction to Coding DSGE Models with Dynare

Dynare

Dynare - first order approximation cnt’d

I We have the following 3 matrix equations

(fy ′gygy + fy0gy + fy_) = 0

(fy ′gygz + fy0gz + fz ) = 0

(fy ′gygσ + fy0gσ) = 0

I From the last one, we can already deduce that gσ = 0. This isa certainty equivalence property.

I But we have still 2 unknowns gy , gz . We recover gz from thefirst equation and gz from the second.

I The hardest one to solve is gy .

Page 41: Introduction to Coding DSGE Models with Dynare

Dynare

Solving

gz

(fy ′gygz + fy0gz + fz ) = 0

gz = −(fy ′gy + fy0)−1fz

Page 42: Introduction to Coding DSGE Models with Dynare

Dynare

Solvinggy

I Consider following state space representation[0 fy ′I 0

] [Igy

]gy y =

[−fy_ −fy00 I

] [Igy

]y (6)

or [0 fy ′I 0

]︸ ︷︷ ︸

D

[yt − yyt+1 − y

]︸ ︷︷ ︸

xt+1

=

[−fy_ −fy00 I

]︸ ︷︷ ︸

E

[yt−1 − yyt − y

]︸ ︷︷ ︸

xt

(7)

I So, above can be expressed as

Dxt+1 = Ext (8)

Page 43: Introduction to Coding DSGE Models with Dynare

Dynare

Generalized Schur decomposition

I We want stable solution to (8).I Problem of finding a solution to (8) is that D might not beinvertible. But to solve the system we still need to find itseigenvalues and eigenvectors. The generalized Schurdecomposition allows to solve generalised eigenvalues.

I There exist matrices Q,T,Z,S such that

D = QTZ

E = QSZ

I where T and S are upper triangular, Q ′Q = I , Z ′Z = I .

Page 44: Introduction to Coding DSGE Models with Dynare

Dynare

Applying the decompositionD[Igy

]gy y = E

[Igy

]y[

T11 T120 T22

] [Z11 Z12Z21 Z22

] [Igy

]gy y

=

[S11 S120 S22

] [Z11 Z12Z21 Z22

] [Igy

]y

I To exclude explosive trajectories we impose

Z21 + Z22gy = 0

gy = −Z−122 Z21

I A unique stable trajectory exists if Z22 is non-singular: thereare as many roots larger than one in modulus as there areforward looking variables in the model (Blanchard and Kahn(1980) condition) and the rank condition is satisfied.

Page 45: Introduction to Coding DSGE Models with Dynare

Dynare

First order approximated decision function and variance

yt = y + gy yt−1 + gzztE (yt ) = y

Σy = gyΣyg′y + σ2guΣεg ′u

I Variance Σy is solved using an algorithm for Lyapunovequations. Second moments can therefore be computeddirectly, without a need for simulation. The same applies toAR coeffi cients.