Coverage Report

Created: 2025-08-05 06:45

/src/quantlib/ql/processes/stochasticprocessarray.cpp
Line
Count
Source (jump to first uncovered line)
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
24
namespace QuantLib {
25
26
    StochasticProcessArray::StochasticProcessArray(
27
        const std::vector<ext::shared_ptr<StochasticProcess1D> >& processes,
28
        const Matrix& correlation)
29
0
    : processes_(processes),
30
0
      sqrtCorrelation_(pseudoSqrt(correlation,SalvagingAlgorithm::Spectral)) {
31
32
0
        QL_REQUIRE(!processes.empty(), "no processes given");
33
0
        QL_REQUIRE(correlation.rows() == processes.size(),
34
0
                   "mismatch between number of processes "
35
0
                   "and size of correlation matrix");
36
0
        for (auto& process : processes_) {
37
0
            QL_REQUIRE(process, "null 1-D stochastic process");
38
0
            registerWith(process);
39
0
        }
40
0
    }
41
42
0
    Size StochasticProcessArray::size() const {
43
0
        return processes_.size();
44
0
    }
45
46
0
    Array StochasticProcessArray::initialValues() const {
47
0
        Array tmp(size());
48
0
        for (Size i=0; i<size(); ++i)
49
0
            tmp[i] = processes_[i]->x0();
50
0
        return tmp;
51
0
    }
52
53
    Array StochasticProcessArray::drift(Time t,
54
0
                                        const Array& x) const {
55
0
        Array tmp(size());
56
0
        for (Size i=0; i<size(); ++i)
57
0
            tmp[i] = processes_[i]->drift(t, x[i]);
58
0
        return tmp;
59
0
    }
60
61
    Matrix StochasticProcessArray::diffusion(Time t,
62
0
                                             const Array& x) const {
63
0
        Matrix tmp = sqrtCorrelation_;
64
0
        for (Size i=0; i<size(); ++i) {
65
0
            Real sigma = processes_[i]->diffusion(t, x[i]);
66
0
            std::transform(tmp.row_begin(i), tmp.row_end(i),
67
0
                           tmp.row_begin(i),
68
0
                           [=](Real x) -> Real { return x * sigma; });
69
0
        }
70
0
        return tmp;
71
0
    }
72
73
    Array StochasticProcessArray::expectation(Time t0,
74
                                              const Array& x0,
75
0
                                              Time dt) const {
76
0
        Array tmp(size());
77
0
        for (Size i=0; i<size(); ++i)
78
0
            tmp[i] = processes_[i]->expectation(t0, x0[i], dt);
79
0
        return tmp;
80
0
    }
81
82
    Matrix StochasticProcessArray::stdDeviation(Time t0,
83
                                                const Array& x0,
84
0
                                                Time dt) const {
85
0
        Matrix tmp = sqrtCorrelation_;
86
0
        for (Size i=0; i<size(); ++i) {
87
0
            Real sigma = processes_[i]->stdDeviation(t0, x0[i], dt);
88
0
            std::transform(tmp.row_begin(i), tmp.row_end(i),
89
0
                           tmp.row_begin(i),
90
0
                           [=](Real x) -> Real { return x * sigma; });
91
0
        }
92
0
        return tmp;
93
0
    }
94
95
    Matrix StochasticProcessArray::covariance(Time t0,
96
                                              const Array& x0,
97
0
                                              Time dt) const {
98
0
        Matrix tmp = stdDeviation(t0, x0, dt);
99
0
        return tmp*transpose(tmp);
100
0
    }
101
102
    Array StochasticProcessArray::evolve(
103
0
                  Time t0, const Array& x0, Time dt, const Array& dw) const {
104
0
        const Array dz = sqrtCorrelation_ * dw;
105
106
0
        Array tmp(size());
107
0
        for (Size i=0; i<size(); ++i)
108
0
            tmp[i] = processes_[i]->evolve(t0, x0[i], dt, dz[i]);
109
0
        return tmp;
110
0
    }
111
112
    Array StochasticProcessArray::apply(const Array& x0,
113
0
                                        const Array& dx) const {
114
0
        Array tmp(size());
115
0
        for (Size i=0; i<size(); ++i)
116
0
            tmp[i] = processes_[i]->apply(x0[i],dx[i]);
117
0
        return tmp;
118
0
    }
119
120
0
    Time StochasticProcessArray::time(const Date& d) const {
121
0
        return processes_[0]->time(d);
122
0
    }
123
124
    const ext::shared_ptr<StochasticProcess1D>&
125
0
    StochasticProcessArray::process(Size i) const {
126
0
        return processes_[i];
127
0
    }
128
129
0
    Matrix StochasticProcessArray::correlation() const {
130
0
        return sqrtCorrelation_ * transpose(sqrtCorrelation_);
131
0
    }
132
133
}