/src/quantlib/ql/pricingengines/vanilla/fdsabrvanillaengine.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* |
4 | | Copyright (C) 2019 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 | | <https://www.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 fdsabrvanillaengine.hpp */ |
21 | | |
22 | | #include <ql/exercise.hpp> |
23 | | #include <ql/math/distributions/normaldistribution.hpp> |
24 | | #include <ql/methods/finitedifferences/meshers/concentrating1dmesher.hpp> |
25 | | #include <ql/methods/finitedifferences/meshers/fdmcev1dmesher.hpp> |
26 | | #include <ql/methods/finitedifferences/meshers/fdmmeshercomposite.hpp> |
27 | | #include <ql/methods/finitedifferences/operators/fdmsabrop.hpp> |
28 | | #include <ql/methods/finitedifferences/solvers/fdm2dimsolver.hpp> |
29 | | #include <ql/methods/finitedifferences/stepconditions/fdmstepconditioncomposite.hpp> |
30 | | #include <ql/methods/finitedifferences/utilities/cevrndcalculator.hpp> |
31 | | #include <ql/methods/finitedifferences/utilities/fdmdiscountdirichletboundary.hpp> |
32 | | #include <ql/methods/finitedifferences/utilities/fdminnervaluecalculator.hpp> |
33 | | #include <ql/pricingengines/vanilla/fdsabrvanillaengine.hpp> |
34 | | #include <ql/termstructures/volatility/sabr.hpp> |
35 | | #include <ql/termstructures/yieldtermstructure.hpp> |
36 | | #include <utility> |
37 | | |
38 | | namespace QuantLib { |
39 | | |
40 | | FdSabrVanillaEngine::FdSabrVanillaEngine(Real f0, |
41 | | Real alpha, |
42 | | Real beta, |
43 | | Real nu, |
44 | | Real rho, |
45 | | Handle<YieldTermStructure> rTS, |
46 | | Size tGrid, |
47 | | Size fGrid, |
48 | | Size xGrid, |
49 | | Size dampingSteps, |
50 | | Real scalingFactor, |
51 | | Real eps, |
52 | | const FdmSchemeDesc& schemeDesc) |
53 | 0 | : f0_(f0), alpha_(alpha), beta_(beta), nu_(nu), rho_(rho), rTS_(std::move(rTS)), tGrid_(tGrid), |
54 | 0 | fGrid_(fGrid), xGrid_(xGrid), dampingSteps_(dampingSteps), scalingFactor_(scalingFactor), |
55 | 0 | eps_(eps), schemeDesc_(schemeDesc) { |
56 | |
|
57 | 0 | validateSabrParameters(alpha, 0.5, nu, rho); |
58 | |
|
59 | 0 | QL_REQUIRE(beta<1.0, "beta must be smaller than 1.0: " |
60 | 0 | << beta << " not allowed"); |
61 | | |
62 | 0 | registerWith(rTS_); |
63 | 0 | } |
64 | | |
65 | 0 | void FdSabrVanillaEngine::calculate() const { |
66 | | // 1. Mesher |
67 | 0 | const ext::shared_ptr<StrikedTypePayoff> payoff = |
68 | 0 | ext::dynamic_pointer_cast<StrikedTypePayoff>(arguments_.payoff); |
69 | 0 | QL_REQUIRE(payoff, "non-striked payoff given"); |
70 | | |
71 | 0 | const DayCounter dc = rTS_->dayCounter(); |
72 | |
|
73 | 0 | const Date referenceDate = rTS_->referenceDate(); |
74 | 0 | const Date maturityDate = arguments_.exercise->lastDate(); |
75 | 0 | const Time maturityTime = dc.yearFraction(referenceDate, maturityDate); |
76 | |
|
77 | 0 | const Real upperAlpha = alpha_* |
78 | 0 | std::exp(nu_*std::sqrt(maturityTime)*InverseCumulativeNormal()(0.75)); |
79 | |
|
80 | 0 | const ext::shared_ptr<Fdm1dMesher> cevMesher = |
81 | 0 | ext::make_shared<FdmCEV1dMesher>( |
82 | 0 | fGrid_, f0_, upperAlpha, beta_, |
83 | 0 | maturityTime, eps_, scalingFactor_, |
84 | 0 | std::make_pair(payoff->strike(), 0.025)); |
85 | |
|
86 | 0 | const Real normInvEps = InverseCumulativeNormal()(1-eps_); |
87 | 0 | const Real logDrift = -0.5*nu_*nu_*maturityTime; |
88 | 0 | const Real volRange = |
89 | 0 | nu_*std::sqrt(maturityTime)*normInvEps*scalingFactor_; |
90 | |
|
91 | 0 | const Real xMin = std::log(alpha_) + logDrift - volRange; |
92 | 0 | const Real xMax = std::log(alpha_) + logDrift + volRange; |
93 | |
|
94 | 0 | const ext::shared_ptr<Fdm1dMesher> xMesher = |
95 | 0 | ext::make_shared<Concentrating1dMesher>( |
96 | 0 | xMin, xMax, xGrid_, std::make_pair(std::log(alpha_), 0.1)); |
97 | |
|
98 | 0 | const ext::shared_ptr<FdmMesher> mesher = |
99 | 0 | ext::make_shared<FdmMesherComposite>(cevMesher, xMesher); |
100 | | |
101 | | // 2. Calculator |
102 | 0 | const ext::shared_ptr<FdmInnerValueCalculator> calculator = |
103 | 0 | ext::make_shared<FdmCellAveragingInnerValue>(payoff, mesher, 0); |
104 | | |
105 | | // 3. Step conditions |
106 | 0 | const ext::shared_ptr<FdmStepConditionComposite> conditions = |
107 | 0 | FdmStepConditionComposite::vanillaComposite( |
108 | 0 | DividendSchedule(), arguments_.exercise, |
109 | 0 | mesher, calculator, referenceDate, dc); |
110 | | |
111 | | // 4. Boundary conditions |
112 | 0 | FdmBoundaryConditionSet boundaries; |
113 | |
|
114 | 0 | const Real lowerBound = cevMesher->locations().front(); |
115 | 0 | const Real upperBound = cevMesher->locations().back(); |
116 | |
|
117 | 0 | boundaries.push_back( |
118 | 0 | ext::make_shared<FdmDiscountDirichletBoundary>( |
119 | 0 | mesher, rTS_.currentLink(), |
120 | 0 | maturityTime, (*payoff)(upperBound), |
121 | 0 | 0, FdmDiscountDirichletBoundary::Upper)); |
122 | |
|
123 | 0 | boundaries.push_back( |
124 | 0 | ext::make_shared<FdmDiscountDirichletBoundary>( |
125 | 0 | mesher, rTS_.currentLink(), |
126 | 0 | maturityTime, (*payoff)(lowerBound), |
127 | 0 | 0, FdmDiscountDirichletBoundary::Lower)); |
128 | | |
129 | | // 5. Solver |
130 | 0 | const FdmSolverDesc solverDesc = { |
131 | 0 | mesher, boundaries, conditions, |
132 | 0 | calculator, maturityTime, tGrid_, dampingSteps_ |
133 | 0 | }; |
134 | |
|
135 | 0 | const ext::shared_ptr<FdmLinearOpComposite> op = |
136 | 0 | ext::make_shared<FdmSabrOp>( |
137 | 0 | mesher, rTS_.currentLink(), |
138 | 0 | f0_, alpha_, beta_, nu_, rho_); |
139 | |
|
140 | 0 | const ext::shared_ptr<Fdm2DimSolver> solver = |
141 | 0 | ext::make_shared<Fdm2DimSolver>(solverDesc, schemeDesc_, op); |
142 | |
|
143 | 0 | results_.value = solver->interpolateAt(f0_, std::log(alpha_)); |
144 | 0 | } |
145 | | } |
146 | | |