%% Obtain Callable Bond Exercise Information Using a BDT Interest-Rate Tree Model
% Create a BDTTree with the following data:

ZeroRates = [ 0.025;0.03;0.045];
Compounding = 1;
StartDates = ['jan-1-2007';'jan-1-2008';'jan-1-2009'];
EndDates   = ['jan-1-2008';'jan-1-2009';'jan-1-2010'];
ValuationDate = 'jan-1-2007';

%
% Create a RateSpec.

RateSpec = intenvset('Rates', ZeroRates, 'StartDates', ValuationDate, 'EndDates', ...
EndDates, 'Compounding', Compounding, 'ValuationDate', ValuationDate);

%
% Build the BDT tree with the following data.

Volatility = 0.10 * ones (3,1);
VolSpec = bdtvolspec(ValuationDate, EndDates, Volatility)
TimeSpec = bdttimespec(ValuationDate, EndDates, Compounding);
BDTTree = bdttree(VolSpec, RateSpec, TimeSpec)

%
% Define the callable bond instruments.

Settle = '01-Jan-2007';
Maturity = {'01-Jan-2008';'01-Jan-2010'};
CouponRate = {{'01-Jan-2008' .0425;'01-Jan-2010' .0450}};  
OptSpec='call';
Strike= [86;90];
ExerciseDates= {'01-Jan-2008';'01-Jan-2010'};

% Price the instruments.

[Price, PriceTree]= optembndbybdt(BDTTree, CouponRate,  Settle, Maturity, OptSpec, Strike,...
ExerciseDates, 'Period', 1,'AmericanOp', 1)

% Examine the output PriceTree.ExTree. PriceTree.ExTree contains the exercise indicator arrays. Each element of the cell array is an array containing 1's where an option is exercised and 0's where it is not.

PriceTree.ExTree{4} 

% There is no exercise for instrument 1 and instrument 2 is exercised at all nodes.

PriceTree.ExTree{3} 

% There is no exercise for instrument 1 or instrument 2.

PriceTree.ExTree{2} 

% There is exercise for instrument 1 at all nodes and instrument 2 is exercised at some nodes.
% Next view the probability of reaching each node from the root node using PriceTree.ProbTree.

PriceTree.ProbTree{2}
PriceTree.ProbTree{3}
PriceTree.ProbTree{4}

% Then view the exercise probabilities using PriceTree.ExProbTree. PriceTree.ExProbTree contains the exercise probabilities. Each element in the cell array is an array containing 0's where there is no exercise, or the probability of reaching that node where exercise happens.

PriceTree.ExProbTree{4}
PriceTree.ExProbTree{3}
PriceTree.ExProbTree{2}

% View the exercise probabilities at each tree level using PriceTree.ExProbsByTreeLevel. PriceTree.ExProbsByTreeLevel is an array with each row holding the exercise probability for a given option at each tree observation time.

PriceTree.ExProbsByTreeLevel

% Copyright 2019 The MathWorks, Inc.