function [A,q]=IBT(T,S0,Ki,Ci,r,v,N)
% Example to construct an implied binomial tree
% 
% Example market data for BAC on 2021-11-23:
% T=24/365;  % Nov 23 to Dec 17, 2021
% S0 = 47.56;  % spot price for BAC
% Ki=42:50;  % strike prices for Calls on BAC, expiry Dec 17, 2021
% Ci=[5.45;4.55;3.60;2.76;2.05;1.42;0.92;0.60;0.38];  % Call bids
% r = 0.07/100; % riskless APR (T-bill, 4 wks)
% v=23/100; % implied volatility for ATM Call on BAC, this expiry
% Computed quantities:
m=length(Ki);  % number of strike prices to include
% N=m+1;  % minimal depth for this many options
up=exp(v*sqrt(T/N)); % up factor
dn=exp(-v*sqrt(T/N)); % down factor
ee=-N:2:N;  % powers of up to use for SNj
SNj=S0*up.^ee;  % S(N,j), j=0,...,N
R=exp(r*T/N); % riskless factor per time step
% Call payoffs
CiNj=zeros(length(Ki),N+1);  % one row per Call, with N+1 states
for i=1:length(Ki)
 for j=1:N+1
  CiNj(i,j)=max(SNj(j)-Ki(i),0); % Plus(S(N,j)-K(i))
 end
end
% Market payoff matrix
A=[ones(1,N+1)*exp(r*T);  % riskless, or numeraire
  SNj;                    % underlying asset S(N,j)
  CiNj ];                 % Call payoffs 
% Spot price vector
q=[1;                     % numeraire
   S0;                    % underlying
   Ci];                   % options
% Time-T risk neutral probabilities
% Q=A\q;
return
end

