QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
analytichestonengine.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2004, 2005, 2008 Klaus Spanderen
5 Copyright (C) 2007 StatPro Italia srl
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#ifndef quantlib_analytic_heston_engine_hpp
26#define quantlib_analytic_heston_engine_hpp
27
28#include <ql/utilities/null.hpp>
29#include <ql/math/integrals/integral.hpp>
30#include <ql/math/integrals/gaussianquadratures.hpp>
31#include <ql/pricingengines/genericmodelengine.hpp>
32#include <ql/models/equity/hestonmodel.hpp>
33#include <ql/instruments/vanillaoption.hpp>
34#include <ql/functional.hpp>
35#include <complex>
36
37namespace QuantLib {
38
40
88 : public GenericModelEngine<HestonModel,
89 VanillaOption::arguments,
90 VanillaOption::results> {
91 public:
92 class Integration;
94 // Gatheral form of characteristic function w/o control variate
96 // old branch correction form of the characteristic function w/o control variate
98 // Gatheral form with Andersen-Piterbarg control variate
100 // same as AndersenPiterbarg, but a slightly better control variate
102 // Gatheral form with asymptotic expansion of the characteristic function as control variate
103 // https://hpcquantlib.wordpress.com/2020/08/30/a-novel-control-variate-for-the-heston-model
105 // auto selection of best control variate algorithm from above
107 };
108
109 // Simple to use constructor: Using adaptive
110 // Gauss-Lobatto integration and Gatheral's version of complex log.
111 // Be aware: using a too large number for maxEvaluations might result
112 // in a stack overflow as the Lobatto integration is a recursive
113 // algorithm.
114 AnalyticHestonEngine(const ext::shared_ptr<HestonModel>& model,
115 Real relTolerance, Size maxEvaluations);
116
117 // Constructor using Laguerre integration
118 // and Gatheral's version of complex log.
119 AnalyticHestonEngine(const ext::shared_ptr<HestonModel>& model,
120 Size integrationOrder = 144);
121
122 // Constructor giving full control
123 // over the Fourier integration algorithm
124 AnalyticHestonEngine(const ext::shared_ptr<HestonModel>& model,
125 ComplexLogFormula cpxLog, const Integration& itg,
126 Real andersenPiterbargEpsilon = 1e-8);
127
128 // normalized characteristic function
129 std::complex<Real> chF(const std::complex<Real>& z, Time t) const;
130 std::complex<Real> lnChF(const std::complex<Real>& z, Time t) const;
131
132 void calculate() const override;
134
135 static void doCalculation(Real riskFreeDiscount,
136 Real dividendDiscount,
137 Real spotPrice,
138 Real strikePrice,
139 Real term,
140 Real kappa,
141 Real theta,
142 Real sigma,
143 Real v0,
144 Real rho,
145 const TypePayoff& type,
146 const Integration& integration,
147 ComplexLogFormula cpxLog,
148 const AnalyticHestonEngine* enginePtr,
149 Real& value,
150 Size& evaluations);
151
153 Time t, Real v0, Real kappa, Real theta, Real sigma, Real rho);
154
155 class AP_Helper {
156 public:
157 AP_Helper(Time term,
158 Real fwd,
159 Real strike,
160 ComplexLogFormula cpxLog,
161 const AnalyticHestonEngine* enginePtr);
162
163 Real operator()(Real u) const;
165
166 private:
167 const Time term_;
172 std::complex<Real> phi_, psi_;
173 };
174
175 protected:
176 // call back for extended stochastic volatility
177 // plus jump diffusion engines like bates model
178 virtual std::complex<Real> addOnTerm(Real phi,
179 Time t,
180 Size j) const;
181
182 private:
183 class Fj_Helper;
184
187 const ext::shared_ptr<Integration> integration_;
189 };
190
191
193 public:
194 // non adaptive integration algorithms based on Gaussian quadrature
195 static Integration gaussLaguerre (Size integrationOrder = 128);
196 static Integration gaussLegendre (Size integrationOrder = 128);
197 static Integration gaussChebyshev (Size integrationOrder = 128);
198 static Integration gaussChebyshev2nd(Size integrationOrder = 128);
199
200 // for an adaptive integration algorithm Gatheral's version has to
201 // be used.Be aware: using a too large number for maxEvaluations might
202 // result in a stack overflow as the these integrations are based on
203 // recursive algorithms.
204 static Integration gaussLobatto(Real relTolerance, Real absTolerance,
205 Size maxEvaluations = 1000);
206
207 // usually these routines have a poor convergence behavior.
208 static Integration gaussKronrod(Real absTolerance,
209 Size maxEvaluations = 1000);
210 static Integration simpson(Real absTolerance,
211 Size maxEvaluations = 1000);
212 static Integration trapezoid(Real absTolerance,
213 Size maxEvaluations = 1000);
214 static Integration discreteSimpson(Size evaluation = 1000);
215 static Integration discreteTrapezoid(Size evaluation = 1000);
216
218 Real c_inf, Real epsilon, Real v0, Real t);
219
220 Real calculate(Real c_inf,
221 const ext::function<Real(Real)>& f,
222 const ext::function<Real()>& maxBound = {}) const;
223
224 Real calculate(Real c_inf,
225 const ext::function<Real(Real)>& f,
226 Real maxBound) const;
227
229 bool isAdaptiveIntegration() const;
230
231 private:
237
238 Integration(Algorithm intAlgo, ext::shared_ptr<GaussianQuadrature> quadrature);
239
240 Integration(Algorithm intAlgo, ext::shared_ptr<Integrator> integrator);
241
243 const ext::shared_ptr<Integrator> integrator_;
244 const ext::shared_ptr<GaussianQuadrature> gaussianQuadrature_;
245 };
246
247 // inline
248
249 inline
251 Time,
252 Size) const {
253 return std::complex<Real>(0,0);
254 }
255}
256
257#endif
const AnalyticHestonEngine *const enginePtr_
static Real andersenPiterbargIntegrationLimit(Real c_inf, Real epsilon, Real v0, Real t)
static Integration discreteSimpson(Size evaluation=1000)
static Integration gaussChebyshev2nd(Size integrationOrder=128)
static Integration simpson(Real absTolerance, Size maxEvaluations=1000)
const ext::shared_ptr< GaussianQuadrature > gaussianQuadrature_
static Integration gaussLegendre(Size integrationOrder=128)
static Integration gaussChebyshev(Size integrationOrder=128)
static Integration gaussLobatto(Real relTolerance, Real absTolerance, Size maxEvaluations=1000)
static Integration trapezoid(Real absTolerance, Size maxEvaluations=1000)
static Integration gaussLaguerre(Size integrationOrder=128)
static Integration gaussKronrod(Real absTolerance, Size maxEvaluations=1000)
const ext::shared_ptr< Integrator > integrator_
static Integration discreteTrapezoid(Size evaluation=1000)
analytic Heston-model engine based on Fourier transform
std::complex< Real > lnChF(const std::complex< Real > &z, Time t) const
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)
const ext::shared_ptr< Integration > integration_
std::complex< Real > chF(const std::complex< Real > &z, Time t) const
virtual std::complex< Real > addOnTerm(Real phi, Time t, Size j) const
static ComplexLogFormula optimalControlVariate(Time t, Real v0, Real kappa, Real theta, Real sigma, Real rho)
Base class for some pricing engine on a particular model.
Intermediate class for put/call payoffs.
Definition: payoffs.hpp:49
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35