function [W,C] = CRRcc(T,T1,S0,K,L,r,v,N) 
%  Octave/MATLAB function to price a compound Call option
%  using the Cox-Ross-Rubinstein (CRR) binomial model.
% INPUTS:                                     (Example)
%   T  = expiration time in years             (1)
%   T1 = choice time; must have 0<T1<T        (0.5)
%   S0 = spot stock price                     (90)
%   K =  stock strike price at expiry T       (95)
%   L =  option strike price at time T1       (4.50)
%   r =  risk-free yield                      (0.02)
%   v =  volatility; must be >0               (0.15)
%   N =  height of the binomial tree          (10)
% OUTPUT:
%   W =  price of the compound Call option
%   C  =  price of the vanilla European Call option
% EXAMPLE:
%  [W,C] = CRRcc(1,0.5,90,95,4.50,0.02,0.15,10); 
%  W(1,1),C(1,1)  % to get just W(0) and C(0)
%
  [pu,up,R]=CRRparams(T,r,v,N); % Use CRR values 
  [C,P]=CRReur(T,S0,K,r,v,N); % vanilla options at T
  M=round(T1*N/T); % number of time steps to time T1
  W=zeros(M+1,M+1);   % smaller output matrix
  for j=0:M  % Set terminal values at (M,0),...,(M,M):
    W(M+1,j+1) = max(C(M+1,j+1)-L,0); % positive part
  end
  for n=M-1:-1:0   % Backward induction 
    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
  end
  return  % Prices in matrices W and C are defined.
end
