function [W, C, P] = CRRcho(T, T1, S0, K, r, v, N) 
% Octave/MATLAB function to price a chooser option
% with the Cox-Ross-Rubinstein (CRR) binomial model.
% INPUTS:                                 (Example)
%    T  =  expiry time in years            (1)
%    T1 =  choice time; must have 0<T1<T   (0.5)
%    S0 = spot stock price                 (90)
%    K =  strike price                     (95)
%    r =  risk-free annual yield           (0.02)
%    v =  volatility; must be >0           (0.15)
%    N =  height of the binomial tree      (10)
% OUTPUT:
%    W =  price of the chooser option
%    C =  price of the vanilla European Call option
%    P =  price of the vanilla European Put option
% EXAMPLE:
%   [W, C, P] = CRRcho(1, 0.5, 90, 95, 0.02, 0.15, 10);
%   W(1,1),C(1,1),P(1,1) % get just W(0),C(0), and P(0)
%
  [pu,up,R] = CRRparams(T,r,v,N); % Use CRR values 
  [C,P]=CRReur(T,S0,K,r,v,N); % price C,P from expiry T
  M=floor(T1*N/T);  % number of time steps before T1
  W=zeros(M+1,M+1); % smaller output matrix W
  for j = 0:M     % terminal values at (M,0),...,(M,M)
    W(M+1,j+1) = max(C(M+1,j+1), P(M+1,j+1)); % choose 
  end  % Call and Put prices at time T1 initialize W
  for n = M-1:-1:0  % start at terminal time M
    for j = 0:n  % Binomial pricing model value
      W(n+1,j+1)=(pu*W(n+2,j+2)+(1-pu)*W(n+2,j+1))/R;   
    end  % ...backward induction fills W.
  end
  return; % Prices in matrices W, C, and P are defined.
end
