/src/quantlib/ql/pricingengines/barrier/fdhestonbarrierengine.cpp
Line | Count | Source |
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 | | <https://www.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 | | |
36 | | namespace 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) |
46 | 0 | : GenericModelEngine<HestonModel, |
47 | 0 | BarrierOption::arguments, |
48 | 0 | BarrierOption::results>(model), |
49 | 0 | tGrid_(tGrid), xGrid_(xGrid), vGrid_(vGrid), dampingSteps_(dampingSteps), |
50 | 0 | 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) |
61 | 0 | : GenericModelEngine<HestonModel, |
62 | 0 | BarrierOption::arguments, |
63 | 0 | BarrierOption::results>(model), |
64 | 0 | dividends_(std::move(dividends)), |
65 | 0 | tGrid_(tGrid), xGrid_(xGrid), vGrid_(vGrid), dampingSteps_(dampingSteps), |
66 | 0 | schemeDesc_(schemeDesc), leverageFct_(std::move(leverageFct)), mixingFactor_(mixingFactor) {} |
67 | | |
68 | 0 | void FdHestonBarrierEngine::calculate() const { |
69 | | |
70 | | // 1. Mesher |
71 | 0 | const ext::shared_ptr<HestonProcess>& process = model_->process(); |
72 | 0 | const Time maturity = process->time(arguments_.exercise->lastDate()); |
73 | | |
74 | | // 1.1 The variance mesher |
75 | 0 | const Size tGridMin = 5; |
76 | 0 | const Size tGridAvgSteps = std::max(tGridMin, tGrid_/50); |
77 | |
|
78 | 0 | const auto vMesher = ext::make_shared<FdmHestonLocalVolatilityVarianceMesher>( |
79 | 0 | vGrid_, process, leverageFct_, maturity, tGridAvgSteps, 0.0001, mixingFactor_); |
80 | | |
81 | | // 1.2 The equity mesher |
82 | 0 | const ext::shared_ptr<StrikedTypePayoff> payoff = |
83 | 0 | ext::dynamic_pointer_cast<StrikedTypePayoff>(arguments_.payoff); |
84 | |
|
85 | 0 | Real xMin=Null<Real>(); |
86 | 0 | Real xMax=Null<Real>(); |
87 | 0 | if ( arguments_.barrierType == Barrier::DownIn |
88 | 0 | || arguments_.barrierType == Barrier::DownOut) { |
89 | 0 | xMin = std::log(arguments_.barrier); |
90 | 0 | } |
91 | 0 | if ( arguments_.barrierType == Barrier::UpIn |
92 | 0 | || arguments_.barrierType == Barrier::UpOut) { |
93 | 0 | xMax = std::log(arguments_.barrier); |
94 | 0 | } |
95 | |
|
96 | 0 | const auto equityMesher = ext::make_shared<FdmBlackScholesMesher>( |
97 | 0 | xGrid_, |
98 | 0 | FdmBlackScholesMesher::processHelper( |
99 | 0 | process->s0(), process->dividendYield(), |
100 | 0 | process->riskFreeRate(), vMesher->volaEstimate()), |
101 | 0 | maturity, payoff->strike(), |
102 | 0 | xMin, xMax, 0.0001, 1.5, |
103 | 0 | std::make_pair(Null<Real>(), Null<Real>()), |
104 | 0 | dividends_); |
105 | |
|
106 | 0 | const auto mesher = ext::make_shared<FdmMesherComposite>(equityMesher, vMesher); |
107 | | |
108 | | // 2. Calculator |
109 | 0 | auto calculator = ext::make_shared<FdmLogInnerValue>(payoff, mesher, 0); |
110 | | |
111 | | // 3. Step conditions |
112 | 0 | std::list<ext::shared_ptr<StepCondition<Array> > > stepConditions; |
113 | 0 | std::list<std::vector<Time> > stoppingTimes; |
114 | | |
115 | | // 3.1 Step condition if discrete dividends |
116 | 0 | auto dividendCondition = ext::make_shared<FdmDividendHandler>(dividends_, mesher, |
117 | 0 | process->riskFreeRate()->referenceDate(), |
118 | 0 | process->riskFreeRate()->dayCounter(), 0); |
119 | |
|
120 | 0 | if (!dividends_.empty()) { |
121 | 0 | stepConditions.push_back(dividendCondition); |
122 | 0 | std::vector<Time> dividendTimes = dividendCondition->dividendTimes(); |
123 | | // this effectively excludes times after maturity |
124 | 0 | for (auto& t: dividendTimes) |
125 | 0 | t = std::min(maturity, t); |
126 | 0 | stoppingTimes.push_back(dividendTimes); |
127 | 0 | } |
128 | |
|
129 | 0 | QL_REQUIRE(arguments_.exercise->type() == Exercise::European, |
130 | 0 | "only european style option are supported"); |
131 | | |
132 | 0 | auto conditions = ext::make_shared<FdmStepConditionComposite>(stoppingTimes, stepConditions); |
133 | | |
134 | | // 4. Boundary conditions |
135 | 0 | FdmBoundaryConditionSet boundaries; |
136 | 0 | if ( arguments_.barrierType == Barrier::DownIn |
137 | 0 | || arguments_.barrierType == Barrier::DownOut) { |
138 | 0 | boundaries.push_back( |
139 | 0 | ext::make_shared<FdmDirichletBoundary>(mesher, arguments_.rebate, 0, |
140 | 0 | FdmDirichletBoundary::Lower)); |
141 | |
|
142 | 0 | } |
143 | 0 | if ( arguments_.barrierType == Barrier::UpIn |
144 | 0 | || arguments_.barrierType == Barrier::UpOut) { |
145 | 0 | boundaries.push_back( |
146 | 0 | ext::make_shared<FdmDirichletBoundary>(mesher, arguments_.rebate, 0, |
147 | 0 | FdmDirichletBoundary::Upper)); |
148 | 0 | } |
149 | | |
150 | | // 5. Solver |
151 | 0 | FdmSolverDesc solverDesc = { mesher, boundaries, conditions, |
152 | 0 | calculator, maturity, |
153 | 0 | tGrid_, dampingSteps_ }; |
154 | |
|
155 | 0 | auto solver = ext::make_shared<FdmHestonSolver>( |
156 | 0 | Handle<HestonProcess>(process), solverDesc, schemeDesc_, |
157 | 0 | Handle<FdmQuantoHelper>(), leverageFct_, mixingFactor_); |
158 | |
|
159 | 0 | const Real spot = process->s0()->value(); |
160 | 0 | results_.value = solver->valueAt(spot, process->v0()); |
161 | 0 | results_.delta = solver->deltaAt(spot, process->v0()); |
162 | 0 | results_.gamma = solver->gammaAt(spot, process->v0()); |
163 | 0 | results_.theta = solver->thetaAt(spot, process->v0()); |
164 | | |
165 | | // 6. Calculate vanilla option and rebate for in-barriers |
166 | 0 | if ( arguments_.barrierType == Barrier::DownIn |
167 | 0 | || arguments_.barrierType == Barrier::UpIn) { |
168 | | // Cast the payoff |
169 | 0 | ext::shared_ptr<StrikedTypePayoff> payoff = |
170 | 0 | ext::dynamic_pointer_cast<StrikedTypePayoff>( |
171 | 0 | arguments_.payoff); |
172 | | // Calculate the vanilla option |
173 | 0 | VanillaOption vanillaOption(payoff, arguments_.exercise); |
174 | 0 | vanillaOption.setPricingEngine(ext::shared_ptr<PricingEngine>( |
175 | 0 | ext::make_shared<FdHestonVanillaEngine>(*model_, dividends_, |
176 | 0 | tGrid_, xGrid_, |
177 | 0 | vGrid_, dampingSteps_, |
178 | 0 | schemeDesc_))); |
179 | | // Calculate the rebate value |
180 | 0 | BarrierOption rebateOption(arguments_.barrierType, |
181 | 0 | arguments_.barrier, |
182 | 0 | arguments_.rebate, |
183 | 0 | payoff, arguments_.exercise); |
184 | 0 | const Size xGridMin = 20; |
185 | 0 | const Size vGridMin = 10; |
186 | 0 | const Size rebateDampingSteps |
187 | 0 | = (dampingSteps_ > 0) ? std::min(Size(1), dampingSteps_/2) : 0; |
188 | 0 | rebateOption.setPricingEngine( |
189 | 0 | ext::make_shared<FdHestonRebateEngine>(*model_, dividends_, |
190 | 0 | tGrid_, |
191 | 0 | std::max(xGridMin, xGrid_/4), |
192 | 0 | std::max(vGridMin, vGrid_/4), |
193 | 0 | rebateDampingSteps, |
194 | 0 | schemeDesc_)); |
195 | |
|
196 | 0 | results_.value = vanillaOption.NPV() + rebateOption.NPV() - results_.value; |
197 | 0 | results_.delta = vanillaOption.delta() + rebateOption.delta() - results_.delta; |
198 | 0 | results_.gamma = vanillaOption.gamma() + rebateOption.gamma() - results_.gamma; |
199 | 0 | results_.theta = vanillaOption.theta() + rebateOption.theta() - results_.theta; |
200 | 0 | } |
201 | 0 | } |
202 | | } |