%% Use CDS Black Pricer and CDS Black Model to Price CDS Index Options
% This example shows the workflow to use a CDSOption instrument to price CDS index options when you use a CDSBlack model and a CDSBlack pricing method.
%% Set Up Data for CDS Index

% CDS index and option data
Recovery = .4;
Basis = 2;
Period = 4;
CDSMaturity = datetime(2017, 6, 20);
ContractSpread = 100;
IndexSpread = 140;
BusDayConvention = 'follow';
Settle = datetime(2012, 4, 13);
OptionMaturity = datetime(2012, 6, 20);
OptionStrike = 140;
SpreadVolatility = .69;

%% Create ratecurve Object for Zero Curve Using irbootstrap
% Create ratecurve object for a zero curve using irbootstrap.

% Zero curve data
DepRates = [0.004111 0.00563 0.00757 0.01053]';
DepTimes = calmonths([1 2 3 6]');
DepDates = Settle + DepTimes;
nDeposits = length(DepTimes);

SwapRates = [0.01387 0.01035 0.01145 0.01318 0.01508 0.01700 0.01868 ...
    0.02012 0.02132 0.02237 0.02408 0.02564 0.02612 0.02524]';
SwapTimes = calyears([1 2 3 4 5 6 7 8 9 10 12 15 20 30]');
SwapDates = Settle + SwapTimes;
nSwaps = length(SwapTimes);

nInst = nDeposits + nSwaps;

BootInstruments(nInst,1) = fininstrument.FinInstrument;
for ii=1:length(DepDates)
    BootInstruments(ii) = fininstrument("deposit","Maturity",DepDates(ii),"Rate",DepRates(ii));
end

for ii=1:length(SwapDates)
    BootInstruments(ii+nDeposits) = fininstrument("swap","Maturity",SwapDates(ii),"LegRate",[SwapRates(ii) 0]);
end

ZeroCurve = irbootstrap(BootInstruments,Settle)

%% Bootstrap Default Probability Curve
% Use defprobstrip to bootstrap default probability curve assuming a flat index spread.

ProbDates = datemnth(OptionMaturity,(0:5*12)');
MarketCDSInstruments = fininstrument("cds", ...
    'ContractSpread', ContractSpread, 'Maturity', CDSMaturity);
DefaultProbCurve = defprobstrip(ZeroCurve, MarketCDSInstruments, IndexSpread, 'ProbDates', ProbDates)

%% Compute Spot and Forward RPV01s
% Compute the spot and forward RPV01s using cdsrpv01.

ProbData = [datenum(DefaultProbCurve.Dates) DefaultProbCurve.DefaultProbabilities];

% RPV01(t,T)
RPV01_CDSMaturity = cdsrpv01(ZeroCurve,ProbData,Settle,CDSMaturity)

% RPV01(t,t_E,T)
RPV01_OptionExpiryForward = cdsrpv01(ZeroCurve,ProbData,Settle,CDSMaturity,...
    'StartDate',OptionMaturity)

% RPV01(t,t_E) = RPV01(t,T) - RPV01(t,t_E,T)
RPV01_OptionExpiry = RPV01_CDSMaturity - RPV01_OptionExpiryForward

%% Compute Spot Spreads
% Compute the spot spreads using cdsspread.

% S(t,t_E)
Spread_OptionExpiry = cdsspread(ZeroCurve,ProbData,Settle,OptionMaturity,...
    'Period',Period,'Basis',Basis,'BusDayConvention',BusDayConvention,...
    'PayAccruedPremium',true,'recoveryrate',Recovery)

% S(t,T)
Spread_CDSMaturity = cdsspread(ZeroCurve,ProbData,Settle,CDSMaturity,...
    'Period',Period,'Basis',Basis,'BusDayConvention',BusDayConvention,...
    'PayAccruedPremium',true,'recoveryrate',Recovery)

%% Compute Forward Spread
% Compute the forward spread using the spot spreads and RPV01s.

% F = S(t,t_E,T)
ForwardSpread = (Spread_CDSMaturity.*RPV01_CDSMaturity - Spread_OptionExpiry.*RPV01_OptionExpiry)./RPV01_OptionExpiryForward

%% Compute Front-End Protection
% Compute the front-end protection (FEP).

FEP = 10000*(1-Recovery)*ZeroCurve.discountfactors(OptionMaturity)*DefaultProbCurve.DefaultProbabilities(1)

%% Compute Adjusted Forward Spread
% Compute the adjusted forward spread to use when creating an CDSOption instrument.

AdjustedForwardSpread = ForwardSpread + FEP./RPV01_OptionExpiryForward

%% Compute CDS Option Prices with Adjusted Forward Spread
% Use fininstrument to create a CDSOption instrument for a single-name CDS option.

CDS = fininstrument("cds",'ContractSpread', ContractSpread, 'Maturity', CDSMaturity)

% Use fininstrument to create a CDSOption instrument for two CDS index option instruments.

CDSCallOption = fininstrument("cdsoption", 'Strike', OptionStrike, ...
    'ExerciseDate', OptionMaturity, 'OptionType', 'call', 'CDS', CDS, ...
    'Knockout',true, 'AdjustedForwardSpread', AdjustedForwardSpread)

CDSPutOption = fininstrument("cdsoption", 'Strike', OptionStrike, ...
    'ExerciseDate', OptionMaturity, 'OptionType', 'put', 'CDS', CDS, ...
    'Knockout',true, 'AdjustedForwardSpread', AdjustedForwardSpread)

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

CDSOptionModel = finmodel("cdsblack",'SpreadVolatility',SpreadVolatility)

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

CDSOptionpricer = finpricer("analytic",'Model',CDSOptionModel,'DiscountCurve',ZeroCurve,'DefaultProbabilityCurve',DefaultProbCurve)

%% Price CDS Index Options
% Use price to compute the price for the CDS index options.

outPrice = price(CDSOptionpricer, [CDSCallOption;CDSPutOption]);
fprintf('    Payer: %.0f   Receiver: %.0f  \n',outPrice(1),outPrice(2));

% Copyright 2021 The MathWorks, Inc.