Monetary Economics Matlab code for Lecture 5 Numerical .../menu/standard/file/... · Monetary...

Post on 09-May-2018

214 views 1 download

Transcript of Monetary Economics Matlab code for Lecture 5 Numerical .../menu/standard/file/... · Monetary...

Monetary EconomicsMatlab code for Lecture 5

Numerical simulation of dynamic modelsJohan Söderberg

Spring 2012

Provided below is code for solving the basic NK model

xt = Etxt+1 − 1σ

(it − Etπt+1 − rnt

)πt = κxt + βEtπt+1

it = φππt + φyxt

rnt = ρr rnt−1 + εt

numerically using the Blanchard-Kahn method, QZ-method, and Dynare program. The QZ-algorithmrequires Chris Sims’s qzdiv.m and qzswitch.m routines, which can be downloaded from his website.

The Blanchard-Kahn method:

clear all

% calibrationbeta =0.99;sigma =1;phi =1;theta =2/3;phi_y =0.1;phi_pi =1.5;rho_r =0.8;

kappa =(1- theta )*(1 - theta*beta )/ theta *( sigma+phi );

n=1; % number of pre - determined variablesm=2; % number of forward - looking variables

% define matrices A, B and CA=[1 0 0;

0 beta 0;

1

-1/ sigma -1/ sigma -1]

B=[ rho_r 0 0;0 1 -kappa;0 -phi_pi /sigma -(1+ phi_y/sigma )]

C=[1;0;0]

F=inv(A)*B;G=inv(A)*C;

[P J]= eig(F); % get eigenvectors and eigenvaluesJ=diag(J); % vectorize J[ unused order ]= sort(abs(J),’ascend ’); % sort eigenvalues in ascending orderJ=diag(J(order )); % reorder J and make diagonal againP=P(:, order ); % reorder eigenvectors

%check number of eigenvalues outside unit circle equal to mif (sum(abs(diag(J)) >1)~=m)

break;end

% partition matricesPinv=inv(P);Phat11 =Pinv (1:n ,1:n);Phat21 =Pinv(n+1:m+n ,1:n);Phat12 =Pinv (1:n,n+1:m+n);Phat22 =Pinv(n+1:m+n,n+1:m+n);

F11=F(1:n ,1:n);F21=F(n+1:m+n ,1:n);F12=F(1:n,n+1:m+n);F22=F(n+1:m+n,n+1:m+n);

J1=J(1:n ,1:n);J2=J(n+1:m+n,n+1:m+n);

G1=G(1:n);G2=G(n+1:n+m);

% calculate coefficientsC11=real(F11 -F12*inv( Phat22 )* Phat21 )

2

C12=real(G1 -F12*inv( Phat22 )* inv(J2 )*( Phat21 *G1+ Phat22 *G2))

C21=real(-inv( Phat22 )* Phat21 )C22=real(-inv( Phat22 )* inv(J2 )*( Phat21 *G1+ Phat22 *G2))

% calculate imp.resp.j=20; % number of periods

x1=zeros(n,j);x2=zeros(m,j);e =zeros (1,j);

e (1 ,1)=0.01; %size of shock

for i=1:1:j;x1(:,i+1)= C11*x1(:,i)+ C12*e(:,i);x2(:,i) =C21*x1(:,i)+ C22*e(:,i);

end

%plot imp.respfigureplot(x2 ’*100)legend (’Inflation ’,’Output gap ’)

break; % remove if simulate

% simulate modelj=700; % number of periods

x1=zeros(n,j);x2=zeros(m,j);e =randn (1,j )*0.01; %draw shocks from normal distribution

for i=1:1:j;x1(:,i+1)= C11*x1(:,i)+ C12*e(:,i);x2(:,i) =C21*x1(:,i)+ C22*e(:,i);

end

3

The QZ-method:

clear all

% calibrationbeta =0.99;sigma =1;phi =1;theta =2/3;phi_y =0.1;phi_pi =1.5;rho_r =0.8;

kappa =(1- theta )*(1 - theta*beta )/ theta *( sigma+phi );

n=1; % number of pre - determined variablesm=2; % number of forward - looking variables

% define matrices A, B and CA=[1 0 0;

0 beta 0;-1/ sigma -1/ sigma -1]

B=[ rho_r 0 0;0 1 -kappa;0 -phi_pi /sigma -(1+ phi_y/sigma )]

C=[1;0;0]

[S T Q Z]=qz(A,B); %qz - decompositionlambda =eig(B,A); %get generalized eigenvalues

%check number of eigenvalues on or inside unit circle equal to nif (sum(abs( lambda ) <=1)~=n)

break;end

[S,T,Q,Z] = qzdiv (1,S,T,Q,Z); % rearrange matrices

R=Q*C;

% partition matricesS11=S(1:n ,1:n);S21=S(n+1:m+n ,1:n);

4

S12=S(1:n,n+1:m+n);S22=S(n+1:m+n,n+1:m+n);

T11=T(1:n ,1:n);T21=T(n+1:m+n ,1:n);T12=T(1:n,n+1:m+n);T22=T(n+1:m+n,n+1:m+n);

Zinv=inv(Z);Zhat11 =Zinv (1:n ,1:n);Zhat21 =Zinv(n+1:m+n ,1:n);Zhat12 =Zinv (1:n,n+1:m+n);Zhat22 =Zinv(n+1:m+n,n+1:m+n);

R1=R(1:n);R2=R(n+1:n+m);

% calculate coefficientsC11=real(inv (( Zhat11 - Zhat12 *inv( Zhat22 )* Zhat21 ))* inv(S11 )...

*T11 *( Zhat11 - Zhat12 *inv( Zhat22 )* Zhat21 ))C12=real(-inv(Zhat11 - Zhat12 *inv( Zhat22 )* Zhat21 )* inv(S11 )...

*( T11* Zhat12 *inv( Zhat22 )* inv(T22 )*R2+T12*inv(T22 )*R2 -R1))

C21=real(-inv( Zhat22 )* Zhat21 )C22=real(-inv( Zhat22 )* inv(T22 )*R2)

% calculate imp.resp.j=20; % number of periods

x1=zeros(n,j);x2=zeros(m,j);e =zeros (1,j);

e (1 ,1)=0.01; %size of shock

for i=1:1:j;x1(:,i+1)= C11*x1(:,i)+ C12*e(:,i);x2(:,i) =C21*x1(:,i)+ C22*e(:,i);

end

%plot imp.respfigureplot(x2 ’*100)legend (’Inflation ’,’Output gap ’)

break; % remove if simulate

5

% simulate modelj=700; % number of periods

x1=zeros(n,j);x2=zeros(m,j);e =randn (1,j )*0.01; %draw shocks from normal distribution

for i=1:1:j;x1(:,i+1)= C11*x1(:,i)+ C12*e(:,i);x2(:,i) =C21*x1(:,i)+ C22*e(:,i);

end

6

Dynare:

var x pi i r_n;varexo e_r;

parameters beta sigma phi theta phi_y phi_pi rho_r kappa;

beta =0.99;sigma =1;phi =1;theta =2/3;phi_y =0.1;phi_pi =1.5;rho_r =0.8;

kappa =(1- theta )*(1 - theta*beta )/ theta *( sigma+phi );

model( linear );x=x(+1) -1/ sigma *(i-pi (+1) - r_n );pi=kappa*x+beta*pi (+1);i= phi_pi *pi+phi_y*x;r_n=rho_r*r_n ( -1)+ e_r;end;

shocks ;var e_r =0.01^2;end;

stoch_simul (irf =15);

Note that because we are working with a linear model, we need to tell Dynare not to linearize themodel by adding the option linear to the model block.

7

Dynare code for optimal monetary policy when a cost-push shock has been appended to the Phillipscurve:

var x pi i;varexo e_u;

parameters beta sigma phi theta phi_y phi_pi rho_r kappa eta;

beta =0.99;sigma =1;phi =1;theta =2/3;phi_y =0.1;phi_pi =1.5;rho_r =0.8;eta =6;

kappa =(1- theta )*(1 - theta*beta )/ theta *( sigma+phi );

model( linear );x=x(+1) -1/ sigma *(i-pi (+1));pi=kappa*x+beta*pi (+1)+ e_u;end;

shocks ;var e_u =0.01^2;end;

planner_objective pi ^2+( kappa/eta )* x^2;ramsey_policy ( planner_discount =0.99 , irf =15);

8

We can also let Dynare compute the linearization for us. As an example, consider a simple RBCmodel with fixed labor supply, described by the equilibrium conditions:

C−σt = βEt

{C−σt+1Rt+1

}Kt = eztKα

t−1 + (1 − δ)Kt−1 − Ct

Rt = 1 + αeztKα−1t−1 − δ

zt = ρzt−1 + εt

where Ct is consumption, Kt is capital, Rt is the gross rental rate of capital, ezt is technology, andεt is an i.i.d. productivity shock with finite variance. Note that Dynare requires us to use the “endof period stock” concept for stock variables.

The Dynare code for linearizing and solving this model is

var c k r z;varexo e;

parameters alpha beta delta sigma rho;

alpha =1/3;beta =0.95;delta =0.1;sigma =5;rho =0.8;

model;c^(- sigma )= beta*c(+1)^( - sigma )*r(+1);k=exp(z)*k( -1)^ alpha +(1- delta )*k(-1)-c;r=1+ alpha*exp(z)*k( -1)^( alpha -1)- delta;z=rho*z( -1)+e;end;

initval ;c=1;k=1;r=1;z=0;end;

steady ;

shocks ;var e =0.01^2;end;

stoch_simul (irf =60, order =1);

9

To compute a log-linear approximation instead, redefine each variable as the logarithm of theoriginal variable. The Dynare code then modifies to

var c k r z;varexo e;

parameters alpha beta delta sigma rho;

alpha =1/3;beta =0.95;delta =0.1;sigma =5;rho =0.8;

model;exp(c)^(- sigma )= beta*exp(c(+1))^( - sigma )* exp(r (+1));exp(c)= exp(z)* exp(k( -1))^ alpha +(1- delta )* exp(k(-1))- exp(k);exp(r)=1+ alpha*exp(z)* exp(k( -1))^( alpha -1)- delta;z=rho*z( -1)+e;end;

initval ;c=1;k=1;r=1;z=0;end;

steady ;

shocks ;var e =0.01^2;end;

stoch_simul (irf =60, order =1);

For a second-order approximation, set order = 2 (default if no order is given) in the stoch_simulcommand. As of version 4.1, Dynare can also compute third-order approximations by settingorder = 3.

10