Maths toolkit5 / 17

Lesson 5 · Linear algebra

Directions that only stretch: eigenvectors and eigenvalues

The problem

A town has three pizza shops: A, B and C. Each week, some customers stay with their shop and some switch. This table says how they move:

This week → next weekto Ato Bto C
from A0.80.10.1
from B0.20.70.1
from C0.10.30.6

Read a row as "of this shop's customers, what share goes where next week". Of A's customers, 80% stay, 10% move to B and 10% move to C. Each row adds up to 1, because everyone goes somewhere.

Call this table the switching matrix TT. Rows are this week's shop, and columns are next week's shop.

Say shop B has opened with a big launch, and this week every customer is at B. The shares are s0=(0,1,0)\mathbf{s}_0 = (0, 1, 0) for (A, B, C). Next week, B's customers spread out along row B: s1=(0.2,0.7,0.1)\mathbf{s}_1 = (0.2, 0.7, 0.1).

Week 2 takes a little more work. Each shop's new share collects what flows in from all three shops:

A:0.2×0.8+0.7×0.2+0.1×0.1=0.31B:0.2×0.1+0.7×0.7+0.1×0.3=0.54C:0.2×0.1+0.7×0.1+0.1×0.6=0.15\begin{aligned} \text{A:}\quad & 0.2 \times 0.8 + 0.7 \times 0.2 + 0.1 \times 0.1 = 0.31 \\ \text{B:}\quad & 0.2 \times 0.1 + 0.7 \times 0.7 + 0.1 \times 0.3 = 0.54 \\ \text{C:}\quad & 0.2 \times 0.1 + 0.7 \times 0.1 + 0.1 \times 0.6 = 0.15 \end{aligned}

So s2=(0.31,0.54,0.15)\mathbf{s}_2 = (0.31, 0.54, 0.15). B is losing customers. Does that go on until B has none? Do the shares swing back and forth? Or do they settle?

To answer that, we'll start with a 2×2 picture and look for directions that a matrix only stretches.

Arrows that only stretch

Take the matrix from the last chapter:

A=[2101]A = \begin{bmatrix} 2 & 1 \\ 0 & 1 \end{bmatrix}

Try three arrows:

  • A(1,1)=(3,1)A(1, 1) = (3, 1). The arrow turned: it points in a new direction.
  • A(1,0)=(2,0)A(1, 0) = (2, 0). Same direction, twice as long.
  • A(1,−1)=(2−1, 0−1)=(1,−1)A(1, -1) = (2 - 1,\ 0 - 1) = (1, -1). Exactly the same arrow.

Most arrows get knocked off their line. But (1,0)(1, 0) and (1,−1)(1, -1) stay on theirs. The matrix only stretches them, by 2 and by 1.

Drag v\mathbf{v} around and look for the directions where AvA\mathbf{v} stays on v\mathbf{v}'s line. Then try the other tabs.

-4-4-2-22244Avv

A = 2101 · v = (1, 1) · Av = (3, 1)

Knocked off its line

Blue is v and orange is Av. When Av lies on v's line (pointing either way), the readout turns green and shows the stretch.

An arrow that a matrix only stretches is an eigenvector of that matrix. The stretch factor is its eigenvalue. As an equation:

Av=λvA\mathbf{v} = \lambda \mathbf{v}

Read it back: doing the matrix to v\mathbf{v} (left side) gives the same as multiplying v\mathbf{v} by one number λ\lambda (right side). The Greek letter λ\lambda ("lambda") is the usual name for an eigenvalue. "Eigen" is German for "own": each eigenvector is a direction the matrix keeps as its own.

A few things to notice from the figure:

  • An eigenvector is a direction. If (1,0)(1, 0) works, so does (3,0)(3, 0) or (−2,0)(-2, 0). They all get stretched by 2.
  • The eigenvalue can be negative. In the Mirror tab, (1,−1)(1, -1) comes out as (−1,1)(-1, 1). That's still on the same line, but flipped, so its eigenvalue is −1-1.
  • The eigenvalue can be 1. In the A tab, (1,−1)(1, -1) doesn't move at all. That counts as a stretch by 1.

Predict first

In the Rotate tab, the matrix turns everything by a quarter turn. How many directions does it only stretch?

Every arrow at once

The figure below draws 16 arrows around a circle and transforms them all together. Press play.

-2-222
Frame 0 of 12

A = 2101

Green: arrows that never leave their line (4 of 16). Blue: arrows that turn.

Sixteen arrows, transformed together. Green arrows stay on their own line the whole way; blue ones turn.

Notice that the green arrows stay on their lines in every frame, not only at the end. Each frame is (1−t) I+t A(1 - t)\,I + t\,A. On an eigenvector, that gives ((1−t)+tλ)v\big((1 - t) + t\lambda\big)\mathbf{v}, which is still on v\mathbf{v}'s line. In the Mirror tab, λ=−1\lambda = -1 for (1,−1)(1, -1). So at t=12t = \tfrac12 that arrow shrinks to zero for a moment, then comes out flipped.

In the Symmetric tab, the green arrows lie on the lines through (1,1)(1, 1) and (1,−1)(1, -1), at right angles. We'll come back to that.

Finding them by hand

How do you find eigenvalues without dragging? Use the last chapter's idea of a singular matrix.

Rewrite Av=λvA\mathbf{v} = \lambda\mathbf{v} as Av−λIv=0A\mathbf{v} - \lambda I \mathbf{v} = \mathbf{0}, which is (A−λI)v=0(A - \lambda I)\mathbf{v} = \mathbf{0}. So the matrix A−λIA - \lambda I sends a non-zero arrow to zero. It squashes a whole direction flat, so it must be singular:

det⁡(A−λI)=0\det(A - \lambda I) = 0

Slide λ\lambda below and watch the grid of A−λIA - \lambda I, for A=[2112]A = \begin{bmatrix} 2 & 1 \\ 1 & 2 \end{bmatrix}. At two values of λ\lambda the grid collapses onto a line.

-2-22200.511.522.533.54−10123λdet(A − λI)

A − λI = 2112

det = (2)² − 1 = 3

The grid of A − λI. When its determinant hits 0, the plane goes flat, and the green arrow marks the direction sent to zero: an eigenvector.

Worked example

Eigenvalues and eigenvectors of [[2, 1], [1, 2]]

  1. Subtract λ\lambda from the diagonal: A−λI=[2−λ112−λ]A - \lambda I = \begin{bmatrix} 2 - \lambda & 1 \\ 1 & 2 - \lambda \end{bmatrix}.

For a 2×2 matrix this is a quadratic equation. For bigger matrices, nobody does it by hand. You let a computer do it, or use the next idea.

DeeperEigenvalues and the determinantMultiply the eigenvalues and you get the determinant. The area picture says why. · 2 min

Take the symmetric matrix above. It stretches by 3 along (1,1)(1, 1) and by 1 along (1,−1)(1, -1). Draw a small square with sides along those two directions. After the matrix, it's a rectangle 3 times as long one way and the same the other way. Its area went up by 3×1=33 \times 1 = 3.

The determinant is exactly that area scale. Check: det⁡A=2×2−1×1=3\det A = 2 \times 2 - 1 \times 1 = 3.

This works in general: the determinant is the product of the eigenvalues. For the first matrix, det⁡[2101]=2=2×1\det \begin{bmatrix} 2 & 1 \\ 0 & 1 \end{bmatrix} = 2 = 2 \times 1. A matrix with an eigenvalue of 0 squashes a direction flat, so its determinant is 0.

Applying a matrix again and again

The pizza question is about applying the same matrix week after week. So what happens when you apply AA many times?

Applying AA to the result kk times is the matrix power Ak=A⋅A⋯AA^k = A \cdot A \cdots A. On an eigenvector, it's easy: each application multiplies by λ\lambda again.

Akv=λkvA^k \mathbf{v} = \lambda^k \mathbf{v}

Most arrows aren't eigenvectors. But in 2D, any arrow is a mix of the two eigenvector directions (when the matrix has two). Each part gets its own stretch, every step.

Worked example

Repeat M = [[0.8, 0.3], [0.2, 0.7]] on v = (−2, 1)

  1. MM has eigenvector (3,2)(3, 2) with eigenvalue 1, and (1,−1)(1, -1) with eigenvalue 0.5. Check the first: M(3,2)=(2.4+0.6, 0.6+1.4)=(3,2)M(3, 2) = (2.4 + 0.6,\ 0.6 + 1.4) = (3, 2).

The part with the biggest stretch takes over. All the others fade in comparison. That's the main fact of this chapter.

Predict first

A matrix's biggest stretch is 0.9 (and every other eigenvalue is smaller). What happens to any vector after 100 applications?

Power iteration

This gives a way to find the biggest-stretch direction for any matrix. Start with almost any arrow. Apply the matrix, then divide by the length so the arrow stays a sensible size. Repeat. The arrow swings onto the direction with the biggest stretch.

This is power iteration. The rescaling only changes the length, never the direction, so the direction ends up the same as with AkA^k.

-4-4-2-22244step 0
Step 0 of 20

A = 0.80.30.20.7 stretches by 1 along the green line, through (3, 2), and by 0.5 along (1, −1).

Step 0: arrow (−2, 1)

Drag the arrow to choose where to start, then press play. Rescaling hides the length, so the biggest stretch only has a slider in the other tab.

Press play. The green line is the biggest-stretch direction, and the dashed blue arrows are earlier steps. Change the smaller stretch to see how fast the arrow lands. Switch to 'Keep the length' to watch the length shrink, settle or explode.

Notice three things:

  • With rescaling, the arrow lands on the green line within a few steps.
  • The closer the two stretches are, the longer it takes. Each step multiplies the leftover part by (smaller stretch) ÷ (biggest stretch). At 0.5 ÷ 1 it halves every step. At 0.8 ÷ 1 it loses only a fifth each step. Try the smaller-stretch slider.
  • Without rescaling, the length depends on the biggest stretch, which has its own slider in that tab. At 0.9 it shrinks to nothing. At 1.1 it dips at first, then keeps growing. At 1.0 it settles.

One catch: the start must have some of the biggest-stretch direction in it. Start exactly on the line through (1,−1)(1, -1) and the arrow never leaves it. In practice, a random start almost never lands exactly on such a line.

DeeperSpectral radius: the one number that decides settle or explodeThe biggest eigenvalue size tells you whether repeating a matrix shrinks, settles or blows up. · 3 min

The figure above has one dial: the biggest stretch. That number has a name. The spectral radius of a matrix is the largest size ∣λ∣|\lambda| among its eigenvalues. We use the size, ignoring sign, because −2-2 stretches as much as 22 does. It also flips.

It decides the long run:

Spectral radiusRepeating the matrix…
less than 1shrinks every vector towards zero
exactly 1can settle to a steady vector
more than 1blows up almost every vector

You'll meet this table again. In reinforcement learning, future rewards get multiplied by a discount factor below 1 at every step, and that's what makes the sums settle. In deep networks, a signal passes through many layers. If each layer shrinks it, you get vanishing gradients. If each layer grows it, you get exploding gradients.

Two caveats:

  • For a rotation, the eigenvalues are complex numbers. A complex number a+bia + bi has size a2+b2\sqrt{a^2 + b^2}. The quarter turn's eigenvalues are ii and −i-i, and both have size 1. So a rotation has spectral radius 1, and it neither shrinks nor grows arrows.
  • The table describes the long run. A matrix that isn't symmetric can make some vectors grow for a few steps before they shrink, even with spectral radius below 1.

Back to the pizza shops

Now apply this to the shops. The shares are a row vector s\mathbf{s}, and one week multiplies it by the same matrix:

snext=s T\mathbf{s}_{\text{next}} = \mathbf{s}\,T

Call the long-run shares d\mathbf{d}. If the shares stop changing, next week equals this week. So d T=d\mathbf{d}\,T = \mathbf{d}.

That's almost the eigenvector equation, but Av=λvA\mathbf{v} = \lambda\mathbf{v} has a column vector to the right of the matrix. To switch, take the transpose of both sides. Transposing a product swaps the order: (d T)⊤=T⊤d⊤(\mathbf{d}\,T)^\top = T^\top \mathbf{d}^\top, where d⊤\mathbf{d}^\top is the same shares written as a column. Writing d\mathbf{d} as a column from here on:

T⊤d=1⋅dT^\top \mathbf{d} = 1 \cdot \mathbf{d}

So the long-run shares are an eigenvector of T⊤T^\top with eigenvalue 1.

Play the weeks forward from different starts.

Week 0 of 30
05101520253000.20.40.60.81weekshare

Week 0: A 0.0% · B 100.0% · C 0.0%

Dashed: the long-run shares, A 45% · B 35% · C 20%.

Bars show this week's shares; dashed marks show the long-run shares. Every start ends up at A 45%, B 35%, C 20%.

Predict first

Instead of everyone at B, start with an even split: a third at each shop. Where do the shares end up?

Worked example

Check that d = (0.45, 0.35, 0.20) stays put

  1. A collects 0.45×0.8+0.35×0.2+0.20×0.1=0.36+0.07+0.02=0.450.45 \times 0.8 + 0.35 \times 0.2 + 0.20 \times 0.1 = 0.36 + 0.07 + 0.02 = 0.45.

Why every start settles

T⊤T^\top has three eigenvalues: 1, 0.6 and 0.5. Their eigenvectors are:

EigenvalueEigenvectorNote
1(0.45,0.35,0.20)(0.45, 0.35, 0.20)the long-run shares
0.6(−1,1,0)(-1, 1, 0)entries add up to 0
0.5(1,−2,1)(1, -2, 1)entries add up to 0

Write the start as a mix of these three, exactly as in the 2×2 example. Everyone at B is:

(0,1,0)=(0.45,0.35,0.20)+0.25⋅(−1,1,0)−0.2⋅(1,−2,1)(0, 1, 0) = (0.45, 0.35, 0.20) + 0.25 \cdot (-1, 1, 0) - 0.2 \cdot (1, -2, 1)

Each week multiplies the second part by 0.6 and the third by 0.5:

sk=(0.45,0.35,0.20)+0.25⋅0.6k⋅(−1,1,0)−0.2⋅0.5k⋅(1,−2,1)\mathbf{s}_k = (0.45, 0.35, 0.20) + 0.25 \cdot 0.6^k \cdot (-1, 1, 0) - 0.2 \cdot 0.5^k \cdot (1, -2, 1)

The extra parts add up to zero, so the shares always total 1. And they fade every week. The slower one shrinks by 0.6 per week, so the second eigenvalue sets the speed. By week 10, every share is within 0.2 percentage points of its long-run value. By week 30, it's within 0.00001 percentage points.

Switch the figure's plot to "Gap (log scale)". On a log scale, a gap that shrinks by 0.6 every week falls along a straight line, and that's what the dots do.

Why is there an eigenvalue of exactly 1? Each row of TT adds up to 1, so T(1,1,1)=(1,1,1)T(1, 1, 1) = (1, 1, 1). That makes 1 an eigenvalue of TT. A matrix and its transpose always have the same eigenvalues, because transposing doesn't change a determinant: det⁡(T⊤−λI)=det⁡(T−λI)\det(T^\top - \lambda I) = \det(T - \lambda I). So 1 is an eigenvalue of T⊤T^\top too.

No eigenvalue is bigger than 1. A part with a bigger stretch would grow without limit, but shares stay between 0 and 1.

Coming back in Markov chains

Here the shares describe a whole town. In Markov chains, the same matrix TT describes one customer's random choices, and d\mathbf{d} becomes the stationary distribution. The maths is the same.

Symmetric matrices

In the Symmetric tab, the two eigenvectors were at right angles. That always happens for matrices like this one.

A symmetric matrix is one that equals its own transpose, S=S⊤S = S^\top. The entry in row ii, column jj equals the one in row jj, column ii. For example, [2112]\begin{bmatrix} 2 & 1 \\ 1 & 2 \end{bmatrix} is symmetric, and [2101]\begin{bmatrix} 2 & 1 \\ 0 & 1 \end{bmatrix} isn't.

Symmetric matrices have two useful properties:

  • their eigenvalues are always real, never complex
  • their eigenvectors can always be chosen at right angles to each other

So a symmetric matrix is a pure stretch along perpendicular axes, with no twist. Compare the A and Symmetric tabs. The matrix AA has eigenvectors (1,0)(1, 0) and (1,−1)(1, -1), at 45°. The symmetric one has (1,1)(1, 1) and (1,−1)(1, -1), at 90°.

Symmetric matrices come up all the time in ML. The directions in which a cloud of data spreads out most are the eigenvectors of a symmetric matrix built from the data. That idea is principal component analysis.

Rows or columns?

Some books write shares as a row vector and use s T\mathbf{s}\,T, like this chapter's table. Others write a column vector and use T⊤sT^\top\mathbf{s}, or define the matrix the other way round. Before using any switching matrix, check which index is "from" and which is "to". Here, rows are "from" and each row adds up to 1.

DigressionPageRank: the web as a giant switching matrixOne of Google's early ranking signals was power iteration on the pizza-shop model, with web pages as shops. · 2 min

Swap the pizza shops for web pages. At each step, a reader clicks a random link on the page they're on. That's a switching matrix with one row per page: millions of rows then, billions now. The long-run share of readers on each page is a measure of how important the page is.

This is the idea behind PageRank, one of the signals Google's founders described in 1998. One extra rule keeps it well-behaved: now and then, the reader jumps to a completely random page. That stops readers getting stuck in corners of the web.

A matrix that big is far too large to solve det⁡(A−λI)=0\det(A - \lambda I) = 0 directly. But power iteration needs only matrix-vector products, one per step, so it still works at that size.

Common misconceptions

"An eigenvector is one specific arrow." It's a direction. Any non-zero multiple works with the same eigenvalue.

"Every matrix has real eigenvectors." Not in general. A rotation turns every arrow, so its eigenvalues are complex. Symmetric matrices always have real ones.

"An eigenvalue of 0 means no eigenvector." It means the matrix squashes that direction to zero. That's still an eigenvector, and the matrix is singular.

"AkA^k raises each entry to the power kk." No. AkA^k multiplies the whole matrix by itself kk times. For A=[2101]A = \begin{bmatrix} 2 & 1 \\ 0 & 1 \end{bmatrix}, A2=[4301]A^2 = \begin{bmatrix} 4 & 3 \\ 0 & 1 \end{bmatrix}, not [4101]\begin{bmatrix} 4 & 1 \\ 0 & 1 \end{bmatrix}.

Recap

  • An eigenvector is a direction a matrix only stretches: Av=λvA\mathbf{v} = \lambda\mathbf{v}. The stretch λ\lambda is its eigenvalue.
  • Eigenvalues solve det⁡(A−λI)=0\det(A - \lambda I) = 0: the matrix A−λIA - \lambda I squashes the eigenvector flat.
  • Applying a matrix kk times multiplies each eigenvector part by λk\lambda^k, so the biggest stretch takes over.
  • Power iteration finds that biggest-stretch direction by applying and rescaling.
  • The pizza shares settle to the eigenvector with eigenvalue 1, (0.45,0.35,0.20)(0.45, 0.35, 0.20), from any start. The second eigenvalue, 0.6, sets how fast.
  • Symmetric matrices have real eigenvalues and perpendicular eigenvectors.

Section 2 shows how to compute these in PyTorch.