.cholesky()
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.
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.
Syntax
numpy.linalg.cholesky(a, upper=False)
Parameters:
a
: array_like of shape (M, M). The input matrix must be Hermitian (symmetric if all elements are real) and positive-definite.upper
: bool, optional. IfTrue
, the function returns the upper-triangular Cholesky factor. IfFalse
(default), it returns the lower-triangular Cholesky factor.
Return value:
L
: array_like of shape (M, M). The lower or upper-triangular Cholesky factor ofa
. Returns a matrix object ifa
is a matrix object.
Raises:
LinAlgError
: If the decomposition fails, typically when the input matrix is not positive-definite.
Example 1: Basic Cholesky Decomposition
This example demonstrates how to perform a basic Cholesky decomposition on a simple symmetric positive-definite matrix:
import numpy as np# Create a symmetric positive-definite matrixA = np.array([[4, 2], [2, 5]])print("Original matrix:")print(A)# Perform Cholesky decompositionL = np.linalg.cholesky(A)print("\nCholesky factor (lower triangular):")print(L)# Verify the decomposition: L @ L.T should equal the original matrixprint("\nVerification (L @ L.T):")print(L @ L.T)# Check if the reconstruction matches the original matrixprint("\nIs the reconstruction equal to the original matrix?")print(np.allclose(L @ L.T, A))
This example results in the following output:
Original matrix:[[4 2][2 5]]Cholesky factor (lower triangular):[[2. 0.][1. 2.]]Verification (L @ L.T):[[4. 2.][2. 5.]]Is the reconstruction equal to the original matrix?True
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.
Example 2: Positive-Definite Check Using Eigenvalues
This example demonstrates how to verify if a matrix is positive-definite using its eigenvalues and how np.linalg.cholesky()
behaves accordingly:
import numpy as np# A symmetric, positive-definite matrixA = np.array([[6, 2], [2, 3]])print("Matrix A:\n", A)# Check eigenvalueseigenvalues = np.linalg.eigvals(A)print("\nEigenvalues of A:", eigenvalues)print("Is A positive-definite?", np.all(eigenvalues > 0))# Attempt Cholesky decompositiontry:L = np.linalg.cholesky(A)print("\nCholesky factor L:\n", L)except np.linalg.LinAlgError:print("\nCholesky decomposition failed.")# A symmetric matrix that is not positive-definiteB = np.array([[1, 2], [2, -3]])print("\nMatrix B:\n", B)# Check eigenvalueseigenvalues_B = np.linalg.eigvals(B)print("\nEigenvalues of B:", eigenvalues_B)print("Is B positive-definite?", np.all(eigenvalues_B > 0))# Attempt Cholesky decompositiontry:L_B = np.linalg.cholesky(B)print("\nCholesky factor of B:\n", L_B)except np.linalg.LinAlgError:print("\nCholesky decomposition failed for B: Matrix is not positive-definite.")
This example results in the following output:
Matrix A:[[6 2][2 3]]Eigenvalues of A: [7. 2.]Is A positive-definite? TrueCholesky factor L:[[2.44948974 0. ][0.81649658 1.52752523]]Matrix B:[[ 1 2][ 2 -3]]Eigenvalues of B: [ 1.82842712 -3.82842712]Is B positive-definite? FalseCholesky decomposition failed for B: Matrix is not positive-definite.
Codebyte Example: Error When Matrix is Not Square
This example demonstrates how the Cholesky decomposition fails when the input matrix is not square:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:NumPy on Codecademy
- Career path
Data Scientist: Machine Learning Specialist
Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.Includes 27 CoursesWith Professional CertificationBeginner Friendly95 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours