function [C, P] = CRRaro(T, S0, K, r, v, N)
% Octave/MATLAB function to price Average Rate Options
% (ARO, or Asian Options) using the Cox-Ross-Rubinstein
% (CRR) binomial pricing model.
% INPUTS:                             (Example)
%    T  = expiration time in years      (0.5)
%    S0 = stock price                   (100)
%    K  = strike price                  (95)
%    r  = riskless yield per year       (0.05)
%    v  = volatility; must be >0        (0.15)
%    N  = height of the binomial tree   (5)
% OUTPUTS:
%    [C,P] = Call and Put option prices, all m
% EXAMPLE:
%   [C,P]=CRRaro(0.5,100,95,0.05,0.15,5); C(1),P(1)
%
  [pu,up,R]=CRRparams(T,r,v,N);
  Sbar=NRTCRR(S0,up,1/up,N); % expanded S tree
  Ubar=NRTpsums(Sbar,N); % sums along each path
  C=zeros(size(Sbar));  P=zeros(size(Sbar));
  for m=2^N:(2*2^N -1) % m indices at expiry
    Avg = Ubar(m)/(N+1);  % N+1 summands for these
    C(m) = max(0,Avg-K); % Call payoff at expiry
    P(m) = max(0,K-Avg); % Put payoff at expiry
  end  % ...prices set at expiry.
  for m=(2^N-1):-1:1 % recursive previous indices
    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 % ...all prices set by backward induction. 
  return
end
