QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
gbsmrndcalculator.cpp
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 <http://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
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
32namespace QuantLib {
33
34 GBSMRNDCalculator::GBSMRNDCalculator(ext::shared_ptr<GeneralizedBlackScholesProcess> process)
35 : process_(std::move(process)) {}
36
38 const Real dk = 1e-3*k;
39
40 return (cdf(k+dk, t) - cdf(k-dk, t)) / (2*dk);
41 }
42
45 = process_->blackVolatility();
46
47 const Real dk = 1e-3*k;
48 const Real dvol_dk
49 = (volTS->blackVol(t, k+dk) - volTS->blackVol(t, k-dk)) / (2*dk);
50
51 const DiscountFactor dR
52 = process_->riskFreeRate()->discount(t, true);
53 const DiscountFactor dD
54 = process_->dividendYield()->discount(t, true);
55
56 const Real forward = process_->x0() * dD / dR;
57 const Real stdDev = std::sqrt(
58 process_->blackVolatility()->blackVariance(t, k, true));
59
60 if (forward <= k) {
61 const BlackCalculator calc(Option::Call, k, forward, stdDev, dR);
62
63 return 1.0 + ( calc.strikeSensitivity()
64 + calc.vega(t) * dvol_dk) /dR;
65 }
66 else {
67 const BlackCalculator calc(Option::Put, k, forward, stdDev, dR);
68
69 return ( calc.strikeSensitivity()
70 + calc.vega(t) * dvol_dk) /dR;
71 }
72 }
73
75 const Real fwd = process_->x0()
76 / process_->riskFreeRate()->discount(t, true)
77 * process_->dividendYield()->discount(t, true);
78
79 const Volatility atmVariance = std::sqrt(
80 process_->blackVolatility()->blackVariance(t, fwd, true));
81
82 const Real atmX = InverseCumulativeNormal()(q);
83
84 const Real guess = fwd*std::exp(atmVariance*atmX);
85
86 Real lower = guess;
87 while (guess/lower < 65535.0 && cdf(lower, t) > q)
88 lower*=0.5;
89
90 Real upper = guess;
91 while (upper/guess < 65535.0 && cdf(upper, t) < q) upper*=2;
92
93 QL_REQUIRE(guess/lower < 65535.0 && upper/guess < 65535.0,
94 "Could not find an start interval with ("
95 << lower << ", " << upper << ") -> ("
96 << cdf(lower, t) << ", " << cdf(upper, t) << ")");
97
98 return Brent().solve(
99 [&](Real _k) -> Real { return cdf(_k, t) - q; },
100 1e-10, 0.5*(lower+upper), lower, upper);
101 }
102}
Black 1976 calculator class.
Real vega(Time maturity) const
Brent 1-D solver
Definition: brent.hpp:37
Real cdf(Real s, Time t) const override
Real pdf(Real s, Time t) const override
Real invcdf(Real q, Time t) const override
const ext::shared_ptr< GeneralizedBlackScholesProcess > process_
GBSMRNDCalculator(ext::shared_ptr< GeneralizedBlackScholesProcess > process)
Shared handle to an observable.
Definition: handle.hpp:41
Inverse cumulative normal distribution function.
Real solve(const F &f, Real accuracy, Real guess, Real step) const
Definition: solver1d.hpp:84
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
Real DiscountFactor
discount factor between dates
Definition: types.hpp:66
Real Volatility
volatility
Definition: types.hpp:78
Definition: any.hpp:35
STL namespace.