TExcelTAD
Home › Math for Machine Learning › Cholesky Decomposition

Cholesky Decomposition

A positive number has a square root. A symmetric positive definite matrix has one too: the Cholesky factor LL, a lower-triangular matrix with A=LLTA = LL^T. It is the fastest way to solve SPD systems, to compute log⁡det⁡\log\det 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 A∈Rn×nA \in \mathbb{R}^{n \times n} can be written uniquely as

A=LLT,L lower triangular with positive diagonalA = LL^T, \qquad L \text{ lower triangular with positive diagonal}

For a 3×33 \times 3 matrix, comparing entries of LLTLL^T with AA column by column gives explicit formulas:

l11=a11,l21=a21/l11,l31=a31/l11l22=a22−l212,l32=(a32−l31l21)/l22,l33=a33−l312−l322\begin{aligned} l_{11} &= \sqrt{a_{11}}, & l_{21} &= a_{21}/l_{11}, & l_{31} &= a_{31}/l_{11} \\ l_{22} &= \sqrt{a_{22} - l_{21}^2}, & l_{32} &= (a_{32} - l_{31}l_{21})/l_{22}, & l_{33} &= \sqrt{a_{33} - l_{31}^2 - l_{32}^2} \end{aligned}

If a number under a square root is ≤0\le 0, the matrix was not positive definite: attempting a Cholesky factorization is a practical positive-definiteness test.

python · try it yourself
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 Ax=bA\mathbf{x} = \mathbf{b}. Solve Lz=bL\mathbf{z} = \mathbf{b} by forward substitution, then LTx=zL^T\mathbf{x} = \mathbf{z} by back substitution. It costs about half of an LU solve.

2. The log-determinant. Because det⁡A=det⁡L det⁡LT=∏ilii2\det A = \det L\,\det L^T = \prod_i l_{ii}^2,

log⁡det⁡A=2∑i=1nlog⁡lii\log\det A = 2\sum_{i=1}^{n} \log l_{ii}

which never overflows, unlike forming det⁡A\det A for a large covariance matrix.

3. Sampling correlated Gaussians. If z∼N(0,I)\mathbf{z} \sim \mathcal{N}(\mathbf{0}, I) and Σ=LLT\Sigma = LL^T, then x=μ+Lz\mathbf{x} = \boldsymbol{\mu} + L\mathbf{z} has covariance L I LT=ΣL\,I\,L^T = \Sigma. This is also the reparameterization trick used to train VAEs.

python · try it yourself
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" ϵI\epsilon I to the kernel matrix so the factorization does not fail from rounding.
  • numpy.random.Generator.multivariate_normal can use a Cholesky factor internally (method="cholesky").
  • In Pyodide's NumPy, np.linalg.cholesky may not raise for a non-positive-definite matrix; check the eigenvalues when in doubt.

Exercises

Exercise 1: A 2×22 \times 2 factor

Find the Cholesky factor of [9665]\begin{bmatrix} 9 & 6 \\ 6 & 5 \end{bmatrix}.

Answer

L=[3021]L = \begin{bmatrix} 3 & 0 \\ 2 & 1 \end{bmatrix}.

Solution

l11=9=3l_{11} = \sqrt{9} = 3, l21=6/3=2l_{21} = 6/3 = 2, l22=5−22=1l_{22} = \sqrt{5 - 2^2} = 1. Check: [3021][3201]=[9665]\begin{bmatrix} 3 & 0 \\ 2 & 1 \end{bmatrix}\begin{bmatrix} 3 & 2 \\ 0 & 1 \end{bmatrix} = \begin{bmatrix} 9 & 6 \\ 6 & 5 \end{bmatrix}.

Exercise 2: Determinant from the factor

Using the factor from Exercise 1, what is det⁡A\det A?

Answer
Solution

det⁡A=(l11l22)2=(3⋅1)2=9\det A = (l_{11}l_{22})^2 = (3 \cdot 1)^2 = 9. Directly: 9⋅5−6⋅6=99 \cdot 5 - 6 \cdot 6 = 9.

Exercise 3: Solve with two triangular solves (code)

Solve Ax=(12,9)A\mathbf{x} = (12, 9) for the matrix of Exercise 1 using forward and back substitution.

Answer

x=(23,1)\mathbf{x} = (\frac{2}{3}, 1).

Solution

Lz=(12,9)L\mathbf{z} = (12, 9) gives z1=4z_1 = 4, z2=9−2⋅4=1z_2 = 9 - 2 \cdot 4 = 1. Then LTx=zL^T\mathbf{x} = \mathbf{z}: x2=1x_2 = 1, 3x1+2⋅1=43x_1 + 2 \cdot 1 = 4, so x1=23x_1 = \frac{2}{3}.

python · try it yourself
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 A=LLTA = LL^T with positive diagonal?

  1. All square matrices
  2. Symmetric positive definite matrices
  3. Orthogonal matrices
  4. Triangular matrices

If Σ=LLT\Sigma = LL^T and z∼N(0,I)\mathbf{z} \sim \mathcal{N}(\mathbf{0}, I), then LzL\mathbf{z} has covariance…

  1. II
  2. LL
  3. Σ\Sigma
  4. L2L^2

log⁡det⁡A\log\det A from the Cholesky factor is…

  1. ∑ilii\sum_i l_{ii}
  2. 2∑ilog⁡lii2\sum_i \log l_{ii}
  3. ∏ilii\prod_i l_{ii}
  4. log⁡tr⁡L\log \operatorname{tr} L