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
|
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2007 Cristina Duminuco
Copyright (C) 2006 Ferdinando Ametrano
Copyright (C) 2006 Franois du Vignaud
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 "swaptionvolatilitymatrix.hpp"
#include "swaptionvolstructuresutilities.hpp"
#include "utilities.hpp"
#include <ql/utilities/dataformatters.hpp>
#include <string>
using namespace QuantLib;
using namespace boost::unit_test_framework;
QL_BEGIN_TEST_LOCALS(SwaptionVolatilityMatrixTest)
// global data
Date referenceDate_;
SwaptionMarketConventions conventions_;
AtmVolatility atm_;
RelinkableHandle<YieldTermStructure> termStructure_;
RelinkableHandle<SwaptionVolatilityStructure> atmVolMatrix_;
Real tolerance_;
// utilities
void setup() {
conventions_.setConventions();
atm_.setMarketData();
atmVolMatrix_ = RelinkableHandle<SwaptionVolatilityStructure>(
boost::shared_ptr<SwaptionVolatilityStructure>(new
SwaptionVolatilityMatrix(conventions_.calendar,
atm_.tenors.options,
atm_.tenors.swaps,
atm_.volsHandle,
conventions_.dayCounter,
conventions_.optionBdc)));
}
void makeObservabilityTest(
const std::string& description,
const boost::shared_ptr<SwaptionVolatilityStructure>& vol,
bool mktDataFloating,
bool referenceDateFloating) {
Rate dummyStrike = .02;
Date referenceDate = Settings::instance().evaluationDate();
Volatility initialVol = vol->volatility(
referenceDate + atm_.tenors.options[0],
atm_.tenors.swaps[0], dummyStrike, false);
// testing evaluation date change ...
Settings::instance().evaluationDate() = referenceDate - Period(1, Years);
Volatility newVol = vol->volatility(
referenceDate + atm_.tenors.options[0],
atm_.tenors.swaps[0], dummyStrike, false);
Settings::instance().evaluationDate() = referenceDate;
if (referenceDateFloating && (initialVol == newVol))
BOOST_ERROR(description <<
" the volatility should change when the reference date is changed !");
if (!referenceDateFloating && (initialVol != newVol))
BOOST_ERROR(description <<
" the volatility should not change when the reference date is changed !");
// test market data change...
if (mktDataFloating){
Volatility initialVolatility = atm_.volsHandle[0][0]->value();
boost::dynamic_pointer_cast<SimpleQuote>(
atm_.volsHandle[0][0].currentLink())->setValue(10);
newVol = vol->volatility(
referenceDate + atm_.tenors.options[0],
atm_.tenors.swaps[0], dummyStrike, false);
boost::dynamic_pointer_cast<SimpleQuote>(
atm_.volsHandle[0][0].currentLink())->setValue(initialVolatility);
if (initialVol == newVol)
BOOST_ERROR(description << " the volatility should change when"
" the market data is changed !");
}
}
void makeCoherenceTest(
const std::string& description,
const boost::shared_ptr<SwaptionVolatilityDiscrete>& vol) {
Date refDate = vol->referenceDate();
for (Size i=0; i<atm_.tenors.options.size(); ++i) {
Date expOptDate = conventions_.calendar.advance(refDate, atm_.tenors.options[i], conventions_.optionBdc);
Date actOptDate = vol->optionDates()[i];
if (actOptDate!=expOptDate)
BOOST_FAIL("\nrecovery of option dates failed for " <<
description << ":"
"\n calendar = " << conventions_.calendar <<
"\n referenceDate = " << refDate <<
"\n expiry tenor = " << atm_.tenors.options[i] <<
"\nexpected option date = " << expOptDate <<
"\n actual option date = " << actOptDate);
}
Date lengthRef = vol->optionDates()[0];
DayCounter volDC = vol->dayCounter();
for (Size j=0; j<atm_.tenors.swaps.size(); ++j) {
Period actSwapTenor = vol->swapTenors()[j];
Date endDate = lengthRef + atm_.tenors.swaps[j];
Time expSwapLength = volDC.yearFraction(lengthRef, endDate);
Time actSwapLength = vol->swapLengths()[j];
if ((atm_.tenors.swaps[j]!=actSwapTenor) ||
(expSwapLength!=actSwapLength))
BOOST_FAIL("\nrecovery of " << io::ordinal(j) <<
" swap tenor failed for " <<
description << ":"
"\nexpected swap tenor = " << atm_.tenors.swaps[j] <<
"\n actual swap tenor = " << actSwapTenor <<
"\nexpected swap length = " << expSwapLength <<
"\n actual swap length = " << actSwapLength);
}
Real tolerance = 1.0e-16;
for (Size i=0; i<atm_.tenors.options.size(); ++i) {
for (Size j=0; j<atm_.tenors.swaps.size(); j++) {
Period thisOptionTenor= atm_.tenors.options[i];
Date thisOptionDate = vol->optionDates()[i];
Period thisSwapTenor = atm_.tenors.swaps[j];
std::pair<Time, Time> p = vol->convertDates(
thisOptionDate, thisSwapTenor);
if ((p.first !=vol->optionTimes()[i]) ||
(p.second!=vol->swapLengths()[j]))
BOOST_FAIL("\nconvertDates failure for " <<
description << ":"
"\n option date = " << thisOptionDate <<
"\n option tenor = " << thisOptionTenor <<
"\nactual option time = " << p.first <<
"\n exp. option time = " << vol->optionTimes()[i] <<
"\n swap tenor = " << thisSwapTenor <<
"\n actual swap length = " << p.second <<
"\n exp. swap length = " << vol->swapLengths()[j]);
Volatility error, actVol, expVol = atm_.vols[i][j];
actVol = vol->volatility(
thisOptionDate, thisSwapTenor, 0.05, true);
error = std::abs(expVol-actVol);
if (error>tolerance)
BOOST_FAIL("\nrecovery of atm vols failed for " <<
description << ":"
"\noption date = " << thisOptionDate <<
"\nswap length = " << thisSwapTenor <<
"\n exp. vol = " << io::volatility(expVol) <<
"\n actual vol = " << io::volatility(actVol) <<
"\n error = " << io::volatility(error) <<
"\n tolerance = " << tolerance);
actVol = vol->volatility(
thisOptionTenor, thisSwapTenor, 0.05, true);
error = std::abs(expVol-actVol);
if (error>tolerance)
BOOST_FAIL("\nrecovery of atm vols failed for " <<
description << ":"
"\noption tenor = " << thisOptionTenor <<
"\n swap length = " << thisSwapTenor <<
"\nexpected vol = " << io::volatility(expVol) <<
"\n actual vol = " << io::volatility(actVol) <<
"\n error = " << io::volatility(error) <<
"\n tolerance = " << tolerance);
}
}
}
QL_END_TEST_LOCALS(SwaptionVolatilityMatrixTest)
void SwaptionVolatilityMatrixTest::testSwaptionVolMatrixObservability() {
BOOST_MESSAGE("Testing swaption volatility matrix observability...");
SavedSettings backup;
setup();
boost::shared_ptr<SwaptionVolatilityMatrix> vol;
std::string description;
//floating reference date, floating market data
description = "floating reference date, floating market data";
vol = boost::shared_ptr<SwaptionVolatilityMatrix>(new
SwaptionVolatilityMatrix(conventions_.calendar,
atm_.tenors.options,
atm_.tenors.swaps,
atm_.volsHandle,
conventions_.dayCounter,
conventions_.optionBdc));
makeObservabilityTest(description, vol, true, true);
//fixed reference date, floating market data
description = "fixed reference date, floating market data";
vol = boost::shared_ptr<SwaptionVolatilityMatrix>(new
SwaptionVolatilityMatrix(Settings::instance().evaluationDate(),
conventions_.calendar,
atm_.tenors.options,
atm_.tenors.swaps,
atm_.volsHandle,
conventions_.dayCounter,
conventions_.optionBdc));
makeObservabilityTest(description, vol, true, false);
// floating reference date, fixed market data
description = "floating reference date, fixed market data";
vol = boost::shared_ptr<SwaptionVolatilityMatrix>(new
SwaptionVolatilityMatrix(conventions_.calendar,
atm_.tenors.options,
atm_.tenors.swaps,
atm_.volsHandle,
conventions_.dayCounter,
conventions_.optionBdc));
makeObservabilityTest(description, vol, false, true);
// fixed reference date, fixed market data
description = "fixed reference date, fixed market data";
vol = boost::shared_ptr<SwaptionVolatilityMatrix>(new
SwaptionVolatilityMatrix(Settings::instance().evaluationDate(),
conventions_.calendar,
atm_.tenors.options,
atm_.tenors.swaps,
atm_.volsHandle,
conventions_.dayCounter,
conventions_.optionBdc));
makeObservabilityTest(description, vol, false, false);
// fixed reference date and fixed market data, option dates
//SwaptionVolatilityMatrix(const Date& referenceDate,
// const std::vector<Date>& exerciseDates,
// const std::vector<Period>& swapTenors,
// const Matrix& volatilities,
// const DayCounter& dayCounter);
}
void SwaptionVolatilityMatrixTest::testSwaptionVolMatrixCoherence() {
BOOST_MESSAGE("Testing swaption volatility matrix...");
SavedSettings backup;
setup();
boost::shared_ptr<SwaptionVolatilityMatrix> vol;
std::string description;
//floating reference date, floating market data
description = "floating reference date, floating market data";
vol = boost::shared_ptr<SwaptionVolatilityMatrix>(new
SwaptionVolatilityMatrix(conventions_.calendar,
atm_.tenors.options,
atm_.tenors.swaps,
atm_.volsHandle,
conventions_.dayCounter,
conventions_.optionBdc));
makeCoherenceTest(description, vol);
//fixed reference date, floating market data
description = "fixed reference date, floating market data";
vol = boost::shared_ptr<SwaptionVolatilityMatrix>(new
SwaptionVolatilityMatrix(Settings::instance().evaluationDate(),
conventions_.calendar,
atm_.tenors.options,
atm_.tenors.swaps,
atm_.volsHandle,
conventions_.dayCounter,
conventions_.optionBdc));
makeCoherenceTest(description, vol);
// floating reference date, fixed market data
description = "floating reference date, fixed market data";
vol = boost::shared_ptr<SwaptionVolatilityMatrix>(new
SwaptionVolatilityMatrix(conventions_.calendar,
atm_.tenors.options,
atm_.tenors.swaps,
atm_.volsHandle,
conventions_.dayCounter,
conventions_.optionBdc));
makeCoherenceTest(description, vol);
// fixed reference date, fixed market data
description = "fixed reference date, fixed market data";
vol = boost::shared_ptr<SwaptionVolatilityMatrix>(new
SwaptionVolatilityMatrix(Settings::instance().evaluationDate(),
conventions_.calendar,
atm_.tenors.options,
atm_.tenors.swaps,
atm_.volsHandle,
conventions_.dayCounter,
conventions_.optionBdc));
makeCoherenceTest(description, vol);
}
test_suite* SwaptionVolatilityMatrixTest::suite() {
test_suite* suite = BOOST_TEST_SUITE("Swaption Volatility Matrix tests");
suite->add(BOOST_TEST_CASE(&SwaptionVolatilityMatrixTest::testSwaptionVolMatrixCoherence));
suite->add(BOOST_TEST_CASE(&SwaptionVolatilityMatrixTest::testSwaptionVolMatrixObservability));
return suite;
}
|