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>
35using namespace boost::unit_test_framework;
36using namespace boost::assign;
48 QuantLib::ext::shared_ptr<SimpleQuote> flatZero;
49 DayCounter dayCounter;
50 vector<Period> priceTenors;
51 vector<QuantLib::ext::shared_ptr<SimpleQuote> > priceQuotes;
54 CommonData() : tolerance(1e-10), priceTenors(5), priceQuotes(5) {
55 flatZero = QuantLib::ext::make_shared<SimpleQuote>(0.015);
56 dayCounter = Actual365Fixed();
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);
75BOOST_AUTO_TEST_SUITE(PriceTermStructureAdapterTest)
79 BOOST_TEST_MESSAGE(
"Testing implied zero rates from PriceTermStructureAdapter");
84 Date asof(27, Feb, 2018);
85 Settings::instance().evaluationDate() = asof;
88 QuantLib::ext::shared_ptr<YieldTermStructure> discount =
89 QuantLib::ext::make_shared<FlatForward>(0, NullCalendar(), Handle<Quote>(td.flatZero), td.dayCounter);
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]);
98 QuantLib::ext::shared_ptr<PriceTermStructure> priceCurve =
99 QuantLib::ext::make_shared<InterpolatedPriceCurve<Linear> >(td.priceTenors, prices, td.dayCounter, td.currency);
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);
113 for (Size i = 0; i < td.priceTenors.size(); i++) {
114 td.priceQuotes[i]->setValue(td.priceQuotes[i]->value() * 1.10);
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);
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);
136 BOOST_TEST_MESSAGE(
"Testing behaviour of PriceTermStructureAdapter with floating reference discount curve and "
137 "fixed reference price curve");
142 Date asof(27, Feb, 2018);
143 Settings::instance().evaluationDate() = asof;
146 QuantLib::ext::shared_ptr<YieldTermStructure> floatReferenceDiscountCurve =
147 QuantLib::ext::make_shared<FlatForward>(0, NullCalendar(), Handle<Quote>(td.flatZero), td.dayCounter);
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]);
156 QuantLib::ext::shared_ptr<PriceTermStructure> fixedReferencePriceCurve =
157 QuantLib::ext::make_shared<InterpolatedPriceCurve<Linear> >(asof, dates, prices, td.dayCounter, td.currency);
164 BOOST_CHECK_NO_THROW(adaptedPriceCurve.zeroRate(0.5, Continuous));
167 Settings::instance().evaluationDate() = asof + 1 * Days;
168 BOOST_CHECK_THROW(adaptedPriceCurve.zeroRate(0.5, Continuous), Error);
173 BOOST_TEST_MESSAGE(
"Testing behaviour of PriceTermStructureAdapter with fixed reference discount curve and "
174 "floating reference price curve");
179 Date asof(27, Feb, 2018);
180 Settings::instance().evaluationDate() = asof;
183 QuantLib::ext::shared_ptr<YieldTermStructure> floatReferenceDiscountCurve =
184 QuantLib::ext::make_shared<FlatForward>(asof, Handle<Quote>(td.flatZero), td.dayCounter);
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]);
191 QuantLib::ext::shared_ptr<PriceTermStructure> fixedReferencePriceCurve =
192 QuantLib::ext::make_shared<InterpolatedPriceCurve<Linear> >(td.priceTenors, prices, td.dayCounter, td.currency);
199 BOOST_CHECK_NO_THROW(adaptedPriceCurve.zeroRate(0.5, Continuous));
202 Settings::instance().evaluationDate() = asof + 1 * Days;
203 BOOST_CHECK_THROW(adaptedPriceCurve.zeroRate(0.5, Continuous), Error);
208 BOOST_TEST_MESSAGE(
"Testing extrapolation behaviour of PriceTermStructureAdapter");
213 Date asof(27, Feb, 2018);
214 Settings::instance().evaluationDate() = asof;
217 vector<Date> zeroDates(2);
218 vector<Real> zeroRates(2);
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);
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]);
233 QuantLib::ext::shared_ptr<PriceTermStructure> priceCurve =
234 QuantLib::ext::make_shared<InterpolatedPriceCurve<Linear> >(td.priceTenors, prices, td.dayCounter, td.currency);
243 BOOST_CHECK_NO_THROW(adaptedPriceCurve.zeroRate(1.0, Continuous));
244 BOOST_CHECK_NO_THROW(adaptedPriceCurve.zeroRate(asof + 1 * Years, td.dayCounter, Continuous));
248 BOOST_CHECK_THROW(adaptedPriceCurve.zeroRate(4.0, Continuous), Error);
249 BOOST_CHECK_THROW(adaptedPriceCurve.zeroRate(asof + 4 * Years, td.dayCounter, Continuous), Error);
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));
257BOOST_AUTO_TEST_SUITE_END()
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.