%% Price a FixedBondOption Instrument Using Hull-White Model and Hull-White Tree Pricer
% This example shows the workflow to price a FixedBondOption instrument when you use a HullWhite model and an IRTree pricing method.
%% Create FixedBond Instrument Object
% Use fininstrument to create a FixedBond instrument object as the underlying bond.

BondInst = fininstrument("FixedBond",'Maturity',datetime(2029,9,15),'CouponRate',.021,'Period',1,'Name',"bond_instrument")

%% Create FixedBondOption Instrument Objects
% Use fininstrument to create three callable FixedBondOption instrument objects with European, American, and Bermudan exercise.

FixedBOptionEuro = fininstrument("FixedBondOption",'ExerciseDate',datetime(2025,9,15),'Strike',98,'Bond',BondInst,'OptionType',"call",'ExerciseStyle',"european",'Name',"fixed_bond_option_european")
FixedBOptionAmerican = fininstrument("FixedBondOption",'ExerciseDate',datetime(2025,9,15),'Strike',98,'Bond',BondInst,'OptionType',"call",'ExerciseStyle',"american",'Name',"fixed_bond_option_american")
FixedBOptionBermudan = fininstrument("FixedBondOption",'ExerciseDate',[datetime(2025,9,15) ; datetime(2025,11,15)],'Strike',[98,1000],'Bond',BondInst,'OptionType',"call",'ExerciseStyle',"bermudan",'Name',"fixed_bond_option_bermudan")

%
%% Create ratecurve Object
% Create a ratecurve object using ratecurve.

Settle = datetime(2019,9,15);
Type = 'zero';
ZeroTimes = [calyears([1:10])]';
ZeroRates = [0.0055 0.0061 0.0073 0.0094 0.0119 0.0168 0.0222 0.0293 0.0307 0.0310]';
ZeroDates = Settle + ZeroTimes;
 
myRC = ratecurve('zero',Settle,ZeroDates,ZeroRates)

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

HullWhiteModel = finmodel("HullWhite",'Alpha',0.01,'Sigma',0.05)

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

HWTreePricer = finpricer("IRTree",'Model',HullWhiteModel,'DiscountCurve',myRC,'TreeDates',ZeroDates)
HWTreePricer.Tree

%% Price FixedBondOption Instruments
% Use price to compute the price and sensitivities for the two FixedBondOption instruments.

[Price, outPR] = price(HWTreePricer,FixedBOptionEuro,["all"])
outPR.Results
[Price, outPR] = price(HWTreePricer,FixedBOptionAmerican,["all"])
outPR.Results
[Price, outPR] = price(HWTreePricer,FixedBOptionBermudan,["all"])
outPR.Results

% Copyright 2019 The MathWorks, Inc.