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
|
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2014 Master IMAFA - Polytech'Nice Sophia - Université de Nice Sophia Antipolis
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/exercise.hpp>
#include <ql/pricingengines/barrier/analyticpartialtimebarrieroptionengine.hpp>
#include <ql/math/distributions/bivariatenormaldistribution.hpp>
#include <ql/pricingengines/vanilla/analyticeuropeanengine.hpp>
#include <utility>
namespace QuantLib {
AnalyticPartialTimeBarrierOptionEngine::AnalyticPartialTimeBarrierOptionEngine(
ext::shared_ptr<GeneralizedBlackScholesProcess> process)
: process_(std::move(process)) {
registerWith(process_);
}
Real AnalyticPartialTimeBarrierOptionEngine::calculate(PartialTimeBarrierOption::arguments& arguments,
const ext::shared_ptr<PlainVanillaPayoff>& payoff,
const ext::shared_ptr<GeneralizedBlackScholesProcess>& process) const {
Barrier::Type barrierType = arguments.barrierType;
PartialBarrier::Range barrierRange = arguments.barrierRange;
Rate r = process->riskFreeRate()->zeroRate(residualTime(), Continuous,
NoFrequency);
Rate q = process->dividendYield()->zeroRate(residualTime(), Continuous,
NoFrequency);
Real barrier = arguments.barrier;
Real strike = payoff->strike();
switch (barrierType) {
case Barrier::DownOut:
switch (barrierRange) {
case PartialBarrier::Start:
return CA(1, barrier, strike, r, q);
case PartialBarrier::EndB1:
return CoB1(barrier, strike, r, q);
case PartialBarrier::EndB2:
return CoB2(Barrier::DownOut, barrier, strike, r, q);
default:
QL_FAIL("invalid barrier range");
}
break;
case Barrier::DownIn:
switch (barrierRange) {
case PartialBarrier::Start:
return CIA(1, barrier, strike, r, q);
case PartialBarrier::EndB1:
case PartialBarrier::EndB2:
QL_FAIL("Down-and-in partial-time end barrier is not implemented");
default:
QL_FAIL("invalid barrier range");
}
break;
case Barrier::UpOut:
switch (barrierRange) {
case PartialBarrier::Start:
return CA(-1, barrier, strike, r, q);
case PartialBarrier::EndB1:
return CoB1(barrier, strike, r, q);
case PartialBarrier::EndB2:
return CoB2(Barrier::UpOut, barrier, strike, r, q);
default:
QL_FAIL("invalid barrier range");
}
break;
case Barrier::UpIn:
switch (barrierRange) {
case PartialBarrier::Start:
return CIA(-1, barrier, strike, r, q);
case PartialBarrier::EndB1:
case PartialBarrier::EndB2:
QL_FAIL("Up-and-in partial-time end barrier is not implemented");
default:
QL_FAIL("invalid barrier range");
}
break;
default:
QL_FAIL("unknown barrier type");
}
}
void AnalyticPartialTimeBarrierOptionEngine::calculate() const {
ext::shared_ptr<PlainVanillaPayoff> payoff =
ext::dynamic_pointer_cast<PlainVanillaPayoff>(arguments_.payoff);
QL_REQUIRE(payoff, "non-plain payoff given");
QL_REQUIRE(payoff->strike()>0.0,
"strike must be positive");
Real spot = process_->x0();
QL_REQUIRE(spot > 0.0, "negative or null underlying given");
auto getSymmetricBarrierType = [](Barrier::Type barrierType) -> Barrier::Type {
if (barrierType == Barrier::UpIn) return Barrier::DownIn;
if (barrierType == Barrier::DownIn) return Barrier::UpIn;
if (barrierType == Barrier::UpOut) return Barrier::DownOut;
return Barrier::UpOut;
};
auto tmp_arguments_ = arguments_;
if (payoff->optionType() == Option::Put)
{
Real spotSq = spot * spot;
Real callStrike = spotSq / payoff->strike();
ext::shared_ptr<PlainVanillaPayoff> callPayoff =
ext::make_shared<PlainVanillaPayoff>(Option::Call, callStrike);
tmp_arguments_.barrierType = getSymmetricBarrierType(arguments_.barrierType);
tmp_arguments_.barrier = spotSq / arguments_.barrier;
tmp_arguments_.payoff = callPayoff;
auto callProcess = ext::make_shared<GeneralizedBlackScholesProcess>(
process_->stateVariable(),
process_->riskFreeRate(),
process_->dividendYield(),
process_->blackVolatility()
);
results_.value = payoff->strike() / spot * calculate(tmp_arguments_, callPayoff, callProcess);
} else
results_.value = calculate(tmp_arguments_, payoff, process_);
}
Real AnalyticPartialTimeBarrierOptionEngine::CoB2(
Barrier::Type barrierType,
Real barrier, Real strike, Rate r, Rate q) const {
Real result = 0.0;
Real b = r - q;
Real T = residualTime();
Real S = underlying();
Real mu_ = mu(strike, b);
Real g1_ = g1(barrier, strike, b);
Real g2_ = g2(barrier, strike, b);
Real g3_ = g3(barrier, strike, b);
Real g4_ = g4(barrier, strike, b);
Real e1_ = e1(barrier, strike, b);
Real e2_ = e2(barrier, strike, b);
Real e3_ = e3(barrier, strike, b);
Real e4_ = e4(barrier, strike, b);
Real rho_ = rho();
Real HSMu = HS(S, barrier, 2 * mu_);
Real HSMu1 = HS(S, barrier, 2 * (mu_ + 1));
Real X1 = strike * std::exp(-r * T);
if (strike < barrier){
switch (barrierType) {
case Barrier::DownOut:
result = S * std::exp((b - r) * T);
result *= (M(g1_, e1_, rho_) - HSMu1 * M(g3_, -e3_, -rho_));
result -= X1 * (M(g2_, e2_, rho_)-HSMu*M(g4_, -e4_, -rho_));
return result;
case Barrier::UpOut:
result = S * std::exp((b - r) * T);
result *= (M(-g1_, -e1_, rho_) - HSMu1 * M(-g3_, e3_, -rho_));
result -= X1 * (M(-g2_, -e2_, rho_) - HSMu * M(-g4_, e4_, -rho_));
result -= S * std::exp((b - r) * T) *
(M(-d1(strike, b), -e1_, rho_) - HSMu1 *
M(e3_, -f1(barrier, strike, b),-rho_));
result += X1 * (M(-d2(strike, b), -e2_, rho_) - HSMu *
M(e4_, -f2(barrier, strike, b), -rho_));
return result;
default:
QL_FAIL("invalid barrier type");
}
} else {
QL_FAIL("case of strike>barrier is not implemented for OutEnd B2 type");
}
}
Real AnalyticPartialTimeBarrierOptionEngine::CoB1(Real barrier, Real strike, Rate r, Rate q) const {
Real result = 0.0;
Rate b = r - q;
Real T = residualTime();
Real S = underlying();
Real mu_ = mu(strike, b);
Real g1_ = g1(barrier, strike, b);
Real g2_ = g2(barrier, strike, b);
Real g3_ = g3(barrier, strike, b);
Real g4_ = g4(barrier, strike, b);
Real e1_ = e1(barrier, strike, b);
Real e2_ = e2(barrier, strike, b);
Real e3_ = e3(barrier, strike, b);
Real e4_ = e4(barrier, strike, b);
Real rho_ = rho();
Real HSMu = HS(S, barrier, 2 * mu_);
Real HSMu1 = HS(S, barrier, 2 * (mu_ + 1));
Real X1 = strike * std::exp(-r * T);
if (strike > barrier) {
result = S * std::exp((b - r) * T);
result *= (M(d1(strike, b), e1_, rho_) - HSMu1 * M(f1(barrier, strike, b), -e3_, -rho_));
result -= X1 * (M(d2(strike, b), e2_, rho_) - HSMu * M(f2(barrier, strike, b), -e4_, -rho_));
return result;
} else {
Real S1 = S * std::exp((b - r) * T);
result = S1;
result *= (M(-g1_, -e1_, rho_) - HSMu1 * M(-g3_,e3_,-rho_));
result -= X1 * (M(-g2_, -e2_, rho_) - HSMu * M(-g4_, e4_, -rho_));
result -= S1 * (M(-d1(strike, b), -e1_, rho_) - HSMu1 * M(-f1(barrier, strike, b), e3_, -rho_));
result += X1 * (M(-d2(strike, b), -e2_, rho_) - HSMu * M(-f2(barrier, strike, b), e4_, -rho_));
result += S1 * (M(g1_, e1_, rho_) - HSMu1 * M(g3_, -e3_, -rho_));
result -= X1 * (M(g2_, e2_, rho_) - HSMu * M(g4_, -e4_, -rho_));
return result;
}
}
// eta = -1: Up-and-In Call
// eta = 1: Down-and-In Call
Real AnalyticPartialTimeBarrierOptionEngine::CIA(Integer eta, Real barrier, Real strike, Rate r, Rate q) const {
ext::shared_ptr<EuropeanExercise> exercise =
ext::dynamic_pointer_cast<EuropeanExercise>(arguments_.exercise);
ext::shared_ptr<PlainVanillaPayoff> payoff =
ext::dynamic_pointer_cast<PlainVanillaPayoff>(arguments_.payoff);
VanillaOption europeanOption(payoff, exercise);
europeanOption.setPricingEngine(
ext::make_shared<AnalyticEuropeanEngine>(process_));
return europeanOption.NPV() - CA(eta, barrier, strike, r, q);
}
Real AnalyticPartialTimeBarrierOptionEngine::CA(Integer eta, Real barrier, Real strike, Rate r, Rate q) const {
//Partial-Time-Start- OUT Call Option calculation
Real b = r - q;
Real rho_ = rho();
Real T = residualTime();
Real S = underlying();
Real mu_ = mu(strike, b);
Real e1_ = e1(barrier, strike, b);
Real e2_ = e2(barrier, strike, b);
Real e3_ = e3(barrier, strike, b);
Real e4_ = e4(barrier, strike, b);
Real HSMu = HS(S, barrier,2 * mu_);
Real HSMu1 = HS(S, barrier, 2 * (mu_ + 1));
Real result;
result = S * std::exp((b - r) * T);
result *= (M(d1(strike, b), eta * e1_, eta * rho_)-HSMu1 *
M(f1(barrier, strike, b), eta * e3_, eta * rho_));
result -= (strike * std::exp(-r * T) *
(M(d2(strike, b),eta * e2_, eta * rho_) - HSMu *
M(f2(barrier, strike, b), eta * e4_, eta * rho_)));
return result;
}
Real AnalyticPartialTimeBarrierOptionEngine::underlying() const {
return process_->x0();
}
Time AnalyticPartialTimeBarrierOptionEngine::residualTime() const {
return process_->time(arguments_.exercise->lastDate());
}
Time AnalyticPartialTimeBarrierOptionEngine::coverEventTime() const {
return process_->time(arguments_.coverEventDate);
}
Volatility AnalyticPartialTimeBarrierOptionEngine::volatility(Time t, Real strike) const {
return process_->blackVolatility()->blackVol(t, strike);
}
Real AnalyticPartialTimeBarrierOptionEngine::f1(Real barrier, Real strike, Rate b) const {
Real S = underlying();
Real T = residualTime();
Real sigma = volatility(T, strike);
return (std::log(S / strike) + 2 * std::log(barrier / S) +
(b + (std::pow(sigma, 2) / 2))*T) / (sigma*std::sqrt(T));
}
Real AnalyticPartialTimeBarrierOptionEngine::f2(Real barrier, Real strike, Rate b) const {
Time T = residualTime();
return f1(barrier, strike, b) - volatility(T, strike) * std::sqrt(T);
}
Real AnalyticPartialTimeBarrierOptionEngine::M(Real a, Real b, Real rho) const {
BivariateCumulativeNormalDistributionDr78 CmlNormDist(rho);
return CmlNormDist(a,b);
}
Real AnalyticPartialTimeBarrierOptionEngine::rho() const {
return std::sqrt(coverEventTime() / residualTime());
}
Rate AnalyticPartialTimeBarrierOptionEngine::mu(Real strike, Rate b) const {
Volatility vol = volatility(coverEventTime(), strike);
return (b - (vol * vol) / 2) / (vol * vol);
}
Real AnalyticPartialTimeBarrierOptionEngine::d1(Real strike, Rate b) const {
Time T2 = residualTime();
Volatility vol = volatility(T2, strike);
return (std::log(underlying() / strike) + (b + vol * vol / 2) * T2) / (std::sqrt(T2) * vol);
}
Real AnalyticPartialTimeBarrierOptionEngine::d2(Real strike, Rate b) const {
Time T2 = residualTime();
Volatility vol = volatility(T2, strike);
return d1(strike, b) - vol * std::sqrt(T2);
}
Real AnalyticPartialTimeBarrierOptionEngine::e1(Real barrier, Real strike, Rate b) const {
Time T1 = coverEventTime();
Volatility vol = volatility(T1, strike);
return (std::log(underlying() / barrier) + (b + vol * vol / 2) * T1) / (std::sqrt(T1) * vol);
}
Real AnalyticPartialTimeBarrierOptionEngine::e2(Real barrier, Real strike, Rate b) const {
Time T1 = coverEventTime();
Volatility vol = volatility(T1, strike);
return e1(barrier, strike, b) - vol * std::sqrt(T1);
}
Real AnalyticPartialTimeBarrierOptionEngine::e3(Real barrier, Real strike, Rate b) const {
Time T1 = coverEventTime();
Real vol = volatility(T1, strike);
return e1(barrier, strike, b) + (2 * std::log(barrier / underlying()) / (vol * std::sqrt(T1)));
}
Real AnalyticPartialTimeBarrierOptionEngine::e4(Real barrier, Real strike, Rate b) const {
Time t = coverEventTime();
return e3(barrier, strike, b) - volatility(t, strike) * std::sqrt(t);
}
Real AnalyticPartialTimeBarrierOptionEngine::g1(Real barrier, Real strike, Rate b) const {
Time T2 = residualTime();
Volatility vol = volatility(T2, strike);
return (std::log(underlying() / barrier) + (b + vol * vol / 2) * T2) / (std::sqrt(T2) * vol);
}
Real AnalyticPartialTimeBarrierOptionEngine::g2(Real barrier, Real strike, Rate b) const {
Time T2 = residualTime();
Volatility vol = volatility(T2, strike);
return g1(barrier, strike, b) - vol * std::sqrt(T2);
}
Real AnalyticPartialTimeBarrierOptionEngine::g3(Real barrier, Real strike, Rate b) const {
Time T2 = residualTime();
Real vol = volatility(T2, strike);
return g1(barrier, strike, b) + (2 * std::log(barrier / underlying()) /(vol * std::sqrt(T2)));
}
Real AnalyticPartialTimeBarrierOptionEngine::g4(Real barrier, Real strike, Rate b) const {
Time T2 = residualTime();
Real vol = volatility(T2, strike);
return g3(barrier, strike, b) - vol * std::sqrt(T2);
}
Real AnalyticPartialTimeBarrierOptionEngine::HS(Real S, Real H, Real power) const {
return std::pow((H / S), power);
}
}
|