Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
Functions
pricetermstructureadapter.cpp File Reference
#include "toplevelfixture.hpp"
#include <boost/assign/list_of.hpp>
#include <boost/make_shared.hpp>
#include <boost/test/unit_test.hpp>
#include <ql/currencies/america.hpp>
#include <ql/math/interpolations/all.hpp>
#include <ql/quotes/simplequote.hpp>
#include <ql/termstructures/yield/flatforward.hpp>
#include <ql/termstructures/yield/zerocurve.hpp>
#include <ql/time/daycounters/actual365fixed.hpp>
#include <qle/termstructures/pricecurve.hpp>
#include <qle/termstructures/pricetermstructureadapter.hpp>

Go to the source code of this file.

Functions

 BOOST_AUTO_TEST_CASE (testImpliedZeroRates)
 
 BOOST_AUTO_TEST_CASE (testFloatingDiscountFixedPrice)
 
 BOOST_AUTO_TEST_CASE (testFixedDiscountFloatingPrice)
 
 BOOST_AUTO_TEST_CASE (testExtrapolation)
 

Function Documentation

◆ BOOST_AUTO_TEST_CASE() [1/4]

BOOST_AUTO_TEST_CASE ( testImpliedZeroRates  )

Definition at line 77 of file pricetermstructureadapter.cpp.

77 {
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}
Adapter class for turning a PriceTermStructure in to a YieldTermStructure.

◆ BOOST_AUTO_TEST_CASE() [2/4]

BOOST_AUTO_TEST_CASE ( testFloatingDiscountFixedPrice  )

Definition at line 134 of file pricetermstructureadapter.cpp.

134 {
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}

◆ BOOST_AUTO_TEST_CASE() [3/4]

BOOST_AUTO_TEST_CASE ( testFixedDiscountFloatingPrice  )

Definition at line 171 of file pricetermstructureadapter.cpp.

171 {
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}

◆ BOOST_AUTO_TEST_CASE() [4/4]

BOOST_AUTO_TEST_CASE ( testExtrapolation  )

Definition at line 206 of file pricetermstructureadapter.cpp.

206 {
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}