/src/quantlib/ql/pricingengines/asian/fdblackscholesasianengine.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) 2009 Ralph Schreyer |
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 | | #include <ql/exercise.hpp> |
21 | | #include <ql/math/distributions/normaldistribution.hpp> |
22 | | #include <ql/methods/finitedifferences/meshers/fdmblackscholesmesher.hpp> |
23 | | #include <ql/methods/finitedifferences/meshers/fdmmeshercomposite.hpp> |
24 | | #include <ql/methods/finitedifferences/operators/fdmlinearoplayout.hpp> |
25 | | #include <ql/methods/finitedifferences/solvers/fdmsimple2dbssolver.hpp> |
26 | | #include <ql/methods/finitedifferences/stepconditions/fdmarithmeticaveragecondition.hpp> |
27 | | #include <ql/methods/finitedifferences/stepconditions/fdmstepconditioncomposite.hpp> |
28 | | #include <ql/methods/finitedifferences/utilities/fdminnervaluecalculator.hpp> |
29 | | #include <ql/pricingengines/asian/fdblackscholesasianengine.hpp> |
30 | | #include <ql/processes/blackscholesprocess.hpp> |
31 | | #include <utility> |
32 | | |
33 | | namespace QuantLib { |
34 | | |
35 | | |
36 | | FdBlackScholesAsianEngine::FdBlackScholesAsianEngine( |
37 | | ext::shared_ptr<GeneralizedBlackScholesProcess> process, |
38 | | Size tGrid, |
39 | | Size xGrid, |
40 | | Size aGrid, |
41 | | const FdmSchemeDesc& schemeDesc) |
42 | 0 | : process_(std::move(process)), tGrid_(tGrid), xGrid_(xGrid), aGrid_(aGrid), |
43 | 0 | schemeDesc_(schemeDesc) {} |
44 | | |
45 | | |
46 | 0 | void FdBlackScholesAsianEngine::calculate() const { |
47 | |
|
48 | 0 | QL_REQUIRE(arguments_.exercise->type() == Exercise::European, |
49 | 0 | "European exercise supported only"); |
50 | 0 | QL_REQUIRE(arguments_.averageType == Average::Arithmetic, |
51 | 0 | "Arithmetic averaging supported only"); |
52 | 0 | QL_REQUIRE( arguments_.runningAccumulator == 0 |
53 | 0 | || arguments_.pastFixings > 0, |
54 | 0 | "Running average requires at least one past fixing"); |
55 | | |
56 | | // 1. Mesher |
57 | 0 | const ext::shared_ptr<StrikedTypePayoff> payoff = |
58 | 0 | ext::dynamic_pointer_cast<StrikedTypePayoff>(arguments_.payoff); |
59 | 0 | const Time maturity = process_->time(arguments_.exercise->lastDate()); |
60 | 0 | const ext::shared_ptr<Fdm1dMesher> equityMesher( |
61 | 0 | new FdmBlackScholesMesher(xGrid_, process_, maturity, |
62 | 0 | payoff->strike())); |
63 | |
|
64 | 0 | const Real spot = process_->x0(); |
65 | 0 | QL_REQUIRE(spot > 0.0, "negative or null underlying given"); |
66 | | |
67 | 0 | const Real avg = (arguments_.runningAccumulator == 0) |
68 | 0 | ? spot : arguments_.runningAccumulator/arguments_.pastFixings; |
69 | |
|
70 | 0 | const Real normInvEps = InverseCumulativeNormal()(1-0.0001); |
71 | 0 | const Real sigmaSqrtT |
72 | 0 | = process_->blackVolatility()->blackVol(maturity, payoff->strike()) |
73 | 0 | *std::sqrt(maturity); |
74 | 0 | const Real r = sigmaSqrtT*normInvEps; |
75 | |
|
76 | 0 | Real xMin = std::min(std::log(avg) - 0.25*r, std::log(spot) - 1.5*r); |
77 | 0 | Real xMax = std::max(std::log(avg) + 0.25*r, std::log(spot) + 1.5*r); |
78 | |
|
79 | 0 | const ext::shared_ptr<Fdm1dMesher> averageMesher( |
80 | 0 | new FdmBlackScholesMesher(aGrid_, process_, maturity, |
81 | 0 | payoff->strike(), xMin, xMax)); |
82 | |
|
83 | 0 | const ext::shared_ptr<FdmMesher> mesher ( |
84 | 0 | new FdmMesherComposite(equityMesher, averageMesher)); |
85 | | |
86 | | // 2. Calculator |
87 | 0 | ext::shared_ptr<FdmInnerValueCalculator> calculator( |
88 | 0 | new FdmLogInnerValue(payoff, mesher, 1)); |
89 | | |
90 | | // 3. Step conditions |
91 | 0 | std::list<ext::shared_ptr<StepCondition<Array> > > stepConditions; |
92 | 0 | std::list<std::vector<Time> > stoppingTimes; |
93 | | |
94 | | // 3.1 Arithmetic average step conditions |
95 | 0 | std::vector<Time> averageTimes; |
96 | 0 | for (auto& fixingDate : arguments_.fixingDates) { |
97 | 0 | Time t = process_->time(fixingDate); |
98 | 0 | QL_REQUIRE(t >= 0, "Fixing dates must not contain past date"); |
99 | 0 | averageTimes.push_back(t); |
100 | 0 | } |
101 | 0 | stoppingTimes.emplace_back(averageTimes); |
102 | 0 | stepConditions.push_back(ext::shared_ptr<StepCondition<Array> >( |
103 | 0 | new FdmArithmeticAverageCondition( |
104 | 0 | averageTimes, arguments_.runningAccumulator, |
105 | 0 | arguments_.pastFixings, mesher, 0))); |
106 | |
|
107 | 0 | ext::shared_ptr<FdmStepConditionComposite> conditions( |
108 | 0 | new FdmStepConditionComposite(stoppingTimes, stepConditions)); |
109 | | |
110 | | // 4. Boundary conditions |
111 | 0 | const FdmBoundaryConditionSet boundaries; |
112 | | |
113 | | // 5. Solver |
114 | 0 | FdmSolverDesc solverDesc = { mesher, boundaries, conditions, |
115 | 0 | calculator, maturity, tGrid_, 0 }; |
116 | 0 | ext::shared_ptr<FdmSimple2dBSSolver> solver( |
117 | 0 | new FdmSimple2dBSSolver( |
118 | 0 | Handle<GeneralizedBlackScholesProcess>(process_), |
119 | 0 | payoff->strike(), solverDesc, schemeDesc_)); |
120 | |
|
121 | 0 | results_.value = solver->valueAt(spot, avg); |
122 | 0 | results_.delta = solver->deltaAt(spot, avg, spot*0.01); |
123 | 0 | results_.gamma = solver->gammaAt(spot, avg, spot*0.01); |
124 | 0 | } |
125 | | } |