QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
fdmsabrop.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2018 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/methods/finitedifferences/meshers/fdmmesher.hpp>
25#include <ql/methods/finitedifferences/operators/fdmlinearoplayout.hpp>
26#include <ql/methods/finitedifferences/operators/fdmsabrop.hpp>
27#include <ql/methods/finitedifferences/operators/firstderivativeop.hpp>
28#include <ql/methods/finitedifferences/operators/secondderivativeop.hpp>
29#include <ql/methods/finitedifferences/operators/secondordermixedderivativeop.hpp>
30#include <ql/termstructures/yieldtermstructure.hpp>
31#include <utility>
32
33namespace QuantLib {
34 FdmSabrOp::FdmSabrOp(const ext::shared_ptr<FdmMesher>& mesher,
35 ext::shared_ptr<YieldTermStructure> rTS,
36 Real f0,
37 Real alpha,
38 Real beta,
39 Real nu,
40 Real rho)
41 : rTS_(std::move(rTS)),
42 dffMap_(SecondDerivativeOp(0, mesher).mult(0.5 * Exp(2.0 * mesher->locations(1)) *
43 Pow(mesher->locations(0), 2.0 * beta))),
44 dxMap_(FirstDerivativeOp(1, mesher).mult(Array(mesher->layout()->size(), -0.5 * nu * nu))),
45 dxxMap_(SecondDerivativeOp(1, mesher).mult(Array(mesher->layout()->size(), 0.5 * nu * nu))),
46 correlationMap_(
48 .mult(rho * nu * Exp(mesher->locations(1)) * Pow(mesher->locations(0), beta))),
49 mapF_(0, mesher), mapA_(1, mesher) {}
50
52 const Rate r = rTS_->forwardRate(t1, t2, Continuous).rate();
53
54 mapF_.axpyb(Array(), dffMap_, dffMap_, Array(1, -0.5*r));
55 mapA_.axpyb(Array(1, 1.0), dxMap_, dxxMap_, Array(1, -0.5*r));
56 }
57
59 return 2;
60 }
61
62 Array FdmSabrOp::apply(const Array& u) const {
63 return mapF_.apply(u) + mapA_.apply(u) + correlationMap_.apply(u);
64 }
65
67 return correlationMap_.apply(r);
68 }
69
71 Size direction, const Array& r) const {
72 if (direction == 0)
73 return mapF_.apply(r);
74 else if (direction == 1)
75 return mapA_.apply(r);
76 else
77 QL_FAIL("direction too large");
78 }
79
81 Size direction, const Array& r, Real a) const {
82
83 if (direction == 0) {
84 return mapF_.solve_splitting(r, a, 1.0);
85 }
86 else if (direction == 1) {
87 return mapA_.solve_splitting(r, a, 1.0);
88 }
89 else
90 QL_FAIL("direction too large");
91 }
92
94 const Array& r, Real dt) const {
95
96 return solve_splitting(1, solve_splitting(0, r, dt), dt) ;
97 }
98
99 std::vector<SparseMatrix> FdmSabrOp::toMatrixDecomp() const {
100 return {
101 mapA_.toMatrix(),
102 mapF_.toMatrix(),
104 };
105 }
106
107}
1-D array used in linear algebra.
Definition: array.hpp:52
Size size() const override
Definition: fdmsabrop.cpp:58
FdmSabrOp(const ext::shared_ptr< FdmMesher > &mesher, ext::shared_ptr< YieldTermStructure > rTS, Real f0, Real alpha, Real beta, Real nu, Real rho)
Definition: fdmsabrop.cpp:34
const TripleBandLinearOp dxMap_
Definition: fdmsabrop.hpp:70
Array apply_direction(Size direction, const Array &r) const override
Definition: fdmsabrop.cpp:70
Array preconditioner(const Array &r, Real s) const override
Definition: fdmsabrop.cpp:93
std::vector< SparseMatrix > toMatrixDecomp() const override
Definition: fdmsabrop.cpp:99
const TripleBandLinearOp dxxMap_
Definition: fdmsabrop.hpp:70
void setTime(Time t1, Time t2) override
Time is required.
Definition: fdmsabrop.cpp:51
Array apply_mixed(const Array &r) const override
Definition: fdmsabrop.cpp:66
const TripleBandLinearOp dffMap_
Definition: fdmsabrop.hpp:69
Array solve_splitting(Size direction, const Array &r, Real s) const override
Definition: fdmsabrop.cpp:80
const ext::shared_ptr< YieldTermStructure > rTS_
Definition: fdmsabrop.hpp:67
Array apply(const Array &r) const override
Definition: fdmsabrop.cpp:62
TripleBandLinearOp mapF_
Definition: fdmsabrop.hpp:73
TripleBandLinearOp mapA_
Definition: fdmsabrop.hpp:73
const NinePointLinearOp correlationMap_
Definition: fdmsabrop.hpp:71
SparseMatrix toMatrix() const override
Array apply(const Array &r) const override
SparseMatrix toMatrix() const override
Array solve_splitting(const Array &r, Real a, Real b=1.0) const
void axpyb(const Array &a, const TripleBandLinearOp &x, const TripleBandLinearOp &y, const Array &b)
Array apply(const Array &r) const override
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
Real Rate
interest rates
Definition: types.hpp:70
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
Array Pow(const Array &v, Real alpha)
Definition: array.hpp:889
Array Exp(const Array &v)
Definition: array.hpp:875
STL namespace.