| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | |
| 3 | /* |
| 4 | Copyright (C) 2018 Klaus Spanderen |
| 5 | |
| 6 | This file is part of QuantLib, a free-software/open-source library |
| 7 | for financial quantitative analysts and developers - http://quantlib.org/ |
| 8 | |
| 9 | QuantLib is free software: you can redistribute it and/or modify it |
| 10 | under the terms of the QuantLib license. You should have received a |
| 11 | copy of the license along with this program; if not, please email |
| 12 | <quantlib-dev@lists.sf.net>. The license is also available online at |
| 13 | <http://quantlib.org/license.shtml>. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 17 | FOR A PARTICULAR PURPOSE. See the license for more details. |
| 18 | */ |
| 19 | |
| 20 | /*! \file fdcevvanillaengine.hpp |
| 21 | \brief Finite-Differences pricing engine for the CEV model |
| 22 | */ |
| 23 | |
| 24 | #include <ql/exercise.hpp> |
| 25 | #include <ql/methods/finitedifferences/meshers/concentrating1dmesher.hpp> |
| 26 | #include <ql/methods/finitedifferences/meshers/fdmcev1dmesher.hpp> |
| 27 | #include <ql/methods/finitedifferences/meshers/fdmmeshercomposite.hpp> |
| 28 | #include <ql/methods/finitedifferences/operators/fdmcevop.hpp> |
| 29 | #include <ql/methods/finitedifferences/operators/fdmlinearoplayout.hpp> |
| 30 | #include <ql/methods/finitedifferences/solvers/fdm1dimsolver.hpp> |
| 31 | #include <ql/methods/finitedifferences/stepconditions/fdmstepconditioncomposite.hpp> |
| 32 | #include <ql/methods/finitedifferences/utilities/fdmdiscountdirichletboundary.hpp> |
| 33 | #include <ql/methods/finitedifferences/utilities/fdminnervaluecalculator.hpp> |
| 34 | #include <ql/methods/finitedifferences/utilities/fdmtimedepdirichletboundary.hpp> |
| 35 | #include <ql/pricingengines/vanilla/analyticcevengine.hpp> |
| 36 | #include <ql/pricingengines/vanilla/fdcevvanillaengine.hpp> |
| 37 | #include <ql/termstructures/yieldtermstructure.hpp> |
| 38 | #include <utility> |
| 39 | |
| 40 | namespace QuantLib { |
| 41 | |
| 42 | namespace { |
| 43 | class PriceAtBoundary { |
| 44 | public: |
| 45 | PriceAtBoundary(Time maturityTime, |
| 46 | ext::shared_ptr<StrikedTypePayoff> payoff, |
| 47 | ext::shared_ptr<YieldTermStructure> rTS, |
| 48 | ext::shared_ptr<CEVCalculator> calculator) |
| 49 | : maturityTime_(maturityTime), payoff_(std::move(payoff)), |
| 50 | calculator_(std::move(calculator)), rTS_(std::move(rTS)) {} |
| 51 | |
| 52 | Real operator()(Real t) const { |
| 53 | const Time time2Expiry = std::max(a: 1/365., b: maturityTime_ - t); |
| 54 | const DiscountFactor df = |
| 55 | rTS_->discount(t: maturityTime_) / rTS_->discount(t); |
| 56 | |
| 57 | return df * calculator_->value( |
| 58 | optionType: payoff_->optionType(), strike: payoff_->strike(), t: time2Expiry); |
| 59 | } |
| 60 | |
| 61 | private: |
| 62 | const Time maturityTime_; |
| 63 | const ext::shared_ptr<StrikedTypePayoff> payoff_; |
| 64 | const ext::shared_ptr<CEVCalculator> calculator_; |
| 65 | const ext::shared_ptr<YieldTermStructure> rTS_; |
| 66 | }; |
| 67 | } |
| 68 | |
| 69 | FdCEVVanillaEngine::FdCEVVanillaEngine(Real f0, |
| 70 | Real alpha, |
| 71 | Real beta, |
| 72 | Handle<YieldTermStructure> discountCurve, |
| 73 | Size tGrid, |
| 74 | Size xGrid, |
| 75 | Size dampingSteps, |
| 76 | Real scalingFactor, |
| 77 | Real eps, |
| 78 | const FdmSchemeDesc& schemeDesc) |
| 79 | : f0_(f0), alpha_(alpha), beta_(beta), discountCurve_(std::move(discountCurve)), tGrid_(tGrid), |
| 80 | xGrid_(xGrid), dampingSteps_(dampingSteps), scalingFactor_(scalingFactor), eps_(eps), |
| 81 | schemeDesc_(schemeDesc) { |
| 82 | registerWith(h: discountCurve_); |
| 83 | } |
| 84 | |
| 85 | void FdCEVVanillaEngine::calculate() const { |
| 86 | |
| 87 | // 1. Mesher |
| 88 | const ext::shared_ptr<StrikedTypePayoff> payoff = |
| 89 | ext::dynamic_pointer_cast<StrikedTypePayoff>(r: arguments_.payoff); |
| 90 | QL_REQUIRE(payoff, "non-striked payoff given" ); |
| 91 | |
| 92 | const ext::shared_ptr<YieldTermStructure> rTS = |
| 93 | discountCurve_.currentLink(); |
| 94 | |
| 95 | const DayCounter dc = rTS->dayCounter(); |
| 96 | |
| 97 | const Date referenceDate = rTS->referenceDate(); |
| 98 | const Date maturityDate = arguments_.exercise->lastDate(); |
| 99 | const Time maturityTime = dc.yearFraction(d1: referenceDate, d2: maturityDate); |
| 100 | |
| 101 | const ext::shared_ptr<Fdm1dMesher> cevMesher = |
| 102 | ext::make_shared<FdmCEV1dMesher>( |
| 103 | args: xGrid_, |
| 104 | args: f0_, args: alpha_, args: beta_, |
| 105 | args: maturityTime, args: eps_, args: scalingFactor_, |
| 106 | args: std::make_pair(x: payoff->strike(), y: 0.1)); |
| 107 | |
| 108 | const Real lowerBound = cevMesher->locations().front(); |
| 109 | const Real upperBound = cevMesher->locations().back(); |
| 110 | |
| 111 | const ext::shared_ptr<FdmMesher> mesher = |
| 112 | ext::make_shared<FdmMesherComposite>(args: cevMesher); |
| 113 | |
| 114 | // 2. Calculator |
| 115 | const ext::shared_ptr<FdmInnerValueCalculator> calculator = |
| 116 | ext::make_shared<FdmCellAveragingInnerValue>(args: payoff, args: mesher, args: 0); |
| 117 | |
| 118 | // 3. Step conditions |
| 119 | const ext::shared_ptr<FdmStepConditionComposite> conditions = |
| 120 | FdmStepConditionComposite::vanillaComposite( |
| 121 | schedule: DividendSchedule(), exercise: arguments_.exercise, |
| 122 | mesher, calculator, |
| 123 | refDate: referenceDate, dayCounter: dc); |
| 124 | |
| 125 | // 4. Boundary conditions |
| 126 | FdmBoundaryConditionSet boundaries; |
| 127 | |
| 128 | const PriceAtBoundary upperBoundPrice( |
| 129 | maturityTime, payoff, rTS, |
| 130 | ext::make_shared<CEVCalculator>(args: upperBound, args: alpha_, args: beta_)); |
| 131 | |
| 132 | boundaries.push_back(x: ext::make_shared<FdmTimeDepDirichletBoundary>( |
| 133 | args: mesher, args: ext::function<Real (Real)>(upperBoundPrice), |
| 134 | args: 0, args: FdmTimeDepDirichletBoundary::Upper)); |
| 135 | |
| 136 | const Real delta = (1-2*beta_)/(1-beta_); |
| 137 | if (delta < 2.0) { |
| 138 | const Real terminalCashFlow = (*payoff)(lowerBound); |
| 139 | |
| 140 | boundaries.push_back( |
| 141 | x: ext::make_shared<FdmDiscountDirichletBoundary>( |
| 142 | args: mesher, args: rTS, args: maturityTime, args: terminalCashFlow, |
| 143 | args: 0, args: FdmTimeDepDirichletBoundary::Lower)); |
| 144 | } |
| 145 | |
| 146 | // 5. Solver |
| 147 | const FdmSolverDesc solverDesc = { |
| 148 | .mesher: mesher, .bcSet: boundaries, .condition: conditions, |
| 149 | .calculator: calculator, .maturity: maturityTime, .timeSteps: tGrid_, .dampingSteps: dampingSteps_ |
| 150 | }; |
| 151 | |
| 152 | const ext::shared_ptr<FdmLinearOpComposite> op = |
| 153 | ext::make_shared<FdmCEVOp>( |
| 154 | args: mesher, args: discountCurve_.currentLink(), args: f0_, args: alpha_, args: beta_, args: 0); |
| 155 | |
| 156 | const ext::shared_ptr<Fdm1DimSolver> solver = |
| 157 | ext::make_shared<Fdm1DimSolver>(args: solverDesc, args: schemeDesc_, args: op); |
| 158 | |
| 159 | results_.value = solver->interpolateAt(x: f0_); |
| 160 | results_.delta = solver->derivativeX(x: f0_); |
| 161 | results_.gamma = solver->derivativeXX(x: f0_); |
| 162 | results_.theta = solver->thetaAt(x: f0_); |
| 163 | } |
| 164 | } |
| 165 | |