function [Ca,Ce,EE] = CRRmDaeC(T,S0,K,Di,ri,v,N) 
% Octave/MATLAB function to price American and
% European Call options on an asset decomposed
% into S=Sx+Ipv by CRRD(), using the modified
% Cox-Ross-Rubinstein (CRR) yield curve model.
% INPUTS:                             (Example)
%   T =  expiration time               (1 year)
%   S0 = stock price                      (100)
%   K =  strike price                     (101)
%   Di = dividend sequence 2:N+2    (see below)
%   ri = risk-free APRs 1:N+1       (see below)
%   v =  volatility; must be >0          (0.15)
%   N =  height of the binomial tree       (12)
% OUTPUT:
%   Ca =  price of American Call at all (n,j).
%   Ce =  price of European Call at all (n,j).
%   EE =  Is early exercise optimal at (n,j)?
% EXAMPLE:
%   Di=[0 1 0 0 1 0 0 2 0 0 2 0 0 2]; % Di(k+1)=D_k
%   ri=[1 1 1 1 1 2 2 2 2 2 3 3 3]/100; % ri(k+1)=r_k
%   [Ca,Ce,EE]=CRRmDaeC(1,100,101,Di,ri,0.15,12);
%
  [pui,up,Ri]=CRRparams(T,ri,v,N); % Modified CRR
  [S,Sx,Ipv]=CRRmD(T,S0,Di,ri,v,N); % decompose
  Ca=zeros(N+1,N+1); Ce=zeros(N+1,N+1); % output
  EE=zeros(N,N); % early exercise optimal? T/F
  for j=0:N  % to set terminal values at (N,j)
    xC=S(N+1,j+1)-K; % Call payoff at expiry is the
    Ca(N+1,j+1)=Ce(N+1,j+1)=max(xC,0); % plus part
  end
  for n=(N-1):(-1):0 % textbook time indices
    for j=0:n % states j={0,1,...,n} at time n
      % Backward pricing for A and E:
      bCe=(pui(n+1)*Ce(n+2,j+2) + ...
	   (1-pui(n+1))*Ce(n+2,j+1))/Ri(n+1);   
      bCa=(pui(n+1)*Ca(n+2,j+2) + ...
	   (1-pui(n+1))*Ca(n+2,j+1))/Ri(n+1);   
      xC=S(n+1,j+1)-K; % Call exercise value
      % Set prices at node (n,j):
      Ce(n+1,j+1)=bCe; % always binomial price
      Ca(n+1,j+1)=max(bCa,xC); % highest price
      % Is early exercise optimal?
      EE(n+1,j+1)=(xC>bCa); % Yes, if xC>bCa
    end
  end % ...backward induction pricing:
  return; % Ca,Ce, and EE are defined.
end
