%% Obtain Callable Bond Exercise Information Using an HW Interest-Rate Tree Model
% Create a HWTree with the following data:

Rates = [0.035; 0.042147; 0.047345; 0.052707];
ValuationDate = 'Jan-1-2019';
StartDates = ValuationDate;
EndDates = {'Jan-1-2020'; 'Jan-1-2021'; 'Jan-1-2022'; 'Jan-1-2023'};
Compounding = 1;

%
% Create a RateSpec.

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

% Define the callable bond instruments.

Settle = '01-Jan-2019';
Maturity = {'01-Jan-2022';'01-Jan-2023'};
CouponRate = {{'01-Jan-2021' .0425;'01-Jan-2023' .0450}};  
OptSpec='call';
Strike= [98;95];
ExerciseDates= {'01-Jan-2021';'01-Jan-2022'};

%
% Build the HW tree with the following data.

VolDates = ['1-Jan-2020'; '1-Jan-2021'; '1-Jan-2022'; '1-Jan-2023'];
VolCurve = 0.05;
AlphaDates = '01-01-2023';
AlphaCurve = 0.05;
 
 
HWVolSpec = hwvolspec(RS.ValuationDate, VolDates, VolCurve,... 
AlphaDates, AlphaCurve);
HWTimeSpec = hwtimespec(RS.ValuationDate, VolDates, Compounding);
HWT = hwtree(HWVolSpec, RS, HWTimeSpec);

%
% Price the instruments.

[Price, PriceTree]= optembndbyhw(HWT, CouponRate,  Settle, Maturity,OptSpec, Strike,...
ExerciseDates, 'Period', 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{5}   

% There is no exercise for instrument 1 or 2.

PriceTree.ExTree{4} 

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

PriceTree.ExTree{3} 

% There is the exercise for instrument 1 at some node and no exercise for instrument 2.

PriceTree.ExTree{2} 

% There is no exercise for instrument 1 or 2.
% Next view the probability of reaching each node from the root node using PriceTree.ProbTree.

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

% 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{5}
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.