% Solution to Ch 3 exercise 1 (second part)

% set the parameters of the law of motion of consumption growth

mu=0.0179 ;
rho=-0.139 ;
sig=0.0348 ;

% choose sample size of simulation (short for demo purposes only)

ss=501 ;

% load the quadrature data and loop over the relevant submatrix
% of data for the number of quadrature points

load ghquad.dat ;

% put in some output labels

'    No.Pts.          Mean             Std. Dev.'
'               R.F.      E.P.       R.F.       E.P.'

for nqp=2:10 ; % number of quadrature points to use ( must be <= 10 )

 ymat=ghquad(0.5*(nqp-1)*nqp+1:0.5*nqp*(nqp+1),2) ;
 wmat=ghquad(0.5*(nqp-1)*nqp+1:0.5*nqp*(nqp+1),3) ;

 % ymat is the vector of abscissas
 % wmat is the vector of weights

 % Now transform the abscissas given the law of motion

 ymat=mu+ymat*sig ;

 % generate the transition matrix

 pim=gettrans(ymat,wmat,mu,rho,sig) ;

 % simulate discrete state space process

 ix=dspace(pim,ss) ;
 x=ymat(ix,1) ;  % values of consumption growth

 % choose values of beta and gamma

 b=0.99 ;
 g=2.0 ;

 % get solution for price dividend ratio

 pdr=getprice(ymat,pim,b,g) ;
 v=pdr(ix,1) ;  % time series of price dividend ratios

 % get solution for price of risk free asset

 qp=getrfree(ymat,pim,b,g) ;
 q=qp(ix,1) ;   % time series of risk free prices

 % compute rates of return

 re=(v(2:ss,1)+1).*exp(x(2:ss,1))./v(1:ss-1,1)-1 ;
 rf=ones(ss-1,1)./q(1:ss-1,1)-1 ;

 % compute and print the moments

 meanrfr=mean(rf) ;
 meanepr=mean(re-rf) ;
 stdrfr=std(rf) ;
 stdepr=std(re-rf) ;

 [nqp meanrfr meanepr stdrfr stdepr]

end ;
