QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
hestonblackvolsurface.cpp
Go to the documentation of this file.
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
21/*! \file hestonblackvolsurface.hpp
22 \brief Black volatility surface back by Heston model
23*/
24
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 AnalyticHestonEngine hestonEngine(
79
80 const ext::shared_ptr<HestonProcess>& process = hestonModel_->process();
81
82 const DiscountFactor df = process->riskFreeRate()->discount(t, true);
83
84 const Real fwd = process->s0()->value()
85 * process->dividendYield()->discount(t, true) / df;
86
87 const ext::shared_ptr<PlainVanillaPayoff> payoff =
88 ext::make_shared<PlainVanillaPayoff>(
89 fwd > strike ? Option::Put : Option::Call, strike);
90
91 const Real npv = hestonEngine.priceVanillaPayoff(payoff, t);
92
93 const Real theta = hestonModel_->theta();
94 if (npv <= 0.0) return std::sqrt(theta);
95
96 Brent solver;
97 solver.setMaxEvaluations(10000);
98 const Volatility guess = std::sqrt(theta);
99 constexpr double accuracy = std::numeric_limits<double>::epsilon();
100
101 return solver.solve(
102 [&](Volatility _v) {
103 return blackValue(
104 payoff->optionType(), strike, fwd, t, _v, df, npv);
105 },
106 accuracy, guess, 0.01
107 );
108 }
109}
Black formula.
Brent 1-D solver.
analytic Heston-model engine based on Fourier transform
Real priceVanillaPayoff(const ext::shared_ptr< PlainVanillaPayoff > &payoff, const Date &maturity) const
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:173
const AnalyticHestonEngine::Integration integration_
HestonBlackVolSurface(const Handle< HestonModel > &hestonModel, AnalyticHestonEngine::ComplexLogFormula cpxLogFormula=AnalyticHestonEngine::AngledContour, AnalyticHestonEngine::Integration integration=AnalyticHestonEngine::Integration::gaussLaguerre(160))
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
void setMaxEvaluations(Size evaluations)
Definition: solver1d.hpp:238
Real solve(const F &f, Real accuracy, Real guess, Real step) const
Definition: solver1d.hpp:84
const DefaultType & t
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
Black volatility surface back by Heston model.
Real theta
ext::shared_ptr< QuantLib::Payoff > payoff
functionals and combinators not included in the STL
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.
Calendar for reproducing theoretical calculations.