This example bootstraps a default-probability curve over a number of CDS and reprices them.
#include <ql/quantlib.hpp>
#include <boost/timer.hpp>
#include <iostream>
#include <iomanip>
#ifdef BOOST_MSVC
# ifdef QL_ENABLE_THREAD_SAFE_OBSERVER_PATTERN
# include <ql/auto_link.hpp>
# define BOOST_LIB_NAME boost_system
# include <boost/config/auto_link.hpp>
# undef BOOST_LIB_NAME
# define BOOST_LIB_NAME boost_thread
# include <boost/config/auto_link.hpp>
# undef BOOST_LIB_NAME
# endif
#endif
#if defined(QL_ENABLE_SESSIONS)
}
#endif
int main(int, char* []) {
try {
boost::timer timer;
std::cout << std::endl;
Date todaysDate(15, May, 2007);
todaysDate = calendar.
adjust(todaysDate);
Settings::instance().evaluationDate() = todaysDate;
boost::shared_ptr<Quote> flatRate(
new SimpleQuote(0.01));
boost::shared_ptr<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(calendar.
adjust(todaysDate + tenors[i],
}
std::vector<boost::shared_ptr<DefaultProbabilityHelper> > instruments;
for (
Size i=0; i<4; i++) {
instruments.push_back(boost::shared_ptr<DefaultProbabilityHelper>(
tenors[i],
0,
calendar,
DateGeneration::TwentiethIMM,
recovery_rate,
tsCurve)));
}
boost::shared_ptr<PiecewiseDefaultCurve<HazardRate, BackwardFlat> >
hazardRateStructure(
todaysDate,
instruments,
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: "
<< endl;
cout << "2Y survival probability: "
<<
io::percent(hazardRateStructure->survivalProbability(
todaysDate + 2*Years))
<< endl
<< " expected: "
<< endl;
cout << endl << endl;
Real nominal = 1000000.0;
boost::shared_ptr<PricingEngine> engine(
new MidPointCdsEngine(probability, recovery_rate, tsCurve));
.
withRule(DateGeneration::TwentiethIMM);
nominal,
quoted_spreads[0],
cdsSchedule,
cdsSchedule =
.withCalendar(calendar)
.withRule(DateGeneration::TwentiethIMM);
nominal,
quoted_spreads[1],
cdsSchedule,
cdsSchedule =
.withCalendar(calendar)
.withRule(DateGeneration::TwentiethIMM);
nominal,
quoted_spreads[2],
cdsSchedule,
cdsSchedule =
.withCalendar(calendar)
.withRule(DateGeneration::TwentiethIMM);
nominal,
quoted_spreads[3],
cdsSchedule,
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;
Real seconds = timer.elapsed();
seconds -= hours * 3600;
seconds -= minutes * 60;
cout << "Run completed in ";
if (hours > 0)
cout << hours << " h ";
if (hours > 0 || minutes > 0)
cout << minutes << " m ";
cout << fixed << setprecision(0)
<< seconds << " s" << endl;
return 0;
} catch (exception& e) {
cerr << e.what() << endl;
return 1;
} catch (...) {
cerr << "unknown error" << endl;
return 1;
}
}