19#include <boost/assign.hpp>
20#include <boost/test/unit_test.hpp>
21#include <ql/quotes/simplequote.hpp>
22#include <ql/termstructures/volatility/optionlet/strippedoptionlet.hpp>
23#include <ql/time/calendars/unitedstates.hpp>
30using namespace boost::unit_test_framework;
32using boost::assign::list_of;
42 vector<Date> expiries;
45 vector<vector<Handle<Quote> > > vols;
46 QuantLib::ext::shared_ptr<StrippedOptionlet> optionletSurface;
48 F() : expiries(2),
strikes(2), dc(Actual365Fixed()), vols(2) {
51 asof = Date(17, Apr, 2019);
52 Settings::instance().evaluationDate() = asof;
55 Natural settlementDays = 2;
56 UnitedStates calendar(UnitedStates::Settlement);
57 BusinessDayConvention bdc = Following;
58 QuantLib::ext::shared_ptr<IborIndex> dummyIborIndex;
59 VolatilityType type = Normal;
61 expiries[0] = Date(17, Apr, 2020);
62 expiries[1] = Date(19, Apr, 2021);
68 vols[0].push_back(Handle<Quote>(QuantLib::ext::make_shared<SimpleQuote>(0.0091)));
69 vols[0].push_back(Handle<Quote>(QuantLib::ext::make_shared<SimpleQuote>(0.0092)));
70 vols[1].push_back(Handle<Quote>(QuantLib::ext::make_shared<SimpleQuote>(0.0070)));
71 vols[1].push_back(Handle<Quote>(QuantLib::ext::make_shared<SimpleQuote>(0.0088)));
75 optionletSurface = QuantLib::ext::make_shared<StrippedOptionlet>(settlementDays, calendar, bdc, dummyIborIndex,
76 expiries,
strikes, vols, dc, type);
85BOOST_AUTO_TEST_SUITE(StrippedOptionletAdapterTests)
90 QuantLib::ext::shared_ptr<StrippedOptionletAdapter<LinearFlat, LinearFlat> > adapter =
91 QuantLib::ext::make_shared<StrippedOptionletAdapter<LinearFlat, LinearFlat> >(optionletSurface);
94 Date testDate = expiries.back() + 1 * Years;
97 for (Size i = 0; i <
strikes.size(); i++) {
98 Volatility testVol = adapter->volatility(testDate,
strikes[i],
true);
99 BOOST_CHECK_CLOSE(testVol, vols.back()[i]->value(), 1e-12);
103 Volatility testVol = adapter->volatility(testDate,
strikes.front() / 2.0,
true);
104 BOOST_CHECK_CLOSE(testVol, vols.back().front()->value(), 1e-12);
107 testVol = adapter->volatility(testDate, 2.0 *
strikes.back(),
true);
108 BOOST_CHECK_CLOSE(testVol, vols.back().back()->value(), 1e-12);
112 Volatility expectedVol = (vols.back()[0]->value() + vols.back()[1]->value()) / 2.0;
113 testVol = adapter->volatility(testDate, avgStrike,
true);
114 BOOST_CHECK_CLOSE(testVol, expectedVol, 1e-12);
120 QuantLib::ext::shared_ptr<StrippedOptionletAdapter<LinearFlat, LinearFlat> > adapter =
121 QuantLib::ext::make_shared<StrippedOptionletAdapter<LinearFlat, LinearFlat> >(optionletSurface);
124 for (Size i = 0; i < expiries.size(); i++) {
126 Volatility testVol = adapter->volatility(expiries[i],
strikes.front() / 2.0,
true);
127 BOOST_CHECK_CLOSE(testVol, vols[i].front()->value(), 1e-12);
129 testVol = adapter->volatility(expiries[i], 2.0 *
strikes.back(),
true);
130 BOOST_CHECK_CLOSE(testVol, vols[i].back()->value(), 1e-12);
134 Size numDays = dc.dayCount(expiries.front(), expiries.back()) / 2;
135 Date testDate = expiries.front() + numDays * Days;
138 Volatility testVol = adapter->volatility(testDate,
strikes.front() / 2.0,
true);
139 Volatility expectedVol = adapter->volatility(testDate,
strikes.front(),
true);
140 BOOST_CHECK_CLOSE(testVol, expectedVol, 1e-12);
143 testVol = adapter->volatility(testDate, 2.0 *
strikes.back(),
true);
144 expectedVol = adapter->volatility(testDate,
strikes.back(),
true);
145 BOOST_CHECK_CLOSE(testVol, expectedVol, 1e-12);
151 QuantLib::ext::shared_ptr<StrippedOptionletAdapter<LinearFlat, LinearFlat> > adapter =
152 QuantLib::ext::make_shared<StrippedOptionletAdapter<LinearFlat, LinearFlat> >(optionletSurface);
155 Size numDays = dc.dayCount(asof, expiries.front()) / 2;
156 Date testDate = asof + numDays * Days;
159 for (Size i = 0; i <
strikes.size(); i++) {
160 Volatility testVol = adapter->volatility(testDate,
strikes[i],
true);
161 BOOST_CHECK_CLOSE(testVol, vols.front()[i]->value(), 1e-12);
165 Volatility testVol = adapter->volatility(testDate,
strikes.front() / 2.0,
true);
166 BOOST_CHECK_CLOSE(testVol, vols.front().front()->value(), 1e-12);
169 testVol = adapter->volatility(testDate, 2.0 *
strikes.back(),
true);
170 BOOST_CHECK_CLOSE(testVol, vols.front().back()->value(), 1e-12);
174 Volatility expectedVol = (vols.front()[0]->value() + vols.front()[1]->value()) / 2.0;
175 testVol = adapter->volatility(testDate, avgStrike,
true);
176 BOOST_CHECK_CLOSE(testVol, expectedVol, 1e-12);
181 BOOST_TEST_MESSAGE(
"Testing StrippedOptionletAdapter when StrippedOptionlet has one strike column");
184 Date asof(17, Apr, 2019);
185 Settings::instance().evaluationDate() = asof;
188 Natural settlementDays = 2;
189 UnitedStates calendar(UnitedStates::Settlement);
190 BusinessDayConvention bdc = Following;
192 QuantLib::ext::shared_ptr<IborIndex> dummyIborIndex;
193 VolatilityType type = Normal;
194 vector<Date> expiries = list_of(Date(17, Jul, 2019))(Date(17, Oct, 2019));
195 vector<Rate>
strikes = list_of(0.0);
197 vector<vector<Handle<Quote> > > vols(2);
198 vols[0].push_back(Handle<Quote>(QuantLib::ext::make_shared<SimpleQuote>(0.0091)));
199 vols[1].push_back(Handle<Quote>(QuantLib::ext::make_shared<SimpleQuote>(0.0070)));
202 QuantLib::ext::shared_ptr<StrippedOptionlet> optionletSurface = QuantLib::ext::make_shared<StrippedOptionlet>(
203 settlementDays, calendar, bdc, dummyIborIndex, expiries,
strikes, vols, dc, type);
206 QuantLib::ext::shared_ptr<StrippedOptionletAdapter<LinearFlat, LinearFlat> > adapter =
207 QuantLib::ext::make_shared<StrippedOptionletAdapter<LinearFlat, LinearFlat> >(optionletSurface);
210 Volatility strikeShift = 0.10;
211 Volatility testVol = adapter->volatility(expiries[0], 0.0,
true);
212 Volatility expectedVol = vols[0][0]->value();
213 BOOST_CHECK_CLOSE(testVol, expectedVol, 1e-12);
215 testVol = adapter->volatility(expiries[0], 0.0 + strikeShift,
true);
216 BOOST_CHECK_CLOSE(testVol, expectedVol, 1e-12);
218 testVol = adapter->volatility(expiries[0], 0.0 - strikeShift,
true);
219 BOOST_CHECK_CLOSE(testVol, expectedVol, 1e-12);
221 testVol = adapter->volatility(expiries[0] - 1 * Weeks, 0.0,
true);
222 BOOST_CHECK_CLOSE(testVol, expectedVol, 1e-12);
224 testVol = adapter->volatility(expiries[1], 0.0,
true);
225 expectedVol = vols[1][0]->value();
226 BOOST_CHECK_CLOSE(testVol, expectedVol, 1e-12);
228 testVol = adapter->volatility(expiries[1] + 1 * Weeks, 0.0,
true);
229 BOOST_CHECK_CLOSE(testVol, expectedVol, 1e-12);
232BOOST_AUTO_TEST_SUITE_END()
234BOOST_AUTO_TEST_SUITE_END()
BOOST_FIXTURE_TEST_CASE(testFlatExtrapAfterLastExpiry, F)
BOOST_AUTO_TEST_CASE(testOneStrikeColumn)
Convert a StrippedOptionletBase in to an OptionletVolatilityStructure.
Fixture that can be used at top level.