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
|
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2010 Dimitri Reiswich
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 <ql/experimental/fx/blackdeltacalculator.hpp>
namespace QuantLib {
BlackDeltaCalculator::BlackDeltaCalculator(
Option::Type ot,
DeltaVolQuote::DeltaType dt,
Real spot,
DiscountFactor dDiscount, // domestic discount
DiscountFactor fDiscount, // foreign discount
Real stdDev):
dt_(dt), ot_(ot),
dDiscount_(dDiscount), fDiscount_(fDiscount),
stdDev_(stdDev), spot_(spot),
forward_(spot*fDiscount/dDiscount), phi_(Integer(ot)) {
QL_REQUIRE(spot_>0.0,
"positive spot value required: " <<
spot_ << " not allowed");
QL_REQUIRE(dDiscount_>0.0,
"positive domestic discount factor required: " <<
dDiscount_ << " not allowed");
QL_REQUIRE(fDiscount_>0.0,
"positive foreign discount factor required: " <<
fDiscount_ << " not allowed");
QL_REQUIRE(stdDev_>=0.0,
"non-negative standard deviation required: "
<< stdDev_ << " not allowed");
fExpPos_ =forward_*exp(0.5*stdDev_*stdDev_);
fExpNeg_ =forward_*exp(-0.5*stdDev_*stdDev_);
}
Real BlackDeltaCalculator::deltaFromStrike(Real strike) const {
QL_REQUIRE(strike >=0.0,
"positive strike value required: " <<
strike << " not allowed");
Real res=0.0;
switch(dt_){
case DeltaVolQuote::Spot:
res=phi_*fDiscount_*cumD1(strike);
break;
case DeltaVolQuote::Fwd:
res=phi_*cumD1(strike);
break;
case DeltaVolQuote::PaSpot:
res=phi_*fDiscount_*cumD2(strike)*strike/forward_;
break;
case DeltaVolQuote::PaFwd:
res=phi_*cumD2(strike)*strike/forward_;
break;
default:
QL_FAIL("invalid delta type");
}
return res;
}
Real BlackDeltaCalculator::strikeFromDelta(Real delta) const {
return(strikeFromDelta(delta, dt_));
}
Real BlackDeltaCalculator::strikeFromDelta(Real delta,
DeltaVolQuote::DeltaType dt)
const{
Real res=0.0;
Real arg=0.0;
InverseCumulativeNormal f;
QL_REQUIRE(delta*phi_>=0.0, "Option type and delta are incoherent.");
switch (dt) {
case DeltaVolQuote::Spot:
QL_REQUIRE(std::fabs(delta)<=fDiscount_,
"Spot delta out of range.");
arg=-phi_*f(phi_*delta/fDiscount_)*stdDev_+0.5*stdDev_*stdDev_;
res=forward_*exp(arg);
break;
case DeltaVolQuote::Fwd:
QL_REQUIRE(std::fabs(delta)<=1.0,
"Forward delta out of range.");
arg=-phi_*f(phi_*delta)*stdDev_+0.5*stdDev_*stdDev_;
res=forward_*exp(arg);
break;
case DeltaVolQuote::PaSpot:
case DeltaVolQuote::PaFwd: {
// This has to be solved numerically. One of the
// problems is that the premium adjusted call delta is
// not monotonic in strike, such that two solutions
// might occur. The one right to the max of the delta is
// considered to be the correct strike. Some proper
// interval bounds for the strike need to be chosen, the
// numerics can otherwise be very unreliable and
// unstable. I've chosen Brent over Newton, since the
// interval can be specified explicitly and we can not
// run into the area on the left of the maximum. The
// put delta doesn't have this property and can be
// solved without any problems, but also numerically.
BlackDeltaPremiumAdjustedSolverClass f(
ot_, dt , spot_,dDiscount_, fDiscount_, stdDev_,delta);
Brent solver;
solver.setMaxEvaluations(1000);
Real accuracy = 1.0e-10;
Real rightLimit=0.0;
Real leftLimit=0.0;
// Strike of not premium adjusted is always to the right of premium adjusted
if (dt==DeltaVolQuote::PaSpot) {
rightLimit=strikeFromDelta(delta, DeltaVolQuote::Spot);
} else {
rightLimit=strikeFromDelta(delta, DeltaVolQuote::Fwd);
}
if (phi_<0) { // if put
res=solver.solve(f, accuracy, rightLimit, 0.0, spot_*100.0);
break;
} else {
// find out the left limit which is the strike
// corresponding to the value where premium adjusted
// deltas have their maximum.
BlackDeltaPremiumAdjustedMaxStrikeClass g(
ot_,dt, spot_,dDiscount_, fDiscount_, stdDev_);
leftLimit=solver.solve(g, accuracy, rightLimit*0.5,
0.0, rightLimit);
Real guess=leftLimit+(rightLimit-leftLimit)*0.5;
res=solver.solve(f, accuracy, guess, leftLimit, rightLimit);
} // end if phi<0 else
break;
}
default:
QL_FAIL("invalid delta type");
}
return res;
}
Real BlackDeltaCalculator::atmStrike(DeltaVolQuote::AtmType atmT) const {
Real res=0.0;
switch(atmT) {
case DeltaVolQuote::AtmDeltaNeutral:
if(dt_==DeltaVolQuote::Spot || dt_==DeltaVolQuote::Fwd){
res=fExpPos_;
} else {
res=fExpNeg_;
}
break;
case DeltaVolQuote::AtmFwd:
res=forward_;
break;
case DeltaVolQuote::AtmGammaMax: case DeltaVolQuote::AtmVegaMax:
res=fExpPos_;
break;
case DeltaVolQuote::AtmPutCall50:
QL_REQUIRE(dt_==DeltaVolQuote::Fwd,
"|PutDelta|=CallDelta=0.50 only possible for forward delta.");
res=fExpPos_;
break;
default:
QL_FAIL("invalid atm type");
}
return res;
}
Real BlackDeltaCalculator::cumD1(Real strike) const {
Real d1_=0.0;
Real cum_d1_pos_ = 1.0; // N(d1)
Real cum_d1_neg_ = 0.0; // N(-d1)
CumulativeNormalDistribution f;
if (stdDev_>=QL_EPSILON) {
if(strike>0) {
d1_ = std::log(forward_/strike)/stdDev_ + 0.5*stdDev_;
return f(phi_*d1_);
}
} else {
if (forward_<strike) {
cum_d1_pos_ = 0.0;
cum_d1_neg_ = 1.0;
} else if(forward_==strike){
d1_ = 0.5*stdDev_;
return f(phi_*d1_);
}
}
if (phi_>0) { // if Call
return cum_d1_pos_;
} else {
return cum_d1_neg_;
}
}
Real BlackDeltaCalculator::nD1(Real strike) const {
Real d1_=0.0;
Real n_d1_ = 0.0; // n(d1)
if (stdDev_>=QL_EPSILON){
if(strike>0){
d1_ = std::log(forward_/strike)/stdDev_ + 0.5*stdDev_;
CumulativeNormalDistribution f;
n_d1_ = f.derivative(d1_);
}
}
return n_d1_;
}
Real BlackDeltaCalculator::cumD2(Real strike) const {
Real d2_=0.0;
Real cum_d2_pos_= 1.0; // N(d2)
Real cum_d2_neg_= 0.0; // N(-d2)
CumulativeNormalDistribution f;
if (stdDev_>=QL_EPSILON){
if(strike>0){
d2_ = std::log(forward_/strike)/stdDev_ - 0.5*stdDev_;
return f(phi_*d2_);
}
} else {
if (forward_<strike) {
cum_d2_pos_= 0.0;
cum_d2_neg_= 1.0;
} else if (forward_==strike) {
d2_ = -0.5*stdDev_;
return(f(phi_*d2_));
}
}
if (phi_>0) { // if Call
return cum_d2_pos_;
} else {
return cum_d2_neg_;
}
}
Real BlackDeltaCalculator::nD2(Real strike) const {
Real d2_=0.0;
Real n_d2_= 0.0; // n(d2)
if (stdDev_>=QL_EPSILON){
if(strike>0){
d2_ = std::log(forward_/strike)/stdDev_ - 0.5*stdDev_;
CumulativeNormalDistribution f;
n_d2_ = f.derivative(d2_);
}
}
return n_d2_;
}
void BlackDeltaCalculator::setDeltaType(DeltaVolQuote::DeltaType dt){
dt_=dt;
}
void BlackDeltaCalculator::setOptionType(Option::Type ot){
ot_=ot;
phi_=Integer(ot_);
}
// helper classes
BlackDeltaPremiumAdjustedSolverClass::BlackDeltaPremiumAdjustedSolverClass(
Option::Type ot,
DeltaVolQuote::DeltaType dt,
Real spot,
DiscountFactor dDiscount, // domestic discount
DiscountFactor fDiscount, // foreign discount
Real stdDev,
Real delta):
bdc_(ot,dt,spot,dDiscount,fDiscount,stdDev), delta_(delta) {}
Real BlackDeltaPremiumAdjustedSolverClass::operator()(Real strike) const {
return bdc_.deltaFromStrike(strike)-delta_;
}
BlackDeltaPremiumAdjustedMaxStrikeClass::BlackDeltaPremiumAdjustedMaxStrikeClass(
Option::Type ot,
DeltaVolQuote::DeltaType dt,
Real spot,
DiscountFactor dDiscount, // domestic discount
DiscountFactor fDiscount, // foreign discount
Real stdDev):
bdc_(ot,dt,spot,dDiscount,fDiscount,stdDev), stdDev_(stdDev) {}
Real BlackDeltaPremiumAdjustedMaxStrikeClass::operator()(Real strike) const {
return bdc_.cumD2(strike)*stdDev_ - bdc_.nD2(strike);
}
}
|