QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
fdhestonvanillaengine.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2008 Andreas Gaida
5 Copyright (C) 2008, 2009 Ralph Schreyer
6 Copyright (C) 2008, 2009, 2015 Klaus Spanderen
7 Copyright (C) 2015 Johannes Göttker-Schnetmann
8
9 This file is part of QuantLib, a free-software/open-source library
10 for financial quantitative analysts and developers - http://quantlib.org/
11
12 QuantLib is free software: you can redistribute it and/or modify it
13 under the terms of the QuantLib license. You should have received a
14 copy of the license along with this program; if not, please email
15 <quantlib-dev@lists.sf.net>. The license is also available online at
16 <http://quantlib.org/license.shtml>.
17
18 This program is distributed in the hope that it will be useful, but WITHOUT
19 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20 FOR A PARTICULAR PURPOSE. See the license for more details.
21*/
22
23#include <ql/methods/finitedifferences/meshers/fdmblackscholesmesher.hpp>
24#include <ql/methods/finitedifferences/meshers/fdmblackscholesmultistrikemesher.hpp>
25#include <ql/methods/finitedifferences/meshers/fdmhestonvariancemesher.hpp>
26#include <ql/methods/finitedifferences/meshers/fdmmeshercomposite.hpp>
27#include <ql/methods/finitedifferences/operators/fdmlinearoplayout.hpp>
28#include <ql/methods/finitedifferences/solvers/fdmhestonsolver.hpp>
29#include <ql/methods/finitedifferences/stepconditions/fdmstepconditioncomposite.hpp>
30#include <ql/methods/finitedifferences/utilities/fdminnervaluecalculator.hpp>
31#include <ql/pricingengines/vanilla/fdhestonvanillaengine.hpp>
32#include <ql/processes/batesprocess.hpp>
33#include <utility>
34
35namespace QuantLib {
36
37 QL_DEPRECATED_DISABLE_WARNING
38
39 FdHestonVanillaEngine::FdHestonVanillaEngine(const ext::shared_ptr<HestonModel>& model,
40 Size tGrid,
41 Size xGrid,
42 Size vGrid,
43 Size dampingSteps,
44 const FdmSchemeDesc& schemeDesc,
45 ext::shared_ptr<LocalVolTermStructure> leverageFct,
46 const Real mixingFactor)
50 explicitDividends_(false),
51 tGrid_(tGrid), xGrid_(xGrid), vGrid_(vGrid), dampingSteps_(dampingSteps),
52 schemeDesc_(schemeDesc), leverageFct_(std::move(leverageFct)),
53 quantoHelper_(ext::shared_ptr<FdmQuantoHelper>()), mixingFactor_(mixingFactor) {}
54
55 FdHestonVanillaEngine::FdHestonVanillaEngine(const ext::shared_ptr<HestonModel>& model,
56 DividendSchedule dividends,
57 Size tGrid,
58 Size xGrid,
59 Size vGrid,
60 Size dampingSteps,
61 const FdmSchemeDesc& schemeDesc,
62 ext::shared_ptr<LocalVolTermStructure> leverageFct,
63 const Real mixingFactor)
67 dividends_(std::move(dividends)), explicitDividends_(true),
68 tGrid_(tGrid), xGrid_(xGrid), vGrid_(vGrid), dampingSteps_(dampingSteps),
69 schemeDesc_(schemeDesc), leverageFct_(std::move(leverageFct)),
70 quantoHelper_(ext::shared_ptr<FdmQuantoHelper>()), mixingFactor_(mixingFactor) {}
71
72 FdHestonVanillaEngine::FdHestonVanillaEngine(const ext::shared_ptr<HestonModel>& model,
73 ext::shared_ptr<FdmQuantoHelper> quantoHelper,
74 Size tGrid,
75 Size xGrid,
76 Size vGrid,
77 Size dampingSteps,
78 const FdmSchemeDesc& schemeDesc,
79 ext::shared_ptr<LocalVolTermStructure> leverageFct,
80 const Real mixingFactor)
84 explicitDividends_(false),
85 tGrid_(tGrid), xGrid_(xGrid), vGrid_(vGrid), dampingSteps_(dampingSteps),
86 schemeDesc_(schemeDesc), leverageFct_(std::move(leverageFct)),
87 quantoHelper_(std::move(quantoHelper)), mixingFactor_(mixingFactor) {}
88
89 FdHestonVanillaEngine::FdHestonVanillaEngine(const ext::shared_ptr<HestonModel>& model,
90 DividendSchedule dividends,
91 ext::shared_ptr<FdmQuantoHelper> quantoHelper,
92 Size tGrid,
93 Size xGrid,
94 Size vGrid,
95 Size dampingSteps,
96 const FdmSchemeDesc& schemeDesc,
97 ext::shared_ptr<LocalVolTermStructure> leverageFct,
98 const Real mixingFactor)
102 dividends_(std::move(dividends)), explicitDividends_(true),
103 tGrid_(tGrid), xGrid_(xGrid), vGrid_(vGrid), dampingSteps_(dampingSteps),
104 schemeDesc_(schemeDesc), leverageFct_(std::move(leverageFct)),
105 quantoHelper_(std::move(quantoHelper)), mixingFactor_(mixingFactor) {}
106
107 QL_DEPRECATED_ENABLE_WARNING
108
110
111 // dividends will eventually be moved out of arguments, but for now we need the switch
112 QL_DEPRECATED_DISABLE_WARNING
113 const DividendSchedule& passedDividends = explicitDividends_ ? dividends_ : arguments_.cashFlow;
114 QL_DEPRECATED_ENABLE_WARNING
115
116 // 1. Mesher
117 const ext::shared_ptr<HestonProcess> process = model_->process();
118 const Time maturity = process->time(arguments_.exercise->lastDate());
119
120 // 1.1 The variance mesher
121 const Size tGridMin = 5;
122 const Size tGridAvgSteps = std::max(tGridMin, tGrid_/50);
123 const ext::shared_ptr<FdmHestonLocalVolatilityVarianceMesher> vMesher
124 = ext::make_shared<FdmHestonLocalVolatilityVarianceMesher>(
125 vGrid_, process, leverageFct_, maturity, tGridAvgSteps, 0.0001, mixingFactor_);
126
127 const Volatility avgVolaEstimate = vMesher->volaEstimate();
128
129 // 1.2 The equity mesher
130 const ext::shared_ptr<StrikedTypePayoff> payoff =
131 ext::dynamic_pointer_cast<StrikedTypePayoff>(arguments_.payoff);
132
133 ext::shared_ptr<Fdm1dMesher> equityMesher;
134 if (strikes_.empty()) {
135 equityMesher = ext::shared_ptr<Fdm1dMesher>(
137 xGrid_,
139 process->s0(), process->dividendYield(),
140 process->riskFreeRate(), avgVolaEstimate),
141 maturity, payoff->strike(),
142 Null<Real>(), Null<Real>(), 0.0001, 2.0,
143 std::pair<Real, Real>(payoff->strike(), 0.1),
144 passedDividends,
146 }
147 else {
148 QL_REQUIRE(passedDividends.empty(),
149 "multiple strikes engine does not work with discrete dividends");
150 equityMesher = ext::shared_ptr<Fdm1dMesher>(
152 xGrid_,
154 process->s0(), process->dividendYield(),
155 process->riskFreeRate(), avgVolaEstimate),
156 maturity, strikes_, 0.0001, 1.5,
157 std::pair<Real, Real>(payoff->strike(), 0.075)));
158 }
159
160 const ext::shared_ptr<FdmMesher> mesher(
161 new FdmMesherComposite(equityMesher, vMesher));
162
163 // 2. Calculator
164 const ext::shared_ptr<FdmInnerValueCalculator> calculator(
165 new FdmLogInnerValue(arguments_.payoff, mesher, 0));
166
167 // 3. Step conditions
168 const ext::shared_ptr<FdmStepConditionComposite> conditions =
170 passedDividends, arguments_.exercise,
171 mesher, calculator,
172 process->riskFreeRate()->referenceDate(),
173 process->riskFreeRate()->dayCounter());
174
175 // 4. Boundary conditions
176 const FdmBoundaryConditionSet boundaries;
177
178 // 5. Solver
179 FdmSolverDesc solverDesc = { mesher, boundaries, conditions,
180 calculator, maturity,
182
183 return solverDesc;
184 }
185
187
188 // dividends will eventually be moved out of arguments, but for now we need the switch
189 QL_DEPRECATED_DISABLE_WARNING
190 const DividendSchedule& passedDividends = explicitDividends_ ? dividends_ : arguments_.cashFlow;
191 QL_DEPRECATED_ENABLE_WARNING
192
193 // cache lookup for precalculated results
194 for (auto& cachedArgs2result : cachedArgs2results_) {
195 if (cachedArgs2result.first.exercise->type() == arguments_.exercise->type() &&
196 cachedArgs2result.first.exercise->dates() == arguments_.exercise->dates()) {
197 ext::shared_ptr<PlainVanillaPayoff> p1 =
198 ext::dynamic_pointer_cast<PlainVanillaPayoff>(
199 arguments_.payoff);
200 ext::shared_ptr<PlainVanillaPayoff> p2 =
201 ext::dynamic_pointer_cast<PlainVanillaPayoff>(cachedArgs2result.first.payoff);
202
203 if ((p1 != nullptr) && p1->strike() == p2->strike() &&
204 p1->optionType() == p2->optionType()) {
205 QL_REQUIRE(passedDividends.empty(),
206 "multiple strikes engine does not work with discrete dividends");
207 results_ = cachedArgs2result.second;
208 return;
209 }
210 }
211 }
212
213 const ext::shared_ptr<HestonProcess> process = model_->process();
214
215 ext::shared_ptr<FdmHestonSolver> solver(new FdmHestonSolver(
216 Handle<HestonProcess>(process),
220
221 const Real v0 = process->v0();
222 const Real spot = process->s0()->value();
223
224 results_.value = solver->valueAt(spot, v0);
225 results_.delta = solver->deltaAt(spot, v0);
226 results_.gamma = solver->gammaAt(spot, v0);
227 results_.theta = solver->thetaAt(spot, v0);
228
229 cachedArgs2results_.resize(strikes_.size());
230 const ext::shared_ptr<StrikedTypePayoff> payoff =
231 ext::dynamic_pointer_cast<StrikedTypePayoff>(arguments_.payoff);
232 for (Size i=0; i < strikes_.size(); ++i) {
233 cachedArgs2results_[i].first.exercise = arguments_.exercise;
234 cachedArgs2results_[i].first.payoff =
235 ext::make_shared<PlainVanillaPayoff>(
236 payoff->optionType(), strikes_[i]);
237 const Real d = payoff->strike()/strikes_[i];
238
239 QL_DEPRECATED_DISABLE_WARNING
241 QL_DEPRECATED_ENABLE_WARNING
242 results.value = solver->valueAt(spot*d, v0)/d;
243 results.delta = solver->deltaAt(spot*d, v0);
244 results.gamma = solver->gammaAt(spot*d, v0)*d;
245 results.theta = solver->thetaAt(spot*d, v0)/d;
246 }
247 }
248
250 cachedArgs2results_.clear();
251 QL_DEPRECATED_DISABLE_WARNING
255 QL_DEPRECATED_ENABLE_WARNING
256 }
257
259 const std::vector<Real>& strikes) {
260 strikes_ = strikes;
261 cachedArgs2results_.clear();
262 }
263
264
265 MakeFdHestonVanillaEngine::MakeFdHestonVanillaEngine(ext::shared_ptr<HestonModel> hestonModel)
266 : hestonModel_(std::move(hestonModel)),
267 schemeDesc_(ext::make_shared<FdmSchemeDesc>(FdmSchemeDesc::Hundsdorfer())) {}
268
270 const ext::shared_ptr<FdmQuantoHelper>& quantoHelper) {
271 quantoHelper_ = quantoHelper;
272 return *this;
273 }
274
277 tGrid_ = tGrid;
278 return *this;
279 }
280
283 xGrid_ = xGrid;
284 return *this;
285 }
286
289 vGrid_ = vGrid;
290 return *this;
291 }
292
295 dampingSteps_ = dampingSteps;
296 return *this;
297 }
298
301 const FdmSchemeDesc& schemeDesc) {
302 schemeDesc_ = ext::make_shared<FdmSchemeDesc>(schemeDesc);
303 return *this;
304 }
305
308 ext::shared_ptr<LocalVolTermStructure>& leverageFct) {
309 leverageFct_ = leverageFct;
310 return *this;
311 }
312
315 const std::vector<Date>& dividendDates,
316 const std::vector<Real>& dividendAmounts) {
317 dividends_ = DividendVector(dividendDates, dividendAmounts);
318 explicitDividends_ = true;
319 return *this;
320 }
321
322 MakeFdHestonVanillaEngine::operator
323 ext::shared_ptr<PricingEngine>() const {
324 if (explicitDividends_) {
325 return ext::make_shared<FdHestonVanillaEngine>(
326 hestonModel_,
327 dividends_,
328 quantoHelper_,
329 tGrid_, xGrid_, vGrid_, dampingSteps_,
330 *schemeDesc_,
331 leverageFct_);
332 } else {
333 return ext::make_shared<FdHestonVanillaEngine>(
334 hestonModel_,
335 quantoHelper_,
336 tGrid_, xGrid_, vGrid_, dampingSteps_,
337 *schemeDesc_,
338 leverageFct_);
339 }
340 }
341
342}
Single-asset vanilla option (no barriers) with discrete dividends.
FdmSolverDesc getSolverDesc(Real equityScaleFactor) const
const ext::shared_ptr< FdmQuantoHelper > quantoHelper_
const ext::shared_ptr< LocalVolTermStructure > leverageFct_
QL_DEPRECATED_DISABLE_WARNING std::vector< std::pair< DividendVanillaOption::arguments, DividendVanillaOption::results > > cachedArgs2results_
void enableMultipleStrikesCaching(const std::vector< Real > &strikes)
FdHestonVanillaEngine(const ext::shared_ptr< HestonModel > &model, Size tGrid=100, Size xGrid=100, Size vGrid=50, Size dampingSteps=0, const FdmSchemeDesc &schemeDesc=FdmSchemeDesc::Hundsdorfer(), ext::shared_ptr< LocalVolTermStructure > leverageFct={}, Real mixingFactor=1.0)
static ext::shared_ptr< GeneralizedBlackScholesProcess > processHelper(const Handle< Quote > &s0, const Handle< YieldTermStructure > &rTS, const Handle< YieldTermStructure > &qTS, Volatility vol)
static ext::shared_ptr< FdmStepConditionComposite > vanillaComposite(const DividendSchedule &schedule, const ext::shared_ptr< Exercise > &exercise, const ext::shared_ptr< FdmMesher > &mesher, const ext::shared_ptr< FdmInnerValueCalculator > &calculator, const Date &refDate, const DayCounter &dayCounter)
Base class for some pricing engine on a particular model.
Shared handle to an observable.
Definition: handle.hpp:41
Heston model for the stochastic volatility of an asset.
Definition: hestonmodel.hpp:42
MakeFdHestonVanillaEngine & withDampingSteps(Size dampingSteps)
MakeFdHestonVanillaEngine & withQuantoHelper(const ext::shared_ptr< FdmQuantoHelper > &quantoHelper)
MakeFdHestonVanillaEngine & withTGrid(Size tGrid)
MakeFdHestonVanillaEngine(ext::shared_ptr< HestonModel > hestonModel)
MakeFdHestonVanillaEngine & withCashDividends(const std::vector< Date > &dividendDates, const std::vector< Real > &dividendAmounts)
ext::shared_ptr< FdmQuantoHelper > quantoHelper_
MakeFdHestonVanillaEngine & withVGrid(Size vGrid)
ext::shared_ptr< FdmSchemeDesc > schemeDesc_
MakeFdHestonVanillaEngine & withFdmSchemeDesc(const FdmSchemeDesc &schemeDesc)
MakeFdHestonVanillaEngine & withLeverageFunction(ext::shared_ptr< LocalVolTermStructure > &leverageFct)
MakeFdHestonVanillaEngine & withXGrid(Size xGrid)
ext::shared_ptr< LocalVolTermStructure > leverageFct_
template class providing a null value for a given type.
Definition: null.hpp:76
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
Real Volatility
volatility
Definition: types.hpp:78
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
std::vector< ext::shared_ptr< Dividend > > DividendSchedule
std::vector< ext::shared_ptr< Dividend > > DividendVector(const std::vector< Date > &dividendDates, const std::vector< Real > &dividends)
helper function building a sequence of fixed dividends
Definition: dividend.cpp:35
OperatorTraits< FdmLinearOp >::bc_set FdmBoundaryConditionSet
STL namespace.