linearly

Lecture 15

Eigenvalues & Positive Definite Matrices

Almost every direction gets turned when a matrix acts. A few survive and only stretch. Those directions run the long run, and their stretch factors shape every loss surface you will ever descend.

115 slides / MIT 18.06, Lectures 21, 22, 24, 25, 27 / Strang Ch. 6

Slide 1 of 115
1 / 115

The idea in one sentence

Almost every direction gets turned when a matrix acts. A few come back pointing exactly the way they went in, only longer or shorter, and those few decide everything that happens when you apply the matrix again and again.

The directions that survive

Lecture 2 left you with a matrix as a machine: a vector goes in, a moved vector comes out. Now stop watching the whole plane and watch one arrow at a time. Take

A=[4123]A = \begin{bmatrix} 4 & 1 \\ 2 & 3 \end{bmatrix}

and feed it (1,1)(-1, 1). Out comes (3,1)(-3, 1). The arrow got longer, and it also swung around toward the horizontal. Output and input point different ways. That is the ordinary case, and it is what almost every arrow does.

Now feed the same machine (1,1)(1, 1). Out comes (5,5)(5, 5): the same direction, five times as long. Feed it (1,2)(1, -2). Out comes (2,4)(2, -4): the same direction, twice as long. Those two arrows did not turn at all. The machine that bends everything else leaves these two lines exactly where they are and slides points along them.

An arrow that survives like this is an eigenvector of AA. The number it gets multiplied by is its eigenvalue, written λ\lambda. The word eigen is German for own, so these are the matrix’s own directions. One line holds the whole subject:

Ax=λx,x0.Ax = \lambda x, \qquad x \neq 0 .

The condition x0x \neq 0 is not fussiness. The zero vector satisfies A0=λ0A0 = \lambda 0 for every number λ\lambda, so allowing it would make the definition say nothing. Also, if xx is an eigenvector then so is 2x2x and so is x-x, with the same λ\lambda. What survives is a line through the origin, and xx is just a name for a point on it.

Ax = 5xx = (1, 1)Ax = 2xx = (1, −2)xAxthis one turnedthese two lines survive
Fig. 1 

Three arrows, one machine. (1,1)(-1, 1) is turned. (1,1)(1, 1) and (1,2)(1, -2) stay on their own dotted lines and only change length. Those two lines are the eigendirections, and the length ratios 5 and 2 are the eigenvalues.

The eigenvalue is allowed to be zero. If Ax=0Ax = 0 for some nonzero xx, then xx is an eigenvector with λ=0\lambda = 0, and xx sits in the null space N(A)N(A). You have already met one. The ring road machine from Lecture 2 sends (1,1,1)(1, 1, 1) to (0,0,0)(0, 0, 0), because raising all three cities by a meter changes no climb. That blind direction is an eigenvector with eigenvalue zero. Singular and “has eigenvalue zero” are the same news said twice.

Try it. Hunt for the directions that survive
xAx

A = [2.0 0.0; 0.0 0.5]
x  = (1.00, 0.00)
Ax = (2.00, 0.00)

turned by 0.0°

Orange is the input, blue is where A sends it. Sweep slowly. Catch the two angles where the two arrows lie on one line: the picture turns green and the ratio of lengths is the eigenvalue. Rotate has none.

How to find them

Guessing works for small matrices and stops working fast. Here is the procedure, and it comes straight out of Lecture 14.

Move everything to one side of Ax=λxAx = \lambda x. You cannot subtract the number λ\lambda from the matrix AA, so put it on the diagonal first, as λI\lambda I:

(AλI)x=0.(A - \lambda I)\,x = 0 .

Read that as a question about one matrix. We want a nonzero xx in the null space of AλIA - \lambda I. A square matrix has a nonzero null vector exactly when it is singular, and Lecture 14 gave a one-number test for singular. So λ\lambda is an eigenvalue of AA precisely when

det(AλI)=0.\det(A - \lambda I) = 0 .

In words: the eigenvalues of AA are the numbers you can subtract from its diagonal to make it singular. That one sentence is the whole method.

Run it on the matrix from Figure 1:

det[4λ123λ]=(4λ)(3λ)2=λ27λ+10=(λ5)(λ2).\det \begin{bmatrix} 4 - \lambda & 1 \\ 2 & 3 - \lambda \end{bmatrix} = (4 - \lambda)(3 - \lambda) - 2 = \lambda^2 - 7\lambda + 10 = (\lambda - 5)(\lambda - 2).

The roots are λ=5\lambda = 5 and λ=2\lambda = 2, which is what Figure 1 showed. Now put each root back and solve. For λ=5\lambda = 5,

A5I=[1122],A - 5I = \begin{bmatrix} -1 & 1 \\ 2 & -2 \end{bmatrix},

whose null space is spanned by (1,1)(1, 1). For λ=2\lambda = 2 the matrix is [2121]\begin{bmatrix} 2 & 1 \\ 2 & 1 \end{bmatrix} and the null space is spanned by (1,2)(1, -2). Two lines, two numbers, done.

λ = 3det(A − 3I) = −2still has areaλ = 5det(A − 5I) = 0squashed onto a lineλ = 2det(A − 2I) = 0squashed onto a line
Fig. 2 

What det(AλI)=0\det(A - \lambda I) = 0 looks like. The dashed square is the unit square and the blue shape is where AλIA - \lambda I sends it. For a λ\lambda that is not an eigenvalue the landing spot is a parallelogram, and the shaded patch is the area it still has. At λ=5\lambda = 5 and λ=2\lambda = 2 the plane collapses onto a line: no area, determinant zero, and some nonzero vector sent to the origin. That vector is the eigenvector.

Look again at the polynomial λ27λ+10\lambda^2 - 7\lambda + 10. The 7 is 4+34 + 3, the sum along the diagonal of AA. The 10 is 12212 - 2, the determinant of AA. That is not a coincidence of this example. For any nn by nn matrix,

λ1+λ2++λn=tr(A),λ1λ2λn=det(A),\lambda_1 + \lambda_2 + \dots + \lambda_n = \mathrm{tr}(A), \qquad \lambda_1 \lambda_2 \cdots \lambda_n = \det(A),

where tr(A)\mathrm{tr}(A) is the sum of the diagonal entries, the trace. Both checks cost seconds and both catch arithmetic slips. Here 5+2=75 + 2 = 7 and 5×2=105 \times 2 = 10.

One more free result. If AA is triangular then AλIA - \lambda I is triangular too, and the determinant of a triangular matrix is the product of its diagonal entries. So det(AλI)=(a11λ)(annλ)\det(A - \lambda I) = (a_{11} - \lambda)\cdots(a_{nn} - \lambda), and the eigenvalues of a triangular matrix are exactly the numbers sitting on its diagonal. Nothing to compute.

A machine that settles down

You can find eigenvalues now. Here is why anyone bothers. Two towns swap residents every year. Of the people in town 1, eighty percent stay and twenty percent move to town 2. Of the people in town 2, seventy percent stay and thirty percent move to town 1. Write the fractions as columns:

A=[0.80.30.20.7].A = \begin{bmatrix} 0.8 & 0.3 \\ 0.2 & 0.7 \end{bmatrix}.

Each column sums to 1, because everybody ends up somewhere. A matrix like this is called a Markov matrix. If uku_k holds the two population fractions in year kk, then uk+1=Auku_{k+1} = A u_k, and the population in year 100 is A100u0A^{100} u_0. Nobody wants to multiply a hundred matrices.

Do the eigenvalue computation instead:

det[0.8λ0.30.20.7λ]=λ21.5λ+0.5=(λ1)(λ0.5).\det \begin{bmatrix} 0.8 - \lambda & 0.3 \\ 0.2 & 0.7 - \lambda \end{bmatrix} = \lambda^2 - 1.5\lambda + 0.5 = (\lambda - 1)(\lambda - 0.5).

The trace check gives 1+0.5=1.51 + 0.5 = 1.5 and the determinant check gives 1×0.5=0.51 \times 0.5 = 0.5. Both agree. For λ=1\lambda = 1, the matrix AIA - I has rows (0.2,0.3)(-0.2, 0.3) and (0.2,0.3)(0.2, -0.3), so the null space is spanned by (3,2)(3, 2), which we rescale to x1=(0.6,0.4)x_1 = (0.6, 0.4) so the entries sum to 1. For λ=0.5\lambda = 0.5 the null space of A0.5IA - 0.5I is spanned by x2=(1,1)x_2 = (1, -1).

Those two numbers say everything. Start from u0=(1,0)u_0 = (1, 0), everyone in town 1, and split it along the eigenvectors:

u0=(0.6,0.4)+0.4(1,1).u_0 = (0.6,\, 0.4) + 0.4\,(1,\, -1).

Each year, AA leaves the first piece alone and halves the second, because that is what eigenvalues 1 and 0.5 do. So

uk=(0.6,0.4)+0.4(12)k(1,1),u_k = (0.6,\, 0.4) + 0.4 \left(\tfrac{1}{2}\right)^{k} (1,\, -1),

and the second piece dies out. The population settles at 60 percent and 40 percent no matter where it starts. The eigenvector for λ=1\lambda = 1 is the steady state, and the eigenvector for λ=0.5\lambda = 0.5 is the part that decays.

u₀ = (1, 0)u₁u₂steady state (0.6, 0.4)gap halves each year(0, 1)x + y = 1
Fig. 3 

Every column of AA sums to 1, so the total population never changes and every state stays on the line x+y=1x + y = 1. Along that line the gap to (0.6,0.4)(0.6, 0.4) is multiplied by 0.50.5 each year.

The code below runs the whole example. It checks the two eigenvectors by hand, checks the trace and the determinant, and then computes A100A^{100} twice: once by a hundred honest multiplications, once from the eigenvalues alone.

python
import numpy as np

A = np.array([[0.8, 0.3], [0.2, 0.7]])

x1 = np.array([0.6, 0.4])       # null space of A - I
x2 = np.array([1.0, -1.0])      # null space of A - 0.5 I
print(A @ x1)                   # [0.6 0.4]   unchanged, so lambda = 1
print(A @ x2)                   # [ 0.5 -0.5] halved,    so lambda = 0.5

print(np.trace(A), np.linalg.det(A))   # 1.5 0.5  = 1 + 0.5 and 1 * 0.5

def power(M, k):                # the expensive way
    P = M
    for _ in range(k - 1):
        P = P @ M
    return P

print(power(A, 100))            # [[0.6 0.6]
                                #  [0.4 0.4]]

X = np.column_stack([x1, x2])   # eigenvectors in the columns
L = np.diag([1.0 ** 100, 0.5 ** 100])
print(X @ L @ np.linalg.inv(X)) # [[0.6 0.6]
                                #  [0.4 0.4]]   same answer, two numbers of work

Both columns of A100A^{100} are the steady state. That is the promise of eigenvalues: a hundred matrix multiplications collapse into raising two numbers to the hundredth power, and one of those numbers is so small that it vanishes.

Matrices whose eigenvalues you can read off

Some machines wear their answers on the outside. Four of them are worth memorizing, because they show up inside everything else.

Start with the identity. Ix=xIx = x for every xx, so every vector is an eigenvector and the only eigenvalue is 1. Nothing turns because nothing moves.

Next a projection. Lecture 11 built P=aaT/aTaP = aa\T / a\T a, the machine that drops every vector onto the line through aa. Take a=(1,1)a = (1, 1), so

P=[0.50.50.50.5].P = \begin{bmatrix} 0.5 & 0.5 \\ 0.5 & 0.5 \end{bmatrix}.

A vector already on the line, like (1,1)(1, 1), is left alone: λ=1\lambda = 1. A vector perpendicular to the line, like (1,1)(1, -1), is crushed to zero: λ=0\lambda = 0. There is a reason those are the only two options. Projecting twice is the same as projecting once, so P2=PP^2 = P, and applying that to an eigenvector gives λ2=λ\lambda^2 = \lambda. The only numbers that survive are 0 and 1. The column space of PP is the eigenvectors for λ=1\lambda = 1, and the null space is the eigenvectors for λ=0\lambda = 0.

Double a projection and subtract the identity and you get a reflection, a mirror, R=2PI=[0110]R = 2P - I = \begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix}. Vectors on the mirror line are fixed, so λ=1\lambda = 1. Vectors perpendicular to it are sent to their opposites, so λ=1\lambda = -1. Reflecting twice does nothing, so R2=IR^2 = I and λ2=1\lambda^2 = 1. The same argument as before, one letter different.

Last, a rotation. Turn the plane a quarter circle, Q=[0110]Q = \begin{bmatrix} 0 & -1 \\ 1 & 0 \end{bmatrix}. Every direction moves. No line survives, and the algebra agrees: det(QλI)=λ2+1\det(Q - \lambda I) = \lambda^2 + 1, which no real number solves.

projectionλ = 1 and λ = 0reflectionλ = 1 and λ = −1rotationno real λstaysto zerostaysxRx = −xxQx
Fig. 4 

Three machines and their surviving lines. The projection keeps one line and destroys the perpendicular one. The reflection keeps one and reverses the other. The rotation keeps none, and its characteristic equation λ2+1=0\lambda^2 + 1 = 0 says so.

Two shortcuts follow from the definition and cost nothing. Apply AA twice to an eigenvector and you get A2x=λ2xA^2 x = \lambda^2 x, so squaring a matrix squares its eigenvalues and keeps its eigenvectors. Shift the matrix by cIcI and every eigenvalue shifts by cc, since (A+cI)x=(λ+c)x(A + cI)x = (\lambda + c)x. The eigenvectors do not move at all.

The shortcut you might expect next is false. Eigenvalues of A+BA + B are not the sums of the eigenvalues, and eigenvalues of ABAB are not the products, because AA and BB generally have different eigenvectors. Take A=[0100]A = \begin{bmatrix} 0 & 1 \\ 0 & 0 \end{bmatrix} and B=[0010]B = \begin{bmatrix} 0 & 0 \\ 1 & 0 \end{bmatrix}. Every eigenvalue of each is 0. Yet A+B=[0110]A + B = \begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix} has eigenvalues 11 and 1-1, and AB=[1000]AB = \begin{bmatrix} 1 & 0 \\ 0 & 0 \end{bmatrix} has eigenvalues 1 and 0. Sums and products of eigenvalues only behave when the two matrices share eigenvectors, which happens exactly when AB=BAAB = BA.

Diagonalization

So far the eigenvectors have been handled one at a time. Take them all at once and the matrix comes apart into numbers.

Suppose an nn by nn matrix has nn independent eigenvectors. Put them in the columns of a matrix XX and put the eigenvalues on the diagonal of Λ\Lambda. Then look at what AXAX is, column by column: the kk-th column of AXAX is AxkAx_k, which is λkxk\lambda_k x_k. That is the kk-th column of XX scaled, which is exactly the kk-th column of XΛX\Lambda. So

AX=XΛ,A=XΛX1,Λ=X1AX.AX = X\Lambda, \qquad A = X \Lambda X^{-1}, \qquad \Lambda = X^{-1} A X .

XX is invertible because its columns are independent, which is the only thing we assumed.

Read the middle formula from right to left and it becomes a sentence. X1X^{-1} rewrites a vector in eigen-coordinates, saying how much of each eigenvector it contains. Λ\Lambda scales each of those amounts by its own eigenvalue. XX writes the result back in ordinary coordinates. Change the language, do something trivial, change the language back.

Watch it on the matrix from Figure 1, whose eigenvectors are x1=(1,1)x_1 = (1, 1) with λ1=5\lambda_1 = 5 and x2=(1,2)x_2 = (1, -2) with λ2=2\lambda_2 = 2. Take u=(3,0)u = (3, 0). In eigen-coordinates, u=2x1+1x2u = 2x_1 + 1x_2, since 2(1,1)+(1,2)=(3,0)2(1,1) + (1,-2) = (3, 0). Now apply AA by scaling each amount:

Au=25x1+12x2=10(1,1)+2(1,2)=(12,6),Au = 2 \cdot 5\, x_1 + 1 \cdot 2\, x_2 = 10(1, 1) + 2(1, -2) = (12, 6),

which is what A(3,0)A(3,0) gives directly. The matrix never had to be multiplied.

u = (3, 0)Au = (12, 6)2x₁10x₁ (×5)x₂2x₂ (×2)eigenline 1eigenline 2
Fig. 5 

Diagonalization, drawn. Split uu along the two eigenlines, stretch each piece by its own eigenvalue, add the pieces back. The parallelogram before and the parallelogram after have the same two sides, scaled by 5 and by 2.

Powers come out of this for free. Two copies give A2=XΛX1XΛX1=XΛ2X1A^2 = X\Lambda X^{-1} X \Lambda X^{-1} = X \Lambda^2 X^{-1}, since the inner pair cancels, and the same cancellation happens all the way up:

Ak=XΛkX1.A^k = X \Lambda^k X^{-1} .

Raising a diagonal matrix to the hundredth power means raising nn numbers to the hundredth power. That is the entire trick behind the Markov example, and it also answers the stability question at a glance: AkA^k goes to the zero matrix exactly when every λi<1|\lambda_i| < 1.

It does not always work. Diagonalization needs nn independent eigenvectors, and some matrices do not have them. Take

A=[1101].A = \begin{bmatrix} 1 & 1 \\ 0 & 1 \end{bmatrix}.

It is triangular, so the eigenvalues are 1 and 1. The root λ=1\lambda = 1 appears twice in the characteristic polynomial, which we call its algebraic multiplicity. But AI=[0100]A - I = \begin{bmatrix} 0 & 1 \\ 0 & 0 \end{bmatrix} has rank 1, so its null space is only one dimensional: there is a single eigendirection, (1,0)(1, 0), and no second one. The number of independent eigenvectors for a given λ\lambda is its geometric multiplicity, and it is never larger than the algebraic multiplicity. When it comes up short there is no eigenvector basis and no diagonalization. Distinct eigenvalues always give independent eigenvectors, so the trouble can only start when a root repeats.

Fibonacci and the golden ratio

Now a matrix where everything works, and where the answer turns out to be famous.

The numbers 0,1,1,2,3,5,8,13,0, 1, 1, 2, 3, 5, 8, 13, \dots obey Fk+2=Fk+1+FkF_{k+2} = F_{k+1} + F_k. That is a rule for getting the next number from the previous two, which sounds like nothing to do with matrices until you stack two consecutive terms into a vector. Let uk=(Fk+1,Fk)u_k = (F_{k+1}, F_k). Then

uk+1=[Fk+2Fk+1]=[1110][Fk+1Fk]=Auk.u_{k+1} = \begin{bmatrix} F_{k+2} \\ F_{k+1} \end{bmatrix} = \begin{bmatrix} 1 & 1 \\ 1 & 0 \end{bmatrix} \begin{bmatrix} F_{k+1} \\ F_{k} \end{bmatrix} = A u_k .

The top row adds the two entries, the bottom row remembers the old top. So uk=Aku0u_k = A^k u_0 with u0=(1,0)u_0 = (1, 0), and the Fibonacci numbers are the powers of one small matrix. Now diagonalize it:

det[1λ11λ]=λ2λ1=0λ=1±52.\det \begin{bmatrix} 1 - \lambda & 1 \\ 1 & -\lambda \end{bmatrix} = \lambda^2 - \lambda - 1 = 0 \quad\Longrightarrow\quad \lambda = \frac{1 \pm \sqrt{5}}{2}.

The larger root is the golden ratio φ1.618\varphi \approx 1.618, and the smaller is ψ0.618\psi \approx -0.618. The trace check gives φ+ψ=1\varphi + \psi = 1 and the determinant check gives φψ=1\varphi \psi = -1, both of which you can read straight off the polynomial. For the eigenvectors, the second row of AλIA - \lambda I says x1=λx2x_1 = \lambda x_2, so the eigenvector is (λ,1)(\lambda, 1).

Split u0=(1,0)u_0 = (1, 0) along them. Solving c1(φ,1)+c2(ψ,1)=(1,0)c_1(\varphi, 1) + c_2(\psi, 1) = (1, 0) gives c2=c1c_2 = -c_1 and c1(φψ)=1c_1(\varphi - \psi) = 1, and since φψ=5\varphi - \psi = \sqrt{5} we get c1=1/5c_1 = 1/\sqrt5. Multiply by AA a hundred times, which now means multiplying each piece by its own eigenvalue a hundred times, and read the bottom entry:

Fk=φkψk5.F_k = \frac{\varphi^{\,k} - \psi^{\,k}}{\sqrt 5}.

A sequence defined by addition has a closed form built from square roots. And because ψ<1|\psi| < 1, the second term shrinks to nothing: FkF_k is the nearest integer to φk/5\varphi^k / \sqrt5. It also explains a fact people notice without knowing why. The ratio Fk+1/FkF_{k+1}/F_k marches toward φ\varphi, because the φ\varphi piece grows and the ψ\psi piece dies, so after a while the vector uku_k is pointing almost exactly along the eigenvector (φ,1)(\varphi, 1).

235813each side is the sumof the two before it13 / 8 = 1.62521 / 13 ≈ 1.615φ ≈ 1.618the spiral grows by aboutφ every quarter turn
Fig. 6 

The squares are Fibonacci numbers and the arcs join into a spiral. The eigenvalue φ\varphi is the growth factor: each square’s side is φ\varphi times the one before it in the limit, so each quarter turn of the spiral grows by very nearly φ\varphi.

python
import numpy as np

phi = (1 + 5 ** 0.5) / 2
psi = (1 - 5 ** 0.5) / 2
print(phi, psi)            # 1.618033988749895 -0.6180339887498949
print(phi + psi, phi * psi)  # 1.0 -1.0000000000000002  the trace and the determinant

def fib_closed(n):
    return (phi ** n - psi ** n) / 5 ** 0.5

def fib_loop(n):
    a, b = 0, 1
    for _ in range(n):
        a, b = b, a + b
    return a

for n in [10, 30, 50]:
    print(n, fib_loop(n), fib_closed(n))
# 10 55 55.000000000000014
# 30 832040 832040.0000000008
# 50 12586269025 12586269025.00002

A = np.array([[1.0, 1.0], [1.0, 0.0]])
u = np.array([1.0, 0.0])
for _ in range(10):
    u = A @ u
print(u)                     # [89. 55.]  which is (F11, F10)
print(fib_loop(101) / fib_loop(100))   # 1.618033988749895

The closed form drifts from the integers in the sixteenth significant digit, which is a double doing its best, and the loop stays exact. Both are worth having. The formula tells you the answer grows like φk\varphi^k; the loop tells you the answer.

That last observation, that repeated multiplication drags any starting vector toward the largest eigenvalue’s direction, is an algorithm. It is called power iteration, and it is how the biggest eigenvalue of a huge matrix gets computed when nobody can afford a characteristic polynomial. Press the button and watch it happen.

Try it. Multiply over and over
u₀

A  = [1.0 1.0; 1.0 0.0]
k  = 0
u  = (1.00, 0.00)
‖uₖ‖/‖uₖ₋₁‖ = -

still swinging

The blue arrow is redrawn at a fixed length, so only its direction moves; the orange trail is where it has been. Press a few times and it stops moving. That settled direction is the top eigenvector, and the printed ratio is its eigenvalue. Rotate never settles.

Symmetric matrices are the good case

Everything so far has worked for any matrix with enough eigenvectors. One family does much better than that, and it happens to be the family your data arrives in.

A matrix is symmetric when S=STS = S\T, so the entry in row ii column jj equals the entry in row jj column ii. This is not a rare accident. Covariance matrices are symmetric. So is every Gram matrix ATAA\T A, and so is the second-derivative matrix of any smooth function. Symmetric matrices come with two gifts.

The first gift is that the eigenvalues are real. No rotations hide inside a symmetric matrix, so the complex case never arises.

The second is that the eigenvectors are perpendicular. Here is the whole proof for two different eigenvalues. Suppose Sx1=λx1Sx_1 = \lambda x_1 and Sx2=μx2Sx_2 = \mu x_2 with λμ\lambda \neq \mu. Then

λ(x1x2)=(Sx1)x2=x1(Sx2)=μ(x1x2),\lambda \, (x_1 \cdot x_2) = (Sx_1) \cdot x_2 = x_1 \cdot (Sx_2) = \mu \, (x_1 \cdot x_2),

where the middle step is where symmetry is used, since (Sx1)Tx2=x1TSTx2(Sx_1)\T x_2 = x_1\T S\T x_2 and ST=SS\T = S. So (λμ)(x1x2)=0(\lambda - \mu)(x_1 \cdot x_2) = 0, and since λμ\lambda \neq \mu the dot product must be zero. Perpendicular.

Scale each eigenvector to length 1 and call them q1,,qnq_1, \dots, q_n. Put them in the columns of QQ. Perpendicular unit columns mean QTQ=IQ\T Q = I, so Q1=QTQ^{-1} = Q\T, and diagonalization loses its inverse:

S=QΛQT.S = Q \Lambda Q\T .

This is the spectral theorem, and it holds for every symmetric matrix, even when eigenvalues repeat. Read the three factors right to left: QTQ\T rotates space so the eigenvectors become the coordinate axes, Λ\Lambda stretches along those axes, QQ rotates back. Every symmetric matrix is a stretch in disguise, and the disguise is a rotation.

Take S=[2112]S = \begin{bmatrix} 2 & 1 \\ 1 & 2 \end{bmatrix}. The trace is 4 and the determinant is 3, so the eigenvalues are 3 and 1. The eigenvectors are q1=(1,1)/2q_1 = (1,1)/\sqrt2 and q2=(1,1)/2q_2 = (1,-1)/\sqrt2, which are perpendicular as promised. Feed SS the unit circle and it comes out as an ellipse with half-axes 3 and 1, pointing along q1q_1 and q2q_2.

λ₁q₁, length 3λ₂q₂, length 1q₁the unit circle, dashedits image, an ellipse
Fig. 7 

The symmetric matrix with rows (2,1)(2,1) and (1,2)(1,2) turns the unit circle into an ellipse. The axes of that ellipse are the eigenvectors and their half-lengths are the eigenvalues. Rotate, stretch, rotate back.

There is one more way to read S=QΛQTS = Q\Lambda Q\T, and it is the one that will matter later. Multiply it out as a sum of columns times rows:

S=λ1q1q1T+λ2q2q2T++λnqnqnT.S = \lambda_1 q_1 q_1\T + \lambda_2 q_2 q_2\T + \dots + \lambda_n q_n q_n\T .

Every piece qiqiTq_i q_i\T is a familiar object. It is Lecture 11’s projection matrix aaT/aTaaa\T / a\T a with a=qia = q_i, and since qiq_i has length 1 the denominator is 1. So each piece projects onto one eigendirection, and SS scales that projection by λi\lambda_i and adds the results. A symmetric matrix is a weighted sum of projections onto perpendicular lines. For the example, q1q1T=12[1111]q_1 q_1\T = \tfrac12\begin{bmatrix} 1 & 1 \\ 1 & 1 \end{bmatrix} and q2q2T=12[1111]q_2 q_2\T = \tfrac12\begin{bmatrix} 1 & -1 \\ -1 & 1 \end{bmatrix}, and 33 times the first plus 11 times the second rebuilds [2112]\begin{bmatrix} 2 & 1 \\ 1 & 2 \end{bmatrix} exactly.

Positive definite

Some symmetric matrices have all their eigenvalues on the positive side. That single condition turns out to be the difference between a surface with a bottom and a surface without one, which is why this section is really about optimization.

A symmetric matrix SS is positive definite when the number

xTSxx\T S x

is greater than zero for every nonzero xx. That number is called the energy. For a 2 by 2 matrix with rows (a,b)(a, b) and (b,c)(b, c) the energy works out to ax12+2bx1x2+cx22a x_1^2 + 2b x_1 x_2 + c x_2^2. So the condition says: this quadratic is positive everywhere except at the origin.

Why is that the same as “all eigenvalues positive”? Because of the spectral theorem. Write xx in the eigenvector basis, x=c1q1++cnqnx = c_1 q_1 + \dots + c_n q_n. The qiq_i are perpendicular unit vectors, so the cross terms vanish and

xTSx=λ1c12+λ2c22++λncn2.x\T S x = \lambda_1 c_1^2 + \lambda_2 c_2^2 + \dots + \lambda_n c_n^2 .

The energy is a weighted sum of squares with the eigenvalues as weights. It is positive for every nonzero xx exactly when every weight is positive. Take one λi\lambda_i negative and the energy goes negative along qiq_i.

For a symmetric matrix, four tests all say the same thing, and each is worth having because each is cheap in a different situation:

  • every eigenvalue is positive;
  • every pivot from elimination is positive;
  • every leading top-left determinant is positive, the 1×11\times1, the 2×22\times2, and so on;
  • S=ATAS = A\T A for some matrix AA with independent columns.

The last one is the reason positive definite matrices are everywhere in machine learning. If AA holds your data, then xTATAx=(Ax)T(Ax)=Ax2x\T A\T A x = (Ax)\T(Ax) = \norm{Ax}^2, which is a squared length and so never negative, and it is zero only when Ax=0Ax = 0, which independent columns forbid. Least squares and covariance both produce ATAA\T A, so both produce positive definite matrices whenever the data has no redundant column.

Try all four on the tridiagonal matrix from Lecture 5:

K=[210121012].K = \begin{bmatrix} 2 & -1 & 0 \\ -1 & 2 & -1 \\ 0 & -1 & 2 \end{bmatrix}.

Its pivots are 2,32,432, \tfrac32, \tfrac43, all positive. Its leading determinants are 2,3,42, 3, 4, all positive. Its eigenvalues are 220.5862 - \sqrt2 \approx 0.586, then 22, then 2+23.4142 + \sqrt2 \approx 3.414, all positive, and they multiply to 4, which is the determinant, and sum to 6, which is the trace. And Cholesky factors it as K=LLTK = LL\T, which is the fourth test with A=LTA = L\T. Four tests, one verdict.

Positive semidefinite relaxes every strict inequality to an inclusive one. The energy is allowed to reach zero: xTSx0x\T S x \geq 0 for every xx, some eigenvalue may be 0, some pivot may be 0, and the determinant may be 0. The two names get swapped constantly, so keep the distinction sharp: definite means the bowl has exactly one lowest point, semidefinite means the bowl may have a flat valley floor running through it.

N=[1111]N = \begin{bmatrix} 1 & 1 \\ 1 & 1 \end{bmatrix} is the small example. Its energy is x12+2x1x2+x22=(x1+x2)2x_1^2 + 2x_1x_2 + x_2^2 = (x_1 + x_2)^2, which is never negative and is zero all along the line x1+x2=0x_1 + x_2 = 0. Its eigenvalues are 2 and 0. Semidefinite, not definite. And [1001]\begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix}, with eigenvalues 1 and 1-1, has energy x12x22x_1^2 - x_2^2, which is positive in one direction and negative in another. That one is called indefinite, and its graph is a saddle.

positive definiteλ = 1, 3. One lowest point.indefiniteλ = 1, −1. A saddle.positive semidefiniteλ = 2, 0. A flat valley.
Fig. 8 

The graph of the energy xTSxx\T S x, for three symmetric matrices. Positive eigenvalues curve the surface up in every direction. A negative one opens a downhill escape. A zero one flattens a whole line, and the bottom stretches into a valley. That flat direction is a null direction of SS, which is why the trough carries the null-space color from the four-subspaces picture.

You have met this test before. In one variable you look at ff'': positive means the curve holds water and you are sitting at the bottom of it. The Hessian is that same number with more directions to account for, and you read the verdict off the signs exactly as you always did.

more directionsf(x) = x², f″ = 2 > 0λ₁ = 1, λ₂ = 9, both > 0one variable: one number decidestwo variables: the eigenvalues decideλ₁ > 0, λ₂ > 0λ₁ > 0, λ₂ < 0λ₁ > 0, λ₂ = 0a bowla saddlea valley
Fig. 9 

The second derivative test, grown up. On the left one number settles it. On the right that number becomes a list, the eigenvalues of the Hessian, and the signs decide the shape: all positive gives the green bowl, opposite signs the pink saddle, a zero the cyan valley. Flip every sign and the bowl becomes a peak.

python
import numpy as np

K = np.array([[2.0, -1.0, 0.0], [-1.0, 2.0, -1.0], [0.0, -1.0, 2.0]])

print(np.linalg.eigvalsh(K))    # [0.58578644 2. 3.41421356]   2 - sqrt2, 2, 2 + sqrt2

L = np.linalg.cholesky(K)       # succeeds only for positive definite input
print(np.diag(L) ** 2)          # [2. 1.5 1.33333333]  the pivots

print(np.round([np.linalg.det(K[:1, :1]),
                np.linalg.det(K[:2, :2]),
                np.linalg.det(K)], 6))            # [2. 3. 4.]  the leading determinants

for x in [np.array([1.0, 2.0, 3.0]), np.array([1.0, 1.0, 1.0])]:
    print(x @ K @ x)            # 12.0 then 2.0   energy, positive away from the origin

lam = np.linalg.eigvalsh(K)
Q = np.linalg.eigh(K)[1]
S = sum(lam[i] * np.outer(Q[:, i], Q[:, i]) for i in range(3))
print(np.round(S, 10))          # rebuilds K from rank-one projections

N = np.array([[1.0, 1.0], [1.0, 1.0]])
print(np.linalg.eigvalsh(N))          # [0. 2.]   semidefinite, not definite
d = np.array([1.0, -1.0])
print(d @ N @ d)                      # 0.0   the flat direction

Why gradient descent zigzags

Now the payoff. Training a model means walking downhill on a loss surface. Near a minimum, every smooth loss looks like a quadratic, and the quadratic is written with a symmetric matrix, the matrix of second derivatives called the Hessian. So study the simplest possible loss, with SS symmetric and positive definite:

f(x)=12xTSx.f(x) = \tfrac12 \, x\T S x .

Its lowest point is x=0x = 0 and its gradient is SxSx. The level sets f(x)=cf(x) = c are ellipses, which Figure 7 already showed you: axes along the eigenvectors, and along qiq_i the ellipse reaches out to 2c/λi\sqrt{2c/\lambda_i}. A big eigenvalue means a tight, steeply curved direction. A small eigenvalue means a long, nearly flat one.

Gradient descent takes the step xxαSxx \leftarrow x - \alpha S x. Write xx in the eigenvector basis again and the update falls apart into nn separate scalar updates:

ci(1αλi)ci.c_i \leftarrow (1 - \alpha \lambda_i)\, c_i .

One number per direction, no interaction. That one line explains everything people say about learning rates. For the method to converge you need 1αλi<1|1 - \alpha\lambda_i| < 1 in every direction, so α<2/λmax\alpha < 2/\lambda_{\max}: the steepest direction sets the speed limit. But the error along the flattest direction shrinks by 1αλmin|1 - \alpha\lambda_{\min}|, which sits close to 1 when λmin\lambda_{\min} is small. The steep direction caps your step; the flat direction decides how many steps you take. The ratio

κ=λmaxλmin\kappa = \frac{\lambda_{\max}}{\lambda_{\min}}

is the condition number, and it is the number that matters. With the best possible constant step α=2/(λmin+λmax)\alpha = 2/(\lambda_{\min} + \lambda_{\max}), the error shrinks by a factor (κ1)/(κ+1)(\kappa - 1)/(\kappa + 1) each step, which is close to 1 whenever κ\kappa is large.

Two matrices make the point without changing anything else:

S1=[5115],S2=[5445].S_1 = \begin{bmatrix} 5 & -1 \\ -1 & 5 \end{bmatrix}, \qquad S_2 = \begin{bmatrix} 5 & -4 \\ -4 & 5 \end{bmatrix}.

Both are symmetric with the same eigenvectors, (1,1)/2(1,1)/\sqrt2 and (1,1)/2(1,-1)/\sqrt2. Both have trace 10, so both take the same best step α=0.2\alpha = 0.2. The only difference is the spread: S1S_1 has eigenvalues 4 and 6, so κ=1.5\kappa = 1.5, and S2S_2 has eigenvalues 1 and 9, so κ=9\kappa = 9.

Those eigenvalues are curvatures, and that is the whole of it. Slice the bowl along an eigenvector qq and you get a parabola: putting x=tqx = t q into ff leaves 12λt2\tfrac12 \lambda t^2, whose second derivative is λ\lambda itself. The eigenvalues of the Hessian are how hard the bowl bends in its two special directions.

λ = 9λ = 1ftthe bowl, cut two waysthe same two cuts, laid flat
Fig. 10 

The two eigenvalues of S2S_2, drawn as what they are. Cut the bowl along its steep eigenvector and the cut is the orange parabola 129t2\tfrac12 \cdot 9\,t^2. Cut along the shallow one and it is the green parabola 121t2\tfrac12 \cdot 1\,t^2. Both are drawn to one scale, so the nine to one you see is the nine to one in the eigenvalues.

Starting from (1,1.5)(1, 1.5) and running until the distance to the minimum is below 10610^{-6}, the nearly round bowl takes 9 steps and the stretched one takes 65.

nearly round: λ = 4 and 6, κ = 1.59 stepsstretched: λ = 1 and 9, κ = 965 stepsstartstart
Fig. 11 

Same starting point, same step size, same eigenvector directions. Only the eigenvalue spread differs. On the round bowl the gradient points nearly at the minimum. On the stretched one it points mostly across the valley, so the path bounces from wall to wall and inches forward.

The picture also shows what momentum fixes. The bouncing direction reverses sign every step, since 1αλmax1 - \alpha\lambda_{\max} is negative there, while the crawling direction keeps the same sign. Momentum averages the recent steps, so the reversals cancel each other and the steady ones add up. The update becomes vβvαSxv \leftarrow \beta v - \alpha S x then xx+vx \leftarrow x + v, and with tuned α\alpha and β\beta the convergence factor improves from (κ1)/(κ+1)(\kappa-1)/(\kappa+1) to about (κ1)/(κ+1)(\sqrt\kappa - 1)/(\sqrt\kappa + 1). On the stretched bowl that turns 65 steps into 25.

python
import numpy as np

round_bowl = np.array([[5.0, -1.0], [-1.0, 5.0]])   # eigenvalues 4 and 6
stretched  = np.array([[5.0, -4.0], [-4.0, 5.0]])   # eigenvalues 1 and 9
print(np.linalg.eigvalsh(round_bowl), np.linalg.eigvalsh(stretched))
# [4. 6.] [1. 9.]

def descend(S, step=0.2, beta=0.0, tol=1e-6):
    x = np.array([1.0, 1.5])
    v = np.zeros(2)
    n = 0
    while np.sqrt(x @ x) > tol:
        v = beta * v - step * (S @ x)   # beta = 0 is plain gradient descent
        x = x + v
        n += 1
    return n

print(descend(round_bowl))                        # 9
print(descend(stretched))                         # 65
print(descend(stretched, step=0.25, beta=0.25))   # 25  momentum

Momentum treats the symptom. Here is the cure. Gradient descent knows only the slope, so for a given steepness it takes the same step whether the valley is tight or wide. Newton’s method divides by the curvature: its step is H1g-H^{-1} g, and on a quadratic bowl that lands on the minimum from wherever you start, in one move. From (1,1.5)(1, 1.5) the gradient is (1,3.5)(-1, 3.5), so the gradient step goes to (1.2,0.8)(1.2, 0.8), over on the far side of the valley floor. The Newton step is H1g=(1,1.5)-H^{-1} g = (-1, -1.5), which lands on (0,0)(0, 0) exactly.

startgradient steplands across the valleyNewton steplands on the minimum
Fig. 12 

One start, two steps, on the stretched bowl of Figure 11. Blue is the gradient step: it leaves perpendicular to the contour and crosses the valley floor to land on the far side. Green is the Newton step H1g-H^{-1} g, which lands on the minimum. Lecture 0 ran Newton on a curve to hunt for a root; the tangent line has become a tangent bowl, and dividing by ff' has become multiplying by H1H^{-1}.

Where this is going

You now have the two facts that carry the rest of the course. Any symmetric matrix is a stretch along perpendicular axes, S=QΛQTS = Q\Lambda Q\T. Any matrix of the form ATAA\T A is symmetric with nonnegative eigenvalues.

Put them together on a data matrix. Center your data so each column has mean zero, and the covariance matrix is 1nATA\frac{1}{n} A\T A. It is symmetric, so it has perpendicular eigenvectors, and it is at least positive semidefinite, so its eigenvalues are all nonnegative. Those eigenvectors are the directions along which the data spreads out most, and the eigenvalues measure how much spread sits in each one. That is principal component analysis, and it is the first thing Part VI does.

The same trick applied to a matrix that is not square gives the singular value decomposition, and with that the course finishes what it started in Lecture 1.