Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
pricetermstructureadapter.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2018 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
19#include "toplevelfixture.hpp"
20#include <boost/assign/list_of.hpp>
21#include <boost/make_shared.hpp>
22#include <boost/test/unit_test.hpp>
23#include <ql/currencies/america.hpp>
24#include <ql/math/interpolations/all.hpp>
25#include <ql/quotes/simplequote.hpp>
26#include <ql/termstructures/yield/flatforward.hpp>
27#include <ql/termstructures/yield/zerocurve.hpp>
28#include <ql/time/daycounters/actual365fixed.hpp>
29
32
33using namespace std;
34
35using namespace boost::unit_test_framework;
36using namespace boost::assign;
37
38using namespace QuantLib;
39using namespace QuantExt;
40
41namespace {
42
43class CommonData {
44public:
45 SavedSettings backup;
46
47 Real tolerance;
48 QuantLib::ext::shared_ptr<SimpleQuote> flatZero;
49 DayCounter dayCounter;
50 vector<Period> priceTenors;
51 vector<QuantLib::ext::shared_ptr<SimpleQuote> > priceQuotes;
52 USDCurrency currency;
53
54 CommonData() : tolerance(1e-10), priceTenors(5), priceQuotes(5) {
55 flatZero = QuantLib::ext::make_shared<SimpleQuote>(0.015);
56 dayCounter = Actual365Fixed();
57
58 priceTenors[0] = 0 * Days;
59 priceQuotes[0] = QuantLib::ext::make_shared<SimpleQuote>(14.5);
60 priceTenors[1] = 6 * Months;
61 priceQuotes[1] = QuantLib::ext::make_shared<SimpleQuote>(16.7);
62 priceTenors[2] = 1 * Years;
63 priceQuotes[2] = QuantLib::ext::make_shared<SimpleQuote>(19.9);
64 priceTenors[3] = 2 * Years;
65 priceQuotes[3] = QuantLib::ext::make_shared<SimpleQuote>(28.5);
66 priceTenors[4] = 5 * Years;
67 priceQuotes[4] = QuantLib::ext::make_shared<SimpleQuote>(38.8);
68 }
69};
70
71} // namespace
72
73BOOST_FIXTURE_TEST_SUITE(QuantExtTestSuite, qle::test::TopLevelFixture)
74
75BOOST_AUTO_TEST_SUITE(PriceTermStructureAdapterTest)
76
77BOOST_AUTO_TEST_CASE(testImpliedZeroRates) {
78
79 BOOST_TEST_MESSAGE("Testing implied zero rates from PriceTermStructureAdapter");
80
81 CommonData td;
82
83 // Set arbitrary evaluation date
84 Date asof(27, Feb, 2018);
85 Settings::instance().evaluationDate() = asof;
86
87 // Discount curve
88 QuantLib::ext::shared_ptr<YieldTermStructure> discount =
89 QuantLib::ext::make_shared<FlatForward>(0, NullCalendar(), Handle<Quote>(td.flatZero), td.dayCounter);
90
91 // Price curve
92 vector<Time> times(td.priceTenors.size());
93 vector<Handle<Quote> > prices(td.priceTenors.size());
94 for (Size i = 0; i < td.priceTenors.size(); i++) {
95 times[i] = td.dayCounter.yearFraction(asof, asof + td.priceTenors[i]);
96 prices[i] = Handle<Quote>(td.priceQuotes[i]);
97 }
98 QuantLib::ext::shared_ptr<PriceTermStructure> priceCurve =
99 QuantLib::ext::make_shared<InterpolatedPriceCurve<Linear> >(td.priceTenors, prices, td.dayCounter, td.currency);
100
101 // Adapted price curve i.e. implied yield termstructure
102 PriceTermStructureAdapter priceAdapter(priceCurve, discount);
103
104 // Check the implied zero rates
105 Real spot = priceCurve->price(0);
106 for (Size i = 1; i < times.size(); i++) {
107 Real impliedZero = priceAdapter.zeroRate(times[i], Continuous);
108 Real expectedZero = td.flatZero->value() - std::log(td.priceQuotes[i]->value() / spot) / times[i];
109 BOOST_CHECK_CLOSE(impliedZero, expectedZero, td.tolerance);
110 }
111
112 // Bump price curve and check again
113 for (Size i = 0; i < td.priceTenors.size(); i++) {
114 td.priceQuotes[i]->setValue(td.priceQuotes[i]->value() * 1.10);
115 }
116
117 // Check the implied zero rates
118 spot = priceCurve->price(0);
119 for (Size i = 1; i < times.size(); i++) {
120 Real impliedZero = priceAdapter.zeroRate(times[i], Continuous);
121 Real expectedZero = td.flatZero->value() - std::log(td.priceQuotes[i]->value() / spot) / times[i];
122 BOOST_CHECK_CLOSE(impliedZero, expectedZero, td.tolerance);
123 }
124
125 // Bump discount curve and check again
126 td.flatZero->setValue(td.flatZero->value() * 0.9);
127 for (Size i = 1; i < times.size(); i++) {
128 Real impliedZero = priceAdapter.zeroRate(times[i], Continuous);
129 Real expectedZero = td.flatZero->value() - std::log(td.priceQuotes[i]->value() / spot) / times[i];
130 BOOST_CHECK_CLOSE(impliedZero, expectedZero, td.tolerance);
131 }
132}
133
134BOOST_AUTO_TEST_CASE(testFloatingDiscountFixedPrice) {
135
136 BOOST_TEST_MESSAGE("Testing behaviour of PriceTermStructureAdapter with floating reference discount curve and "
137 "fixed reference price curve");
138
139 CommonData td;
140
141 // Set arbitrary evaluation date
142 Date asof(27, Feb, 2018);
143 Settings::instance().evaluationDate() = asof;
144
145 // Discount curve (floating reference)
146 QuantLib::ext::shared_ptr<YieldTermStructure> floatReferenceDiscountCurve =
147 QuantLib::ext::make_shared<FlatForward>(0, NullCalendar(), Handle<Quote>(td.flatZero), td.dayCounter);
148
149 // Price curve (fixed reference)
150 vector<Date> dates(td.priceTenors.size());
151 vector<Handle<Quote> > prices(td.priceTenors.size());
152 for (Size i = 0; i < td.priceTenors.size(); i++) {
153 dates[i] = asof + td.priceTenors[i];
154 prices[i] = Handle<Quote>(td.priceQuotes[i]);
155 }
156 QuantLib::ext::shared_ptr<PriceTermStructure> fixedReferencePriceCurve =
157 QuantLib::ext::make_shared<InterpolatedPriceCurve<Linear> >(asof, dates, prices, td.dayCounter, td.currency);
158
159 // Check construction of adapted price curve passes => reference dates same on construction
160 BOOST_REQUIRE_NO_THROW(PriceTermStructureAdapter(fixedReferencePriceCurve, floatReferenceDiscountCurve));
161
162 // Construct adapted price curve
163 PriceTermStructureAdapter adaptedPriceCurve(fixedReferencePriceCurve, floatReferenceDiscountCurve);
164 BOOST_CHECK_NO_THROW(adaptedPriceCurve.zeroRate(0.5, Continuous));
165
166 // Change Settings::instance().evaluationDate() - discount curve reference date changes and price curve's does not
167 Settings::instance().evaluationDate() = asof + 1 * Days;
168 BOOST_CHECK_THROW(adaptedPriceCurve.zeroRate(0.5, Continuous), Error);
169}
170
171BOOST_AUTO_TEST_CASE(testFixedDiscountFloatingPrice) {
172
173 BOOST_TEST_MESSAGE("Testing behaviour of PriceTermStructureAdapter with fixed reference discount curve and "
174 "floating reference price curve");
175
176 CommonData td;
177
178 // Set arbitrary evaluation date
179 Date asof(27, Feb, 2018);
180 Settings::instance().evaluationDate() = asof;
181
182 // Discount curve (fixed reference)
183 QuantLib::ext::shared_ptr<YieldTermStructure> floatReferenceDiscountCurve =
184 QuantLib::ext::make_shared<FlatForward>(asof, Handle<Quote>(td.flatZero), td.dayCounter);
185
186 // Price curve (floating reference)
187 vector<Handle<Quote> > prices(td.priceTenors.size());
188 for (Size i = 0; i < td.priceTenors.size(); i++) {
189 prices[i] = Handle<Quote>(td.priceQuotes[i]);
190 }
191 QuantLib::ext::shared_ptr<PriceTermStructure> fixedReferencePriceCurve =
192 QuantLib::ext::make_shared<InterpolatedPriceCurve<Linear> >(td.priceTenors, prices, td.dayCounter, td.currency);
193
194 // Check construction of adapted price curve passes => reference dates same on construction
195 BOOST_REQUIRE_NO_THROW(PriceTermStructureAdapter(fixedReferencePriceCurve, floatReferenceDiscountCurve));
196
197 // Construct adapted price curve
198 PriceTermStructureAdapter adaptedPriceCurve(fixedReferencePriceCurve, floatReferenceDiscountCurve);
199 BOOST_CHECK_NO_THROW(adaptedPriceCurve.zeroRate(0.5, Continuous));
200
201 // Change Settings::instance().evaluationDate() - price curve reference date changes and discount curve's does not
202 Settings::instance().evaluationDate() = asof + 1 * Days;
203 BOOST_CHECK_THROW(adaptedPriceCurve.zeroRate(0.5, Continuous), Error);
204}
205
206BOOST_AUTO_TEST_CASE(testExtrapolation) {
207
208 BOOST_TEST_MESSAGE("Testing extrapolation behaviour of PriceTermStructureAdapter");
209
210 CommonData td;
211
212 // Set arbitrary evaluation date
213 Date asof(27, Feb, 2018);
214 Settings::instance().evaluationDate() = asof;
215
216 // Zero curve: times in ~ [0, 3], turn off extrapolation
217 vector<Date> zeroDates(2);
218 vector<Real> zeroRates(2);
219 zeroDates[0] = asof;
220 zeroRates[0] = td.flatZero->value();
221 zeroDates[1] = asof + 3 * Years;
222 zeroRates[1] = td.flatZero->value();
223 QuantLib::ext::shared_ptr<YieldTermStructure> zeroCurve =
224 QuantLib::ext::make_shared<ZeroCurve>(zeroDates, zeroRates, td.dayCounter);
225
226 // Price curve: times in ~ [0, 5], turn off extrapolation
227 vector<Time> times(td.priceTenors.size());
228 vector<Handle<Quote> > prices(td.priceTenors.size());
229 for (Size i = 0; i < td.priceTenors.size(); i++) {
230 times[i] = td.dayCounter.yearFraction(asof, asof + td.priceTenors[i]);
231 prices[i] = Handle<Quote>(td.priceQuotes[i]);
232 }
233 QuantLib::ext::shared_ptr<PriceTermStructure> priceCurve =
234 QuantLib::ext::make_shared<InterpolatedPriceCurve<Linear> >(td.priceTenors, prices, td.dayCounter, td.currency);
235
236 // Check construction of adapted price curve passes
237 BOOST_REQUIRE_NO_THROW(PriceTermStructureAdapter(priceCurve, zeroCurve));
238
239 // Construct adapted price curve
240 PriceTermStructureAdapter adaptedPriceCurve(priceCurve, zeroCurve);
241
242 // Asking for zero at time ~ 1.0 should not throw
243 BOOST_CHECK_NO_THROW(adaptedPriceCurve.zeroRate(1.0, Continuous));
244 BOOST_CHECK_NO_THROW(adaptedPriceCurve.zeroRate(asof + 1 * Years, td.dayCounter, Continuous));
245
246 // Asking for zero at time ~ 4.0 should throw because > 3.0, max time for discount curve
247 // Note: max time for the adapted price curve is the min of the max times for the underlying curves.
248 BOOST_CHECK_THROW(adaptedPriceCurve.zeroRate(4.0, Continuous), Error);
249 BOOST_CHECK_THROW(adaptedPriceCurve.zeroRate(asof + 4 * Years, td.dayCounter, Continuous), Error);
250
251 // Allow extrapolation on adapted price curve, expect no errors
252 adaptedPriceCurve.enableExtrapolation();
253 BOOST_CHECK_NO_THROW(adaptedPriceCurve.zeroRate(6.0, Continuous));
254 BOOST_CHECK_NO_THROW(adaptedPriceCurve.zeroRate(asof + 6 * Years, td.dayCounter, Continuous));
255}
256
257BOOST_AUTO_TEST_SUITE_END()
258
259BOOST_AUTO_TEST_SUITE_END()
Adapter class for turning a PriceTermStructure in to a YieldTermStructure.
Interpolated price curve.
PriceTermStructure adapter.
BOOST_AUTO_TEST_CASE(testImpliedZeroRates)
Fixture that can be used at top level.