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

% similar strusture to ch3_ex2b.m

load ch3_ex2c; % if you say save ch3_ex2c  after running ch3_ex2c.m
               % this command will load in all the matrices from
               % that program (including, importantly, the decision
               % rules obtained)

% 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) ;           % initializes the index of the k process
ik(1,1)=median((1:1:nkp)') ; % the first k is the median value of k

for i=2:nsim ;
 ik(i,1)=drul(ik(i-1,1),ia(i-1,1)) ; % recursively generate the indexes
end ;
k=kgrid(ik,1) ;
a=agrid(ia,1) ;
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') ;
