QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
fdcirvanillaengine.cpp
Go to the documentation of this file.
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
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)),
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 tGrid_(tGrid), xGrid_(xGrid), rGrid_(rGrid), dampingSteps_(dampingSteps), rho_(rho),
64 schemeDesc_(schemeDesc) {}
65
67
68 const ext::shared_ptr<StrikedTypePayoff> payoff =
69 ext::dynamic_pointer_cast<StrikedTypePayoff>(arguments_.payoff);
70 const Time maturity = bsProcess_->time(arguments_.exercise->lastDate());
71
72 // The short rate mesher
73 const ext::shared_ptr<Fdm1dMesher> shortRateMesher(
75
76 // The equity mesher
77 const ext::shared_ptr<Fdm1dMesher> equityMesher(
79 xGrid_, bsProcess_, maturity, payoff->strike(),
80 Null<Real>(), Null<Real>(), 0.0001, 1.5,
81 std::pair<Real, Real>(payoff->strike(), 0.1),
83 0.0));
84
85 const ext::shared_ptr<FdmMesher> mesher(
86 new FdmMesherComposite(equityMesher, shortRateMesher));
87
88 // Calculator
89 const ext::shared_ptr<FdmInnerValueCalculator> calculator(
90 new FdmLogInnerValue(arguments_.payoff, mesher, 0));
91
92 // Step conditions
93 const ext::shared_ptr<FdmStepConditionComposite> conditions =
95 dividends_, arguments_.exercise,
96 mesher, calculator,
97 bsProcess_->riskFreeRate()->referenceDate(),
98 bsProcess_->riskFreeRate()->dayCounter());
99
100 // Boundary conditions
101 const FdmBoundaryConditionSet boundaries;
102
103 // Solver
104 FdmSolverDesc solverDesc = { mesher, boundaries, conditions,
105 calculator, maturity,
107
108 return solverDesc;
109 }
110
112 const ext::shared_ptr<StrikedTypePayoff> payoff =
113 ext::dynamic_pointer_cast<StrikedTypePayoff>(arguments_.payoff);
114
115 ext::shared_ptr<FdmCIRSolver> solver(new FdmCIRSolver(
119 rho_, payoff->strike()));
120
121 const Real r0 = cirProcess_->x0();
122 const Real spot = bsProcess_->x0();
123
124 results_.value = solver->valueAt(spot, r0);
125 results_.delta = solver->deltaAt(spot, r0);
126 results_.gamma = solver->gammaAt(spot, r0);
127 results_.theta = solver->thetaAt(spot, r0);
128 }
129
131 ext::shared_ptr<CoxIngersollRossProcess> cirProcess,
132 ext::shared_ptr<GeneralizedBlackScholesProcess> bsProcess,
133 const Real rho)
134 : cirProcess_(std::move(cirProcess)), bsProcess_(std::move(bsProcess)), rho_(rho),
135 schemeDesc_(ext::make_shared<FdmSchemeDesc>(FdmSchemeDesc::ModifiedHundsdorfer())) {}
136
138 const ext::shared_ptr<FdmQuantoHelper>& quantoHelper) {
139 quantoHelper_ = quantoHelper;
140 return *this;
141 }
142
145 tGrid_ = tGrid;
146 return *this;
147 }
148
151 xGrid_ = xGrid;
152 return *this;
153 }
154
157 rGrid_ = rGrid;
158 return *this;
159 }
160
163 dampingSteps_ = dampingSteps;
164 return *this;
165 }
166
169 const FdmSchemeDesc& schemeDesc) {
170 schemeDesc_ = ext::make_shared<FdmSchemeDesc>(schemeDesc);
171 return *this;
172 }
173
176 const std::vector<Date>& dividendDates,
177 const std::vector<Real>& dividendAmounts) {
178 dividends_ = DividendVector(dividendDates, dividendAmounts);
179 return *this;
180 }
181
182 MakeFdCIRVanillaEngine::operator
183 ext::shared_ptr<PricingEngine>() const {
184 return ext::make_shared<FdCIRVanillaEngine>(
185 cirProcess_,
186 bsProcess_,
187 dividends_,
188 tGrid_, xGrid_, rGrid_, dampingSteps_,
189 rho_,
190 *schemeDesc_,
191 quantoHelper_);
192 }
193
194}
Black-Scholes processes.
const Instrument::results * results_
Definition: cdsoption.cpp:63
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
CoxIngersollRoss process.
Finite-differences CIR vanilla option engine.
1-d mesher for the Black-Scholes process (in ln(S))
layer of abstraction to calculate the inner value
memory layout of a fdm linear operator
FdmMesher which is a composite of Fdm1dMesher.
One-dimensional grid mesher.
composite of fdm step conditions
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
Real rho
ext::shared_ptr< QuantLib::Payoff > payoff
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.