This example bootstraps a default-probability curve over a number of CDS and reprices them.
#include <ql/qldefines.hpp>
#if !defined(BOOST_ALL_NO_LIB) && defined(BOOST_MSVC)
# include <ql/auto_link.hpp>
#endif
#include <ql/cashflows/fixedratecoupon.hpp>
#include <ql/cashflows/iborcoupon.hpp>
#include <ql/instruments/creditdefaultswap.hpp>
#include <ql/indexes/ibor/euribor.hpp>
#include <ql/pricingengines/credit/midpointcdsengine.hpp>
#include <ql/pricingengines/credit/isdacdsengine.hpp>
#include <ql/termstructures/credit/piecewisedefaultcurve.hpp>
#include <ql/termstructures/credit/defaultprobabilityhelpers.hpp>
#include <ql/termstructures/credit/flathazardrate.hpp>
#include <ql/termstructures/yield/flatforward.hpp>
#include <ql/termstructures/yield/piecewiseyieldcurve.hpp>
#include <ql/termstructures/yield/ratehelpers.hpp>
#include <ql/math/interpolations/backwardflatinterpolation.hpp>
#include <ql/time/calendars/target.hpp>
#include <ql/time/calendars/weekendsonly.hpp>
#include <ql/time/daycounters/thirty360.hpp>
#include <ql/time/daycounters/actual365fixed.hpp>
#include <ql/time/daycounters/actual360.hpp>
#include <ql/currencies/europe.hpp>
#include <ql/quotes/simplequote.hpp>
#include <iostream>
#include <iomanip>
void example01() {
std::cout << std::endl;
Date todaysDate(15, May, 2007);
todaysDate = calendar.
adjust(todaysDate);
auto flatRate = ext::make_shared<SimpleQuote>(0.01);
ext::make_shared<FlatForward>(
Natural settlementDays = 1;
Real recovery_rate = 0.5;
Real quoted_spreads[] = { 0.0150, 0.0150, 0.0150, 0.0150 };
vector<Period> tenors;
tenors.push_back(3 * Months);
tenors.push_back(6 * Months);
tenors.push_back(1 * Years);
tenors.push_back(2 * Years);
Date settlementDate = calendar.
advance(todaysDate, settlementDays, Days);
vector<Date> maturities;
maturities.reserve(4);
for (Size i = 0; i < 4; i++) {
maturities.push_back(
}
std::vector<ext::shared_ptr<DefaultProbabilityHelper>> instruments;
instruments.reserve(4);
for (Size i = 0; i < 4; i++) {
instruments.push_back(ext::make_shared<SpreadCdsHelper>(
makeQuoteHandle(quoted_spreads[i]),
recovery_rate, tsCurve));
}
auto hazardRateStructure = ext::make_shared<PiecewiseDefaultCurve<HazardRate, BackwardFlat>>(
vector<pair<Date, Real>> hr_curve_data = hazardRateStructure->nodes();
cout << "Calibrated hazard rate values: " << endl;
for (auto& i : hr_curve_data) {
cout << "hazard rate on " << i.first << " is " << i.second << endl;
}
cout << endl;
cout << "Some survival probability values: " << endl;
cout << "1Y survival probability: "
<<
io::percent(hazardRateStructure->survivalProbability(todaysDate +
1 * Years))
<< endl <<
" expected: " <<
io::percent(0.9704) << endl;
cout << "2Y survival probability: "
<<
io::percent(hazardRateStructure->survivalProbability(todaysDate +
2 * Years))
<< endl <<
" expected: " <<
io::percent(0.9418) << endl;
cout << endl << endl;
Real nominal = 1000000.0;
auto engine = ext::make_shared<MidPointCdsEngine>(probability, recovery_rate, tsCurve);
.from(settlementDate)
.to(maturities[1])
.withCalendar(calendar)
.from(settlementDate)
.to(maturities[2])
.withCalendar(calendar)
.from(settlementDate)
.to(maturities[3])
.withCalendar(calendar)
cout << "Repricing of quoted CDSs employed for calibration: " << endl;
<<
" NPV: " << cds_3m.
NPV() << endl
<<
" default leg: " << cds_3m.
defaultLegNPV() << endl
<<
" coupon leg: " << cds_3m.
couponLegNPV() << endl << endl;
<<
" NPV: " << cds_6m.
NPV() << endl
<< " default leg: " << cds_6m.defaultLegNPV() << endl
<< " coupon leg: " << cds_6m.couponLegNPV() << endl << endl;
<<
" NPV: " << cds_1y.
NPV() << endl
<< " default leg: " << cds_1y.defaultLegNPV() << endl
<< " coupon leg: " << cds_1y.couponLegNPV() << endl << endl;
<<
" NPV: " << cds_2y.
NPV() << endl
<< " default leg: " << cds_2y.defaultLegNPV() << endl
<< " coupon leg: " << cds_2y.couponLegNPV() << endl << endl;
cout << endl << endl;
}
void example02() {
Date todaysDate(25, September, 2014);
std::copy(cdsSchedule.
begin(), cdsSchedule.
end(),
std::ostream_iterator<Date>(cout, "\n"));
Date evaluationDate =
Date(21, October, 2014);
auto dp1m = ext::make_shared<DepositRateHelper>(0.000060, 1 * Months, 2,
auto dp2m = ext::make_shared<DepositRateHelper>(0.000450, 2 * Months, 2,
auto dp3m = ext::make_shared<DepositRateHelper>(0.000810, 3 * Months, 2,
auto dp6m = ext::make_shared<DepositRateHelper>(0.001840, 6 * Months, 2,
auto dp9m = ext::make_shared<DepositRateHelper>(0.002560, 9 * Months, 2,
auto dp12m = ext::make_shared<DepositRateHelper>(0.003370, 12 * Months, 2,
auto euribor6m = ext::make_shared<Euribor>(
Euribor(6 * Months));
auto sw2y = ext::make_shared<SwapRateHelper>(
euribor6m);
auto sw3y = ext::make_shared<SwapRateHelper>(
euribor6m);
auto sw4y = ext::make_shared<SwapRateHelper>(
euribor6m);
auto sw5y = ext::make_shared<SwapRateHelper>(
euribor6m);
auto sw6y = ext::make_shared<SwapRateHelper>(
euribor6m);
auto sw7y = ext::make_shared<SwapRateHelper>(
euribor6m);
auto sw8y = ext::make_shared<SwapRateHelper>(
euribor6m);
auto sw9y = ext::make_shared<SwapRateHelper>(
euribor6m);
auto sw10y = ext::make_shared<SwapRateHelper>(
euribor6m);
auto sw12y = ext::make_shared<SwapRateHelper>(
euribor6m);
auto sw15y = ext::make_shared<SwapRateHelper>(
euribor6m);
auto sw20y = ext::make_shared<SwapRateHelper>(
euribor6m);
auto sw30y = ext::make_shared<SwapRateHelper>(
euribor6m);
std::vector<ext::shared_ptr<RateHelper>> isdaRateHelper;
isdaRateHelper.push_back(dp1m);
isdaRateHelper.push_back(dp2m);
isdaRateHelper.push_back(dp3m);
isdaRateHelper.push_back(dp6m);
isdaRateHelper.push_back(dp9m);
isdaRateHelper.push_back(dp12m);
isdaRateHelper.push_back(sw2y);
isdaRateHelper.push_back(sw3y);
isdaRateHelper.push_back(sw4y);
isdaRateHelper.push_back(sw5y);
isdaRateHelper.push_back(sw6y);
isdaRateHelper.push_back(sw7y);
isdaRateHelper.push_back(sw8y);
isdaRateHelper.push_back(sw9y);
isdaRateHelper.push_back(sw10y);
isdaRateHelper.push_back(sw12y);
isdaRateHelper.push_back(sw15y);
isdaRateHelper.push_back(sw20y);
isdaRateHelper.push_back(sw30y);
rateTs->enableExtrapolation();
std::cout << "ISDA rate curve: " << std::endl;
for (auto& i : isdaRateHelper) {
Date d = i->latestDate();
std::cout << d << "\t" << setprecision(6) <<
rateTs->discount(d) << std::endl;
}
auto trade = ext::make_shared<CreditDefaultSwap>(
Protection::Buyer, 100000000.0, 0.01, sched,
Date(22,October,2014), ext::shared_ptr<Claim>(),
auto cp = ext::dynamic_pointer_cast<FixedRateCoupon>(trade->coupons()[0]);
std::cout << "first period = " << cp->accrualStartDate() << " to " << cp->accrualEndDate() <<
" accrued amount = " << cp->accruedAmount(
Date(24,October,2014)) << std::endl;
auto engine = ext::make_shared<IsdaCdsEngine>(
false, IsdaCdsEngine::Taylor, IsdaCdsEngine::NoBias, IsdaCdsEngine::Piecewise);
trade->setPricingEngine(engine);
std::cout << "reference trade NPV = " << trade->NPV() << std::endl;
std::vector<ext::shared_ptr<DefaultProbabilityHelper>> isdaCdsHelper;
auto cds5y = ext::make_shared<SpreadCdsHelper>(
isdaCdsHelper.push_back(cds5y);
std::cout << "ISDA credit curve: " << std::endl;
for (auto& i : isdaCdsHelper) {
Date d = i->latestDate();
Real pd = defaultTs->defaultProbability(d);
Real t = defaultTs->timeFromReference(d);
std::cout << d << ";" << pd << ";" << 1.0 - pd << ";" <<
-std::log(1.0-pd)/t << std::endl;
}
}
void example03() {
Date tradeDate(13,June,2011);
auto dp1m = ext::make_shared<DepositRateHelper>(0.00445, 1 * Months, 2,
false, actual360);
auto dp2m = ext::make_shared<DepositRateHelper>(0.00949, 2 * Months, 2,
false, actual360);
auto dp3m = ext::make_shared<DepositRateHelper>(0.01234, 3 * Months, 2,
false, actual360);
auto dp6m = ext::make_shared<DepositRateHelper>(0.01776, 6 * Months, 2,
false, actual360);
auto dp9m = ext::make_shared<DepositRateHelper>(0.01935, 9 * Months, 2,
false, actual360);
auto dp1y = ext::make_shared<DepositRateHelper>(0.02084, 12 * Months, 2,
false, actual360);
auto euribor6m = ext::make_shared<IborIndex>(
auto sw2y = ext::make_shared<SwapRateHelper>(
euribor6m);
auto sw3y = ext::make_shared<SwapRateHelper>(
euribor6m);
auto sw4y = ext::make_shared<SwapRateHelper>(
euribor6m);
auto sw5y = ext::make_shared<SwapRateHelper>(
euribor6m);
auto sw6y = ext::make_shared<SwapRateHelper>(
euribor6m);
auto sw7y = ext::make_shared<SwapRateHelper>(
euribor6m);
auto sw8y = ext::make_shared<SwapRateHelper>(
euribor6m);
auto sw9y = ext::make_shared<SwapRateHelper>(
euribor6m);
auto sw10y = ext::make_shared<SwapRateHelper>(
euribor6m);
auto sw11y = ext::make_shared<SwapRateHelper>(
euribor6m);
auto sw12y = ext::make_shared<SwapRateHelper>(
euribor6m);
auto sw15y = ext::make_shared<SwapRateHelper>(
euribor6m);
auto sw20y = ext::make_shared<SwapRateHelper>(
euribor6m);
auto sw25y = ext::make_shared<SwapRateHelper>(
euribor6m);
auto sw30y = ext::make_shared<SwapRateHelper>(
euribor6m);
std::vector<ext::shared_ptr<RateHelper>> isdaYieldHelpers;
isdaYieldHelpers.push_back(dp1m);
isdaYieldHelpers.push_back(dp2m);
isdaYieldHelpers.push_back(dp3m);
isdaYieldHelpers.push_back(dp6m);
isdaYieldHelpers.push_back(dp9m);
isdaYieldHelpers.push_back(dp1y);
isdaYieldHelpers.push_back(sw2y);
isdaYieldHelpers.push_back(sw3y);
isdaYieldHelpers.push_back(sw4y);
isdaYieldHelpers.push_back(sw5y);
isdaYieldHelpers.push_back(sw6y);
isdaYieldHelpers.push_back(sw7y);
isdaYieldHelpers.push_back(sw8y);
isdaYieldHelpers.push_back(sw9y);
isdaYieldHelpers.push_back(sw10y);
isdaYieldHelpers.push_back(sw11y);
isdaYieldHelpers.push_back(sw12y);
isdaYieldHelpers.push_back(sw15y);
isdaYieldHelpers.push_back(sw20y);
isdaYieldHelpers.push_back(sw25y);
isdaYieldHelpers.push_back(sw30y);
isdaYts->enableExtrapolation();
CreditDefaultSwap::PricingModel model = CreditDefaultSwap::ISDA;
auto cds6m = ext::make_shared<SpreadCdsHelper>(
auto cds1y = ext::make_shared<SpreadCdsHelper>(
auto cds3y = ext::make_shared<SpreadCdsHelper>(
auto cds5y = ext::make_shared<SpreadCdsHelper>(
auto cds7y = ext::make_shared<SpreadCdsHelper>(
auto cds10y = ext::make_shared<SpreadCdsHelper>(
std::vector<ext::shared_ptr<DefaultProbabilityHelper>> isdaCdsHelpers;
isdaCdsHelpers.push_back(cds6m);
isdaCdsHelpers.push_back(cds1y);
isdaCdsHelpers.push_back(cds3y);
isdaCdsHelpers.push_back(cds5y);
isdaCdsHelpers.push_back(cds7y);
isdaCdsHelpers.push_back(cds10y);
auto isdaCts =
auto isdaPricer = ext::make_shared<IsdaCdsEngine>(isdaCts, 0.4, isdaYts);
std::cout << "ISDA yield curve:" << std::endl;
std::cout << "date;time;zeroyield" << std::endl;
for (auto& isdaYieldHelper : isdaYieldHelpers) {
Date d = isdaYieldHelper->latestDate();
Real t = isdaYts->timeFromReference(d);
std::cout << d << ";" << t << ";"
<< std::endl;
}
std::cout << "ISDA credit curve:" << std::endl;
std::cout << "date;time;survivalprob" << std::endl;
for (auto& isdaCdsHelper : isdaCdsHelpers) {
Date d = isdaCdsHelper->latestDate();
Real t = isdaCts->timeFromReference(d);
std::cout << d << ";" << t << ";" << isdaCts->survivalProbability(d)
<< std::endl;
}
}
int main(int argc, char *argv[]) {
try {
Size example = 0;
if (argc == 2)
example = std::atoi(argv[1]);
if (example == 0 || example == 1) {
std::cout << "***** Running example #1 *****" << std::endl;
example01();
}
if (example == 0 || example == 2) {
std::cout << "***** Running example #2 *****" << std::endl;
example02();
}
if (example == 0 || example == 3) {
std::cout << "***** Running example #3 *****" << std::endl;
example03();
}
return 0;
}
catch (exception &e) {
cerr << e.what() << endl;
return 1;
}
catch (...) {
cerr << "unknown error" << endl;
return 1;
}
}