/src/quantlib/ql/math/matrixutilities/choleskydecomposition.cpp
Line | Count | Source |
1 | | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* |
4 | | Copyright (C) 2003, 2004 Ferdinando Ametrano |
5 | | Copyright (C) 2016 Peter Caspers |
6 | | Copyright (C) 2024 Klaus Spanderen |
7 | | |
8 | | This file is part of QuantLib, a free-software/open-source library |
9 | | for financial quantitative analysts and developers - http://quantlib.org/ |
10 | | |
11 | | QuantLib is free software: you can redistribute it and/or modify it |
12 | | under the terms of the QuantLib license. You should have received a |
13 | | copy of the license along with this program; if not, please email |
14 | | <quantlib-dev@lists.sf.net>. The license is also available online at |
15 | | <https://www.quantlib.org/license.shtml>. |
16 | | |
17 | | This program is distributed in the hope that it will be useful, but WITHOUT |
18 | | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
19 | | FOR A PARTICULAR PURPOSE. See the license for more details. |
20 | | */ |
21 | | |
22 | | #include <ql/math/matrixutilities/choleskydecomposition.hpp> |
23 | | #include <ql/math/comparison.hpp> |
24 | | |
25 | | namespace QuantLib { |
26 | | |
27 | 0 | Matrix CholeskyDecomposition(const Matrix& S, bool flexible) { |
28 | 0 | Size i, j, size = S.rows(); |
29 | |
|
30 | 0 | QL_REQUIRE(size == S.columns(), |
31 | 0 | "input matrix is not a square matrix"); |
32 | | #if defined(QL_EXTRA_SAFETY_CHECKS) |
33 | | for (i=0; i<S.rows(); i++) |
34 | | for (j=0; j<i; j++) |
35 | | QL_REQUIRE(S[i][j] == S[j][i], |
36 | | "input matrix is not symmetric"); |
37 | | #endif |
38 | | |
39 | 0 | Matrix result(size, size, 0.0); |
40 | 0 | Real sum; |
41 | 0 | for (i=0; i<size; i++) { |
42 | 0 | for (j=i; j<size; j++) { |
43 | 0 | sum = S[i][j]; |
44 | 0 | for (Integer k=0; k<=Integer(i)-1; k++) { |
45 | 0 | sum -= result[i][k]*result[j][k]; |
46 | 0 | } |
47 | 0 | if (i == j) { |
48 | 0 | QL_REQUIRE(flexible || sum > 0.0, |
49 | 0 | "input matrix is not positive definite"); |
50 | | // To handle positive semi-definite matrices take the |
51 | | // square root of sum if positive, else zero. |
52 | 0 | result[i][i] = std::sqrt(std::max<Real>(sum, 0.0)); |
53 | 0 | } else { |
54 | | // With positive semi-definite matrices is possible |
55 | | // to have result[i][i]==0.0 |
56 | | // In this case sum happens to be zero as well |
57 | 0 | result[j][i] = close_enough(result[i][i], 0.0) |
58 | 0 | ? 0.0 |
59 | 0 | : Real(sum / result[i][i]); |
60 | 0 | } |
61 | 0 | } |
62 | 0 | } |
63 | 0 | return result; |
64 | 0 | } |
65 | | |
66 | 0 | Array CholeskySolveFor(const Matrix& L, const Array& b) { |
67 | 0 | const Size n = b.size(); |
68 | |
|
69 | 0 | QL_REQUIRE(L.columns() == n && L.rows() == n, |
70 | 0 | "Size of input matrix and vector does not match."); |
71 | | |
72 | 0 | Array x(n); |
73 | 0 | for (Size i=0; i < n; ++i) { |
74 | 0 | x[i] = -std::inner_product(L.row_begin(i), L.row_begin(i)+i, x.begin(), Real(-b[i])); |
75 | 0 | x[i] /= L[i][i]; |
76 | 0 | } |
77 | |
|
78 | 0 | for (Integer i=n-1; i >=0; --i) { |
79 | 0 | x[i] = -std::inner_product( |
80 | 0 | L.column_begin(i)+i+1, L.column_end(i), x.begin()+i+1, Real(-x[i])); |
81 | 0 | x[i] /= L[i][i]; |
82 | 0 | } |
83 | |
|
84 | 0 | return x; |
85 | 0 | } |
86 | | } |