|
| 1 | +--- |
| 2 | +Title: '.cholesky()' |
| 3 | +Description: 'Returns the Cholesky decomposition of a matrix.' |
| 4 | +Subjects: |
| 5 | + - 'Computer Science' |
| 6 | + - 'Data Science' |
| 7 | +Tags: |
| 8 | + - 'Linear Algebra' |
| 9 | + - 'Matrices' |
| 10 | + - 'NumPy' |
| 11 | + - 'Python' |
| 12 | +CatalogContent: |
| 13 | + - 'learn-python-3' |
| 14 | + - 'paths/data-science' |
| 15 | +--- |
| 16 | + |
| 17 | +The **Cholesky decomposition** is a matrix factorization technique that decomposes a Hermitian, positive-definite matrix into the product of a lower triangular matrix and its conjugate transpose. In NumPy's linear algebra module, the **`.cholesky()**` function implements this decomposition, returning either the lower or upper triangular Cholesky factor of a given matrix. |
| 18 | + |
| 19 | +Cholesky decomposition is widely used in numerical analysis for efficient solutions of linear systems, Monte Carlo simulations, and matrix inversion operations. It's particularly valuable in applications like least squares problems, Kalman filters, and multivariate normal distributions in statistics and machine learning due to its numerical stability and computational efficiency compared to other matrix decomposition methods. |
| 20 | + |
| 21 | +## Syntax |
| 22 | + |
| 23 | +```pseudo |
| 24 | +numpy.linalg.cholesky(a, upper=False) |
| 25 | +``` |
| 26 | + |
| 27 | +**Parameters:** |
| 28 | + |
| 29 | +- `a`: array_like of shape (M, M). The input matrix must be Hermitian (symmetric if all elements are real) and positive-definite. |
| 30 | +- `upper`: bool, optional. If `True`, the function returns the upper-triangular Cholesky factor. If `False` (default), it returns the lower-triangular Cholesky factor. |
| 31 | + |
| 32 | +**Return value:** |
| 33 | + |
| 34 | +- `L`: array_like of shape (M, M). The lower or upper-triangular Cholesky factor of `a`. Returns a matrix object if `a` is a matrix object. |
| 35 | + |
| 36 | +**Raises:** |
| 37 | + |
| 38 | +- `LinAlgError`: If the decomposition fails, typically when the input matrix is not positive-definite. |
| 39 | + |
| 40 | +## Example 1: Basic Cholesky Decomposition |
| 41 | + |
| 42 | +This example demonstrates how to perform a basic Cholesky decomposition on a simple symmetric positive-definite matrix: |
| 43 | + |
| 44 | +```py |
| 45 | +import numpy as np |
| 46 | + |
| 47 | +# Create a symmetric positive-definite matrix |
| 48 | +A = np.array([[4, 2], [2, 5]]) |
| 49 | +print("Original matrix:") |
| 50 | +print(A) |
| 51 | + |
| 52 | +# Perform Cholesky decomposition |
| 53 | +L = np.linalg.cholesky(A) |
| 54 | +print("\nCholesky factor (lower triangular):") |
| 55 | +print(L) |
| 56 | + |
| 57 | +# Verify the decomposition: L @ L.T should equal the original matrix |
| 58 | +print("\nVerification (L @ L.T):") |
| 59 | +print(L @ L.T) |
| 60 | + |
| 61 | +# Check if the reconstruction matches the original matrix |
| 62 | +print("\nIs the reconstruction equal to the original matrix?") |
| 63 | +print(np.allclose(L @ L.T, A)) |
| 64 | +``` |
| 65 | + |
| 66 | +This example results in the following output: |
| 67 | + |
| 68 | +```shell |
| 69 | +Original matrix: |
| 70 | +[[4 2] |
| 71 | + [2 5]] |
| 72 | + |
| 73 | +Cholesky factor (lower triangular): |
| 74 | +[[2. 0.] |
| 75 | + [1. 2.]] |
| 76 | + |
| 77 | +Verification (L @ L.T): |
| 78 | +[[4. 2.] |
| 79 | + [2. 5.]] |
| 80 | + |
| 81 | +Is the reconstruction equal to the original matrix? |
| 82 | +True |
| 83 | +``` |
| 84 | + |
| 85 | +The code first creates a _2×2_ symmetric positive-definite matrix, then applies the Cholesky decomposition to obtain the lower triangular factor `L`. It then verifies that L multiplied by its transpose equals the original matrix. |
| 86 | + |
| 87 | +## Example 2: Positive-Definite Check Using Eigenvalues |
| 88 | + |
| 89 | +This example demonstrates how to verify if a matrix is positive-definite using its eigenvalues and how `np.linalg.cholesky()` behaves accordingly: |
| 90 | + |
| 91 | +```py |
| 92 | +import numpy as np |
| 93 | + |
| 94 | +# A symmetric, positive-definite matrix |
| 95 | +A = np.array([[6, 2], [2, 3]]) |
| 96 | +print("Matrix A:\n", A) |
| 97 | + |
| 98 | +# Check eigenvalues |
| 99 | +eigenvalues = np.linalg.eigvals(A) |
| 100 | +print("\nEigenvalues of A:", eigenvalues) |
| 101 | +print("Is A positive-definite?", np.all(eigenvalues > 0)) |
| 102 | + |
| 103 | +# Attempt Cholesky decomposition |
| 104 | +try: |
| 105 | + L = np.linalg.cholesky(A) |
| 106 | + print("\nCholesky factor L:\n", L) |
| 107 | +except np.linalg.LinAlgError: |
| 108 | + print("\nCholesky decomposition failed.") |
| 109 | + |
| 110 | +# A symmetric matrix that is not positive-definite |
| 111 | +B = np.array([[1, 2], [2, -3]]) |
| 112 | +print("\nMatrix B:\n", B) |
| 113 | + |
| 114 | +# Check eigenvalues |
| 115 | +eigenvalues_B = np.linalg.eigvals(B) |
| 116 | +print("\nEigenvalues of B:", eigenvalues_B) |
| 117 | +print("Is B positive-definite?", np.all(eigenvalues_B > 0)) |
| 118 | + |
| 119 | +# Attempt Cholesky decomposition |
| 120 | +try: |
| 121 | + L_B = np.linalg.cholesky(B) |
| 122 | + print("\nCholesky factor of B:\n", L_B) |
| 123 | +except np.linalg.LinAlgError: |
| 124 | + print("\nCholesky decomposition failed for B: Matrix is not positive-definite.") |
| 125 | +``` |
| 126 | + |
| 127 | +This example results in the following output: |
| 128 | + |
| 129 | +```shell |
| 130 | +Matrix A: |
| 131 | + [[6 2] |
| 132 | + [2 3]] |
| 133 | + |
| 134 | +Eigenvalues of A: [7. 2.] |
| 135 | +Is A positive-definite? True |
| 136 | + |
| 137 | +Cholesky factor L: |
| 138 | + [[2.44948974 0. ] |
| 139 | + [0.81649658 1.52752523]] |
| 140 | + |
| 141 | +Matrix B: |
| 142 | + [[ 1 2] |
| 143 | + [ 2 -3]] |
| 144 | + |
| 145 | +Eigenvalues of B: [ 1.82842712 -3.82842712] |
| 146 | +Is B positive-definite? False |
| 147 | + |
| 148 | +Cholesky decomposition failed for B: Matrix is not positive-definite. |
| 149 | +``` |
| 150 | + |
| 151 | +## Codebyte Example: Error When Matrix is Not Square |
| 152 | + |
| 153 | +This example demonstrates how the Cholesky decomposition fails when the input matrix is not square: |
| 154 | + |
| 155 | +```codebyte/python |
| 156 | +import numpy as np |
| 157 | +
|
| 158 | +# A non-square matrix |
| 159 | +C = np.array([[1, 2, 3], [4, 5, 6]]) |
| 160 | +print("Matrix C (non-square):\n", C) |
| 161 | +
|
| 162 | +try: |
| 163 | + # Attempt Cholesky decomposition |
| 164 | + L = np.linalg.cholesky(C) |
| 165 | + print("\nCholesky factor L:\n", L) |
| 166 | +except np.linalg.LinAlgError: |
| 167 | + print("\nCholesky decomposition failed: Matrix is not square or not positive-definite.") |
| 168 | +``` |
0 commit comments