This example bootstraps a default-probability curve over a number of CDS and reprices them.
#if !defined(BOOST_ALL_NO_LIB) && defined(BOOST_MSVC)
#endif
#include <iostream>
#include <iomanip>
void example01() {
std::cout << std::endl;
Date todaysDate(15, May, 2007);
todaysDate = calendar.
adjust(todaysDate);
Settings::instance().evaluationDate() = todaysDate;
auto flatRate = ext::make_shared<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);
Date settlementDate = calendar.
advance(todaysDate, settlementDays, Days);
vector<Date> maturities;
for (
Size i = 0; i < 4; i++) {
maturities.push_back(
calendar.
adjust(settlementDate + tenors[i], Following));
}
std::vector<ext::shared_ptr<DefaultProbabilityHelper>> instruments;
for (
Size i = 0; i < 4; i++) {
instruments.push_back(ext::make_shared<SpreadCdsHelper>(
makeQuoteHandle(quoted_spreads[i]),
tenors[i], settlementDays, calendar, Quarterly, Following,
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);
.
withRule(DateGeneration::TwentiethIMM);
.
withRule(DateGeneration::TwentiethIMM);
.
withRule(DateGeneration::TwentiethIMM);
.
withRule(DateGeneration::TwentiethIMM);
cout << "Repricing of quoted CDSs employed for calibration: " << endl;
cout <<
"3M fair spread: " << io::rate(cds_3m.
fairSpread()) << endl
<<
" NPV: " << cds_3m.
NPV() << endl
<<
" coupon leg: " << cds_3m.
couponLegNPV() << endl << endl;
cout <<
"6M fair spread: " << io::rate(cds_6m.
fairSpread()) << endl
<<
" NPV: " << cds_6m.
NPV() << endl
<<
" coupon leg: " << cds_6m.
couponLegNPV() << endl << endl;
cout <<
"1Y fair spread: " << io::rate(cds_1y.
fairSpread()) << endl
<<
" NPV: " << cds_1y.
NPV() << endl
<<
" coupon leg: " << cds_1y.
couponLegNPV() << endl << endl;
cout <<
"2Y fair spread: " << io::rate(cds_2y.
fairSpread()) << endl
<<
" NPV: " << cds_2y.
NPV() << endl
<<
" coupon leg: " << cds_2y.
couponLegNPV() << endl << endl;
cout << endl << endl;
}
void example02() {
Date todaysDate(25, September, 2014);
Settings::instance().evaluationDate() = todaysDate;
std::copy(cdsSchedule.
begin(), cdsSchedule.
end(),
std::ostream_iterator<Date>(cout, "\n"));
Date evaluationDate =
Date(21, October, 2014);
Settings::instance().evaluationDate() = evaluationDate;
IborCoupon::Settings::instance().createAtParCoupons();
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>(
0.002230, 2 * Years,
TARGET(), Annual, ModifiedFollowing, thirty360,
euribor6m);
auto sw3y = ext::make_shared<SwapRateHelper>(
0.002760, 3 * Years,
TARGET(), Annual, ModifiedFollowing, thirty360,
euribor6m);
auto sw4y = ext::make_shared<SwapRateHelper>(
0.003530, 4 * Years,
TARGET(), Annual, ModifiedFollowing, thirty360,
euribor6m);
auto sw5y = ext::make_shared<SwapRateHelper>(
0.004520, 5 * Years,
TARGET(), Annual, ModifiedFollowing, thirty360,
euribor6m);
auto sw6y = ext::make_shared<SwapRateHelper>(
0.005720, 6 * Years,
TARGET(), Annual, ModifiedFollowing, thirty360,
euribor6m);
auto sw7y = ext::make_shared<SwapRateHelper>(
0.007050, 7 * Years,
TARGET(), Annual, ModifiedFollowing, thirty360,
euribor6m);
auto sw8y = ext::make_shared<SwapRateHelper>(
0.008420, 8 * Years,
TARGET(), Annual, ModifiedFollowing, thirty360,
euribor6m);
auto sw9y = ext::make_shared<SwapRateHelper>(
0.009720, 9 * Years,
TARGET(), Annual, ModifiedFollowing, thirty360,
euribor6m);
auto sw10y = ext::make_shared<SwapRateHelper>(
0.010900, 10 * Years,
TARGET(), Annual, ModifiedFollowing, thirty360,
euribor6m);
auto sw12y = ext::make_shared<SwapRateHelper>(
0.012870, 12 * Years,
TARGET(), Annual, ModifiedFollowing, thirty360,
euribor6m);
auto sw15y = ext::make_shared<SwapRateHelper>(
0.014970, 15 * Years,
TARGET(), Annual, ModifiedFollowing, thirty360,
euribor6m);
auto sw20y = ext::make_shared<SwapRateHelper>(
0.017000, 20 * Years,
TARGET(), Annual, ModifiedFollowing, thirty360,
euribor6m);
auto sw30y = ext::make_shared<SwapRateHelper>(
0.018210, 30 * Years,
TARGET(), Annual, ModifiedFollowing, thirty360,
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>(
0.00672658551, 4 * Years + 6 * Months, 1,
WeekendsOnly(), Quarterly,
Following, DateGeneration::CDS,
Actual360(), 0.4, rateTs,
true,
true,
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);
Settings::instance().evaluationDate() = tradeDate;
IborCoupon::Settings::instance().createAtParCoupons();
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>(
ModifiedFollowing, false, actual360);
auto sw2y = ext::make_shared<SwapRateHelper>(
0.01652, 2 * Years,
WeekendsOnly(), Annual, ModifiedFollowing, thirty360,
euribor6m);
auto sw3y = ext::make_shared<SwapRateHelper>(
0.02018, 3 * Years,
WeekendsOnly(), Annual, ModifiedFollowing, thirty360,
euribor6m);
auto sw4y = ext::make_shared<SwapRateHelper>(
0.02303, 4 * Years,
WeekendsOnly(), Annual, ModifiedFollowing, thirty360,
euribor6m);
auto sw5y = ext::make_shared<SwapRateHelper>(
0.02525, 5 * Years,
WeekendsOnly(), Annual, ModifiedFollowing, thirty360,
euribor6m);
auto sw6y = ext::make_shared<SwapRateHelper>(
0.02696, 6 * Years,
WeekendsOnly(), Annual, ModifiedFollowing, thirty360,
euribor6m);
auto sw7y = ext::make_shared<SwapRateHelper>(
0.02825, 7 * Years,
WeekendsOnly(), Annual, ModifiedFollowing, thirty360,
euribor6m);
auto sw8y = ext::make_shared<SwapRateHelper>(
0.02931, 8 * Years,
WeekendsOnly(), Annual, ModifiedFollowing, thirty360,
euribor6m);
auto sw9y = ext::make_shared<SwapRateHelper>(
0.03017, 9 * Years,
WeekendsOnly(), Annual, ModifiedFollowing, thirty360,
euribor6m);
auto sw10y = ext::make_shared<SwapRateHelper>(
0.03092, 10 * Years,
WeekendsOnly(), Annual, ModifiedFollowing, thirty360,
euribor6m);
auto sw11y = ext::make_shared<SwapRateHelper>(
0.03160, 11 * Years,
WeekendsOnly(), Annual, ModifiedFollowing, thirty360,
euribor6m);
auto sw12y = ext::make_shared<SwapRateHelper>(
0.03231, 12 * Years,
WeekendsOnly(), Annual, ModifiedFollowing, thirty360,
euribor6m);
auto sw15y = ext::make_shared<SwapRateHelper>(
0.03367, 15 * Years,
WeekendsOnly(), Annual, ModifiedFollowing, thirty360,
euribor6m);
auto sw20y = ext::make_shared<SwapRateHelper>(
0.03419, 20 * Years,
WeekendsOnly(), Annual, ModifiedFollowing, thirty360,
euribor6m);
auto sw25y = ext::make_shared<SwapRateHelper>(
0.03411, 25 * Years,
WeekendsOnly(), Annual, ModifiedFollowing, thirty360,
euribor6m);
auto sw30y = ext::make_shared<SwapRateHelper>(
0.03412, 30 * Years,
WeekendsOnly(), Annual, ModifiedFollowing, thirty360,
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();
auto cds6m = ext::make_shared<SpreadCdsHelper>(
0.007927, 6 * Months, 1,
WeekendsOnly(), Quarterly, Following,
DateGeneration::CDS,
Actual360(), 0.4, isdaYts,
true,
true,
Date(),
auto cds1y = ext::make_shared<SpreadCdsHelper>(
0.007927, 1 * Years, 1,
WeekendsOnly(), Quarterly, Following,
DateGeneration::CDS,
Actual360(), 0.4, isdaYts,
true,
true,
Date(),
auto cds3y = ext::make_shared<SpreadCdsHelper>(
0.012239, 3 * Years, 1,
WeekendsOnly(), Quarterly, Following,
DateGeneration::CDS,
Actual360(), 0.4, isdaYts,
true,
true,
Date(),
auto cds5y = ext::make_shared<SpreadCdsHelper>(
0.016979, 5 * Years, 1,
WeekendsOnly(), Quarterly, Following,
DateGeneration::CDS,
Actual360(), 0.4, isdaYts,
true,
true,
Date(),
auto cds7y = ext::make_shared<SpreadCdsHelper>(
0.019271, 7 * Years, 1,
WeekendsOnly(), Quarterly, Following,
DateGeneration::CDS,
Actual360(), 0.4, isdaYts,
true,
true,
Date(),
auto cds10y = ext::make_shared<SpreadCdsHelper>(
0.020860, 10 * Years, 1,
WeekendsOnly(), Quarterly, Following,
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);
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 {
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;
}
}
Actual/365 (Fixed) day counter.
backward-flat interpolation between discrete points
Actual/360 day count convention.
Actual/365 (Fixed) day count convention.
Date adjust(const Date &, BusinessDayConvention convention=Following) const
Date advance(const Date &, Integer n, TimeUnit unit, BusinessDayConvention convention=Following, bool endOfMonth=false) const
Real couponLegNPV() const
Real defaultLegNPV() const
Shared handle to an observable.
Real NPV() const
returns the net present value of the instrument.
void setPricingEngine(const ext::shared_ptr< PricingEngine > &)
set the pricing engine to be used.
MakeSchedule & withConvention(BusinessDayConvention)
MakeSchedule & withTerminationDateConvention(BusinessDayConvention)
MakeSchedule & withRule(DateGeneration::Rule)
MakeSchedule & to(const Date &terminationDate)
MakeSchedule & from(const Date &effectiveDate)
MakeSchedule & withFrequency(Frequency)
MakeSchedule & withCalendar(const Calendar &)
Piecewise default-probability term structure.
Piecewise yield term structure.
const_iterator begin() const
const_iterator end() const
30/360 day count convention
bootstrap helpers for default-probability term structures
Coupon paying a fixed annual rate.
flat forward rate term structure
flat hazard-rate term structure
unsigned QL_INTEGER Natural
positive integer
std::size_t Size
size of a container
Coupon paying a Libor-type index.
ISDA engine for credit default swaps.
Mid-point engine for credit default swaps.
piecewise-interpolated default-probability structure
piecewise-interpolated term structure
Global definitions and compiler switches.
deposit, FRA, futures, and various swap rate helpers