%% Price Swaption Instrument Using LinearGaussian2F Model and IRMonteCarlo Pricer
% This example shows the workflow to price a Swaption instrument when using a LinearGaussian2F model and an IRMonteCarlo pricing method.
%% 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 30])]';
ZeroRates = [0.0052 0.0055 0.0061 0.0073 0.0094 0.0119 0.0168 0.0222 0.0293 0.0307]';
ZeroDates = Settle + ZeroTimes;
 
myRC = ratecurve('zero',Settle,ZeroDates,ZeroRates)

%% Create Swap Instrument Object
% Use fininstrument to create the underlying Swap instrument object.

Swap = fininstrument("Swap",'Maturity',datetime(2022,1,1),'LegRate',[0.05,0.04],'Name',"swap_instrument")

%% Create Swaption Instrument Object
% Use fininstrument to create a Swaption instrument object.

Swaption = fininstrument("Swaption",'Strike',0.02,'ExerciseDate',datetime(2021,7,1),'Swap',Swap,'Name',"swaption_option")

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

LinearGaussian2FModel = finmodel("LinearGaussian2F",'Alpha1',0.07,'Sigma1',0.01,'Alpha2',0.5,'Sigma2',0.006,'Correlation',-0.7)

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

simDates = datetime(2019,7,1)+calmonths(0:6:30);
outPricer = finpricer("IRMonteCarlo",'Model',LinearGaussian2FModel,'DiscountCurve',myRC,'SimulationDates',simDates)

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

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

% Copyright 2021 The MathWorks, Inc.