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 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
|
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2004, 2007 StatPro Italia srl
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 "forwardoption.hpp"
#include "utilities.hpp"
#include <ql/time/daycounters/actual360.hpp>
#include <ql/instruments/forwardvanillaoption.hpp>
#include <ql/pricingengines/vanilla/analyticeuropeanengine.hpp>
#include <ql/pricingengines/forward/forwardengine.hpp>
#include <ql/pricingengines/forward/forwardperformanceengine.hpp>
#include <ql/termstructures/yield/flatforward.hpp>
#include <ql/termstructures/volatility/equityfx/blackconstantvol.hpp>
#include <ql/utilities/dataformatters.hpp>
#include <map>
using namespace QuantLib;
using namespace boost::unit_test_framework;
#define REPORT_FAILURE(greekName, payoff, exercise, s, q, r, today, \
v, moneyness, reset, expected, calculated, \
error, tolerance) \
BOOST_FAIL("Forward " << exerciseTypeToString(exercise) << " " \
<< payoff->optionType() << " option with " \
<< payoffTypeToString(payoff) << " payoff:\n" \
<< " spot value: " << s << "\n" \
<< " strike: " << payoff->strike() <<"\n" \
<< " moneyness: " << moneyness << "\n" \
<< " dividend yield: " << io::rate(q) << "\n" \
<< " risk-free rate: " << io::rate(r) << "\n" \
<< " reference date: " << today << "\n" \
<< " reset date: " << reset << "\n" \
<< " maturity: " << exercise->lastDate() << "\n" \
<< " volatility: " << io::volatility(v) << "\n\n" \
<< " expected " << greekName << ": " << expected << "\n" \
<< " calculated " << greekName << ": " << calculated << "\n"\
<< " error: " << error << "\n" \
<< " tolerance: " << tolerance);
QL_BEGIN_TEST_LOCALS(ForwardOptionTest)
struct ForwardOptionData {
Option::Type type;
Real moneyness;
Real s; // spot
Rate q; // dividend
Rate r; // risk-free rate
Time start; // time to reset
Time t; // time to maturity
Volatility v; // volatility
Real result; // expected result
Real tol; // tolerance
};
QL_END_TEST_LOCALS(ForwardOptionTest)
void ForwardOptionTest::testValues() {
BOOST_MESSAGE("Testing forward option values...");
/* The data below are from
"Option pricing formulas", E.G. Haug, McGraw-Hill 1998
*/
ForwardOptionData values[] = {
// type, moneyness, spot, div, rate,start, t, vol, result, tol
// "Option pricing formulas", pag. 37
{ Option::Call, 1.1, 60.0, 0.04, 0.08, 0.25, 1.0, 0.30, 4.4064, 1.0e-4 },
// "Option pricing formulas", VBA code
{ Option::Put, 1.1, 60.0, 0.04, 0.08, 0.25, 1.0, 0.30, 8.2971, 1.0e-4 }
};
DayCounter dc = Actual360();
Date today = Date::todaysDate();
boost::shared_ptr<SimpleQuote> spot(new SimpleQuote(0.0));
boost::shared_ptr<SimpleQuote> qRate(new SimpleQuote(0.0));
Handle<YieldTermStructure> qTS(flatRate(today, qRate, dc));
boost::shared_ptr<SimpleQuote> rRate(new SimpleQuote(0.0));
Handle<YieldTermStructure> rTS(flatRate(today, rRate, dc));
boost::shared_ptr<SimpleQuote> vol(new SimpleQuote(0.0));
Handle<BlackVolTermStructure> volTS(flatVol(today, vol, dc));
boost::shared_ptr<BlackScholesMertonProcess> stochProcess(
new BlackScholesMertonProcess(Handle<Quote>(spot),
Handle<YieldTermStructure>(qTS),
Handle<YieldTermStructure>(rTS),
Handle<BlackVolTermStructure>(volTS)));
boost::shared_ptr<PricingEngine> engine(
new ForwardVanillaEngine<AnalyticEuropeanEngine>(stochProcess));
for (Size i=0; i<LENGTH(values); i++) {
boost::shared_ptr<StrikedTypePayoff> payoff(
new PlainVanillaPayoff(values[i].type, 0.0));
Date exDate = today + Integer(values[i].t*360+0.5);
boost::shared_ptr<Exercise> exercise(new EuropeanExercise(exDate));
Date reset = today + Integer(values[i].start*360+0.5);
spot ->setValue(values[i].s);
qRate->setValue(values[i].q);
rRate->setValue(values[i].r);
vol ->setValue(values[i].v);
ForwardVanillaOption option(values[i].moneyness, reset,
payoff, exercise);
option.setPricingEngine(engine);
Real calculated = option.NPV();
Real error = std::fabs(calculated-values[i].result);
Real tolerance = 1e-4;
if (error>tolerance) {
REPORT_FAILURE("value", payoff, exercise, values[i].s,
values[i].q, values[i].r, today,
values[i].v, values[i].moneyness, reset,
values[i].result, calculated,
error, tolerance);
}
}
}
void ForwardOptionTest::testPerformanceValues() {
BOOST_MESSAGE("Testing forward performance option values...");
/* The data below are the performance equivalent of the
forward options tested above and taken from
"Option pricing formulas", E.G. Haug, McGraw-Hill 1998
*/
ForwardOptionData values[] = {
// type, moneyness, spot, div, rate,start, maturity, vol, result, tol
{ Option::Call, 1.1, 60.0, 0.04, 0.08, 0.25, 1.0, 0.30, 4.4064/60*std::exp(-0.04*0.25), 1.0e-4 },
{ Option::Put, 1.1, 60.0, 0.04, 0.08, 0.25, 1.0, 0.30, 8.2971/60*std::exp(-0.04*0.25), 1.0e-4 }
};
DayCounter dc = Actual360();
Date today = Date::todaysDate();
boost::shared_ptr<SimpleQuote> spot(new SimpleQuote(0.0));
boost::shared_ptr<SimpleQuote> qRate(new SimpleQuote(0.0));
Handle<YieldTermStructure> qTS(flatRate(today, qRate, dc));
boost::shared_ptr<SimpleQuote> rRate(new SimpleQuote(0.0));
Handle<YieldTermStructure> rTS(flatRate(today, rRate, dc));
boost::shared_ptr<SimpleQuote> vol(new SimpleQuote(0.0));
Handle<BlackVolTermStructure> volTS(flatVol(today, vol, dc));
boost::shared_ptr<BlackScholesMertonProcess> stochProcess(
new BlackScholesMertonProcess(Handle<Quote>(spot),
Handle<YieldTermStructure>(qTS),
Handle<YieldTermStructure>(rTS),
Handle<BlackVolTermStructure>(volTS)));
boost::shared_ptr<PricingEngine> engine(
new ForwardPerformanceVanillaEngine<AnalyticEuropeanEngine>(
stochProcess));
for (Size i=0; i<LENGTH(values); i++) {
boost::shared_ptr<StrikedTypePayoff> payoff(
new PlainVanillaPayoff(values[i].type, 0.0));
Date exDate = today + Integer(values[i].t*360+0.5);
boost::shared_ptr<Exercise> exercise(new EuropeanExercise(exDate));
Date reset = today + Integer(values[i].start*360+0.5);
spot ->setValue(values[i].s);
qRate->setValue(values[i].q);
rRate->setValue(values[i].r);
vol ->setValue(values[i].v);
ForwardVanillaOption option(values[i].moneyness, reset,
payoff, exercise);
option.setPricingEngine(engine);
Real calculated = option.NPV();
Real error = std::fabs(calculated-values[i].result);
Real tolerance = 1e-4;
if (error>tolerance) {
REPORT_FAILURE("value", payoff, exercise, values[i].s,
values[i].q, values[i].r, today,
values[i].v, values[i].moneyness, reset,
values[i].result, calculated,
error, tolerance);
}
}
}
QL_BEGIN_TEST_LOCALS(ForwardOptionTest)
template <template <class> class Engine>
void testForwardGreeks() {
std::map<std::string,Real> calculated, expected, tolerance;
tolerance["delta"] = 1.0e-5;
tolerance["gamma"] = 1.0e-5;
tolerance["theta"] = 1.0e-5;
tolerance["rho"] = 1.0e-5;
tolerance["divRho"] = 1.0e-5;
tolerance["vega"] = 1.0e-5;
Option::Type types[] = { Option::Call, Option::Put };
Real moneyness[] = { 0.9, 1.0, 1.1 };
Real underlyings[] = { 100.0 };
Rate qRates[] = { 0.04, 0.05, 0.06 };
Rate rRates[] = { 0.01, 0.05, 0.15 };
Integer lengths[] = { 1, 2 };
Integer startMonths[] = { 6, 9 };
Volatility vols[] = { 0.11, 0.50, 1.20 };
DayCounter dc = Actual360();
Date today = Date::todaysDate();
Settings::instance().evaluationDate() = today;
boost::shared_ptr<SimpleQuote> spot(new SimpleQuote(0.0));
boost::shared_ptr<SimpleQuote> qRate(new SimpleQuote(0.0));
Handle<YieldTermStructure> qTS(flatRate(qRate, dc));
boost::shared_ptr<SimpleQuote> rRate(new SimpleQuote(0.0));
Handle<YieldTermStructure> rTS(flatRate(rRate, dc));
boost::shared_ptr<SimpleQuote> vol(new SimpleQuote(0.0));
Handle<BlackVolTermStructure> volTS(flatVol(vol, dc));
boost::shared_ptr<BlackScholesMertonProcess> stochProcess(
new BlackScholesMertonProcess(Handle<Quote>(spot), qTS, rTS, volTS));
boost::shared_ptr<PricingEngine> engine(
new Engine<AnalyticEuropeanEngine>(stochProcess));
for (Size i=0; i<LENGTH(types); i++) {
for (Size j=0; j<LENGTH(moneyness); j++) {
for (Size k=0; k<LENGTH(lengths); k++) {
for (Size h=0; h<LENGTH(startMonths); h++) {
Date exDate = today + lengths[k]*Years;
boost::shared_ptr<Exercise> exercise(new EuropeanExercise(exDate));
Date reset = today + startMonths[h]*Months;
boost::shared_ptr<StrikedTypePayoff> payoff(
new PlainVanillaPayoff(types[i], 0.0));
ForwardVanillaOption option(moneyness[j], reset,
payoff, exercise);
option.setPricingEngine(engine);
for (Size l=0; l<LENGTH(underlyings); l++) {
for (Size m=0; m<LENGTH(qRates); m++) {
for (Size n=0; n<LENGTH(rRates); n++) {
for (Size p=0; p<LENGTH(vols); p++) {
Real u = underlyings[l];
Rate q = qRates[m],
r = rRates[n];
Volatility v = vols[p];
spot->setValue(u);
qRate->setValue(q);
rRate->setValue(r);
vol->setValue(v);
Real value = option.NPV();
calculated["delta"] = option.delta();
calculated["gamma"] = option.gamma();
calculated["theta"] = option.theta();
calculated["rho"] = option.rho();
calculated["divRho"] = option.dividendRho();
calculated["vega"] = option.vega();
if (value > spot->value()*1.0e-5) {
// perturb spot and get delta and gamma
Real du = u*1.0e-4;
spot->setValue(u+du);
Real value_p = option.NPV(),
delta_p = option.delta();
spot->setValue(u-du);
Real value_m = option.NPV(),
delta_m = option.delta();
spot->setValue(u);
expected["delta"] = (value_p - value_m)/(2*du);
expected["gamma"] = (delta_p - delta_m)/(2*du);
// perturb rates and get rho and dividend rho
Spread dr = r*1.0e-4;
rRate->setValue(r+dr);
value_p = option.NPV();
rRate->setValue(r-dr);
value_m = option.NPV();
rRate->setValue(r);
expected["rho"] = (value_p - value_m)/(2*dr);
Spread dq = q*1.0e-4;
qRate->setValue(q+dq);
value_p = option.NPV();
qRate->setValue(q-dq);
value_m = option.NPV();
qRate->setValue(q);
expected["divRho"] = (value_p - value_m)/(2*dq);
// perturb volatility and get vega
Volatility dv = v*1.0e-4;
vol->setValue(v+dv);
value_p = option.NPV();
vol->setValue(v-dv);
value_m = option.NPV();
vol->setValue(v);
expected["vega"] = (value_p - value_m)/(2*dv);
// perturb date and get theta
Time dT = dc.yearFraction(today-1, today+1);
Settings::instance().evaluationDate() = today-1;
value_m = option.NPV();
Settings::instance().evaluationDate() = today+1;
value_p = option.NPV();
Settings::instance().evaluationDate() = today;
expected["theta"] = (value_p - value_m)/dT;
// compare
std::map<std::string,Real>::iterator it;
for (it = calculated.begin();
it != calculated.end(); ++it) {
std::string greek = it->first;
Real expct = expected [greek],
calcl = calculated[greek],
tol = tolerance [greek];
Real error = relativeError(expct,calcl,u);
if (error>tol) {
REPORT_FAILURE(greek, payoff, exercise,
u, q, r, today, v,
moneyness[j], reset,
expct, calcl, error, tol);
}
}
}
}
}
}
}
}
}
}
}
}
QL_END_TEST_LOCALS(ForwardOptionTest)
void ForwardOptionTest::testGreeks() {
BOOST_MESSAGE("Testing forward option greeks...");
SavedSettings backup;
testForwardGreeks<ForwardVanillaEngine>();
}
void ForwardOptionTest::testPerformanceGreeks() {
BOOST_MESSAGE("Testing forward performance option greeks...");
SavedSettings backup;
testForwardGreeks<ForwardPerformanceVanillaEngine>();
}
test_suite* ForwardOptionTest::suite() {
test_suite* suite = BOOST_TEST_SUITE("Forward option tests");
suite->add(BOOST_TEST_CASE(&ForwardOptionTest::testValues));
suite->add(BOOST_TEST_CASE(&ForwardOptionTest::testGreeks));
suite->add(BOOST_TEST_CASE(&ForwardOptionTest::testPerformanceValues));
suite->add(BOOST_TEST_CASE(&ForwardOptionTest::testPerformanceGreeks));
return suite;
}
|