/src/quantlib/ql/methods/finitedifferences/utilities/gbsmrndcalculator.cpp
Line | Count | Source |
1 | | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* |
4 | | Copyright (C) 2017 Klaus Spanderen |
5 | | |
6 | | This file is part of QuantLib, a free-software/open-source library |
7 | | for financial quantitative analysts and developers - http://quantlib.org/ |
8 | | |
9 | | QuantLib is free software: you can redistribute it and/or modify it |
10 | | under the terms of the QuantLib license. You should have received a |
11 | | copy of the license along with this program; if not, please email |
12 | | <quantlib-dev@lists.sf.net>. The license is also available online at |
13 | | <https://www.quantlib.org/license.shtml>. |
14 | | |
15 | | This program is distributed in the hope that it will be useful, but WITHOUT |
16 | | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
17 | | FOR A PARTICULAR PURPOSE. See the license for more details. |
18 | | */ |
19 | | |
20 | | /*! \file gbsmrndcalculator.hpp |
21 | | \brief risk neutral terminal density calculator for the |
22 | | Black-Scholes-Merton model with skew dependent volatility |
23 | | */ |
24 | | |
25 | | #include <ql/math/distributions/normaldistribution.hpp> |
26 | | #include <ql/math/solvers1d/brent.hpp> |
27 | | #include <ql/methods/finitedifferences/utilities/gbsmrndcalculator.hpp> |
28 | | #include <ql/pricingengines/blackcalculator.hpp> |
29 | | #include <ql/processes/blackscholesprocess.hpp> |
30 | | #include <utility> |
31 | | |
32 | | namespace QuantLib { |
33 | | |
34 | | GBSMRNDCalculator::GBSMRNDCalculator(ext::shared_ptr<GeneralizedBlackScholesProcess> process) |
35 | 0 | : process_(std::move(process)) {} |
36 | | |
37 | 0 | Real GBSMRNDCalculator::pdf(Real k, Time t) const { |
38 | 0 | const Real dk = 1e-3*k; |
39 | |
|
40 | 0 | return (cdf(k+dk, t) - cdf(k-dk, t)) / (2*dk); |
41 | 0 | } |
42 | | |
43 | 0 | Real GBSMRNDCalculator::cdf(Real k, Time t) const { |
44 | 0 | const Handle<BlackVolTermStructure> volTS |
45 | 0 | = process_->blackVolatility(); |
46 | |
|
47 | 0 | const Real dk = 1e-3*k; |
48 | 0 | const Real dvol_dk |
49 | 0 | = (volTS->blackVol(t, k+dk) - volTS->blackVol(t, k-dk)) / (2*dk); |
50 | |
|
51 | 0 | const DiscountFactor dR |
52 | 0 | = process_->riskFreeRate()->discount(t, true); |
53 | 0 | const DiscountFactor dD |
54 | 0 | = process_->dividendYield()->discount(t, true); |
55 | |
|
56 | 0 | const Real forward = process_->x0() * dD / dR; |
57 | 0 | const Real stdDev = std::sqrt( |
58 | 0 | process_->blackVolatility()->blackVariance(t, k, true)); |
59 | |
|
60 | 0 | if (forward <= k) { |
61 | 0 | const BlackCalculator calc(Option::Call, k, forward, stdDev, dR); |
62 | |
|
63 | 0 | return 1.0 + ( calc.strikeSensitivity() |
64 | 0 | + calc.vega(t) * dvol_dk) /dR; |
65 | 0 | } |
66 | 0 | else { |
67 | 0 | const BlackCalculator calc(Option::Put, k, forward, stdDev, dR); |
68 | |
|
69 | 0 | return ( calc.strikeSensitivity() |
70 | 0 | + calc.vega(t) * dvol_dk) /dR; |
71 | 0 | } |
72 | 0 | } |
73 | | |
74 | 0 | Real GBSMRNDCalculator::invcdf(Real q, Time t) const { |
75 | 0 | const Real fwd = process_->x0() |
76 | 0 | / process_->riskFreeRate()->discount(t, true) |
77 | 0 | * process_->dividendYield()->discount(t, true); |
78 | |
|
79 | 0 | const Volatility atmVariance = std::sqrt( |
80 | 0 | process_->blackVolatility()->blackVariance(t, fwd, true)); |
81 | |
|
82 | 0 | const Real atmX = InverseCumulativeNormal()(q); |
83 | |
|
84 | 0 | const Real guess = fwd*std::exp(atmVariance*atmX); |
85 | |
|
86 | 0 | Real lower = guess; |
87 | 0 | while (guess/lower < 65535.0 && cdf(lower, t) > q) |
88 | 0 | lower*=0.5; |
89 | |
|
90 | 0 | Real upper = guess; |
91 | 0 | while (upper/guess < 65535.0 && cdf(upper, t) < q) upper*=2; |
92 | |
|
93 | 0 | QL_REQUIRE(guess/lower < 65535.0 && upper/guess < 65535.0, |
94 | 0 | "Could not find an start interval with (" |
95 | 0 | << lower << ", " << upper << ") -> (" |
96 | 0 | << cdf(lower, t) << ", " << cdf(upper, t) << ")"); |
97 | | |
98 | 0 | return Brent().solve( |
99 | 0 | [&](Real _k) -> Real { return cdf(_k, t) - q; }, |
100 | 0 | 1e-10, 0.5*(lower+upper), lower, upper); |
101 | 0 | } |
102 | | } |