QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.38
Loading...
Searching...
No Matches
choleskydecomposition.cpp
Go to the documentation of this file.
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 <http://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
24
25namespace QuantLib {
26
27 Matrix CholeskyDecomposition(const Matrix& S, bool flexible) {
28 Size i, j, size = S.rows();
29
30 QL_REQUIRE(size == S.columns(),
31 "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 Matrix result(size, size, 0.0);
40 Real sum;
41 for (i=0; i<size; i++) {
42 for (j=i; j<size; j++) {
43 sum = S[i][j];
44 for (Integer k=0; k<=Integer(i)-1; k++) {
45 sum -= result[i][k]*result[j][k];
46 }
47 if (i == j) {
48 QL_REQUIRE(flexible || sum > 0.0,
49 "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 result[i][i] = std::sqrt(std::max<Real>(sum, 0.0));
53 } 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 result[j][i] = close_enough(result[i][i], 0.0)
58 ? 0.0
59 : Real(sum / result[i][i]);
60 }
61 }
62 }
63 return result;
64 }
65
66 Array CholeskySolveFor(const Matrix& L, const Array& b) {
67 const Size n = b.size();
68
69 QL_REQUIRE(L.columns() == n && L.rows() == n,
70 "Size of input matrix and vector does not match.");
71
72 Array x(n);
73 for (Size i=0; i < n; ++i) {
74 x[i] = -std::inner_product(L.row_begin(i), L.row_begin(i)+i, x.begin(), Real(-b[i]));
75 x[i] /= L[i][i];
76 }
77
78 for (Integer i=n-1; i >=0; --i) {
79 x[i] = -std::inner_product(
80 L.column_begin(i)+i+1, L.column_end(i), x.begin()+i+1, Real(-x[i]));
81 x[i] /= L[i][i];
82 }
83
84 return x;
85 }
86}
Cholesky decomposition.
1-D array used in linear algebra.
Definition: array.hpp:52
const_iterator begin() const
Definition: array.hpp:491
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
Size columns() const
Definition: matrix.hpp:508
const_column_iterator column_begin(Size i) const
Definition: matrix.hpp:415
const_column_iterator column_end(Size i) const
Definition: matrix.hpp:434
floating-point comparisons
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
std::function< Real(Real)> b
QL_REAL Real
real number
Definition: types.hpp:50
QL_INTEGER Integer
integer number
Definition: types.hpp:35
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:37
Array CholeskySolveFor(const Matrix &L, const Array &b)
Matrix CholeskyDecomposition(const Matrix &S, bool flexible)
bool close_enough(const Quantity &m1, const Quantity &m2, Size n)
Definition: quantity.cpp:182