Skip to content
AriadneTechnology

The Inner Ring · Chamber 8 of 9

Eigenvectors and Eigenvalues

The directions a matrix only stretches: characteristic polynomials, power iteration and PageRank.

45 min 60 XP + 12 questions + 1 challengeMathVideoPapersProofsCodeLab

In this chamber you will

  • Find eigenvalues and eigenvectors of small matrices
  • Diagonalise a matrix and compute its powers
  • Run power iteration by hand and in code
  • Read PageRank as an eigenvector problem
DiscoverLearnRead beyondPapers & lecturesYour turn

Discover: a definition that chases its own tail

In 1998 two Stanford PhD students, Sergey Brin and Lawrence Page, described a prototype search engine called Google. One of its key ideas was a way to score every page on the web by importance, and it fits on one line:

Spotted in the wild

PR(A)=(1−d)+d(PR(T1)C(T1)+⋯+PR(Tn)C(Tn))PR(A) = (1-d) + d\left(\frac{PR(T_1)}{C(T_1)} + \cdots + \frac{PR(T_n)}{C(T_n)}\right)
Brin & Page (1998), “The Anatomy of a Large-Scale Hypertextual Web Search Engine”, Section 2.1.1

Read it aloud: “the PageRank of page AA is one minus dd, plus dd times the sum, over the pages T1T_1 to TnT_n that link to AA, of each one's PageRank divided by its number of outgoing links.” Here C(T)C(T) counts the links going out of page TT, and dd is a “damping factor” the paper usually sets to 0.850.85. Each page splits its own importance equally among the pages it links to, and a page is important if important pages link to it.

Now look again. To compute PR(A)PR(A) you need PR(T1)PR(T_1). But PR(T1)PR(T_1) is defined by the same formula, using the pages that link to it, and one of those might well be AA. The definition chases its own tail. How can a number be defined in terms of itself, and how would you ever compute it?

The answer is one of the most useful ideas in linear algebra. Collect all the PageRanks into a single vector: the formula says that a certain matrix leaves that vector exactly where it is. A vector that a matrix doesn't turn is called an eigenvector, and by the end of this chamber you'll compute PageRank yourself.

Quick check +20 XP

In Brin and Page's formula, a page TT with PR(T)=0.9PR(T) = 0.9 has C(T)=3C(T) = 3 outgoing links, one of which points to page AA. With d=0.85d = 0.85, how much does TT contribute to PR(A)PR(A)?

DiscoverLearnRead beyondPapers & lecturesYour turn

Learn: the directions a matrix only stretches

Chamber 4 taught you to see a matrix as a machine that moves space. Feed it a vector and, almost always, the output points somewhere new. But for most matrices a few special directions are merely stretched: the output lies on the same line through the origin as the input.

Av=λv,v≠0.A\mathbf{v} = \lambda\mathbf{v}, \qquad \mathbf{v} \ne \mathbf{0}.

Read it as “A v equals lambda v”. A non-zero vector v\mathbf{v} with this property is an eigenvector of AA, and the number λ\lambda (lambda) is its eigenvalue: the factor by which AA stretches that direction. The zero vector is excluded because A0=λ0A\mathbf{0} = \lambda\mathbf{0} for every λ\lambda, which tells you nothing.

The size and sign of λ\lambda tell the story. With λ=3\lambda = 3 the eigenvector triples in length. With λ=12\lambda = \tfrac{1}{2} it halves. With λ=−1\lambda = -1 it flips to point the other way, still on its line. With λ=0\lambda = 0 it is flattened to 0\mathbf{0}, so it lies in the null space.

You have met eigenvectors already without the name:

  • A diagonal matrix such as [2000.5]\begin{bmatrix} 2 & 0 \\ 0 & 0.5 \end{bmatrix} stretches the xx-axis by 2 and squashes the yy-axis by half. Its eigenvectors are e1\mathbf{e}_1 (with λ=2\lambda = 2) and e2\mathbf{e}_2 (with λ=0.5\lambda = 0.5).
  • A projection PP onto a line (Chamber 7) leaves vectors on the line alone (λ=1\lambda = 1) and sends the perpendicular direction to zero (λ=0\lambda = 0).
  • A reflection across a line keeps the line (λ=1\lambda = 1) and flips the perpendicular direction (λ=−1\lambda = -1).
  • A rotation by 90°90° turns every direction. It has no real eigenvectors at all.

One more fact before you go hunting. If v\mathbf{v} is an eigenvector, so is any non-zero multiple cvc\mathbf{v}, because A(cv)=cAv=λ(cv)A(c\mathbf{v}) = cA\mathbf{v} = \lambda(c\mathbf{v}). Eigenvectors come in whole lines through the origin, so we really look for eigen-directions.

Eigen notation
  • Av=λvA\mathbf{v} = \lambda\mathbf{v}“A v equals lambda v”
    The eigenvalue equation: AA only stretches v\mathbf{v}, by the factor λ\lambda, and never turns it off its line. A non-zero v\mathbf{v} that satisfies it is an eigenvector, and λ\lambda is its eigenvalue.
  • λ\lambda“lambda”
    An eigenvalue: the stretch factor. ∣λ∣>1|\lambda| > 1 stretches, ∣λ∣<1|\lambda| < 1 shrinks, a negative λ\lambda flips, and λ=0\lambda = 0 flattens the vector to 0\mathbf{0}.
    λ1=3, λ2=1\lambda_1 = 3,\ \lambda_2 = 1
  • det⁡(A−λI)=0\det(A - \lambda I) = 0“det of A minus lambda I equals zero”
    The characteristic equation. Its solutions are exactly the eigenvalues of AA.
  • p(λ)p(\lambda)“p of lambda”
    The characteristic polynomial det⁡(A−λI)\det(A - \lambda I), of degree nn for an n×nn \times n matrix. For 2×22 \times 2 matrices it is λ2−tr⁡(A) λ+det⁡A\lambda^2 - \operatorname{tr}(A)\,\lambda + \det A.
    p(λ)=λ2−4λ+3p(\lambda) = \lambda^2 - 4\lambda + 3
  • tr⁡(A)\operatorname{tr}(A)“trace of A”
    The trace: the sum of the diagonal entries. It always equals the sum of the eigenvalues.
    tr⁡[2112]=4\operatorname{tr}\begin{bmatrix} 2 & 1 \\ 1 & 2 \end{bmatrix} = 4
  • x⊤Axx⊤x\frac{\mathbf{x}^\top A\mathbf{x}}{\mathbf{x}^\top\mathbf{x}}“the Rayleigh quotient of x”
    Exactly λ\lambda when x\mathbf{x} is an eigenvector, and a good estimate of it when x\mathbf{x} is close to one. Power iteration uses it to read off the eigenvalue.
Quick check +20 XP

Suppose Av=3vA\mathbf{v} = 3\mathbf{v}. What is A(−2v)A(-2\mathbf{v})?

Now find some by eye. Turn the unit vector x\mathbf{x} around the circle and watch where AA sends it.

Interactive lab

Eigenvector hunt

The gold arrow is a unit vector x; the teal arrow is Ax. Most directions get knocked off their line. Find the ones that don't: when Ax lies along x (either way), record the direction and read off λ.
−22x on the unit circle · Ax · A(circle)

Turn x around the circle. Watch for the moments when Ax lines up with x.

A =
2000.5

x

(0.94, 0.34)

Ax

(1.88, 0.17)

Angle between the lines of x and Ax

14.8°

Under 1° counts as parallel.

Stretch· ·0/2
Symmetric· ·0/2
Triangular· ·0/2
Challenge: Eigen hunterFind every eigenvector direction of three matrices.+50 XP

A few things to notice. The stretch matrix's eigenvectors are the axes, as expected. The symmetric matrix [2112]\begin{bmatrix} 2 & 1 \\ 1 & 2 \end{bmatrix} has eigen-directions at 45°45° and 135°135°, which meet at a right angle. The triangular matrix's two directions don't: they meet at about 72°72°. And one of its eigenvalues is negative, so there AxA\mathbf{x} points straight back the way x\mathbf{x} came. With Show A(circle) on, you can see each eigen-direction pointing to where the ellipse crosses it, at distance ∣λ∣|\lambda| from the origin.

Learn: finding eigenvalues with the determinant

Hunting by eye works in two dimensions. For anything bigger we need algebra, and the derivation takes four steps, each resting on an earlier chamber.

  1. 1

    Move everything to one side

    Av=λvA\mathbf{v} = \lambda\mathbf{v} is the same as Av−λIv=0A\mathbf{v} - \lambda I\mathbf{v} = \mathbf{0}, that is, (A−λI)v=0(A - \lambda I)\mathbf{v} = \mathbf{0}. We slip in the identity II because we can't subtract a number from a matrix, but λI\lambda I is a matrix.

  2. 2

    Read it as a null space

    We need a non-zero v\mathbf{v} with (A−λI)v=0(A - \lambda I)\mathbf{v} = \mathbf{0}: the matrix A−λIA - \lambda I must have a non-trivial null space (Chamber 6).

  3. 3

    Translate to singular

    A square matrix has a non-trivial null space exactly when it is singular, not invertible (Chamber 5).

  4. 4

    Translate to a determinant

    A square matrix is singular exactly when its determinant is zero (Chamber 5). So λ\lambda is an eigenvalue of AA if and only if det⁡(A−λI)=0\det(A - \lambda I) = 0.

This is the characteristic equation, and p(λ)=det⁡(A−λI)p(\lambda) = \det(A - \lambda I) (read “p of lambda”) is the characteristic polynomial. For a 2×22 \times 2 matrix it is worth expanding once and for all:

det⁡[a−λbcd−λ]=(a−λ)(d−λ)−bc=λ2−(a+d)λ+(ad−bc).\det\begin{bmatrix} a - \lambda & b \\ c & d - \lambda \end{bmatrix} = (a - \lambda)(d - \lambda) - bc = \lambda^2 - (a + d)\lambda + (ad - bc).

The coefficient a+da + d is the trace tr⁡(A)\operatorname{tr}(A) (read “trace of A”), the sum of the diagonal entries, and ad−bcad - bc is the determinant. So p(λ)=λ2−tr⁡(A) λ+det⁡Ap(\lambda) = \lambda^2 - \operatorname{tr}(A)\,\lambda + \det A. If the roots are λ1\lambda_1 and λ2\lambda_2, then p(λ)=(λ−λ1)(λ−λ2)=λ2−(λ1+λ2)λ+λ1λ2p(\lambda) = (\lambda - \lambda_1)(\lambda - \lambda_2) = \lambda^2 - (\lambda_1 + \lambda_2)\lambda + \lambda_1\lambda_2, and matching coefficients gives two free checks:

λ1+λ2=tr⁡(A),λ1λ2=det⁡A.\lambda_1 + \lambda_2 = \operatorname{tr}(A), \qquad \lambda_1\lambda_2 = \det A.

A worked example with the lab's symmetric matrix S=[2112]S = \begin{bmatrix} 2 & 1 \\ 1 & 2 \end{bmatrix}. The trace is 4 and the determinant is 3, so p(λ)=λ2−4λ+3=(λ−3)(λ−1)p(\lambda) = \lambda^2 - 4\lambda + 3 = (\lambda - 3)(\lambda - 1) and the eigenvalues are 3 and 1. For each one, find the null space of S−λIS - \lambda I:

S−3I=[−111−1]⇒v1=(1,1),S−I=[1111]⇒v2=(1,−1).S - 3I = \begin{bmatrix} -1 & 1 \\ 1 & -1 \end{bmatrix} \Rightarrow \mathbf{v}_1 = (1, 1), \qquad S - I = \begin{bmatrix} 1 & 1 \\ 1 & 1 \end{bmatrix} \Rightarrow \mathbf{v}_2 = (1, -1).

Those are the 45°45° and 135°135° lines you found in the lab. For the triangular matrix T=[210−1]T = \begin{bmatrix} 2 & 1 \\ 0 & -1 \end{bmatrix} it's even quicker. T−λIT - \lambda I is triangular, and a triangular determinant is the product of its diagonal (Chamber 5), so p(λ)=(2−λ)(−1−λ)p(\lambda) = (2 - \lambda)(-1 - \lambda) and the eigenvalues are simply the diagonal entries, 2 and −1-1. For λ=−1\lambda = -1, T+I=[3100]T + I = \begin{bmatrix} 3 & 1 \\ 0 & 0 \end{bmatrix} gives v2=(1,−3)\mathbf{v}_2 = (1, -3).

And the rotation R=[0−110]R = \begin{bmatrix} 0 & -1 \\ 1 & 0 \end{bmatrix}? Its trace is 0 and its determinant is 1, so p(λ)=λ2+1p(\lambda) = \lambda^2 + 1, which has no real roots. The algebra agrees with the picture: a quarter turn leaves no direction on its own line.

Quick check +20 XP

The matrix A=[4123]A = \begin{bmatrix} 4 & 1 \\ 2 & 3 \end{bmatrix} has two real eigenvalues. What is the larger one?

Learn: diagonalisation and matrix powers

Put the eigen-equations for all nn eigenvectors side by side. Stack the eigenvectors as the columns of a matrix VV and the eigenvalues on the diagonal of Λ\Lambda (capital lambda). Column ii of AVAV is Avi=λiviA\mathbf{v}_i = \lambda_i\mathbf{v}_i, which is also column ii of VΛV\Lambda. So AV=VΛAV = V\Lambda, and when VV is invertible:

A=VΛV−1.A = V\Lambda V^{-1}.

Read it right to left, as a journey. V−1V^{-1} translates a vector into eigen-coordinates. Λ\Lambda stretches each coordinate by its own eigenvalue, independently. VV translates back. In the right coordinates, every diagonalisable matrix is just a stretch.

The payoff is powers. Multiply two copies and the middle cancels:

A2=VΛV−1 VΛV−1=VΛ2V−1,and in generalAk=VΛkV−1.A^2 = V\Lambda V^{-1}\,V\Lambda V^{-1} = V\Lambda^2 V^{-1}, \qquad\text{and in general}\qquad A^k = V\Lambda^k V^{-1}.

Powering a diagonal matrix just powers its diagonal entries. Applying AA a hundred times costs no more than raising nn numbers to the hundredth power.

When is VV invertible? When the eigenvectors are independent, and there's a clean guarantee. Here is the proof for two eigenvectors; the same idea extends to any number.

Claim. If Av1=λ1v1A\mathbf{v}_1 = \lambda_1\mathbf{v}_1 and Av2=λ2v2A\mathbf{v}_2 = \lambda_2\mathbf{v}_2, with v1,v2≠0\mathbf{v}_1, \mathbf{v}_2 \ne \mathbf{0} and λ1≠λ2\lambda_1 \ne \lambda_2, then v1\mathbf{v}_1 and v2\mathbf{v}_2 are linearly independent.

Proof. Suppose c1v1+c2v2=0c_1\mathbf{v}_1 + c_2\mathbf{v}_2 = \mathbf{0}. We must show c1=c2=0c_1 = c_2 = 0. Apply AA to both sides. By linearity and the eigen-equations,

c1λ1v1+c2λ2v2=0.c_1\lambda_1\mathbf{v}_1 + c_2\lambda_2\mathbf{v}_2 = \mathbf{0}.

Multiply the original equation by λ2\lambda_2 instead, giving c1λ2v1+c2λ2v2=0c_1\lambda_2\mathbf{v}_1 + c_2\lambda_2\mathbf{v}_2 = \mathbf{0}, and subtract it from the line above. The v2\mathbf{v}_2 terms cancel:

c1(λ1−λ2) v1=0.c_1(\lambda_1 - \lambda_2)\,\mathbf{v}_1 = \mathbf{0}.

Here λ1−λ2≠0\lambda_1 - \lambda_2 \ne 0 and v1≠0\mathbf{v}_1 \ne \mathbf{0}, so c1=0c_1 = 0. Then the original equation reads c2v2=0c_2\mathbf{v}_2 = \mathbf{0} with v2≠0\mathbf{v}_2 \ne \mathbf{0}, so c2=0c_2 = 0 too. ■\blacksquare

The trick is worth remembering: apply AA to create a second equation, then combine the two to eliminate one eigenvector. So an n×nn \times n matrix with nn distinct eigenvalues is always diagonalisable. With repeated eigenvalues it may not be: the shear [1101]\begin{bmatrix} 1 & 1 \\ 0 & 1 \end{bmatrix} has only one eigen-direction, and no VV exists.

Diagonalisation, powers and chains
  • Λ\Lambda“capital lambda”
    The diagonal matrix of eigenvalues, Λ=diag⁡(λ1,…,λn)\Lambda = \operatorname{diag}(\lambda_1, \ldots, \lambda_n).
    Λ=[3001]\Lambda = \begin{bmatrix} 3 & 0 \\ 0 & 1 \end{bmatrix}
  • A=VΛV−1A = V\Lambda V^{-1}“A equals V capital lambda V inverse”
    Diagonalisation. The columns of VV are eigenvectors of AA. Reading right to left: change to eigen-coordinates (V−1V^{-1}), stretch each coordinate (Λ\Lambda), change back (VV).
  • Ak=VΛkV−1A^k = V\Lambda^k V^{-1}“A to the k equals V capital lambda to the k V inverse”
    Powers of a diagonalisable matrix: the eigenvectors stay put and only the eigenvalues are raised to the kk-th power.
  • xk+1=Axk∥Axk∥\mathbf{x}_{k+1} = \frac{A\mathbf{x}_k}{\|A\mathbf{x}_k\|}“x k plus one equals A x k over the norm of A x k”
    Power iteration: multiply by AA and rescale, over and over. The direction converges to the eigenvector whose eigenvalue is largest in absolute value.
  • λ1\lambda_1“lambda one”
    Usually the dominant eigenvalue, the one largest in absolute value, when eigenvalues are listed by size. It controls the long-run growth of AkxA^k\mathbf{x}.
  • φ\varphi“phi”
    The golden ratio 1+52≈1.618\frac{1 + \sqrt{5}}{2} \approx 1.618: the dominant eigenvalue of the Fibonacci matrix.
    φ=1+52\varphi = \tfrac{1 + \sqrt{5}}{2}
  • Pπ=πP\boldsymbol{\pi} = \boldsymbol{\pi}“P pi equals pi”
    π\boldsymbol{\pi} (bold pi) is a stationary distribution of a Markov chain with transition matrix PP: an eigenvector with eigenvalue 1 whose entries are probabilities.

Fibonacci from eigenvalues

Here is diagonalisation doing something that looks like magic. The Fibonacci numbers 1,1,2,3,5,8,13,…1, 1, 2, 3, 5, 8, 13, \ldots obey Fn+1=Fn+Fn−1F_{n+1} = F_n + F_{n-1}. Write one step as a matrix acting on a pair:

[Fn+1Fn]=[1110]⏟F[FnFn−1],so[Fn+1Fn]=Fn[10]\begin{bmatrix} F_{n+1} \\ F_n \end{bmatrix} = \underbrace{\begin{bmatrix} 1 & 1 \\ 1 & 0 \end{bmatrix}}_{F}\begin{bmatrix} F_n \\ F_{n-1} \end{bmatrix}, \qquad\text{so}\qquad \begin{bmatrix} F_{n+1} \\ F_n \end{bmatrix} = F^n\begin{bmatrix} 1 \\ 0 \end{bmatrix}

(starting from F1=1F_1 = 1, F0=0F_0 = 0). The trace is 1 and the determinant is −1-1, so p(λ)=λ2−λ−1p(\lambda) = \lambda^2 - \lambda - 1, with roots

φ=1+52≈1.618,ψ=1−52≈−0.618.\varphi = \frac{1 + \sqrt{5}}{2} \approx 1.618, \qquad \psi = \frac{1 - \sqrt{5}}{2} \approx -0.618.

That's φ\varphi (phi), the golden ratio, and its partner ψ\psi (psi). Since λ2=λ+1\lambda^2 = \lambda + 1 for both roots, you can check that (λ,1)(\lambda, 1) is an eigenvector: F(λ,1)=(λ+1,λ)=(λ2,λ)=λ(λ,1)F(\lambda, 1) = (\lambda + 1, \lambda) = (\lambda^2, \lambda) = \lambda(\lambda, 1). Now write the starting vector in eigen-coordinates. The difference of the two eigenvectors is (φ−ψ,0)=(5,0)(\varphi - \psi, 0) = (\sqrt{5}, 0), so

[10]=15([φ1]−[ψ1]).\begin{bmatrix} 1 \\ 0 \end{bmatrix} = \frac{1}{\sqrt{5}}\left(\begin{bmatrix} \varphi \\ 1 \end{bmatrix} - \begin{bmatrix} \psi \\ 1 \end{bmatrix}\right).

Applying FnF^n multiplies each eigenvector by its eigenvalue nn times. Reading off the second entry:

Fn=φn−ψn5.F_n = \frac{\varphi^n - \psi^n}{\sqrt{5}}.

This is Binet's formula: irrational numbers on the right, whole numbers on the left, for every nn. Because ∣ψ∣<1|\psi| < 1, the ψn\psi^n term dies away, so FnF_n is simply the nearest integer to φn/5\varphi^n/\sqrt{5}, and each Fibonacci number is about 1.6181.618 times the one before. The golden ratio is the growth rate, because it is the dominant eigenvalue.

Learn: power iteration

The characteristic polynomial is fine for a 2×22 \times 2 matrix. For the web, with billions of pages, it is hopeless: nobody can expand a determinant that size. But multiplying a vector by a matrix, even a huge sparse one, is cheap. Power iteration finds the dominant eigenvector with nothing else:

xk+1=Axk∥Axk∥.\mathbf{x}_{k+1} = \frac{A\mathbf{x}_k}{\|A\mathbf{x}_k\|}.

Multiply, rescale so the numbers don't blow up, repeat. Why does it work? Suppose AA has eigenvectors v1,v2\mathbf{v}_1, \mathbf{v}_2 with ∣λ1∣>∣λ2∣|\lambda_1| > |\lambda_2|, and the start is x0=c1v1+c2v2\mathbf{x}_0 = c_1\mathbf{v}_1 + c_2\mathbf{v}_2 with c1≠0c_1 \ne 0. Each multiplication by AA multiplies each eigen-component by its eigenvalue, so

Akx0=c1λ1kv1+c2λ2kv2=λ1k(c1v1+c2(λ2λ1)kv2).A^k\mathbf{x}_0 = c_1\lambda_1^k\mathbf{v}_1 + c_2\lambda_2^k\mathbf{v}_2 = \lambda_1^k\left(c_1\mathbf{v}_1 + c_2\left(\frac{\lambda_2}{\lambda_1}\right)^k\mathbf{v}_2\right).

The ratio ∣λ2/λ1∣|\lambda_2/\lambda_1| is less than 1, so the second term fades geometrically and the direction swings onto v1\mathbf{v}_1. The rescaling only changes the length, never the direction. The speed is set by the ratio: the error shrinks by a factor of about ∣λ2/λ1∣|\lambda_2/\lambda_1| per step.

By hand, with S=[2112]S = \begin{bmatrix} 2 & 1 \\ 1 & 2 \end{bmatrix} and x0=(1,0)\mathbf{x}_0 = (1, 0), skipping the rescaling:

(1,0)→(2,1)→(5,4)→(14,13)→(41,40)→(122,121).(1, 0) \to (2, 1) \to (5, 4) \to (14, 13) \to (41, 40) \to (122, 121).

The vectors line up with (1,1)(1, 1) fast, because ∣λ2/λ1∣=13|\lambda_2/\lambda_1| = \tfrac{1}{3}. To read off the eigenvalue, use the Rayleigh quotient x⊤Axx⊤x\frac{\mathbf{x}^\top A\mathbf{x}}{\mathbf{x}^\top\mathbf{x}}, which equals λ\lambda exactly when x\mathbf{x} is an eigenvector. At (5,4)(5, 4) it is already 12241≈2.976\tfrac{122}{41} \approx 2.976, and at (41,40)(41, 40) it is 2.99972.9997, closing in on λ1=3\lambda_1 = 3.

Python
import numpy as np

def power_iteration(A, x, steps=50):
    for _ in range(steps):
        x = A @ x
        x = x / np.linalg.norm(x)          # rescale: direction is all that matters
    return x, (x @ A @ x) / (x @ x)        # eigenvector and Rayleigh quotient

S = np.array([[2.0, 1.0], [1.0, 2.0]])
x, lam = power_iteration(S, np.array([1.0, 0.0]))
print(x.round(4), round(lam, 4))           # [0.7071 0.7071] 3.0

The Fibonacci recurrence is power iteration too. Starting from (1,0)(1, 0) and applying FF over and over, the direction settles on (φ,1)(\varphi, 1), which is why Fn+1/Fn→φF_{n+1}/F_n \to \varphi.

Two caveats. The start must have some component along v1\mathbf{v}_1 (c1≠0c_1 \ne 0); a random start almost surely does. And there must be a single eigenvalue of largest size: if λ2=−λ1\lambda_2 = -\lambda_1, the vector flips back and forth for ever.

Learn: Markov chains and PageRank

A Markov chain is a system that hops between states at random, where the next hop depends only on where you are now. Its transition matrix PP has PijP_{ij} equal to the probability of moving to state ii from state jj, so each column is a probability distribution and sums to 1. If πt\boldsymbol{\pi}_t is the vector of probabilities of being in each state at time tt, one step is a matrix multiplication: πt+1=Pπt\boldsymbol{\pi}_{t+1} = P\boldsymbol{\pi}_t. Running the chain is power iteration.

A worked example. Tomorrow is sunny with probability 0.90.9 after a sunny day, and with probability 0.50.5 after a rainy one:

P=[0.90.50.10.5].P = \begin{bmatrix} 0.9 & 0.5 \\ 0.1 & 0.5 \end{bmatrix}.

Start on a sunny day, π0=(1,0)\boldsymbol{\pi}_0 = (1, 0). Then π1=(0.9,0.1)\boldsymbol{\pi}_1 = (0.9, 0.1), π2=(0.86,0.14)\boldsymbol{\pi}_2 = (0.86, 0.14), π3=(0.844,0.156)\boldsymbol{\pi}_3 = (0.844, 0.156), and the numbers settle down. The limit is a stationary distribution: a π\boldsymbol{\pi} with Pπ=πP\boldsymbol{\pi} = \boldsymbol{\pi} (read “P pi equals pi”). That is an eigenvector with eigenvalue 1. Solving (P−I)π=0(P - I)\boldsymbol{\pi} = \mathbf{0}: the first row says −0.1 s+0.5 r=0-0.1\,s + 0.5\,r = 0, so s=5rs = 5r, and with s+r=1s + r = 1 we get π=(56,16)\boldsymbol{\pi} = (\tfrac{5}{6}, \tfrac{1}{6}). Five days in six are sunny in the long run. The other eigenvalue is tr⁡(P)−1=0.4\operatorname{tr}(P) - 1 = 0.4, so the chain forgets its starting day like 0.4t0.4^t.

Why must 1 always be an eigenvalue? Because each column of PP sums to 1, the row vector of ones passes through unchanged: 1⊤P=1⊤\mathbf{1}^\top P = \mathbf{1}^\top. Transposing, P⊤1=1P^\top\mathbf{1} = \mathbf{1}, so 1 is an eigenvalue of P⊤P^\top. And AA and A⊤A^\top always share eigenvalues, because det⁡(A⊤−λI)=det⁡((A−λI)⊤)=det⁡(A−λI)\det(A^\top - \lambda I) = \det\big((A - \lambda I)^\top\big) = \det(A - \lambda I).

Quick check +20 XP

In a two-state weather model, a sunny day is followed by a sunny day with probability 0.80.8, and a rainy day is followed by a sunny day with probability 0.40.4. In the long run, what fraction of days are sunny?

PageRank is a Markov chain

Back to the formula you couldn't compute. Brin and Page describe a “random surfer” who clicks links at random and, every so often, gets bored and jumps to a random page. As the formula is written, at each step the surfer follows a random outgoing link with probability dd and jumps to a random page with probability 1−d1 - d. A page's PageRank measures how often the surfer is there in the long run: the stationary distribution of this chain.

Take a web of four pages: AA links to BB and CC, BB links to CC, CC links to AA, and DD links to CC. Build the link matrix MM, with Mij=1/C(j)M_{ij} = 1/C(j) when page jj links to page ii:

M=[00100.50000.51010000].M = \begin{bmatrix} 0 & 0 & 1 & 0 \\ 0.5 & 0 & 0 & 0 \\ 0.5 & 1 & 0 & 1 \\ 0 & 0 & 0 & 0 \end{bmatrix}.

Column jj says where page jj's rank goes. All four of the paper's equations at once read r=(1−d)1+dMr\mathbf{r} = (1 - d)\mathbf{1} + dM\mathbf{r}, and the Decoder below shows how that becomes an eigenvector equation Gr=rG\mathbf{r} = \mathbf{r}. To compute it, just iterate: start with every rank equal to 1 and apply the formula again and again. It's power iteration, and the error shrinks by at least a factor dd each round.

Python
import numpy as np

d = 0.85
M = np.array([[0, 0, 1, 0],
              [0.5, 0, 0, 0],
              [0.5, 1, 0, 1],
              [0, 0, 0, 0]])
r = np.ones(4)
for _ in range(100):
    r = (1 - d) + d * M @ r       # the paper's formula, for every page at once
print(r.round(3), r.sum().round(6))   # [1.49  0.783 1.577 0.15 ] 4.0

Page CC wins: three pages link to it. But AA comes a close second with a single incoming link, because that link comes from the important page CC and is CC's only outgoing link. Page DD, which nobody links to, gets only the baseline 1−d=0.151 - d = 0.15. This is exactly the intuition the paper gives: a page ranks highly if many pages point to it, or if a few highly ranked pages do.

DiscoverLearnRead beyondPapers & lecturesYour turn

Read beyond the course

Interactive · free online · ~15 min

Eigenvectors and Eigenvalues Explained Visually

Victor Powell & Lewis Lehe (Setosa)

Drag a vector until it lines up with its image, then watch eigen-directions attract the sequence v,Av,A2v,…\mathbf{v}, A\mathbf{v}, A^2\mathbf{v}, \ldots. It revisits this chamber's examples, Fibonacci and a two-state migration chain, as interactive pictures. The perfect ten minutes right after the lab.

Book · free online · ~60 min

Mathematics for Machine Learning

Marc Peter Deisenroth, A. Aldo Faisal & Cheng Soon Ong · Sections 4.1, 4.2 and 4.4

Determinant and trace, eigenvalues and eigenvectors, then eigendecomposition and diagonalisation, with the same notation you'll meet in ML papers. Section 4.2 also proves the facts about symmetric matrices that the Chamber 9 spectral theorem builds on.

Article · free online · ~45 min

The $25,000,000,000 Eigenvector: The Linear Algebra behind Google

Kurt Bryan & Tanya Leise (SIAM Review, 2006) · Sections 1–4

Written for students who have just met eigenvectors. It builds the PageRank matrix from small example webs, shows why the damping term guarantees a unique ranking, and explains why power iteration converges. Many of the exercises can be done by hand.

Book · free online · ~40 min

Mining of Massive Datasets

Jure Leskovec, Anand Rajaraman & Jeffrey D. Ullman · Chapter 5, Section 5.1 (PageRank)

The Stanford data-mining textbook's account of PageRank as an engineer sees it: dead ends, spider traps, “taxation” (their name for damping), and how the iteration is run on a graph far too big for memory.

DiscoverLearnRead beyondPapers & lecturesYour turn

Papers and lectures

Brin and Page's paper is long and mostly about engineering a search engine: crawling, indexing, storage. You only need a page and a half of it now. Read the abstract, then jump to Section 2.1, “PageRank: Bringing Order to the Web”, and read Sections 2.1.1 and 2.1.2 slowly. That is the formula, the sentence connecting it to “the principal eigenvector of the normalized link matrix of the web”, and the random-surfer story. Skip Sections 3 to 5 unless you're curious how a search engine was built in 1998. Appendix A is short and worth a look: the founders argue that search engines funded by advertising will be “inherently biased towards the advertisers”.

The Anatomy of a Large-Scale Hypertextual Web Search EngineSergey Brin, Lawrence Page · Computer Networks and ISDN Systems 30 (WWW7), 1998

The paper that introduced Google to the research world. Its PageRank section turns the link structure of the web into an eigenvector problem.

Decode the paper · Section 2.1.1, “Description of PageRank Calculation”

The Anatomy of a Large-Scale Hypertextual Web Search Engine

Sergey Brin, Lawrence Page · Computer Networks and ISDN Systems 30 (WWW7), 1998

+30 XP
PR(A)=(1−d)+d(PR(T1)C(T1)+⋯+PR(Tn)C(Tn))PR(A) = (1-d) + d\left(\frac{PR(T_1)}{C(T_1)} + \cdots + \frac{PR(T_n)}{C(T_n)}\right)

Brin and Page's definition of PageRank. It looks circular: PR(A)PR(A) is defined through the PageRanks of the pages linking to it, whose own definitions may involve PR(A)PR(A). Match each symbol to its meaning, then see how an eigenvector breaks the circle.

PR(A)PR(A)
dd
T1,…,TnT_1, \ldots, T_n
C(T1)C(T_1)
PR(T1)C(T1)\frac{PR(T_1)}{C(T_1)}
(1−d)(1 - d)

Options

Watch

Eigenvectors and eigenvalues | Chapter 14, Essence of linear algebra3Blue1Brown · 17 min
A quick trick for computing eigenvalues | Chapter 15, Essence of linear algebra3Blue1Brown · 13 min

The first video animates exactly what you did in the lab: most vectors are knocked off their span, a few stay on it. Watch for the moment the eigenbasis turns a matrix into a diagonal one. The second derives the mean-and-product trick for 2×22 \times 2 eigenvalues; try each of its examples before it reveals the answer.

DiscoverLearnRead beyondPapers & lecturesYour turn

Your turn

Match symbols to words and to code, prove two facts, then compute: Fibonacci's growth, a frog's favourite lily pad, and the PageRank of a small web, using the paper's formula exactly as printed.

Match · Maths ↔ In words

Say what it means

+25 XP

Match each statement in symbols to the same statement in words.

Av=λvA\mathbf{v} = \lambda\mathbf{v}, v≠0\mathbf{v} \ne \mathbf{0}
det⁡(A−λI)=0\det(A - \lambda I) = 0
tr⁡(A)=λ1+λ2\operatorname{tr}(A) = \lambda_1 + \lambda_2
A=VΛV−1A = V\Lambda V^{-1}
Akv=λkvA^k\mathbf{v} = \lambda^k\mathbf{v}
Pπ=πP\boldsymbol{\pi} = \boldsymbol{\pi}
Av=0vA\mathbf{v} = 0\mathbf{v}, v≠0\mathbf{v} \ne \mathbf{0}

Options

Match · Maths ↔ NumPy

From symbols to NumPy

+25 XP

Match each piece of mathematics to the NumPy that computes it.

all eigenpairs of AA
all eigenpairs of a symmetric SS
tr⁡(A)\operatorname{tr}(A)
VΛkV−1V\Lambda^k V^{-1}
one step of power iteration
x⊤Axx⊤x\frac{\mathbf{x}^\top A\mathbf{x}}{\mathbf{x}^\top\mathbf{x}}
det⁡(A−λI)\det(A - \lambda I)

Options

Proofs

Proof puzzle

Inverting a matrix inverts its eigenvalues

+25 XP

Claim

If AA is invertible and Av=λvA\mathbf{v} = \lambda\mathbf{v} with v≠0\mathbf{v} \ne \mathbf{0}, then λ≠0\lambda \ne 0 and v\mathbf{v} is an eigenvector of A−1A^{-1} with eigenvalue 1/λ1/\lambda.

Tap lines in the order they should appear. Not every line belongs. Tap a line in your proof to send it back.

Your proof

  1. Pick the first line below.

Available lines

Prove it yourself

Symmetric matrices have perpendicular eigenvectors

+40 XP

Claim

Let AA be symmetric (A⊤=AA^\top = A), with Av1=λ1v1A\mathbf{v}_1 = \lambda_1\mathbf{v}_1 and Av2=λ2v2A\mathbf{v}_2 = \lambda_2\mathbf{v}_2, where λ1≠λ2\lambda_1 \ne \lambda_2. Prove that v1⊥v2\mathbf{v}_1 \perp \mathbf{v}_2. (This is half of the spectral theorem you'll meet in Chamber 9: a symmetric matrix has an orthonormal basis of eigenvectors, so A=QΛQ⊤A = Q\Lambda Q^\top.)

Preview

Your typeset proof appears here.

Code it up

Problem 22·Warm-up

A thousand rabbits deep

+20 XP

The Fibonacci numbers are F1=F2=1F_1 = F_2 = 1 and Fn+1=Fn+Fn−1F_{n+1} = F_n + F_{n-1}. One step of the recurrence is a matrix multiplication,

[Fn+1Fn]=[1110][FnFn−1],\begin{bmatrix} F_{n+1} \\ F_n \end{bmatrix} = \begin{bmatrix} 1 & 1 \\ 1 & 0 \end{bmatrix}\begin{bmatrix} F_n \\ F_{n-1} \end{bmatrix},

and diagonalising that matrix gives Binet's formula

Fn=φn−ψn5,φ=1+52,ψ=1−52.F_n = \frac{\varphi^n - \psi^n}{\sqrt{5}}, \qquad \varphi = \frac{1 + \sqrt{5}}{2}, \quad \psi = \frac{1 - \sqrt{5}}{2}.

How many decimal digits does F1000F_{1000} have?

An exact integer (or a fraction like 7/12)

Problem 23·Standard

Where does the frog spend its time?

+35 XP

A frog hops among ten lily pads numbered 0,1,…,90, 1, \ldots, 9. Each minute, if it is on pad ii, it hops to pad (i+1) mod 10(i + 1) \bmod 10 with probability 12\tfrac{1}{2}, leaps to pad (i2+1) mod 10(i^2 + 1) \bmod 10 with probability 13\tfrac{1}{3}, and stays on pad ii with probability 16\tfrac{1}{6}. If two of these moves land on the same pad, their probabilities add. This is a Markov chain with a 10×1010 \times 10 transition matrix PP, where PjiP_{ji} is the probability of moving from pad ii to pad jj, so each column sums to 1. In the long run, the fraction of minutes the frog spends on each pad is given by the stationary distribution π\boldsymbol{\pi}, with Pπ=πP\boldsymbol{\pi} = \boldsymbol{\pi} and ∑iπi=1\sum_i \pi_i = 1.

What fraction of the time does the frog spend on pad 00? Give π0\pi_0 to 4 decimal places.

A number, rounded to 4 decimal places

Problem 24·Challenge

Rank a tiny web

+50 XP

Apply Brin and Page's formula to a web of ten pages numbered 0,1,…,90, 1, \ldots, 9. Page ii links to every distinct page in the set

{(i+1) mod 10,  2i mod 10,  i2 mod 10}\{(i + 1) \bmod 10,\ \ 2i \bmod 10,\ \ i^2 \bmod 10\}

other than itself. So page 0 links only to page 1, page 1 only to page 2, and page 2 to pages 3 and 4. Every page ends up with at least one outgoing link.

Use the formula exactly as the paper prints it, with d=0.85d = 0.85:

PR(A)=(1−d)+d(PR(T1)C(T1)+⋯+PR(Tn)C(Tn)),PR(A) = (1 - d) + d\left(\frac{PR(T_1)}{C(T_1)} + \cdots + \frac{PR(T_n)}{C(T_n)}\right),

where T1,…,TnT_1, \ldots, T_n are the pages that link to AA and C(T)C(T) is the number of links going out of TT. Start with every PRPR equal to 1 and update all ten at once, repeatedly, until nothing changes.

What is the largest PageRank of the ten pages? Give it to 4 decimal places.

A number, rounded to 4 decimal places

Key takeaways

  • An eigenvector is a direction a matrix only stretches: Av=λvA\mathbf{v} = \lambda\mathbf{v} with v≠0\mathbf{v} \ne \mathbf{0}. The eigenvalue λ\lambda is the stretch factor; negative flips, zero flattens.
  • Eigenvalues solve det⁡(A−λI)=0\det(A - \lambda I) = 0, because A−λIA - \lambda I must be singular. For 2×22 \times 2 matrices, λ2−tr⁡(A)λ+det⁡A=0\lambda^2 - \operatorname{tr}(A)\lambda + \det A = 0; the eigenvalues add to the trace and multiply to the determinant.
  • Diagonalisation A=VΛV−1A = V\Lambda V^{-1} makes a matrix a pure stretch in eigen-coordinates, so Ak=VΛkV−1A^k = V\Lambda^k V^{-1}. Distinct eigenvalues guarantee it; Fibonacci's growth rate is the eigenvalue φ\varphi.
  • Power iteration multiplies and rescales; every direction but the dominant one fades like ∣λ2/λ1∣k|\lambda_2/\lambda_1|^k.
  • Markov chains settle into an eigenvector with eigenvalue 1, and PageRank is exactly that: the stationary distribution of a random surfer, computed by power iteration.

Checkpoint

Prove it to the labyrinth

Answer every question to clear this chamber. First-try answers earn the most XP.

0/8
Question 1 of 8 +20 XP

Which of these is an eigenvector of A=[3122]A = \begin{bmatrix} 3 & 1 \\ 2 & 2 \end{bmatrix}?

Question 2 of 8 +20 XP

Why does the 90°90° rotation R=[0−110]R = \begin{bmatrix} 0 & -1 \\ 1 & 0 \end{bmatrix} have no real eigenvectors?

Question 3 of 8 +20 XP

What is the product of the eigenvalues of [5710−23004]\begin{bmatrix} 5 & 7 & 1 \\ 0 & -2 & 3 \\ 0 & 0 & 4 \end{bmatrix}?

Question 4 of 8 +20 XP

A=VΛV−1A = V\Lambda V^{-1} with Λ=[2001/2]\Lambda = \begin{bmatrix} 2 & 0 \\ 0 & 1/2 \end{bmatrix}. What is the largest eigenvalue of A10A^{10}?

Question 5 of 8 +20 XP

00 is an eigenvalue of a square matrix AA exactly when…

Question 6 of 8 +20 XP

Why can't the shear S=[1101]S = \begin{bmatrix} 1 & 1 \\ 0 & 1 \end{bmatrix} be diagonalised?

Question 7 of 8 +20 XP

Power iteration is run on four matrices, each with the two eigenvalues listed. On which does it converge fastest?

Question 8 of 8 +20 XP

Use Brin and Page's formula exactly as printed, PR(A)=(1−d)+d(PR(T1)C(T1)+⋯+PR(Tn)C(Tn))PR(A) = (1 - d) + d\left(\frac{PR(T_1)}{C(T_1)} + \cdots + \frac{PR(T_n)}{C(T_n)}\right), on a web of NN pages in which every page has at least one outgoing link. What do all the PageRanks add up to?

End of the chamber

Clear this chamber

+60 XPEigenvectorCharacteristic PolynomialDiagonalisationPower Iteration