% Program that solves Ch. 2 Exercise 3

% define parameters

delt=0.025 ;
beta=1.03^(-0.25) ;
thet=0.4 ;
rho=0.9 ;
sigma=0.01 ;

% define crucial ratios

'K-Y Ratio'
ky=thet/(1/beta-(1-delt))
'C-Y Ratio'
cy=1-delt*ky
'C-K Ratio'
ck=cy/ky

mu=beta*thet/ky ;

% solve for the kappas and the alphas

k1 = (thet-1)*mu*ck-(1+1/beta) ;
k2 = 1/beta ;
a1 = (1-mu)/ky+mu*delt ;
a2 = -1/ky ;

% solve for the lambdas

phi=beta*k1 ;

'Roots of the Difference Equation'

l1=(-k1-sqrt(k1*k1-4/beta))/2 ;
l2=(-k1+sqrt(k1*k1-4/beta))/2 ;
[l1 l2]

% solve for the decision rule coefficients

'Coefficients on k(t) and a(t)'

h1=l1 ;
h2=-(a1*rho+a2)*(1/l2)/(1-rho/l2) ;
[h1 h2]

% simulate the model

nsim=1001 ;
a=normar1(0,rho,sigma,nsim) ;
k=zeros(nsim,1) ;

lssk=(1/(1-thet))*log(ky) ; % log of nssv of K for A=1 case
k(1,1)=lssk ;

for i=2:nsim ;
 k(i,1)=lssk+h1*(k(i-1,1)-lssk)+h2*a(i-1,1) ;
end ;
y=thet*k(1:nsim-1,1)+a(1:nsim-1,1) ;
c=log(exp(y)+(1-delt)*exp(k(1:nsim-1,1))-exp(k(2:nsim,1))) ;
dk=log(exp(y)-exp(c)) ;

'Standard Deviations'
'Capital    Output    Consumption  Investment'
std( [ k(1:nsim-1,1) y c dk ] )

'Correlation Matrix'
corrcoef( [ k(1:nsim-1,1) y c dk ] )

plot((1:1:nsim-1)',k(1:nsim-1,1)) ;
title('Capital') ;
pause
plot((1:1:nsim-1)',y(1:nsim-1,1)) ;
title('Output') ;
pause
plot((1:1:nsim-1)',c(1:nsim-1,1)) ;
title('Consumption') ;
pause
plot((1:1:nsim-1)',dk(1:nsim-1,1)) ;
title('Investment') ;
