QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
fdmornsteinuhlenbeckop.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2016 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
23#include <ql/math/functional.hpp>
24#include <ql/methods/finitedifferences/meshers/fdmmesher.hpp>
25#include <ql/methods/finitedifferences/operators/fdmlinearoplayout.hpp>
26#include <ql/methods/finitedifferences/operators/fdmornsteinuhlenbeckop.hpp>
27#include <ql/methods/finitedifferences/operators/secondderivativeop.hpp>
28#include <ql/processes/ornsteinuhlenbeckprocess.hpp>
29#include <ql/termstructures/yieldtermstructure.hpp>
30#include <utility>
31
32namespace QuantLib {
33
35 const ext::shared_ptr<FdmMesher>& mesher,
36 ext::shared_ptr<OrnsteinUhlenbeckProcess> process,
37 ext::shared_ptr<YieldTermStructure> rTS,
38 Size direction)
39 : mesher_(mesher), process_(std::move(process)), rTS_(std::move(rTS)), direction_(direction),
40 m_(direction, mesher), mapX_(direction, mesher) {
41
42 Array drift(mesher_->layout()->size());
43 const Array x(mesher_->locations(direction));
44
45 for (const auto& iter : *mesher_->layout()) {
46 const Size i = iter.index();
47 drift[i] = process_->drift(0.0, x[i]);
48 }
49
50 m_.axpyb(drift, FirstDerivativeOp(direction, mesher),
51 SecondDerivativeOp(direction, mesher)
52 .mult(0.5*squared(process_->volatility())
53 *Array(mesher->layout()->size(), 1.0)), Array());
54 }
55
57 return mesher_->layout()->dim().size();;
58 }
59
61 const Rate r = rTS_->forwardRate(t1, t2, Continuous).rate();
62
63 mapX_.axpyb(Array(), m_, m_, Array(1, -r));
64 }
65
67 return mapX_.apply(r);
68 }
69
71 return Array(r.size(), 0.0);
72 }
73
75 if (direction == direction_) {
76 return mapX_.apply(r);
77 }
78 else {
79 return Array(r.size(), 0.0);
80 }
81 }
82
84 if (direction == direction_) {
85 return mapX_.solve_splitting(r, a, 1.0);
86 }
87 else {
88 return r;
89 }
90 }
91
93 return solve_splitting(direction_, r, dt);
94 }
95
96 std::vector<SparseMatrix> FdmOrnsteinUhlenbeckOp::toMatrixDecomp() const {
97 return std::vector<SparseMatrix>(1, mapX_.toMatrix());
98 }
99
100}
1-D array used in linear algebra.
Definition: array.hpp:52
Array apply_direction(Size direction, const Array &r) const override
Array preconditioner(const Array &r, Real s) const override
FdmOrnsteinUhlenbeckOp(const ext::shared_ptr< FdmMesher > &mesher, ext::shared_ptr< OrnsteinUhlenbeckProcess > p, ext::shared_ptr< YieldTermStructure > rTS, Size direction=0)
std::vector< SparseMatrix > toMatrixDecomp() const override
void setTime(Time t1, Time t2) override
Time is required.
Array apply_mixed(const Array &r) const override
const ext::shared_ptr< FdmMesher > mesher_
Array solve_splitting(Size direction, const Array &r, Real s) const override
const ext::shared_ptr< YieldTermStructure > rTS_
Array apply(const Array &r) const override
const ext::shared_ptr< OrnsteinUhlenbeckProcess > process_
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
T squared(T x)
Definition: functional.hpp:37
STL namespace.