| 1 | |
| 2 | /* |
| 3 | Copyright (C) 2014 StatPro Italia srl |
| 4 | |
| 5 | This file is part of QuantLib, a free-software/open-source library |
| 6 | for financial quantitative analysts and developers - http://quantlib.org/ |
| 7 | |
| 8 | QuantLib is free software: you can redistribute it and/or modify it |
| 9 | under the terms of the QuantLib license. You should have received a |
| 10 | copy of the license along with this program; if not, please email |
| 11 | <quantlib-dev@lists.sf.net>. The license is also available online at |
| 12 | <http://quantlib.org/license.shtml>. |
| 13 | |
| 14 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 15 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 16 | FOR A PARTICULAR PURPOSE. See the license for more details. |
| 17 | */ |
| 18 | |
| 19 | #include "piecewisezerospreadedtermstructure.hpp" |
| 20 | #include "utilities.hpp" |
| 21 | #include <ql/termstructures/yield/piecewisezerospreadedtermstructure.hpp> |
| 22 | #include <ql/termstructures/yield/zerocurve.hpp> |
| 23 | #include <ql/indexes/iborindex.hpp> |
| 24 | #include <ql/termstructures/yield/ratehelpers.hpp> |
| 25 | #include <ql/time/daycounters/actual360.hpp> |
| 26 | #include <ql/time/calendars/target.hpp> |
| 27 | #include <ql/time/daycounters/thirty360.hpp> |
| 28 | #include <ql/math/interpolations/forwardflatinterpolation.hpp> |
| 29 | #include <ql/math/interpolations/backwardflatinterpolation.hpp> |
| 30 | #include <ql/math/interpolations/cubicinterpolation.hpp> |
| 31 | |
| 32 | using namespace QuantLib; |
| 33 | using namespace boost::unit_test_framework; |
| 34 | |
| 35 | namespace piecewise_zero_spreaded_term_structure_test { |
| 36 | |
| 37 | struct Datum { |
| 38 | Integer n; |
| 39 | TimeUnit units; |
| 40 | Rate rate; |
| 41 | }; |
| 42 | |
| 43 | struct CommonVars { |
| 44 | // common data |
| 45 | Calendar calendar; |
| 46 | Natural settlementDays; |
| 47 | DayCounter dayCount; |
| 48 | Compounding compounding; |
| 49 | ext::shared_ptr<YieldTermStructure> termStructure; |
| 50 | Date today; |
| 51 | Date settlementDate; |
| 52 | |
| 53 | // setup |
| 54 | CommonVars() { |
| 55 | calendar = TARGET(); |
| 56 | settlementDays = 2; |
| 57 | today =Date(9,June,2009); |
| 58 | compounding = Continuous; |
| 59 | dayCount = Actual360(); |
| 60 | settlementDate = calendar.advance(today,n: settlementDays,unit: Days); |
| 61 | |
| 62 | Settings::instance().evaluationDate() = today; |
| 63 | |
| 64 | Integer ts[] = { 13, 41, 75, 165, 256 , 345, 524, 703 }; |
| 65 | Rate r[] = { 0.035,0.033,0.034, 0.034, 0.036,0.037,0.039,0.040 }; |
| 66 | std::vector<Rate> rates(1, 0.035); |
| 67 | std::vector<Date> dates(1, settlementDate); |
| 68 | for (Size i = 0; i < 8; ++i) { |
| 69 | dates.push_back(x: calendar.advance(today,n: ts[i],unit: Days)); |
| 70 | rates.push_back(x: r[i]); |
| 71 | } |
| 72 | termStructure = ext::make_shared<ZeroCurve>(args&: dates, args&: rates, args&: dayCount); |
| 73 | } |
| 74 | }; |
| 75 | |
| 76 | } |
| 77 | |
| 78 | void PiecewiseZeroSpreadedTermStructureTest::testFlatInterpolationLeft() { |
| 79 | |
| 80 | BOOST_TEST_MESSAGE("Testing flat interpolation before the first spreaded date..." ); |
| 81 | |
| 82 | using namespace piecewise_zero_spreaded_term_structure_test; |
| 83 | |
| 84 | CommonVars vars; |
| 85 | |
| 86 | ext::shared_ptr<SimpleQuote> spread1 = ext::make_shared<SimpleQuote>(args: 0.02); |
| 87 | ext::shared_ptr<SimpleQuote> spread2 = ext::make_shared<SimpleQuote>(args: 0.03); |
| 88 | std::vector<Handle<Quote> > spreads = { Handle<Quote>(spread1), Handle<Quote>(spread2) }; |
| 89 | |
| 90 | std::vector<Date> spreadDates = {vars.calendar.advance(vars.today, n: 8, unit: Months), |
| 91 | vars.calendar.advance(vars.today, n: 15, unit: Months)}; |
| 92 | |
| 93 | Date interpolationDate = vars.calendar.advance(vars.today, n: 6, unit: Months); |
| 94 | |
| 95 | ext::shared_ptr<ZeroYieldStructure> spreadedTermStructure = |
| 96 | ext::make_shared<PiecewiseZeroSpreadedTermStructure>( |
| 97 | args: Handle<YieldTermStructure>(vars.termStructure), |
| 98 | args&: spreads, args&: spreadDates); |
| 99 | |
| 100 | Time t = vars.dayCount.yearFraction(d1: vars.today, d2: interpolationDate); |
| 101 | Rate interpolatedZeroRate = spreadedTermStructure->zeroRate(t,comp: vars.compounding); |
| 102 | |
| 103 | Real tolerance = 1e-9; |
| 104 | Real expectedRate = vars.termStructure->zeroRate(t,comp: vars.compounding) + |
| 105 | spread1->value(); |
| 106 | |
| 107 | if (std::fabs(x: interpolatedZeroRate - expectedRate) > tolerance) |
| 108 | BOOST_ERROR( |
| 109 | "unable to reproduce interpolated rate\n" |
| 110 | << std::setprecision(10) |
| 111 | << " calculated: " << io::rate(interpolatedZeroRate) << "\n" |
| 112 | << " expected: " << io::rate(expectedRate)); |
| 113 | |
| 114 | } |
| 115 | |
| 116 | void PiecewiseZeroSpreadedTermStructureTest::testFlatInterpolationRight() { |
| 117 | |
| 118 | BOOST_TEST_MESSAGE("Testing flat interpolation after the last spreaded date..." ); |
| 119 | |
| 120 | using namespace piecewise_zero_spreaded_term_structure_test; |
| 121 | |
| 122 | CommonVars vars; |
| 123 | |
| 124 | ext::shared_ptr<SimpleQuote> spread1 = ext::make_shared<SimpleQuote>(args: 0.02); |
| 125 | ext::shared_ptr<SimpleQuote> spread2 = ext::make_shared<SimpleQuote>(args: 0.03); |
| 126 | std::vector<Handle<Quote> > spreads = { Handle<Quote>(spread1), Handle<Quote>(spread2) }; |
| 127 | |
| 128 | std::vector<Date> spreadDates = {vars.calendar.advance(vars.today, n: 8, unit: Months), |
| 129 | vars.calendar.advance(vars.today, n: 15, unit: Months)}; |
| 130 | |
| 131 | Date interpolationDate = vars.calendar.advance(vars.today, n: 20, unit: Months); |
| 132 | |
| 133 | ext::shared_ptr<ZeroYieldStructure> spreadedTermStructure = |
| 134 | ext::make_shared<PiecewiseZeroSpreadedTermStructure>( |
| 135 | args: Handle<YieldTermStructure>(vars.termStructure), |
| 136 | args&: spreads, args&: spreadDates); |
| 137 | spreadedTermStructure->enableExtrapolation(); |
| 138 | |
| 139 | Time t = vars.dayCount.yearFraction(d1: vars.today, d2: interpolationDate); |
| 140 | Rate interpolatedZeroRate = spreadedTermStructure->zeroRate(t,comp: vars.compounding); |
| 141 | |
| 142 | Real tolerance = 1e-9; |
| 143 | Real expectedRate = vars.termStructure->zeroRate(t,comp: vars.compounding) + |
| 144 | spread2->value(); |
| 145 | |
| 146 | if (std::fabs(x: interpolatedZeroRate - expectedRate) > tolerance) |
| 147 | BOOST_ERROR( |
| 148 | "unable to reproduce interpolated rate\n" |
| 149 | << std::setprecision(10) |
| 150 | << " calculated: " << io::rate(interpolatedZeroRate) << "\n" |
| 151 | << " expected: " << io::rate(expectedRate)); |
| 152 | |
| 153 | } |
| 154 | |
| 155 | void PiecewiseZeroSpreadedTermStructureTest::testLinearInterpolationMultipleSpreads() { |
| 156 | |
| 157 | BOOST_TEST_MESSAGE("Testing linear interpolation with more than two spreaded dates..." ); |
| 158 | |
| 159 | using namespace piecewise_zero_spreaded_term_structure_test; |
| 160 | |
| 161 | CommonVars vars; |
| 162 | |
| 163 | ext::shared_ptr<SimpleQuote> spread1 = ext::make_shared<SimpleQuote>(args: 0.02); |
| 164 | ext::shared_ptr<SimpleQuote> spread2 = ext::make_shared<SimpleQuote>(args: 0.02); |
| 165 | ext::shared_ptr<SimpleQuote> spread3 = ext::make_shared<SimpleQuote>(args: 0.035); |
| 166 | ext::shared_ptr<SimpleQuote> spread4 = ext::make_shared<SimpleQuote>(args: 0.04); |
| 167 | std::vector<Handle<Quote> > spreads = { |
| 168 | Handle<Quote>(spread1), Handle<Quote>(spread2), |
| 169 | Handle<Quote>(spread3), Handle<Quote>(spread4), |
| 170 | }; |
| 171 | |
| 172 | std::vector<Date> spreadDates = {vars.calendar.advance(vars.today, n: 90, unit: Days), |
| 173 | vars.calendar.advance(vars.today, n: 150, unit: Days), |
| 174 | vars.calendar.advance(vars.today, n: 30, unit: Months), |
| 175 | vars.calendar.advance(vars.today, n: 40, unit: Months)}; |
| 176 | |
| 177 | Date interpolationDate = vars.calendar.advance(vars.today, n: 120, unit: Days); |
| 178 | |
| 179 | ext::shared_ptr<ZeroYieldStructure> spreadedTermStructure = |
| 180 | ext::make_shared<PiecewiseZeroSpreadedTermStructure>( |
| 181 | args: Handle<YieldTermStructure>(vars.termStructure), |
| 182 | args&: spreads, args&: spreadDates); |
| 183 | |
| 184 | Time t = vars.dayCount.yearFraction(d1: vars.today, d2: interpolationDate); |
| 185 | Rate interpolatedZeroRate = spreadedTermStructure->zeroRate(t,comp: vars.compounding); |
| 186 | |
| 187 | Real tolerance = 1e-9; |
| 188 | Real expectedRate = vars.termStructure->zeroRate(t,comp: vars.compounding) + |
| 189 | spread1->value(); |
| 190 | |
| 191 | if (std::fabs(x: interpolatedZeroRate - expectedRate) > tolerance) |
| 192 | BOOST_ERROR( |
| 193 | "unable to reproduce interpolated rate\n" |
| 194 | << std::setprecision(10) |
| 195 | << " calculated: " << io::rate(interpolatedZeroRate) << "\n" |
| 196 | << " expected: " << io::rate(expectedRate)); |
| 197 | |
| 198 | } |
| 199 | |
| 200 | void PiecewiseZeroSpreadedTermStructureTest::testLinearInterpolation() { |
| 201 | |
| 202 | BOOST_TEST_MESSAGE("Testing linear interpolation between two dates..." ); |
| 203 | |
| 204 | using namespace piecewise_zero_spreaded_term_structure_test; |
| 205 | |
| 206 | CommonVars vars; |
| 207 | |
| 208 | ext::shared_ptr<SimpleQuote> spread1 = ext::make_shared<SimpleQuote>(args: 0.02); |
| 209 | ext::shared_ptr<SimpleQuote> spread2 = ext::make_shared<SimpleQuote>(args: 0.03); |
| 210 | std::vector<Handle<Quote> > spreads = { Handle<Quote>(spread1), Handle<Quote>(spread2) }; |
| 211 | |
| 212 | std::vector<Date> spreadDates = {vars.calendar.advance(vars.today, n: 100, unit: Days), |
| 213 | vars.calendar.advance(vars.today, n: 150, unit: Days)}; |
| 214 | |
| 215 | Date interpolationDate = vars.calendar.advance(vars.today, n: 120, unit: Days); |
| 216 | |
| 217 | ext::shared_ptr<ZeroYieldStructure> spreadedTermStructure = |
| 218 | ext::make_shared<InterpolatedPiecewiseZeroSpreadedTermStructure<Linear> >( |
| 219 | args: Handle<YieldTermStructure>(vars.termStructure), |
| 220 | args&: spreads, args&: spreadDates); |
| 221 | |
| 222 | Date d0 = vars.calendar.advance(vars.today, n: 100, unit: Days); |
| 223 | Date d1 = vars.calendar.advance(vars.today, n: 150, unit: Days); |
| 224 | Date d2 = vars.calendar.advance(vars.today, n: 120, unit: Days); |
| 225 | |
| 226 | Real m = (0.03-0.02)/vars.dayCount.yearFraction(d1: d0,d2: d1); |
| 227 | Real expectedRate = m * vars.dayCount.yearFraction(d1: d0, d2) + 0.054; |
| 228 | |
| 229 | Time t = vars.dayCount.yearFraction(d1: vars.settlementDate, d2: interpolationDate); |
| 230 | Rate interpolatedZeroRate = spreadedTermStructure->zeroRate(t,comp: vars.compounding); |
| 231 | |
| 232 | Real tolerance = 1e-9; |
| 233 | |
| 234 | if (std::fabs(x: interpolatedZeroRate - expectedRate) > tolerance) |
| 235 | BOOST_ERROR( |
| 236 | "unable to reproduce interpolated rate\n" |
| 237 | << std::setprecision(10) |
| 238 | << " calculated: " << io::rate(interpolatedZeroRate) << "\n" |
| 239 | << " expected: " << io::rate(expectedRate)); |
| 240 | |
| 241 | } |
| 242 | |
| 243 | void PiecewiseZeroSpreadedTermStructureTest::testForwardFlatInterpolation() { |
| 244 | |
| 245 | BOOST_TEST_MESSAGE("Testing forward flat interpolation between two dates..." ); |
| 246 | |
| 247 | using namespace piecewise_zero_spreaded_term_structure_test; |
| 248 | |
| 249 | CommonVars vars; |
| 250 | |
| 251 | ext::shared_ptr<SimpleQuote> spread1 = ext::make_shared<SimpleQuote>(args: 0.02); |
| 252 | ext::shared_ptr<SimpleQuote> spread2 = ext::make_shared<SimpleQuote>(args: 0.03); |
| 253 | std::vector<Handle<Quote> > spreads = { Handle<Quote>(spread1), Handle<Quote>(spread2) }; |
| 254 | |
| 255 | std::vector<Date> spreadDates = {vars.calendar.advance(vars.today, n: 75, unit: Days), |
| 256 | vars.calendar.advance(vars.today, n: 260, unit: Days)}; |
| 257 | |
| 258 | Date interpolationDate = vars.calendar.advance(vars.today, n: 100, unit: Days); |
| 259 | |
| 260 | ext::shared_ptr<ZeroYieldStructure> spreadedTermStructure = |
| 261 | ext::make_shared<InterpolatedPiecewiseZeroSpreadedTermStructure<ForwardFlat> >( |
| 262 | args: Handle<YieldTermStructure>(vars.termStructure), |
| 263 | args&: spreads, args&: spreadDates); |
| 264 | |
| 265 | Time t = vars.dayCount.yearFraction(d1: vars.today, d2: interpolationDate); |
| 266 | Rate interpolatedZeroRate = spreadedTermStructure->zeroRate(t,comp: vars.compounding); |
| 267 | |
| 268 | Real tolerance = 1e-9; |
| 269 | Real expectedRate = vars.termStructure->zeroRate(t,comp: vars.compounding) + |
| 270 | spread1->value(); |
| 271 | |
| 272 | if (std::fabs(x: interpolatedZeroRate - expectedRate) > tolerance) |
| 273 | BOOST_ERROR( |
| 274 | "unable to reproduce interpolated rate\n" |
| 275 | << std::setprecision(10) |
| 276 | << " calculated: " << io::rate(interpolatedZeroRate) << "\n" |
| 277 | << " expected: " << io::rate(expectedRate)); |
| 278 | |
| 279 | } |
| 280 | |
| 281 | void PiecewiseZeroSpreadedTermStructureTest::testBackwardFlatInterpolation() { |
| 282 | |
| 283 | BOOST_TEST_MESSAGE("Testing backward flat interpolation between two dates..." ); |
| 284 | |
| 285 | using namespace piecewise_zero_spreaded_term_structure_test; |
| 286 | |
| 287 | CommonVars vars; |
| 288 | |
| 289 | ext::shared_ptr<SimpleQuote> spread1 = ext::make_shared<SimpleQuote>(args: 0.02); |
| 290 | ext::shared_ptr<SimpleQuote> spread2 = ext::make_shared<SimpleQuote>(args: 0.03); |
| 291 | ext::shared_ptr<SimpleQuote> spread3 = ext::make_shared<SimpleQuote>(args: 0.04); |
| 292 | std::vector<Handle<Quote> > spreads = { |
| 293 | Handle<Quote>(spread1), Handle<Quote>(spread2), Handle<Quote>(spread3) |
| 294 | }; |
| 295 | |
| 296 | std::vector<Date> spreadDates = {vars.calendar.advance(vars.today, n: 100, unit: Days), |
| 297 | vars.calendar.advance(vars.today, n: 200, unit: Days), |
| 298 | vars.calendar.advance(vars.today, n: 300, unit: Days)}; |
| 299 | |
| 300 | Date interpolationDate = vars.calendar.advance(vars.today, n: 110, unit: Days); |
| 301 | |
| 302 | ext::shared_ptr<ZeroYieldStructure> spreadedTermStructure = |
| 303 | ext::make_shared<InterpolatedPiecewiseZeroSpreadedTermStructure<BackwardFlat> >( |
| 304 | args: Handle<YieldTermStructure>(vars.termStructure), |
| 305 | args&: spreads, args&: spreadDates); |
| 306 | |
| 307 | Time t = vars.dayCount.yearFraction(d1: vars.today, d2: interpolationDate); |
| 308 | Rate interpolatedZeroRate = spreadedTermStructure->zeroRate(t,comp: vars.compounding); |
| 309 | |
| 310 | Real tolerance = 1e-9; |
| 311 | Real expectedRate = vars.termStructure->zeroRate(t,comp: vars.compounding) + |
| 312 | spread2->value(); |
| 313 | |
| 314 | if (std::fabs(x: interpolatedZeroRate - expectedRate) > tolerance) |
| 315 | BOOST_ERROR( |
| 316 | "unable to reproduce interpolated rate\n" |
| 317 | << std::setprecision(10) |
| 318 | << " calculated: " << io::rate(interpolatedZeroRate) << "\n" |
| 319 | << " expected: " << io::rate(expectedRate)); |
| 320 | |
| 321 | } |
| 322 | |
| 323 | void PiecewiseZeroSpreadedTermStructureTest::testDefaultInterpolation() { |
| 324 | |
| 325 | BOOST_TEST_MESSAGE("Testing default interpolation between two dates..." ); |
| 326 | |
| 327 | using namespace piecewise_zero_spreaded_term_structure_test; |
| 328 | |
| 329 | CommonVars vars; |
| 330 | |
| 331 | ext::shared_ptr<SimpleQuote> spread1 = ext::make_shared<SimpleQuote>(args: 0.02); |
| 332 | ext::shared_ptr<SimpleQuote> spread2 = ext::make_shared<SimpleQuote>(args: 0.02); |
| 333 | std::vector<Handle<Quote> > spreads = { Handle<Quote>(spread1), Handle<Quote>(spread2) }; |
| 334 | |
| 335 | std::vector<Date> spreadDates = {vars.calendar.advance(vars.today, n: 75, unit: Days), |
| 336 | vars.calendar.advance(vars.today, n: 160, unit: Days)}; |
| 337 | |
| 338 | Date interpolationDate = vars.calendar.advance(vars.today, n: 100, unit: Days); |
| 339 | |
| 340 | ext::shared_ptr<ZeroYieldStructure> spreadedTermStructure = |
| 341 | ext::make_shared<PiecewiseZeroSpreadedTermStructure>( |
| 342 | args: Handle<YieldTermStructure>(vars.termStructure), |
| 343 | args&: spreads, args&: spreadDates); |
| 344 | |
| 345 | Time t = vars.dayCount.yearFraction(d1: vars.today, d2: interpolationDate); |
| 346 | Rate interpolatedZeroRate = spreadedTermStructure->zeroRate(t,comp: vars.compounding); |
| 347 | |
| 348 | Real tolerance = 1e-9; |
| 349 | Real expectedRate = vars.termStructure->zeroRate(t,comp: vars.compounding) + |
| 350 | spread1->value(); |
| 351 | |
| 352 | if (std::fabs(x: interpolatedZeroRate - expectedRate) > tolerance) |
| 353 | BOOST_ERROR( |
| 354 | "unable to reproduce interpolated rate\n" |
| 355 | << std::setprecision(10) |
| 356 | << " calculated: " << io::rate(interpolatedZeroRate) << "\n" |
| 357 | << " expected: " << io::rate(expectedRate)); |
| 358 | |
| 359 | } |
| 360 | |
| 361 | void PiecewiseZeroSpreadedTermStructureTest::testSetInterpolationFactory() { |
| 362 | |
| 363 | BOOST_TEST_MESSAGE("Testing factory constructor with additional parameters..." ); |
| 364 | |
| 365 | using namespace piecewise_zero_spreaded_term_structure_test; |
| 366 | |
| 367 | CommonVars vars; |
| 368 | |
| 369 | ext::shared_ptr<SimpleQuote> spread1 = ext::make_shared<SimpleQuote>(args: 0.02); |
| 370 | ext::shared_ptr<SimpleQuote> spread2 = ext::make_shared<SimpleQuote>(args: 0.03); |
| 371 | ext::shared_ptr<SimpleQuote> spread3 = ext::make_shared<SimpleQuote>(args: 0.01); |
| 372 | std::vector<Handle<Quote> > spreads = { |
| 373 | Handle<Quote>(spread1), Handle<Quote>(spread2), Handle<Quote>(spread3) |
| 374 | }; |
| 375 | |
| 376 | std::vector<Date> spreadDates = {vars.calendar.advance(vars.today, n: 8, unit: Months), |
| 377 | vars.calendar.advance(vars.today, n: 15, unit: Months), |
| 378 | vars.calendar.advance(vars.today, n: 25, unit: Months)}; |
| 379 | |
| 380 | Date interpolationDate = vars.calendar.advance(vars.today, n: 11, unit: Months); |
| 381 | |
| 382 | ext::shared_ptr<ZeroYieldStructure> spreadedTermStructure; |
| 383 | |
| 384 | Frequency freq = NoFrequency; |
| 385 | |
| 386 | Cubic factory; |
| 387 | factory = Cubic(CubicInterpolation::Spline, false); |
| 388 | |
| 389 | spreadedTermStructure = |
| 390 | ext::make_shared<InterpolatedPiecewiseZeroSpreadedTermStructure<Cubic> >( |
| 391 | args: Handle<YieldTermStructure>(vars.termStructure), |
| 392 | args&: spreads, args&: spreadDates, args&: vars.compounding, |
| 393 | args&: freq, args&: vars.dayCount,args&: factory); |
| 394 | |
| 395 | Time t = vars.dayCount.yearFraction(d1: vars.today, d2: interpolationDate); |
| 396 | Rate interpolatedZeroRate = spreadedTermStructure->zeroRate(t,comp: vars.compounding); |
| 397 | |
| 398 | Real tolerance = 1e-9; |
| 399 | Real expectedRate = vars.termStructure->zeroRate(t,comp: vars.compounding) + |
| 400 | Real(0.026065770863); |
| 401 | |
| 402 | if (std::fabs(x: interpolatedZeroRate - expectedRate) > tolerance) |
| 403 | BOOST_ERROR( |
| 404 | "unable to reproduce interpolated rate\n" |
| 405 | << std::setprecision(10) |
| 406 | << " calculated: " << io::rate(interpolatedZeroRate) << "\n" |
| 407 | << " expected: " << io::rate(expectedRate)); |
| 408 | |
| 409 | } |
| 410 | |
| 411 | void PiecewiseZeroSpreadedTermStructureTest::testMaxDate() { |
| 412 | |
| 413 | BOOST_TEST_MESSAGE("Testing term structure max date..." ); |
| 414 | |
| 415 | using namespace piecewise_zero_spreaded_term_structure_test; |
| 416 | |
| 417 | CommonVars vars; |
| 418 | |
| 419 | ext::shared_ptr<SimpleQuote> spread1 = ext::make_shared<SimpleQuote>(args: 0.02); |
| 420 | ext::shared_ptr<SimpleQuote> spread2 = ext::make_shared<SimpleQuote>(args: 0.03); |
| 421 | std::vector<Handle<Quote> > spreads = { Handle<Quote>(spread1), Handle<Quote>(spread2) }; |
| 422 | |
| 423 | std::vector<Date> spreadDates = {vars.calendar.advance(vars.today, n: 8, unit: Months), |
| 424 | vars.calendar.advance(vars.today, n: 15, unit: Months)}; |
| 425 | |
| 426 | ext::shared_ptr<ZeroYieldStructure> spreadedTermStructure = |
| 427 | ext::make_shared<PiecewiseZeroSpreadedTermStructure>( |
| 428 | args: Handle<YieldTermStructure>(vars.termStructure), |
| 429 | args&: spreads, args&: spreadDates); |
| 430 | |
| 431 | Date maxDate = spreadedTermStructure->maxDate(); |
| 432 | |
| 433 | Date expectedDate = |
| 434 | std::min(a: vars.termStructure->maxDate(), b: spreadDates.back()); |
| 435 | |
| 436 | if (maxDate != expectedDate) |
| 437 | BOOST_ERROR( |
| 438 | "unable to reproduce max date\n" |
| 439 | << " calculated: " << maxDate << "\n" |
| 440 | << " expected: " << expectedDate); |
| 441 | |
| 442 | } |
| 443 | |
| 444 | void PiecewiseZeroSpreadedTermStructureTest::testQuoteChanging() { |
| 445 | |
| 446 | BOOST_TEST_MESSAGE("Testing quote update..." ); |
| 447 | |
| 448 | using namespace piecewise_zero_spreaded_term_structure_test; |
| 449 | |
| 450 | CommonVars vars; |
| 451 | |
| 452 | ext::shared_ptr<SimpleQuote> spread1 = ext::make_shared<SimpleQuote>(args: 0.02); |
| 453 | ext::shared_ptr<SimpleQuote> spread2 = ext::make_shared<SimpleQuote>(args: 0.03); |
| 454 | std::vector<Handle<Quote> > spreads = { Handle<Quote>(spread1), Handle<Quote>(spread2) }; |
| 455 | |
| 456 | std::vector<Date> spreadDates = {vars.calendar.advance(vars.today, n: 100, unit: Days), |
| 457 | vars.calendar.advance(vars.today, n: 150, unit: Days)}; |
| 458 | |
| 459 | Date interpolationDate = vars.calendar.advance(vars.today, n: 120, unit: Days); |
| 460 | |
| 461 | ext::shared_ptr<ZeroYieldStructure> spreadedTermStructure = |
| 462 | ext::make_shared<InterpolatedPiecewiseZeroSpreadedTermStructure<BackwardFlat> >( |
| 463 | args: Handle<YieldTermStructure>(vars.termStructure), |
| 464 | args&: spreads, args&: spreadDates); |
| 465 | |
| 466 | Time t = vars.dayCount.yearFraction(d1: vars.settlementDate, d2: interpolationDate); |
| 467 | Rate interpolatedZeroRate = spreadedTermStructure->zeroRate(t,comp: vars.compounding); |
| 468 | Real tolerance = 1e-9; |
| 469 | Real expectedRate = vars.termStructure->zeroRate(t,comp: vars.compounding) + |
| 470 | Real(0.03); |
| 471 | |
| 472 | if (std::fabs(x: interpolatedZeroRate - expectedRate) > tolerance) |
| 473 | BOOST_ERROR( |
| 474 | "unable to reproduce interpolated rate\n" |
| 475 | << std::setprecision(10) |
| 476 | << " calculated: " << io::rate(interpolatedZeroRate) << "\n" |
| 477 | << " expected: " << io::rate(expectedRate)); |
| 478 | |
| 479 | spread2->setValue(0.025); |
| 480 | |
| 481 | interpolatedZeroRate = spreadedTermStructure->zeroRate(t,comp: vars.compounding); |
| 482 | expectedRate = vars.termStructure->zeroRate(t,comp: vars.compounding) + |
| 483 | Real(0.025); |
| 484 | |
| 485 | if (std::fabs(x: interpolatedZeroRate - expectedRate) > tolerance) |
| 486 | BOOST_ERROR( |
| 487 | "unable to reproduce interpolated rate\n" |
| 488 | << std::setprecision(10) |
| 489 | << " calculated: " << io::rate(interpolatedZeroRate) << "\n" |
| 490 | << " expected: " << io::rate(expectedRate)); |
| 491 | |
| 492 | } |
| 493 | |
| 494 | test_suite* PiecewiseZeroSpreadedTermStructureTest::suite() { |
| 495 | auto* suite = BOOST_TEST_SUITE("Interpolated piecewise zero spreaded yield curve tests" ); |
| 496 | suite->add(QUANTLIB_TEST_CASE( |
| 497 | &PiecewiseZeroSpreadedTermStructureTest::testFlatInterpolationLeft)); |
| 498 | suite->add(QUANTLIB_TEST_CASE( |
| 499 | &PiecewiseZeroSpreadedTermStructureTest::testFlatInterpolationRight)); |
| 500 | suite->add(QUANTLIB_TEST_CASE( |
| 501 | &PiecewiseZeroSpreadedTermStructureTest::testLinearInterpolationMultipleSpreads)); |
| 502 | suite->add(QUANTLIB_TEST_CASE( |
| 503 | &PiecewiseZeroSpreadedTermStructureTest::testLinearInterpolation)); |
| 504 | suite->add(QUANTLIB_TEST_CASE( |
| 505 | &PiecewiseZeroSpreadedTermStructureTest::testBackwardFlatInterpolation)); |
| 506 | suite->add(QUANTLIB_TEST_CASE( |
| 507 | &PiecewiseZeroSpreadedTermStructureTest::testForwardFlatInterpolation)); |
| 508 | suite->add(QUANTLIB_TEST_CASE( |
| 509 | &PiecewiseZeroSpreadedTermStructureTest::testDefaultInterpolation)); |
| 510 | suite->add(QUANTLIB_TEST_CASE( |
| 511 | &PiecewiseZeroSpreadedTermStructureTest::testSetInterpolationFactory)); |
| 512 | suite->add(QUANTLIB_TEST_CASE(&PiecewiseZeroSpreadedTermStructureTest::testMaxDate)); |
| 513 | suite->add(QUANTLIB_TEST_CASE(&PiecewiseZeroSpreadedTermStructureTest::testQuoteChanging)); |
| 514 | return suite; |
| 515 | } |
| 516 | |
| 517 | |