This example bootstraps a default-probability curve over a number of CDS and reprices them.
#include <ql/qldefines.hpp>
#ifdef 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>
using namespace std;
#if defined(QL_ENABLE_SESSIONS)
ThreadKey sessionId() { return 0; }
}
#endif
void example01() {
std::cout << std::endl;
Date todaysDate(15, May, 2007);
todaysDate = calendar.
adjust(todaysDate);
Settings::instance().evaluationDate() = todaysDate;
ext::shared_ptr<Quote> flatRate(
new SimpleQuote(0.01));
ext::make_shared<FlatForward>(
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);
vector<Date> maturities;
for (
Size i = 0; i < 4; i++) {
maturities.push_back(
}
std::vector<ext::shared_ptr<DefaultProbabilityHelper> > instruments;
for (
Size i = 0; i < 4; i++) {
instruments.push_back(ext::shared_ptr<DefaultProbabilityHelper>(
recovery_rate, tsCurve)));
}
ext::shared_ptr<PiecewiseDefaultCurve<HazardRate, BackwardFlat> >
vector<pair<Date, Real> > hr_curve_data = hazardRateStructure->nodes();
cout << "Calibrated hazard rate values: " << endl;
for (
Size i = 0; i < hr_curve_data.size(); i++) {
cout << "hazard rate on " << hr_curve_data[i].first << " is "
<< hr_curve_data[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;
ext::shared_ptr<PricingEngine> engine(
new MidPointCdsEngine(probability, recovery_rate, tsCurve));
.
withRule(DateGeneration::TwentiethIMM);
.from(todaysDate)
.to(maturities[1])
.withCalendar(calendar)
.withRule(DateGeneration::TwentiethIMM);
.from(todaysDate)
.to(maturities[2])
.withCalendar(calendar)
.withRule(DateGeneration::TwentiethIMM);
.from(todaysDate)
.to(maturities[3])
.withCalendar(calendar)
.withRule(DateGeneration::TwentiethIMM);
cds_3m.setPricingEngine(engine);
cds_6m.setPricingEngine(engine);
cds_1y.setPricingEngine(engine);
cds_2y.setPricingEngine(engine);
cout << "Repricing of quoted CDSs employed for calibration: " << endl;
cout <<
"3M fair spread: " <<
io::rate(cds_3m.fairSpread()) << endl
<< " NPV: " << cds_3m.NPV() << endl
<< " default leg: " << cds_3m.defaultLegNPV() << endl
<< " coupon leg: " << cds_3m.couponLegNPV() << endl << endl;
cout <<
"6M fair spread: " <<
io::rate(cds_6m.fairSpread()) << endl
<< " NPV: " << cds_6m.NPV() << endl
<< " default leg: " << cds_6m.defaultLegNPV() << endl
<< " coupon leg: " << cds_6m.couponLegNPV() << endl << endl;
cout <<
"1Y fair spread: " <<
io::rate(cds_1y.fairSpread()) << endl
<< " NPV: " << cds_1y.NPV() << endl
<< " default leg: " << cds_1y.defaultLegNPV() << endl
<< " coupon leg: " << cds_1y.couponLegNPV() << endl << endl;
cout <<
"2Y fair spread: " <<
io::rate(cds_2y.fairSpread()) << 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);
Settings::instance().evaluationDate() = todaysDate;
.withRule(DateGeneration::CDS);
std::copy(cdsSchedule.
begin(), cdsSchedule.
end(),
std::ostream_iterator<Date>(cout, "\n"));
Date evaluationDate =
Date(21, October, 2014);
Settings::instance().evaluationDate() = evaluationDate;
ext::shared_ptr<DepositRateHelper> dp1m =
ext::make_shared<DepositRateHelper>(0.000060, 1 * Months, 2,
ext::shared_ptr<DepositRateHelper> dp2m =
ext::make_shared<DepositRateHelper>(0.000450, 2 * Months, 2,
ext::shared_ptr<DepositRateHelper> dp3m =
ext::make_shared<DepositRateHelper>(0.000810, 3 * Months, 2,
ext::shared_ptr<DepositRateHelper> dp6m =
ext::make_shared<DepositRateHelper>(0.001840, 6 * Months, 2,
ext::shared_ptr<DepositRateHelper> dp9m =
ext::make_shared<DepositRateHelper>(0.002560, 9 * Months, 2,
ext::shared_ptr<DepositRateHelper> dp12m =
ext::make_shared<DepositRateHelper>(0.003370, 12 * Months, 2,
ext::shared_ptr<IborIndex> euribor6m =
ext::make_shared<Euribor>(
Euribor(6 * Months));
if (!IborCoupon::usingAtParCoupons()) {
std::cout << "Warning: IborCoupon::usingAtParCoupons() == false is used, "
<< "which is not precisely consistent with the specification "
<< "of the ISDA rate curve." << std::endl;
}
ext::shared_ptr<SwapRateHelper> sw2y = ext::make_shared<SwapRateHelper>(
euribor6m);
ext::shared_ptr<SwapRateHelper> sw3y = ext::make_shared<SwapRateHelper>(
euribor6m);
ext::shared_ptr<SwapRateHelper> sw4y = ext::make_shared<SwapRateHelper>(
euribor6m);
ext::shared_ptr<SwapRateHelper> sw5y = ext::make_shared<SwapRateHelper>(
euribor6m);
ext::shared_ptr<SwapRateHelper> sw6y = ext::make_shared<SwapRateHelper>(
euribor6m);
ext::shared_ptr<SwapRateHelper> sw7y = ext::make_shared<SwapRateHelper>(
euribor6m);
ext::shared_ptr<SwapRateHelper> sw8y = ext::make_shared<SwapRateHelper>(
euribor6m);
ext::shared_ptr<SwapRateHelper> sw9y = ext::make_shared<SwapRateHelper>(
euribor6m);
ext::shared_ptr<SwapRateHelper> sw10y = ext::make_shared<SwapRateHelper>(
euribor6m);
ext::shared_ptr<SwapRateHelper> sw12y = ext::make_shared<SwapRateHelper>(
euribor6m);
ext::shared_ptr<SwapRateHelper> sw15y = ext::make_shared<SwapRateHelper>(
euribor6m);
ext::shared_ptr<SwapRateHelper> sw20y = ext::make_shared<SwapRateHelper>(
euribor6m);
ext::shared_ptr<SwapRateHelper> 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(
Size i=0;i<isdaRateHelper.size(); i++) {
Date d = isdaRateHelper[i]->latestDate();
std::cout << d << "\t" << setprecision(6) <<
rateTs->discount(d) << std::endl;
}
ext::shared_ptr<DefaultProbabilityTermStructure> defaultTs0 =
ext::shared_ptr<CreditDefaultSwap> trade =
ext::shared_ptr<CreditDefaultSwap>(
Date(22,October,2014), ext::shared_ptr<Claim>(),
ext::shared_ptr<FixedRateCoupon> 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;
ext::shared_ptr<IsdaCdsEngine> 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;
isdaCdsHelper.push_back(cds5y);
std::cout << "ISDA credit curve: " << std::endl;
for(
Size i=0;i<isdaCdsHelper.size();i++) {
Date d = isdaCdsHelper[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);
Settings::instance().evaluationDate() = tradeDate;
ext::shared_ptr<DepositRateHelper> dp1m =
ext::make_shared<DepositRateHelper>(0.00445, 1 * Months, 2,
ext::shared_ptr<DepositRateHelper> dp2m =
ext::make_shared<DepositRateHelper>(0.00949, 2 * Months, 2,
ext::shared_ptr<DepositRateHelper> dp3m =
ext::make_shared<DepositRateHelper>(0.01234, 3 * Months, 2,
ext::shared_ptr<DepositRateHelper> dp6m =
ext::make_shared<DepositRateHelper>(0.01776, 6 * Months, 2,
ext::shared_ptr<DepositRateHelper> dp9m =
ext::make_shared<DepositRateHelper>(0.01935, 9 * Months, 2,
ext::shared_ptr<DepositRateHelper> dp1y =
ext::make_shared<DepositRateHelper>(0.02084, 12 * Months, 2,
ext::shared_ptr<IborIndex> euribor6m = ext::make_shared<IborIndex>(
ext::shared_ptr<SwapRateHelper> sw2y = ext::make_shared<SwapRateHelper>(
euribor6m);
ext::shared_ptr<SwapRateHelper> sw3y = ext::make_shared<SwapRateHelper>(
euribor6m);
ext::shared_ptr<SwapRateHelper> sw4y = ext::make_shared<SwapRateHelper>(
euribor6m);
ext::shared_ptr<SwapRateHelper> sw5y = ext::make_shared<SwapRateHelper>(
euribor6m);
ext::shared_ptr<SwapRateHelper> sw6y = ext::make_shared<SwapRateHelper>(
euribor6m);
ext::shared_ptr<SwapRateHelper> sw7y = ext::make_shared<SwapRateHelper>(
euribor6m);
ext::shared_ptr<SwapRateHelper> sw8y = ext::make_shared<SwapRateHelper>(
euribor6m);
ext::shared_ptr<SwapRateHelper> sw9y = ext::make_shared<SwapRateHelper>(
euribor6m);
ext::shared_ptr<SwapRateHelper> sw10y = ext::make_shared<SwapRateHelper>(
euribor6m);
ext::shared_ptr<SwapRateHelper> sw11y = ext::make_shared<SwapRateHelper>(
euribor6m);
ext::shared_ptr<SwapRateHelper> sw12y = ext::make_shared<SwapRateHelper>(
euribor6m);
ext::shared_ptr<SwapRateHelper> sw15y = ext::make_shared<SwapRateHelper>(
euribor6m);
ext::shared_ptr<SwapRateHelper> sw20y = ext::make_shared<SwapRateHelper>(
euribor6m);
ext::shared_ptr<SwapRateHelper> sw25y = ext::make_shared<SwapRateHelper>(
euribor6m);
ext::shared_ptr<SwapRateHelper> 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;
DateGeneration::CDS,
Actual360(), 0.4, isdaYts,
true,
true,
Date(),
DateGeneration::CDS,
Actual360(), 0.4, isdaYts,
true,
true,
Date(),
DateGeneration::CDS,
Actual360(), 0.4, isdaYts,
true,
true,
Date(),
DateGeneration::CDS,
Actual360(), 0.4, isdaYts,
true,
true,
Date(),
DateGeneration::CDS,
Actual360(), 0.4, isdaYts,
true,
true,
Date(),
DateGeneration::CDS,
Actual360(), 0.4, isdaYts,
true,
true,
Date(),
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);
ext::shared_ptr<IsdaCdsEngine> isdaPricer =
ext::make_shared<IsdaCdsEngine>(
isdaCts, 0.4, isdaYts);
std::cout << "ISDA yield curve:" << std::endl;
std::cout << "date;time;zeroyield" << std::endl;
for (
Size i = 0; i < isdaYieldHelpers.size(); i++) {
Date d = isdaYieldHelpers[i]->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 (
Size i = 0; i < isdaCdsHelpers.size(); i++) {
Date d = isdaCdsHelpers[i]->latestDate();
Real t = isdaCts->timeFromReference(d);
std::cout << d << ";" << t << ";" << isdaCts->survivalProbability(d)
<< std::endl;
}
}
int main(int argc, char *argv[]) {
try {
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;
}
}