Skip to content
LeetQuant
F6 Linear Algebra and Optimisation

F6 · Linear Algebra and Optimisation · Lesson 3 of 5

Matrix Decompositions: LU, Cholesky, SVD

What LU, Cholesky and the SVD are, when each exists and what it costs, and how they solve systems, simulate correlated normals, compress matrices and measure conditioning.

20 min

Factor once, use many times

A decomposition rewrites a matrix as a product of matrices that are easy to work with: triangular (solve by substitution), orthogonal (inverse is the transpose) or diagonal (everything is elementwise). You pay the factorisation cost once and then reuse it: for many right-hand sides, for determinants, for simulation, for diagnosing how trustworthy an answer is.

The objective of this lesson: for each of LU, Cholesky and SVD, know what it is, when it exists, roughly what it costs for an n×nn \times n matrix, and which quant problems it solves.

DecompositionFormExists forCost (flops)Typical use
LUPA=LUPA = LUany square AA (with row pivoting PP)23n3\tfrac23 n^3solving general Ax=bAx = b, determinants
CholeskyA=LL⊤A = LL^\topsymmetric positive definite AA13n3\tfrac13 n^3covariance matrices: solving, simulating, testing PD
SVDA=UΣV⊤A = U\Sigma V^\topevery matrix, any shapeseveral times Choleskyrank, low-rank approximation, conditioning, least squares

After any of these, solving with a new right-hand side costs only O(n2)O(n^2): a forward and a back substitution.

LU: Gaussian elimination, recorded

Gaussian elimination turns AA into an upper-triangular UU by subtracting multiples of rows. Store those multipliers in a unit lower-triangular LL and you have A=LUA = LU. To solve Ax=bAx = b:

  1. Forward-substitute Ly=bLy = b (top to bottom).
  2. Back-substitute Ux=yUx = y (bottom to top).

Existence. Without row swaps, A=LUA = LU (with LL unit lower-triangular) exists and is unique for invertible AA exactly when every leading principal minor is non-zero. (0110)\begin{pmatrix}0 & 1 \\ 1 & 0\end{pmatrix} fails at the first pivot. With partial pivoting (swap the largest available entry into the pivot position) PA=LUPA = LU always exists, and pivoting is also what keeps elimination numerically stable.

Determinant for free: det⁡A=±∏iUii\det A = \pm\prod_i U_{ii}, the sign coming from the number of row swaps.

Do not invert. Computing A−1A^{-1} costs about 2n32n^3 flops (three times an LU) and is less accurate; A−1bA^{-1}b in a formula means "solve", not "invert".

Worked example

LU and two triangular solves

Factor A=(211433879)A = \begin{pmatrix} 2 & 1 & 1 \\ 4 & 3 & 3 \\ 8 & 7 & 9 \end{pmatrix} as LULU, find det⁡A\det A, and solve Ax=(3,7,19)⊤Ax = (3, 7, 19)^\top.

Show solution

Eliminate column 1. Multipliers ℓ21=4/2=2\ell_{21} = 4/2 = 2 and ℓ31=8/2=4\ell_{31} = 8/2 = 4. Row 2 − 2×-\,2\times row 1 gives (0,1,1)(0, 1, 1); row 3 − 4×-\,4\times row 1 gives (0,3,5)(0, 3, 5).

Eliminate column 2. Multiplier ℓ32=3/1=3\ell_{32} = 3/1 = 3. (0,3,5)−3(0,1,1)=(0,0,2)(0, 3, 5) - 3(0, 1, 1) = (0, 0, 2).

L=(100210431),U=(211011002),det⁡A=2⋅1⋅2=4.L = \begin{pmatrix} 1 & 0 & 0 \\ 2 & 1 & 0 \\ 4 & 3 & 1 \end{pmatrix}, \qquad U = \begin{pmatrix} 2 & 1 & 1 \\ 0 & 1 & 1 \\ 0 & 0 & 2 \end{pmatrix}, \qquad \det A = 2 \cdot 1 \cdot 2 = 4 .

Forward solve Ly=bLy = b: y1=3y_1 = 3, y2=7−2(3)=1y_2 = 7 - 2(3) = 1, y3=19−4(3)−3(1)=4y_3 = 19 - 4(3) - 3(1) = 4.

Back solve Ux=yUx = y: x3=4/2=2x_3 = 4/2 = 2, x2=1−x3=−1x_2 = 1 - x_3 = -1, x1=(3−x2−x3)/2=(3+1−2)/2=1x_1 = (3 - x_2 - x_3)/2 = (3 + 1 - 2)/2 = 1.

So x=(1,−1,2)x = (1, -1, 2). Check row 3: 8−7+18=198 - 7 + 18 = 19.

Cholesky: the square root of a covariance matrix

A symmetric positive definite (PD) matrix factors as A=LL⊤A = LL^\top with LL lower-triangular and positive diagonal, uniquely. The entries come column by column:

Ljj=Ajj−∑k<jLjk2,Lij=Aij−∑k<jLikLjkLjj(i>j).L_{jj} = \sqrt{A_{jj} - \textstyle\sum_{k<j} L_{jk}^2}, \qquad L_{ij} = \frac{A_{ij} - \sum_{k<j} L_{ik}L_{jk}}{L_{jj}} \quad (i > j).

It exploits symmetry, so it costs half an LU and needs no pivoting. Three uses come up constantly:

  • Solving Σx=b\Sigma x = b (normal equations, GLS, mean–variance weights Σ−1μ\Sigma^{-1}\mu): two triangular solves.
  • Simulating X∼N(μ,Σ)X \sim N(\mu, \Sigma): draw independent standard normals ZZ and set X=μ+LZX = \mu + LZ, since Cov⁡(LZ)=LL⊤=Σ\operatorname{Cov}(LZ) = LL^\top = \Sigma. Because LL is triangular, X1X_1 depends on Z1Z_1 only, X2X_2 on Z1,Z2Z_1, Z_2, and so on.
  • Testing positive definiteness: the algorithm needs Ajj−∑kLjk2>0A_{jj} - \sum_k L_{jk}^2 > 0 at every step. If a pivot is zero or negative, the matrix is not PD. This is the cheapest reliable test, much cheaper than computing eigenvalues.

A PSD but singular matrix (say, a covariance matrix of assets where one is a combination of the others) hits a zero pivot; a factor still exists but is not unique. Practical code either adds a tiny ridge εI\varepsilon I or uses an eigendecomposition and clips negative eigenvalues.

Worked example

Cholesky of a 3×3 covariance matrix

Factor Σ=(422253236)\Sigma = \begin{pmatrix} 4 & 2 & 2 \\ 2 & 5 & 3 \\ 2 & 3 & 6 \end{pmatrix} and write down how to simulate X∼N(0,Σ)X \sim N(0, \Sigma) from independent standard normals.

Show solution

Column 1: L11=4=2L_{11} = \sqrt4 = 2, L21=2/2=1L_{21} = 2/2 = 1, L31=2/2=1L_{31} = 2/2 = 1.

Column 2: L22=5−12=2L_{22} = \sqrt{5 - 1^2} = 2, L32=(3−1⋅1)/2=1L_{32} = (3 - 1 \cdot 1)/2 = 1.

Column 3: L33=6−12−12=2L_{33} = \sqrt{6 - 1^2 - 1^2} = 2.

L=(200120112),X1=2Z1,X2=Z1+2Z2,X3=Z1+Z2+2Z3.L = \begin{pmatrix} 2 & 0 & 0 \\ 1 & 2 & 0 \\ 1 & 1 & 2 \end{pmatrix}, \qquad X_1 = 2Z_1, \quad X_2 = Z_1 + 2Z_2, \quad X_3 = Z_1 + Z_2 + 2Z_3 .

Check two entries: Var⁡(X2)=1+4=5\operatorname{Var}(X_2) = 1 + 4 = 5 and Cov⁡(X2,X3)=1⋅1+2⋅1=3\operatorname{Cov}(X_2, X_3) = 1 \cdot 1 + 2 \cdot 1 = 3. Every pivot was positive, so Σ\Sigma is PD, and det⁡Σ=(2⋅2⋅2)2=64\det\Sigma = (2 \cdot 2 \cdot 2)^2 = 64.

The same thing in code (standard library only), with a Monte Carlo check of the covariance and a failed factorisation of an impossible correlation matrix:

import math
import random


def cholesky(S):
    n = len(S)
    L = [[0.0] * n for _ in range(n)]
    for j in range(n):
        d = S[j][j] - sum(L[j][k] ** 2 for k in range(j))
        if d <= 0:
            raise ValueError("matrix is not positive definite")
        L[j][j] = math.sqrt(d)
        for i in range(j + 1, n):
            L[i][j] = (S[i][j] - sum(L[i][k] * L[j][k] for k in range(j))) / L[j][j]
    return L


S = [[4, 2, 2], [2, 5, 3], [2, 3, 6]]
L = cholesky(S)
print(L)

random.seed(0)
N = 200_000
draws = []
for _ in range(N):
    z = [random.gauss(0, 1) for _ in range(3)]
    draws.append([sum(L[i][k] * z[k] for k in range(3)) for i in range(3)])
cov = [[sum(x[i] * x[j] for x in draws) / N for j in range(3)] for i in range(3)]
print([[round(c, 2) for c in row] for row in cov])

try:
    cholesky([[1, 0.9, 0.9], [0.9, 1, 0.0], [0.9, 0.0, 1]])
except ValueError as e:
    print(e)

Output:

[[2.0, 0.0, 0.0], [1.0, 2.0, 0.0], [1.0, 1.0, 2.0]]
[[3.99, 2.01, 2.0], [2.01, 5.03, 3.01], [2.0, 3.01, 6.0]]
matrix is not positive definite

The last matrix says asset 1 has correlation 0.9 with assets 2 and 3, yet 2 and 3 are uncorrelated. Its eigenvalues are 11 and 1±0.921 \pm 0.9\sqrt2, and 1−0.92≈−0.27<01 - 0.9\sqrt2 \approx -0.27 < 0, so no such assets exist.

SVD: every matrix is rotate, stretch, rotate

Every real m×nm \times n matrix factors as

A=UΣV⊤=∑i=1rσi uivi⊤,σ1≥σ2≥⋯≥σr>0,A = U\Sigma V^\top = \sum_{i=1}^{r} \sigma_i\,u_i v_i^\top, \qquad \sigma_1 \ge \sigma_2 \ge \dots \ge \sigma_r > 0 ,

with UU and VV orthogonal, Σ\Sigma diagonal and r=rank⁡Ar = \operatorname{rank} A. Geometrically: rotate by V⊤V^\top, stretch the axes by σi\sigma_i, rotate by UU. The singular values are the square roots of the eigenvalues of A⊤AA^\top A, the viv_i are its eigenvectors, and ui=Avi/σiu_i = Av_i/\sigma_i. For a symmetric PSD matrix (a covariance matrix) the SVD is the eigendecomposition; for a symmetric matrix in general, σi=∣λi∣\sigma_i = |\lambda_i|.

Low-rank approximation (Eckart–Young). Truncating the sum after kk terms gives the best rank-kk approximation AkA_k, in both the spectral and the Frobenius norm:

∥A−Ak∥2=σk+1,∥A−Ak∥F=σk+12+⋯+σr2.\|A - A_k\|_2 = \sigma_{k+1}, \qquad \|A - A_k\|_F = \sqrt{\sigma_{k+1}^2 + \dots + \sigma_r^2} .

This is PCA: the SVD of a centred returns matrix gives the principal components, and keeping the top few gives a factor model plus residual.

Condition number. κ(A)=σmax⁡/σmin⁡\kappa(A) = \sigma_{\max}/\sigma_{\min} (for a covariance matrix, λmax⁡/λmin⁡\lambda_{\max}/\lambda_{\min}) bounds how much relative error in bb can be amplified in the solution of Ax=bAx = b:

∥Δx∥∥x∥≤κ(A) ∥Δb∥∥b∥.\frac{\|\Delta x\|}{\|x\|} \le \kappa(A)\,\frac{\|\Delta b\|}{\|b\|} .

Rule of thumb: you lose about log⁡10κ\log_{10}\kappa significant digits. Two consequences for regression: forming X⊤XX^\top X squares the condition number, κ(X⊤X)=κ(X)2\kappa(X^\top X) = \kappa(X)^2, which is why solvers use QR or the SVD of XX rather than the normal equations; and near-collinear predictors (tiny σmin⁡\sigma_{\min}) make coefficients unstable even when the fit is good.

Worked example

an SVD by hand and its rank-1 truncation

Find the singular values of A=(3045)A = \begin{pmatrix} 3 & 0 \\ 4 & 5 \end{pmatrix}, its condition number, and its best rank-1 approximation.

Show solution

A⊤A=(25202025)A^\top A = \begin{pmatrix} 25 & 20 \\ 20 & 25 \end{pmatrix} has eigenvalues 4545 and 55 (trace 50, determinant 225), with eigenvectors v1=(1,1)/2v_1 = (1, 1)/\sqrt2 and v2=(1,−1)/2v_2 = (1, -1)/\sqrt2. So

σ1=45=35≈6.71,σ2=5≈2.24,κ=σ1/σ2=3.\sigma_1 = \sqrt{45} = 3\sqrt5 \approx 6.71, \qquad \sigma_2 = \sqrt5 \approx 2.24, \qquad \kappa = \sigma_1/\sigma_2 = 3 .

Check: σ1σ2=15=∣det⁡A∣\sigma_1\sigma_2 = 15 = |\det A|.

u1=Av1/σ1=(3,9)/235=(1,3)10u_1 = Av_1/\sigma_1 = \dfrac{(3, 9)/\sqrt2}{3\sqrt5} = \dfrac{(1, 3)}{\sqrt{10}}. The rank-1 approximation is

A1=σ1u1v1⊤=3520(1133)=(1.51.54.54.5).A_1 = \sigma_1 u_1 v_1^\top = \frac{3\sqrt5}{\sqrt{20}}\begin{pmatrix} 1 & 1 \\ 3 & 3 \end{pmatrix} = \begin{pmatrix} 1.5 & 1.5 \\ 4.5 & 4.5 \end{pmatrix} .

The residual is A−A1=(1.5−1.5−0.50.5)A - A_1 = \begin{pmatrix} 1.5 & -1.5 \\ -0.5 & 0.5 \end{pmatrix}, with Frobenius norm 2.25+2.25+0.25+0.25=5=σ2\sqrt{2.25 + 2.25 + 0.25 + 0.25} = \sqrt5 = \sigma_2, as Eckart–Young promises.

Worked example

what a condition number of 199 looks like

Two assets have correlation 0.990.99. Solve (10.990.991)x=b\begin{pmatrix} 1 & 0.99 \\ 0.99 & 1 \end{pmatrix}x = b for b=(1.99,1.99)b = (1.99, 1.99) and for the slightly different b′=(1.99,1.98)b' = (1.99, 1.98).

Show solution

The eigenvalues are 1±0.991 \pm 0.99, i.e. 1.991.99 and 0.010.01, so κ=1.99/0.01=199\kappa = 1.99/0.01 = 199.

For bb, x=(1,1)x = (1, 1) by inspection. For b′b', solving the 2×22 \times 2 system gives x′=(298/199, 99/199)≈(1.497, 0.497)x' = (298/199,\ 99/199) \approx (1.497,\ 0.497).

The right-hand side moved by about 0.36%0.36\% in relative terms (0.01/(1.992)0.01 / (1.99\sqrt2)), while the solution moved by 50%50\% (∥x′−x∥/∥x∥≈0.707/1.414\|x' - x\| / \|x\| \approx 0.707/1.414): an amplification of about 140, inside the bound of 199. The perturbation pointed along the eigenvector (1,−1)(1, -1) with the tiny eigenvalue, which is the worst direction. This is exactly what happens to mean–variance weights Σ−1μ\Sigma^{-1}\mu when two assets are near-duplicates: a tiny change in the estimated return spread flips large long–short positions.

Knowledge check

Which of these symmetric matrices has no Cholesky factorisation?

Knowledge check

What is the condition number of a 2×2 correlation matrix with correlation 0.9?

Knowledge check

A matrix has singular values 10, 5, 2 and 1. What is the Frobenius-norm error of its best rank-2 approximation?

Key takeaways

  • LU (23n3\tfrac23 n^3) solves general square systems; with pivoting it always exists. Reuse it for every right-hand side and never form A−1A^{-1} just to solve.
  • Cholesky (13n3\tfrac13 n^3) exists exactly for symmetric PD matrices. It solves covariance systems, simulates N(μ,Σ)N(\mu, \Sigma) via μ+LZ\mu + LZ, and is the cheapest PD test.
  • The SVD exists for every matrix. Truncating it gives the best rank-kk approximation, with error σk+1\sigma_{k+1} (spectral) or ∑i>kσi2\sqrt{\sum_{i>k}\sigma_i^2} (Frobenius).
  • κ=σmax⁡/σmin⁡\kappa = \sigma_{\max}/\sigma_{\min} measures error amplification. Highly correlated assets or predictors mean a large κ\kappa and unstable solutions, and the normal equations square it.

Practice problems

Finished reading?

Answer the 3 knowledge checks to complete this lesson (0 done).