%% Compute Implied Volatilities for Swaption Instrument
% This example shows the workflow to compute implied volatilities for a Swaption Instrument.
%% Create ratecurve Object
% Create a ratecurve object using ratecurve.

ValuationDate = datetime(2016,3,5);
ZeroDates = datemnth(ValuationDate,[1 2 3 6 9 12*[1 2 3 4 5 6 7 8 9 10 12]])';
ZeroRates = [-0.33 -0.28 -0.24 -0.12 -0.08 -0.03 0.015 0.028 ...
            0.033 0.042 0.056 0.095 0.194 0.299 0.415 0.525]'/100;
Compounding = 1;
ZeroCurve = ratecurve("zero",ValuationDate,ZeroDates,ZeroRates,'Compounding',Compounding)

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

Alpha = 0.0135;
Beta = 0.5;
Rho = 0.4654;
Nu = 0.4957;
Shift = 0.008;
 
SABRModel = finmodel("SABR",'Alpha',Alpha,'Beta',Beta,'Rho',Rho,'Nu',Nu,'Shift',Shift)

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

SABRPricer = finpricer("Analytic", 'Model', SABRModel,'DiscountCurve', ZeroCurve)

%% Compute Implied Volatilities
% Use volatilities to compute the implied volatilites for a Swaption instrument.

SwaptionExerciseDate = datetime(2017,3,5);
ForwardValue = 0.0007;
StrikeGrid = [-0.5; -0.25; -0.125; 0; 0.125; 0.25; 0.5; 1.0; 1.5]/100;
MarketStrikes = ForwardValue + StrikeGrid;

outVolatilities = volatilities(SABRPricer, SwaptionExerciseDate, ForwardValue, MarketStrikes)

% Copyright 2020 The MathWorks, Inc.