% hsdemo5.m

clc
clg
echo on
%
%              Equilibrium Allocations in Two-Agent
%                     Pure Exchange Economy
%
%   This demo illustrates the use of the programs `heter.m' and
%   `simulha.m' in computing allocations to individuals in
%   a heterogeneous agent economy.  The economy is a pure
%   exchange economy, like the one we studied for the linear
%   quadratic version of Lucas's asset pricing model.
%
%   The economy consists of two agents, each of whom maximizes
%
%     E SUM { beta^ t (ci(t) - bi(t))^2 )} | I(0)
%
%   subject to the budget constraint
%
%      E SUM { beta ^ t p0(t) (ci(t) - di(t))} | I(0) = 0 ,
%
%   where p0(t) is the Harrison-Kreps price process, {ci(t)}
%   is the consumption process of agent i, {di(t)} is the
%   endowment process owned by agent i, and {bi(t)} is the
%   preference shock process for agent i.
pause   %  press a key to continue the demonstration
clc
%
%   The endowment and proference processes satisfy
%
%     bi(t) = Ubi* z(t),  di(t) = Udi(t)   for i = 1,2
%
%     z(t) = a22 * z(t-1) + c2 * w(t),   E w(t)w(t)' = I,
%
%   where w(t) is a martingale difference sequence.
%   The equilibrium condition is
%
%     c1(t) + c2(t) = d1(t) + d2(t).
%
%
%   This is a representative agent economy whose aggregate
%   allocation satisfies the above equation.   Preferences of the
%   representative agent are
%
%     SUM { beta ^ t (c1(t) + c2(t) - [b1(t) + b2(t)] ) } | I(0) .
%
pause   %  Press a key to continue demonstration
clc
%
%
%   First we read in the paramters of the economy, which we
%   have placed in the file clex35.m
%
pause  %  press any key to read in the economy
clex35
pause  %  The economy is in memory.  Press a key to see some matrices.
clc
%
%
a22
c2
%
pause  %  Press a key to see ud1 and ud2
clc
ud1
ud2
pause  %  Press a key to see ub1 and ub2
clc
ub1
ub2
pause  %  press a key to continue
clc
%   We have set ub1 and ub2 so that b1(t) = b2(t) = 15.
%
%   From the above settings for a22, c2, ud1, ud2, you can figure
%   out what the processes for d1(t) and d2(t) are.
%
pause   % press a key to compute the equilibrium with `solvea'
echo off
solvea
echo on

%
%
pause   % press a key to compute and simulate allocation to the first agent

ubi=ub1
udi=ud1

t1=100
x0=x0'
echo off
heter
echo on
sy=[sci;sca;sdi]
echo off
simulha
echo on
y1=y
%  We have stored  c1(t), aggregate consumption, and d1(t) in y1.
pause  %  Press any key to continue
clc
clg
%
%
pause  %  Press a key to compute and simulate allocation to the second agent
clc
ubi=ub2
udi=ud2
echo off
heter
echo on
sy=[sci;sca;sdi]
echo off
simulha
echo on
y2=y
%  We have stored c2(t), aggregate cons, and d2(t) in y2.
pause %  press a key to continue
clc
%
%
z=[y1(:,1),y2(:,1),y2(:,2),y1(:,3),y2(:,3)]
s1=y1(:,3)-y1(:,1);    % compute savings of agent 1
s2=y2(:,3)-y2(:,1);    % compute savings of agent 2
clg
%
%   Now we plot the consumption and saving of each type of agent.
%   Notice that the `consumption sharing' property holds.
%   c1(t) and c2(t) are perfectly correlated.
pause  %  Press a key to see the plot
clg

subplot(221),plot([y1(:,1),y2(:,1)]),title('consn allocations'),...
subplot(222),plot(s1),title('saving of agent 1'),subplot(223),plot(s2),...
title('saving of agent2'),pause
clg
clc
%
%
pause  %  This completes the demo.  Press any key to return to menu.

