| 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, 2015 Klaus Spanderen |
| 7 | Copyright (C) 2015 Johannes Göttker-Schnetmann |
| 8 | |
| 9 | This file is part of QuantLib, a free-software/open-source library |
| 10 | for financial quantitative analysts and developers - http://quantlib.org/ |
| 11 | |
| 12 | QuantLib is free software: you can redistribute it and/or modify it |
| 13 | under the terms of the QuantLib license. You should have received a |
| 14 | copy of the license along with this program; if not, please email |
| 15 | <quantlib-dev@lists.sf.net>. The license is also available online at |
| 16 | <http://quantlib.org/license.shtml>. |
| 17 | |
| 18 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 19 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 20 | FOR A PARTICULAR PURPOSE. See the license for more details. |
| 21 | */ |
| 22 | |
| 23 | #include <ql/methods/finitedifferences/meshers/fdmblackscholesmesher.hpp> |
| 24 | #include <ql/methods/finitedifferences/meshers/fdmblackscholesmultistrikemesher.hpp> |
| 25 | #include <ql/methods/finitedifferences/meshers/fdmhestonvariancemesher.hpp> |
| 26 | #include <ql/methods/finitedifferences/meshers/fdmmeshercomposite.hpp> |
| 27 | #include <ql/methods/finitedifferences/operators/fdmlinearoplayout.hpp> |
| 28 | #include <ql/methods/finitedifferences/solvers/fdmhestonsolver.hpp> |
| 29 | #include <ql/methods/finitedifferences/stepconditions/fdmstepconditioncomposite.hpp> |
| 30 | #include <ql/methods/finitedifferences/utilities/fdminnervaluecalculator.hpp> |
| 31 | #include <ql/pricingengines/vanilla/fdhestonvanillaengine.hpp> |
| 32 | #include <ql/processes/batesprocess.hpp> |
| 33 | #include <utility> |
| 34 | |
| 35 | namespace QuantLib { |
| 36 | |
| 37 | QL_DEPRECATED_DISABLE_WARNING |
| 38 | |
| 39 | FdHestonVanillaEngine::FdHestonVanillaEngine(const ext::shared_ptr<HestonModel>& model, |
| 40 | Size tGrid, |
| 41 | Size xGrid, |
| 42 | Size vGrid, |
| 43 | Size dampingSteps, |
| 44 | const FdmSchemeDesc& schemeDesc, |
| 45 | ext::shared_ptr<LocalVolTermStructure> leverageFct, |
| 46 | const Real mixingFactor) |
| 47 | : GenericModelEngine<HestonModel, |
| 48 | DividendVanillaOption::arguments, |
| 49 | DividendVanillaOption::results>(model), |
| 50 | explicitDividends_(false), |
| 51 | tGrid_(tGrid), xGrid_(xGrid), vGrid_(vGrid), dampingSteps_(dampingSteps), |
| 52 | schemeDesc_(schemeDesc), leverageFct_(std::move(leverageFct)), |
| 53 | quantoHelper_(ext::shared_ptr<FdmQuantoHelper>()), mixingFactor_(mixingFactor) {} |
| 54 | |
| 55 | FdHestonVanillaEngine::FdHestonVanillaEngine(const ext::shared_ptr<HestonModel>& model, |
| 56 | DividendSchedule dividends, |
| 57 | Size tGrid, |
| 58 | Size xGrid, |
| 59 | Size vGrid, |
| 60 | Size dampingSteps, |
| 61 | const FdmSchemeDesc& schemeDesc, |
| 62 | ext::shared_ptr<LocalVolTermStructure> leverageFct, |
| 63 | const Real mixingFactor) |
| 64 | : GenericModelEngine<HestonModel, |
| 65 | DividendVanillaOption::arguments, |
| 66 | DividendVanillaOption::results>(model), |
| 67 | dividends_(std::move(dividends)), explicitDividends_(true), |
| 68 | tGrid_(tGrid), xGrid_(xGrid), vGrid_(vGrid), dampingSteps_(dampingSteps), |
| 69 | schemeDesc_(schemeDesc), leverageFct_(std::move(leverageFct)), |
| 70 | quantoHelper_(ext::shared_ptr<FdmQuantoHelper>()), mixingFactor_(mixingFactor) {} |
| 71 | |
| 72 | FdHestonVanillaEngine::FdHestonVanillaEngine(const ext::shared_ptr<HestonModel>& model, |
| 73 | ext::shared_ptr<FdmQuantoHelper> quantoHelper, |
| 74 | Size tGrid, |
| 75 | Size xGrid, |
| 76 | Size vGrid, |
| 77 | Size dampingSteps, |
| 78 | const FdmSchemeDesc& schemeDesc, |
| 79 | ext::shared_ptr<LocalVolTermStructure> leverageFct, |
| 80 | const Real mixingFactor) |
| 81 | : GenericModelEngine<HestonModel, |
| 82 | DividendVanillaOption::arguments, |
| 83 | DividendVanillaOption::results>(model), |
| 84 | explicitDividends_(false), |
| 85 | tGrid_(tGrid), xGrid_(xGrid), vGrid_(vGrid), dampingSteps_(dampingSteps), |
| 86 | schemeDesc_(schemeDesc), leverageFct_(std::move(leverageFct)), |
| 87 | quantoHelper_(std::move(quantoHelper)), mixingFactor_(mixingFactor) {} |
| 88 | |
| 89 | FdHestonVanillaEngine::FdHestonVanillaEngine(const ext::shared_ptr<HestonModel>& model, |
| 90 | DividendSchedule dividends, |
| 91 | ext::shared_ptr<FdmQuantoHelper> quantoHelper, |
| 92 | Size tGrid, |
| 93 | Size xGrid, |
| 94 | Size vGrid, |
| 95 | Size dampingSteps, |
| 96 | const FdmSchemeDesc& schemeDesc, |
| 97 | ext::shared_ptr<LocalVolTermStructure> leverageFct, |
| 98 | const Real mixingFactor) |
| 99 | : GenericModelEngine<HestonModel, |
| 100 | DividendVanillaOption::arguments, |
| 101 | DividendVanillaOption::results>(model), |
| 102 | dividends_(std::move(dividends)), explicitDividends_(true), |
| 103 | tGrid_(tGrid), xGrid_(xGrid), vGrid_(vGrid), dampingSteps_(dampingSteps), |
| 104 | schemeDesc_(schemeDesc), leverageFct_(std::move(leverageFct)), |
| 105 | quantoHelper_(std::move(quantoHelper)), mixingFactor_(mixingFactor) {} |
| 106 | |
| 107 | QL_DEPRECATED_ENABLE_WARNING |
| 108 | |
| 109 | FdmSolverDesc FdHestonVanillaEngine::getSolverDesc(Real) const { |
| 110 | |
| 111 | // dividends will eventually be moved out of arguments, but for now we need the switch |
| 112 | QL_DEPRECATED_DISABLE_WARNING |
| 113 | const DividendSchedule& passedDividends = explicitDividends_ ? dividends_ : arguments_.cashFlow; |
| 114 | QL_DEPRECATED_ENABLE_WARNING |
| 115 | |
| 116 | // 1. Mesher |
| 117 | const ext::shared_ptr<HestonProcess> process = model_->process(); |
| 118 | const Time maturity = process->time(arguments_.exercise->lastDate()); |
| 119 | |
| 120 | // 1.1 The variance mesher |
| 121 | const Size tGridMin = 5; |
| 122 | const Size tGridAvgSteps = std::max(a: tGridMin, b: tGrid_/50); |
| 123 | const ext::shared_ptr<FdmHestonLocalVolatilityVarianceMesher> vMesher |
| 124 | = ext::make_shared<FdmHestonLocalVolatilityVarianceMesher>( |
| 125 | args: vGrid_, args: process, args: leverageFct_, args: maturity, args: tGridAvgSteps, args: 0.0001, args: mixingFactor_); |
| 126 | |
| 127 | const Volatility avgVolaEstimate = vMesher->volaEstimate(); |
| 128 | |
| 129 | // 1.2 The equity mesher |
| 130 | const ext::shared_ptr<StrikedTypePayoff> payoff = |
| 131 | ext::dynamic_pointer_cast<StrikedTypePayoff>(r: arguments_.payoff); |
| 132 | |
| 133 | ext::shared_ptr<Fdm1dMesher> equityMesher; |
| 134 | if (strikes_.empty()) { |
| 135 | equityMesher = ext::shared_ptr<Fdm1dMesher>( |
| 136 | new FdmBlackScholesMesher( |
| 137 | xGrid_, |
| 138 | FdmBlackScholesMesher::processHelper( |
| 139 | s0: process->s0(), rTS: process->dividendYield(), |
| 140 | qTS: process->riskFreeRate(), vol: avgVolaEstimate), |
| 141 | maturity, payoff->strike(), |
| 142 | Null<Real>(), Null<Real>(), 0.0001, 2.0, |
| 143 | std::pair<Real, Real>(payoff->strike(), 0.1), |
| 144 | passedDividends, |
| 145 | quantoHelper_)); |
| 146 | } |
| 147 | else { |
| 148 | QL_REQUIRE(passedDividends.empty(), |
| 149 | "multiple strikes engine does not work with discrete dividends" ); |
| 150 | equityMesher = ext::shared_ptr<Fdm1dMesher>( |
| 151 | new FdmBlackScholesMultiStrikeMesher( |
| 152 | xGrid_, |
| 153 | FdmBlackScholesMesher::processHelper( |
| 154 | s0: process->s0(), rTS: process->dividendYield(), |
| 155 | qTS: process->riskFreeRate(), vol: avgVolaEstimate), |
| 156 | maturity, strikes_, 0.0001, 1.5, |
| 157 | std::pair<Real, Real>(payoff->strike(), 0.075))); |
| 158 | } |
| 159 | |
| 160 | const ext::shared_ptr<FdmMesher> mesher( |
| 161 | new FdmMesherComposite(equityMesher, vMesher)); |
| 162 | |
| 163 | // 2. Calculator |
| 164 | const ext::shared_ptr<FdmInnerValueCalculator> calculator( |
| 165 | new FdmLogInnerValue(arguments_.payoff, mesher, 0)); |
| 166 | |
| 167 | // 3. Step conditions |
| 168 | const ext::shared_ptr<FdmStepConditionComposite> conditions = |
| 169 | FdmStepConditionComposite::vanillaComposite( |
| 170 | schedule: passedDividends, exercise: arguments_.exercise, |
| 171 | mesher, calculator, |
| 172 | refDate: process->riskFreeRate()->referenceDate(), |
| 173 | dayCounter: process->riskFreeRate()->dayCounter()); |
| 174 | |
| 175 | // 4. Boundary conditions |
| 176 | const FdmBoundaryConditionSet boundaries; |
| 177 | |
| 178 | // 5. Solver |
| 179 | FdmSolverDesc solverDesc = { .mesher: mesher, .bcSet: boundaries, .condition: conditions, |
| 180 | .calculator: calculator, .maturity: maturity, |
| 181 | .timeSteps: tGrid_, .dampingSteps: dampingSteps_ }; |
| 182 | |
| 183 | return solverDesc; |
| 184 | } |
| 185 | |
| 186 | void FdHestonVanillaEngine::calculate() const { |
| 187 | |
| 188 | // dividends will eventually be moved out of arguments, but for now we need the switch |
| 189 | QL_DEPRECATED_DISABLE_WARNING |
| 190 | const DividendSchedule& passedDividends = explicitDividends_ ? dividends_ : arguments_.cashFlow; |
| 191 | QL_DEPRECATED_ENABLE_WARNING |
| 192 | |
| 193 | // cache lookup for precalculated results |
| 194 | for (auto& cachedArgs2result : cachedArgs2results_) { |
| 195 | if (cachedArgs2result.first.exercise->type() == arguments_.exercise->type() && |
| 196 | cachedArgs2result.first.exercise->dates() == arguments_.exercise->dates()) { |
| 197 | ext::shared_ptr<PlainVanillaPayoff> p1 = |
| 198 | ext::dynamic_pointer_cast<PlainVanillaPayoff>( |
| 199 | r: arguments_.payoff); |
| 200 | ext::shared_ptr<PlainVanillaPayoff> p2 = |
| 201 | ext::dynamic_pointer_cast<PlainVanillaPayoff>(r: cachedArgs2result.first.payoff); |
| 202 | |
| 203 | if ((p1 != nullptr) && p1->strike() == p2->strike() && |
| 204 | p1->optionType() == p2->optionType()) { |
| 205 | QL_REQUIRE(passedDividends.empty(), |
| 206 | "multiple strikes engine does not work with discrete dividends" ); |
| 207 | results_ = cachedArgs2result.second; |
| 208 | return; |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | const ext::shared_ptr<HestonProcess> process = model_->process(); |
| 214 | |
| 215 | ext::shared_ptr<FdmHestonSolver> solver(new FdmHestonSolver( |
| 216 | Handle<HestonProcess>(process), |
| 217 | getSolverDesc(1.5), schemeDesc_, |
| 218 | Handle<FdmQuantoHelper>(quantoHelper_), leverageFct_, |
| 219 | mixingFactor_)); |
| 220 | |
| 221 | const Real v0 = process->v0(); |
| 222 | const Real spot = process->s0()->value(); |
| 223 | |
| 224 | results_.value = solver->valueAt(s: spot, v: v0); |
| 225 | results_.delta = solver->deltaAt(s: spot, v: v0); |
| 226 | results_.gamma = solver->gammaAt(s: spot, v: v0); |
| 227 | results_.theta = solver->thetaAt(s: spot, v: v0); |
| 228 | |
| 229 | cachedArgs2results_.resize(new_size: strikes_.size()); |
| 230 | const ext::shared_ptr<StrikedTypePayoff> payoff = |
| 231 | ext::dynamic_pointer_cast<StrikedTypePayoff>(r: arguments_.payoff); |
| 232 | for (Size i=0; i < strikes_.size(); ++i) { |
| 233 | cachedArgs2results_[i].first.exercise = arguments_.exercise; |
| 234 | cachedArgs2results_[i].first.payoff = |
| 235 | ext::make_shared<PlainVanillaPayoff>( |
| 236 | args: payoff->optionType(), args: strikes_[i]); |
| 237 | const Real d = payoff->strike()/strikes_[i]; |
| 238 | |
| 239 | QL_DEPRECATED_DISABLE_WARNING |
| 240 | DividendVanillaOption::results& results = cachedArgs2results_[i].second; |
| 241 | QL_DEPRECATED_ENABLE_WARNING |
| 242 | results.value = solver->valueAt(s: spot*d, v: v0)/d; |
| 243 | results.delta = solver->deltaAt(s: spot*d, v: v0); |
| 244 | results.gamma = solver->gammaAt(s: spot*d, v: v0)*d; |
| 245 | results.theta = solver->thetaAt(s: spot*d, v: v0)/d; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | void FdHestonVanillaEngine::update() { |
| 250 | cachedArgs2results_.clear(); |
| 251 | QL_DEPRECATED_DISABLE_WARNING |
| 252 | GenericModelEngine<HestonModel, |
| 253 | DividendVanillaOption::arguments, |
| 254 | DividendVanillaOption::results>::update(); |
| 255 | QL_DEPRECATED_ENABLE_WARNING |
| 256 | } |
| 257 | |
| 258 | void FdHestonVanillaEngine::enableMultipleStrikesCaching( |
| 259 | const std::vector<Real>& strikes) { |
| 260 | strikes_ = strikes; |
| 261 | cachedArgs2results_.clear(); |
| 262 | } |
| 263 | |
| 264 | |
| 265 | MakeFdHestonVanillaEngine::MakeFdHestonVanillaEngine(ext::shared_ptr<HestonModel> hestonModel) |
| 266 | : hestonModel_(std::move(hestonModel)), |
| 267 | schemeDesc_(ext::make_shared<FdmSchemeDesc>(args: FdmSchemeDesc::Hundsdorfer())) {} |
| 268 | |
| 269 | MakeFdHestonVanillaEngine& MakeFdHestonVanillaEngine::withQuantoHelper( |
| 270 | const ext::shared_ptr<FdmQuantoHelper>& quantoHelper) { |
| 271 | quantoHelper_ = quantoHelper; |
| 272 | return *this; |
| 273 | } |
| 274 | |
| 275 | MakeFdHestonVanillaEngine& |
| 276 | MakeFdHestonVanillaEngine::withTGrid(Size tGrid) { |
| 277 | tGrid_ = tGrid; |
| 278 | return *this; |
| 279 | } |
| 280 | |
| 281 | MakeFdHestonVanillaEngine& |
| 282 | MakeFdHestonVanillaEngine::withXGrid(Size xGrid) { |
| 283 | xGrid_ = xGrid; |
| 284 | return *this; |
| 285 | } |
| 286 | |
| 287 | MakeFdHestonVanillaEngine& |
| 288 | MakeFdHestonVanillaEngine::withVGrid(Size vGrid) { |
| 289 | vGrid_ = vGrid; |
| 290 | return *this; |
| 291 | } |
| 292 | |
| 293 | MakeFdHestonVanillaEngine& |
| 294 | MakeFdHestonVanillaEngine::withDampingSteps(Size dampingSteps) { |
| 295 | dampingSteps_ = dampingSteps; |
| 296 | return *this; |
| 297 | } |
| 298 | |
| 299 | MakeFdHestonVanillaEngine& |
| 300 | MakeFdHestonVanillaEngine::withFdmSchemeDesc( |
| 301 | const FdmSchemeDesc& schemeDesc) { |
| 302 | schemeDesc_ = ext::make_shared<FdmSchemeDesc>(args: schemeDesc); |
| 303 | return *this; |
| 304 | } |
| 305 | |
| 306 | MakeFdHestonVanillaEngine& |
| 307 | MakeFdHestonVanillaEngine::withLeverageFunction( |
| 308 | ext::shared_ptr<LocalVolTermStructure>& leverageFct) { |
| 309 | leverageFct_ = leverageFct; |
| 310 | return *this; |
| 311 | } |
| 312 | |
| 313 | MakeFdHestonVanillaEngine& |
| 314 | MakeFdHestonVanillaEngine::withCashDividends( |
| 315 | const std::vector<Date>& dividendDates, |
| 316 | const std::vector<Real>& dividendAmounts) { |
| 317 | dividends_ = DividendVector(dividendDates, dividends: dividendAmounts); |
| 318 | explicitDividends_ = true; |
| 319 | return *this; |
| 320 | } |
| 321 | |
| 322 | MakeFdHestonVanillaEngine::operator |
| 323 | ext::shared_ptr<PricingEngine>() const { |
| 324 | if (explicitDividends_) { |
| 325 | return ext::make_shared<FdHestonVanillaEngine>( |
| 326 | args: hestonModel_, |
| 327 | args: dividends_, |
| 328 | args: quantoHelper_, |
| 329 | args: tGrid_, args: xGrid_, args: vGrid_, args: dampingSteps_, |
| 330 | args&: *schemeDesc_, |
| 331 | args: leverageFct_); |
| 332 | } else { |
| 333 | return ext::make_shared<FdHestonVanillaEngine>( |
| 334 | args: hestonModel_, |
| 335 | args: quantoHelper_, |
| 336 | args: tGrid_, args: xGrid_, args: vGrid_, args: dampingSteps_, |
| 337 | args&: *schemeDesc_, |
| 338 | args: leverageFct_); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | } |
| 343 | |