function [C,P] = CRRlb(T,S0,r,v,N)
% Octave/MATLAB function to price Lookback Call and
% Put options using the Cox-Ross-Rubinstein (CRR)
% binomial pricing model.
% INPUTS:                             (Example)
%   T  =  expiration time in years     (1)
%   S0 =  spot stock price             (100)
%   r  =  riskless yield per year      (0.05)
%   v  =  volatility; must be >0       (0.20)
%   N  =  height of the tree           (4)
% OUTPUTS:
%    C = Call option NRT
%    P = Put option NRT
% EXAMPLE:
%    [C,P]=CRRlb(1,100,0.05,0.20,4); C(1),P(1)
%
  [pu,up,R]=CRRparams(T,r,v,N);
  Sbar=NRTCRR(S0,up,1/up,N); % expanded S tree
  MinS=NRTmin(Sbar,N); MaxS=NRTmax(Sbar,N);
  C=zeros(size(Sbar)); P=zeros(size(Sbar));
  for m=2^N:(2*2^N -1) % states at expiry N
    C(m)=Sbar(m)-MinS(m); % Call payoff
    P(m)=MaxS(m)-Sbar(m); % Put payoff
  end  % ...payoffs at expiry are set.
  for m=(2^N-1):-1:1 % indices down to t=0
    C(m) = (pu*C(2*m+1) + (1-pu)*C(2*m))/R;
    P(m) = (pu*P(2*m+1) + (1-pu)*P(2*m))/R;
  end % ... backward induction is complete
  return %  C(1),P(1) contain the premiums
end

