1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<meta name="robots" content="none">
<title>CDS.cpp</title>
<link rel="stylesheet" href="quantlib.css" type="text/css">
<link rel="stylesheet" href="print.css" type="text/css" media="print">
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
<link rel="icon" href="favicon.ico" type="image/x-icon">
</head>
<body>
<div id="container">
<div id="header">
<img class="titleimage"
src="QL-title.jpg" width="185" height="50" border="0"
alt="QuantLib">
<br>
<h3 class="subtitle">A free/open-source library for quantitative finance</h3>
</div>
<div id="menu">
<h3 class="navbartitle">Version 1.2</h3>
<hr>
<h3 class="navbartitle">Getting started</h3>
<ul class="navbarlist">
<li class="navlink"><a href="index.html">Introduction</a></li>
<li class="navlink"><a href="where.html">Where to get QuantLib</a></li>
<li class="navlink"><a href="install.html">Installation</a></li>
<li class="navlink"><a href="config.html">Configuration</a></li>
<li class="navlink"><a href="usage.html">Usage</a></li>
<li class="navlink"><a href="history.html">Version history</a></li>
<li class="navlink"><a href="resources.html">Additional resources</a></li>
<li class="navlink"><a href="group.html">The QuantLib group</a></li>
<li class="navlink"><a href="license.html">Copyright and license</a></li>
</ul>
<hr>
<h3 class="navbartitle">Reference manual</h3>
<ul class="navbarlist">
<li class="navlink"><a href="modules.html">Modules</a></li>
<li class="navlink"><a href="hierarchy.html">Class Hierarchy</a></li>
<li class="navlink"><a href="annotated.html">Compound List</a></li>
<li class="navlink"><a href="files.html">File List</a></li>
<li class="navlink"><a href="functions.html">Compound Members</a></li>
<li class="navlink"><a href="globals.html">File Members</a></li>
<li class="navlink"><a href="todo.html">Todo List</a></li>
<li class="navlink"><a href="bug.html">Known Bugs</a></li>
<li class="navlink"><a href="caveats.html">Caveats</a></li>
<li class="navlink"><a href="test.html">Test Suite</a></li>
<li class="navlink"><a href="examples.html">Examples</a></li>
</ul>
</div>
<div id="content">
<!--Doxygen-generated content-->
<!-- Generated by Doxygen 1.7.6.1 -->
</div>
<div class="header">
<div class="headertitle">
<div class="title">CDS.cpp</div> </div>
</div><!--header-->
<div class="contents">
<p>This example bootstraps a default-probability curve over a number of CDS and reprices them.</p>
<div class="fragment"><pre class="fragment"><span class="comment">/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */</span>
<span class="comment">/*</span>
<span class="comment"> Copyright (C) 2008 Jose Aparicio</span>
<span class="comment"></span>
<span class="comment"> This file is part of QuantLib, a free-software/open-source library</span>
<span class="comment"> for financial quantitative analysts and developers - http://quantlib.org/</span>
<span class="comment"></span>
<span class="comment"> QuantLib is free software: you can redistribute it and/or modify it</span>
<span class="comment"> under the terms of the QuantLib license. You should have received a</span>
<span class="comment"> copy of the license along with this program; if not, please email</span>
<span class="comment"> <quantlib-dev@lists.sf.net>. The license is also available online at</span>
<span class="comment"> <http://quantlib.org/license.shtml>.</span>
<span class="comment"></span>
<span class="comment"> This program is distributed in the hope that it will be useful, but WITHOUT</span>
<span class="comment"> ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS</span>
<span class="comment"> FOR A PARTICULAR PURPOSE. See the license for more details.</span>
<span class="comment">*/</span>
<span class="preprocessor">#include <ql/quantlib.hpp></span>
<span class="preprocessor">#include <boost/timer.hpp></span>
<span class="preprocessor">#include <iostream></span>
<span class="preprocessor">#include <iomanip></span>
<span class="keyword">using namespace </span>std;
<span class="keyword">using namespace </span>QuantLib;
<span class="preprocessor">#if defined(QL_ENABLE_SESSIONS)</span>
<span class="preprocessor"></span><span class="keyword">namespace </span>QuantLib {
<a name="a0"></a><a class="code" href="group__types.html#gab9c87440c314438e51a899a03d2442d0" title="integer number">Integer</a> sessionId() { <span class="keywordflow">return</span> 0; }
}
<span class="preprocessor">#endif</span>
<span class="preprocessor"></span>
<span class="keywordtype">int</span> main(<span class="keywordtype">int</span>, <span class="keywordtype">char</span>* []) {
<span class="keywordflow">try</span> {
boost::timer timer;
std::cout << std::endl;
<span class="comment">/*********************</span>
<span class="comment"> *** MARKET DATA ***</span>
<span class="comment"> *********************/</span>
<a name="_a1"></a><a class="code" href="class_quant_lib_1_1_calendar.html" title="calendar class">Calendar</a> calendar = <a name="_a2"></a><a class="code" href="class_quant_lib_1_1_t_a_r_g_e_t.html" title="TARGET calendar">TARGET</a>();
<a name="_a3"></a><a class="code" href="class_quant_lib_1_1_date.html" title="Concrete date class.">Date</a> todaysDate(15, May, 2007);
<span class="comment">// must be a business day</span>
todaysDate = calendar.<a name="a4"></a><a class="code" href="class_quant_lib_1_1_calendar.html#aee7c18511c7ee1bcda6124f43a37aa28">adjust</a>(todaysDate);
Settings::instance().evaluationDate() = todaysDate;
<span class="comment">// dummy curve</span>
boost::shared_ptr<Quote> flatRate(<span class="keyword">new</span> <a name="_a5"></a><a class="code" href="class_quant_lib_1_1_simple_quote.html" title="market element returning a stored value">SimpleQuote</a>(0.01));
<a name="_a6"></a><a class="code" href="class_quant_lib_1_1_handle.html">Handle<YieldTermStructure></a> tsCurve(
boost::shared_ptr<FlatForward>(
<span class="keyword">new</span> <a name="_a7"></a><a class="code" href="class_quant_lib_1_1_flat_forward.html" title="Flat interest-rate curve.">FlatForward</a>(todaysDate, <a name="_a8"></a><a class="code" href="class_quant_lib_1_1_handle.html">Handle<Quote></a>(flatRate),
<a name="_a9"></a><a class="code" href="class_quant_lib_1_1_actual365_fixed.html" title="Actual/365 (Fixed) day count convention.">Actual365Fixed</a>())));
<span class="comment">/*</span>
<span class="comment"> In Lehmans Brothers "guide to exotic credit derivatives"</span>
<span class="comment"> p. 32 there's a simple case, zero flat curve with a flat CDS</span>
<span class="comment"> curve with constant market spreads of 150 bp and RR = 50%</span>
<span class="comment"> corresponds to a flat 3% hazard rate. The implied 1-year</span>
<span class="comment"> survival probability is 97.04% and the 2-years is 94.18%</span>
<span class="comment"> */</span>
<span class="comment">// market</span>
<a name="a10"></a><a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> recovery_rate = 0.5;
<a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> 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;
<span class="keywordflow">for</span> (<a class="code" href="group__types.html#gaf38bdb4c54463b1f456655efa95b5c77" title="size of a container">Size</a> i=0; i<4; i++) {
maturities.push_back(calendar.<a class="code" href="class_quant_lib_1_1_calendar.html#aee7c18511c7ee1bcda6124f43a37aa28">adjust</a>(todaysDate + tenors[i],
<a name="a11"></a><a class="code" href="group__datetime.html#ggaff46c5ae9385d20709bedade86edd368ab6a37af780aa2b97f8bbdc4d149dae18">Following</a>));
}
std::vector<boost::shared_ptr<DefaultProbabilityHelper> > instruments;
<span class="keywordflow">for</span> (<a class="code" href="group__types.html#gaf38bdb4c54463b1f456655efa95b5c77" title="size of a container">Size</a> i=0; i<4; i++) {
instruments.push_back(boost::shared_ptr<DefaultProbabilityHelper>(
<span class="keyword">new</span> <a name="_a12"></a><a class="code" href="class_quant_lib_1_1_spread_cds_helper.html" title="Spread-quoted CDS hazard rate bootstrap helper.">SpreadCdsHelper</a>(
<a class="code" href="class_quant_lib_1_1_handle.html">Handle<Quote></a>(boost::shared_ptr<Quote>(
<span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_simple_quote.html" title="market element returning a stored value">SimpleQuote</a>(quoted_spreads[i]))),
tenors[i],
0,
calendar,
<a name="a13"></a><a class="code" href="group__datetime.html#gga6d41db8ba0ea90d22df35889df452adaab4fce358383d1822f7596659e00b07f7" title="every third month">Quarterly</a>,
<a class="code" href="group__datetime.html#ggaff46c5ae9385d20709bedade86edd368ab6a37af780aa2b97f8bbdc4d149dae18">Following</a>,
DateGeneration::TwentiethIMM,
<a class="code" href="class_quant_lib_1_1_actual365_fixed.html" title="Actual/365 (Fixed) day count convention.">Actual365Fixed</a>(),
recovery_rate,
tsCurve)));
}
<span class="comment">// Bootstrap hazard rates</span>
boost::shared_ptr<PiecewiseDefaultCurve<HazardRate, BackwardFlat> >
hazardRateStructure(
<span class="keyword">new</span> <a name="_a14"></a><a class="code" href="class_quant_lib_1_1_piecewise_default_curve.html" title="Piecewise default-probability term structure.">PiecewiseDefaultCurve<HazardRate, BackwardFlat></a>(
todaysDate,
instruments,
<a class="code" href="class_quant_lib_1_1_actual365_fixed.html" title="Actual/365 (Fixed) day count convention.">Actual365Fixed</a>()));
vector<pair<Date, Real> > hr_curve_data = hazardRateStructure->nodes();
cout << <span class="stringliteral">"Calibrated hazard rate values: "</span> << endl ;
<span class="keywordflow">for</span> (<a class="code" href="group__types.html#gaf38bdb4c54463b1f456655efa95b5c77" title="size of a container">Size</a> i=0; i<hr_curve_data.size(); i++) {
cout << <span class="stringliteral">"hazard rate on "</span> << hr_curve_data[i].first << <span class="stringliteral">" is "</span>
<< hr_curve_data[i].second << endl;
}
cout << endl;
cout << <span class="stringliteral">"Some survival probability values: "</span> << endl ;
cout << <span class="stringliteral">"1Y survival probability: "</span>
<< <a name="a15"></a><a class="code" href="group__manips.html#gae524bfb28ff5254a99ba5a4e778aee29" title="output reals as percentages">io::percent</a>(hazardRateStructure->survivalProbability(
todaysDate + 1*Years))
<< endl
<< <span class="stringliteral">" expected: "</span>
<< <a class="code" href="group__manips.html#gae524bfb28ff5254a99ba5a4e778aee29" title="output reals as percentages">io::percent</a>(0.9704)
<< endl;
cout << <span class="stringliteral">"2Y survival probability: "</span>
<< <a class="code" href="group__manips.html#gae524bfb28ff5254a99ba5a4e778aee29" title="output reals as percentages">io::percent</a>(hazardRateStructure->survivalProbability(
todaysDate + 2*Years))
<< endl
<< <span class="stringliteral">" expected: "</span>
<< <a class="code" href="group__manips.html#gae524bfb28ff5254a99ba5a4e778aee29" title="output reals as percentages">io::percent</a>(0.9418)
<< endl;
cout << endl << endl;
<span class="comment">// reprice instruments</span>
<a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> nominal = 1000000.0;
<a name="_a16"></a><a class="code" href="class_quant_lib_1_1_handle.html">Handle<DefaultProbabilityTermStructure></a> probability(hazardRateStructure);
boost::shared_ptr<PricingEngine> engine(
<span class="keyword">new</span> MidPointCdsEngine(probability, recovery_rate, tsCurve));
<a name="_a17"></a><a class="code" href="class_quant_lib_1_1_schedule.html" title="Payment schedule.">Schedule</a> cdsSchedule =
<a name="_a18"></a><a class="code" href="class_quant_lib_1_1_make_schedule.html" title="helper class">MakeSchedule</a>().<a name="a19"></a>from(todaysDate).<a name="a20"></a>to(maturities[0])
.<a name="a21"></a>withFrequency(<a class="code" href="group__datetime.html#gga6d41db8ba0ea90d22df35889df452adaab4fce358383d1822f7596659e00b07f7" title="every third month">Quarterly</a>)
.<a name="a22"></a>withCalendar(calendar)
.<a name="a23"></a>withTerminationDateConvention(<a name="a24"></a><a class="code" href="group__datetime.html#ggaff46c5ae9385d20709bedade86edd368a5fb8b90b7fdf1a3a228fbbc083e64547">Unadjusted</a>)
.<a name="a25"></a>withRule(DateGeneration::TwentiethIMM);
<a name="_a26"></a><a class="code" href="class_quant_lib_1_1_credit_default_swap.html" title="Credit default swap.">CreditDefaultSwap</a> cds_3m(Protection::Seller,
nominal,
quoted_spreads[0],
cdsSchedule,
<a class="code" href="group__datetime.html#ggaff46c5ae9385d20709bedade86edd368ab6a37af780aa2b97f8bbdc4d149dae18">Following</a>,
<a class="code" href="class_quant_lib_1_1_actual365_fixed.html" title="Actual/365 (Fixed) day count convention.">Actual365Fixed</a>());
cdsSchedule =
<a class="code" href="class_quant_lib_1_1_make_schedule.html" title="helper class">MakeSchedule</a>().from(todaysDate).to(maturities[1])
.withFrequency(<a class="code" href="group__datetime.html#gga6d41db8ba0ea90d22df35889df452adaab4fce358383d1822f7596659e00b07f7" title="every third month">Quarterly</a>)
.withCalendar(calendar)
.withTerminationDateConvention(<a class="code" href="group__datetime.html#ggaff46c5ae9385d20709bedade86edd368a5fb8b90b7fdf1a3a228fbbc083e64547">Unadjusted</a>)
.withRule(DateGeneration::TwentiethIMM);
<a class="code" href="class_quant_lib_1_1_credit_default_swap.html" title="Credit default swap.">CreditDefaultSwap</a> cds_6m(Protection::Seller,
nominal,
quoted_spreads[1],
cdsSchedule,
<a class="code" href="group__datetime.html#ggaff46c5ae9385d20709bedade86edd368ab6a37af780aa2b97f8bbdc4d149dae18">Following</a>,
<a class="code" href="class_quant_lib_1_1_actual365_fixed.html" title="Actual/365 (Fixed) day count convention.">Actual365Fixed</a>());
cdsSchedule =
<a class="code" href="class_quant_lib_1_1_make_schedule.html" title="helper class">MakeSchedule</a>().from(todaysDate).to(maturities[2])
.withFrequency(<a class="code" href="group__datetime.html#gga6d41db8ba0ea90d22df35889df452adaab4fce358383d1822f7596659e00b07f7" title="every third month">Quarterly</a>)
.withCalendar(calendar)
.withTerminationDateConvention(<a class="code" href="group__datetime.html#ggaff46c5ae9385d20709bedade86edd368a5fb8b90b7fdf1a3a228fbbc083e64547">Unadjusted</a>)
.withRule(DateGeneration::TwentiethIMM);
<a class="code" href="class_quant_lib_1_1_credit_default_swap.html" title="Credit default swap.">CreditDefaultSwap</a> cds_1y(Protection::Seller,
nominal,
quoted_spreads[2],
cdsSchedule,
<a class="code" href="group__datetime.html#ggaff46c5ae9385d20709bedade86edd368ab6a37af780aa2b97f8bbdc4d149dae18">Following</a>,
<a class="code" href="class_quant_lib_1_1_actual365_fixed.html" title="Actual/365 (Fixed) day count convention.">Actual365Fixed</a>());
cdsSchedule =
<a class="code" href="class_quant_lib_1_1_make_schedule.html" title="helper class">MakeSchedule</a>().from(todaysDate).to(maturities[3])
.withFrequency(<a class="code" href="group__datetime.html#gga6d41db8ba0ea90d22df35889df452adaab4fce358383d1822f7596659e00b07f7" title="every third month">Quarterly</a>)
.withCalendar(calendar)
.withTerminationDateConvention(<a class="code" href="group__datetime.html#ggaff46c5ae9385d20709bedade86edd368a5fb8b90b7fdf1a3a228fbbc083e64547">Unadjusted</a>)
.withRule(DateGeneration::TwentiethIMM);
<a class="code" href="class_quant_lib_1_1_credit_default_swap.html" title="Credit default swap.">CreditDefaultSwap</a> cds_2y(Protection::Seller,
nominal,
quoted_spreads[3],
cdsSchedule,
<a class="code" href="group__datetime.html#ggaff46c5ae9385d20709bedade86edd368ab6a37af780aa2b97f8bbdc4d149dae18">Following</a>,
<a class="code" href="class_quant_lib_1_1_actual365_fixed.html" title="Actual/365 (Fixed) day count convention.">Actual365Fixed</a>());
cds_3m.setPricingEngine(engine);
cds_6m.setPricingEngine(engine);
cds_1y.setPricingEngine(engine);
cds_2y.setPricingEngine(engine);
cout << <span class="stringliteral">"Repricing of quoted CDSs employed for calibration: "</span> << endl;
cout << <span class="stringliteral">"3M fair spread: "</span> << <a name="a27"></a><a class="code" href="group__manips.html#gac63ea3dad10259db3764c7d15a61cde5" title="output rates and spreads as percentages">io::rate</a>(cds_3m.fairSpread()) << endl
<< <span class="stringliteral">" NPV: "</span> << cds_3m.NPV() << endl
<< <span class="stringliteral">" default leg: "</span> << cds_3m.defaultLegNPV() << endl
<< <span class="stringliteral">" coupon leg: "</span> << cds_3m.couponLegNPV() << endl
<< endl;
cout << <span class="stringliteral">"6M fair spread: "</span> << <a class="code" href="group__manips.html#gac63ea3dad10259db3764c7d15a61cde5" title="output rates and spreads as percentages">io::rate</a>(cds_6m.fairSpread()) << endl
<< <span class="stringliteral">" NPV: "</span> << cds_6m.NPV() << endl
<< <span class="stringliteral">" default leg: "</span> << cds_6m.defaultLegNPV() << endl
<< <span class="stringliteral">" coupon leg: "</span> << cds_6m.couponLegNPV() << endl
<< endl;
cout << <span class="stringliteral">"1Y fair spread: "</span> << <a class="code" href="group__manips.html#gac63ea3dad10259db3764c7d15a61cde5" title="output rates and spreads as percentages">io::rate</a>(cds_1y.fairSpread()) << endl
<< <span class="stringliteral">" NPV: "</span> << cds_1y.NPV() << endl
<< <span class="stringliteral">" default leg: "</span> << cds_1y.defaultLegNPV() << endl
<< <span class="stringliteral">" coupon leg: "</span> << cds_1y.couponLegNPV() << endl
<< endl;
cout << <span class="stringliteral">"2Y fair spread: "</span> << <a class="code" href="group__manips.html#gac63ea3dad10259db3764c7d15a61cde5" title="output rates and spreads as percentages">io::rate</a>(cds_2y.fairSpread()) << endl
<< <span class="stringliteral">" NPV: "</span> << cds_2y.NPV() << endl
<< <span class="stringliteral">" default leg: "</span> << cds_2y.defaultLegNPV() << endl
<< <span class="stringliteral">" coupon leg: "</span> << cds_2y.couponLegNPV() << endl
<< endl;
cout << endl << endl;
<a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> seconds = timer.elapsed();
<a class="code" href="group__types.html#gab9c87440c314438e51a899a03d2442d0" title="integer number">Integer</a> hours = <a class="code" href="group__types.html#gab9c87440c314438e51a899a03d2442d0" title="integer number">Integer</a>(seconds/3600);
seconds -= hours * 3600;
<a class="code" href="group__types.html#gab9c87440c314438e51a899a03d2442d0" title="integer number">Integer</a> minutes = <a class="code" href="group__types.html#gab9c87440c314438e51a899a03d2442d0" title="integer number">Integer</a>(seconds/60);
seconds -= minutes * 60;
cout << <span class="stringliteral">"Run completed in "</span>;
<span class="keywordflow">if</span> (hours > 0)
cout << hours << <span class="stringliteral">" h "</span>;
<span class="keywordflow">if</span> (hours > 0 || minutes > 0)
cout << minutes << <span class="stringliteral">" m "</span>;
cout << fixed << setprecision(0)
<< seconds << <span class="stringliteral">" s"</span> << endl;
<span class="keywordflow">return</span> 0;
} <span class="keywordflow">catch</span> (exception& e) {
cerr << e.what() << endl;
<span class="keywordflow">return</span> 1;
} <span class="keywordflow">catch</span> (...) {
cerr << <span class="stringliteral">"unknown error"</span> << endl;
<span class="keywordflow">return</span> 1;
}
}
</pre></div> </div><!-- contents -->
</div><!-- contents -->
</div>
<div class="footer">
<div class="endmatter">
Documentation generated by
<a href="http://www.doxygen.org">Doxygen</a> 1.7.6.1
</div>
</div>
</div>
</body>
</html>
|