function Lbar = PathAD(pu, R, N)
% Octave/MATLAB function to compute spot prices
% for path-dependent Arrow-Debreu securities by
% forward induction.
% INPUTS:                                (Example)
%  pu = NxN matrix of up probabilities (3x3 lower)
%  R  = NxN matrix of riskless returns (3x3 lower)
%  N = tree height, must be >0                 (3)
% OUTPUTS:
%   Lbar = array of Arrow-Debreu prices barlambda(m).
% EXAMPLE:
%   pu = [.51,0,0; .52,.53,0; .54,.55,.56];
%   R = [1.01,0,0; 1.02,1.03,0; 1.04,1.05,1.06];
%   Lbar = PathAD(pu,R,3)
%
  Lbar = ones(1,2*2^N-1); %...so Lbar(1)=1=lambda(1)
  pbar = NRTinject(pu,N-1); % inject up probs.
  Rbar = NRTinject(R,N-1); % inject riskless returns
  for m=1:2^N-1  % all indices before the last row
    Lbar(2*m)=(1-pbar(m))*Lbar(m)/Rbar(m); % down
    Lbar(2*m+1) = pbar(m)*Lbar(m)/Rbar(m); % up
  end
  return; % Lbar(m) == barlambda(m), 0<m<2*2^N
end
