%% Price OptionEmbeddedFixedBond Instrument and Obtain Exercise Probabilities Using Black-Karasinski Model and IRTree Pricer
% This example shows the workflow to price a callable OptionEmbeddedFixedBond instrument and obtain the exercise probabilities when you use a BlackKarasinski model and an IRTree pricing method.
%
%% Create ratecurve Object
% Create a ratecurve object using ratecurve.

Settle = datetime(2018, 1, 1);
ZeroTimes = calyears(1:4)';
ZeroRates = [0.035; 0.042147; 0.047345; 0.052707];
ZeroDates = Settle + ZeroTimes;
Compounding = 1;
ZeroCurve = ratecurve("zero",Settle,ZeroDates,ZeroRates, "Compounding",Compounding)

%% Create OptionEmbeddedFixedBond Instrument Object
% Use fininstrument to create an OptionEmbeddedFixedBond instrument object with an American exercise style.

CouponRate = 0.0425;
Strike = [95; 98];
ExerciseDates = [datetime(2021,1,1); datetime(2022,1,1)];
Maturity = datetime(2022,1,1);
Period = 1;
CallSchedule =  timetable(ExerciseDates,Strike,'VariableNames',{'Strike Schedule'}); 
CallableBond = fininstrument("OptionEmbeddedFixedBond", 'Maturity',Maturity,...
                              'CouponRate',CouponRate,'Period', Period, ...
                              'CallSchedule',CallSchedule,...
                              'CallExerciseStyle', "American",...
                              'Name',"MyCallableBond")                

%% Create BlackKarasinski Model Object
% Use finmodel to create a BlackKarasinski model object.

VolCurve = 0.01;
AlphaCurve = 0.1;
BKModel = finmodel("BlackKarasinski",'alpha',AlphaCurve,'sigma',VolCurve)

%% Create IRTree Pricer Object
% Use finpricer to create an IRTree pricer object and use the ratecurve object for the 'DiscountCurve' name-value pair argument.

BKTreePricer = finpricer("IRTree",'Model',BKModel,'DiscountCurve',ZeroCurve,'TreeDates',ZeroDates)

%% Price OptionEmbeddedFixedBond Instrument
% Use price to compute the price and sensitivities for the OptionEmbeddedFixedBond instrument.

[Price,  PriceResults]= price(BKTreePricer, CallableBond)

% Examine the output PriceResults.PricerData.PriceTree.ExTree, which contains the exercise indicator arrays. In the cell array, a 1 indicates an exercised option and a 0 indicates an unexercised option.

PriceResults.PricerData.PriceTree.ExTree{5} 

% No options are exercised.

PriceResults.PricerData.PriceTree.ExTree{4} 

% The instrument is exercised at all nodes.

PriceResults.PricerData.PriceTree.ExTree{3} 

% No options are exercised.

PriceResults.PricerData.PriceTree.ExTree{2} 

% No options are exercised.
% View the probability of reaching each node from the root node using PriceResults.PricerData.PriceTree.ProbTree.

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

% View the exercise probabilities using PriceResults.PricerData.PriceTree.ExProbTree. PriceResults.PricerData.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.

PriceResults.PricerData.PriceTree.ExProbTree{5}
PriceResults.PricerData.PriceTree.ExProbTree{4}
PriceResults.PricerData.PriceTree.ExProbTree{3}
PriceResults.PricerData.PriceTree.ExProbTree{2}

% View the exercise probabilities at each tree level using PriceResults.PricerData.PriceTree.ExProbsByTreeLevel. PriceResults.PricerData.PriceTree.ExProbsByTreeLevel is an array in which each row holds the exercise probability for a given option at each tree observation time.

PriceResults.PricerData.PriceTree.ExProbsByTreeLevel

% Copyright 2020 The MathWorks, Inc.