%% Bootstrapping a Default Probability Curve from Credit Default Swaps
% This example shows how to bootstrap a default probability curve for CDS instruments.
%% Create a ratecurve Object for a Zero Curve
% Create a ratecurve object using ratecurve.

Settle = datetime(2017,9,15);
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;
ZeroCurve = ratecurve("zero",Settle,ZeroDates,ZeroRates)

%
%% Market CDS Spreads and a Vector of Market CDS Instruments
% Define the market CDS spreads and use fininstrument to create a vector of market CDS instrument objects.

SpreadTimes = [1 2 3 4 5 7 10 20 30]';
Spread = [140 175 210 265 310 360 410 460 490]';
MarketDates = datemnth(Settle,12*SpreadTimes);

NumMarketInst = length(MarketDates);
ContractSpreadBP = 50.*ones(NumMarketInst,1);

MarketCDSInstruments(NumMarketInst,1) = fininstrument("cds", ...
    'ContractSpread', ContractSpreadBP(end), 'Maturity', MarketDates(end));
for k = 1:NumMarketInst
    MarketCDSInstruments(k,1) = fininstrument("cds", ...
        'ContractSpread', ContractSpreadBP(k), 'Maturity', MarketDates(k));
end
MarketCDSInstruments

%% Bootstrap a Default Probability Curve
% Use defprobstrip, hazardrates,and survprobs to analyse a default probability curve for the market CDS instruments.

DefaultProbCurve = defprobstrip(ZeroCurve, MarketCDSInstruments, Spread)
HazardRates = hazardrates(DefaultProbCurve)
SurvivalProbabilities = survprobs(DefaultProbCurve, MarketDates)

% Copyright 2019 The MathWorks, Inc.