QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
stochasticprocessarray.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2005 Klaus Spanderen
5 Copyright (C) 2005 StatPro Italia srl
6
7 This file is part of QuantLib, a free-software/open-source library
8 for financial quantitative analysts and developers - http://quantlib.org/
9
10 QuantLib is free software: you can redistribute it and/or modify it
11 under the terms of the QuantLib license. You should have received a
12 copy of the license along with this program; if not, please email
13 <quantlib-dev@lists.sf.net>. The license is also available online at
14 <http://quantlib.org/license.shtml>.
15
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the license for more details.
19*/
20
21#include <ql/processes/stochasticprocessarray.hpp>
22#include <ql/math/matrixutilities/pseudosqrt.hpp>
23
24namespace QuantLib {
25
27 const std::vector<ext::shared_ptr<StochasticProcess1D> >& processes,
28 const Matrix& correlation)
29 : processes_(processes),
30 sqrtCorrelation_(pseudoSqrt(correlation,SalvagingAlgorithm::Spectral)) {
31
32 QL_REQUIRE(!processes.empty(), "no processes given");
33 QL_REQUIRE(correlation.rows() == processes.size(),
34 "mismatch between number of processes "
35 "and size of correlation matrix");
36 for (auto& process : processes_) {
37 QL_REQUIRE(process, "null 1-D stochastic process");
39 }
40 }
41
43 return processes_.size();
44 }
45
47 Array tmp(size());
48 for (Size i=0; i<size(); ++i)
49 tmp[i] = processes_[i]->x0();
50 return tmp;
51 }
52
54 const Array& x) const {
55 Array tmp(size());
56 for (Size i=0; i<size(); ++i)
57 tmp[i] = processes_[i]->drift(t, x[i]);
58 return tmp;
59 }
60
62 const Array& x) const {
64 for (Size i=0; i<size(); ++i) {
65 Real sigma = processes_[i]->diffusion(t, x[i]);
66 std::transform(tmp.row_begin(i), tmp.row_end(i),
67 tmp.row_begin(i),
68 [=](Real x) -> Real { return x * sigma; });
69 }
70 return tmp;
71 }
72
74 const Array& x0,
75 Time dt) const {
76 Array tmp(size());
77 for (Size i=0; i<size(); ++i)
78 tmp[i] = processes_[i]->expectation(t0, x0[i], dt);
79 return tmp;
80 }
81
83 const Array& x0,
84 Time dt) const {
86 for (Size i=0; i<size(); ++i) {
87 Real sigma = processes_[i]->stdDeviation(t0, x0[i], dt);
88 std::transform(tmp.row_begin(i), tmp.row_end(i),
89 tmp.row_begin(i),
90 [=](Real x) -> Real { return x * sigma; });
91 }
92 return tmp;
93 }
94
96 const Array& x0,
97 Time dt) const {
98 Matrix tmp = stdDeviation(t0, x0, dt);
99 return tmp*transpose(tmp);
100 }
101
103 Time t0, const Array& x0, Time dt, const Array& dw) const {
104 const Array dz = sqrtCorrelation_ * dw;
105
106 Array tmp(size());
107 for (Size i=0; i<size(); ++i)
108 tmp[i] = processes_[i]->evolve(t0, x0[i], dt, dz[i]);
109 return tmp;
110 }
111
113 const Array& dx) const {
114 Array tmp(size());
115 for (Size i=0; i<size(); ++i)
116 tmp[i] = processes_[i]->apply(x0[i],dx[i]);
117 return tmp;
118 }
119
121 return processes_[0]->time(d);
122 }
123
124 const ext::shared_ptr<StochasticProcess1D>&
126 return processes_[i];
127 }
128
131 }
132
133}
1-D array used in linear algebra.
Definition: array.hpp:52
Concrete date class.
Definition: date.hpp:125
Matrix used in linear algebra.
Definition: matrix.hpp:41
const_row_iterator row_begin(Size i) const
Definition: matrix.hpp:360
Size rows() const
Definition: matrix.hpp:504
const_row_iterator row_end(Size i) const
Definition: matrix.hpp:378
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:228
Array drift(Time t, const Array &x) const override
returns the drift part of the equation, i.e.,
Size size() const override
returns the number of dimensions of the stochastic process
Array evolve(Time t0, const Array &x0, Time dt, const Array &dw) const override
const ext::shared_ptr< StochasticProcess1D > & process(Size i) const
Matrix diffusion(Time t, const Array &x) const override
returns the diffusion part of the equation, i.e.
Time time(const Date &) const override
StochasticProcessArray(const std::vector< ext::shared_ptr< StochasticProcess1D > > &, const Matrix &correlation)
Matrix stdDeviation(Time t0, const Array &x0, Time dt) const override
Matrix covariance(Time t0, const Array &x0, Time dt) const override
Array expectation(Time t0, const Array &x0, Time dt) const override
Array initialValues() const override
returns the initial values of the state variables
Array apply(const Array &x0, const Array &dx) const override
std::vector< ext::shared_ptr< StochasticProcess1D > > processes_
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:35
Matrix pseudoSqrt(const Matrix &matrix, SalvagingAlgorithm::Type sa)
Definition: pseudosqrt.cpp:347
Matrix transpose(const Matrix &m)
Definition: matrix.hpp:700
algorithm used for matricial pseudo square root
Definition: pseudosqrt.hpp:32