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
26 StochasticProcessArray::StochasticProcessArray(
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");
38 registerWith(h: process);
39 }
40 }
41
42 Size StochasticProcessArray::size() const {
43 return processes_.size();
44 }
45
46 Array StochasticProcessArray::initialValues() const {
47 Array tmp(size());
48 for (Size i=0; i<size(); ++i)
49 tmp[i] = processes_[i]->x0();
50 return tmp;
51 }
52
53 Array StochasticProcessArray::drift(Time t,
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: x[i]);
58 return tmp;
59 }
60
61 Matrix StochasticProcessArray::diffusion(Time t,
62 const Array& x) const {
63 Matrix tmp = sqrtCorrelation_;
64 for (Size i=0; i<size(); ++i) {
65 Real sigma = processes_[i]->diffusion(t, x: x[i]);
66 std::transform(first: tmp.row_begin(i), last: tmp.row_end(i),
67 result: tmp.row_begin(i),
68 unary_op: [=](Real x) -> Real { return x * sigma; });
69 }
70 return tmp;
71 }
72
73 Array StochasticProcessArray::expectation(Time t0,
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: x0[i], dt);
79 return tmp;
80 }
81
82 Matrix StochasticProcessArray::stdDeviation(Time t0,
83 const Array& x0,
84 Time dt) const {
85 Matrix tmp = sqrtCorrelation_;
86 for (Size i=0; i<size(); ++i) {
87 Real sigma = processes_[i]->stdDeviation(t0, x0: x0[i], dt);
88 std::transform(first: tmp.row_begin(i), last: tmp.row_end(i),
89 result: tmp.row_begin(i),
90 unary_op: [=](Real x) -> Real { return x * sigma; });
91 }
92 return tmp;
93 }
94
95 Matrix StochasticProcessArray::covariance(Time t0,
96 const Array& x0,
97 Time dt) const {
98 Matrix tmp = stdDeviation(t0, x0, dt);
99 return tmp*transpose(m: tmp);
100 }
101
102 Array StochasticProcessArray::evolve(
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: x0[i], dt, dw: dz[i]);
109 return tmp;
110 }
111
112 Array StochasticProcessArray::apply(const Array& x0,
113 const Array& dx) const {
114 Array tmp(size());
115 for (Size i=0; i<size(); ++i)
116 tmp[i] = processes_[i]->apply(x0: x0[i],dx: dx[i]);
117 return tmp;
118 }
119
120 Time StochasticProcessArray::time(const Date& d) const {
121 return processes_[0]->time(d);
122 }
123
124 const ext::shared_ptr<StochasticProcess1D>&
125 StochasticProcessArray::process(Size i) const {
126 return processes_[i];
127 }
128
129 Matrix StochasticProcessArray::correlation() const {
130 return sqrtCorrelation_ * transpose(m: sqrtCorrelation_);
131 }
132
133}
134

source code of quantlib/ql/processes/stochasticprocessarray.cpp

Morty Proxy This is a proxified and sanitized view of the page, visit original site.