%% Price CDS Instrument Using Default Probability Curve and Credit Pricer
% This example shows the workflow to price a CDS instrument when you use a defprobcurve model and a Credit pricing method.
%% Create CDS Instrument Object
% Use fininstrument to create a CDS instrument object.

CDS = fininstrument("CDS",'Maturity',datetime(2021,9,15),'ContractSpread',15,'Notional',20000,'Period',4,'Basis',3,'BusinessDayConvention',"follow",'Name',"CDS_instrument")

%% 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 Credit Pricer Object
% Use finpricer to create a Credit pricer object and use the ratecurve object for the 'DiscountCurve' name-value pair argument.

outPricer = finpricer("credit",'DefaultProbabilityCurve',DefaultProbCurve,'DiscountCurve',myRC)

%% Price CDS Instrument
% Use price to compute the price for the CDS instrument.

Price = price(outPricer,CDS)

% Copyright 2019 The MathWorks, Inc.