%% Price CDS Option Instrument Using CDS Black Model and CDS Black Pricer
% This example shows the workflow to price a CDSOption instrument for a single-name CDS option when you use a CDSBlack model and a CDSBlack pricing method.
%% Create CDS Instrument Object
% Use fininstrument to create the underlying CDS instrument object.

CDSOpt = fininstrument("CDS",'Maturity',datetime(2021,9,15),'ContractSpread',150,'Notional',100,'Name',"CDS_option")

%% Create defprobcurve Object
% Create a defprobcurve object using defprobcurve.

Settle = datetime(2020,9,20);
DefProbTimes = [calmonths(6) calyears([1 2 3 4 5 7 10 20 30])];
DefaultProbabilities = [0.005 0.007 0.01 0.015 0.026 0.04 0.077 0.093 0.15 0.20]';
ProbDates = Settle + DefProbTimes;
DefaultProbCurve = defprobcurve(Settle,ProbDates,DefaultProbabilities,'Basis',5)

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

Settle = datetime(2020,9,15);
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 CDSOption Instrument Object
% Use fininstrument to create a CDSOption instrument object for a single-name CDS option.

CDSOptionInst = fininstrument("CDSOption",'ExerciseDate',datetime(2021,8,15),'Strike',20,'CDS',CDSOpt,'OptionType',"put",'Knockout',true,'Name',"CDSOption_option")

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

CDSBlackModel = finmodel("CDSBlack",'SpreadVolatility',.2)

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

outPricer = finpricer("analytic",'Model',CDSBlackModel,'DefaultProbabilityCurve',DefaultProbCurve,'DiscountCurve',myRC)

%% Price CDSOption Instrument
% Use price to compute the price for the CDSOption instrument for a single-name CDS option.

Price = price(outPricer,CDSOptionInst)

% Copyright 2019 The MathWorks, Inc.