% Program that solves Ch 3 Exercise 2 (simulation of Tauchen solution)

load ch3_ex2a ;% after running ch3_ex2a type the command
               % save ch3_ex2a which saves all results from ch3_ex2a.m
               % in the file ch3_ex2a.mat

% simulate the model

nsim=1001 ;

ia=dspace(pim,nsim) ; % calls external m file to get indexes of
                      % the simulated A process

ik=zeros(nsim,1) ;
ik(1,1)=median((1:1:nkp)') ; % initialize the capital stock index at
                             % its median value

for i=2:nsim ;
 ik(i,1)=drul(ik(i-1,1),ia(i-1,1)) ; % generate a capital stock index
                                     % time series
end ;
k=kgrid(ik,1) ; % generate the sequence of log capital stocks
a=agrid(ia,1) ; % generate the sequence of tech shocks
y=thet*k(1:nsim-1,1)+a(1:nsim-1,1) ; % generate log(Y)
c=log(exp(y)+(1-delt)*exp(k(1:nsim-1,1))-exp(k(2:nsim,1))) ; % log(C)
dk=log(exp(y)-exp(c)) ; % log(Y-C) investment

'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') ;
