QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
fdcirvanillaengine.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2020 Lew Wei Hao
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
21#include <ql/methods/finitedifferences/meshers/fdmblackscholesmesher.hpp>
22#include <ql/methods/finitedifferences/meshers/fdmmeshercomposite.hpp>
23#include <ql/methods/finitedifferences/meshers/fdmsimpleprocess1dmesher.hpp>
24#include <ql/methods/finitedifferences/operators/fdmlinearoplayout.hpp>
25#include <ql/methods/finitedifferences/solvers/fdmcirsolver.hpp>
26#include <ql/methods/finitedifferences/stepconditions/fdmstepconditioncomposite.hpp>
27#include <ql/methods/finitedifferences/utilities/fdminnervaluecalculator.hpp>
28#include <ql/pricingengines/vanilla/fdcirvanillaengine.hpp>
29#include <ql/processes/blackscholesprocess.hpp>
30#include <ql/processes/coxingersollrossprocess.hpp>
31#include <utility>
32
33namespace QuantLib {
34
36 ext::shared_ptr<CoxIngersollRossProcess> cirProcess,
37 ext::shared_ptr<GeneralizedBlackScholesProcess> bsProcess,
38 Size tGrid,
39 Size xGrid,
40 Size rGrid,
41 Size dampingSteps,
42 const Real rho,
43 const FdmSchemeDesc& schemeDesc,
44 ext::shared_ptr<FdmQuantoHelper> quantoHelper)
45 : bsProcess_(std::move(bsProcess)), cirProcess_(std::move(cirProcess)),
46 quantoHelper_(std::move(quantoHelper)), explicitDividends_(false),
47 tGrid_(tGrid), xGrid_(xGrid), rGrid_(rGrid), dampingSteps_(dampingSteps),
48 rho_(rho), schemeDesc_(schemeDesc) {}
49
51 ext::shared_ptr<CoxIngersollRossProcess> cirProcess,
52 ext::shared_ptr<GeneralizedBlackScholesProcess> bsProcess,
53 DividendSchedule dividends,
54 Size tGrid,
55 Size xGrid,
56 Size rGrid,
57 Size dampingSteps,
58 const Real rho,
59 const FdmSchemeDesc& schemeDesc,
60 ext::shared_ptr<FdmQuantoHelper> quantoHelper)
61 : bsProcess_(std::move(bsProcess)), cirProcess_(std::move(cirProcess)),
62 quantoHelper_(std::move(quantoHelper)), dividends_(std::move(dividends)),
63 explicitDividends_(true),
64 tGrid_(tGrid), xGrid_(xGrid), rGrid_(rGrid), dampingSteps_(dampingSteps), rho_(rho),
65 schemeDesc_(schemeDesc) {}
66
68
69 // dividends will eventually be moved out of arguments, but for now we need the switch
70 QL_DEPRECATED_DISABLE_WARNING
72 QL_DEPRECATED_ENABLE_WARNING
73
74 const ext::shared_ptr<StrikedTypePayoff> payoff =
75 ext::dynamic_pointer_cast<StrikedTypePayoff>(arguments_.payoff);
76 const Time maturity = bsProcess_->time(arguments_.exercise->lastDate());
77
78 // The short rate mesher
79 const ext::shared_ptr<Fdm1dMesher> shortRateMesher(
81
82 // The equity mesher
83 const ext::shared_ptr<Fdm1dMesher> equityMesher(
85 xGrid_, bsProcess_, maturity, payoff->strike(),
86 Null<Real>(), Null<Real>(), 0.0001, 1.5,
87 std::pair<Real, Real>(payoff->strike(), 0.1),
88 passedDividends, quantoHelper_,
89 0.0));
90
91 const ext::shared_ptr<FdmMesher> mesher(
92 new FdmMesherComposite(equityMesher, shortRateMesher));
93
94 // Calculator
95 const ext::shared_ptr<FdmInnerValueCalculator> calculator(
96 new FdmLogInnerValue(arguments_.payoff, mesher, 0));
97
98 // Step conditions
99 const ext::shared_ptr<FdmStepConditionComposite> conditions =
101 passedDividends, arguments_.exercise,
102 mesher, calculator,
103 bsProcess_->riskFreeRate()->referenceDate(),
104 bsProcess_->riskFreeRate()->dayCounter());
105
106 // Boundary conditions
107 const FdmBoundaryConditionSet boundaries;
108
109 // Solver
110 FdmSolverDesc solverDesc = { mesher, boundaries, conditions,
111 calculator, maturity,
113
114 return solverDesc;
115 }
116
118 const ext::shared_ptr<StrikedTypePayoff> payoff =
119 ext::dynamic_pointer_cast<StrikedTypePayoff>(arguments_.payoff);
120
121 ext::shared_ptr<FdmCIRSolver> solver(new FdmCIRSolver(
125 rho_, payoff->strike()));
126
127 const Real r0 = cirProcess_->x0();
128 const Real spot = bsProcess_->x0();
129
130 results_.value = solver->valueAt(spot, r0);
131 results_.delta = solver->deltaAt(spot, r0);
132 results_.gamma = solver->gammaAt(spot, r0);
133 results_.theta = solver->thetaAt(spot, r0);
134 }
135
137 ext::shared_ptr<CoxIngersollRossProcess> cirProcess,
138 ext::shared_ptr<GeneralizedBlackScholesProcess> bsProcess,
139 const Real rho)
140 : cirProcess_(std::move(cirProcess)), bsProcess_(std::move(bsProcess)), rho_(rho),
141 schemeDesc_(ext::make_shared<FdmSchemeDesc>(FdmSchemeDesc::ModifiedHundsdorfer())) {}
142
144 const ext::shared_ptr<FdmQuantoHelper>& quantoHelper) {
145 quantoHelper_ = quantoHelper;
146 return *this;
147 }
148
151 tGrid_ = tGrid;
152 return *this;
153 }
154
157 xGrid_ = xGrid;
158 return *this;
159 }
160
163 rGrid_ = rGrid;
164 return *this;
165 }
166
169 dampingSteps_ = dampingSteps;
170 return *this;
171 }
172
175 const FdmSchemeDesc& schemeDesc) {
176 schemeDesc_ = ext::make_shared<FdmSchemeDesc>(schemeDesc);
177 return *this;
178 }
179
182 const std::vector<Date>& dividendDates,
183 const std::vector<Real>& dividendAmounts) {
184 dividends_ = DividendVector(dividendDates, dividendAmounts);
185 explicitDividends_ = true;
186 return *this;
187 }
188
189 MakeFdCIRVanillaEngine::operator
190 ext::shared_ptr<PricingEngine>() const {
191 if (explicitDividends_) {
192 return ext::make_shared<FdCIRVanillaEngine>(
193 cirProcess_,
194 bsProcess_,
195 dividends_,
196 tGrid_, xGrid_, rGrid_, dampingSteps_,
197 rho_,
198 *schemeDesc_,
199 quantoHelper_);
200 } else {
201 return ext::make_shared<FdCIRVanillaEngine>(
202 cirProcess_,
203 bsProcess_,
204 tGrid_, xGrid_, rGrid_, dampingSteps_,
205 rho_,
206 *schemeDesc_,
207 quantoHelper_);
208 }
209 }
210
211}
FdmSolverDesc getSolverDesc(Real equityScaleFactor) const
ext::shared_ptr< GeneralizedBlackScholesProcess > bsProcess_
FdCIRVanillaEngine(ext::shared_ptr< CoxIngersollRossProcess > cirProcess, ext::shared_ptr< GeneralizedBlackScholesProcess > bsProcess, Size tGrid, Size xGrid, Size vGrid, Size dampingSteps, Real rho, const FdmSchemeDesc &schemeDesc=FdmSchemeDesc::ModifiedHundsdorfer(), ext::shared_ptr< FdmQuantoHelper > quantoHelper={})
ext::shared_ptr< CoxIngersollRossProcess > cirProcess_
ext::shared_ptr< FdmQuantoHelper > quantoHelper_
static ext::shared_ptr< FdmStepConditionComposite > vanillaComposite(const DividendSchedule &schedule, const ext::shared_ptr< Exercise > &exercise, const ext::shared_ptr< FdmMesher > &mesher, const ext::shared_ptr< FdmInnerValueCalculator > &calculator, const Date &refDate, const DayCounter &dayCounter)
Shared handle to an observable.
Definition: handle.hpp:41
MakeFdCIRVanillaEngine & withTGrid(Size tGrid)
MakeFdCIRVanillaEngine & withQuantoHelper(const ext::shared_ptr< FdmQuantoHelper > &quantoHelper)
MakeFdCIRVanillaEngine & withCashDividends(const std::vector< Date > &dividendDates, const std::vector< Real > &dividendAmounts)
MakeFdCIRVanillaEngine & withRGrid(Size rGrid)
MakeFdCIRVanillaEngine & withFdmSchemeDesc(const FdmSchemeDesc &schemeDesc)
MakeFdCIRVanillaEngine(ext::shared_ptr< CoxIngersollRossProcess > cirProcess, ext::shared_ptr< GeneralizedBlackScholesProcess > bsProcess, Real rho)
MakeFdCIRVanillaEngine & withXGrid(Size xGrid)
MakeFdCIRVanillaEngine & withDampingSteps(Size dampingSteps)
ext::shared_ptr< FdmQuantoHelper > quantoHelper_
ext::shared_ptr< FdmSchemeDesc > schemeDesc_
template class providing a null value for a given type.
Definition: null.hpp:76
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
std::vector< ext::shared_ptr< Dividend > > DividendSchedule
std::vector< ext::shared_ptr< Dividend > > DividendVector(const std::vector< Date > &dividendDates, const std::vector< Real > &dividends)
helper function building a sequence of fixed dividends
Definition: dividend.cpp:35
OperatorTraits< FdmLinearOp >::bc_set FdmBoundaryConditionSet
STL namespace.