%ASSETA.M, a script file, not a function.
%   ASSETA calculates asset prices for a recursive linear equilbrium.
%   SOLVEA and ASIMUL must be run first.  Their output must be in memory.
%   The state variable x(t+1) satisfies   x(t+1) = ao x(t) + c w(t+1)
%   where ao and c are computed in SOLVEA, the asset payoff is
%   pay*x(t), discount factor is beta, and marginal utility is
%   mc*x(t).  The matrix pay and the scalar nt must be in memory.
%   The integer nt is the number of periods over which the asset prices
%   are calculated.
%   ASSETA calculates and simulates the price of a perpetual claim to
%   the stream of payouts pay*x(t).  The marginal rate of substitution,
%   the payoff, the asset price, and the gross return on the asset
%   are returned in y for a simulation of length nt.  The program
%   also computes the prices of j-period claims on sure consumption
%   for j=1,2, and 5.  The simulated values of these are returned
%   in R1, R2, and R5, respectively.
										
disp('Asset Pricing Calculations.')
disp('      ')
  % Number of time periods to simulate is nt, which must be input
  % before using program

pay      % The asset with payoff pay*x(t) is being priced.
disp('  ')

init=x0;      %sets vector of initial conditions

init=init';

[nas,junk]=size(pay);

numer=1;        %sets numeraire as first consumption good
disp('  ')
disp('Calculating. Please Wait.')

mn=zeros(1,nc);  mn(1,numer)=1;

mn=mn*mc;


%simulate x

rand('normal');

w=rand(nt,nw);
w=u;      %Replaces w with innovations from simulation
[ys,xs]=dlsim(ao,c,[pay;mn],zeros(nas+1,nw),w,init);

%Calculate generalized variance

a11s=sqrt(beta)*ao;

v1=doublej(a11s,c*c')*beta/(1-beta);

aprice=zeros(nt,1);

ii=1;
while ii <= nas;

pay1=pay(ii,:);

v2=doublej(a11s',pay1'*mn);

i=2;

aprice1=xs(1,:)*v2*xs(1,:)';

while i <= nt;

aprice1=[aprice1; xs(i,:)*v2*xs(i,:)'];

   i=i+1;
   end;

aprice=[aprice aprice1];

ii=ii+1;
end;

aprice=aprice(:,2:nas+1);

aprice=ones(nt,1)*mn*v1*pay' + aprice;
riskpr=mn*v1*pay';     % Calculates risk premium
mus1=xs*mn';

aprice=aprice./(mus1*ones(1,nas));
[ms,ns]=size(ao);
R1=beta*mn*ao*(xs')./(mus1');
R2=(beta^2)*mn*(ao^2)*(xs')./(mus1');
R5=(beta^5)*mn*(ao^5)*(xs')./(mus1');
pays=xs*pay';

ret=aprice(2:nt,:)./(aprice(1:nt-1,:)-pays(1:nt-1,:));

mrs=beta*mus1(2:nt,:)./mus1(1:nt-1,:);

mus1=mus1(2:nt,:);

pays=pays(2:nt,:);

aprice=aprice(2:nt,:);

timen=2:nt;

y=[mrs, pays, aprice, ret];

disp('The simulated values for the marginal rate of subsitution, ...')
disp('are in the vector Y.  The columns of Y are in the order:')
disp('[mrs, payoff, asset prices, returns]')
disp('  ')
disp(' risk premium is in riskpr ')
disp(' v2 is the moment matrix in asset formula')
t=1:(nt-1);

