/src/quantlib/ql/pricingengines/barrier/fdhestonrebateengine.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/solvers/fdmbackwardsolver.hpp> |
27 | | #include <ql/methods/finitedifferences/stepconditions/fdmstepconditioncomposite.hpp> |
28 | | #include <ql/methods/finitedifferences/utilities/fdmdirichletboundary.hpp> |
29 | | #include <ql/methods/finitedifferences/utilities/fdminnervaluecalculator.hpp> |
30 | | #include <ql/pricingengines/barrier/fdhestonrebateengine.hpp> |
31 | | #include <utility> |
32 | | |
33 | | namespace QuantLib { |
34 | | |
35 | | QL_DEPRECATED_DISABLE_WARNING |
36 | | |
37 | | FdHestonRebateEngine::FdHestonRebateEngine(const ext::shared_ptr<HestonModel>& model, |
38 | | Size tGrid, |
39 | | Size xGrid, |
40 | | Size vGrid, |
41 | | Size dampingSteps, |
42 | | const FdmSchemeDesc& schemeDesc, |
43 | | ext::shared_ptr<LocalVolTermStructure> leverageFct, |
44 | | const Real mixingFactor) |
45 | 0 | : GenericModelEngine<HestonModel, |
46 | 0 | BarrierOption::arguments, |
47 | 0 | BarrierOption::results>(model), |
48 | 0 | tGrid_(tGrid), xGrid_(xGrid), vGrid_(vGrid), dampingSteps_(dampingSteps), |
49 | 0 | schemeDesc_(schemeDesc), leverageFct_(std::move(leverageFct)), mixingFactor_(mixingFactor) {} |
50 | | |
51 | | FdHestonRebateEngine::FdHestonRebateEngine(const ext::shared_ptr<HestonModel>& model, |
52 | | DividendSchedule dividends, |
53 | | Size tGrid, |
54 | | Size xGrid, |
55 | | Size vGrid, |
56 | | Size dampingSteps, |
57 | | const FdmSchemeDesc& schemeDesc, |
58 | | ext::shared_ptr<LocalVolTermStructure> leverageFct, |
59 | | const Real mixingFactor) |
60 | 0 | : GenericModelEngine<HestonModel, |
61 | 0 | BarrierOption::arguments, |
62 | 0 | BarrierOption::results>(model), |
63 | 0 | dividends_(std::move(dividends)), |
64 | 0 | tGrid_(tGrid), xGrid_(xGrid), vGrid_(vGrid), dampingSteps_(dampingSteps), |
65 | 0 | schemeDesc_(schemeDesc), leverageFct_(std::move(leverageFct)), mixingFactor_(mixingFactor) {} |
66 | | |
67 | | QL_DEPRECATED_ENABLE_WARNING |
68 | | |
69 | 0 | void FdHestonRebateEngine::calculate() const { |
70 | | |
71 | | // 1. Mesher |
72 | 0 | const ext::shared_ptr<HestonProcess>& process = model_->process(); |
73 | 0 | const Time maturity = process->time(arguments_.exercise->lastDate()); |
74 | | |
75 | | // 1.1 The variance mesher |
76 | 0 | const Size tGridMin = 5; |
77 | 0 | const Size tGridAvgSteps = std::max(tGridMin, tGrid_/50); |
78 | |
|
79 | 0 | const auto vMesher = 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 auto equityMesher = ext::make_shared<FdmBlackScholesMesher>( |
98 | 0 | xGrid_, |
99 | 0 | FdmBlackScholesMesher::processHelper( |
100 | 0 | process->s0(), process->dividendYield(), |
101 | 0 | process->riskFreeRate(), vMesher->volaEstimate()), |
102 | 0 | maturity, payoff->strike(), |
103 | 0 | xMin, xMax, 0.0001, 1.5, |
104 | 0 | std::make_pair(Null<Real>(), Null<Real>()), |
105 | 0 | dividends_); |
106 | |
|
107 | 0 | const auto mesher = ext::make_shared<FdmMesherComposite>(equityMesher, vMesher); |
108 | | |
109 | | // 2. Calculator |
110 | 0 | const auto rebatePayoff = ext::make_shared<CashOrNothingPayoff>(Option::Call, 0.0, arguments_.rebate); |
111 | 0 | const auto calculator = ext::make_shared<FdmLogInnerValue>(rebatePayoff, mesher, 0); |
112 | | |
113 | | // 3. Step conditions |
114 | 0 | QL_REQUIRE(arguments_.exercise->type() == Exercise::European, |
115 | 0 | "only european style option are supported"); |
116 | | |
117 | 0 | const ext::shared_ptr<FdmStepConditionComposite> conditions = |
118 | 0 | FdmStepConditionComposite::vanillaComposite( |
119 | 0 | dividends_, arguments_.exercise, |
120 | 0 | mesher, calculator, |
121 | 0 | process->riskFreeRate()->referenceDate(), |
122 | 0 | process->riskFreeRate()->dayCounter()); |
123 | | |
124 | | // 4. Boundary conditions |
125 | 0 | FdmBoundaryConditionSet boundaries; |
126 | 0 | if ( arguments_.barrierType == Barrier::DownIn |
127 | 0 | || arguments_.barrierType == Barrier::DownOut) { |
128 | 0 | boundaries.push_back(ext::make_shared<FdmDirichletBoundary>( |
129 | 0 | mesher, arguments_.rebate, 0, FdmDirichletBoundary::Lower)); |
130 | |
|
131 | 0 | } |
132 | 0 | if ( arguments_.barrierType == Barrier::UpIn |
133 | 0 | || arguments_.barrierType == Barrier::UpOut) { |
134 | 0 | boundaries.push_back(ext::make_shared<FdmDirichletBoundary>( |
135 | 0 | mesher, arguments_.rebate, 0, FdmDirichletBoundary::Upper)); |
136 | 0 | } |
137 | | |
138 | | // 5. Solver |
139 | 0 | FdmSolverDesc solverDesc = { mesher, boundaries, conditions, |
140 | 0 | calculator, maturity, |
141 | 0 | tGrid_, dampingSteps_ }; |
142 | |
|
143 | 0 | auto solver = ext::make_shared<FdmHestonSolver>( |
144 | 0 | Handle<HestonProcess>(process), solverDesc, schemeDesc_, |
145 | 0 | Handle<FdmQuantoHelper>(), leverageFct_, mixingFactor_); |
146 | |
|
147 | 0 | const Real spot = process->s0()->value(); |
148 | 0 | results_.value = solver->valueAt(spot, process->v0()); |
149 | 0 | results_.delta = solver->deltaAt(spot, process->v0()); |
150 | 0 | results_.gamma = solver->gammaAt(spot, process->v0()); |
151 | 0 | results_.theta = solver->thetaAt(spot, process->v0()); |
152 | 0 | } |
153 | | } |