%   lucdemo.m
echo on
clc
%
%                 Lucas Pure Endowment Economy
%                     Asset Pricing Example
%
%   This demonstration shows how to use programs from Hansen
%   and Sargent's RECURSIVE LINEAR MODELS OF DYNAMIC ECONOMIES
%   to compute asset prices and the term structure of interest
%   rates for a linear quadratic version of the pure exchange
%   economy used by Robert E. Lucas, Jr. in his 1978 Econometrica
%   paper `` Asset Pricing in an Exchange Economy.''
%
%   The economy consists of a representative agent who maximizes
%   
%       -.5 E SUM { beta^t * (c(t) - 30)^2 )}
%
%       subject to c(t) = d1(t),
%   
%   where d1(t) is a first order autoregressive process with
%   mean 5.  We set beta = 1/1.05.
%  
%   We want to price the `Lucas tree', which is an equity that
%   entitles the owner to the dividend d1(t) each period.
%   We also want to compute the term structure of interest rates.
%
pause    %  Press any key to continue the demonstration
clc  
% 
%   To price the tree, we first construct the Arrow-Debreu
%   price process, which we denote pj(t).  The Arrow-Debreu price
%   process,  normalized in terms of time j as the time of the opening
%   market is determined by the formula pj(t) = Mc*x(t)/numj, where
%   numj is the numeraire for the time j price system, and where
%   x(t) is the state vector of the economy.  Let the dividend on
%   the tree be represented as d1(t) = pay * x(t), where pay is
%   a vector that determines the payoff.  For this example,
%   pay = sd(:,1), the first component of the matrix determining
%   the d(t) process.  Let the asset price be denoted a(j) at time
%   j.  Then   we have the representation
%
%        a(j) = E ( Sum beta^(t-j) (p(t) * x(t)) )| I(j) 
%
%    where I(j) is the time j information set.
%
%   Hansen and Sargent show that
%    
%        a(j) = x(t)' v2 x(t) + riskpr
%
%   where v2 is a matrix and riskpr a scalar that are reported below.
%
%
pause    %  Press any key to continue the demonstration

clc
%
%
%   We also compute the term structure of interest rates.
%   Let R(k) be the reciprocal of the gross interest rate on
%   k period ahead sure loans.  The formula for R(k) at time t is
%  
%      R(j) = beta^k (Mc ao ^ k ) x(t) / numt.
%
pause    %  Press any key to continue the demonstration

clc
%
%
%   We have created a version of Lucas's economy in the file
%   clex14.m.  We load in the matrices defining this economy by
%   typing `clex14';
    
pause    %  Press any key to continue the demonstration
clc

clex14
pause    %  Press any key to continue the demonstration
clc
%
%  
%   The matrices have been read in.  For example, to see what
%   values have been assigned to a22 and ud, we type
a22
ud
%
pause    %  Press any key to continue the demonstration
clc
%
%
%   Now we'll compute the equilibrium of the economy using
%   the program solvea.  We just type `solvea'.
echo off
solvea 
echo on


pause    %  Press any key to continue the demonstration
clc
%
%
%   The equilibrium has been computed.
%   To see the values of the equilibrium transition equation,
%   we just type ao and c:
%
ao
c
pause    %  Press any key to continue the demonstration
clc
%
%
%   Now  we'll construct a simulation 
%   of the economy using the
%   program asimul.m.  We set the length of the simulation t1=100.
%   We specify that sy=sc, so that we are simulating the consumption
%   process.   

t1=100;


sy=sc;    %    We type asimul to compute the simulation.
echo off
asimul
echo on
pause    %  Press any key to continue the demonstration
clc
%
%
%    We'll plot the value of consumption:
pause  %  press a key to see the plot of consumption

plot(y),title('Consumption in Lucas economy'),pause
%
%
pause    %  Press any key to continue the demonstration
clc
%
%
%   Now we'll simulate asset prices.  We set the length of the
%   simulation at nt=100.  We want to price the Lucas tree,
%   so we set pay = sd(1,:).   After setting these, we just
%   type asseta.
nt=t1;
pay=sd(1,:)       %   Type asseta to compute the asset prices and simulate 
echo off          %   them.
asseta
echo on
pause    %  Press any key to continue the demonstration
clc
%  
%   The riskpremium for the asset price is stored in riskpr,
%   while the matrix in the quadratic form for the
%   asset price is stored in v2.  Let's look at them.
riskpr
v2
pause    %  Press any key to continue the demonstration
clc
%
%
%    Now let's plot the price of the `tree'.
%
pause  %  press a key to see the price of the tree
%
plot(y(:,3)),title('Price of tree in Lucas economy'),pause
clc
%    The one period gross return on the tree and the one-period 
%    gross return on a risk free one period bond are now plotted.
%
%    The solid line is the return on the Lucas tree.  The dotted
%    line is the return on a one period riskless bond.
%
pause  %  Press a key to see the plot

r1=R1(2:nt).^(-1);r1=r1';
RR=[y(:,4) r1];

plot(RR),title('Rates of return on Lucas tree and safe asset'),pause,clc
%
%     The correlation between these two returns can now be computed.


corrcoef(RR)
pause    %  Press any key to continue the demonstration
clc
%
%   Now let's plot the net one-period rate of return on sure
%   one-period and five-period bonds. The solid line will be the
%   one period return, the dotted line the five period return. 
%
pause   %  Press a key to see the plot


r5=R5(2:nt).^(-1);r5=r5';

r2=R2(2:nt).^(-1);r2=r2';

lr1=log(r1);
lr2=log(r2)./2;
lr5=log(r5)./5;

lr=[lr1 lr5];
plot(lr),title('Rates of return on one- and five-period safe bonds'),pause
clc
%
%
%  Notice that the term structure of interest rates slopes upward
%  when rates are low, and generally slopes downward when rates are high.
%
pause  %  press a key to return to menu
return
