/src/quantlib/ql/pricingengines/vanilla/fdsimplebsswingengine.cpp
Line | Count | Source |
1 | | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* |
4 | | Copyright (C) 2010 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 fdsimplebsswingengine.cpp |
21 | | \brief Finite Differences Black-Scholes engine for simple swing options |
22 | | */ |
23 | | |
24 | | #include <ql/methods/finitedifferences/meshers/fdmblackscholesmesher.hpp> |
25 | | #include <ql/methods/finitedifferences/meshers/fdmmeshercomposite.hpp> |
26 | | #include <ql/methods/finitedifferences/meshers/uniform1dmesher.hpp> |
27 | | #include <ql/methods/finitedifferences/operators/fdmlinearoplayout.hpp> |
28 | | #include <ql/methods/finitedifferences/solvers/fdmsimple2dbssolver.hpp> |
29 | | #include <ql/methods/finitedifferences/stepconditions/fdmsimpleswingcondition.hpp> |
30 | | #include <ql/methods/finitedifferences/stepconditions/fdmstepconditioncomposite.hpp> |
31 | | #include <ql/methods/finitedifferences/utilities/fdminnervaluecalculator.hpp> |
32 | | #include <ql/pricingengines/vanilla/fdsimplebsswingengine.hpp> |
33 | | #include <ql/processes/blackscholesprocess.hpp> |
34 | | #include <utility> |
35 | | |
36 | | namespace QuantLib { |
37 | | |
38 | | FdSimpleBSSwingEngine::FdSimpleBSSwingEngine( |
39 | | ext::shared_ptr<GeneralizedBlackScholesProcess> process, |
40 | | Size tGrid, |
41 | | Size xGrid, |
42 | | const FdmSchemeDesc& schemeDesc) |
43 | 0 | : process_(std::move(process)), tGrid_(tGrid), xGrid_(xGrid), schemeDesc_(schemeDesc) {} |
44 | | |
45 | 0 | void FdSimpleBSSwingEngine::calculate() const { |
46 | 0 | QL_REQUIRE(arguments_.exercise->type() == Exercise::Bermudan, |
47 | 0 | "Bermudan exercise supported only"); |
48 | | |
49 | | // 1. Mesher |
50 | 0 | const ext::shared_ptr<StrikedTypePayoff> payoff = |
51 | 0 | ext::dynamic_pointer_cast<StrikedTypePayoff>(arguments_.payoff); |
52 | 0 | QL_REQUIRE(payoff, "Strike type payoff expected"); |
53 | | |
54 | 0 | const Time maturity = process_->time(arguments_.exercise->lastDate()); |
55 | 0 | const ext::shared_ptr<Fdm1dMesher> equityMesher( |
56 | 0 | new FdmBlackScholesMesher(xGrid_, process_, |
57 | 0 | maturity, payoff->strike())); |
58 | | |
59 | 0 | const ext::shared_ptr<Fdm1dMesher> exerciseMesher( |
60 | 0 | new Uniform1dMesher( |
61 | 0 | 0, static_cast<Real>(arguments_.maxExerciseRights), |
62 | 0 | arguments_.maxExerciseRights+1)); |
63 | |
|
64 | 0 | const ext::shared_ptr<FdmMesher> mesher ( |
65 | 0 | new FdmMesherComposite(equityMesher, exerciseMesher)); |
66 | | |
67 | | // 2. Calculator |
68 | 0 | ext::shared_ptr<FdmInnerValueCalculator> calculator( |
69 | 0 | new FdmZeroInnerValue()); |
70 | | |
71 | | // 3. Step conditions |
72 | 0 | std::list<ext::shared_ptr<StepCondition<Array> > > stepConditions; |
73 | 0 | std::list<std::vector<Time> > stoppingTimes; |
74 | | |
75 | | // 3.1 Bermudan step conditions |
76 | 0 | std::vector<Time> exerciseTimes; |
77 | 0 | for (auto i : arguments_.exercise->dates()) { |
78 | 0 | Time t = process_->time(i); |
79 | 0 | QL_REQUIRE(t >= 0, "exercise dates must not contain past date"); |
80 | 0 | exerciseTimes.push_back(t); |
81 | 0 | } |
82 | 0 | stoppingTimes.push_back(exerciseTimes); |
83 | | |
84 | 0 | ext::shared_ptr<FdmInnerValueCalculator> exerciseCalculator( |
85 | 0 | new FdmLogInnerValue(payoff, mesher, 0)); |
86 | |
|
87 | 0 | stepConditions.push_back(ext::shared_ptr<StepCondition<Array> >( |
88 | 0 | new FdmSimpleSwingCondition( |
89 | 0 | exerciseTimes, mesher, exerciseCalculator, |
90 | 0 | 1, arguments_.minExerciseRights))); |
91 | | |
92 | 0 | ext::shared_ptr<FdmStepConditionComposite> conditions( |
93 | 0 | new FdmStepConditionComposite(stoppingTimes, stepConditions)); |
94 | | |
95 | | // 4. Boundary conditions |
96 | 0 | const FdmBoundaryConditionSet boundaries; |
97 | | |
98 | | // 5. Solver |
99 | 0 | FdmSolverDesc solverDesc = { mesher, boundaries, conditions, |
100 | 0 | calculator, maturity, tGrid_, 0 }; |
101 | 0 | ext::shared_ptr<FdmSimple2dBSSolver> solver( |
102 | 0 | new FdmSimple2dBSSolver( |
103 | 0 | Handle<GeneralizedBlackScholesProcess>(process_), |
104 | 0 | payoff->strike(), solverDesc, schemeDesc_)); |
105 | | |
106 | 0 | const Real spot = process_->x0(); |
107 | |
|
108 | 0 | results_.value = solver->valueAt(spot, 1); |
109 | 0 | results_.delta = solver->deltaAt(spot, 1, spot*0.01); |
110 | 0 | results_.gamma = solver->gammaAt(spot, 1, spot*0.01); |
111 | 0 | results_.theta = solver->thetaAt(spot, 1); |
112 | 0 | } |
113 | | } |