QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
fdhestonbarrierengine.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 Klaus Spanderen
7
8 This file is part of QuantLib, a free-software/open-source library
9 for financial quantitative analysts and developers - http://quantlib.org/
10
11 QuantLib is free software: you can redistribute it and/or modify it
12 under the terms of the QuantLib license. You should have received a
13 copy of the license along with this program; if not, please email
14 <quantlib-dev@lists.sf.net>. The license is also available online at
15 <http://quantlib.org/license.shtml>.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the license for more details.
20*/
21
22#include <ql/methods/finitedifferences/meshers/fdmblackscholesmesher.hpp>
23#include <ql/methods/finitedifferences/meshers/fdmhestonvariancemesher.hpp>
24#include <ql/methods/finitedifferences/meshers/fdmmeshercomposite.hpp>
25#include <ql/methods/finitedifferences/operators/fdmlinearoplayout.hpp>
26#include <ql/methods/finitedifferences/stepconditions/fdmstepconditioncomposite.hpp>
27#include <ql/methods/finitedifferences/utilities/fdmdirichletboundary.hpp>
28#include <ql/methods/finitedifferences/utilities/fdmdividendhandler.hpp>
29#include <ql/methods/finitedifferences/utilities/fdminnervaluecalculator.hpp>
30#include <ql/instruments/vanillaoption.hpp>
31#include <ql/pricingengines/barrier/fdhestonbarrierengine.hpp>
32#include <ql/pricingengines/barrier/fdhestonrebateengine.hpp>
33#include <ql/pricingengines/vanilla/fdhestonvanillaengine.hpp>
34#include <utility>
35
36namespace QuantLib {
37
38 QL_DEPRECATED_DISABLE_WARNING
39
40 FdHestonBarrierEngine::FdHestonBarrierEngine(const ext::shared_ptr<HestonModel>& model,
41 Size tGrid,
42 Size xGrid,
43 Size vGrid,
44 Size dampingSteps,
45 const FdmSchemeDesc& schemeDesc,
46 ext::shared_ptr<LocalVolTermStructure> leverageFct,
47 const Real mixingFactor)
51 explicitDividends_(false), tGrid_(tGrid), xGrid_(xGrid), vGrid_(vGrid), dampingSteps_(dampingSteps),
52 schemeDesc_(schemeDesc), leverageFct_(std::move(leverageFct)), mixingFactor_(mixingFactor) {}
53
54 FdHestonBarrierEngine::FdHestonBarrierEngine(const ext::shared_ptr<HestonModel>& model,
55 DividendSchedule dividends,
56 Size tGrid,
57 Size xGrid,
58 Size vGrid,
59 Size dampingSteps,
60 const FdmSchemeDesc& schemeDesc,
61 ext::shared_ptr<LocalVolTermStructure> leverageFct,
62 const Real mixingFactor)
66 dividends_(std::move(dividends)), explicitDividends_(true),
67 tGrid_(tGrid), xGrid_(xGrid), vGrid_(vGrid), dampingSteps_(dampingSteps),
68 schemeDesc_(schemeDesc), leverageFct_(std::move(leverageFct)), mixingFactor_(mixingFactor) {}
69
70 QL_DEPRECATED_ENABLE_WARNING
71
73
74 // dividends will eventually be moved out of arguments, but for now we need the switch
75 QL_DEPRECATED_DISABLE_WARNING
76 const DividendSchedule& dividendSchedule = explicitDividends_ ? dividends_ : arguments_.cashFlow;
77 QL_DEPRECATED_ENABLE_WARNING
78
79 // 1. Mesher
80 const ext::shared_ptr<HestonProcess>& process = model_->process();
81 const Time maturity = process->time(arguments_.exercise->lastDate());
82
83 // 1.1 The variance mesher
84 const Size tGridMin = 5;
85 const Size tGridAvgSteps = std::max(tGridMin, tGrid_/50);
86
87 const ext::shared_ptr<FdmHestonLocalVolatilityVarianceMesher> vMesher
88 = ext::make_shared<FdmHestonLocalVolatilityVarianceMesher>(
89 vGrid_, process, leverageFct_, maturity, tGridAvgSteps, 0.0001, mixingFactor_);
90
91 // 1.2 The equity mesher
92 const ext::shared_ptr<StrikedTypePayoff> payoff =
93 ext::dynamic_pointer_cast<StrikedTypePayoff>(arguments_.payoff);
94
95 Real xMin=Null<Real>();
96 Real xMax=Null<Real>();
97 if ( arguments_.barrierType == Barrier::DownIn
98 || arguments_.barrierType == Barrier::DownOut) {
99 xMin = std::log(arguments_.barrier);
100 }
101 if ( arguments_.barrierType == Barrier::UpIn
102 || arguments_.barrierType == Barrier::UpOut) {
103 xMax = std::log(arguments_.barrier);
104 }
105
106 const ext::shared_ptr<Fdm1dMesher> equityMesher(
108 xGrid_,
110 process->s0(), process->dividendYield(),
111 process->riskFreeRate(), vMesher->volaEstimate()),
112 maturity, payoff->strike(),
113 xMin, xMax, 0.0001, 1.5,
114 std::make_pair(Null<Real>(), Null<Real>()),
115 dividendSchedule));
116
117 const ext::shared_ptr<FdmMesher> mesher (
118 ext::make_shared<FdmMesherComposite>(equityMesher, vMesher));
119
120 // 2. Calculator
121 ext::shared_ptr<FdmInnerValueCalculator> calculator(
122 ext::make_shared<FdmLogInnerValue>(payoff, mesher, 0));
123
124 // 3. Step conditions
125 std::list<ext::shared_ptr<StepCondition<Array> > > stepConditions;
126 std::list<std::vector<Time> > stoppingTimes;
127
128 // 3.1 Step condition if discrete dividends
129 ext::shared_ptr<FdmDividendHandler> dividendCondition(
130 ext::make_shared<FdmDividendHandler>(dividendSchedule, mesher,
131 process->riskFreeRate()->referenceDate(),
132 process->riskFreeRate()->dayCounter(), 0));
133
134 if (!dividendSchedule.empty()) {
135 stepConditions.push_back(dividendCondition);
136 std::vector<Time> dividendTimes = dividendCondition->dividendTimes();
137 // this effectively excludes times after maturity
138 for (auto& t: dividendTimes)
139 t = std::min(maturity, t);
140 stoppingTimes.push_back(dividendTimes);
141 }
142
143 QL_REQUIRE(arguments_.exercise->type() == Exercise::European,
144 "only european style option are supported");
145
146 ext::shared_ptr<FdmStepConditionComposite> conditions(
147 ext::make_shared<FdmStepConditionComposite>(stoppingTimes, stepConditions));
148
149 // 4. Boundary conditions
150 FdmBoundaryConditionSet boundaries;
151 if ( arguments_.barrierType == Barrier::DownIn
152 || arguments_.barrierType == Barrier::DownOut) {
153 boundaries.push_back(
154 ext::make_shared<FdmDirichletBoundary>(mesher, arguments_.rebate, 0,
156
157 }
158 if ( arguments_.barrierType == Barrier::UpIn
159 || arguments_.barrierType == Barrier::UpOut) {
160 boundaries.push_back(
161 ext::make_shared<FdmDirichletBoundary>(mesher, arguments_.rebate, 0,
163 }
164
165 // 5. Solver
166 FdmSolverDesc solverDesc = { mesher, boundaries, conditions,
167 calculator, maturity,
169
170 ext::shared_ptr<FdmHestonSolver> solver(ext::make_shared<FdmHestonSolver>(
171 Handle<HestonProcess>(process), solverDesc, schemeDesc_,
173
174 const Real spot = process->s0()->value();
175 results_.value = solver->valueAt(spot, process->v0());
176 results_.delta = solver->deltaAt(spot, process->v0());
177 results_.gamma = solver->gammaAt(spot, process->v0());
178 results_.theta = solver->thetaAt(spot, process->v0());
179
180 // 6. Calculate vanilla option and rebate for in-barriers
181 if ( arguments_.barrierType == Barrier::DownIn
182 || arguments_.barrierType == Barrier::UpIn) {
183 // Cast the payoff
184 ext::shared_ptr<StrikedTypePayoff> payoff =
185 ext::dynamic_pointer_cast<StrikedTypePayoff>(
186 arguments_.payoff);
187 // Calculate the vanilla option
188 VanillaOption vanillaOption(payoff, arguments_.exercise);
189 vanillaOption.setPricingEngine(ext::shared_ptr<PricingEngine>(
190 ext::make_shared<FdHestonVanillaEngine>(*model_, dividendSchedule,
191 tGrid_, xGrid_,
193 schemeDesc_)));
194 // Calculate the rebate value
195 BarrierOption rebateOption(arguments_.barrierType,
196 arguments_.barrier,
197 arguments_.rebate,
198 payoff, arguments_.exercise);
199 const Size xGridMin = 20;
200 const Size vGridMin = 10;
201 const Size rebateDampingSteps
202 = (dampingSteps_ > 0) ? std::min(Size(1), dampingSteps_/2) : 0;
203 rebateOption.setPricingEngine(
204 ext::make_shared<FdHestonRebateEngine>(*model_, dividendSchedule,
205 tGrid_,
206 std::max(xGridMin, xGrid_/4),
207 std::max(vGridMin, vGrid_/4),
208 rebateDampingSteps,
209 schemeDesc_));
210
211 results_.value = vanillaOption.NPV() + rebateOption.NPV() - results_.value;
212 results_.delta = vanillaOption.delta() + rebateOption.delta() - results_.delta;
213 results_.gamma = vanillaOption.gamma() + rebateOption.gamma() - results_.gamma;
214 results_.theta = vanillaOption.theta() + rebateOption.theta() - results_.theta;
215 }
216 }
217}
Barrier option on a single asset.
Single-asset barrier option with discrete dividends.
FdHestonBarrierEngine(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)
ext::shared_ptr< LocalVolTermStructure > leverageFct_
static ext::shared_ptr< GeneralizedBlackScholesProcess > processHelper(const Handle< Quote > &s0, const Handle< YieldTermStructure > &rTS, const Handle< YieldTermStructure > &qTS, Volatility vol)
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
Real NPV() const
returns the net present value of the instrument.
Definition: instrument.hpp:167
void setPricingEngine(const ext::shared_ptr< PricingEngine > &)
set the pricing engine to be used.
Definition: instrument.cpp:35
template class providing a null value for a given type.
Definition: null.hpp:76
Vanilla option (no discrete dividends, no barriers) on a single asset.
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
std::vector< ext::shared_ptr< Dividend > > DividendSchedule
OperatorTraits< FdmLinearOp >::bc_set FdmBoundaryConditionSet
STL namespace.