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 matrix, and which quant problems it solves.
| Decomposition | Form | Exists for | Cost (flops) | Typical use |
|---|---|---|---|---|
| LU | any square (with row pivoting ) | solving general , determinants | ||
| Cholesky | symmetric positive definite | covariance matrices: solving, simulating, testing PD | ||
| SVD | every matrix, any shape | several times Cholesky | rank, low-rank approximation, conditioning, least squares |
After any of these, solving with a new right-hand side costs only : a forward and a back substitution.
LU: Gaussian elimination, recorded
Gaussian elimination turns into an upper-triangular by subtracting multiples of rows. Store those multipliers in a unit lower-triangular and you have . To solve :
- Forward-substitute (top to bottom).
- Back-substitute (bottom to top).
Existence. Without row swaps, (with unit lower-triangular) exists and is unique for invertible exactly when every leading principal minor is non-zero. fails at the first pivot. With partial pivoting (swap the largest available entry into the pivot position) always exists, and pivoting is also what keeps elimination numerically stable.
Determinant for free: , the sign coming from the number of row swaps.
Do not invert. Computing costs about flops (three times an LU) and is less accurate; in a formula means "solve", not "invert".
Worked example
LU and two triangular solves
Factor as , find , and solve .
Show solutionHide solution
Eliminate column 1. Multipliers and . Row 2 row 1 gives ; row 3 row 1 gives .
Eliminate column 2. Multiplier . .
Forward solve : , , .
Back solve : , , .
So . Check row 3: .
Cholesky: the square root of a covariance matrix
A symmetric positive definite (PD) matrix factors as with lower-triangular and positive diagonal, uniquely. The entries come column by column:
It exploits symmetry, so it costs half an LU and needs no pivoting. Three uses come up constantly:
- Solving (normal equations, GLS, mean–variance weights ): two triangular solves.
- Simulating : draw independent standard normals and set , since . Because is triangular, depends on only, on , and so on.
- Testing positive definiteness: the algorithm needs 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 or uses an eigendecomposition and clips negative eigenvalues.
Worked example
Cholesky of a 3×3 covariance matrix
Factor and write down how to simulate from independent standard normals.
Show solutionHide solution
Column 1: , , .
Column 2: , .
Column 3: .
Check two entries: and . Every pivot was positive, so is PD, and .
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 and , and , so no such assets exist.
SVD: every matrix is rotate, stretch, rotate
Every real matrix factors as
with and orthogonal, diagonal and . Geometrically: rotate by , stretch the axes by , rotate by . The singular values are the square roots of the eigenvalues of , the are its eigenvectors, and . For a symmetric PSD matrix (a covariance matrix) the SVD is the eigendecomposition; for a symmetric matrix in general, .
Low-rank approximation (Eckart–Young). Truncating the sum after terms gives the best rank- approximation , in both the spectral and the Frobenius norm:
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. (for a covariance matrix, ) bounds how much relative error in can be amplified in the solution of :
Rule of thumb: you lose about significant digits. Two consequences for regression: forming squares the condition number, , which is why solvers use QR or the SVD of rather than the normal equations; and near-collinear predictors (tiny ) 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 , its condition number, and its best rank-1 approximation.
Show solutionHide solution
has eigenvalues and (trace 50, determinant 225), with eigenvectors and . So
Check: .
. The rank-1 approximation is
The residual is , with Frobenius norm , as Eckart–Young promises.
Worked example
what a condition number of 199 looks like
Two assets have correlation . Solve for and for the slightly different .
Show solutionHide solution
The eigenvalues are , i.e. and , so .
For , by inspection. For , solving the system gives .
The right-hand side moved by about in relative terms (), while the solution moved by (): an amplification of about 140, inside the bound of 199. The perturbation pointed along the eigenvector with the tiny eigenvalue, which is the worst direction. This is exactly what happens to mean–variance weights 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 () solves general square systems; with pivoting it always exists. Reuse it for every right-hand side and never form just to solve.
- Cholesky () exists exactly for symmetric PD matrices. It solves covariance systems, simulates via , and is the cheapest PD test.
- The SVD exists for every matrix. Truncating it gives the best rank- approximation, with error (spectral) or (Frobenius).
- measures error amplification. Highly correlated assets or predictors mean a large and unstable solutions, and the normal equations square it.
Practice problems
Finished reading?
Answer the 3 knowledge checks to complete this lesson (0 done).