QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.38
Loading...
Searching...
No Matches
fdmwienerop.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) 2024 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
20
21/*! \file fdmwienerop.cpp
22*/
23
29
30#include <boost/numeric/ublas/matrix.hpp>
31
32namespace QuantLib {
33
35 const ext::shared_ptr<FdmMesher>& mesher,
36 ext::shared_ptr<YieldTermStructure> rTS,
37 const Array& lambdas)
38 : rTS_(std::move(rTS)) {
39
40 QL_REQUIRE(mesher->layout()->dim().size() == lambdas.size(),
41 "mesher and lambdas need to be of the same dimension");
42
43 for (Size i=0; i < lambdas.size(); ++i)
44 ops_.emplace_back(ext::make_shared<TripleBandLinearOp>(
45 SecondDerivativeOp(i, mesher)
46 .mult(Array(mesher->layout()->size(), 0.5*lambdas[i])))
47 );
48 }
49
51 return ops_.size();
52 }
53
55 if (rTS_ != nullptr)
56 r_ = rTS_->forwardRate(t1, t2, Continuous).rate();
57 }
58
59 Array FdmWienerOp::apply(const Array& x) const {
60 Array y(-r_*x);
61 for (const auto& op: ops_)
62 y += op->apply(x);
63
64 return y;
65 }
66
68 return Array(x.size(), 0.0);
69 }
70
71 Array FdmWienerOp::apply_direction(Size direction, const Array& x) const {
72 return ops_[direction]->apply(x);
73 }
74
76 Size direction, const Array& x, Real s) const {
77
78 return ops_[direction]->solve_splitting(x, s, 1.0);
79 }
80
82 return solve_splitting(0, r, dt);
83 }
84
85 std::vector<SparseMatrix> FdmWienerOp::toMatrixDecomp() const {
86 std::vector<SparseMatrix> retVal;
87
88 retVal.reserve(ops_.size());
89 for (const auto& op: ops_)
90 retVal.push_back(op->toMatrix());
91
92 return retVal;
93 }
94}
1-D array used in linear algebra.
Definition: array.hpp:52
Size size() const
dimension of the array
Definition: array.hpp:483
Array solve_splitting(Size direction, const Array &x, Real s) const override
Definition: fdmwienerop.cpp:75
FdmWienerOp(const ext::shared_ptr< FdmMesher > &mesher, ext::shared_ptr< YieldTermStructure > rTS, const Array &lambdas)
Definition: fdmwienerop.cpp:34
Size size() const override
Definition: fdmwienerop.cpp:50
Array preconditioner(const Array &r, Real s) const override
Definition: fdmwienerop.cpp:81
std::vector< SparseMatrix > toMatrixDecomp() const override
Definition: fdmwienerop.cpp:85
Array apply_mixed(const Array &x) const override
Definition: fdmwienerop.cpp:67
void setTime(Time t1, Time t2) override
Time is required.
Definition: fdmwienerop.cpp:54
std::vector< ext::shared_ptr< TripleBandLinearOp > > ops_
Definition: fdmwienerop.hpp:56
Array apply(const Array &x) const override
Definition: fdmwienerop.cpp:59
Array apply_direction(Size direction, const Array &x) const override
Definition: fdmwienerop.cpp:71
const ext::shared_ptr< YieldTermStructure > rTS_
Definition: fdmwienerop.hpp:55
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
const ext::shared_ptr< YieldTermStructure > rTS_
memory layout of a fdm linear operator
mesher for a fdm grid
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:37
STL namespace.
ext::shared_ptr< YieldTermStructure > r
second derivative operator
Interest-rate term structure.