Introduction to Dynare and localapproximation.
Workshop: Identification analysis and global sensitivityanalysis for macroeconomic models
Michel Juillard, Banque de France
Milan, April 22, 2015
DYNARE
1. computes the solution of deterministic models (arbitraryaccuracy),
2. computes first and second order approximation to solutionof stochastic models,
3. estimates (maximum likelihood or Bayesian approach)parameters of DSGE models, for linear and non-linearmodels.
4. check for identification of estimated parameters5. computes optimal policy,6. performs global sensitivity analysis of a model,7. solves problems under partial information,8. estimates BVAR and Markov-Switching Baysian VAR
models.
The general problem
Deterministic, perfect foresight, case:
f (yt+1, yt , yt−1,ut ) = 0
Stochastic case:
Et {f (yt+1, yt , yt−1,ut )} = 0
y : vector of endogenous variablesu : vector of exogenous shocks
Solution methods
I For a deterministic, perfect foresight, it is possible tocompute numerical trajectories for the endogenousvariables
I In a a stochastic framework, the unknowns are thedecision functions:
yt = g(yt−1,ut )
For a large class of DSGE models, DYNARE computesapproximated decision rules and transition equations by aperturbation method.
Example: Neoclassical growth model
max{ct}∞t=1
∞∑t=1
βt−1 c1−σt
1− σ
s.t.ct + kt = Atkαt−1 + (1− δ)kt−1
First order conditions:
c−σt = βc−σt+1
(αAt+1kα−1
t + 1− δ)
ct + kt = Atkαt−1 + (1− δ)kt−1
Steady state:
k =
(1− β(1− δ)
βαA
) 1α−1
c = Akα − δk
Calibration
α = 0.3β = 0.98δ = 0.025σ = 1
Dynare syntax// variables declarationvar c k;varexo A;
// parameters declarationparameters alpha beta delta sigma;alpha = 0.3;beta = 0.98;delta = 0.025;sigma = 1;
// model equationsmodel;c^(-sigma) = beta*c(+1)^(-sigma)
*(alpha*A(+1)*k^(alpha-1)+1-delta);c+k = A*k(-1)^alpha+(1-delta)*k(-1);end;
Dynare syntax
// setting value for exogenous variable// providing exact value for steady state of// endogenous variablessteady_state_model;k = ((1-beta*(1-delta))/
(beta*alpha*A))^(1/(alpha-1));c = A*k^alpha-delta*k;end;
Computation of first order approximation
I Perturbation approach: recovering a Taylor expansion ofthe solution function from a Taylor expansion of the originalmodel.
I A first order approximation is nothing else than a standardsolution thru linearization.
I A first order approximation in terms of the logarithm of thevariables provides standard log-linearization.
General model
Et {f (yt+1, yt , yt−1,ut )} = 0
E(ut ) = 0E(utu′t ) = Σu
E(utu′τ ) = 0 t 6= τ
y : vector of endogenous variablesu : vector of exogenous stochastic shocks
Timing assumptions
Et {f (yt+1, yt , yt−1,ut )} = 0
I shocks ut are observed at the beginning of period t ,I decisions affecting the current value of the variables yt , are
function ofI the previous state of the system, yt−1,I the shocks ut .
The stochastic scale variable
Et {f (yt+1, yt , yt−1,ut )} = 0
I At period t , the only unknown stochastic variable is yt+1,and, implicitly, ut+1.
I We introduce the stochastic scale variable, σ and theauxiliary random variable, εt , such that
ut+1 = σεt+1
The stochastic scale variable (continued)
E(εt ) = 0 (1)E(εtε
′t ) = Σε (2)
E(εtε′τ ) = 0 t 6= τ (3)
andΣu = σ2Σε
Remarks
Et {f (yt+1, yt , yt−1,ut )} = 0
I The exogenous shocks may appear only at the currentperiod
I There is no deterministic exogenous variablesI Not all variables are necessarily present with a lead and a
lagI Generalization to leads and lags on more than one period
(2nd order approximation requires a more complicatedalgorithm)
Solution function
yt = g(yt−1,ut , σ)
where σ is the stochastic scale of the model. If σ = 0, themodel is deterministic. For σ > 0, the model is stochastic.Under some conditions, the existence of g() function is provenvia an implicit function theorem. See H. Jin and K. Judd“Solving Dynamic Stochastic Models”(http://bucky.stanford.edu/papers/PerturbationMethodRatEx.pdf)
Solution function (continued)
Then,
yt+1 = g(yt ,ut+1, σ)
= g(g(yt−1,ut , σ),ut+1, σ)
F (yt−1,ut , εt+1, σ)
= f (g(g(yt−1,ut , σ), σεt+1, σ),g(yt−1,ut , σ), yt−1,ut )
Et {F (yt−1,ut , εt+1, σ)} = 0
The perturbation approach
I Obtain a Taylor expansion of the unkown solution functionin the neighborhood of a problem that we know how tosolve.
I The problem that we know how to solve is the deterministicsteady state.
I One obtains the Taylor expansion of the solution for theTaylor expansion of the original problem.
I One consider two different perturbations:1. points in the neighborhood from the steady sate,2. from a deterministic model towards a stochastic one (by
increasing σ from a zero value).
The perturbation approach (continued)
I The Taylor approximation is taken with respect to yt−1, utand σ, the arguments of the solution function
yt = g(yt−1,ut , σ).
I At the deterministic steady state, all derivatives aredeterministic as well.
Steady state
A deterministic steady state, y , for the model satisfies
f (y , y , y ,0) = 0
A model can have several steady states, but only one of themwill be used for approximation.Furthermore,
y = g(y ,0,0)
First order approximation
Around y :
Et
{F (1)(yt−1,ut , εt+1, σ)
}=
Et
{f (y , y , y ,0) + fy+
(gy (gy y + guu + gσσ) + guσε
′ + gσσ)
+fy0 (gy y + guu + gσσ) + fy− y + fuu}
= 0
with y = yt−1 − y , u = ut , ε′ = εt+1, fy+ = ∂f∂yt+1
, fy0 = ∂f∂yt
,
fy− = ∂f∂yt−1
, fu = ∂f∂ut
, gy = ∂g∂yt−1
, gu = ∂g∂ut
, gσ = ∂g∂σ .
Taking the expectation
Et
{F (1)(yt−1,ut , εt+1, σ)
}=
f (y , y , y ,0) + fy+ (gy (gy y + guu + gσσ) + gσσ)
+fy0 (gy y + guu + gσσ) + fy− y + fuu}
=(fy+gygy + fy0gy + fy−
)y + (fy+gygu + fy0gu + fu) u
+ (fy+gygσ + fy0gσ)σ
= 0
Recovering gy
(fy+gygy + fy0gy + fy−
)y = 0
Two approaches:I State space representation and generalized real Schur
decompositionI Cyclic reductionI Maintained assumption: there exist a unique stable
solution
Recovering gu
fy+gygu + fy0gu + fu = 0
gu = − (fy+gy + fy0)−1 fu
Recovering gσ
fy+gygσ + fy0gσ = 0
gσ = 0
Yet another manifestation of the certainty equivalence propertyof first order approximation.
First order approximated decision function
yt = y + gy y + guu
E {yt} = yΣy = gy Σyg′y + σ2guΣεg′u
The variance is solved for with an algorithm for Lyapunovequations.
Second and third order approximation of the model
I Second and third order approximation of the solutionfunction are obtained from second, respectively third, orderapproximation of the model.
I It requires only the solution of (tricky) linear problems.I The stochastic scale of the model, σ, appears in the
solution and breaks certainty equivalence
Second order approximation of the model
Et
{F (2)(yt−1, ut , εt+1, σ)
}=
Et
{F (1)(yt−1, ut , ut+1, σ)
+0.5(
Fy−y− (y ⊗ y) + Fuu(u ⊗ u) + Fu′u′σ2(ε′ ⊗ ε′) + Fσσσ
2)
+Fy−u(y ⊗ u) + Fy−u′ (y ⊗ σε′) + Fy−σ yσ + Fuu′ (u ⊗ σε′) + Fuσuσ + Fu′σσε′σ}
= Et
{F (1)(yt−1, ut , εt+1, σ)
}+0.5
(Fy−y− (y ⊗ y) + Fuu(u ⊗ u) + Fu′u′ (σ
2~Σε) + Fσσσ2)
+Fy−u(y ⊗ u) + Fy−σ yσ + Fuσuσ
= 0
Representing the second order derivatives
The second order derivatives of a vector of multivariatefunctions is a three dimensional object. We use the followingnotation
∂2F∂x∂x
=
∂2F1∂x1∂x1
∂2F1∂x1∂x2
. . . ∂2F1∂x2∂x1
. . . ∂2F1∂xn∂xn
∂2F2∂x1∂x1
∂2F2∂x1∂x2
. . . ∂2F2∂x2∂x1
. . . ∂2F2∂xn∂xn
....... . .
.... . .
...∂2Fm∂x1∂x1
∂2Fm∂x1∂x2
. . . ∂2Fm∂x2∂x1
. . . ∂2Fm∂xn∂xn
Composition of two functions
Let
y = g(s)
f (y) = f (g(s))
then,∂2f∂s∂s
=∂f∂y
∂2g∂s∂s
+∂2f∂y∂y
(∂g∂s⊗ ∂g∂s
)
Recovering gyy
Fy−y− = fy+ (gyy (gy ⊗ gy ) + gygyy ) + fy0gyy + B= 0
where B is a term that doesn’t contain second order derivativesof g().The equation can be rearranged:
(fy+gy + fy0) gyy + fy+gyy (gy ⊗ gy ) = −B
This is a Sylvester type of equation and must be solved with anappropriate algorithm.
Recovering gyu
Fy−u = fy+ (gyy (gy ⊗ gu) + gygyu) + fy0gyu + B= 0
where B is a term that doesn’t contain second order derivativesof g().This is a standard linear problem:
gyu = − (fy+gy + fy0)−1 (B + fy+gyy (gy ⊗ gu))
Recovering guu
Fuu = fy+ (gyy (gu ⊗ gu) + gyguu) + fy0guu + B= 0
where B is a term that doesn’t contain second order derivativesof g().This is a standard linear problem:
guu = − (fy+gy + fy0)−1 (B + fy+gyy (gu ⊗ gu))
Recovering gyσ, guσ
Fyσ = fy+gygyσ + fy0gyσ
= 0Fuσ = fy+gyguσ + fy0guσ
= 0
as gσ = 0. Thengyσ = guσ = 0
Recovering gσσ
Fσσ + Fu′u′Σε = fy+ (gσσ + gygσσ) + fy0gσσ+ (fy+y+(gu ⊗ gu) + fy+guu) ~Σε
= 0
taking into account gσ = 0.This is a standard linear problem:
gσσ = − (fy+(I + gy ) + fy0)−1 (fy+y+(gu ⊗ gu) + fy+guu) ~Σε
Second and third order decision functions
I Second order
yt = y + 0.5gσσσ2 + gy y + guu+ 0.5 (gyy (y ⊗ y) + guu(u ⊗ u)) + gyu(y ⊗ u)
I Third order
yt = y +12
gσσσ2 +16σ3 +
12
gyσσyσ2 +12
guσσuσ2
+ gy y + guu +12
(gyy (y ⊗ y) + guu(u ⊗ u)) + gyu(y ⊗ u)
+16
(gyyy (y ⊗ y ⊗ y) + guuu(u ⊗ u ⊗ u))
+12
(gyyu(y ⊗ y ⊗ u) + gyuu(y ⊗ y ⊗ u))
We can fix σ = 1.
Second order accurate moments
Σy = gy Σyg′y + σ2guΣεg′u
E {yt} = y + (I − gy )−1(
0.5(
gσσ + gyy ~Σy + guu~Σε
))
Further issues
I Impulse response functions depend of state at time ofshocks and history of future shocks.
I For large shocks second order approximation simulationmay explode
I pruning algorithm (Sims)I truncate normal distribution (Judd)
Dynare example: Neoclassical growth model (I)// variables declarationvar c k A;varexo ea;
// parameters declarationparameters alpha beta delta sigma;alpha = 0.3;beta = 0.98;delta = 0.025;sigma = 1;
// model equationsmodel;c^(-sigma) = beta*c(+1)^(-sigma)
*(alpha*A(+1)*k^(alpha-1)+1-delta);c+k = A*k(-1)^alpha+(1-delta)*k(-1);A = exp(ea);end;
Dynare example: Neoclassical growth model (II)
steady_state_model;A = 1;k = ((1-beta*(1-delta))/
(beta*alpha*A))^(1/(alpha-1));c = A*k^alpha-delta*k;end;
shocks;var ea; stderr 0.01;end;// display steady statesteady;// check BK conditionscheck;
stoch_simul(order=1);
Approximated decision function
POLICY AND TRANSITION FUNCTIONSc
Constant 1.875089k(-1) 0.072144ea 0.158801
ct ≈ 1.875 + 0.072(kt−1 − k) + 0.159eat
The general problem
Deterministic, perfect foresight, case:
f (yt+1, yt , yt−1,ut ) = 0
y : vector of endogenous variablesu : vector of exogenous shocks
Solution of deterministic models
I based on work of Laffargue, Boucekkine and myselfI recently much accelerated by MihoubiI approximation: impose return to equilibrium in finite time
instead of asymptoticallyI computes the trajectory of the variables numericallyI uses a Newton–type methodI usefull to study full implications of non–linearities
LBJ for deterministic cases
Approximation of an infinite horizon model by a finite horizonone.The stacked system for a simulation on T periods:
f (y0, y1, y2, x1) = 0f (y1, y2, y3, x2) = 0
...f (yT−1, yT , yT +1, xT ) = 0
for y0 and yT +1 given, or
F (Y ) = 0
where Y =[
y ′1 y ′2 . . . y ′T]′.
A Newton approach
I for an initial guess Y (0)
I updated solutions Y (k+1) are obtained by solving[∂F∂Y
](Y (k+1) − Y (k)
)= −F (Y (k))
I until ||Y (k+1) − Y (k)|| < εY and/or ||F (Y (k))|| < εF .
The Jacobian matrix . . .
∂F
∂Y=
B1 C1A2 B2 C2
. . .. . .
. . .At Bt Ct
. . .. . .
. . .AT−1 BT−1 CT−1
AT BT
with
At =∂f (yt−1, yt , yt+1, xt )
∂yt−1
Bt =∂f (yt−1, yt , yt+1, xt )
∂yt
Ct =∂f (yt−1, yt , yt+1, xt )
∂yt+1
. . . in block triangular form
∂F
∂Y=
I M10 I M2
. . .. . .
. . .0 I Mt
. . .. . .
. . .AT−1 BT−1 CT−1
AT BT
with
Mt =(Bt − At Mt−1
)−1 Ct t = 2, . . . , T − 1
I a similar transformation is applied to the RHS of the Newton update equationI the updated solution is then obtained through backward iterationsI in each period a linear problem of the size of the model for one period is solved
Example: neoclassical growth model
max{ct}∞t=1
∞∑t=1
βt−1 c1−σt
1− σ
s.t.ct + kt = Atkαt−1 + (1− δ)kt−1
First order conditions:
c−σt = βc−σt+1
(αAt+1kα−1
t + 1− δ)
ct + kt = Atkαt−1 + (1− δ)kt−1
Steady state:
k =
(1− β(1− δ)
βαA
) 1α−1
c = Akα − δk
Calibration
α = 0.3β = 0.98δ = 0.025σ = 1
A = 1
First example: return to equilibrium when k0 = 0.5k .
neoclassical1.mod
var c k;varexo A;
parameters alpha beta delta sigma;alpha = 0.3;beta = 0.98;delta = 0.025;sigma = 1;
model;c^(-sigma) = beta*c(+1)^(-sigma)
*(alpha*A(+1)*k^(alpha-1)+1-delta);c+k = A*k(-1)^alpha+(1-delta)*k(-1);end;
neoclassical1.mod (continued)
steady_state_model;k = ((1-beta*(1-delta))/(beta*alpha*A))
^(1/(alpha-1));c = A*k^alpha-delta*k;end;
initval;A=1;end;
steady;
histval;k(0) = 0.5*((1-beta*(1-delta))/(beta*alpha*A))
^(1/(alpha-1));end;
neoclassical1.mod (continued)
simul(periods=100);rplot k;rplot c;
The role of the Dynare preprocessor
I the Dynare toolbox solves generic problemsI the preprocessor reads your *.mod file and translates it in
specific Matlab filesI filename.m: main Matlab script for your modelI filename_static.m: static modelI filename_dynamic.m: dynamic model
A temporary shock to TFP
I the economy is at the steady stateI their is an unexpected drop in TFP of 10% at the beginning
of period 1I deterministic shocks are described in shocks blockI See neoclassical2.mod
Fragment from neoclassical2.modsteady_state_model;k = ((1-beta*(1-delta))/(beta*alpha*A))
^(1/(alpha-1));c = A*k^alpha-delta*k;end;
initval;A=1;end;
steady;
shocks;var A;periods 1;values 0.9;end;
A period of temporary favorable shocks announced inthe future
I the economy is at the steady stateI TFP jumps by 4% in period 4 and grows by 1% during the
4 following periodsI See neoclassical3.mod
shocks;var A;periods 4, 5, 6, 7, 8;values 1.04, 1.05, 1.06, 1.07, 1.08;end;
A permanent shock
I the economy is at the initial steady state (A = 1)I in period 1, TFP jumps to 1.05, permanentlyI See neoclassical4.mod
A permanent shock (continued)steady_state_model;k = ((1-beta*(1-delta))/(beta*alpha*A))
^(1/(alpha-1));c = A*k^alpha-delta*k;end;
initval;A=1;end;
steady;
endval;A=1.05;end;
steady;
A pre–announced permanent shock
I the economy is at the initial steady state (A = 1)I in period 6, TFP jumps to 1.05, permanentlyI one uses shocks to maintain TFP at initial value during
the first 5 periodsI See neoclassical5.mod
shocks;var A;periods 1:5;values 1;end;
A two–country overlapping generation modelInspired by W. Buiter (1981) “Time Preference and InternationalLending and Borrowing in an Overlapping- Generations Model.”Journal of Political Economy, 89, pp. 769-97.
I All agents live for T periods. The first periods are spentworking. The last periods are spent in retirement.
I Agents of age i in country n = h, f maximizeT−i+1∑
t=1
(1
1 + ρn
)t−1 c1−σi,n,t
1− σ
I During their active life (TA periods), they supply one unit oflabor in each period.
I They face a budget constraint
si,n,t + ci,n,t = wt + (1 + rt )si−1,n,t−1
I During retirement, agents live out of their savings, withbudget constraint
si,n,t + ci,n,t = wt + (1 + rt )si−1,n,t−1
OLG model (continued)I Agents enter active life without savings
s0,n,t = 0
I With perfect-foresight, they don’t leave any inheritance
sT ,n,t = 0
I There is a common technology worldwideI Wage rate
wt = (1− α)Akαt−1
I Rate of return on capital
rt = αAkα−1t−1
I Worldwide resource constraint with population sizes `n,t
kt(`h,t+1 + `f ,t+1
)= sh,t`h,t + sf ,t`f ,t
First order conditions
c−σi,n,t =1
1 + ρnc−σi+1,n,t+1(1 + rt+1) i = 1, . . . ,T − 1
s1,n,t + c1,n,t = wt
si,n,t + ci,n,t = wt + (1 + rt )si−1,n,t1 i = 2, . . . ,TA
si,n,t + ci,n,t = (1 + rt )si−1,n,t−1 i = TA + 1, . . . ,T − 1cT ,n,t = (1 + rt )sT−1,n,t−1
wt = A(1− α)kαt−1
rt = αAkα−1t−1
kt(`h,t+1 + `f ,t+1
)= sh,t`h,t + sf ,t`f ,t
Example: 2 countries, 2 generations
var w,hc1,hc2,fc1,fc2,r,k,hs,fs;varexo drho,fl,hl;
parameters sigma hrho frho alpha A;
sigma = .6;hrho=0.80;frho=0.80;alpha=0.2;A=1;
Example: 2 countries, 2 generations (continued)
model;
(hc2(+1)/hc1)^sigma = (1+r(+1))/(1+hrho+drho);hs(-1)*(1+r) = hc2;w = hs + hc1;(fc2(+1)/fc1)^sigma = (1+r(+1))/(1+frho);fs(-1)*(1+r) = fc2;w = fs + fc1;
w = A*(1-alpha)*k(-1)^alpha;r = A*alpha*k(-1)^(alpha-1);k*(hl(+1)+fl(+1)) = hs*hl+fs*fl;end;
Example: 2 countries, 2 generations (continued)initval;drho=0;fl=1;hl=1;
w=.28;hc1=.12;hc2=.16;fc1=.12;fc2=.16;k=0.15;r=0.6;hs=0.2;fs=0.2;end;
steady;
Example: 2 countries, 2 generations (continued)
A permanent increase of the discount rate in the home country:
endval;drho=0.05;end;
steady;
simul(periods=20);
rplot hc1;rplot r;
Example with macros
@#define countries = ["h", "f"]@#define T = 10@#define Ta = 6
var w r k;@#for c in countries@# for t in 1:T
var @{c}_c_@{t}@#if t < T@{c}_s_@{t}
@#endif;
@#endfor@#endfor
varexo drho f_l h_l;
Example with macros (continued)
parameters sigma h_rho f_rho alpha A;
sigma = .6;h_rho=0.80;f_rho=0.80;alpha=0.2;A=1;
Example with macros (continued)model;@#for c in countries
@# for t in 1:T-1@{c}_c_@{t}^(-sigma) =
@{c}_c_@{t+1}(+1)^(-sigma)*(1+r(+1))/(1+@{c}_rho+drho);
@#if t == 1@{c}_s_@{t} + @{c}_c_@{t} = w;
@#endif@#if (t > 1) && (t <= Ta)
@{c}_s_@{t} + @{c}_c_@{t} =w + (1+r)*@{c}_s_@{t-1}(-1);
@#endif@#if t > Ta
@{c}_s_@{t} + @{c}_c_@{t} =(1+r)*@{c}_s_@{t-1}(-1);
@#endif@#endfor@{c}_c_@{T} = (1+r)*@{c}_s_@{T-1}(-1);
@#endfor
Example with macros (continued)
w = A*(1-alpha)*k(-1)^alpha;r = A*alpha*k(-1)^(alpha-1);(h_l(+1)+f_l(+1))*k = (h_s_1@#for t in 2:T-1+h_s_@{t}
@#endfor)*h_l + (f_s_1@#for t in 2:T-1+f_s_@{t}
@#endfor)*f_l;end;
Example with macros (continued)initval;drho=0;f_l=1;h_l=1;
w=.5;@#for c in countries@#for t in 1:T@{c}_c_@{t} = 0.13;@#if t < T@{c}_s_@{t} = 0.2;
@#endif@#endfor
@#endfork=0.1;r=1.0;end;
Example with macros (continued)
steady;
endval;drho=0.05;w=1.0;end;
steady;
simul(periods=20);
rplot h_c_1;rplot r;