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;
Calendar calendar = TARGET();
Date todaysDate(15, May, 2007);
todaysDate = calendar.adjust(todaysDate);
Settings::instance().evaluationDate() = todaysDate;
ext::shared_ptr<Quote> flatRate(new SimpleQuote(0.01));
Handle<YieldTermStructure> tsCurve(
ext::make_shared<FlatForward>(
todaysDate, Handle<Quote>(flatRate), Actual365Fixed()));
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(
calendar.adjust(todaysDate + tenors[i],
Following));
}
std::vector<ext::shared_ptr<DefaultProbabilityHelper> > instruments;
for (
Size i = 0; i < 4; i++) {
instruments.push_back(ext::shared_ptr<DefaultProbabilityHelper>(
new SpreadCdsHelper(Handle<Quote>(ext::shared_ptr<Quote>(
new SimpleQuote(quoted_spreads[i]))),
DateGeneration::TwentiethIMM, Actual365Fixed(),
recovery_rate, tsCurve)));
}
ext::shared_ptr<PiecewiseDefaultCurve<HazardRate, BackwardFlat> >
hazardRateStructure(new PiecewiseDefaultCurve<HazardRate, BackwardFlat>(
todaysDate, instruments, Actual365Fixed()));
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;
Handle<DefaultProbabilityTermStructure> probability(hazardRateStructure);
ext::shared_ptr<PricingEngine> engine(
new MidPointCdsEngine(probability, recovery_rate, tsCurve));
Schedule cdsSchedule = MakeSchedule()
.from(todaysDate)
.to(maturities[0])
.withCalendar(calendar)
.withRule(DateGeneration::TwentiethIMM);
CreditDefaultSwap cds_3m(Protection::Seller, nominal, quoted_spreads[0],
cdsSchedule = MakeSchedule()
.from(todaysDate)
.to(maturities[1])
.withCalendar(calendar)
.withRule(DateGeneration::TwentiethIMM);
CreditDefaultSwap cds_6m(Protection::Seller, nominal, quoted_spreads[1],
cdsSchedule = MakeSchedule()
.from(todaysDate)
.to(maturities[2])
.withCalendar(calendar)
.withRule(DateGeneration::TwentiethIMM);
CreditDefaultSwap cds_1y(Protection::Seller, nominal, quoted_spreads[2],
cdsSchedule = MakeSchedule()
.from(todaysDate)
.to(maturities[3])
.withCalendar(calendar)
.withRule(DateGeneration::TwentiethIMM);
CreditDefaultSwap cds_2y(Protection::Seller, nominal, quoted_spreads[3],
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;
Date termDate = TARGET().adjust(todaysDate+Period(2*Years),
Following);
Schedule cdsSchedule =
MakeSchedule().from(todaysDate).to(termDate)
.withCalendar(WeekendsOnly())
.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,
false, Actual360());
ext::shared_ptr<DepositRateHelper> dp2m =
ext::make_shared<DepositRateHelper>(0.000450, 2 * Months, 2,
false, Actual360());
ext::shared_ptr<DepositRateHelper> dp3m =
ext::make_shared<DepositRateHelper>(0.000810, 3 * Months, 2,
false, Actual360());
ext::shared_ptr<DepositRateHelper> dp6m =
ext::make_shared<DepositRateHelper>(0.001840, 6 * Months, 2,
false, Actual360());
ext::shared_ptr<DepositRateHelper> dp9m =
ext::make_shared<DepositRateHelper>(0.002560, 9 * Months, 2,
false, Actual360());
ext::shared_ptr<DepositRateHelper> dp12m =
ext::make_shared<DepositRateHelper>(0.003370, 12 * Months, 2,
false, Actual360());
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);
Handle<YieldTermStructure> rateTs(
ext::make_shared<PiecewiseYieldCurve<Discount, LogLinear> >(
0, WeekendsOnly(), isdaRateHelper, Actual365Fixed()));
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->zeroRate(d,Actual365Fixed(),Continuous).rate() << "\t" <<
rateTs->discount(d) << std::endl;
}
ext::shared_ptr<DefaultProbabilityTermStructure> defaultTs0 =
ext::make_shared<FlatHazardRate>(0, WeekendsOnly(), 0.016739207493630,Actual365Fixed());
Schedule sched( Date(22,September,2014), Date(20,December,2019), 3*Months,
ext::shared_ptr<CreditDefaultSwap> trade =
ext::shared_ptr<CreditDefaultSwap>(
new CreditDefaultSwap(Protection::Buyer, 100000000.0, 0.01, sched,
Date(22,October,2014), ext::shared_ptr<Claim>(),
Actual360(true), true));
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>(
Handle<DefaultProbabilityTermStructure>(defaultTs0), 0.4, rateTs,
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;
ext::shared_ptr<CdsHelper> cds5y(new SpreadCdsHelper(
0.00672658551, 4 * Years + 6 * Months, 1, WeekendsOnly(),
Quarterly,
Following, DateGeneration::CDS, Actual360(), 0.4, rateTs,
true,
true,
Date(), Actual360(true), true, CreditDefaultSwap::ISDA));
isdaCdsHelper.push_back(cds5y);
Handle<DefaultProbabilityTermStructure> defaultTs(ext::make_shared<
PiecewiseDefaultCurve<SurvivalProbability, LogLinear> >(
0, WeekendsOnly(), isdaCdsHelper, Actual365Fixed()));
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,
false, Actual360());
ext::shared_ptr<DepositRateHelper> dp2m =
ext::make_shared<DepositRateHelper>(0.00949, 2 * Months, 2,
false, Actual360());
ext::shared_ptr<DepositRateHelper> dp3m =
ext::make_shared<DepositRateHelper>(0.01234, 3 * Months, 2,
false, Actual360());
ext::shared_ptr<DepositRateHelper> dp6m =
ext::make_shared<DepositRateHelper>(0.01776, 6 * Months, 2,
false, Actual360());
ext::shared_ptr<DepositRateHelper> dp9m =
ext::make_shared<DepositRateHelper>(0.01935, 9 * Months, 2,
false, Actual360());
ext::shared_ptr<DepositRateHelper> dp1y =
ext::make_shared<DepositRateHelper>(0.02084, 12 * Months, 2,
false, Actual360());
ext::shared_ptr<IborIndex> euribor6m = ext::make_shared<IborIndex>(
"IsdaIbor", 6 * Months, 2, EURCurrency(), WeekendsOnly(),
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);
Handle<YieldTermStructure> isdaYts = Handle<YieldTermStructure>(
ext::make_shared<PiecewiseYieldCurve<Discount, LogLinear> >(
0, WeekendsOnly(), isdaYieldHelpers, Actual365Fixed()));
isdaYts->enableExtrapolation();
CreditDefaultSwap::PricingModel model = CreditDefaultSwap::ISDA;
ext::shared_ptr<CdsHelper> cds6m(new SpreadCdsHelper(
DateGeneration::CDS, Actual360(), 0.4, isdaYts, true, true, Date(),
Actual360(true), true, model));
ext::shared_ptr<CdsHelper> cds1y(new SpreadCdsHelper(
DateGeneration::CDS, Actual360(), 0.4, isdaYts, true, true, Date(),
Actual360(true), true, model));
ext::shared_ptr<CdsHelper> cds3y(new SpreadCdsHelper(
DateGeneration::CDS, Actual360(), 0.4, isdaYts, true, true, Date(),
Actual360(true), true, model));
ext::shared_ptr<CdsHelper> cds5y(new SpreadCdsHelper(
DateGeneration::CDS, Actual360(), 0.4, isdaYts, true, true, Date(),
Actual360(true), true, model));
ext::shared_ptr<CdsHelper> cds7y(new SpreadCdsHelper(
DateGeneration::CDS, Actual360(), 0.4, isdaYts, true, true, Date(),
Actual360(true), true, model));
ext::shared_ptr<CdsHelper> cds10y(new SpreadCdsHelper(
DateGeneration::CDS, Actual360(), 0.4, isdaYts, true, true, Date(),
Actual360(true), true, model));
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);
Handle<DefaultProbabilityTermStructure> isdaCts =
Handle<DefaultProbabilityTermStructure>(ext::make_shared<
PiecewiseDefaultCurve<SurvivalProbability, LogLinear> >(
0, WeekendsOnly(), isdaCdsHelpers, Actual365Fixed()));
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 << ";"
<< isdaYts->zeroRate(d, Actual365Fixed(), Continuous).rate()
<< 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;
}
}