Cholesky Decomposition
A positive number has a square root. A symmetric positive definite matrix has one too: the Cholesky factor , a lower-triangular matrix with . It is the fastest way to solve SPD systems, to compute of a covariance matrix, and to draw correlated Gaussian samples, which is why Gaussian processes, Kalman filters and variational autoencoders rely on it.
The theorem
Every symmetric positive definite can be written uniquely as
For a matrix, comparing entries of with column by column gives explicit formulas:
If a number under a square root is , the matrix was not positive definite: attempting a Cholesky factorization is a practical positive-definiteness test.
import numpy as np
def cholesky(A):
n = len(A); L = np.zeros_like(A)
for j in range(n):
d = A[j, j] - L[j, :j] @ L[j, :j]
if d <= 0:
raise ValueError("not positive definite")
L[j, j] = np.sqrt(d)
for i in range(j + 1, n):
L[i, j] = (A[i, j] - L[i, :j] @ L[j, :j]) / L[j, j]
return L
A = np.array([[4.0, 2.0, -2.0], [2.0, 10.0, 2.0], [-2.0, 2.0, 6.0]])
L = cholesky(A)
print(L)
print("L L^T = A:", np.allclose(L @ L.T, A), " matches numpy:", np.allclose(L, np.linalg.cholesky(A)))
try:
cholesky(np.array([[1.0, 2.0], [2.0, 1.0]]))
except ValueError as e:
print("[[1,2],[2,1]]:", e)[[ 2. 0. 0.]
[ 1. 3. 0.]
[-1. 1. 2.]]
L L^T = A: True matches numpy: True
[[1,2],[2,1]]: not positive definite
Three uses in ML
1. Solving . Solve by forward substitution, then by back substitution. It costs about half of an LU solve.
2. The log-determinant. Because ,
which never overflows, unlike forming for a large covariance matrix.
3. Sampling correlated Gaussians. If and , then has covariance . This is also the reparameterization trick used to train VAEs.
import numpy as np
Sigma = np.array([[2.0, 1.2], [1.2, 1.0]])
mu = np.array([1.0, -1.0])
L = np.linalg.cholesky(Sigma)
rng = np.random.default_rng(0)
Z = rng.normal(size=(100000, 2))
X = mu + Z @ L.T # each row: mu + L z
print("sample mean:", np.round(X.mean(axis=0), 2))
print("sample covariance:\n", np.round(np.cov(X.T), 2))
print("log det via Cholesky:", round(2 * np.log(np.diag(L)).sum(), 6),
" via slogdet:", round(np.linalg.slogdet(Sigma)[1], 6))sample mean: [ 1. -1.]
sample covariance:
[[2.01 1.2 ]
[1.2 1. ]]
log det via Cholesky: -0.579818 via slogdet: -0.579818
In software
np.linalg.cholesky,scipy.linalg.cho_factor/cho_solve; Gaussian process libraries add a small "jitter" to the kernel matrix so the factorization does not fail from rounding.numpy.random.Generator.multivariate_normalcan use a Cholesky factor internally (method="cholesky").- In Pyodide's NumPy,
np.linalg.choleskymay not raise for a non-positive-definite matrix; check the eigenvalues when in doubt.
Exercises
Exercise 1: A factor
Find the Cholesky factor of .
Answer
.
Solution
, , . Check: .
Exercise 2: Determinant from the factor
Using the factor from Exercise 1, what is ?
Answer
Solution
. Directly: .
Exercise 3: Solve with two triangular solves (code)
Solve for the matrix of Exercise 1 using forward and back substitution.
Answer
.
Solution
gives , . Then : , , so .
import numpy as np
L = np.array([[3.0, 0.0], [2.0, 1.0]])
b = np.array([12.0, 9.0])
z = np.array([b[0] / L[0, 0], 0.0]); z[1] = (b[1] - L[1, 0] * z[0]) / L[1, 1]
x = np.array([0.0, z[1] / L[1, 1]]); x[0] = (z[0] - L[1, 0] * x[1]) / L[0, 0]
print(z, np.round(x, 6), np.allclose(L @ L.T @ x, b))[4. 1.] [0.666667 1. ] True
Test yourself
Which matrices have a Cholesky decomposition with positive diagonal?
- All square matrices
- Symmetric positive definite matrices
- Orthogonal matrices
- Triangular matrices
If and , then has covariance…
from the Cholesky factor is…