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 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
|
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2003, 2004, 2005, 2006 Ferdinando Ametrano
Copyright (C) 2006 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
<https://www.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 <ql/pricingengines/blackcalculator.hpp>
#include <ql/math/distributions/normaldistribution.hpp>
#include <ql/math/comparison.hpp>
namespace QuantLib {
class BlackCalculator::Calculator : public AcyclicVisitor,
public Visitor<Payoff>,
public Visitor<PlainVanillaPayoff>,
public Visitor<CashOrNothingPayoff>,
public Visitor<AssetOrNothingPayoff>,
public Visitor<GapPayoff> {
private:
BlackCalculator& black_;
public:
explicit Calculator(BlackCalculator& black) : black_(black) {}
void visit(Payoff&) override;
void visit(PlainVanillaPayoff&) override;
void visit(CashOrNothingPayoff&) override;
void visit(AssetOrNothingPayoff&) override;
void visit(GapPayoff&) override;
};
BlackCalculator::BlackCalculator(const ext::shared_ptr<StrikedTypePayoff>& p,
Real forward,
Real stdDev,
Real discount)
: strike_(p->strike()), forward_(forward), stdDev_(stdDev),
discount_(discount), variance_(stdDev*stdDev) {
initialize(p);
}
BlackCalculator::BlackCalculator(Option::Type optionType,
Real strike,
Real forward,
Real stdDev,
Real discount)
: strike_(strike), forward_(forward), stdDev_(stdDev),
discount_(discount), variance_(stdDev*stdDev) {
initialize(ext::shared_ptr<StrikedTypePayoff>(new
PlainVanillaPayoff(optionType, strike)));
}
void BlackCalculator::initialize(const ext::shared_ptr<StrikedTypePayoff>& p) {
QL_REQUIRE(strike_>=0.0,
"strike (" << strike_ << ") must be non-negative");
QL_REQUIRE(forward_>0.0,
"forward (" << forward_ << ") must be positive");
//QL_REQUIRE(displacement_>=0.0,
// "displacement (" << displacement_ << ") must be non-negative");
QL_REQUIRE(stdDev_>=0.0,
"stdDev (" << stdDev_ << ") must be non-negative");
QL_REQUIRE(discount_>0.0,
"discount (" << discount_ << ") must be positive");
if (stdDev_>=QL_EPSILON) {
if (close(strike_, 0.0)) {
d1_ = QL_MAX_REAL;
d2_ = QL_MAX_REAL;
cum_d1_ = 1.0;
cum_d2_ = 1.0;
n_d1_ = 0.0;
n_d2_ = 0.0;
} else {
d1_ = std::log(forward_/strike_)/stdDev_ + 0.5*stdDev_;
d2_ = d1_-stdDev_;
CumulativeNormalDistribution f;
cum_d1_ = f(d1_);
cum_d2_ = f(d2_);
n_d1_ = f.derivative(d1_);
n_d2_ = f.derivative(d2_);
}
} else {
if (close(forward_, strike_)) {
d1_ = 0;
d2_ = 0;
cum_d1_ = 0.5;
cum_d2_ = 0.5;
n_d1_ = M_SQRT_2 * M_1_SQRTPI;
n_d2_ = M_SQRT_2 * M_1_SQRTPI;
} else if (forward_>strike_) {
d1_ = QL_MAX_REAL;
d2_ = QL_MAX_REAL;
cum_d1_ = 1.0;
cum_d2_ = 1.0;
n_d1_ = 0.0;
n_d2_ = 0.0;
} else {
d1_ = QL_MIN_REAL;
d2_ = QL_MIN_REAL;
cum_d1_ = 0.0;
cum_d2_ = 0.0;
n_d1_ = 0.0;
n_d2_ = 0.0;
}
}
x_ = strike_;
DxDstrike_ = 1.0;
// the following one will probably disappear as soon as
// super-share will be properly handled
DxDs_ = 0.0;
// this part is always executed.
// in case of plain-vanilla payoffs, it is also the only part
// which is executed.
switch (p->optionType()) {
case Option::Call:
alpha_ = cum_d1_;// N(d1)
DalphaDd1_ = n_d1_;// n(d1)
beta_ = -cum_d2_;// -N(d2)
DbetaDd2_ = - n_d2_;// -n(d2)
break;
case Option::Put:
alpha_ = -1.0+cum_d1_;// -N(-d1)
DalphaDd1_ = n_d1_;// n( d1)
beta_ = 1.0-cum_d2_;// N(-d2)
DbetaDd2_ = - n_d2_;// -n( d2)
break;
default:
QL_FAIL("invalid option type");
}
// now dispatch on type.
Calculator calc(*this);
p->accept(calc);
}
void BlackCalculator::Calculator::visit(Payoff& p) {
QL_FAIL("unsupported payoff type: " << p.name());
}
void BlackCalculator::Calculator::visit(PlainVanillaPayoff&) {}
void BlackCalculator::Calculator::visit(CashOrNothingPayoff& payoff) {
black_.alpha_ = black_.DalphaDd1_ = 0.0;
black_.x_ = payoff.cashPayoff();
black_.DxDstrike_ = 0.0;
switch (payoff.optionType()) {
case Option::Call:
black_.beta_ = black_.cum_d2_;
black_.DbetaDd2_ = black_.n_d2_;
break;
case Option::Put:
black_.beta_ = 1.0-black_.cum_d2_;
black_.DbetaDd2_ = -black_.n_d2_;
break;
default:
QL_FAIL("invalid option type");
}
}
void BlackCalculator::Calculator::visit(AssetOrNothingPayoff& payoff) {
black_.beta_ = black_.DbetaDd2_ = 0.0;
switch (payoff.optionType()) {
case Option::Call:
black_.alpha_ = black_.cum_d1_;
black_.DalphaDd1_ = black_.n_d1_;
break;
case Option::Put:
black_.alpha_ = 1.0-black_.cum_d1_;
black_.DalphaDd1_ = -black_.n_d1_;
break;
default:
QL_FAIL("invalid option type");
}
}
void BlackCalculator::Calculator::visit(GapPayoff& payoff) {
black_.x_ = payoff.secondStrike();
black_.DxDstrike_ = 0.0;
}
Real BlackCalculator::value() const {
Real result = discount_ * (forward_ * alpha_ + x_ * beta_);
return result;
}
Real BlackCalculator::delta(Real spot) const {
QL_REQUIRE(spot > 0.0, "positive spot value required: " <<
spot << " not allowed");
// Handle zero volatility case
if (stdDev_ <= QL_EPSILON) {
// For zero volatility, delta is:
// ITM Call: 1.0, OTM Call: 0.0, ATM Call: 0.5
// ITM Put: -1.0, OTM Put: 0.0, ATM Put: -0.5
Real DforwardDs = forward_ / spot;
if (close(forward_, strike_)) {
// ATM case
if (alpha_ >= 0) { // Call
return discount_ * 0.5 * DforwardDs;
} else { // Put
return discount_ * (-0.5) * DforwardDs;
}
} else if (forward_ > strike_) {
// ITM Call, OTM Put
if (alpha_ >= 0) { // Call
return discount_ * 1.0 * DforwardDs;
} else { // Put
return 0.0;
}
} else {
// OTM Call, ITM Put
if (alpha_ >= 0) { // Call
return 0.0;
} else { // Put
return discount_ * (-1.0) * DforwardDs;
}
}
}
Real DforwardDs = forward_ / spot;
Real temp = stdDev_*spot;
Real DalphaDs = DalphaDd1_/temp;
Real DbetaDs = DbetaDd2_/temp;
Real temp2 = DalphaDs * forward_ + alpha_ * DforwardDs
+DbetaDs * x_ + beta_ * DxDs_;
return discount_ * temp2;
}
Real BlackCalculator::deltaForward() const {
// Handle zero volatility case
if (stdDev_ <= QL_EPSILON) {
// For zero volatility, forward delta is:
// ITM Call: 1.0, OTM Call: 0.0, ATM Call: 0.5
// ITM Put: -1.0, OTM Put: 0.0, ATM Put: -0.5
if (close(forward_, strike_)) {
// ATM case
if (alpha_ >= 0) { // Call
return discount_ * 0.5;
} else { // Put
return discount_ * (-0.5);
}
} else if (forward_ > strike_) {
// ITM Call, OTM Put
if (alpha_ >= 0) { // Call
return discount_ * 1.0;
} else { // Put
return 0.0;
}
} else {
// OTM Call, ITM Put
if (alpha_ >= 0) { // Call
return 0.0;
} else { // Put
return discount_ * (-1.0);
}
}
}
Real temp = stdDev_*forward_;
Real DalphaDforward = DalphaDd1_/temp;
Real DbetaDforward = DbetaDd2_/temp;
Real temp2 = DalphaDforward * forward_ + alpha_
+DbetaDforward * x_; // DXDforward = 0.0
return discount_ * temp2;
}
Real BlackCalculator::elasticity(Real spot) const {
Real val = value();
Real del = delta(spot);
if (val>QL_EPSILON)
return del/val*spot;
else if (std::fabs(del)<QL_EPSILON)
return 0.0;
else if (del>0.0)
return QL_MAX_REAL;
else
return QL_MIN_REAL;
}
Real BlackCalculator::elasticityForward() const {
Real val = value();
Real del = deltaForward();
if (val>QL_EPSILON)
return del/val*forward_;
else if (std::fabs(del)<QL_EPSILON)
return 0.0;
else if (del>0.0)
return QL_MAX_REAL;
else
return QL_MIN_REAL;
}
Real BlackCalculator::gamma(Real spot) const {
QL_REQUIRE(spot > 0.0, "positive spot value required: " <<
spot << " not allowed");
// Handle zero volatility case
if (stdDev_ <= QL_EPSILON) {
// For zero volatility, gamma is always 0 (no convexity)
return 0.0;
}
Real DforwardDs = forward_ / spot;
Real temp = stdDev_*spot;
Real DalphaDs = DalphaDd1_/temp;
Real DbetaDs = DbetaDd2_/temp;
Real D2alphaDs2 = - DalphaDs/spot*(1+d1_/stdDev_);
Real D2betaDs2 = - DbetaDs /spot*(1+d2_/stdDev_);
Real temp2 = D2alphaDs2 * forward_ + 2.0 * DalphaDs * DforwardDs
+D2betaDs2 * x_ + 2.0 * DbetaDs * DxDs_;
return discount_ * temp2;
}
Real BlackCalculator::gammaForward() const {
// Handle zero volatility case
if (stdDev_ <= QL_EPSILON) {
// For zero volatility, gamma is always 0 (no convexity)
return 0.0;
}
Real temp = stdDev_*forward_;
Real DalphaDforward = DalphaDd1_/temp;
Real DbetaDforward = DbetaDd2_/temp;
Real D2alphaDforward2 = - DalphaDforward/forward_*(1+d1_/stdDev_);
Real D2betaDforward2 = - DbetaDforward /forward_*(1+d2_/stdDev_);
Real temp2 = D2alphaDforward2 * forward_ + 2.0 * DalphaDforward
+D2betaDforward2 * x_; // DXDforward = 0.0
return discount_ * temp2;
}
Real BlackCalculator::theta(Real spot,
Time maturity) const {
QL_REQUIRE(maturity>=0.0,
"maturity (" << maturity << ") must be non-negative");
if (close(maturity, 0.0)) return 0.0;
return -( std::log(discount_) * value()
+std::log(forward_/spot) * spot * delta(spot)
+0.5*variance_ * spot * spot * gamma(spot))/maturity;
}
Real BlackCalculator::vega(Time maturity) const {
QL_REQUIRE(maturity>=0.0,
"negative maturity not allowed");
// Handle zero volatility case
if (stdDev_ <= QL_EPSILON) {
return 0.0;
}
Real temp = std::log(strike_/forward_)/variance_;
// actually DalphaDsigma / SQRT(T)
Real DalphaDsigma = DalphaDd1_*(temp+0.5);
Real DbetaDsigma = DbetaDd2_ *(temp-0.5);
Real temp2 = DalphaDsigma * forward_ + DbetaDsigma * x_;
return discount_ * std::sqrt(maturity) * temp2;
}
Real BlackCalculator::rho(Time maturity) const {
QL_REQUIRE(maturity>=0.0,
"negative maturity not allowed");
// Handle zero volatility case
if (stdDev_ <= QL_EPSILON) {
// For zero volatility, rho = T * (delta_forward * forward - value/discount)
Real deltaFwd = deltaForward();
return maturity * (deltaFwd * forward_ - value());
}
// actually DalphaDr / T
Real DalphaDr = DalphaDd1_/stdDev_;
Real DbetaDr = DbetaDd2_/stdDev_;
Real temp = DalphaDr * forward_ + alpha_ * forward_ + DbetaDr * x_;
return maturity * (discount_ * temp - value());
}
Real BlackCalculator::dividendRho(Time maturity) const {
QL_REQUIRE(maturity>=0.0,
"negative maturity not allowed");
// Handle zero volatility case
if (stdDev_ <= QL_EPSILON) {
// For zero volatility, dividend rho = -T * discount * delta_forward * forward
Real deltaFwd = deltaForward() / discount_; // Remove discount to get pure delta
return -maturity * discount_ * deltaFwd * forward_;
}
// actually DalphaDq / T
Real DalphaDq = -DalphaDd1_/stdDev_;
Real DbetaDq = -DbetaDd2_/stdDev_;
Real temp = DalphaDq * forward_ - alpha_ * forward_ + DbetaDq * x_;
return maturity * discount_ * temp;
}
Real BlackCalculator::strikeSensitivity() const {
// Handle zero volatility case
if (stdDev_ <= QL_EPSILON) {
// For zero volatility, strike sensitivity is:
// Call: -N(d2) where d2 -> 1 (ITM), 0 (OTM), 0.5 (ATM)
// Put: N(-d2) = 1 - N(d2)
if (close(forward_, strike_)) {
// ATM case
if (alpha_ >= 0) { // Call
return -discount_ * 0.5;
} else { // Put
return discount_ * 0.5;
}
} else if (forward_ > strike_) {
// ITM Call, OTM Put
if (alpha_ >= 0) { // Call
return -discount_ * 1.0;
} else { // Put
return discount_ * 0.0;
}
} else {
// OTM Call, ITM Put
if (alpha_ >= 0) { // Call
return -discount_ * 0.0;
} else { // Put
return discount_ * 1.0;
}
}
}
Real temp = stdDev_*strike_;
Real DalphaDstrike = -DalphaDd1_/temp;
Real DbetaDstrike = -DbetaDd2_/temp;
Real temp2 =
DalphaDstrike * forward_ + DbetaDstrike * x_ + beta_ * DxDstrike_;
return discount_ * temp2;
}
Real BlackCalculator::strikeGamma() const {
// Handle zero volatility case
if (stdDev_ <= QL_EPSILON) {
// For zero volatility, strike gamma is 0 (no convexity)
return 0.0;
}
Real temp = stdDev_*strike_;
Real DalphaDstrike = -DalphaDd1_/temp;
Real DbetaDstrike = -DbetaDd2_/temp;
Real D2alphaD2strike = -DalphaDstrike/strike_*(1-d1_/stdDev_);
Real D2betaD2strike = -DbetaDstrike /strike_*(1-d2_/stdDev_);
Real temp2 =
D2alphaD2strike * forward_ + D2betaD2strike * x_
+ 2.0*DbetaDstrike *DxDstrike_;
return discount_ * temp2;
}
}
|