QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
fdmbatesop.cpp
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 <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
24#include <ql/math/interpolations/linearinterpolation.hpp>
25#include <ql/math/matrix.hpp>
26#include <ql/methods/finitedifferences/meshers/fdmmesher.hpp>
27#include <ql/methods/finitedifferences/operators/fdmbatesop.hpp>
28#include <ql/methods/finitedifferences/operators/fdmlinearoplayout.hpp>
29#include <ql/methods/finitedifferences/utilities/fdmdirichletboundary.hpp>
30#include <ql/processes/batesprocess.hpp>
31#include <ql/quotes/simplequote.hpp>
32#include <ql/termstructures/yield/zerospreadedtermstructure.hpp>
33#include <utility>
34
35namespace QuantLib {
36
37 FdmBatesOp::FdmBatesOp(const ext::shared_ptr<FdmMesher>& mesher,
38 const ext::shared_ptr<BatesProcess>& batesProcess,
40 const Size integroIntegrationOrder,
41 const ext::shared_ptr<FdmQuantoHelper>& quantoHelper)
42 : lambda_(batesProcess->lambda()), delta_(batesProcess->delta()), nu_(batesProcess->nu()),
43 m_(std::exp(nu_ + 0.5 * delta_ * delta_) - 1.0),
44 gaussHermiteIntegration_(integroIntegrationOrder), mesher_(mesher), bcSet_(std::move(bcSet)),
45 hestonOp_(new FdmHestonOp(
46 mesher,
47 ext::make_shared<HestonProcess>(
48 batesProcess->riskFreeRate(),
50 batesProcess->dividendYield(),
51 Handle<Quote>(ext::shared_ptr<Quote>(new SimpleQuote(lambda_ * m_))),
54 batesProcess->dividendYield()->dayCounter())),
55 batesProcess->s0(),
56 batesProcess->v0(),
57 batesProcess->kappa(),
58 batesProcess->theta(),
59 batesProcess->sigma(),
60 batesProcess->rho()),
61 quantoHelper)) {}
62
64 const ext::shared_ptr<LinearInterpolation>& interpl,
65 const FdmBoundaryConditionSet& bcSet,
66 Real x, Real delta, Real nu)
67 : x_(x), delta_(delta), nu_(nu),
68 bcSet_(bcSet), interpl_(interpl) { }
69
71 const Real x = x_ + M_SQRT2*delta_*y + nu_;
72 Real valueOfDerivative = (*interpl_)(x, true);
73
74 for (auto iter = bcSet_.begin(); iter < bcSet_.end(); ++iter) {
75
76 const ext::shared_ptr<FdmDirichletBoundary> dirichlet
77 = ext::dynamic_pointer_cast<FdmDirichletBoundary>(*iter);
78
79 QL_REQUIRE(dirichlet, "FdmBatesOp can only deal with Dirichlet "
80 "boundary conditions.");
81
82 valueOfDerivative
83 = dirichlet->applyAfterApplying(x, valueOfDerivative);
84 }
85
86 return std::exp(-y*y)*valueOfDerivative;
87 }
88
90 QL_REQUIRE(mesher_->layout()->dim().size() == 2, "invalid layout dimension");
91
92 Array x(mesher_->layout()->dim()[0]);
93 Matrix f(mesher_->layout()->dim()[1], mesher_->layout()->dim()[0]);
94
95 for (const auto& iter : *mesher_->layout()) {
96 const Size i = iter.coordinates()[0];
97 const Size j = iter.coordinates()[1];
98
99 x[i] = mesher_->location(iter, 0);
100 f[j][i] = r[iter.index()];
101
102 }
103 std::vector<ext::shared_ptr<LinearInterpolation> > interpl(f.rows());
104 for (Size i=0; i < f.rows(); ++i) {
105 interpl[i] = ext::make_shared<LinearInterpolation>(
106 x.begin(), x.end(), f.row_begin(i));
107 }
108
109 Array integral(r.size());
110 for (const auto& iter : *mesher_->layout()) {
111 const Size i = iter.coordinates()[0];
112 const Size j = iter.coordinates()[1];
113
114 integral[iter.index()] = M_1_SQRTPI*
116 IntegroIntegrand(interpl[j], bcSet_, x[i], delta_, nu_));
117 }
118
119 return lambda_*(integral-r);
120 }
121
122 std::vector<SparseMatrix> FdmBatesOp::toMatrixDecomp() const {
123 QL_FAIL("not implemented");
124 }
125
126}
1-D array used in linear algebra.
Definition: array.hpp:52
const_iterator end() const
Definition: array.hpp:511
const_iterator begin() const
Definition: array.hpp:503
IntegroIntegrand(const ext::shared_ptr< LinearInterpolation > &i, const FdmBoundaryConditionSet &bcSet, Real x, Real delta, Real nu)
Definition: fdmbatesop.cpp:63
GaussHermiteIntegration gaussHermiteIntegration_
Definition: fdmbatesop.hpp:76
std::vector< SparseMatrix > toMatrixDecomp() const override
Definition: fdmbatesop.cpp:122
const FdmBoundaryConditionSet bcSet_
Definition: fdmbatesop.hpp:79
Array integro(const Array &r) const
Definition: fdmbatesop.cpp:89
const ext::shared_ptr< FdmMesher > mesher_
Definition: fdmbatesop.hpp:78
FdmBatesOp(const ext::shared_ptr< FdmMesher > &mesher, const ext::shared_ptr< BatesProcess > &batesProcess, FdmBoundaryConditionSet bcSet, Size integroIntegrationOrder, const ext::shared_ptr< FdmQuantoHelper > &quantoHelper=ext::shared_ptr< FdmQuantoHelper >())
Definition: fdmbatesop.cpp:37
Shared handle to an observable.
Definition: handle.hpp:41
Square-root stochastic-volatility Heston process.
Matrix used in linear algebra.
Definition: matrix.hpp:41
purely virtual base class for market observables
Definition: quote.hpp:37
market element returning a stored value
Definition: simplequote.hpp:33
Interest-rate term structure.
Term structure with an added spread on the zero yield rate.
@ NoFrequency
null frequency
Definition: frequency.hpp:37
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
OperatorTraits< FdmLinearOp >::bc_set FdmBoundaryConditionSet
STL namespace.