QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
fdhestonbarrierengine.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) 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
34#include <utility>
35
36namespace QuantLib {
37
38 FdHestonBarrierEngine::FdHestonBarrierEngine(const ext::shared_ptr<HestonModel>& model,
39 Size tGrid,
40 Size xGrid,
41 Size vGrid,
42 Size dampingSteps,
43 const FdmSchemeDesc& schemeDesc,
44 ext::shared_ptr<LocalVolTermStructure> leverageFct,
45 const Real mixingFactor)
48 BarrierOption::results>(model),
49 tGrid_(tGrid), xGrid_(xGrid), vGrid_(vGrid), dampingSteps_(dampingSteps),
50 schemeDesc_(schemeDesc), leverageFct_(std::move(leverageFct)), mixingFactor_(mixingFactor) {}
51
52 FdHestonBarrierEngine::FdHestonBarrierEngine(const ext::shared_ptr<HestonModel>& model,
53 DividendSchedule dividends,
54 Size tGrid,
55 Size xGrid,
56 Size vGrid,
57 Size dampingSteps,
58 const FdmSchemeDesc& schemeDesc,
59 ext::shared_ptr<LocalVolTermStructure> leverageFct,
60 const Real mixingFactor)
63 BarrierOption::results>(model),
64 dividends_(std::move(dividends)),
65 tGrid_(tGrid), xGrid_(xGrid), vGrid_(vGrid), dampingSteps_(dampingSteps),
66 schemeDesc_(schemeDesc), leverageFct_(std::move(leverageFct)), mixingFactor_(mixingFactor) {}
67
69
70 // 1. Mesher
71 const ext::shared_ptr<HestonProcess>& process = model_->process();
72 const Time maturity = process->time(arguments_.exercise->lastDate());
73
74 // 1.1 The variance mesher
75 const Size tGridMin = 5;
76 const Size tGridAvgSteps = std::max(tGridMin, tGrid_/50);
77
78 const ext::shared_ptr<FdmHestonLocalVolatilityVarianceMesher> vMesher
79 = ext::make_shared<FdmHestonLocalVolatilityVarianceMesher>(
80 vGrid_, process, leverageFct_, maturity, tGridAvgSteps, 0.0001, mixingFactor_);
81
82 // 1.2 The equity mesher
83 const ext::shared_ptr<StrikedTypePayoff> payoff =
84 ext::dynamic_pointer_cast<StrikedTypePayoff>(arguments_.payoff);
85
86 Real xMin=Null<Real>();
87 Real xMax=Null<Real>();
88 if ( arguments_.barrierType == Barrier::DownIn
89 || arguments_.barrierType == Barrier::DownOut) {
90 xMin = std::log(arguments_.barrier);
91 }
92 if ( arguments_.barrierType == Barrier::UpIn
93 || arguments_.barrierType == Barrier::UpOut) {
94 xMax = std::log(arguments_.barrier);
95 }
96
97 const ext::shared_ptr<Fdm1dMesher> equityMesher(
99 xGrid_,
101 process->s0(), process->dividendYield(),
102 process->riskFreeRate(), vMesher->volaEstimate()),
103 maturity, payoff->strike(),
104 xMin, xMax, 0.0001, 1.5,
105 std::make_pair(Null<Real>(), Null<Real>()),
106 dividends_));
107
108 const ext::shared_ptr<FdmMesher> mesher (
109 ext::make_shared<FdmMesherComposite>(equityMesher, vMesher));
110
111 // 2. Calculator
112 ext::shared_ptr<FdmInnerValueCalculator> calculator(
113 ext::make_shared<FdmLogInnerValue>(payoff, mesher, 0));
114
115 // 3. Step conditions
116 std::list<ext::shared_ptr<StepCondition<Array> > > stepConditions;
117 std::list<std::vector<Time> > stoppingTimes;
118
119 // 3.1 Step condition if discrete dividends
120 ext::shared_ptr<FdmDividendHandler> dividendCondition(
121 ext::make_shared<FdmDividendHandler>(dividends_, mesher,
122 process->riskFreeRate()->referenceDate(),
123 process->riskFreeRate()->dayCounter(), 0));
124
125 if (!dividends_.empty()) {
126 stepConditions.push_back(dividendCondition);
127 std::vector<Time> dividendTimes = dividendCondition->dividendTimes();
128 // this effectively excludes times after maturity
129 for (auto& t: dividendTimes)
130 t = std::min(maturity, t);
131 stoppingTimes.push_back(dividendTimes);
132 }
133
134 QL_REQUIRE(arguments_.exercise->type() == Exercise::European,
135 "only european style option are supported");
136
137 ext::shared_ptr<FdmStepConditionComposite> conditions(
138 ext::make_shared<FdmStepConditionComposite>(stoppingTimes, stepConditions));
139
140 // 4. Boundary conditions
141 FdmBoundaryConditionSet boundaries;
142 if ( arguments_.barrierType == Barrier::DownIn
143 || arguments_.barrierType == Barrier::DownOut) {
144 boundaries.push_back(
145 ext::make_shared<FdmDirichletBoundary>(mesher, arguments_.rebate, 0,
147
148 }
149 if ( arguments_.barrierType == Barrier::UpIn
150 || arguments_.barrierType == Barrier::UpOut) {
151 boundaries.push_back(
152 ext::make_shared<FdmDirichletBoundary>(mesher, arguments_.rebate, 0,
154 }
155
156 // 5. Solver
157 FdmSolverDesc solverDesc = { mesher, boundaries, conditions,
158 calculator, maturity,
160
161 ext::shared_ptr<FdmHestonSolver> solver(ext::make_shared<FdmHestonSolver>(
162 Handle<HestonProcess>(process), solverDesc, schemeDesc_,
164
165 const Real spot = process->s0()->value();
166 results_.value = solver->valueAt(spot, process->v0());
167 results_.delta = solver->deltaAt(spot, process->v0());
168 results_.gamma = solver->gammaAt(spot, process->v0());
169 results_.theta = solver->thetaAt(spot, process->v0());
170
171 // 6. Calculate vanilla option and rebate for in-barriers
172 if ( arguments_.barrierType == Barrier::DownIn
173 || arguments_.barrierType == Barrier::UpIn) {
174 // Cast the payoff
175 ext::shared_ptr<StrikedTypePayoff> payoff =
176 ext::dynamic_pointer_cast<StrikedTypePayoff>(
177 arguments_.payoff);
178 // Calculate the vanilla option
179 VanillaOption vanillaOption(payoff, arguments_.exercise);
180 vanillaOption.setPricingEngine(ext::shared_ptr<PricingEngine>(
181 ext::make_shared<FdHestonVanillaEngine>(*model_, dividends_,
182 tGrid_, xGrid_,
184 schemeDesc_)));
185 // Calculate the rebate value
186 BarrierOption rebateOption(arguments_.barrierType,
187 arguments_.barrier,
188 arguments_.rebate,
189 payoff, arguments_.exercise);
190 const Size xGridMin = 20;
191 const Size vGridMin = 10;
192 const Size rebateDampingSteps
193 = (dampingSteps_ > 0) ? std::min(Size(1), dampingSteps_/2) : 0;
194 rebateOption.setPricingEngine(
195 ext::make_shared<FdHestonRebateEngine>(*model_, dividends_,
196 tGrid_,
197 std::max(xGridMin, xGrid_/4),
198 std::max(vGridMin, vGrid_/4),
199 rebateDampingSteps,
200 schemeDesc_));
201
202 results_.value = vanillaOption.NPV() + rebateOption.NPV() - results_.value;
203 results_.delta = vanillaOption.delta() + rebateOption.delta() - results_.delta;
204 results_.gamma = vanillaOption.gamma() + rebateOption.gamma() - results_.gamma;
205 results_.theta = vanillaOption.theta() + rebateOption.theta() - results_.theta;
206 }
207 }
208}
Barrier option on a single asset.
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.
const DefaultType & t
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
Finite-differences Heston barrier-option engine.
Finite-differences Heston barrier-option rebate helper engine.
Finite-differences Heston vanilla option engine.
1-d mesher for the Black-Scholes process (in ln(S))
Dirichlet boundary conditions for differential operators.
dividend handler for fdm method for one equity direction
One-dimensional grid mesher for the variance part of the Heston model.
layer of abstraction to calculate the inner value
memory layout of a fdm linear operator
FdmMesher which is a composite of Fdm1dMesher.
composite of fdm step conditions
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
ext::shared_ptr< QuantLib::Payoff > payoff
Definition: any.hpp:35
std::vector< ext::shared_ptr< Dividend > > DividendSchedule
OperatorTraits< FdmLinearOp >::bc_set FdmBoundaryConditionSet
STL namespace.
Vanilla option on a single asset.