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 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776
|
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2007, 2009 Chris Kenyon
Copyright (C) 2008 Piero Del Boca
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it
under the terms of the QuantLib license. You should have received a
copy of the license along with this program; if not, please email
<quantlib-dev@lists.sf.net>. The license is also available online at
<http://quantlib.org/license.shtml>.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/
#include "toplevelfixture.hpp"
#include "utilities.hpp"
#include <ql/cashflows/indexedcashflow.hpp>
#include <ql/indexes/inflation/ukrpi.hpp>
#include <ql/indexes/inflation/euhicp.hpp>
#include <ql/indexes/inflation/ukhicp.hpp>
#include <ql/indexes/inflation/aucpi.hpp>
#include <ql/termstructures/inflation/piecewisezeroinflationcurve.hpp>
#include <ql/termstructures/inflation/piecewiseyoyinflationcurve.hpp>
#include <ql/termstructures/yield/flatforward.hpp>
#include <ql/time/date.hpp>
#include <ql/time/daycounters/actual360.hpp>
#include <ql/time/daycounters/thirty360.hpp>
#include <ql/time/calendars/unitedkingdom.hpp>
#include <ql/time/calendars/target.hpp>
#include <ql/time/schedule.hpp>
#include <ql/pricingengines/swap/discountingswapengine.hpp>
#include <ql/instruments/zerocouponinflationswap.hpp>
#include <ql/termstructures/inflation/inflationhelpers.hpp>
#include <ql/cashflows/yoyinflationcoupon.hpp>
#include <ql/cashflows/inflationcouponpricer.hpp>
#include <ql/cashflows/fixedratecoupon.hpp>
#include <ql/cashflows/zeroinflationcashflow.hpp>
#include <ql/instruments/yearonyearinflationswap.hpp>
#include <functional>
using boost::unit_test_framework::test_suite;
using namespace QuantLib;
using std::fabs;
using std::pow;
using std::vector;
BOOST_FIXTURE_TEST_SUITE(QuantLibTests, TopLevelFixture)
BOOST_AUTO_TEST_SUITE(InflationTests)
#undef REPORT_FAILURE
#define REPORT_FAILURE(d, res, periodName) \
BOOST_ERROR("wrong " << periodName << " inflation period for Date (1 " \
<< d << "), Start Date ( " \
<< res.first << "), End Date (" \
<< res.second << ")"); \
struct Datum {
Date date;
Rate rate;
};
ext::shared_ptr<YieldTermStructure> nominalTermStructure() {
Date evaluationDate(13, August, 2007);
return ext::shared_ptr<YieldTermStructure>(
new FlatForward(evaluationDate, 0.05, Actual360()));
}
template <class T>
std::vector<ext::shared_ptr<BootstrapHelper<T> > > makeHelpers(
const std::vector<Datum>& iiData,
const std::function<ext::shared_ptr<BootstrapHelper<T> >(const Handle<Quote>&, const Date&)>&
makeHelper) {
std::vector<ext::shared_ptr<BootstrapHelper<T> > > instruments;
for (Datum datum : iiData) {
Date maturity = datum.date;
Handle<Quote> quote(ext::shared_ptr<Quote>(new SimpleQuote(datum.rate / 100.0)));
auto anInstrument = makeHelper(quote, maturity);
instruments.push_back(anInstrument);
}
return instruments;
}
void checkSeasonality(const Handle<ZeroInflationTermStructure>& hz,
const ext::shared_ptr<ZeroInflationIndex>& ii) {
QL_REQUIRE(!hz->hasSeasonality(), "We require that the initially passed in term structure "
<< "does not have seasonality");
// Tolerance that we will use below when comparing projected index fixings
Rate tolerance = 1e-12;
Date trueBaseDate = inflationPeriod(hz->baseDate(), ii->frequency()).second;
Date seasonalityBaseDate(31, January, trueBaseDate.year());
// Create two different seasonality objects
// 1) Monthly seasonality with all elements equal to 1 <=> no seasonality
vector<Rate> seasonalityFactors(12, 1.0);
ext::shared_ptr<MultiplicativePriceSeasonality> unitSeasonality =
ext::make_shared<MultiplicativePriceSeasonality>(seasonalityBaseDate, Monthly, seasonalityFactors);
// 2) Seasonality with factors != 1.0
seasonalityFactors[0] = 1.003245;
seasonalityFactors[1] = 1.000000;
seasonalityFactors[2] = 0.999715;
seasonalityFactors[3] = 1.000495;
seasonalityFactors[4] = 1.000929;
seasonalityFactors[5] = 0.998687;
seasonalityFactors[6] = 0.995949;
seasonalityFactors[7] = 0.994682;
seasonalityFactors[8] = 0.995949;
seasonalityFactors[9] = 1.000519;
seasonalityFactors[10] = 1.003705;
seasonalityFactors[11] = 1.004186;
ext::shared_ptr<MultiplicativePriceSeasonality> nonUnitSeasonality =
ext::make_shared<MultiplicativePriceSeasonality>(seasonalityBaseDate, Monthly, seasonalityFactors);
// Create dates on which we will check fixings
vector<Date> fixingDates(12);
Date anchorDate(14, January, 2013);
for (Size i = 0; i < fixingDates.size(); ++i) {
fixingDates[i] = anchorDate + i * Months;
}
// Projected inflation index fixings when there is no seasonality
vector<Rate> noSeasonalityFixings(12, 1.0);
for (Size i = 0; i < fixingDates.size(); ++i) {
noSeasonalityFixings[i] = ii->fixing(fixingDates[i], true);
}
// Set seasonality of all 1's and get the projected index fixings
hz->setSeasonality(unitSeasonality);
vector<Rate> unitSeasonalityFixings(12, 1.0);
for (Size i = 0; i < fixingDates.size(); ++i) {
unitSeasonalityFixings[i] = ii->fixing(fixingDates[i], true);
}
// Check that the unit seasonality fixings agree with the no seasonality fixings
for (Size i = 0; i < fixingDates.size(); i++) {
if (fabs(noSeasonalityFixings[i] - unitSeasonalityFixings[i]) > tolerance) {
BOOST_ERROR("Seasonality doesn't work correctly when seasonality factors are set = 1"
<< "No seasonality fixing is: " << noSeasonalityFixings[i]
<< " but unit seasonality fixing is: " << unitSeasonalityFixings[i]
<< " for fixing date " << io::iso_date(fixingDates[i]));
}
}
// Testing seasonality correction when seasonality factors are different from 1
// We expect to see that I_{SA}(t) = I_{NSA}(t) * S(t) / S(t_b)
Month baseCpiMonth = hz->baseDate().month();
Size baseCpiIndex = static_cast<Size>(baseCpiMonth) - 1;
Rate baseSeasonality = seasonalityFactors[baseCpiIndex];
// These are the expected fixings
vector<Rate> expectedSeasonalityFixings(12, 1.0);
for (Size i = 0; i < expectedSeasonalityFixings.size(); ++i) {
expectedSeasonalityFixings[i] =
ii->fixing(fixingDates[i], true) * seasonalityFactors[i] / baseSeasonality;
}
// Set the seasonality and calculate the actual seasonally adjusted fixings
hz->setSeasonality(nonUnitSeasonality);
vector<Rate> nonUnitSeasonalityFixings(12, 1.0);
for (Size i = 0; i < fixingDates.size(); ++i) {
nonUnitSeasonalityFixings[i] = ii->fixing(fixingDates[i], true);
}
// Check that the calculated fixings agree with the expected fixings
for (Size i = 0; i < fixingDates.size(); i++) {
if (fabs(expectedSeasonalityFixings[i] - nonUnitSeasonalityFixings[i]) > tolerance) {
BOOST_ERROR("Seasonality doesn't work correctly for non-unit seasonality factors."
<< " Expected fixing is: " << expectedSeasonalityFixings[i]
<< " but calculated fixing is: " << nonUnitSeasonalityFixings[i]
<< " for fixing date " << io::iso_date(fixingDates[i]));
}
}
// Testing that unsetting seasonality works also
hz->setSeasonality({});
vector<Rate> unsetSeasonalityFixings(12, 1.0);
for (Size i = 0; i < fixingDates.size(); ++i) {
unsetSeasonalityFixings[i] = ii->fixing(fixingDates[i], true);
}
// Check that seasonality has been unset by comparing with the no seasonality fixings
for (Size i = 0; i < fixingDates.size(); i++) {
if (fabs(noSeasonalityFixings[i] - unsetSeasonalityFixings[i]) > tolerance) {
BOOST_ERROR("Unsetting seasonality doesn't work correctly."
<< " No seasonality fixing is: " << noSeasonalityFixings[i]
<< " but after unsetting seasonality fixing is: " << unitSeasonalityFixings[i]
<< " for fixing date " << io::iso_date(fixingDates[i]));
}
}
}
//===========================================================================================
// zero inflation tests, index, termstructure, and swaps
//===========================================================================================
BOOST_AUTO_TEST_CASE(testZeroIndex) {
BOOST_TEST_MESSAGE("Testing zero inflation indices...");
EUHICP euhicp;
if (euhicp.name() != "EU HICP"
|| euhicp.frequency() != Monthly
|| euhicp.revised()
|| euhicp.availabilityLag() != 1*Months) {
BOOST_ERROR("wrong EU HICP data ("
<< euhicp.name() << ", "
<< euhicp.frequency() << ", "
<< euhicp.revised() << ", "
<< euhicp.availabilityLag() << ")");
}
UKRPI ukrpi;
if (ukrpi.name() != "UK RPI"
|| ukrpi.frequency() != Monthly
|| ukrpi.revised()
|| ukrpi.availabilityLag() != 1*Months) {
BOOST_ERROR("wrong UK RPI data ("
<< ukrpi.name() << ", "
<< ukrpi.frequency() << ", "
<< ukrpi.revised() << ", "
<< ukrpi.availabilityLag() << ")");
}
UKHICP ukhicp;
if (ukhicp.name() != "UK HICP"
|| ukhicp.frequency() != Monthly
|| ukhicp.revised()
|| ukhicp.availabilityLag() != 1 * Months) {
BOOST_ERROR("wrong UK HICP data ("
<< ukhicp.name() << ", "
<< ukhicp.frequency() << ", "
<< ukhicp.revised() << ", "
<< ", " << ukhicp.availabilityLag() << ")");
}
// Retrieval test.
//----------------
// make sure of the evaluation date
Date evaluationDate(13, August, 2007);
evaluationDate = UnitedKingdom().adjust(evaluationDate);
Settings::instance().evaluationDate() = evaluationDate;
// fixing data
Date from(1, January, 2005);
Date to(1, August, 2007);
Schedule rpiSchedule =
MakeSchedule()
.from(from)
.to(to)
.withFrequency(Monthly);
Real fixData[] = {
189.9, 189.9, 189.6, 190.5, 191.6, 192.0,
192.2, 192.2, 192.6, 193.1, 193.3, 193.6,
194.1, 193.4, 194.2, 195.0, 196.5, 197.7,
198.5, 198.5, 199.2, 200.1, 200.4, 201.1,
202.7, 201.6, 203.1, 204.4, 205.4, 206.2,
207.3, 206.1
};
auto iir = ext::make_shared<UKRPI>();
for (Size i=0; i<std::size(fixData); i++) {
iir->addFixing(rpiSchedule[i], fixData[i]);
}
if (iir->lastFixingDate() != to)
BOOST_ERROR("Wrong last fixing date for UK RPI"
<< "\n calculated: " << iir->lastFixingDate()
<< "\n expected: " << to);
Date todayMinusLag = evaluationDate - iir->availabilityLag();
std::pair<Date,Date> lim = inflationPeriod(todayMinusLag, iir->frequency());
todayMinusLag = lim.first;
Real eps = 1.0e-8;
// -1 because last value not yet available,
// (no TS so can't forecast).
for (Size i=0; i<rpiSchedule.size()-1;i++) {
std::pair<Date,Date> lim = inflationPeriod(rpiSchedule[i],
iir->frequency());
for (Date d=lim.first; d<=lim.second; d++) {
if (d < inflationPeriod(todayMinusLag,iir->frequency()).first) {
if (std::fabs(iir->fixing(d) - fixData[i]) > eps)
BOOST_ERROR("Fixings not constant within a period: "
<< iir->fixing(d)
<< ", should be " << fixData[i]);
}
}
}
AUCPI aucpi(Quarterly, false);
aucpi.addFixing(Date(15, December, 2007), 100.0);
Date expected(1, October, 2007);
if (aucpi.lastFixingDate() != expected)
BOOST_ERROR("Wrong last fixing date for quarterly AU CPI"
<< "\n calculated: " << aucpi.lastFixingDate()
<< "\n expected: " << expected);
}
BOOST_AUTO_TEST_CASE(testZeroTermStructure) {
BOOST_TEST_MESSAGE("Testing zero inflation term structure...");
// try the Zero UK
Calendar calendar = UnitedKingdom();
BusinessDayConvention bdc = ModifiedFollowing;
Date evaluationDate(13, August, 2007);
evaluationDate = calendar.adjust(evaluationDate);
Settings::instance().evaluationDate() = evaluationDate;
// fixing data
Date from(1, January, 2005);
Date to(1, July, 2007);
Schedule rpiSchedule =
MakeSchedule().from(from).to(to)
.withFrequency(Monthly);
Real fixData[] = {
189.9, 189.9, 189.6, 190.5, 191.6, 192.0,
192.2, 192.2, 192.6, 193.1, 193.3, 193.6,
194.1, 193.4, 194.2, 195.0, 196.5, 197.7,
198.5, 198.5, 199.2, 200.1, 200.4, 201.1,
202.7, 201.6, 203.1, 204.4, 205.4, 206.2,
207.3};
RelinkableHandle<ZeroInflationTermStructure> hz;
auto ii = ext::make_shared<UKRPI>(hz);
for (Size i=0; i<std::size(fixData); i++) {
ii->addFixing(rpiSchedule[i], fixData[i]);
}
Handle<YieldTermStructure> nominalTS(nominalTermStructure());
// now build the zero inflation curve
std::vector<Datum> zcData = {
{ Date(13, August, 2008), 2.93 },
{ Date(13, August, 2009), 2.95 },
{ Date(13, August, 2010), 2.965 },
{ Date(15, August, 2011), 2.98 },
{ Date(13, August, 2012), 3.0 },
{ Date(13, August, 2014), 3.06 },
{ Date(13, August, 2017), 3.175 },
{ Date(13, August, 2019), 3.243 },
{ Date(15, August, 2022), 3.293 },
{ Date(14, August, 2027), 3.338 },
{ Date(13, August, 2032), 3.348 },
{ Date(15, August, 2037), 3.348 },
{ Date(13, August, 2047), 3.308 },
{ Date(13, August, 2057), 3.228 }
};
Period observationLag = Period(3, Months);
DayCounter dc = Thirty360(Thirty360::BondBasis);
Frequency frequency = Monthly;
auto makeHelper = [&](const Handle<Quote>& quote, const Date& maturity) {
return ext::make_shared<ZeroCouponInflationSwapHelper>(
quote, observationLag, maturity, calendar, bdc, dc, ii, CPI::AsIndex);
};
auto helpers = makeHelpers<ZeroInflationTermStructure>(zcData, makeHelper);
Date baseDate = ii->lastFixingDate();
ext::shared_ptr<PiecewiseZeroInflationCurve<Linear> > pZITS =
ext::make_shared<PiecewiseZeroInflationCurve<Linear>>(
evaluationDate, baseDate, frequency, dc, helpers);
hz.linkTo(pZITS);
//===========================================================================================
// first check that the quoted swaps are repriced correctly
const Real eps = 1.0e-7;
auto engine = ext::make_shared<DiscountingSwapEngine>(nominalTS);
for (const auto& datum: zcData) {
ZeroCouponInflationSwap nzcis(Swap::Payer,
1000000.0,
evaluationDate,
datum.date,
calendar, bdc, dc,
datum.rate/100.0,
ii, observationLag,
CPI::AsIndex);
nzcis.setPricingEngine(engine);
BOOST_CHECK_MESSAGE(std::fabs(nzcis.NPV()) < eps,
"zero-coupon inflation swap does not reprice to zero"
<< "\n NPV: " << nzcis.NPV()
<< "\n maturity: " << nzcis.maturityDate()
<< "\n rate: " << datum.rate/100.0);
}
//===========================================================================================
// now test the forecasting capability of the index.
from = hz->referenceDate();
to = hz->maxDate()-1*Months; // a bit of margin for adjustments
Schedule testIndex = MakeSchedule().from(from).to(to)
.withTenor(1*Months)
.withCalendar(UnitedKingdom())
.withConvention(ModifiedFollowing);
// we are testing UKRPI which is not interpolated
Date bd = hz->baseDate();
Real bf = ii->fixing(bd);
for (const auto& d : testIndex) {
Real z = hz->zeroRate(d, Period(0, Days));
Real t = hz->dayCounter().yearFraction(bd, inflationPeriod(d, ii->frequency()).first);
Real calc = bf * std::pow(1+z, t);
if (t<=0)
calc = ii->fixing(d,false); // still historical
if (std::fabs(calc - ii->fixing(d,true)) > eps)
BOOST_ERROR("inflation index does not forecast correctly"
<< "\n date: " << d
<< "\n base date: " << bd
<< "\n base fixing: " << bf
<< "\n expected: " << calc
<< "\n forecast: " << ii->fixing(d,true));
}
//===========================================================================================
// Add a seasonality correction. The curve should recalculate and still reprice the swaps.
Date nextBaseDate = inflationPeriod(hz->baseDate(), ii->frequency()).second;
Date seasonalityBaseDate(31, January, nextBaseDate.year());
vector<Rate> seasonalityFactors = {
1.003245,
1.000000,
0.999715,
1.000495,
1.000929,
0.998687,
0.995949,
0.994682,
0.995949,
1.000519,
1.003705,
1.004186
};
ext::shared_ptr<MultiplicativePriceSeasonality> nonUnitSeasonality =
ext::make_shared<MultiplicativePriceSeasonality>(seasonalityBaseDate, Monthly, seasonalityFactors);
pZITS->setSeasonality(nonUnitSeasonality);
for (const auto& datum: zcData) {
ZeroCouponInflationSwap nzcis(Swap::Payer,
1000000.0,
evaluationDate,
datum.date,
calendar, bdc, dc,
datum.rate/100.0,
ii, observationLag,
CPI::AsIndex);
nzcis.setPricingEngine(engine);
BOOST_CHECK_MESSAGE(std::fabs(nzcis.NPV()) < eps,
"zero-coupon inflation swap does not reprice to zero"
<< "\n NPV: " << nzcis.NPV()
<< "\n maturity: " << nzcis.maturityDate()
<< "\n rate: " << datum.rate);
}
// remove circular refernce
hz.reset();
}
QL_DEPRECATED_DISABLE_WARNING
BOOST_AUTO_TEST_CASE(testZeroTermStructureWithNominalCurve) {
BOOST_TEST_MESSAGE("Testing zero inflation term structure passing nominal curve to helpers...");
// try the Zero UK
Calendar calendar = UnitedKingdom();
BusinessDayConvention bdc = ModifiedFollowing;
Date evaluationDate(13, August, 2007);
evaluationDate = calendar.adjust(evaluationDate);
Settings::instance().evaluationDate() = evaluationDate;
// fixing data
Date from(1, January, 2005);
Date to(1, July, 2007);
Schedule rpiSchedule =
MakeSchedule().from(from).to(to)
.withFrequency(Monthly);
Real fixData[] = {
189.9, 189.9, 189.6, 190.5, 191.6, 192.0,
192.2, 192.2, 192.6, 193.1, 193.3, 193.6,
194.1, 193.4, 194.2, 195.0, 196.5, 197.7,
198.5, 198.5, 199.2, 200.1, 200.4, 201.1,
202.7, 201.6, 203.1, 204.4, 205.4, 206.2,
207.3};
RelinkableHandle<ZeroInflationTermStructure> hz;
auto ii = ext::make_shared<UKRPI>(hz);
for (Size i=0; i<std::size(fixData); i++) {
ii->addFixing(rpiSchedule[i], fixData[i]);
}
Handle<YieldTermStructure> nominalTS(nominalTermStructure());
// now build the zero inflation curve
std::vector<Datum> zcData = {
{ Date(13, August, 2008), 2.93 },
{ Date(13, August, 2009), 2.95 },
{ Date(13, August, 2010), 2.965 },
{ Date(15, August, 2011), 2.98 },
{ Date(13, August, 2012), 3.0 },
{ Date(13, August, 2014), 3.06 },
{ Date(13, August, 2017), 3.175 },
{ Date(13, August, 2019), 3.243 },
{ Date(15, August, 2022), 3.293 },
{ Date(14, August, 2027), 3.338 },
{ Date(13, August, 2032), 3.348 },
{ Date(15, August, 2037), 3.348 },
{ Date(13, August, 2047), 3.308 },
{ Date(13, August, 2057), 3.228 }
};
Period observationLag = Period(3, Months);
DayCounter dc = Thirty360(Thirty360::BondBasis);
Frequency frequency = Monthly;
auto makeHelper = [&](const Handle<Quote>& quote, const Date& maturity) {
return ext::shared_ptr<ZeroCouponInflationSwapHelper>( // NOLINT(modernize-make-shared)
new ZeroCouponInflationSwapHelper(
quote, observationLag, maturity, calendar, bdc, dc, ii, CPI::AsIndex, nominalTS));
};
auto helpers = makeHelpers<ZeroInflationTermStructure>(zcData, makeHelper);
Date baseDate = ii->lastFixingDate();
ext::shared_ptr<PiecewiseZeroInflationCurve<Linear> > pZITS =
ext::make_shared<PiecewiseZeroInflationCurve<Linear>>(
evaluationDate, baseDate, frequency, dc, helpers);
hz.linkTo(pZITS);
//===========================================================================================
// first check that the quoted swaps are repriced correctly
const Real eps = 1.0e-7;
auto engine = ext::make_shared<DiscountingSwapEngine>(nominalTS);
for (const auto& datum: zcData) {
ZeroCouponInflationSwap nzcis(Swap::Payer,
1000000.0,
evaluationDate,
datum.date,
calendar, bdc, dc,
datum.rate/100.0,
ii, observationLag,
CPI::AsIndex);
nzcis.setPricingEngine(engine);
BOOST_CHECK_MESSAGE(std::fabs(nzcis.NPV()) < eps,
"zero-coupon inflation swap does not reprice to zero"
<< "\n NPV: " << nzcis.NPV()
<< "\n maturity: " << nzcis.maturityDate()
<< "\n rate: " << datum.rate/100.0);
}
//===========================================================================================
// now test the forecasting capability of the index.
from = hz->referenceDate();
to = hz->maxDate()-1*Months; // a bit of margin for adjustments
Schedule testIndex = MakeSchedule().from(from).to(to)
.withTenor(1*Months)
.withCalendar(UnitedKingdom())
.withConvention(ModifiedFollowing);
// we are testing UKRPI which is not interpolated
Date bd = hz->baseDate();
Real bf = ii->fixing(bd);
for (const auto& d : testIndex) {
Real z = hz->zeroRate(d, Period(0, Days));
Real t = hz->dayCounter().yearFraction(bd, inflationPeriod(d, ii->frequency()).first);
Real calc = bf * std::pow(1+z, t);
if (t<=0)
calc = ii->fixing(d,false); // still historical
if (std::fabs(calc - ii->fixing(d,true)) > eps)
BOOST_ERROR("inflation index does not forecast correctly"
<< "\n date: " << d
<< "\n base date: " << bd
<< "\n base fixing: " << bf
<< "\n expected: " << calc
<< "\n forecast: " << ii->fixing(d,true));
}
//===========================================================================================
// Add a seasonality correction. The curve should recalculate and still reprice the swaps.
Date nextBaseDate = inflationPeriod(hz->baseDate(), ii->frequency()).second;
Date seasonalityBaseDate(31, January, nextBaseDate.year());
vector<Rate> seasonalityFactors = {
1.003245,
1.000000,
0.999715,
1.000495,
1.000929,
0.998687,
0.995949,
0.994682,
0.995949,
1.000519,
1.003705,
1.004186
};
ext::shared_ptr<MultiplicativePriceSeasonality> nonUnitSeasonality =
ext::make_shared<MultiplicativePriceSeasonality>(seasonalityBaseDate, Monthly, seasonalityFactors);
pZITS->setSeasonality(nonUnitSeasonality);
for (const auto& datum: zcData) {
ZeroCouponInflationSwap nzcis(Swap::Payer,
1000000.0,
evaluationDate,
datum.date,
calendar, bdc, dc,
datum.rate/100.0,
ii, observationLag,
CPI::AsIndex);
nzcis.setPricingEngine(engine);
BOOST_CHECK_MESSAGE(std::fabs(nzcis.NPV()) < eps,
"zero-coupon inflation swap does not reprice to zero"
<< "\n NPV: " << nzcis.NPV()
<< "\n maturity: " << nzcis.maturityDate()
<< "\n rate: " << datum.rate);
}
// remove circular refernce
hz.reset();
}
QL_DEPRECATED_ENABLE_WARNING
BOOST_AUTO_TEST_CASE(testSeasonalityCorrection) {
BOOST_TEST_MESSAGE("Testing seasonality correction on zero inflation term structure...");
// try the Zero UK
Calendar calendar = UnitedKingdom();
Date evaluationDate(13, August, 2007);
evaluationDate = calendar.adjust(evaluationDate);
Settings::instance().evaluationDate() = evaluationDate;
// fixing data
Date from(1, January, 2005);
Date to(13, August, 2007);
Schedule rpiSchedule = MakeSchedule().from(from).to(to)
.withTenor(1*Months)
.withCalendar(UnitedKingdom())
.withConvention(ModifiedFollowing);
Real fixData[] = { 189.9, 189.9, 189.6, 190.5, 191.6, 192.0,
192.2, 192.2, 192.6, 193.1, 193.3, 193.6,
194.1, 193.4, 194.2, 195.0, 196.5, 197.7,
198.5, 198.5, 199.2, 200.1, 200.4, 201.1,
202.7, 201.6, 203.1, 204.4, 205.4, 206.2,
207.3};
RelinkableHandle<ZeroInflationTermStructure> hz;
auto ii = ext::make_shared<UKRPI>(hz);
for (Size i=0; i<std::size(fixData); i++) {
ii->addFixing(rpiSchedule[i], fixData[i]);
}
ext::shared_ptr<YieldTermStructure> nominalTS = nominalTermStructure();
std::vector<Date> nodes = {
Date(1, June, 2007),
Date(1, June, 2008),
Date(1, June, 2009),
Date(1, June, 2010),
Date(1, June, 2011),
Date(1, June, 2012),
Date(1, June, 2014),
Date(1, June, 2017),
Date(1, June, 2019),
Date(1, June, 2022),
Date(1, June, 2027),
Date(1, June, 2032),
Date(1, June, 2037),
Date(1, June, 2047),
Date(1, June, 2057)
};
std::vector<Rate> rates = {
0.0293,
0.0293,
0.0295,
0.02965,
0.0298,
0.03,
0.0306,
0.03175,
0.03243,
0.03293,
0.03338,
0.03348,
0.03348,
0.03308,
0.03228
};
DayCounter dc = Thirty360(Thirty360::BondBasis);
Frequency frequency = Monthly;
auto zeroCurve = ext::make_shared<InterpolatedZeroInflationCurve<Linear>>(
evaluationDate, nodes, rates, frequency, dc);
hz.linkTo(zeroCurve);
// Perform checks on the seasonality for this non-interpolated index
checkSeasonality(hz, ii);
}
BOOST_AUTO_TEST_CASE(testZeroIndexFutureFixing) {
BOOST_TEST_MESSAGE("Testing that zero inflation indices forecast future fixings...");
// we create an index without a term structure, so
// it won't be able to forecast fixings
EUHICP euhicp;
// let's say we're at some point in April 2024...
Settings::instance().evaluationDate() = {10, April, 2024};
// ..and the last available fixing is February 2024, we don't have March yet
euhicp.addFixing({1,December,2023}, 100.0);
euhicp.addFixing({1,January,2024}, 100.1);
euhicp.addFixing({1,February,2024}, 100.2);
// Asking for the February fixing works, it's stored
Real fixing = euhicp.fixing({1,February,2024});
Real expected = 100.2;
if (std::fabs(fixing - expected) > 1e-12)
BOOST_ERROR("Failed to retrieve correct fixing: "
<< "\n returned: " << fixing
<< "\n expected: " << expected);
// Asking for the March fixing doesn't (because we can't forecast)
BOOST_CHECK_EXCEPTION(euhicp.fixing({1,March,2024}), Error,
ExpectedErrorMessage("empty Handle"));
// but it works once it gets published:
euhicp.addFixing({1,March,2024}, 100.3);
fixing = euhicp.fixing({1,March,2024});
expected = 100.3;
if (std::fabs(fixing - expected) > 1e-12)
BOOST_ERROR("Failed to retrieve correct fixing: "
<< "\n returned: " << fixing
<< "\n expected: " << expected);
// On the other hand, April would be forecast...
BOOST_CHECK_EXCEPTION(euhicp.fixing({1,April,2024}), Error,
ExpectedErrorMessage("empty Handle"));
// ...even if it's stored:
euhicp.addFixing({1,April,2024}, 100.4);
BOOST_CHECK_EXCEPTION(euhicp.fixing({1,April,2024}), Error,
ExpectedErrorMessage("empty Handle"));
}
BOOST_AUTO_TEST_CASE(testInterpolatedZeroTermStructure) {
BOOST_TEST_MESSAGE("Testing interpolated zero-rate inflation curve...");
Date today = Date(27, January, 2022);
Settings::instance().evaluationDate() = today;
Date baseDate = Date(1, December, 2021);
std::vector<Date> dates = {
baseDate,
today + 7 * Days,
today + 14 * Days,
today + 1 * Months,
today + 2 * Months,
today + 3 * Months,
today + 6 * Months,
today + 1 * Years,
today + 2 * Years,
today + 5 * Years,
today + 10 * Years
};
std::vector<Rate> rates = { 0.01, 0.01, 0.011, 0.012, 0.013, 0.015, 0.018, 0.02, 0.025, 0.03, 0.03 };
auto curve = ext::make_shared<InterpolatedZeroInflationCurve<Linear>>(
today, dates, rates, Monthly, Actual360());
auto nodes = curve->nodes();
BOOST_CHECK_MESSAGE(nodes.size() == dates.size(), "different number of nodes and input dates");
for (Size i=0; i<dates.size(); ++i) {
BOOST_CHECK_MESSAGE(dates[i] == nodes[i].first,
"node " << i << " at " << nodes[i].first << "; " << dates[i] << " expected");
}
}
//===========================================================================================
// year on year tests, index, termstructure, and swaps
//===========================================================================================
BOOST_AUTO_TEST_CASE(testQuotedYYIndex) {
BOOST_TEST_MESSAGE("Testing quoted year-on-year inflation indices...");
QL_DEPRECATED_DISABLE_WARNING
YYEUHICP yyeuhicp(true);
if (yyeuhicp.name() != "EU YY_HICP"
|| yyeuhicp.frequency() != Monthly
|| yyeuhicp.revised()
|| !yyeuhicp.interpolated()
|| yyeuhicp.ratio()
|| yyeuhicp.availabilityLag() != 1*Months) {
BOOST_ERROR("wrong year-on-year EU HICP data ("
<< yyeuhicp.name() << ", "
<< yyeuhicp.frequency() << ", "
<< yyeuhicp.revised() << ", "
<< yyeuhicp.interpolated() << ", "
<< yyeuhicp.ratio() << ", "
<< yyeuhicp.availabilityLag() << ")");
}
QL_DEPRECATED_ENABLE_WARNING
YYUKRPI yyukrpi;
if (yyukrpi.name() != "UK YY_RPI"
|| yyukrpi.frequency() != Monthly
|| yyukrpi.revised()
|| yyukrpi.interpolated()
|| yyukrpi.ratio()
|| yyukrpi.availabilityLag() != 1*Months) {
BOOST_ERROR("wrong year-on-year UK RPI data ("
<< yyukrpi.name() << ", "
<< yyukrpi.frequency() << ", "
<< yyukrpi.revised() << ", "
<< yyukrpi.interpolated() << ", "
<< yyukrpi.ratio() << ", "
<< yyukrpi.availabilityLag() << ")");
}
}
BOOST_AUTO_TEST_CASE(testQuotedYYIndexFutureFixing) {
BOOST_TEST_MESSAGE("Testing that quoted year-on-year inflation indices forecast future fixings...");
// we create indexes without a term structure, so
// they won't be able to forecast fixings
YYEUHICP quoted_flat;
QL_DEPRECATED_DISABLE_WARNING
YYEUHICP quoted_linear(true);
QL_DEPRECATED_ENABLE_WARNING
// let's say we're at some point in April 2024...
Settings::instance().evaluationDate() = {10, April, 2024};
// ..and the last available fixing is February 2024, we don't have March yet
quoted_flat.addFixing({1,December,2023}, 100.0);
quoted_flat.addFixing({1,January,2024}, 100.1);
quoted_flat.addFixing({1,February,2024}, 100.2);
BOOST_CHECK_EQUAL(quoted_flat.lastFixingDate(), Date(1,February,2024));
BOOST_CHECK_EQUAL(quoted_linear.lastFixingDate(), Date(1,February,2024));
// mid-January fixing: ok for both flat and interpolated
BOOST_CHECK_NO_THROW(quoted_flat.fixing({15,January,2024}));
BOOST_CHECK_NO_THROW(quoted_linear.fixing({15,January,2024}));
// mid-February fixing: ok for flat, interpolated needs March
BOOST_CHECK_NO_THROW(quoted_flat.fixing({15,February,2024}));
BOOST_CHECK_EXCEPTION(quoted_linear.fixing({15,February,2024}), Error,
ExpectedErrorMessage("empty Handle"));
// but February 1st works (special case, March would have null
// weight in the interpolation)
BOOST_CHECK_NO_THROW(quoted_linear.fixing({1,February,2024}));
// both ok after March is published:
quoted_flat.addFixing({1,March,2024}, 100.3);
BOOST_CHECK_EQUAL(quoted_flat.lastFixingDate(), Date(1,March,2024));
BOOST_CHECK_EQUAL(quoted_linear.lastFixingDate(), Date(1,March,2024));
BOOST_CHECK_NO_THROW(quoted_flat.fixing({15,February,2024}));
BOOST_CHECK_NO_THROW(quoted_linear.fixing({15,February,2024}));
// April can't be available now, both fail even if it's stored:
quoted_flat.addFixing({1,April,2024}, 100.4);
BOOST_CHECK_EXCEPTION(quoted_flat.fixing({1,April,2024}), Error,
ExpectedErrorMessage("empty Handle"));
BOOST_CHECK_EXCEPTION(quoted_linear.fixing({1,April,2024}), Error,
ExpectedErrorMessage("empty Handle"));
}
BOOST_AUTO_TEST_CASE(testRatioYYIndex) {
BOOST_TEST_MESSAGE("Testing ratio year-on-year inflation indices...");
auto euhicp = ext::make_shared<EUHICP>();
auto ukrpi = ext::make_shared<UKRPI>();
QL_DEPRECATED_DISABLE_WARNING
YoYInflationIndex yyeuhicpr(euhicp, true);
QL_DEPRECATED_ENABLE_WARNING
if (yyeuhicpr.name() != "EU YYR_HICP"
|| yyeuhicpr.frequency() != Monthly
|| yyeuhicpr.revised()
|| !yyeuhicpr.interpolated()
|| !yyeuhicpr.ratio()
|| yyeuhicpr.availabilityLag() != 1*Months) {
BOOST_ERROR("wrong year-on-year EU HICPr data ("
<< yyeuhicpr.name() << ", "
<< yyeuhicpr.frequency() << ", "
<< yyeuhicpr.revised() << ", "
<< yyeuhicpr.interpolated() << ", "
<< yyeuhicpr.ratio() << ", "
<< yyeuhicpr.availabilityLag() << ")");
}
YoYInflationIndex yyukrpir(ukrpi);
if (yyukrpir.name() != "UK YYR_RPI"
|| yyukrpir.frequency() != Monthly
|| yyukrpir.revised()
|| yyukrpir.interpolated()
|| !yyukrpir.ratio()
|| yyukrpir.availabilityLag() != 1*Months) {
BOOST_ERROR("wrong year-on-year UK RPIr data ("
<< yyukrpir.name() << ", "
<< yyukrpir.frequency() << ", "
<< yyukrpir.revised() << ", "
<< yyukrpir.interpolated() << ", "
<< yyukrpir.ratio() << ", "
<< yyukrpir.availabilityLag() << ")");
}
// Retrieval test.
//----------------
// make sure of the evaluation date
Date evaluationDate(13, August, 2007);
evaluationDate = UnitedKingdom().adjust(evaluationDate);
Settings::instance().evaluationDate() = evaluationDate;
// fixing data
Date from(1, January, 2005);
Date to(13, August, 2007);
Schedule rpiSchedule = MakeSchedule().from(from).to(to)
.withTenor(1*Months)
.withCalendar(UnitedKingdom())
.withConvention(ModifiedFollowing);
Real fixData[] = { 189.9, 189.9, 189.6, 190.5, 191.6, 192.0,
192.2, 192.2, 192.6, 193.1, 193.3, 193.6,
194.1, 193.4, 194.2, 195.0, 196.5, 197.7,
198.5, 198.5, 199.2, 200.1, 200.4, 201.1,
202.7, 201.6, 203.1, 204.4, 205.4, 206.2,
207.3 };
for (Size i=0; i<std::size(fixData);i++) {
ukrpi->addFixing(rpiSchedule[i], fixData[i]);
}
auto iir = ext::make_shared<YoYInflationIndex>(ukrpi);
QL_DEPRECATED_DISABLE_WARNING
// NOLINTNEXTLINE(modernize-make-shared)
auto iirYES = ext::shared_ptr<YoYInflationIndex>(
new YoYInflationIndex(ukrpi, true));
QL_DEPRECATED_ENABLE_WARNING
Date todayMinusLag = evaluationDate - iir->availabilityLag();
std::pair<Date,Date> lim = inflationPeriod(todayMinusLag, iir->frequency());
todayMinusLag = lim.second + 1 - 2*Period(iir->frequency());
Real eps = 1.0e-8;
// Interpolation tests
//--------------------
// (no TS so can't forecast).
for (Size i=13; i<rpiSchedule.size();i++) {
std::pair<Date,Date> lim = inflationPeriod(rpiSchedule[i],
iir->frequency());
std::pair<Date,Date> limBef = inflationPeriod(rpiSchedule[i-12],
iir->frequency());
for (Date d=lim.first; d<=lim.second; d++) {
if (d < todayMinusLag) {
Rate expected = fixData[i]/fixData[i-12] - 1.0;
Rate calculated = iir->fixing(d);
BOOST_CHECK_MESSAGE(std::fabs(calculated - expected) < eps,
"Non-interpolated fixings not constant within a period: "
<< calculated
<< ", should be "
<< expected);
Real dp= lim.second + 1- lim.first;
Real dpBef=limBef.second + 1 - limBef.first;
Real dl = d-lim.first;
// potentially does not work on 29th Feb
Real dlBef = NullCalendar().advance(d, -1*Years, ModifiedFollowing)
-limBef.first;
Real linearNow = fixData[i] + (fixData[i+1]-fixData[i])*dl/dp;
Real linearBef = fixData[i-12] + (fixData[i+1-12]-fixData[i-12])*dlBef/dpBef;
Rate expectedYES = linearNow / linearBef - 1.0;
Rate calculatedYES = iirYES->fixing(d);
BOOST_CHECK_MESSAGE(fabs(expectedYES-calculatedYES)<eps,
"Error in interpolated fixings: expect "<<expectedYES
<<" see " << calculatedYES
<<" flat " << calculated
<<", data: "<< fixData[i-12] <<", "<< fixData[i+1-12]
<<", "<< fixData[i] <<", "<< fixData[i+1]
<<", fac: "<< dp <<", "<< dl
<<", "<< dpBef <<", "<< dlBef
<<", to: "<<linearNow<<", "<<linearBef
);
}
}
}
}
BOOST_AUTO_TEST_CASE(testRatioYYIndexFutureFixing) {
BOOST_TEST_MESSAGE("Testing that ratio year-on-year inflation indices forecast future fixings...");
// we create indexes without a term structure, so
// they won't be able to forecast fixings
auto euhicp = ext::make_shared<EUHICP>();
YoYInflationIndex ratio_flat(euhicp);
QL_DEPRECATED_DISABLE_WARNING
YoYInflationIndex ratio_linear(euhicp, true);
QL_DEPRECATED_ENABLE_WARNING
// let's say we're at some point in April 2024...
Settings::instance().evaluationDate() = {10, April, 2024};
// ..and the last available fixing is February 2024, we don't have March yet
euhicp->addFixing({1,December,2022}, 98.0);
euhicp->addFixing({1,January,2023}, 98.1);
euhicp->addFixing({1,February,2023}, 98.2);
euhicp->addFixing({1,March,2023}, 98.3);
// ...
euhicp->addFixing({1,December,2023}, 100.0);
euhicp->addFixing({1,January,2024}, 100.1);
euhicp->addFixing({1,February,2024}, 100.2);
BOOST_CHECK_EQUAL(ratio_flat.lastFixingDate(), Date(1,February,2024));
BOOST_CHECK_EQUAL(ratio_linear.lastFixingDate(), Date(1,February,2024));
// mid-January fixing: ok for both flat and interpolated
BOOST_CHECK_NO_THROW(ratio_flat.fixing({15,January,2024}));
BOOST_CHECK_NO_THROW(ratio_linear.fixing({15,January,2024}));
// mid-February fixing: ok for flat, interpolated needs March
BOOST_CHECK_NO_THROW(ratio_flat.fixing({15,February,2024}));
BOOST_CHECK_EXCEPTION(ratio_linear.fixing({15,February,2024}), Error,
ExpectedErrorMessage("empty Handle"));
// but February 1st works (special case, March would have null
// weight in the interpolation)
BOOST_CHECK_NO_THROW(ratio_linear.fixing({1,February,2024}));
// both ok after March is published:
euhicp->addFixing({1,March,2024}, 100.3);
BOOST_CHECK_EQUAL(ratio_flat.lastFixingDate(), Date(1,March,2024));
BOOST_CHECK_EQUAL(ratio_linear.lastFixingDate(), Date(1,March,2024));
BOOST_CHECK_NO_THROW(ratio_flat.fixing({15,February,2024}));
BOOST_CHECK_NO_THROW(ratio_linear.fixing({15,February,2024}));
// April can't be available now, both fail even if it's stored:
euhicp->addFixing({1,April,2024}, 100.4);
BOOST_CHECK_EXCEPTION(ratio_flat.fixing({1,April,2024}), Error,
ExpectedErrorMessage("empty Handle"));
BOOST_CHECK_EXCEPTION(ratio_linear.fixing({1,April,2024}), Error,
ExpectedErrorMessage("empty Handle"));
}
BOOST_AUTO_TEST_CASE(testYYTermStructure) {
BOOST_TEST_MESSAGE("Testing year-on-year inflation term structure...");
// try the YY UK
Calendar calendar = UnitedKingdom();
BusinessDayConvention bdc = ModifiedFollowing;
Date evaluationDate(13, August, 2007);
evaluationDate = calendar.adjust(evaluationDate);
Settings::instance().evaluationDate() = evaluationDate;
// fixing data
Date from(1, January, 2005);
Date to(1, July, 2007);
Schedule rpiSchedule = MakeSchedule().from(from).to(to)
.withTenor(1*Months)
.withCalendar(UnitedKingdom())
.withConvention(ModifiedFollowing);
Real fixData[] = {
189.9, 189.9, 189.6, 190.5, 191.6, 192.0,
192.2, 192.2, 192.6, 193.1, 193.3, 193.6,
194.1, 193.4, 194.2, 195.0, 196.5, 197.7,
198.5, 198.5, 199.2, 200.1, 200.4, 201.1,
202.7, 201.6, 203.1, 204.4, 205.4, 206.2,
207.3
};
RelinkableHandle<YoYInflationTermStructure> hy;
auto rpi = ext::make_shared<UKRPI>();
auto iir = ext::make_shared<YoYInflationIndex>(rpi, hy);
for (Size i=0; i<std::size(fixData); i++) {
rpi->addFixing(rpiSchedule[i], fixData[i]);
}
ext::shared_ptr<YieldTermStructure> nominalTS = nominalTermStructure();
// now build the YoY inflation curve
std::vector<Datum> yyData = {
{ Date(13, August, 2008), 2.95 },
{ Date(13, August, 2009), 2.95 },
{ Date(13, August, 2010), 2.93 },
{ Date(15, August, 2011), 2.955 },
{ Date(13, August, 2012), 2.945 },
{ Date(13, August, 2013), 2.985 },
{ Date(13, August, 2014), 3.01 },
{ Date(13, August, 2015), 3.035 },
{ Date(13, August, 2016), 3.055 }, // note that
{ Date(13, August, 2017), 3.075 }, // some dates will be on
{ Date(13, August, 2019), 3.105 }, // holidays but the payment
{ Date(15, August, 2022), 3.135 }, // calendar will roll them
{ Date(13, August, 2027), 3.155 },
{ Date(13, August, 2032), 3.145 },
{ Date(13, August, 2037), 3.145 }
};
Period observationLag = Period(2,Months);
DayCounter dc = Thirty360(Thirty360::BondBasis);
// now build the helpers ...
auto makeHelper = [&](const Handle<Quote>& quote, const Date& maturity) {
return ext::make_shared<YearOnYearInflationSwapHelper>(
quote, observationLag, maturity, calendar, bdc, dc, iir, CPI::AsIndex,
Handle<YieldTermStructure>(nominalTS));
};
auto helpers = makeHelpers<YoYInflationTermStructure>(yyData, makeHelper);
Date baseDate = rpi->lastFixingDate();
Rate baseYYRate = yyData[0].rate/100.0;
auto pYYTS =
ext::make_shared<PiecewiseYoYInflationCurve<Linear>>(
evaluationDate, baseDate, baseYYRate,
iir->frequency(), dc, helpers);
// validation
// yoy swaps should reprice to zero
// yy rates should not equal yySwap rates
Real eps = 0.000001;
// usual swap engine
Handle<YieldTermStructure> hTS(nominalTS);
ext::shared_ptr<PricingEngine> sppe(new DiscountingSwapEngine(hTS));
// make sure that the index has the latest yoy term structure
hy.linkTo(pYYTS);
for (Size j = 1; j < yyData.size(); j++) {
from = nominalTS->referenceDate();
to = yyData[j].date;
Schedule yoySchedule = MakeSchedule().from(from).to(to)
.withConvention(Unadjusted) // fixed leg gets calendar from
.withCalendar(calendar) // schedule
.withTenor(1*Years)
.backwards()
;
YearOnYearInflationSwap yyS2(Swap::Payer,
1000000.0,
yoySchedule,//fixed schedule, but same as yoy
yyData[j].rate/100.0,
dc,
yoySchedule,
iir,
observationLag,
CPI::Flat,
0.0, //spread on index
dc,
UnitedKingdom());
yyS2.setPricingEngine(sppe);
BOOST_CHECK_MESSAGE(fabs(yyS2.NPV())<eps,"fresh yoy swap NPV!=0 from TS "
<<"swap quote for pt " << j
<< ", is " << yyData[j].rate/100.0
<<" vs YoY rate "<< pYYTS->yoyRate(yyData[j].date-observationLag)
<<" at quote date "<<(yyData[j].date-observationLag)
<<", NPV of a fresh yoy swap is " << yyS2.NPV()
<<"\n fair rate " << yyS2.fairRate()
<<" payment "<<yyS2.paymentConvention());
}
Size jj=3;
for (Size k = 0; k < 14; k++) {
from = nominalTS->referenceDate() - k*Months;
to = yyData[jj].date - k*Months;
Schedule yoySchedule = MakeSchedule().from(from).to(to)
.withConvention(Unadjusted) // fixed leg gets calendar from
.withCalendar(calendar) // schedule
.withTenor(1*Years)
.backwards()
;
YearOnYearInflationSwap yyS3(Swap::Payer,
1000000.0,
yoySchedule,//fixed schedule, but same as yoy
yyData[jj].rate/100.0,
dc,
yoySchedule,
iir,
observationLag,
CPI::Flat,
0.0, //spread on index
dc,
UnitedKingdom());
yyS3.setPricingEngine(sppe);
BOOST_CHECK_MESSAGE(fabs(yyS3.NPV())< 20000.0,
"unexpected size of aged YoY swap, aged "
<<k<<" months: YY aged NPV = " << yyS3.NPV()
<<", legs "<< yyS3.legNPV(0) << " and " << yyS3.legNPV(1)
);
}
// remove circular refernce
hy.reset();
}
BOOST_AUTO_TEST_CASE(testPeriod) {
BOOST_TEST_MESSAGE("Testing inflation period...");
Date d;
Frequency f;
std::pair<Date,Date> res;
int days[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
for (int year = 1950; year < 2051; ++year) {
if (Date::isLeap(year))
days[2] = 29;
else
days[2] = 28;
for (Size i=1; i<=12; ++i){
d = Date(1,Month(i),year);
f = Monthly;
res = inflationPeriod (d,f);
if (res.first != Date(1,Month(i),year)
|| res.second != Date(days[i],Month(i),year)) {
REPORT_FAILURE(d, res, "Monthly");
}
f = Quarterly;
res = inflationPeriod (d,f);
if ( (i==1 || i==2 || i==3) &&
(res.first != Date(1,Month(1),year)
|| res.second != Date(31,Month(3),year))) {
REPORT_FAILURE(d, res, "Quarterly");
}
else if ( (i==4 || i==5 || i==6) &&
(res.first != Date(1,Month(4),year)
|| res.second != Date(30,Month(6),year))) {
REPORT_FAILURE(d, res, "Quarterly");
}
else if ( (i==7 || i==8 || i==9) &&
(res.first != Date(1,Month(7),year)
|| res.second != Date(30,Month(9),year))) {
REPORT_FAILURE(d, res, "Quarterly");
}
else if ( (i==10 || i==11 || i==12) &&
(res.first != Date(1,Month(10),year)
|| res.second != Date(31,Month(12),year))) {
REPORT_FAILURE(d, res, "Quarterly");
}
f = Semiannual;
res = inflationPeriod (d,f);
if ( (i>0 && i<7) && (
res.first != Date(1,Month(1),year)
|| res.second != Date(30,Month(6),year))) {
REPORT_FAILURE(d, res, "Semiannual");
}
else if ( (i>6 && i<13) && (
res.first != Date(1,Month(7),year)
|| res.second != Date(31,Month(12),year))) {
REPORT_FAILURE(d, res, "Semiannual");
}
f = Annual;
res = inflationPeriod (d,f);
if (res.first != Date(1,Month(1),year)
|| res.second != Date(31,Month(12),year)) {
REPORT_FAILURE(d, res, "Annual");
}
}
}
}
BOOST_AUTO_TEST_CASE(testCpiFlatInterpolation) {
BOOST_TEST_MESSAGE("Testing CPI flat interpolation for inflation fixings...");
Settings::instance().evaluationDate() = Date(10, February, 2022);
auto testIndex = ext::make_shared<UKRPI>();
testIndex->addFixing(Date(1, November, 2020), 293.5);
testIndex->addFixing(Date(1, December, 2020), 295.4);
testIndex->addFixing(Date(1, January, 2021), 294.6);
testIndex->addFixing(Date(1, February, 2021), 296.0);
testIndex->addFixing(Date(1, March, 2021), 296.9);
Real calculated = CPI::laggedFixing(testIndex, Date(10, February, 2021), 3 * Months, CPI::Flat);
Real expected = 293.5;
QL_CHECK_CLOSE(calculated, expected, 1e-8);
calculated = CPI::laggedFixing(testIndex, Date(12, May, 2021), 3 * Months, CPI::Flat);
expected = 296.0;
QL_CHECK_CLOSE(calculated, expected, 1e-8);
calculated = CPI::laggedFixing(testIndex, Date(25, June, 2021), 3 * Months, CPI::Flat);
expected = 296.9;
QL_CHECK_CLOSE(calculated, expected, 1e-8);
}
BOOST_AUTO_TEST_CASE(testCpiLinearInterpolation) {
BOOST_TEST_MESSAGE("Testing CPI linear interpolation for inflation fixings...");
Settings::instance().evaluationDate() = Date(10, February, 2022);
auto testIndex = ext::make_shared<UKRPI>();
testIndex->addFixing(Date(1, November, 2020), 293.5);
testIndex->addFixing(Date(1, December, 2020), 295.4);
testIndex->addFixing(Date(1, January, 2021), 294.6);
testIndex->addFixing(Date(1, February, 2021), 296.0);
testIndex->addFixing(Date(1, March, 2021), 296.9);
Real calculated = CPI::laggedFixing(testIndex, Date(10, February, 2021), 3 * Months, CPI::Linear);
Real expected = 293.5 * (19/28.0) + 295.4 * (9/28.0);
QL_CHECK_CLOSE(calculated, expected, 1e-8);
calculated = CPI::laggedFixing(testIndex, Date(12, May, 2021), 3 * Months, CPI::Linear);
expected = 296.0 * (20/31.0) + 296.9 * (11/31.0);
QL_CHECK_CLOSE(calculated, expected, 1e-8);
// this would require April's fixing
BOOST_CHECK_THROW(
calculated = CPI::laggedFixing(testIndex, Date(25, June, 2021), 3 * Months, CPI::Linear),
Error);
// however, this is a special case
calculated = CPI::laggedFixing(testIndex, Date(1, June, 2021), 3 * Months, CPI::Linear);
expected = 296.9;
QL_CHECK_CLOSE(calculated, expected, 1e-8);
}
BOOST_AUTO_TEST_CASE(testCpiAsIndexInterpolation) {
BOOST_TEST_MESSAGE("Testing CPI as-index interpolation for inflation fixings...");
Date today = Date(10, February, 2022);
Settings::instance().evaluationDate() = today;
// AsIndex requires a term structure, even for fixings in the past
std::vector<Date> dates = { today - 3*Months, today + 5*Years };
std::vector<Rate> rates = { 0.02, 0.02 };
Handle<ZeroInflationTermStructure> mock_curve(
ext::make_shared<ZeroInflationCurve>(today, dates, rates, Monthly, Actual360()));
auto testIndex = ext::make_shared<UKRPI>(mock_curve);
testIndex->addFixing(Date(1, November, 2020), 293.5);
testIndex->addFixing(Date(1, December, 2020), 295.4);
testIndex->addFixing(Date(1, January, 2021), 294.6);
testIndex->addFixing(Date(1, February, 2021), 296.0);
testIndex->addFixing(Date(1, March, 2021), 296.9);
Real calculated = CPI::laggedFixing(testIndex, Date(10, February, 2021), 3 * Months, CPI::AsIndex);
Real expected = 293.5;
QL_CHECK_CLOSE(calculated, expected, 1e-8);
calculated = CPI::laggedFixing(testIndex, Date(12, May, 2021), 3 * Months, CPI::AsIndex);
expected = 296.0;
QL_CHECK_CLOSE(calculated, expected, 1e-8);
calculated = CPI::laggedFixing(testIndex, Date(25, June, 2021), 3 * Months, CPI::AsIndex);
expected = 296.9;
QL_CHECK_CLOSE(calculated, expected, 1e-8);
}
BOOST_AUTO_TEST_CASE(testCpiYoYQuotedFlatInterpolation) {
BOOST_TEST_MESSAGE("Testing CPI flat interpolation for year-on-year quoted rates...");
Settings::instance().evaluationDate() = Date(10, February, 2022);
auto testIndex1 = ext::make_shared<YYUKRPI>();
QL_DEPRECATED_DISABLE_WARNING
// NOLINTNEXTLINE(modernize-make-shared)
auto testIndex2 = ext::shared_ptr<YYUKRPI>(new YYUKRPI(true));
QL_DEPRECATED_ENABLE_WARNING
testIndex1->addFixing(Date(1, November, 2020), 0.02935);
testIndex1->addFixing(Date(1, December, 2020), 0.02954);
testIndex1->addFixing(Date(1, January, 2021), 0.02946);
testIndex1->addFixing(Date(1, February, 2021), 0.02960);
testIndex1->addFixing(Date(1, March, 2021), 0.02969);
Real calculated = CPI::laggedYoYRate(testIndex1, Date(10, February, 2021), 3 * Months, CPI::Flat);
Real expected = 0.02935;
QL_CHECK_CLOSE(calculated, expected, 1e-8);
// same expected flat fixing for interpolated and not interpolated
calculated = CPI::laggedYoYRate(testIndex2, Date(10, February, 2021), 3 * Months, CPI::Flat);
QL_CHECK_CLOSE(calculated, expected, 1e-8);
calculated = CPI::laggedYoYRate(testIndex1, Date(25, June, 2021), 3 * Months, CPI::Flat);
expected = 0.02969;
QL_CHECK_CLOSE(calculated, expected, 1e-8);
calculated = CPI::laggedYoYRate(testIndex2, Date(25, June, 2021), 3 * Months, CPI::Flat);
QL_CHECK_CLOSE(calculated, expected, 1e-8);
}
BOOST_AUTO_TEST_CASE(testCpiYoYQuotedLinearInterpolation) {
BOOST_TEST_MESSAGE("Testing CPI linear interpolation for year-on-year quoted rates...");
Settings::instance().evaluationDate() = Date(10, February, 2022);
auto testIndex1 = ext::make_shared<YYUKRPI>();
QL_DEPRECATED_DISABLE_WARNING
// NOLINTNEXTLINE(modernize-make-shared)
auto testIndex2 = ext::shared_ptr<YYUKRPI>(new YYUKRPI(true));
QL_DEPRECATED_ENABLE_WARNING
testIndex1->addFixing(Date(1, November, 2020), 0.02935);
testIndex1->addFixing(Date(1, December, 2020), 0.02954);
testIndex1->addFixing(Date(1, January, 2021), 0.02946);
testIndex1->addFixing(Date(1, February, 2021), 0.02960);
testIndex1->addFixing(Date(1, March, 2021), 0.02969);
Real calculated = CPI::laggedYoYRate(testIndex1, Date(10, February, 2021), 3 * Months, CPI::Linear);
Real expected = 0.02935 * (19/28.0) + 0.02954 * (9/28.0);
QL_CHECK_CLOSE(calculated, expected, 1e-8);
calculated = CPI::laggedYoYRate(testIndex2, Date(10, February, 2021), 3 * Months, CPI::Linear);
QL_CHECK_CLOSE(calculated, expected, 1e-8);
calculated = CPI::laggedYoYRate(testIndex1, Date(12, May, 2021), 3 * Months, CPI::Linear);
expected = 0.02960 * (20/31.0) + 0.02969 * (11/31.0);
QL_CHECK_CLOSE(calculated, expected, 1e-8);
calculated = CPI::laggedYoYRate(testIndex2, Date(12, May, 2021), 3 * Months, CPI::Linear);
QL_CHECK_CLOSE(calculated, expected, 1e-8);
// this would require April's fixing
BOOST_CHECK_EXCEPTION(
CPI::laggedYoYRate(testIndex1, Date(25, June, 2021), 3 * Months, CPI::Linear),
Error, ExpectedErrorMessage("Missing UK YY_RPI fixing"));
BOOST_CHECK_EXCEPTION(
CPI::laggedYoYRate(testIndex2, Date(25, June, 2021), 3 * Months, CPI::Linear),
Error, ExpectedErrorMessage("Missing UK YY_RPI fixing"));
// however, this is a special case
calculated = CPI::laggedYoYRate(testIndex1, Date(1, June, 2021), 3 * Months, CPI::Linear);
expected = 0.02969;
QL_CHECK_CLOSE(calculated, expected, 1e-8);
calculated = CPI::laggedYoYRate(testIndex2, Date(1, June, 2021), 3 * Months, CPI::Linear);
QL_CHECK_CLOSE(calculated, expected, 1e-8);
}
BOOST_AUTO_TEST_CASE(testCpiYoYRatioFlatInterpolation) {
BOOST_TEST_MESSAGE("Testing CPI flat interpolation for year-on-year ratios...");
Settings::instance().evaluationDate() = Date(10, February, 2022);
auto underlying = ext::make_shared<UKRPI>();
auto testIndex1 = ext::make_shared<YoYInflationIndex>(underlying);
QL_DEPRECATED_DISABLE_WARNING
// NOLINTNEXTLINE(modernize-make-shared)
auto testIndex2 = ext::shared_ptr<YoYInflationIndex>(
new YoYInflationIndex(underlying, true));
QL_DEPRECATED_ENABLE_WARNING
underlying->addFixing(Date(1, November, 2019), 291.0);
underlying->addFixing(Date(1, December, 2019), 291.9);
underlying->addFixing(Date(1, January, 2020), 290.6);
underlying->addFixing(Date(1, February, 2020), 292.0);
underlying->addFixing(Date(1, March, 2020), 292.6);
underlying->addFixing(Date(1, November, 2020), 293.5);
underlying->addFixing(Date(1, December, 2020), 295.4);
underlying->addFixing(Date(1, January, 2021), 294.6);
underlying->addFixing(Date(1, February, 2021), 296.0);
underlying->addFixing(Date(1, March, 2021), 296.9);
Real calculated = CPI::laggedYoYRate(testIndex1, Date(10, February, 2021), 3 * Months, CPI::Flat);
Real expected = 293.5/291.0 - 1;
QL_CHECK_CLOSE(calculated, expected, 1e-8);
// same expected flat fixing for interpolated and not interpolated
calculated = CPI::laggedYoYRate(testIndex2, Date(10, February, 2021), 3 * Months, CPI::Flat);
QL_CHECK_CLOSE(calculated, expected, 1e-8);
calculated = CPI::laggedYoYRate(testIndex1, Date(25, June, 2021), 3 * Months, CPI::Flat);
expected = 296.9/292.6 - 1;
QL_CHECK_CLOSE(calculated, expected, 1e-8);
calculated = CPI::laggedYoYRate(testIndex2, Date(25, June, 2021), 3 * Months, CPI::Flat);
QL_CHECK_CLOSE(calculated, expected, 1e-8);
}
BOOST_AUTO_TEST_CASE(testCpiYoYRatioLinearInterpolation) {
BOOST_TEST_MESSAGE("Testing CPI linear interpolation for year-on-year ratios...");
Settings::instance().evaluationDate() = Date(10, February, 2022);
auto underlying = ext::make_shared<UKRPI>();
auto testIndex1 = ext::make_shared<YoYInflationIndex>(underlying);
QL_DEPRECATED_DISABLE_WARNING
// NOLINTNEXTLINE(modernize-make-shared)
auto testIndex2 = ext::shared_ptr<YoYInflationIndex>(
new YoYInflationIndex(underlying, true));
QL_DEPRECATED_ENABLE_WARNING
underlying->addFixing(Date(1, November, 2019), 291.0);
underlying->addFixing(Date(1, December, 2019), 291.9);
underlying->addFixing(Date(1, January, 2020), 290.6);
underlying->addFixing(Date(1, February, 2020), 292.0);
underlying->addFixing(Date(1, March, 2020), 292.6);
underlying->addFixing(Date(1, November, 2020), 293.5);
underlying->addFixing(Date(1, December, 2020), 295.4);
underlying->addFixing(Date(1, January, 2021), 294.6);
underlying->addFixing(Date(1, February, 2021), 296.0);
underlying->addFixing(Date(1, March, 2021), 296.9);
Real calculated = CPI::laggedYoYRate(testIndex1, Date(10, February, 2021), 3 * Months, CPI::Linear);
// interpolate, then take ratio
Real expected = (293.5 * (19/28.0) + 295.4 * (9/28.0)) / (291.0 * (20/29.0) + 291.9 * (9/29.0)) - 1;
QL_CHECK_CLOSE(calculated, expected, 1e-8);
calculated = CPI::laggedYoYRate(testIndex2, Date(10, February, 2021), 3 * Months, CPI::Linear);
QL_CHECK_CLOSE(calculated, expected, 1e-8);
calculated = CPI::laggedYoYRate(testIndex1, Date(12, May, 2021), 3 * Months, CPI::Linear);
expected = (296.0 * (20/31.0) + 296.9 * (11/31.0)) / (292.0 * (20/31.0) + 292.6 * (11/31.0)) - 1;
QL_CHECK_CLOSE(calculated, expected, 1e-8);
calculated = CPI::laggedYoYRate(testIndex2, Date(12, May, 2021), 3 * Months, CPI::Linear);
QL_CHECK_CLOSE(calculated, expected, 1e-8);
// this would require April's fixing
BOOST_CHECK_EXCEPTION(
CPI::laggedYoYRate(testIndex1, Date(25, June, 2021), 3 * Months, CPI::Linear),
Error, ExpectedErrorMessage("Missing UK RPI fixing"));
BOOST_CHECK_EXCEPTION(
CPI::laggedYoYRate(testIndex2, Date(25, June, 2021), 3 * Months, CPI::Linear),
Error, ExpectedErrorMessage("Missing UK RPI fixing"));
// however, this is a special case
calculated = CPI::laggedYoYRate(testIndex1, Date(1, June, 2021), 3 * Months, CPI::Linear);
expected = 296.9/292.6 - 1;
QL_CHECK_CLOSE(calculated, expected, 1e-8);
calculated = CPI::laggedYoYRate(testIndex2, Date(1, June, 2021), 3 * Months, CPI::Linear);
QL_CHECK_CLOSE(calculated, expected, 1e-8);
}
BOOST_AUTO_TEST_CASE(testNotifications) {
BOOST_TEST_MESSAGE("Testing notifications from zero-inflation cash flow...");
Date today = Settings::instance().evaluationDate();
Real nominal = 10000.0;
std::vector<Date> dates = { today - 3*Months, today + 5*Years };
std::vector<Rate> rates = { 0.02, 0.02 };
RelinkableHandle<ZeroInflationTermStructure> inflation_handle;
inflation_handle.linkTo(
ext::make_shared<ZeroInflationCurve>(today, dates, rates, Monthly, Actual360()));
auto index = ext::make_shared<UKRPI>(inflation_handle);
index->addFixing(inflationPeriod(today - 3 * Months, index->frequency()).first, 100.0);
auto cashflow =
ext::make_shared<ZeroInflationCashFlow>(nominal,
index,
CPI::Flat,
today,
today + 1 * Years,
3 * Months,
today + 1 * Years);
cashflow->amount();
Flag flag;
flag.registerWith(cashflow);
flag.lower();
inflation_handle.linkTo(
ext::make_shared<ZeroInflationCurve>(today, dates, rates, Monthly, Actual360()));
if (!flag.isUp())
BOOST_FAIL("cash flow did not notify observer of curve change");
}
BOOST_AUTO_TEST_CASE(testExtrapolationRegression) {
BOOST_TEST_MESSAGE("Testing inflation term structure regression when extrapolating...");
Calendar calendar = UnitedKingdom();
BusinessDayConvention bdc = ModifiedFollowing;
Date evaluationDate(13, August, 2007);
evaluationDate = calendar.adjust(evaluationDate);
Settings::instance().evaluationDate() = evaluationDate;
Date from(1, January, 2005);
Date to(1, July, 2007);
Schedule rpiSchedule =
MakeSchedule().from(from).to(to)
.withFrequency(Monthly);
Real fixData[] = {
189.9, 189.9, 189.6, 190.5, 191.6, 192.0,
192.2, 192.2, 192.6, 193.1, 193.3, 193.6,
194.1, 193.4, 194.2, 195.0, 196.5, 197.7,
198.5, 198.5, 199.2, 200.1, 200.4, 201.1,
202.7, 201.6, 203.1, 204.4, 205.4, 206.2,
207.3};
auto rpi = ext::make_shared<UKRPI>();
for (Size i=0; i<std::size(fixData); i++) {
rpi->addFixing(rpiSchedule[i], fixData[i]);
}
Handle<YieldTermStructure> nominalTS(nominalTermStructure());
// now build the zero inflation curve
std::vector<Datum> zcData = {
{ Date(13, August, 2008), 2.93 },
{ Date(13, August, 2009), 2.95 },
{ Date(13, August, 2010), 2.965 },
{ Date(15, August, 2011), 2.98 },
{ Date(13, August, 2012), 3.0 },
{ Date(13, August, 2014), 3.06 },
{ Date(13, August, 2017), 3.175 },
{ Date(13, August, 2019), 3.243 },
{ Date(15, August, 2022), 3.293 },
{ Date(14, August, 2027), 3.338 },
{ Date(13, August, 2032), 3.348 },
{ Date(15, August, 2037), 3.348 },
{ Date(13, August, 2047), 3.308 },
{ Date(13, August, 2057), 3.228 }
};
Period observationLag = Period(3, Months);
DayCounter dc = Thirty360(Thirty360::BondBasis);
Frequency frequency = Monthly;
auto makeHelper = [&](const Handle<Quote>& quote, const Date& maturity) {
return ext::make_shared<ZeroCouponInflationSwapHelper>(
quote, observationLag, maturity, calendar, bdc, dc, rpi, CPI::AsIndex);
};
auto helpers = makeHelpers<ZeroInflationTermStructure>(zcData, makeHelper);
Date baseDate = rpi->lastFixingDate();
ext::shared_ptr<PiecewiseZeroInflationCurve<Linear> > pZITS =
ext::make_shared<PiecewiseZeroInflationCurve<Linear>>(
evaluationDate, baseDate, frequency, dc, helpers);
pZITS->enableExtrapolation();
BOOST_CHECK_NO_THROW(pZITS->zeroRate(10.0));
auto yoy = ext::make_shared<YoYInflationIndex>(rpi);
std::vector<Datum> yyData = {
{ Date(13, August, 2008), 2.95 },
{ Date(13, August, 2009), 2.95 },
{ Date(13, August, 2010), 2.93 },
{ Date(15, August, 2011), 2.955 },
{ Date(13, August, 2012), 2.945 },
{ Date(13, August, 2013), 2.985 },
{ Date(13, August, 2014), 3.01 },
{ Date(13, August, 2015), 3.035 },
{ Date(13, August, 2016), 3.055 }, // note that
{ Date(13, August, 2017), 3.075 }, // some dates will be on
{ Date(13, August, 2019), 3.105 }, // holidays but the payment
{ Date(15, August, 2022), 3.135 }, // calendar will roll them
{ Date(13, August, 2027), 3.155 },
{ Date(13, August, 2032), 3.145 },
{ Date(13, August, 2037), 3.145 }
};
observationLag = Period(2,Months);
// now build the helpers ...
auto makeYoYHelper = [&](const Handle<Quote>& quote, const Date& maturity) {
return ext::make_shared<YearOnYearInflationSwapHelper>(
quote, observationLag, maturity, calendar, bdc, dc, yoy, CPI::AsIndex,
Handle<YieldTermStructure>(nominalTS));
};
auto yoyHelpers = makeHelpers<YoYInflationTermStructure>(yyData, makeYoYHelper);
Rate baseYYRate = yyData[0].rate/100.0;
auto pYYTS =
ext::make_shared<PiecewiseYoYInflationCurve<Linear>>(
evaluationDate, baseDate, baseYYRate,
yoy->frequency(), dc, yoyHelpers);
pYYTS->enableExtrapolation();
BOOST_CHECK_NO_THROW(pYYTS->yoyRate(10.0));
}
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_SUITE_END()
|