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