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 week | to A | to B | to C |
|---|---|---|---|
| from A | 0.8 | 0.1 | 0.1 |
| from B | 0.2 | 0.7 | 0.1 |
| from C | 0.1 | 0.3 | 0.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 . 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 for (A, B, C). Next week, B's customers spread out along row B: .
Week 2 takes a little more work. Each shop's new share collects what flows in from all three shops:
So . 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:
Try three arrows:
- . The arrow turned: it points in a new direction.
- . Same direction, twice as long.
- . Exactly the same arrow.
Most arrows get knocked off their line. But and stay on theirs. The matrix only stretches them, by 2 and by 1.
Drag around and look for the directions where stays on 's line. Then try the other tabs.
A = 2101 · v = (1, 1) · Av = (3, 1)
Knocked off its line
An arrow that a matrix only stretches is an eigenvector of that matrix. The stretch factor is its eigenvalue. As an equation:
Read it back: doing the matrix to (left side) gives the same as multiplying by one number (right side). The Greek letter ("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 works, so does or . They all get stretched by 2.
- The eigenvalue can be negative. In the Mirror tab, comes out as . That's still on the same line, but flipped, so its eigenvalue is .
- The eigenvalue can be 1. In the A tab, 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.
A = 2101
Green: arrows that never leave their line (4 of 16). Blue: arrows that turn.
Notice that the green arrows stay on their lines in every frame, not only at the end. Each frame is . On an eigenvector, that gives , which is still on 's line. In the Mirror tab, for . So at that arrow shrinks to zero for a moment, then comes out flipped.
In the Symmetric tab, the green arrows lie on the lines through and , 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 as , which is . So the matrix sends a non-zero arrow to zero. It squashes a whole direction flat, so it must be singular:
Slide below and watch the grid of , for . At two values of the grid collapses onto a line.
A − λI = 2112
det = (2)² − 1 = 3
Worked example
Eigenvalues and eigenvectors of [[2, 1], [1, 2]]
Subtract from the diagonal: .
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 and by 1 along . 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 .
The determinant is exactly that area scale. Check: .
This works in general: the determinant is the product of the eigenvalues. For the first matrix, . 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 many times?
Applying to the result times is the matrix power . On an eigenvector, it's easy: each application multiplies by again.
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)
has eigenvector with eigenvalue 1, and with eigenvalue 0.5. Check the first: .
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 .
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.
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 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 among its eigenvalues. We use the size, ignoring sign, because stretches as much as does. It also flips.
It decides the long run:
| Spectral radius | Repeating the matrix… |
|---|---|
| less than 1 | shrinks every vector towards zero |
| exactly 1 | can settle to a steady vector |
| more than 1 | blows 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 has size . The quarter turn's eigenvalues are and , 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 , and one week multiplies it by the same matrix:
Call the long-run shares . If the shares stop changing, next week equals this week. So .
That's almost the eigenvector equation, but has a column vector to the right of the matrix. To switch, take the transpose of both sides. Transposing a product swaps the order: , where is the same shares written as a column. Writing as a column from here on:
So the long-run shares are an eigenvector of with eigenvalue 1.
Play the weeks forward from different starts.
Week 0: A 0.0% · B 100.0% · C 0.0%
Dashed: the long-run shares, 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
A collects .
Why every start settles
has three eigenvalues: 1, 0.6 and 0.5. Their eigenvectors are:
| Eigenvalue | Eigenvector | Note |
|---|---|---|
| 1 | the long-run shares | |
| 0.6 | entries add up to 0 | |
| 0.5 | 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:
Each week multiplies the second part by 0.6 and the third by 0.5:
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 adds up to 1, so . That makes 1 an eigenvalue of . A matrix and its transpose always have the same eigenvalues, because transposing doesn't change a determinant: . So 1 is an eigenvalue of 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 describes one customer's random choices, and 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, . The entry in row , column equals the one in row , column . For example, is symmetric, and 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 has eigenvectors and , at 45°. The symmetric one has and , 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 , like this chapter's table. Others write a column vector and use , 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 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.
" raises each entry to the power ." No. multiplies the whole matrix by itself times. For , , not .
Recap
- An eigenvector is a direction a matrix only stretches: . The stretch is its eigenvalue.
- Eigenvalues solve : the matrix squashes the eigenvector flat.
- Applying a matrix times multiplies each eigenvector part by , 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, , 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.