% =========================================================================
% code for NK model chapter 3
% replicate fig3.4 - money growth rule
% Effects of a technology Shock (money growth rule)
% patricio jaramillo, sept 2014
% =========================================================================

var infl y ir rn a ye r yn dm l n;
varexo em ea;
parameters mu beta sigma rhom phi phi1 phi2 phina rhov rhoa lambda kappa theta alpha epsilon rho seda vega; 
alpha=1/3;
phi=1;
epsilon=6;
beta=0.99;
theta=2/3;
sigma=1;
phi1=1.5;
phi2=0.5/4;
rho=-log(beta);
rhov=0.5;
rhoa=0.9;
rhom=0.5;
seda=4;
lambda=( ((1-theta)*(1-beta*theta))/ theta) * ( (1-alpha) / (1-alpha+alpha*epsilon));
kappa=lambda*(sigma + ((phi+alpha)/(1-alpha)));
phina=1;
mu=log(epsilon/(epsilon-1));
vega=-(((1-alpha)*mu-log(1-alpha)) / (sigma*(1-alpha)+phi+alpha));

model(linear);
% New Keynesian Phillips Curve: eq(21)
infl=beta*infl(+1)+kappa*y;

% Dynamic IS: eq(22)
y=y(+1)-(1/sigma)*(ir-infl(+1)-rn);

% Natural Interest Rate: eq(23)
rn=rho+sigma*phina*(a(+1)-a);

% Auxiliary equations: 
%(Fisher pag 49)
r=ir-infl(+1);

% adhoc money (growth) demand: eq(29) and (31)
y-seda*ir=l-yn;
l(-1)=l+infl-dm;

% natural product: eq(19) and definition of output gap
yn=phina*a+vega;
ye=y+yn;

% production function: eq(13)
ye=a+(1-alpha)*n;

% exogenous shocks
% monetary shock
dm=0*(rhom*dm(-1)+em);

% non active productivity shock
a=rhoa*a(-1)+ea;
end;

% steady state
initval;
l=0.1;
yn=0.01;
ye=0.2;
infl=0.03;
a=0.01;
r=0.1;
y=0.1;
ir=0.03;
rn=0.012;
dm=0.1;
n=0.2;
end;

steady(solve_algo=2);
check;

shocks;
var em; stderr 0.25;
var ea; stderr 1;

end;
stoch_simul(periods=500, irf=13, nodisplay);
%model_diagnostics(M_,options_,oo_);

%===========================================================================
% Plot 3.4
%===========================================================================
t=0:1:12;
figure(1);
title('Effects of a technology Shock (money growth rule)');

subplot(4,2,1)
plot(t,y_ea,'-ob')
title('output gap')

subplot(4,2,2)
plot(t,4*infl_ea,'-ob')
title('inflation')

subplot(4,2,3)
plot(t,ye_ea,'-ob')
title('output')

subplot(4,2,4)
plot(t,n_ea,'-ob')
title('employment')

subplot(4,2,5)
plot(t,4*ir_ea,'-ob')
title('nominal rate')
%axis([0 12 -1 1])

subplot(4,2,6)
plot(t,4*r_ea,'-ob')
title('real rate')

subplot(4,2,7)
plot(t,4*dm_ea,'-ob')
title('money growth')
axis([0 12 -1 1])

subplot(4,2,8)
plot(t,a_ea,'-ob')
title('productivity shock a')
axis([0 12 0 1])




