QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
hestonblackvolsurface.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2015 Johannes Göttker-Schnetmann
5 Copyright (C) 2015 Klaus Spanderen
6
7 This file is part of QuantLib, a free-software/open-source library
8 for financial quantitative analysts and developers - http://quantlib.org/
9
10 QuantLib is free software: you can redistribute it and/or modify it
11 under the terms of the QuantLib license. You should have received a
12 copy of the license along with this program; if not, please email
13 <quantlib-dev@lists.sf.net>. The license is also available online at
14 <http://quantlib.org/license.shtml>.
15
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the license for more details.
19*/
20
25#include <ql/math/functional.hpp>
26#include <ql/math/solvers1d/brent.hpp>
27#include <ql/pricingengines/blackformula.hpp>
28#include <ql/termstructures/volatility/equityfx/hestonblackvolsurface.hpp>
29#include <ql/time/calendars/nullcalendar.hpp>
30#include <limits>
31#include <utility>
32
33namespace QuantLib {
34
35 namespace {
36 Real blackValue(Option::Type optionType, Real strike,
37 Real forward, Real maturity,
38 Volatility vol, Real discount, Real npv) {
39
40 return blackFormula(optionType, strike, forward,
41 std::max(0.0, vol)*std::sqrt(maturity),
42 discount)-npv;
43 }
44 }
45
47 const Handle<HestonModel>& hestonModel,
50 : BlackVolTermStructure(hestonModel->process()->riskFreeRate()->referenceDate(),
53 hestonModel->process()->riskFreeRate()->dayCounter()),
54 hestonModel_(hestonModel), cpxLogFormula_(cpxLogFormula),
55 integration_(std::move(integration)) {
57 }
58
60 return hestonModel_->process()->riskFreeRate()->dayCounter();
61 }
63 return Date::maxDate();
64 }
66 return 0.0;
67 }
69 return std::numeric_limits<Real>::max();
70 }
71
73 return squared(blackVolImpl(t, strike))*t;
74 }
75
77 const ext::shared_ptr<HestonProcess> process = hestonModel_->process();
78
79 const DiscountFactor df = process->riskFreeRate()->discount(t, true);
80 const DiscountFactor div = process->dividendYield()->discount(t, true);
81 const Real spotPrice = process->s0()->value();
82
83 const Real fwd = spotPrice
84 * process->dividendYield()->discount(t, true)
85 / process->riskFreeRate()->discount(t, true);
86
87
88 const PlainVanillaPayoff payoff(
89 fwd > strike ? Option::Put : Option::Call, strike);
90
91 const Real kappa = hestonModel_->kappa();
92 const Real theta = hestonModel_->theta();
93 const Real rho = hestonModel_->rho();
94 const Real sigma = hestonModel_->sigma();
95 const Real v0 = hestonModel_->v0();
96
97 AnalyticHestonEngine hestonEngine(
99
100 Real npv;
101 Size evaluations;
102
104 df, div, spotPrice, strike, t,
105 kappa, theta, sigma, v0, rho,
107 &hestonEngine, npv, evaluations);
108
109 if (npv <= 0.0) return std::sqrt(theta);
110
111 Brent solver;
112 solver.setMaxEvaluations(10000);
113 const Volatility guess = std::sqrt(theta);
114 constexpr double accuracy = std::numeric_limits<double>::epsilon();
115
116 return solver.solve([&](Volatility _v) { return blackValue(payoff.optionType(), strike, fwd,
117 t, _v, df, npv); },
118 accuracy, guess, 0.01);
119 }
120}
analytic Heston-model engine based on Fourier transform
static void doCalculation(Real riskFreeDiscount, Real dividendDiscount, Real spotPrice, Real strikePrice, Real term, Real kappa, Real theta, Real sigma, Real v0, Real rho, const TypePayoff &type, const Integration &integration, ComplexLogFormula cpxLog, const AnalyticHestonEngine *enginePtr, Real &value, Size &evaluations)
Black-volatility term structure.
Brent 1-D solver
Definition: brent.hpp:37
Concrete date class.
Definition: date.hpp:125
static Date maxDate()
latest allowed date
Definition: date.cpp:771
day counter class
Definition: daycounter.hpp:44
Shared handle to an observable.
Definition: handle.hpp:41
const ext::shared_ptr< T > & currentLink() const
dereferencing
Definition: handle.hpp:148
const AnalyticHestonEngine::Integration integration_
HestonBlackVolSurface(const Handle< HestonModel > &hestonModel, AnalyticHestonEngine::ComplexLogFormula cpxLogFormula=AnalyticHestonEngine::Gatheral, AnalyticHestonEngine::Integration integration=AnalyticHestonEngine::Integration::gaussLaguerre(164))
Real minStrike() const override
the minimum strike for which the term structure can return vols
const Handle< HestonModel > hestonModel_
DayCounter dayCounter() const override
the day counter used for date/time conversion
Date maxDate() const override
the latest date for which the curve can return values
Real blackVarianceImpl(Time t, Real strike) const override
Black variance calculation.
Volatility blackVolImpl(Time t, Real strike) const override
Black volatility calculation.
Real maxStrike() const override
the maximum strike for which the term structure can return vols
const AnalyticHestonEngine::ComplexLogFormula cpxLogFormula_
Calendar for reproducing theoretical calculations.
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:228
Plain-vanilla payoff.
Definition: payoffs.hpp:105
void setMaxEvaluations(Size evaluations)
Definition: solver1d.hpp:238
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
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
T squared(T x)
Definition: functional.hpp:37
Real blackFormula(Option::Type optionType, Real strike, Real forward, Real stdDev, Real discount, Real displacement)
STL namespace.