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 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
|
<!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>FittedBondCurve.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">FittedBondCurve.cpp</div> </div>
</div><!--header-->
<div class="contents">
<p>This example fits a discount curve over a set of bonds with a number of methods.</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">/* This example shows how to fit a term structure to a set of bonds</span>
<span class="comment"> using four different fitting methodologies. Though fitting is most</span>
<span class="comment"> useful for large numbers of bonds with non-smooth yield tenor</span>
<span class="comment"> structures, for comparison purposes, relatively smooth bond yields</span>
<span class="comment"> are fit here and compared to known solutions (par coupons), or</span>
<span class="comment"> results generated from the bootstrap fitting method.</span>
<span class="comment">*/</span>
<span class="preprocessor">#include <ql/quantlib.hpp></span>
<span class="preprocessor">#ifdef BOOST_MSVC</span>
<span class="preprocessor"></span><span class="comment">/* Uncomment the following lines to unmask floating-point</span>
<span class="comment"> exceptions. Warning: unpredictable results can arise...</span>
<span class="comment"></span>
<span class="comment"> See http://www.wilmott.com/messageview.cfm?catid=10&threadid=9481</span>
<span class="comment"> Is there anyone with a definitive word about this?</span>
<span class="comment">*/</span>
<span class="comment">// #include <float.h></span>
<span class="comment">// namespace { unsigned int u = _controlfp(_EM_INEXACT, _MCW_EM); }</span>
<span class="preprocessor">#endif</span>
<span class="preprocessor"></span>
<span class="preprocessor">#include <boost/timer.hpp></span>
<span class="preprocessor">#include <iostream></span>
<span class="preprocessor">#include <iomanip></span>
<span class="preprocessor">#define LENGTH(a) (sizeof(a)/sizeof(a[0]))</span>
<span class="preprocessor"></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="comment">// par-rate approximation</span>
<a name="a1"></a><a class="code" href="group__types.html#gaede435af51236692b1107d7639581d39" title="interest rates">Rate</a> parRate(<span class="keyword">const</span> <a name="_a2"></a><a class="code" href="class_quant_lib_1_1_yield_term_structure.html" title="Interest-rate term structure.">YieldTermStructure</a>& yts,
<span class="keyword">const</span> std::vector<Date>& dates,
<span class="keyword">const</span> <a name="_a3"></a><a class="code" href="class_quant_lib_1_1_day_counter.html" title="day counter class">DayCounter</a>& resultDayCounter) {
<a name="a4"></a><a class="code" href="errors_8hpp.html#a7a9bcab8006882bc7d5302a0861ab1a6" title="throw an error if the given pre-condition is not verified">QL_REQUIRE</a>(dates.size() >= 2, <span class="stringliteral">"at least two dates are required"</span>);
<a name="a5"></a><a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> sum = 0.0;
<a name="a6"></a><a class="code" href="group__types.html#ga14fb8fca43a68f4168654e1f9f7e22f7" title="continuous quantity with 1-year units">Time</a> dt;
<span class="keywordflow">for</span> (<a class="code" href="group__types.html#gaf38bdb4c54463b1f456655efa95b5c77" title="size of a container">Size</a> i=1; i<dates.size(); ++i) {
dt = resultDayCounter.<a name="a7"></a><a class="code" href="class_quant_lib_1_1_day_counter.html#af45d6af610c92c3fa975077f081002e6" title="Returns the period between two dates as a fraction of year.">yearFraction</a>(dates[i-1], dates[i]);
<a class="code" href="errors_8hpp.html#a7a9bcab8006882bc7d5302a0861ab1a6" title="throw an error if the given pre-condition is not verified">QL_REQUIRE</a>(dt>0.0, <span class="stringliteral">"unsorted dates"</span>);
sum += yts.<a name="a8"></a>discount(dates[i]) * dt;
}
<a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> result = yts.discount(dates.front()) - yts.discount(dates.back());
<span class="keywordflow">return</span> result/sum;
}
<span class="keywordtype">void</span> printOutput(<span class="keyword">const</span> std::string& tag,
<span class="keyword">const</span> boost::shared_ptr<FittedBondDiscountCurve>& curve) {
cout << tag << endl;
cout << <span class="stringliteral">"reference date : "</span>
<< curve->referenceDate()
<< endl;
cout << <span class="stringliteral">"number of iterations : "</span>
<< curve->fitResults().numberOfIterations()
<< endl
<< endl;
}
<span class="keywordtype">int</span> main(<span class="keywordtype">int</span>, <span class="keywordtype">char</span>* []) {
<span class="keywordflow">try</span> {
boost::timer timer;
<span class="keyword">const</span> <a class="code" href="group__types.html#gaf38bdb4c54463b1f456655efa95b5c77" title="size of a container">Size</a> numberOfBonds = 15;
<a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> cleanPrice[numberOfBonds];
<span class="keywordflow">for</span> (<a class="code" href="group__types.html#gaf38bdb4c54463b1f456655efa95b5c77" title="size of a container">Size</a> i=0; i<numberOfBonds; i++) {
cleanPrice[i]=100.0;
}
std::vector< boost::shared_ptr<SimpleQuote> > quote;
<span class="keywordflow">for</span> (<a class="code" href="group__types.html#gaf38bdb4c54463b1f456655efa95b5c77" title="size of a container">Size</a> i=0; i<numberOfBonds; i++) {
boost::shared_ptr<SimpleQuote> cp(<span class="keyword">new</span> <a name="_a9"></a><a class="code" href="class_quant_lib_1_1_simple_quote.html" title="market element returning a stored value">SimpleQuote</a>(cleanPrice[i]));
quote.push_back(cp);
}
<a name="_a10"></a><a class="code" href="class_quant_lib_1_1_relinkable_handle.html">RelinkableHandle<Quote></a> quoteHandle[numberOfBonds];
<span class="keywordflow">for</span> (<a class="code" href="group__types.html#gaf38bdb4c54463b1f456655efa95b5c77" title="size of a container">Size</a> i=0; i<numberOfBonds; i++) {
quoteHandle[i].<a name="a11"></a>linkTo(quote[i]);
}
<a class="code" href="group__types.html#gab9c87440c314438e51a899a03d2442d0" title="integer number">Integer</a> lengths[] = { 2, 4, 6, 8, 10, 12, 14, 16,
18, 20, 22, 24, 26, 28, 30 };
<a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> coupons[] = { 0.0200, 0.0225, 0.0250, 0.0275, 0.0300,
0.0325, 0.0350, 0.0375, 0.0400, 0.0425,
0.0450, 0.0475, 0.0500, 0.0525, 0.0550 };
<a class="code" href="group__datetime.html#ga6d41db8ba0ea90d22df35889df452ada" title="Frequency of events.">Frequency</a> frequency = <a name="a12"></a><a class="code" href="group__datetime.html#gga6d41db8ba0ea90d22df35889df452adaa508b18014c96e2d466c12b39d7f4e426" title="once a year">Annual</a>;
<a class="code" href="class_quant_lib_1_1_day_counter.html" title="day counter class">DayCounter</a> dc = <a name="_a13"></a><a class="code" href="class_quant_lib_1_1_simple_day_counter.html" title="Simple day counter for reproducing theoretical calculations.">SimpleDayCounter</a>();
<a class="code" href="group__datetime.html#gaff46c5ae9385d20709bedade86edd368" title="Business Day conventions.">BusinessDayConvention</a> accrualConvention = <a name="a14"></a><a class="code" href="group__datetime.html#ggaff46c5ae9385d20709bedade86edd368a013e092c359f6031cc0563fd413ce707">ModifiedFollowing</a>;
<a class="code" href="group__datetime.html#gaff46c5ae9385d20709bedade86edd368" title="Business Day conventions.">BusinessDayConvention</a> convention = <a class="code" href="group__datetime.html#ggaff46c5ae9385d20709bedade86edd368a013e092c359f6031cc0563fd413ce707">ModifiedFollowing</a>;
<a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> redemption = 100.0;
<a name="_a15"></a><a class="code" href="class_quant_lib_1_1_calendar.html" title="calendar class">Calendar</a> calendar = <a name="_a16"></a><a class="code" href="class_quant_lib_1_1_null_calendar.html" title="Calendar for reproducing theoretical calculations.">NullCalendar</a>();
<a name="_a17"></a><a class="code" href="class_quant_lib_1_1_date.html" title="Concrete date class.">Date</a> today = calendar.<a name="a18"></a><a class="code" href="class_quant_lib_1_1_calendar.html#aee7c18511c7ee1bcda6124f43a37aa28">adjust</a>(Date::todaysDate());
<a class="code" href="class_quant_lib_1_1_date.html" title="Concrete date class.">Date</a> origToday = today;
Settings::instance().evaluationDate() = today;
<span class="comment">// changing bondSettlementDays=3 increases calculation</span>
<span class="comment">// time of exponentialsplines fitting method</span>
<a name="a19"></a><a class="code" href="group__types.html#ga7e529c39c477ba1f5a22264d93e8457a" title="positive integer">Natural</a> bondSettlementDays = 0;
<a class="code" href="group__types.html#ga7e529c39c477ba1f5a22264d93e8457a" title="positive integer">Natural</a> curveSettlementDays = 0;
<a class="code" href="class_quant_lib_1_1_date.html" title="Concrete date class.">Date</a> bondSettlementDate = calendar.<a name="a20"></a><a class="code" href="class_quant_lib_1_1_calendar.html#ac47bb82bf7b147cfacf373991c2905ea">advance</a>(today, bondSettlementDays*Days);
cout << endl;
cout << <span class="stringliteral">"Today's date: "</span> << today << endl;
cout << <span class="stringliteral">"Bonds' settlement date: "</span> << bondSettlementDate << endl;
cout << <span class="stringliteral">"Calculating fit for 15 bonds....."</span> << endl << endl;
std::vector<boost::shared_ptr<BondHelper> > instrumentsA;
std::vector<boost::shared_ptr<RateHelper> > instrumentsB;
<span class="keywordflow">for</span> (<a class="code" href="group__types.html#gaf38bdb4c54463b1f456655efa95b5c77" title="size of a container">Size</a> j=0; j<LENGTH(lengths); j++) {
<a class="code" href="class_quant_lib_1_1_date.html" title="Concrete date class.">Date</a> maturity = calendar.<a class="code" href="class_quant_lib_1_1_calendar.html#ac47bb82bf7b147cfacf373991c2905ea">advance</a>(bondSettlementDate, lengths[j]*Years);
<a name="_a21"></a><a class="code" href="class_quant_lib_1_1_schedule.html" title="Payment schedule.">Schedule</a> schedule(bondSettlementDate, maturity, <a name="_a22"></a><a class="code" href="class_quant_lib_1_1_period.html">Period</a>(frequency),
calendar, accrualConvention, accrualConvention,
DateGeneration::Backward, <span class="keyword">false</span>);
boost::shared_ptr<BondHelper> helperA(
<span class="keyword">new</span> FixedRateBondHelper(quoteHandle[j],
bondSettlementDays,
100.0,
schedule,
std::vector<Rate>(1,coupons[j]),
dc,
convention,
redemption));
boost::shared_ptr<RateHelper> helperB(
<span class="keyword">new</span> FixedRateBondHelper(quoteHandle[j],
bondSettlementDays,
100.0,
schedule,
std::vector<Rate>(1, coupons[j]),
dc,
convention,
redemption));
instrumentsA.push_back(helperA);
instrumentsB.push_back(helperB);
}
<span class="keywordtype">bool</span> constrainAtZero = <span class="keyword">true</span>;
<a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> tolerance = 1.0e-10;
<a class="code" href="group__types.html#gaf38bdb4c54463b1f456655efa95b5c77" title="size of a container">Size</a> max = 5000;
boost::shared_ptr<YieldTermStructure> ts0 (
<span class="keyword">new</span> <a name="_a23"></a><a class="code" href="class_quant_lib_1_1_piecewise_yield_curve.html" title="Piecewise yield term structure.">PiecewiseYieldCurve<Discount,LogLinear></a>(curveSettlementDays,
calendar,
instrumentsB,
dc));
<a name="_a24"></a><a class="code" href="class_quant_lib_1_1_exponential_splines_fitting.html" title="Exponential-splines fitting method.">ExponentialSplinesFitting</a> exponentialSplines(constrainAtZero);
boost::shared_ptr<FittedBondDiscountCurve> ts1 (
<span class="keyword">new</span> <a name="_a25"></a><a class="code" href="class_quant_lib_1_1_fitted_bond_discount_curve.html" title="Discount curve fitted to a set of fixed-coupon bonds.">FittedBondDiscountCurve</a>(curveSettlementDays,
calendar,
instrumentsA,
dc,
exponentialSplines,
tolerance,
max));
printOutput(<span class="stringliteral">"(a) exponential splines"</span>, ts1);
<a name="_a26"></a><a class="code" href="class_quant_lib_1_1_simple_polynomial_fitting.html" title="Simple polynomial fitting method.">SimplePolynomialFitting</a> simplePolynomial(3, constrainAtZero);
boost::shared_ptr<FittedBondDiscountCurve> ts2 (
<span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_fitted_bond_discount_curve.html" title="Discount curve fitted to a set of fixed-coupon bonds.">FittedBondDiscountCurve</a>(curveSettlementDays,
calendar,
instrumentsA,
dc,
simplePolynomial,
tolerance,
max));
printOutput(<span class="stringliteral">"(b) simple polynomial"</span>, ts2);
<a name="_a27"></a><a class="code" href="class_quant_lib_1_1_nelson_siegel_fitting.html" title="Nelson-Siegel fitting method.">NelsonSiegelFitting</a> nelsonSiegel;
boost::shared_ptr<FittedBondDiscountCurve> ts3 (
<span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_fitted_bond_discount_curve.html" title="Discount curve fitted to a set of fixed-coupon bonds.">FittedBondDiscountCurve</a>(curveSettlementDays,
calendar,
instrumentsA,
dc,
nelsonSiegel,
tolerance,
max));
printOutput(<span class="stringliteral">"(c) Nelson-Siegel"</span>, ts3);
<span class="comment">// a cubic bspline curve with 11 knot points, implies</span>
<span class="comment">// n=6 (constrained problem) basis functions</span>
<a class="code" href="group__types.html#ga14fb8fca43a68f4168654e1f9f7e22f7" title="continuous quantity with 1-year units">Time</a> knots[] = { -30.0, -20.0, 0.0, 5.0, 10.0, 15.0,
20.0, 25.0, 30.0, 40.0, 50.0 };
std::vector<Time> knotVector;
<span class="keywordflow">for</span> (<a class="code" href="group__types.html#gaf38bdb4c54463b1f456655efa95b5c77" title="size of a container">Size</a> i=0; i< LENGTH(knots); i++) {
knotVector.push_back(knots[i]);
}
<a name="_a28"></a><a class="code" href="class_quant_lib_1_1_cubic_b_splines_fitting.html" title="CubicSpline B-splines fitting method.">CubicBSplinesFitting</a> cubicBSplines(knotVector, constrainAtZero);
boost::shared_ptr<FittedBondDiscountCurve> ts4 (
<span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_fitted_bond_discount_curve.html" title="Discount curve fitted to a set of fixed-coupon bonds.">FittedBondDiscountCurve</a>(curveSettlementDays,
calendar,
instrumentsA,
dc,
cubicBSplines,
tolerance,
max));
printOutput(<span class="stringliteral">"(d) cubic B-splines"</span>, ts4);
<a name="_a29"></a><a class="code" href="class_quant_lib_1_1_svensson_fitting.html" title="Svensson Fitting method.">SvenssonFitting</a> svensson;
boost::shared_ptr<FittedBondDiscountCurve> ts5 (
<span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_fitted_bond_discount_curve.html" title="Discount curve fitted to a set of fixed-coupon bonds.">FittedBondDiscountCurve</a>(curveSettlementDays,
calendar,
instrumentsA,
dc,
svensson,
tolerance,
max));
printOutput(<span class="stringliteral">"(e) Svensson"</span>, ts5);
cout << <span class="stringliteral">"Output par rates for each curve. In this case, "</span>
<< endl
<< <span class="stringliteral">"par rates should equal coupons for these par bonds."</span>
<< endl
<< endl;
cout << setw(6) << <span class="stringliteral">"tenor"</span> << <span class="stringliteral">" | "</span>
<< setw(6) << <span class="stringliteral">"coupon"</span> << <span class="stringliteral">" | "</span>
<< setw(6) << <span class="stringliteral">"bstrap"</span> << <span class="stringliteral">" | "</span>
<< setw(6) << <span class="stringliteral">"(a)"</span> << <span class="stringliteral">" | "</span>
<< setw(6) << <span class="stringliteral">"(b)"</span> << <span class="stringliteral">" | "</span>
<< setw(6) << <span class="stringliteral">"(c)"</span> << <span class="stringliteral">" | "</span>
<< setw(6) << <span class="stringliteral">"(d)"</span> << <span class="stringliteral">" | "</span>
<< setw(6) << <span class="stringliteral">"(e)"</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<instrumentsA.size(); i++) {
std::vector<boost::shared_ptr<CashFlow> > cfs =
instrumentsA[i]->bond()->cashflows();
<a class="code" href="group__types.html#gaf38bdb4c54463b1f456655efa95b5c77" title="size of a container">Size</a> cfSize = instrumentsA[i]->bond()->cashflows().size();
std::vector<Date> keyDates;
keyDates.push_back(bondSettlementDate);
<span class="keywordflow">for</span> (<a class="code" href="group__types.html#gaf38bdb4c54463b1f456655efa95b5c77" title="size of a container">Size</a> j=0; j<cfSize-1; j++) {
<span class="keywordflow">if</span> (!cfs[j]->hasOccurred(bondSettlementDate, <span class="keyword">false</span>)) {
<a class="code" href="class_quant_lib_1_1_date.html" title="Concrete date class.">Date</a> myDate = cfs[j]->date();
keyDates.push_back(myDate);
}
}
<a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> tenor = dc.<a class="code" href="class_quant_lib_1_1_day_counter.html#af45d6af610c92c3fa975077f081002e6" title="Returns the period between two dates as a fraction of year.">yearFraction</a>(today, cfs[cfSize-1]->date());
cout << setw(6) << fixed << setprecision(3) << tenor << <span class="stringliteral">" | "</span>
<< setw(6) << fixed << setprecision(3)
<< 100.*coupons[i] << <span class="stringliteral">" | "</span>
<span class="comment">// piecewise bootstrap</span>
<< setw(6) << fixed << setprecision(3)
<< 100.*parRate(*ts0,keyDates,dc) << <span class="stringliteral">" | "</span>
<span class="comment">// exponential splines</span>
<< setw(6) << fixed << setprecision(3)
<< 100.*parRate(*ts1,keyDates,dc) << <span class="stringliteral">" | "</span>
<span class="comment">// simple polynomial</span>
<< setw(6) << fixed << setprecision(3)
<< 100.*parRate(*ts2,keyDates,dc) << <span class="stringliteral">" | "</span>
<span class="comment">// Nelson-Siegel</span>
<< setw(6) << fixed << setprecision(3)
<< 100.*parRate(*ts3,keyDates,dc) << <span class="stringliteral">" | "</span>
<span class="comment">// cubic bsplines</span>
<< setw(6) << fixed << setprecision(3)
<< 100.*parRate(*ts4,keyDates,dc) << <span class="stringliteral">" | "</span>
<span class="comment">// Svensson</span>
<< setw(6) << fixed << setprecision(3)
<< 100.*parRate(*ts5,keyDates,dc) << endl;
}
cout << endl << endl << endl;
cout << <span class="stringliteral">"Now add 23 months to today. Par rates should be "</span> << endl
<< <span class="stringliteral">"automatically recalculated because today's date "</span> << endl
<< <span class="stringliteral">"changes. Par rates will NOT equal coupons (YTM "</span> << endl
<< <span class="stringliteral">"will, with the correct compounding), but the "</span> << endl
<< <span class="stringliteral">"piecewise yield curve par rates can be used as "</span> << endl
<< <span class="stringliteral">"a benchmark for correct par rates."</span>
<< endl
<< endl;
today = calendar.<a class="code" href="class_quant_lib_1_1_calendar.html#ac47bb82bf7b147cfacf373991c2905ea">advance</a>(origToday,23,Months,convention);
Settings::instance().evaluationDate() = today;
bondSettlementDate = calendar.<a class="code" href="class_quant_lib_1_1_calendar.html#ac47bb82bf7b147cfacf373991c2905ea">advance</a>(today, bondSettlementDays*Days);
printOutput(<span class="stringliteral">"(a) exponential splines"</span>, ts1);
printOutput(<span class="stringliteral">"(b) simple polynomial"</span>, ts2);
printOutput(<span class="stringliteral">"(c) Nelson-Siegel"</span>, ts3);
printOutput(<span class="stringliteral">"(d) cubic B-splines"</span>, ts4);
printOutput(<span class="stringliteral">"(e) Svensson"</span>, ts5);
cout << endl
<< endl;
cout << setw(6) << <span class="stringliteral">"tenor"</span> << <span class="stringliteral">" | "</span>
<< setw(6) << <span class="stringliteral">"coupon"</span> << <span class="stringliteral">" | "</span>
<< setw(6) << <span class="stringliteral">"bstrap"</span> << <span class="stringliteral">" | "</span>
<< setw(6) << <span class="stringliteral">"(a)"</span> << <span class="stringliteral">" | "</span>
<< setw(6) << <span class="stringliteral">"(b)"</span> << <span class="stringliteral">" | "</span>
<< setw(6) << <span class="stringliteral">"(c)"</span> << <span class="stringliteral">" | "</span>
<< setw(6) << <span class="stringliteral">"(d)"</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<instrumentsA.size(); i++) {
std::vector<boost::shared_ptr<CashFlow> > cfs =
instrumentsA[i]->bond()->cashflows();
<a class="code" href="group__types.html#gaf38bdb4c54463b1f456655efa95b5c77" title="size of a container">Size</a> cfSize = instrumentsA[i]->bond()->cashflows().size();
std::vector<Date> keyDates;
keyDates.push_back(bondSettlementDate);
<span class="keywordflow">for</span> (<a class="code" href="group__types.html#gaf38bdb4c54463b1f456655efa95b5c77" title="size of a container">Size</a> j=0; j<cfSize-1; j++) {
<span class="keywordflow">if</span> (!cfs[j]->hasOccurred(bondSettlementDate, <span class="keyword">false</span>)) {
<a class="code" href="class_quant_lib_1_1_date.html" title="Concrete date class.">Date</a> myDate = cfs[j]->date();
keyDates.push_back(myDate);
}
}
<a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> tenor = dc.<a class="code" href="class_quant_lib_1_1_day_counter.html#af45d6af610c92c3fa975077f081002e6" title="Returns the period between two dates as a fraction of year.">yearFraction</a>(today, cfs[cfSize-1]->date());
cout << setw(6) << fixed << setprecision(3) << tenor << <span class="stringliteral">" | "</span>
<< setw(6) << fixed << setprecision(3)
<< 100.*coupons[i] << <span class="stringliteral">" | "</span>
<span class="comment">// piecewise bootstrap</span>
<< setw(6) << fixed << setprecision(3)
<< 100.*parRate(*ts0,keyDates,dc) << <span class="stringliteral">" | "</span>
<span class="comment">// exponential splines</span>
<< setw(6) << fixed << setprecision(3)
<< 100.*parRate(*ts1,keyDates,dc) << <span class="stringliteral">" | "</span>
<span class="comment">// simple polynomial</span>
<< setw(6) << fixed << setprecision(3)
<< 100.*parRate(*ts2,keyDates,dc) << <span class="stringliteral">" | "</span>
<span class="comment">// Nelson-Siegel</span>
<< setw(6) << fixed << setprecision(3)
<< 100.*parRate(*ts3,keyDates,dc) << <span class="stringliteral">" | "</span>
<span class="comment">// cubic bsplines</span>
<< setw(6) << fixed << setprecision(3)
<< 100.*parRate(*ts4,keyDates,dc) << <span class="stringliteral">" | "</span>
<span class="comment">// Svensson</span>
<< setw(6) << fixed << setprecision(3)
<< 100.*parRate(*ts5,keyDates,dc) << endl;
}
cout << endl << endl << endl;
cout << <span class="stringliteral">"Now add one more month, for a total of two years "</span> << endl
<< <span class="stringliteral">"from the original date. The first instrument is "</span> << endl
<< <span class="stringliteral">"now expired and par rates should again equal "</span> << endl
<< <span class="stringliteral">"coupon values, since clean prices did not change."</span>
<< endl
<< endl;
instrumentsA.erase(instrumentsA.begin(),
instrumentsA.begin()+1);
instrumentsB.erase(instrumentsB.begin(),
instrumentsB.begin()+1);
today = calendar.<a class="code" href="class_quant_lib_1_1_calendar.html#ac47bb82bf7b147cfacf373991c2905ea">advance</a>(origToday,24,Months,convention);
Settings::instance().evaluationDate() = today;
bondSettlementDate = calendar.<a class="code" href="class_quant_lib_1_1_calendar.html#ac47bb82bf7b147cfacf373991c2905ea">advance</a>(today, bondSettlementDays*Days);
boost::shared_ptr<YieldTermStructure> ts00 (
<span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_piecewise_yield_curve.html" title="Piecewise yield term structure.">PiecewiseYieldCurve<Discount,LogLinear></a>(curveSettlementDays,
calendar,
instrumentsB,
dc));
boost::shared_ptr<FittedBondDiscountCurve> ts11 (
<span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_fitted_bond_discount_curve.html" title="Discount curve fitted to a set of fixed-coupon bonds.">FittedBondDiscountCurve</a>(curveSettlementDays,
calendar,
instrumentsA,
dc,
exponentialSplines,
tolerance,
max));
printOutput(<span class="stringliteral">"(a) exponential splines"</span>, ts11);
boost::shared_ptr<FittedBondDiscountCurve> ts22 (
<span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_fitted_bond_discount_curve.html" title="Discount curve fitted to a set of fixed-coupon bonds.">FittedBondDiscountCurve</a>(curveSettlementDays,
calendar,
instrumentsA,
dc,
simplePolynomial,
tolerance,
max));
printOutput(<span class="stringliteral">"(b) simple polynomial"</span>, ts22);
boost::shared_ptr<FittedBondDiscountCurve> ts33 (
<span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_fitted_bond_discount_curve.html" title="Discount curve fitted to a set of fixed-coupon bonds.">FittedBondDiscountCurve</a>(curveSettlementDays,
calendar,
instrumentsA,
dc,
nelsonSiegel,
tolerance,
max));
printOutput(<span class="stringliteral">"(c) Nelson-Siegel"</span>, ts33);
boost::shared_ptr<FittedBondDiscountCurve> ts44 (
<span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_fitted_bond_discount_curve.html" title="Discount curve fitted to a set of fixed-coupon bonds.">FittedBondDiscountCurve</a>(curveSettlementDays,
calendar,
instrumentsA,
dc,
cubicBSplines,
tolerance,
max));
printOutput(<span class="stringliteral">"(d) cubic B-splines"</span>, ts44);
boost::shared_ptr<FittedBondDiscountCurve> ts55 (
<span class="keyword">new</span> <a class="code" href="class_quant_lib_1_1_fitted_bond_discount_curve.html" title="Discount curve fitted to a set of fixed-coupon bonds.">FittedBondDiscountCurve</a>(curveSettlementDays,
calendar,
instrumentsA,
dc,
svensson,
tolerance,
max));
printOutput(<span class="stringliteral">"(e) Svensson"</span>, ts55);
cout << setw(6) << <span class="stringliteral">"tenor"</span> << <span class="stringliteral">" | "</span>
<< setw(6) << <span class="stringliteral">"coupon"</span> << <span class="stringliteral">" | "</span>
<< setw(6) << <span class="stringliteral">"bstrap"</span> << <span class="stringliteral">" | "</span>
<< setw(6) << <span class="stringliteral">"(a)"</span> << <span class="stringliteral">" | "</span>
<< setw(6) << <span class="stringliteral">"(b)"</span> << <span class="stringliteral">" | "</span>
<< setw(6) << <span class="stringliteral">"(c)"</span> << <span class="stringliteral">" | "</span>
<< setw(6) << <span class="stringliteral">"(d)"</span> << <span class="stringliteral">" | "</span>
<< setw(6) << <span class="stringliteral">"(e)"</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<instrumentsA.size(); i++) {
std::vector<boost::shared_ptr<CashFlow> > cfs =
instrumentsA[i]->bond()->cashflows();
<a class="code" href="group__types.html#gaf38bdb4c54463b1f456655efa95b5c77" title="size of a container">Size</a> cfSize = instrumentsA[i]->bond()->cashflows().size();
std::vector<Date> keyDates;
keyDates.push_back(bondSettlementDate);
<span class="keywordflow">for</span> (<a class="code" href="group__types.html#gaf38bdb4c54463b1f456655efa95b5c77" title="size of a container">Size</a> j=0; j<cfSize-1; j++) {
<span class="keywordflow">if</span> (!cfs[j]->hasOccurred(bondSettlementDate, <span class="keyword">false</span>)) {
<a class="code" href="class_quant_lib_1_1_date.html" title="Concrete date class.">Date</a> myDate = cfs[j]->date();
keyDates.push_back(myDate);
}
}
<a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> tenor = dc.<a class="code" href="class_quant_lib_1_1_day_counter.html#af45d6af610c92c3fa975077f081002e6" title="Returns the period between two dates as a fraction of year.">yearFraction</a>(today, cfs[cfSize-1]->date());
cout << setw(6) << fixed << setprecision(3) << tenor << <span class="stringliteral">" | "</span>
<< setw(6) << fixed << setprecision(3)
<< 100.*coupons[i+1] << <span class="stringliteral">" | "</span>
<span class="comment">// piecewise bootstrap</span>
<< setw(6) << fixed << setprecision(3)
<< 100.*parRate(*ts00,keyDates,dc) << <span class="stringliteral">" | "</span>
<span class="comment">// exponential splines</span>
<< setw(6) << fixed << setprecision(3)
<< 100.*parRate(*ts11,keyDates,dc) << <span class="stringliteral">" | "</span>
<span class="comment">// simple polynomial</span>
<< setw(6) << fixed << setprecision(3)
<< 100.*parRate(*ts22,keyDates,dc) << <span class="stringliteral">" | "</span>
<span class="comment">// Nelson-Siegel</span>
<< setw(6) << fixed << setprecision(3)
<< 100.*parRate(*ts33,keyDates,dc) << <span class="stringliteral">" | "</span>
<span class="comment">// cubic bsplines</span>
<< setw(6) << fixed << setprecision(3)
<< 100.*parRate(*ts44,keyDates,dc) << <span class="stringliteral">" | "</span>
<span class="comment">// Svensson</span>
<< setw(6) << fixed << setprecision(3)
<< 100.*parRate(*ts55,keyDates,dc) << endl;
}
cout << endl << endl << endl;
cout << <span class="stringliteral">"Now decrease prices by a small amount, corresponding"</span> << endl
<< <span class="stringliteral">"to a theoretical five basis point parallel + shift of"</span> << endl
<< <span class="stringliteral">"the yield curve. Because bond quotes change, the new "</span> << endl
<< <span class="stringliteral">"par rates should be recalculated automatically."</span>
<< endl
<< endl;
<span class="keywordflow">for</span> (<a class="code" href="group__types.html#gaf38bdb4c54463b1f456655efa95b5c77" title="size of a container">Size</a> k=0; k<LENGTH(lengths)-1; k++) {
<a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> P = instrumentsA[k]->quote()->value();
<span class="keyword">const</span> <a name="_a30"></a><a class="code" href="class_quant_lib_1_1_bond.html" title="Base bond class.">Bond</a>& b = *instrumentsA[k]->bond();
<a class="code" href="group__types.html#gaede435af51236692b1107d7639581d39" title="interest rates">Rate</a> ytm = BondFunctions::yield(b, P,
dc, Compounded, frequency,
today);
<a class="code" href="group__types.html#ga14fb8fca43a68f4168654e1f9f7e22f7" title="continuous quantity with 1-year units">Time</a> dur = BondFunctions::duration(b, ytm,
dc, Compounded, frequency,
Duration::Modified,
today);
<span class="keyword">const</span> <a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> bpsChange = 5.;
<span class="comment">// dP = -dur * P * dY</span>
<a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> deltaP = -dur * P * (bpsChange/10000.);
quote[k+1]->setValue(P + deltaP);
}
cout << setw(6) << <span class="stringliteral">"tenor"</span> << <span class="stringliteral">" | "</span>
<< setw(6) << <span class="stringliteral">"coupon"</span> << <span class="stringliteral">" | "</span>
<< setw(6) << <span class="stringliteral">"bstrap"</span> << <span class="stringliteral">" | "</span>
<< setw(6) << <span class="stringliteral">"(a)"</span> << <span class="stringliteral">" | "</span>
<< setw(6) << <span class="stringliteral">"(b)"</span> << <span class="stringliteral">" | "</span>
<< setw(6) << <span class="stringliteral">"(c)"</span> << <span class="stringliteral">" | "</span>
<< setw(6) << <span class="stringliteral">"(d)"</span> << <span class="stringliteral">" | "</span>
<< setw(6) << <span class="stringliteral">"(e)"</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<instrumentsA.size(); i++) {
std::vector<boost::shared_ptr<CashFlow> > cfs =
instrumentsA[i]->bond()->cashflows();
<a class="code" href="group__types.html#gaf38bdb4c54463b1f456655efa95b5c77" title="size of a container">Size</a> cfSize = instrumentsA[i]->bond()->cashflows().size();
std::vector<Date> keyDates;
keyDates.push_back(bondSettlementDate);
<span class="keywordflow">for</span> (<a class="code" href="group__types.html#gaf38bdb4c54463b1f456655efa95b5c77" title="size of a container">Size</a> j=0; j<cfSize-1; j++) {
<span class="keywordflow">if</span> (!cfs[j]->hasOccurred(bondSettlementDate, <span class="keyword">false</span>)) {
<a class="code" href="class_quant_lib_1_1_date.html" title="Concrete date class.">Date</a> myDate = cfs[j]->date();
keyDates.push_back(myDate);
}
}
<a class="code" href="group__types.html#ga4bdf4bfe76b9ffa6fa64c47d8bfa0c78" title="real number">Real</a> tenor = dc.<a class="code" href="class_quant_lib_1_1_day_counter.html#af45d6af610c92c3fa975077f081002e6" title="Returns the period between two dates as a fraction of year.">yearFraction</a>(today, cfs[cfSize-1]->date());
cout << setw(6) << fixed << setprecision(3) << tenor << <span class="stringliteral">" | "</span>
<< setw(6) << fixed << setprecision(3)
<< 100.*coupons[i+1] << <span class="stringliteral">" | "</span>
<span class="comment">// piecewise bootstrap</span>
<< setw(6) << fixed << setprecision(3)
<< 100.*parRate(*ts00,keyDates,dc) << <span class="stringliteral">" | "</span>
<span class="comment">// exponential splines</span>
<< setw(6) << fixed << setprecision(3)
<< 100.*parRate(*ts11,keyDates,dc) << <span class="stringliteral">" | "</span>
<span class="comment">// simple polynomial</span>
<< setw(6) << fixed << setprecision(3)
<< 100.*parRate(*ts22,keyDates,dc) << <span class="stringliteral">" | "</span>
<span class="comment">// Nelson-Siegel</span>
<< setw(6) << fixed << setprecision(3)
<< 100.*parRate(*ts33,keyDates,dc) << <span class="stringliteral">" | "</span>
<span class="comment">// cubic bsplines</span>
<< setw(6) << fixed << setprecision(3)
<< 100.*parRate(*ts44,keyDates,dc) << <span class="stringliteral">" | "</span>
<span class="comment">// Svensson</span>
<< setw(6) << fixed << setprecision(3)
<< 100.*parRate(*ts55,keyDates,dc) << 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 = int(seconds/3600);
seconds -= hours * 3600;
<a class="code" href="group__types.html#gab9c87440c314438e51a899a03d2442d0" title="integer number">Integer</a> minutes = int(seconds/60);
seconds -= minutes * 60;
std::cout << <span class="stringliteral">" \nRun completed in "</span>;
<span class="keywordflow">if</span> (hours > 0)
std::cout << hours << <span class="stringliteral">" h "</span>;
<span class="keywordflow">if</span> (hours > 0 || minutes > 0)
std::cout << minutes << <span class="stringliteral">" m "</span>;
std::cout << std::fixed << std::setprecision(0)
<< seconds << <span class="stringliteral">" s\n"</span> << std::endl;
<span class="keywordflow">return</span> 0;
} <span class="keywordflow">catch</span> (std::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>
|