%% Use BraceGatarekMusiela Model and IRMonteCarlo Pricer to Price Floor Instrument
% This example shows the workflow to price a Floor instrument when using a BraceGatarekMusiela model and an IRMonteCarlo pricing method.
%% Create Floor Instrument Object
% Use fininstrument to create a Floor instrument object.

FloorOpt = fininstrument("Floor","Maturity",datetime(2022,9,15),'Strike',0.05,'Reset',1,'Name',"floor_option")

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

BGMVolFunc = @(a,t) (a(1)*t + a(2)).*exp(-a(3)*t) + a(4);
BGMVolParams = [.3 -.02 .7 .14];
numRates = 20;
VolFunc(1:numRates-1) = {@(t) BGMVolFunc(BGMVolParams,t)};
Beta = .08;
CorrFunc = @(i,j,Beta) exp(-Beta*abs(i-j));
Correlation = CorrFunc(meshgrid(1:numRates-1)',meshgrid(1:numRates-1),Beta);
BGM = finmodel("BraceGatarekMusiela",'Volatility',VolFunc,'Correlation',Correlation,'Period',1);

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

Settle = datetime(2019,1,1);
Type = 'zero';
ZeroTimes = [calmonths(6) calyears([1 2 3 4 5 7 10 20])]';
ZeroRates = [0.0052 0.0055 0.0061 0.0073 0.0094 0.0119 0.0168 0.0222 0.0293]';
ZeroDates = Settle + ZeroTimes;
 
myRC = ratecurve('zero',Settle,ZeroDates,ZeroRates)

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

outPricer = finpricer("IRMonteCarlo",'Model',BGM,'DiscountCurve',myRC,'SimulationDates',ZeroDates)

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

[Price,outPR] = price(outPricer,FloorOpt,["all"])
outPR.Results

% Copyright 2020 The MathWorks, Inc.