SigmaField
Mathematical Foundations for Quant Finance
0 of 256 lessons
      1. 3.7.1 Quadratic forms and positive definitenessConcept · 35 min
      2. 3.7.2 The Cholesky factorisationConcept · 30 min
      3. 3.7.3 Practice set: positive definitenessPractice set · 20 min
      4. 3.7.4 Case study: valid correlation matrices and correlated simulationCase study · 35 min

Level 3, Linear algebra 3.7 Positive definiteness and Cholesky Lesson 2 of 4

ConceptLesson 3.7.2 in Level 3, Linear algebra. About 30 minutes.

The Cholesky factorisation

A square root of a matrix

Every positive number aa has a square root: a=a⋅aa = \sqrt{a}\cdot\sqrt{a}. A positive definite matrix has a matrix version of this, built from a lower triangular matrix: one with only zeros above the diagonal.

Take

A=(4225),L=(2012)L⊤=(2102)\begin{gathered} A = \begin{pmatrix} 4 & 2 \\ 2 & 5 \end{pmatrix}, \quad L = \begin{pmatrix} 2 & 0 \\ 1 & 2 \end{pmatrix} \\[6pt] L^\top = \begin{pmatrix} 2 & 1 \\ 0 & 2 \end{pmatrix} \end{gathered}

Multiply LL by its transpose, row by column (lesson 3.2.1):

entry of LL⊤LL^\toprow of LL · column of L⊤L^\topvalue
(1,1)(1, 1)(2,0)⋅(2,0)(2, 0) \cdot (2, 0)4
(1,2)(1, 2) and (2,1)(2, 1)(2,0)⋅(1,2)(2, 0) \cdot (1, 2)2
(2,2)(2, 2)(1,2)⋅(1,2)(1, 2) \cdot (1, 2)5

So LL⊤=ALL^\top = A. The two off-diagonal entries are equal because LL⊤LL^\top is always symmetric: (LL⊤)⊤=LL⊤(LL^\top)^\top = LL^\top by the rule of lesson 3.2.3.

Rule: The Cholesky factorisation

A=LL⊤L lower triangular, lii>0\begin{gathered} A = LL^\top \\[4pt] L \text{ lower triangular, } l_{ii} > 0 \end{gathered}

A symmetric matrix AA has such an LL exactly when it is positive definite, and then LL is unique. LL is the Cholesky factor of AA, named after André-Louis Cholesky.

It is the completed square. Lesson 3.7.1 wrote a quadratic form as a sum of squares. With A=LL⊤A = LL^\top,

x⊤A x=(L⊤x)⊤(L⊤x)=∥L⊤x∥2\begin{aligned} \mathbf{x}^\top A\,\mathbf{x} &= (L^\top\mathbf{x})^\top(L^\top\mathbf{x}) \\ &= \lVert L^\top\mathbf{x} \rVert^2 \end{aligned}

Here L⊤x=(2x1+x2, 2x2)L^\top\mathbf{x} = (2x_1 + x_2,\ 2x_2), so 4x12+4x1x2+5x22=(2x1+x2)2+(2x2)24x_1^2 + 4x_1x_2 + 5x_2^2 = (2x_1 + x_2)^2 + (2x_2)^2. That is lesson 3.7.1's a(x1+bax2)2+ac−b2ax22=4(x1+12x2)2+4x22a\big(x_1 + \frac{b}{a}x_2\big)^2 + \frac{ac - b^2}{a}x_2^2 = 4\big(x_1 + \frac{1}{2}x_2\big)^2 + 4x_2^2, written with a matrix. At (1,−1)(1, -1) both sides give 5.

The order matters: L⊤L=(5224)L^\top L = \begin{pmatrix} 5 & 2 \\ 2 & 4 \end{pmatrix}, which is not AA.

Try it

The first two lines multiply LL⊤LL^\top and L⊤LL^\top L: only the first gives AA.

The last line evaluates the form at x=(1,−1)\mathbf{x} = (1, -1) twice: as x⊤Ax\mathbf{x}^\top A\mathbf{x}, and as the squared length of L⊤x=(1,−2)L^\top\mathbf{x} = (1, -2).

⌘/Ctrl + Enter runs the cell

The 2 × 2 formulas

Write L=(l110l21l22)L = \begin{pmatrix} l_{11} & 0 \\ l_{21} & l_{22} \end{pmatrix} and multiply out:

LL⊤=(l112l11l21l11l21l212+l222)LL^\top = \begin{pmatrix} l_{11}^2 & l_{11}l_{21} \\ l_{11}l_{21} & l_{21}^2 + l_{22}^2 \end{pmatrix}

Match this with A=(abbc)A = \begin{pmatrix} a & b \\ b & c \end{pmatrix}, one entry at a time: l112=al_{11}^2 = a first, then l11l21=bl_{11}l_{21} = b, then l212+l222=cl_{21}^2 + l_{22}^2 = c.

Rule: Cholesky for 2 × 2

l11=a,l21=bl11l22=c−l212\begin{gathered} l_{11} = \sqrt{a}, \quad l_{21} = \frac{b}{l_{11}} \\[4pt] l_{22} = \sqrt{c - l_{21}^2} \end{gathered}

Each step uses only what came before. The number under the last square root is c−b2a=ac−b2ac - \frac{b^2}{a} = \frac{ac - b^2}{a}, so both roots are real and positive exactly when a>0a > 0 and ac−b2>0ac - b^2 > 0: lesson 3.7.1's leading-minor test.

Three examples:

AAl11l_{11}l21l_{21}l22l_{22}
(4225)\begin{pmatrix} 4 & 2 \\ 2 & 5 \end{pmatrix}4=2\sqrt{4} = 22÷2=12 \div 2 = 15−1=2\sqrt{5 - 1} = 2
(10.60.61)\begin{pmatrix} 1 & 0.6 \\ 0.6 & 1 \end{pmatrix}10.61−0.36=0.8\sqrt{1 - 0.36} = 0.8
(3112)\begin{pmatrix} 3 & 1 \\ 1 & 2 \end{pmatrix}3≈1.7321\sqrt{3} \approx 1.732113≈0.5774\frac{1}{\sqrt{3}} \approx 0.57745/3≈1.291\sqrt{5/3} \approx 1.291

The second row has 1 on its diagonal and ρ=0.6\rho = 0.6 off it. For any such matrix, L=(10ρ1−ρ2)L = \begin{pmatrix} 1 & 0 \\ \rho & \sqrt{1 - \rho^2} \end{pmatrix}.

When it fails. For lesson 3.7.1's indefinite N=(1221)N = \begin{pmatrix} 1 & 2 \\ 2 & 1 \end{pmatrix}, l11=1l_{11} = 1 and l21=2l_{21} = 2, and l22l_{22} would be 1−4=−3\sqrt{1 - 4} = \sqrt{-3}: no real number. For the semidefinite J=(1111)J = \begin{pmatrix} 1 & 1 \\ 1 & 1 \end{pmatrix}, l22=1−1=0l_{22} = \sqrt{1 - 1} = 0, which is not positive, and LL has no inverse.

Try it

chol2 is the rule. For each example it prints LL to 4 places and checks LL⊤=ALL^\top = A.

The last line is what l22l_{22} would be the square root of for NN: −3-3.

⌘/Ctrl + Enter runs the cell

A 3 × 3 example by hand

For larger matrices, match LL⊤=ALL^\top = A one column of LL at a time, from left to right. Each diagonal entry is a square root, and each entry below it a division.

Rule: Cholesky, column j

ljj=ajj−∑k<jljk2lij=aij−∑k<jlik ljkljjfor each i>j\begin{gathered} l_{jj} = \sqrt{a_{jj} - \textstyle\sum_{k<j} l_{jk}^2} \\[6pt] l_{ij} = \dfrac{a_{ij} - \sum_{k<j} l_{ik}\,l_{jk}}{l_{jj}} \\[4pt] \text{for each } i > j \end{gathered}

The sums run over the columns already found, to the left of column jj; for j=1j = 1 they are empty. If a number under a square root is 0 or negative, AA is not positive definite and the method stops.

Factor

A=(42−22102−226)A = \begin{pmatrix} 4 & 2 & -2 \\ 2 & 10 & 2 \\ -2 & 2 & 6 \end{pmatrix}
stepentryworkingvalue
1l11l_{11}4\sqrt{4}2
2l21l_{21}2÷22 \div 21
3l31l_{31}−2÷2-2 \div 2−1-1
4l22l_{22}10−12\sqrt{10 - 1^2}3
5l32l_{32}(2−(−1)(1))÷3(2 - (-1)(1)) \div 31
6l33l_{33}6−(−1)2−12\sqrt{6 - (-1)^2 - 1^2}2
L=(200130−112)L = \begin{pmatrix} 2 & 0 & 0 \\ 1 & 3 & 0 \\ -1 & 1 & 2 \end{pmatrix}

Check one entry: row 3 of LL dotted with row 2 of LL is (−1)(1)+(1)(3)+(2)(0)=2=a32(-1)(1) + (1)(3) + (2)(0) = 2 = a_{32}.

The link with elimination. Eliminate on AA as in lesson 3.3.1, with no row swaps: R2−12R1R_2 - \frac{1}{2}R_1, R3+12R1R_3 + \frac{1}{2}R_1, then R3−13R2R_3 - \frac{1}{3}R_2. The pivots are 4, 9 and 4, the squares of LL's diagonal.

A symmetric matrix is positive definite exactly when elimination without swaps gives all positive pivots. Since det⁡A\det A is the product of the pivots (lesson 3.4.1), det⁡A=(2⋅3⋅2)2=144\det A = (2 \cdot 3 \cdot 2)^2 = 144.

Cholesky does about half the arithmetic of elimination, because AA's symmetry means only one triangle needs computing. For a positive definite matrix it also needs no row swaps.

Try it

cholesky is the rule, column by column. L[j, :j] is the part of row jj to the left of the diagonal. So L[j, :j] @ L[j, :j] is the sum of its squares.

It agrees with np.linalg.cholesky. The last line gives det⁡A\det A and the squared product of LL's diagonal.

⌘/Ctrl + Enter runs the cell

Try it

The three row operations of the text, without swaps. The diagonal of U holds the pivots, and their square roots are LL's diagonal.

⌘/Ctrl + Enter runs the cell

In NumPy: a test for positive definiteness

np.linalg.cholesky(A) returns LL. When AA is not positive definite it stops with LinAlgError: Matrix is not positive definite, as inv stopped on a singular matrix in lesson 3.2.3.

That makes it the quickest test on a computer: try the factorisation and catch the error. It costs less than finding the eigenvalues. It fails for the indefinite NN and for the semidefinite JJ.

Rule: Cholesky as a test

the factorisation succeeds  ⟺  A is positive definite\begin{gathered} \text{the factorisation succeeds} \\ \iff A \text{ is positive definite} \end{gathered}

The equivalence holds for symmetric AA, so check symmetry first. A failure says only "not positive definite": the eigenvalues tell whether AA is indefinite or semidefinite.

Two traps in the code:

  • NumPy does not check symmetry. It reads only the lower triangle and the diagonal. (410025)\begin{pmatrix} 4 & 100 \\ 2 & 5 \end{pmatrix} returns the same LL as AA, with no warning. Test np.allclose(A, A.T) before factoring.
  • SciPy returns the other triangle. scipy.linalg.cholesky(A) returns the upper factor L⊤L^\top unless you pass lower=True.

A semidefinite matrix sits on the edge. For JJ the last step is exactly 0\sqrt{0}, so the test fails. When rounding is involved, a zero can come out as a tiny positive number, and the factorisation may succeed with a useless, near-zero diagonal entry.

Try it

cholesky returns LL for AA and raises LinAlgError for NN and JJ. The typo matrix is not symmetric, yet it returns AA's LL. SciPy gives L⊤L^\top.

is_positive_definite checks symmetry first, then tries the factorisation. The last matrix is lesson 3.7.1's KK.

⌘/Ctrl + Enter runs the cell

Solving Ax = b with two triangular solves

With A=LL⊤A = LL^\top, the system Ax=bA\mathbf{x} = \mathbf{b} is L(L⊤x)=bL(L^\top\mathbf{x}) = \mathbf{b}. Call the bracket y\mathbf{y} and solve in two steps, each with a triangular matrix.

Rule: Solving with Cholesky

Ly=b(forward)L⊤x=y(back)\begin{gathered} L\mathbf{y} = \mathbf{b} \quad \text{(forward)} \\[4pt] L^\top\mathbf{x} = \mathbf{y} \quad \text{(back)} \end{gathered}

Forward substitution solves Ly=bL\mathbf{y} = \mathbf{b} from the top: row 1 of LL has one unknown, and each row below adds one more. Back substitution (lesson 3.3.1) then solves L⊤x=yL^\top\mathbf{x} = \mathbf{y} from the bottom.

Solve Ax=(−2,−4,8)A\mathbf{x} = (-2, -4, 8) with the 3×33 \times 3 example. Forward, with the rows of LL, (2,0,0)(2, 0, 0), (1,3,0)(1, 3, 0) and (−1,1,2)(-1, 1, 2):

y1=−2÷2=−1y2=(−4−y1)÷3=−1y3=(8+y1−y2)÷2=4\begin{aligned} y_1 &= -2 \div 2 = -1 \\ y_2 &= (-4 - y_1) \div 3 = -1 \\ y_3 &= (8 + y_1 - y_2) \div 2 = 4 \end{aligned}

Back, with L⊤=(21−1031002)L^\top = \begin{pmatrix} 2 & 1 & -1 \\ 0 & 3 & 1 \\ 0 & 0 & 2 \end{pmatrix}:

x3=4÷2=2x2=(−1−x3)÷3=−1x1=(−1−x2+x3)÷2=1\begin{aligned} x_3 &= 4 \div 2 = 2 \\ x_2 &= (-1 - x_3) \div 3 = -1 \\ x_1 &= (-1 - x_2 + x_3) \div 2 = 1 \end{aligned}

So x=(1,−1,2)\mathbf{x} = (1, -1, 2). Check it in row 3 of AA: −2(1)+2(−1)+6(2)=8-2(1) + 2(-1) + 6(2) = 8.

Factoring is the expensive part. Once LL is known, each new right-hand side b\mathbf{b} costs only the two triangular solves, far less than a new elimination.

Try it

solve_triangular from SciPy does each step; lower=True says the matrix is lower triangular. The answer agrees with np.linalg.solve.

The last lines are the forward substitution written out, as in the text.

⌘/Ctrl + Enter runs the cell

L as a square root

A=LL⊤A = LL^\top is the matrix version of a=a⋅aa = \sqrt{a}\cdot\sqrt{a}. For a 1×11 \times 1 matrix (a)(a), L=(a)L = (\sqrt{a}). For a diagonal matrix, LL takes the square root of each diagonal entry: (0.04000.09)\begin{pmatrix} 0.04 & 0 \\ 0 & 0.09 \end{pmatrix} gives L=(0.2000.3)L = \begin{pmatrix} 0.2 & 0 \\ 0 & 0.3 \end{pmatrix}.

And det⁡A=(det⁡L)2\det A = (\det L)^2, with det⁡L\det L the product of LL's diagonal.

As a map, LL sends the unit circle to an ellipse, as lesson 3.6.3 found for symmetric matrices. Its first column Le1=(2,1)L\mathbf{e}_1 = (2, 1) and its second Le2=(0,2)L\mathbf{e}_2 = (0, 2) both land on that ellipse. Le2L\mathbf{e}_2 points straight up, because the first row of LL is (2,0)(2, 0).

LL is not the only square root. Lesson 3.6.3 writes A=QΛQ⊤A = Q\Lambda Q^\top. Taking Λ1/2\Lambda^{1/2}, the diagonal matrix of λi\sqrt{\lambda_i}, gives the symmetric square root B=QΛ1/2Q⊤B = Q\Lambda^{1/2}Q^\top, with BB⊤=ABB^\top = A too. For AA above, B≈(1.94030.48510.48512.1828)B \approx \begin{pmatrix} 1.9403 & 0.4851 \\ 0.4851 & 2.1828 \end{pmatrix}.

LL and BB send the unit circle to the same ellipse, because LL⊤=BB⊤=ALL^\top = BB^\top = A, though each point lands somewhere different. Its semi-axes are λ\sqrt{\lambda} for the eigenvalues of AA: about 2.56 and 1.56.

Any CC with CC⊤=ACC^\top = A gives that ellipse. Cholesky picks the one that is lower triangular with a positive diagonal, which is the cheapest to compute and to solve with. Case study 3.7.4 uses LL to turn independent random numbers into correlated ones, with a covariance matrix as AA.

Try it

BB is built from eigh, as in lesson 3.6.3. Both BB and LL multiply with their transposes to give AA.

Then the eigenvalues of AA and their square roots, the ellipse's semi-axes. The last lines are the diagonal example and det⁡L=4\det L = 4, det⁡A=16\det A = 16.

⌘/Ctrl + Enter runs the cell

Common mistakes

  1. LL⊤LL^\top, not L⊤LL^\top L. For L=(2012)L = \begin{pmatrix} 2 & 0 \\ 1 & 2 \end{pmatrix}, L⊤L=(5224)L^\top L = \begin{pmatrix} 5 & 2 \\ 2 & 4 \end{pmatrix}, not AA.
  2. Take the square root on the diagonal. l11=a11l_{11} = \sqrt{a_{11}}, not a11a_{11}.
  3. Below the diagonal, divide by the diagonal entry of the same column, ljjl_{jj}, after subtracting the products already found.
  4. Check symmetry first. np.linalg.cholesky reads only the lower triangle and never complains about the upper one.
  5. Know which triangle you get. NumPy returns LL; scipy.linalg.cholesky returns L⊤L^\top unless lower=True.
  6. Forward first, then back. Solve Ly=bL\mathbf{y} = \mathbf{b} from the top, then L⊤x=yL^\top\mathbf{x} = \mathbf{y} from the bottom.
  7. A failure means "not positive definite", nothing more. The matrix may be indefinite, or semidefinite with a zero eigenvalue; ask eigvalsh which.

The Cholesky factorisation

0 of 12 correct

  1. 1.
  2. 2.
  3. 3.
  4. 4.
  5. 5.
  6. 6.
  7. 7.
  8. 8.
  9. 9.
  10. 10.
  11. 11.
  12. 12.
How to type answers
  • Powers with ^: x^7, 2^(1/3), a^-2.
  • Multiply with * or by writing side by side: 3x^2, 2(x+1).
  • ln(x) is the natural log, log(x, 2) is log base 2, e is Euler's number, sqrt(x) is a square root.
  • sin(x), cos(x) and tan(x) take radians; write pi for π and sin(x)^2 for sin²x.
  • Your first answer to each question is saved: it counts towards your score and brings the question back for review.
  • Exact answers like ln(20)/ln(3) are best. Decimals need about 4 significant figures.

Summary card

  • A positive definite AA has exactly one Cholesky factor: A=LL⊤A = LL^\top with LL lower triangular and a positive diagonal. Then x⊤Ax=∥L⊤x∥2\mathbf{x}^\top A\mathbf{x} = \lVert L^\top\mathbf{x} \rVert^2, the completed square.
  • 2×22 \times 2: l11=al_{11} = \sqrt{a}, l21=b/l11l_{21} = b/l_{11}, l22=c−l212l_{22} = \sqrt{c - l_{21}^2}. In general, column by column: ljj=ajj−∑k<jljk2l_{jj} = \sqrt{a_{jj} - \sum_{k<j} l_{jk}^2} and lij=(aij−∑k<jlikljk)/ljjl_{ij} = (a_{ij} - \sum_{k<j} l_{ik}l_{jk})/l_{jj}.
  • The diagonal of LL holds the square roots of the pivots, and det⁡A=(det⁡L)2\det A = (\det L)^2.
  • np.linalg.cholesky raises LinAlgError exactly when a symmetric AA is not positive definite: the cheapest test. It reads only the lower triangle, so check symmetry first.
  • Solve Ax=bA\mathbf{x} = \mathbf{b} by forward substitution in Ly=bL\mathbf{y} = \mathbf{b}, then back substitution in L⊤x=yL^\top\mathbf{x} = \mathbf{y}.
  • LL is one square root of AA; the symmetric QΛ1/2Q⊤Q\Lambda^{1/2}Q^\top is another, mapping the unit circle to the same ellipse.

Free further reading

Optional. This lesson covers what you need; these add more examples and practice. All are free to read.