You fill in a covariance matrix by hand, call np.linalg.cholesky, and get LinAlgError: Matrix is not positive definite. The matrix, , is symmetric with every entry positive. It is still no covariance. The signs of the eigenvalues decide that, and they set the shape of .
The surface f(x) = xᵀAx over a disc of radius 1.5, with the level set xᵀAx = 1 drawn on it. Along q₁ = (1, 1)/√2 the surface rises as 3t². Along q₂ = (1, −1)/√2 it follows λ₂t², which the slider sets. λ₂ = 1. A = [[2, 1], [1, 2]]. Eigenvalues 3 and 1. Pivots 2 and 1.5. Leading determinants 2 and 3. Positive definite, so the surface is a bowl.
The surface is for a symmetric with eigenvalues and . The slider moves : above a bowl, at a trough, below a saddle. The solid contour is . The caption prints the three tests.
The spectral theorem#
A real matrix is symmetric when , as covariance matrices and Hessians are. The spectral theorem says that every real symmetric matrix has real eigenvalues and an orthonormal basis of eigenvectors:
Against from Week 10, no eigenvalue is complex, and is just .
Why the eigenvectors are orthogonal. Take and with . Then
so and . The middle step uses . Within a repeated eigenvalue, Gram–Schmidt (Week 8) picks an orthonormal basis.
The scene builds from this formula. It fixes with , and with eigenvalue :
At the default this is . Trace and determinant give , so the eigenvalues are and .
The quadratic form in eigen-coordinates#
The quadratic form of a symmetric is
Change to eigen-coordinates , so . The cross term disappears:
In the scene that is . Two parabolas are drawn on the surface. The dashed one, along , is at every setting. The amber one, along , is , which goes flat and then opens down as the slider crosses .
Three tests for positive definiteness#
is positive definite () when for every . It is positive semidefinite when , and indefinite when the form takes both signs. A Hessian that is positive definite everywhere makes a function strictly convex (Optimization Week 2). Three tests agree for every real symmetric (Strang, §6.4):
- Eigenvalues. Every . Use with .
- Pivots. Elimination without row exchanges gives positive pivots. Equivalently, has a positive , and Cholesky succeeds.
- Leading determinants. Every upper-left determinant is positive (Sylvester's criterion). For that is and .
In the scene, and , so the second pivot is
At the pivots are and , the determinants are and , and all three tests pass. All three change sign at the same slider setting, , because each one carries the factor .
Positive entries prove nothing. Set the slider to . Then , the matrix from the opening. Every entry is positive, and the surface is a saddle: at the form is . A positive diagonal is necessary, since , but not sufficient.
Level sets: ellipse, lines, hyperbola#
In eigen-coordinates the level set is .
- : an ellipse with half-axes along and along . A large eigenvalue gives a short axis.
- : two lines , parallel to the trough's floor along .
- : a hyperbola. The form is positive near and negative near .
Next week. For any , is positive semidefinite, since .
Knowledge Check#
Set the slider to λ₂ = 2. The caption shows A = [[2.5, 0.5], [0.5, 2.5]]. What is det A?
Set the slider to λ₂ = 0. What does the level set xᵀAx = 1 look like?
Browser lab: three tests at three slider settings#
Build the scene's matrix at three slider settings, run the three tests and Cholesky, and plot .
import numpy as np
import matplotlib.pyplot as plt
def scene_matrix(lam2):
# 3 q1 q1ᵀ + lam2 q2 q2ᵀ with q1 = (1, 1)/√2 and q2 = (1, −1)/√2
return 0.5 * np.array([[3 + lam2, 3 - lam2], [3 - lam2, 3 + lam2]])
u, v = np.meshgrid(np.linspace(-1.5, 1.5, 301), np.linspace(-1.5, 1.5, 301))
fig, axes = plt.subplots(1, 3, figsize=(10, 3.6))
for ax, lam2 in zip(axes, [1.0, 0.0, -1.0]):
A = scene_matrix(lam2)
eig = np.linalg.eigvalsh(A)
det = A[0, 0] * A[1, 1] - A[0, 1] ** 2
pivots = [A[0, 0], det / A[0, 0]]
minors = [A[0, 0], det]
try:
np.linalg.cholesky(A)
chol = "succeeds"
except np.linalg.LinAlgError:
chol = "fails"
print(f"lam2 = {lam2:+.0f} A = {A.round(2).tolist()}")
print(f" eigenvalues {eig.round(3)} pivots {np.round(pivots, 3)} minors {np.round(minors, 3)} Cholesky {chol}")
f = A[0, 0] * u**2 + 2 * A[0, 1] * u * v + A[1, 1] * v**2
ax.contour(u, v, f, levels=[1.0])
ax.set_aspect("equal")
ax.set_title(f"λ₂ = {lam2:+.0f}: xᵀAx = 1")
plt.tight_layout()
plt.show()
The printed values match the caption at each setting. At the exact second pivot is , so Cholesky's verdict there turns on rounding. Test a semidefinite matrix by its eigenvalues, with a tolerance. The plots show an ellipse, two lines and a hyperbola. Add -2.0 to the list and predict the second pivot first: .
Key takeaways#
The signs of the eigenvalues of a symmetric set the shape of . With , a positive gives a bowl, zero a trough, and negative a saddle. Eigenvalues, pivots and leading determinants agree, and in the scene all three flip at . Positive entries prove nothing, since is a saddle. The level set is an ellipse with half-axes only when every .
Further reading#
- Strang, G. Introduction to Linear Algebra, 6th ed. — §6.2 Symmetric Matrices, §6.4 Positive Definite Matrices
- Trefethen, L.N. & Bau, D. Numerical Linear Algebra — Lecture 25 (Spectral Theorem)
Reading is free. PDF and ePub unlock with the newsletter. Join once and every lesson opens in this browser.