A random walker who moves by fixed probabilities is described by a matrix whose columns add to 1. Its long-run behaviour is an eigenvector with eigenvalue 1, and Google ranked the web by computing that eigenvector.
Taught in 375/376Beyond the courseSpagnolie, Lecture 1 (the PageRank aside) and Lectures 16 to 18 (eigenvalues)Bryan and Leise, The $25,000,000,000 Eigenvector, SIAM Review 48 (2006)Page, Brin, Motwani and Winograd, The PageRank Citation Ranking (Stanford, 1998)Strang, Introduction to Linear Algebra, section 10.3
In Lecture 1, Spagnolie said that real systems can have “30 billion equations and 30 billion variables (Google PageRank!)” and asked “how can you even compute?!”. He called it “the $25 bil eigenvector”. This chapter shows what that eigenvector is. A person clicking links at random wanders over the web. The pages where they spend the most time are, plausibly, the important ones. The matrix that describes the wandering has an eigenvector with eigenvalue 1, and that eigenvector is the ranking.
Before you start
2.8: eigenvalues and eigenvectors, and diagonalization. 2.7: the determinant of a transpose. 2.5: matrix times vector and the transpose. In this chapter probabilities are numbers from 0 to 1, and the only probability rule used is that the chances of all the possible outcomes add to 1.
Quick checkIf Ax=3x for a non-zero vector x, what is x?
That is the definition: A only stretches x, by the factor 3. The null space would need Ax=O, and eigenvalue 1 would need Ax=x.Review: eigenvectors
Quick checkA square matrix A and its transpose AT have...
det(λI−AT)=det((λI−A)T)=det(λI−A), so the characteristic polynomials agree. The eigenvectors, however, are usually different.Review: determinants and the transpose
Quick checkThe matrix with rows (0,1) and (1,0) multiplies the column vector (3,5). What is the result?
Row 1 dotted with (3,5) is 0⋅3+1⋅5=5 and row 2 gives 3. This matrix swaps the two entries, which will matter below.Review: matrix times vector
The picture
Four web pages, and arrows for the links between them. Page 1 links to pages 2, 3 and 4, page 2 links to pages 3 and 4, page 3 links only to page 1, and page 4 links to pages 1 and 3. A surfer starts on page 1 and, at every step, clicks one of the links on the current page, each link equally likely. The bars show the chance of finding the surfer on each page. Press Next. After one step the surfer is on page 2, 3 or 4 with chance 1/3 each. After many steps the bars stop changing: that final set of heights is the ranking.
Two things to watch. The bars always add up to 1, because the surfer is always somewhere. And the heights settle down to the same values wherever the surfer starts. Both facts need a proof, and the proofs use eigenvalues.
The idea, in words
Number the pages 1,…,n. Let Pij be the chance that a surfer on page j goes next to page i. The convention is “column j describes what happens from page j”. With the four pages above, column 1 is (0,31,31,31) because page 1 has three links, and column 3 is (1,0,0,0) because page 3 has one link, to page 1. So
P=03131310021211000210210.
Every column adds to 1, because from any page the surfer goes somewhere.
Let xk be the list of chances: (xk)i is the chance the surfer is on page i after k steps. To land on page i at the next step, the surfer must be on some page j now and choose the link j→i. Add up over all j:
(xk+1)i=j∑Pij(xk)j,that isxk+1=Pxk.
So the whole story of the surfer is a matrix applied again and again. Chapter 2.8 showed what that means: write the starting vector in eigenvectors, and applying P multiplies each part by its eigenvalue. The part with the biggest eigenvalue wins, and the others fade.
Why· Why an eigenvector with eigenvalue 1 is the ranking
If the surfer’s chances ever reach a vector π that one more step does not change, then Pπ=π. That is the equation of an eigenvector with eigenvalue 1. Pages with larger entries of π are the ones where the surfer spends more time. A page gets a high entry when many pages send surfers to it, and more when the pages sending them have high entries themselves. That is the idea of PageRank: a link from an important page is worth more than a link from an obscure one.
Where this came from· Markov and Eugene Onegin
Andrey Markov introduced these chains around 1906 in an argument about probability. Pavel Nekrasov had claimed that averages settle down (the law of large numbers) only for independent events. Markov showed that sequences in which each step depends on the previous state also settle, and he proved it using transition chances like the ones in this chapter. In 1913 he tried the idea on literature: he took the first 20,000 letters of Pushkin’s poem Eugene Onegin, classified each as a vowel or a consonant, and studied how a vowel or consonant follows the one before. That is a two-state chain like our weather example, and its stationary distribution is the long-run share of vowels. The mathematics came first and the web came ninety years later.
The chapter answers five questions, in order. (1) Why does Px stay a list of chances? (2) Why is 1 always an eigenvalue? (3) Why is it the largest? (4) When do the bars settle on one answer, and how fast? (5) What goes wrong on the real web, and what did Google do about it?
Definitions and theorems
Definition B
Markov chain
A Markov chain has a list of states1,…,n and fixed numbers Pij≥0, where Pij is the chance of moving to state i from state j in one step. The defining rule (the Markov property) is that this chance depends only on the current state, not on how the process got there. In the web example the states are pages and the surfer’s past clicks do not matter.
In one lineA process that moves among states, where the chance of the next state depends only on the current state and is given by fixed numbers Pij.
Why it existsIt is the simplest model of change over time with randomness, and it turns a probability question into a matrix question.
A warning on conventions. Probability courses (including MATH 431) usually put “from” on the row: pji is the chance of moving from j to i, rows add to 1, and the chances are a row vector multiplied on the left. This book, Spagnolie and the PageRank papers put “from” on the column, so columns add to 1 and the chances are a column vector multiplied on the right. The two are transposes of each other. Nothing in the mathematics changes.
Definition B
Stochastic matrix
A list x=(x1,…,xn) is a probability vector if every xi≥0 and x1+⋯+xn=1. A square matrix P is stochastic (more precisely, column-stochastic) if every entry is non-negative and every column is a probability vector, that is, every column adds to 1. Write 1=(1,1,…,1). Then “every column adds to 1” is the single equation 1TP=1T, because entry j of 1TP is the sum of column j.
In one lineA square matrix with no negative entries in which every column adds to 1. Its columns are probability vectors.
Why it existsIt is exactly the shape of a matrix of transition chances, because from every state the process goes somewhere.
Worked example
A surfer on three pages: page 1 links to pages 2 and 3, page 2 links to page 3, and page 3 links to page 1. Write the matrix P, check it is stochastic, and find the surfer's chances after one and two steps if she starts on page 1.
Solution.
Write the columns from the links. Column j lists where page j sends the surfer, equally split among its links. Page 1 has two links: column 1 is (0,21,21). Page 2 has one link, to page 3: column 2 is (0,0,1). Page 3 has one link, to page 1: column 3 is (1,0,0). Each link out of page j is equally likely, so with ℓ links each gets chance 1/ℓ.
Assemble the matrix.P=02121001100. Columns side by side.
Check it. All entries are at least 0 and the columns add to 21+21=1, 1 and 1. That is the definition of stochastic.
One step. Start at x0=(1,0,0). Then x1=Px0=(0,21,21). Px0 with x0=e1 picks out column 1.
Two steps.x2=Px1=21(column 2)+21(column 3)=(21,0,21). Px=x1⋅column 1+x2⋅column 2+…, a combination of the columns with the entries of x as weights.
Answer. After one step the surfer is on page 2 or page 3 with chance 21 each. After two steps she is on page 1 or page 3 with chance 21 each. Both lists add to 1.
Theorem BStrang 10.3; Bryan and Leise, section 3
A stochastic matrix keeps probability vectors
Let P be stochastic and x a probability vector. Then Px is a probability vector. If P and Q are both stochastic n×n matrices, so is PQ. In particular Pk is stochastic for every k, and Pkx0 is a probability vector for every k.
In one lineIf P is stochastic and x is a probability vector, then Px is a probability vector. Also a product of stochastic matrices is stochastic.
Why it is trueProbability is never lost or created. It is moved around, and each column says how it moves.
ProofBryan and Leise section 3
The idea: Entries: sums of products of non-negative numbers. Total: swap the order of summation and use that each column adds to 1.
Every entry of Px is non-negative.
Entry i of Px is ∑jPijxj. Every Pij≥0 and every xj≥0, so every term is at least 0.
i∑(Px)i=i∑j∑Pijxj.
Add up all the entries of Px and write each as its sum over j.
=j∑xj(i∑Pij).
A finite double sum can be added in either order. We add over i first, because that is the sum of a column of P.
=j∑xj⋅1=1. So Px is a probability vector.
Each column adds to 1. Then the entries of x add to 1.
For products: column j of PQ is P times column j of Q.
The columns of PQ are P times the columns of Q (2.5). Each column of Q is a probability vector, so by the first part each column of PQ is one, hence PQ is stochastic.
Pk is stochastic for all k, and so Pkx0 is a probability vector.
Induction on k: Pk+1=P⋅Pk.
Quick checkP is stochastic and x=(0.2,0.3,0.5). What do the entries of Px add up to?
The proof: the total of Px is the sum of the entries of x weighted by the column sums of P, and the column sums are all 1. Probability is moved, never lost.Review: the theorem
Worked example
Prove that the product PQ of two stochastic matrices is stochastic, and check it by squaring the weather matrix (columns (0.8,0.2) and (0.4,0.6)).
Solution.
Entries.(PQ)ij=∑kPikQkj is a sum of products of non-negative numbers, so it is at least 0. Products and sums of non-negative numbers are non-negative.
Column sum.∑i(PQ)ij=∑i∑kPikQkj=∑kQkj∑iPik. Swap the two finite sums, and pull Qkj out of the sum over i because it does not depend on i.
Use that P is stochastic.∑iPik=1 for every k, so the column sum is ∑kQkj=1. First P‘s columns add to 1, then Q‘s column does.
Numerical check.P2=[0.720.280.560.44] and the columns add to 0.72+0.28=1 and 0.56+0.44=1. An independent check on the formula.
Answer. The product is stochastic. Interpretation: P2 gives the chances of two steps, and two steps is still a chain that goes somewhere.
Staying put: stationary distributions
Definition B
Stationary distribution
A probability vector π is a stationary distribution of the stochastic matrix P if Pπ=π. In the language of chapter 2.8, it is a probability vector that is an eigenvector of P with eigenvalue 1.
In one lineA probability vector π with Pπ=π: the chances do not change when the surfer takes another step.
Why it existsIt is the long-run answer. If the chances settle down at all, they settle at a stationary distribution, and it is the PageRank vector.
Theorem BBryan and Leise, Prop. 1
One is always an eigenvalue of a stochastic matrix
Every stochastic matrix P has 1 as an eigenvalue. So there is a non-zero real vector v with Pv=v.
In one lineEvery stochastic matrix has eigenvalue 1.
Why it is trueThe all-ones row vector is a left eigenvector with eigenvalue 1 because the columns add to 1. A matrix and its transpose share eigenvalues, so the matrix itself has eigenvalue 1 too.
ProofBryan and Leise
The idea: Find the eigenvector of the transpose, which is easy (all ones), then move the eigenvalue back to P because P and its transpose have the same characteristic polynomial.
PT1=1.
Entry j of PT1 is the sum of column j of P, which is 1. (The columns of P are the rows of PT.)
So 1 is an eigenvalue of PT.
1 is not the zero vector, so by the definition in 2.8 it is an eigenvector of PT with eigenvalue 1.
det(λI−PT)=det((λI−P)T)=det(λI−P).
The determinant of a transpose equals the determinant (2.7), and (λI−P)T=λI−PT.
P and PT have the same eigenvalues, so 1 is an eigenvalue of P.
The eigenvalues are the roots of the characteristic polynomial (2.8). Equal polynomials have equal roots.
A non-zero real v with Pv=v exists.
P−I is a real matrix with determinant 0, so it is not invertible and its null space contains a non-zero vector. That vector can be taken real, since the system (P−I)v=O has real coefficients.
Why· The picture behind the proof
In the visualizer, the left eigenvector 1 says “add up the bars”. The statement 1TP=1T is the fact that the total height of the bars is the same before and after a step: the bars are rearranged, not created. A quantity that every step leaves unchanged is exactly what an eigenvalue of 1 means. The matrix P itself has a different eigenvector for eigenvalue 1 (the stationary bars), and that one is not all ones.
Worked example
Let M be a square matrix whose rows (not columns) each add to 1. Prove that 1 is an eigenvalue of M, and say how this differs from the case of a column-stochastic P.
Solution.
Compute M1. Entry i of M1 is row i of M dotted with (1,…,1), which is the sum of row i. Multiplying by the all-ones vector adds up each row.
Use the hypothesis. Every row adds to 1, so M1=1. This is the definition of the matrix.
Name the eigenvector.1=O, so 1 is an eigenvalue of M with eigenvector 1. An eigenvector is a non-zero vector with Mv=λv.
Compare with columns. For a column-stochastic P the same computation gives PT1=1, an eigenvector of the transpose. Columns of P are rows of PT. Then P itself gets the eigenvalue 1 because P and PT have the same characteristic polynomial, but its eigenvector is a different vector, the stationary distribution.
Answer.M1=1, so 1 is an eigenvalue and 1 is its eigenvector. This is the row-stochastic convention of probability books. In this book the all-ones vector belongs to PT, not to P.
So there is an eigenvector. Is it a probability vector, and is it unique? The answers wait for the next sections. First, the size of the other eigenvalues.
Theorem BBryan and Leise, Prop. 2
Eigenvalues of a stochastic matrix have size at most one
Let P be stochastic and Pv=λv with v=O (complex numbers allowed). Then ∣λ∣≤1.
In one lineEvery eigenvalue λ of a stochastic matrix, real or complex, satisfies ∣λ∣≤1.
Why it is trueA step never increases the total size of a vector, measured by adding the absolute values of its entries. An eigenvector that grew by more than 1 per step would contradict that.
Here ∣λ∣ is the size of λ: for a complex number a+bi it is a2+b2, its distance from 0 in the complex plane. For a vector x (real or complex), let ∥x∥1=∣x1∣+⋯+∣xn∣ be the 1-norm: add the sizes of the entries. It measures size in a different way from the length x⋅x used in 2.9. We use only two of its properties: it is 0 only for O, and ∥cx∥1=∣c∣∥x∥1.
ProofBryan and Leise
The idea: Show that P never makes the 1-norm bigger. Then an eigenvector with eigenvalue lambda has its norm multiplied by |lambda|, which therefore cannot exceed 1.
∣(Px)i∣≤j∑Pij∣xj∣.
Entry i of Px is ∑jPijxj. The absolute value of a sum is at most the sum of the absolute values (the triangle inequality), and ∣Pijxj∣=Pij∣xj∣ because Pij≥0.
∥Px∥1≤i∑j∑Pij∣xj∣.
Add this up over i.
=j∑∣xj∣(i∑Pij)=j∑∣xj∣=∥x∥1.
Swap the sums, as in the previous proof, and use that every column of P adds to 1.
∣λ∣∥v∥1≤∥v∥1.
For an eigenvector, Pv=λv, so ∥Pv∥1=∣λ∣∥v∥1.
∣λ∣≤1.
v=O, so ∥v∥1>0, and we can divide.
Proof workshopRebuild the proof that no eigenvalue of a stochastic matrix is bigger than 1
Tap the lines in the order a proof would write them. Each line can only use what is already above it.
We start from an arbitrary eigenvalue and its eigenvector.
The triangle inequality, and Pij≥0 so ∣Pijvj∣=Pij∣vj∣.
You need an eigenvector v before you can estimate the entries of Pv.
Add the previous line over all i.
This adds up the entry-by-entry bound, so that bound must be written first.
Swap the finite sums; each column of P adds to 1.
This simplifies the double sum, which the previous line has to produce first.
Because Pv=λv, and by the lines above.
You need the bound ∥Pv∥1≤∥v∥1 to be complete first.
Divide both sides by the positive number ∥v∥1.
Dividing needs the inequality from the previous line.
The proof gives more than the statement. For every vector, ∥Px∥1≤∥x∥1, and equality holds for non-negative x (then no absolute values change). The triangle inequality is where mass can be lost: if the entries Pijxj in a row have mixed signs, they partly cancel. We will use that.
Quick checkCan a stochastic matrix have the eigenvalue 2?
A step cannot increase the 1-norm. An eigenvector for the eigenvalue 2 would double it. A zero column is not allowed in a stochastic matrix anyway, since the columns must add to 1.Review: the theorem
Worked example
Prove that for a stochastic matrix P and a vector x with all entries at least 0, the 1-norm of Px equals the 1-norm of x. Say where the inequality of the earlier proof becomes an equality.
Solution.
Sign of the entries. Every entry of Px is ∑jPijxj, a sum of products of non-negative numbers, so it is at least 0. Both Pij and xj are non-negative.
Drop the absolute values. So ∥Px∥1=∑i(Px)i and ∥x∥1=∑jxj. The 1-norm adds absolute values, and absolute values of non-negative numbers change nothing.
Add up the entries of Px.∑i∑jPijxj=∑jxj∑iPij=∑jxj. Swap the finite sums, then each column of P adds to 1.
Find the equality. In the earlier proof the only inequality was the triangle inequality ∑jPijxj≤∑jPij∣xj∣. For x≥0 the terms Pijxj are all non-negative, so the left side equals the right side. The triangle inequality is an equality exactly when no terms cancel, and non-negative numbers cannot cancel.
Answer.∥Px∥1=∥x∥1 for x≥0. A vector with mixed signs can lose norm, because its positive and negative parts partly cancel. That cancellation is the “wobble” that shrinks as the chain settles.
Lemma BBryan and Leise, Lemma
Other eigenvectors have entries adding to zero
Let P be stochastic and Pv=λv with λ=1. Then 1Tv=0.
In one lineIf Pv=λv with λ=1, the entries of v add to 0.
Why it is trueThe total of the entries is conserved by P. A vector that gets multiplied by a number other than 1 and also keeps its total must have total zero.
ProofBryan and Leise
The idea: Compute the total of the entries of Pv in two ways.
1T(Pv)=1Tv.
1TP=1T because the columns add to 1.
1T(Pv)=λ1Tv.
Pv=λv.
(λ−1)1Tv=0.
Subtract the two lines.
1Tv=0.
λ=1, so the first factor is not zero and can be divided out.
Worked example
Find the stationary distribution of the weather chain: a sunny day is followed by a sunny day with chance 0.8 and a rainy day with chance 0.2; a rainy day is followed by a sunny day with chance 0.4 and a rainy day with chance 0.6. Also find the eigenvalues, and the chances k days after a sunny day.
Solution.
Write the matrix.P=[0.80.20.40.6], with the sunny state first. Column 1 describes what follows a sunny day, column 2 what follows a rainy one. Each column adds to 1.
Write Pπ=π as a homogeneous system.(P−I)π=O gives −0.2π1+0.4π2=0 and 0.2π1−0.4π2=0. Moving π to the left side makes it a null-space problem, as in 2.8.
Solve. Both equations say π1=2π2. A basis of the null space is (2,1). The two equations are multiples of each other, because P−I is singular, as the theorem on eigenvalue 1 promised.
Scale to a probability vector. Divide by the sum 3: π=(32,31). A stationary distribution must be a probability vector, with entries adding to 1.
Find the other eigenvalue. The trace of P is 1.4 and it equals the sum of the eigenvalues (2.8), so the other eigenvalue is 0.4. Its eigenvector is (−1,1), the vector with entries adding to 0, as the lemma says. Check: P(−1,1)=(−0.8+0.4,−0.2+0.6)=(−0.4,0.4)=0.4(−1,1).
Chances after a sunny day. Write x0=(1,0)=π+31(1,−1). Applying Pk times multiplies each eigen-part by its eigenvalue: xk=π+31(0.4)k(1,−1), that is (32+31(0.4)k,31−31(0.4)k). Pπ=π and P(1,−1)=0.4(1,−1).
Check k=1.x1=(32+0.1333,31−0.1333)=(0.8,0.2), the first column of P. An independent check of the formula.
Answer. Long-run, two days in three are sunny. The chances approach (32,31) geometrically, with the error shrinking by a factor 0.4 each day.
Quick checkSolving (P−I)π=O for a 2×2 stochastic matrix gives the null-space vector (3,4). What is the stationary distribution?
A stationary distribution must have entries adding to 1, so divide the null-space vector by its sum, 7. Leaving it as (3,4) is the most common slip. The options (21,21) and (43,34) are not multiples of (3,4), so they are not even eigenvectors for this P.
Your answerA weather chain has P=[0.60.40.30.7]. What fraction of days are sunny in the long run (the first entry of π)?
Solve the one independent equation, then scale so the entries add to 1.
(P−I)π=O gives −0.4π1+0.3π2=0, so π1=0.75π2 and a null-space vector is (3,4). Dividing by the sum 7 gives π=(73,74). Check: 0.6⋅73+0.3⋅74=71.8+1.2=73.
Your answerA 2×2 stochastic matrix has P=[0.750.250.50.5]. What is its other eigenvalue (besides 1)?
Use trace = sum of eigenvalues, from 2.8.
The trace is 0.75+0.5=1.25 and the eigenvalues add to the trace, so the other eigenvalue is 1.25−1=0.25. The chances therefore approach π with error shrinking by a factor 0.25 each step.
Worked example Bryan and Leise's example; the visualizer above
Find the stationary distribution of the four-page web (page 1 links to 2, 3, 4; page 2 to 3, 4; page 3 to 1; page 4 to 1, 3).
Solution.
Write Pπ=π row by row, using the matrix in the chapter’s introduction. π1=π3+21π4, π2=31π1, π3=31π1+21π2+21π4, π4=31π1+21π2. Each equation says “page i gets the same share after a step as before”. Row i lists the pages that link to i and how much of their share they send.
Choose a parameter. Put π1=3t. Then π2=t. Equation 2 gives π2 in terms of π1; choosing π1=3t avoids fractions. One equation is redundant because P−I is singular, so one free parameter is expected.
Find π4 and π3.π4=31(3t)+21t=23t. Then 3t=π3+43t gives π3=49t. Equation 4 gives π4. Equation 1 gives π3.
Check the unused equation. Equation 3: 49t=?t+21t+43t=49t. Yes. It was not used, so it checks the arithmetic independently.
Scale. The entries are 3t,t,49t,23t, which add to 431t. Setting this to 1 gives t=314 and π=(12,4,9,6)/31. A probability vector must add to 1.
Answer.π=(12,4,9,6)/31≈(0.387,0.129,0.290,0.194), the heights the visualizer settles on. Page 1 ranks first, although page 3 has the most links in. Page 3 gives its entire share to page 1.
Does it settle? Convergence and the power method
Eigenvalue 1 exists and the others have size at most 1. If every other eigenvalue has size strictly less than 1, repeated multiplication kills their parts and leaves only the eigenvalue-1 part. This is the “apply it 100 times” idea of chapter 2.8.
Theorem BStrang 10.3; Bryan and Leise, section 4
Convergence for a diagonalizable chain with a simple eigenvalue 1
Suppose P is stochastic and diagonalizable, with eigenvalues 1,λ2,…,λn where ∣λi∣<1 for i≥2. Then there is exactly one stationary distribution π, and for every probability vector x0,
Pkx0→πas k→∞,with error of size about ∣λ2∣k,
where λ2 is the eigenvalue of largest size among the others.
In one lineLet P be stochastic and diagonalizable. If 1 is an eigenvalue with a one-dimensional eigenspace and every other eigenvalue has size less than 1, then Pkx0 approaches the same stationary distribution π from every starting probability vector x0.
Why it is trueWrite x0 in the eigenvectors. Applying Pk times multiplies the part along each eigenvector by λk. The parts with ∣λ∣ less than 1 die away, and only the part along π is left.
ProofStrang 10.3
The idea: Expand x0 in an eigenbasis. The coefficient of the eigenvalue-1 part is forced to be 1 by the fact that probability is conserved (the lemma). Every other part is multiplied by lambda to the k, which goes to 0.
Let v1,…,vn be an eigenbasis, λ1=1.
P is diagonalizable (2.8), so there is a basis of eigenvectors v1,…,vn of Cn with Pvi=λivi. We take λ1=1, with eigenvector v1. The eigenvalues are complex numbers in general.
The entries of vi add to 0 for i≥2.
By the lemma, 1Tvi=0 for i≥2, because λi=1.
So 1Tv1=0. Rescale v1 (dividing by the number 1Tv1) so that its entries add to 1, and call it π. We show in the last step that π is real.
If also 1Tv1=0, then 1T would vanish on a whole basis, hence on every vector. But 1T1=n=0.This is the quiet step that makes the proof work.
x0=c1π+c2v2+⋯+cnvn.
Every vector is a combination of a basis (2.2).
1=c1.
Apply 1T. The left side is 1 (probability vector). On the right, the terms with i≥2 vanish, and 1Tπ=1.
Pkx0=π+c2λ2kv2+⋯+cnλnkvn.
Apply Pk times; Pkvi=λikvi because each application multiplies by λi.
As k→∞, every term after the first goes to O, so Pkx0→π.
∣λi∣<1 for i≥2, so λik→0 (a geometric sequence with ratio of size less than 1). The error is dominated by the largest ∣λi∣.
π is a stationary distribution, and it is the only one.
Every Pkx0 is a real probability vector (the first theorem of this chapter), and a limit of such vectors is real, has entries at least 0 and adds to 1. So π is a probability vector. Applying P to both sides of Pkx0→π gives Pπ=π. Uniqueness: any stationary distribution π′ satisfies Pkπ′=π′, so by the above it also tends to π.
Why· The picture behind the proof
Press Next in the first visualizer again and watch the bars. The starting bars are one tall bar on page 1. Each step keeps the total height and removes a little more of the “wobble”, the part along the other eigenvectors, which has total height zero (it has bars above and below the stationary heights). The wobble shrinks by a factor of ∣λ2∣≈0.55 at each step for this web, which is why about eight to ten steps leave bars that look still. The numbers are in the power-method example below.
Definition B
Power method
The power method for a matrix A chooses a starting vector x0 and computes xk+1=Axk (in general, rescaling each result to keep its size under control). If the eigenvalue of largest size is unique and the starting vector has a component along its eigenvector, the iterates line up with that eigenvector, at the rate ∣λ2/λ1∣k. For a stochastic matrix and a probability vector no rescaling is needed, since Px is again a probability vector, and the eigenvalue of largest size is 1.
In one lineFind the dominant eigenvector by starting anywhere and applying the matrix again and again.
Why it existsIt computes the eigenvector using only matrix-times-vector, which is cheap even when the matrix is enormous and never written down in full.
Worked example Bryan and Leise's example; the visualizer above
Run the power method on the four-page web from x0=(1,0,0,0), and watch the error shrink.
Solution.
Start.x0=(1,0,0,0), so the surfer is on page 1. The 1-norm distance to π=(12,4,9,6)/31 is about 1.226. We measure the error as ∥xk−π∥1, the sum of the absolute differences of the entries.
Apply P once.x1=Px0=(0,31,31,31), error ≈0.774. Applying P to e1 picks out column 1: page 1 sends a third of the surfer to each of pages 2, 3, 4.
Apply P again.x2=(21,0,31,61), error ≈0.312. Page 1 receives all of page 3’s 31 and half of page 4’s 31, which is 31+61=21. Page 2 receives nothing, since only page 1 links to it and page 1 is empty.
Keep going.x3=(125,61,41,61) with error ≈0.134, and x4=(31,365,3611,92) with error ≈0.108. The entries rise and fall around the final values, because two of the other three eigenvalues of P are complex and make their part of the error rotate as it shrinks.
Read the rate.x7=(14455,43255,432127,43285) has error ≈0.0137, and x12≈(0.3866,0.1292,0.2904,0.1938) has error ≈0.00095. Over steps 4 to 7 the error shrinks by about 0.5 per step, close to ∣λ2∣≈0.547. The eigenvalues other than 1 are the roots of 12λ3+12λ2+6λ+1, about −0.279 and −0.361±0.411i.
Verify the limit exactly.Pπ=311(9+3,4,4+2+3,4+2)=311(12,4,9,6)=π. One application of P to π in exact arithmetic leaves it unchanged, so π is stationary.
The step-through below shows the same iteration with the exact fractions.
Answer. The iterates approach (12,4,9,6)/31. A web of billions of pages needs the same steps; what changes is that each application of P costs about one operation per link, not one per pair of pages.
Python labThe power method on the four-page web, and the Google matrix
import numpy as np# Column j lists where a surfer on page j goes next (Bryan and Leise's web).P = np.array([[0, 0, 1, 1/2], [1/3, 0, 0, 0 ], [1/3, 1/2, 0, 1/2], [1/3, 1/2, 0, 0 ]])exact = np.array([12, 4, 9, 6]) / 31x = np.array([1.0, 0, 0, 0]) # start on page 1for k in range(0, 31, 5): print(f"step {k:2d}: x = {np.round(x, 4)} error = {np.abs(x - exact).sum():.2e}") for _ in range(5): x = P @ xvals = np.linalg.eigvals(P)print("eigenvalue sizes:", np.round(sorted(abs(vals), reverse=True), 4))d, n = 0.85, 4 # the Google matrix: follow a link with probability dG = d * P + (1 - d) / n * np.ones((n, n))x = np.ones(n) / nfor _ in range(100): x = G @ xprint("PageRank with d = 0.85:", np.round(x, 4))
Try this.The error shrinks by a factor of about 0.0485 every five steps. Check that this is 0.54685, where 0.5468 is the second largest eigenvalue size printed at the end: the rate ∣λ2/λ1∣k from the text. Then change d to 0.5 and see the ranking move toward equal shares, and add a fifth page that links only to page 1.
Your answerIn the four-page web, a surfer has the chances x=(21,0,31,61). What is entry 3 of Px, the chance of being on page 3 one step later?
Row 3 of P says which pages send surfers to page 3, and what share of their chance they send.
Row 3 of P is (31,21,0,21): page 3 receives a third of page 1, half of page 2 and half of page 4. So (Px)3=31⋅21+21⋅0+0⋅31+21⋅61=61+121=41. This is the third entry of x3 in the step-through.
Worked example
A walker moves on the graph with vertices 1, 2, 3, 4 and edges 12, 23, 34, 41, 13, each step to a uniformly random neighbour. Write P, find the stationary distribution, and say whether the walker's chances settle.
Solution.
List the neighbours. Vertex 1 has neighbours 2, 3, 4. Vertex 2 has 1, 3. Vertex 3 has 2, 4, 1. Vertex 4 has 3, 1. Read each vertex’s edges from the list.
Write the columns. Column j puts 1/degj on each neighbour of j: column 1 is (0,31,31,31), column 2 is (21,0,21,0), column 3 is (31,31,0,31), column 4 is (21,0,21,0). From a vertex with ℓ neighbours each neighbour has chance 1/ℓ. Each column adds to 1.
Use the degree rule. The degrees are 3,2,3,2, adding to 10=2×5 edges. So π=(3,2,3,2)/10. On an undirected graph the walker visits each vertex in proportion to its degree (proved in the “Where it is used” section).
Check it. Row 1 of Pπ is 21π2+31π3+21π4=101+101+101=103. Row 2 is 31π1+31π3=102. Multiplying back is the check that does not depend on the rule being remembered correctly.
Decide about settling. The graph has triangles (1,2,3 and 1,3,4), so the walker can return in 2 or 3 steps, and the chain is not stuck in a lockstep cycle. The eigenvalues are 1, −31, −32 and 0, all other than 1 of size less than 1. Contrast with the path 1−2−3, where the walker alternates between the middle and the ends and the eigenvalue −1 never fades.
Answer.π=(3,2,3,2)/10, and Pkx0→π from every start, at the rate (32)k.
Your answerA walker on an undirected graph with 5 edges has stationary distribution πv=deg(v)/10. What is πv for a vertex of degree 3?
Total of all degrees = 2 times the number of edges.
The degrees add to twice the number of edges, 2×5=10, so πv=3/10=0.3.
Now the theorem that says when the hypotheses hold. It is the major result of this area, and we state it without proof.
Theorem BPerron 1907, Frobenius 1912; Bryan and Leise, section 4 (proof not given here)
Perron–Frobenius theorem for regular chains
Call a stochastic matrix regular if some power Pm has every entry strictly positive (every state can reach every state in exactly m steps). For a regular P, there is exactly one stationary distribution π, all of its entries are positive, every eigenvalue other than 1 has size less than 1, and Pkx0→π for every probability vector x0.
In one lineIf some power of the stochastic matrix P has all entries strictly positive (a regular chain), then 1 is a simple eigenvalue with a positive eigenvector, every other eigenvalue has size less than 1, and Pkx0→π for every probability vector x0.
Why it is truePositive entries mean every state can reach every other, and in a way that mixes time well enough to forget the start. That removes the ways the conclusion can fail.
We do not prove this theorem. It is a result of Perron (1907) for positive matrices, extended by Frobenius (1912). Diagonalizability is not needed. The proof without it uses the Jordan form, which is beyond this chapter. For positive matrices there is a short idea behind the size claim, and it reuses the proof that ∣λ∣≤1.
Beyond the course: for understanding· Why positive entries push the other eigenvalues strictly inside (a sketch)
In the proof that ∥Px∥1≤∥x∥1, equality needs equality in every triangle inequality ∑jPijxj≤∑jPij∣xj∣. For complex numbers, this holds only if all the non-zero terms Pijxj point in the same direction in the complex plane. If every Pij is positive, that means all entries xj of the vector point in the same direction. Rotate the vector so they are all real and at least 0. Then Px is also real and non-negative, so Px=λx forces λ≥0, and the equality of norms forces λ=1. So an eigenvalue of size exactly 1 must be 1 itself, and its eigenvector can be taken positive. An eigenvector with mixed signs loses norm at every step: that is the “wobble” shrinking in the picture. For a regular chain, apply the same argument to the positive matrix Pm. It shows that an eigenvalue λ of P with ∣λ∣=1 has λm=1. To get λ=1 one more fact is needed, that 1 is a simple eigenvalue. This is a sketch of the main idea. It leaves out why 1 is a simple eigenvalue, that last step, and the case without diagonalizability.
Quick checkP is the stochastic matrix that swaps the two entries of a vector (columns (0,1) and (1,0)), and the surfer starts at x0=(1,0). What happens to Pkx0?
P swaps the two entries, so the surfer bounces between the pages. The eigenvalues are 1 and −1: the eigenvalue −1 has size 1, so its part never fades. The matrix is not regular, because every power is either P or I, and both have zeros. (The uniform vector (21,21) is stationary, but the chances do not approach it.)Review: regular chains
What goes wrong on the real web
Where this came from· Page, Brin and the random surfer
In 1996 Larry Page and Sergey Brin, then students at Stanford, began work on ranking web pages by importance, as well as by whether they contained the search words. They borrowed an idea from scholarly citations: a page is important if important pages link to it. That sentence is circular. The way out was the surfer who follows links at random, so that importance becomes a stationary distribution. Brin and Page’s 1998 paper on the Google search engine gives d=0.85 as the usual damping factor, and the Stanford technical report of the same year by Page, Brin, Rajeev Motwani and Terry Winograd describes the ranking itself. The patent, granted in 2001 with Page as inventor, belongs to Stanford, and Google, founded in 1998, held an exclusive licence.
The picture works for the four-page web. The real web has features that break the hypotheses, and each is shown below with a small example. One idea fixes all of them.
Dead ends (dangling pages). A page with no links has a zero column, so P is not stochastic. A surfer reaching it vanishes: for pages 1→2 and a dead-end page 2, xk→(0,0). Probability leaks out.
Cycles in lockstep (periodicity). Pages 1↔2 swap the surfer forever. The eigenvalue −1 never fades, and the chances never settle.
Disconnected pieces. If pages {1,2} link only to each other and {3,4} link only to each other, then every mixture of the two pieces’ stationary vectors is stationary. The ranking is not unique.
Spider traps. A page that links only to itself (or a small group that only links to itself) absorbs the surfer for good. With pages 1→2,3; 2→1; 3→3, the only stationary distribution is (0,0,1): page 3 gets all the rank and the others get nothing.
Google’s fix is to make the surfer occasionally bored. This is the matrix.
Definition B
Google matrix
Let P be stochastic (first replace every zero column, from a dead-end page, by the column (n1,…,n1): a stuck surfer jumps to a random page). Choose a damping factord with 0≤d<1. The Google matrix is
G=dP+n1−d11T,
where 11T is the n×n matrix with every entry 1. So Gij=dPij+n1−d. Page and Brin’s papers use d=0.85: the surfer follows a link 85 times in 100 and teleports 15 times in 100.
In one lineG=dP+n1−d11T: with chance d the surfer follows a link, and with chance 1−d jumps to a page chosen uniformly at random.
Why it existsThe random jumps connect every page to every page, so none of the failures above can happen, and a unique ranking always exists.
Each column of G adds to d⋅1+n⋅n1−d=1, so G is stochastic. Every entry is at least n1−d>0, so G is regular (already G1 is positive). By Perron–Frobenius, which we did not prove, G has a unique stationary distribution. For this matrix we can prove everything we need directly, and get a speed guarantee at the same time.
Theorem BHaveliwala and Kamvar (2003) for the rate; Bryan and Leise, section 5
The Google matrix has a unique stationary distribution
Let P be stochastic, 0≤d<1, and G=dP+n1−d11T. Then:
G is stochastic with every entry at least n1−d.
G has a stationary distribution π, and every entry of π is at least n1−d.
For every probability vector x0 and every k, ∥Gkx0−π∥1≤2dk. In particular π is the only stationary distribution.
Every eigenvalue λ=1 of G has ∣λ∣≤d.
In one lineFor 0≤d<1, G has exactly one stationary distribution π, all its entries are at least n1−d, and from any starting probability vector ∥Gkx0−π∥1≤2dk. Every eigenvalue other than 1 has size at most d.
Why it is trueOn vectors whose entries add to zero, the teleport part of G vanishes and G acts as d times P. So the wobble shrinks by at least a factor d per step, whatever the web looks like.
ProofBryan and Leise section 5; Haveliwala and Kamvar
The idea: Existence: the eigenvalue-1 theorem plus a sign argument. Convergence: the difference of two probability vectors adds to zero, and on such vectors G is d times P, which never increases the 1-norm.
G is stochastic with all entries positive.
Part 1. Each column of G adds to d⋅1+n⋅n1−d=1, and each entry is dPij+n1−d≥n1−d.
Take a real v=O with Gv=v.
Part 2. By the eigenvalue-1 theorem G has a non-zero real v with Gv=v.
Suppose v has entries of both signs. Then ∣vi∣=∣(Gv)i∣<∑jGij∣vj∣ for every i.
Suppose, for contradiction, that v has an entry above 0 and an entry below 0. In every row i, the term with the negative entry vk satisfies Gikvk<Gik∣vk∣ because Gik>0 (strictly). So ∑jGijvj<∑jGij∣vj∣ in every row: the positive and negative terms cancel a little.
Then ∥v∥1<∑j∣vj∣∑iGij=∥v∥1, which is absurd.
Add over i, swap the sums, and use that the columns of G add to 1 (the computation from the 1-norm theorem, now with strict inequality).
So v can be taken non-negative, and rescaled to a stationary distribution π.
So all entries of v have the same sign (some may be zero). Replace v by −v if needed, so that v≥0 with v=O. Then its entries add to a positive number, and dividing by it gives a probability vector π with Gπ=π.
Every πi≥n1−d>0.
Since Gπ=π and π adds to 1, each entry is πi=(Gπ)i=∑jGijπj≥n1−d∑jπj.
Gy=dPy.
Part 3. Let y be any vector whose entries add to 0. Then 11Ty=1(1Ty)=O, the teleport part sees nothing.This is the heart of the proof: the damping splits G into 'follow a link' (scaled by d) and 'teleport', and the difference of two probability vectors is invisible to teleporting.
Gky=dkPky, and ∥Gky∥1≤dk∥y∥1.
1T(Py)=1Ty=0, so Py also adds to 0. Repeating the step k times, and using ∥Pz∥1≤∥z∥1 each time, gives the bound.
∥Gkx0−π∥1=∥Gk(x0−π)∥1≤2dk.
y=x0−π adds to 1−1=0, and Gkπ=π. The triangle inequality gives ∥x0−π∥1≤∥x0∥1+∥π∥1=2.
The stationary distribution is unique.
If π′ is also stationary, then Gkπ′=π′, so ∥π′−π∥1≤2dk for every k. This tends to 0, so π′=π.
∣λ∣∥v∥1=d∥Pv∥1≤d∥v∥1, so ∣λ∣≤d.
Part 4. If Gv=λv with λ=1, then 1Tv=0 by the lemma, so λv=Gv=dPv. Take 1-norms and use the bound ∥Pv∥1≤∥v∥1.
Proof workshopRebuild the speed bound: the Google chain forgets its start at least as fast as d to the k
Tap the lines in the order a proof would write them. Each line can only use what is already above it.
Two probability vectors each add to 1, so their difference adds to 0.
The teleport part of G multiplies 1Ty, which is 0.
You need y with entries adding to 0 before the teleport part can vanish.
Each column of P adds to 1, so 1TP=1T.
To repeat the step on Py you first need the formula Gy=dPy.
Apply the previous two lines k times in a row.
Repeating needs both the one-step formula and the fact that Py also adds to 0.
A step of P never increases the 1-norm.
This bounds the vector that appears in dkPky, so that expression comes first.
Gkπ=π, and ∥y∥1≤∥x0∥1+∥π∥1=2 by the triangle inequality.
The final bound combines the two lines above it.
Why· The picture behind the proof
Turn on the damping slider in the visualizer below. At d=1 you get the undamped web. Lower d and the stationary bars flatten toward equal heights, because more of the surfer’s time is spent teleporting. At d=0.85 the bars become (0.368,0.142,0.288,0.202) instead of (0.387,0.129,0.290,0.194): pages ranked low gain a little, high-ranked pages lose a little. Step through with the slider at a small d and the bars settle in very few steps; at d near 1 they take longer. That is the bound 2dk in motion: damping is the price paid for speed and uniqueness.
Quick checkWhy does the damping factor guarantee a unique ranking and fast convergence?
Every entry of the Google matrix is at least (1−d)/n, which rules out dead ends, lockstep cycles, isolated pieces and spider traps, and on vectors that add to zero G equals dP. The matrix is not symmetric, and the links are kept (scaled by d).Review: the Google matrix theorem
How fast is 2dk for d=0.85? 0.8550≈3.0×10−4 and 0.85100≈8.7×10−8. The bound does not depend on the number of pages. This is the answer to Spagnolie’s “how can you even compute?!”. Page, Brin, Motwani and Winograd (1998) report that for a web of 322 million links, the iteration converged to a tolerable limit in about 52 steps. Two facts make each step cheap. First, P is sparse: a page has maybe tens of links, so Px costs about one operation per link, not n2. Second, one never builds the dense matrix G: for a probability vector x,
Gx=dPx+n1−d1(1Tx)=dPx+n1−d1,
so a step of the damped chain is a step of the sparse chain, scaled by d, plus the same small number added to every page.
Worked example
Let G = dP + ((1 − d)/n)·1·1ᵀ with P stochastic. Prove that for every probability vector x, Gx = dPx + ((1 − d)/n)·1, and that Gx is a probability vector.
Solution.
Split the product.Gx=dPx+n1−d1(1Tx). Matrix multiplication distributes over the sum, and we group 11Tx as 1(1Tx), which is allowed because matrix multiplication is associative.
Evaluate the bracket.1Tx is the sum of the entries of x, which is 1. x is a probability vector.
Conclude the formula.Gx=dPx+n1−d1. Replace the bracket by 1. The dense matrix G never needs to be written down.
Entries are non-negative.Px has entries at least 0 and 0≤d<1, and the added number n1−d is positive. Sums and products of non-negative numbers are non-negative.
Entries add to 1. The entries of dPx add to d⋅1 because Px is a probability vector. The added vector contributes n⋅n1−d=1−d. The total is d+(1−d)=1. This is the stochastic-matrix theorem for P, plus counting n equal entries.
Answer.Gx=dPx+n1−d1, and Gx is a probability vector.
Your answerThe Google matrix for n=4 pages with d=0.85 has Gij=0.85Pij+40.15. If P21=31, what is G21 (to 4 decimal places is plenty)?
Compute n1−d=40.15 first, then add d times the link share.
G21=0.85⋅31+0.0375=0.28333+0.0375=0.32083. Even an entry with Pij=0 becomes 0.0375, which is why every page can reach every page.
Your answerFor the Google matrix with d=0.9, the bound ∥Gkx0−π∥1≤2dk holds. What is the smallest whole number k for which the bound is below 0.01?
Solve 0.9k<0.005 with logarithms, then round up to a whole number.
We need 2(0.9)k<0.01, that is 0.9k<0.005. Taking logarithms, k>ln0.9ln0.005≈50.3, so k=51. Check: 2(0.9)51≈0.0093 and 2(0.9)50≈0.0103.
Worked example
Find the PageRank vector of the three-page web (1 links to 2 and 3; 2 links to 3; 3 links to 1) with damping factor d=43.
Solution.
State what to solve.Gπ=π with 1Tπ=1. Since Gπ=dPπ+n1−d1 for a probability vector, this is (I−dP)π=n1−d1=1211. Using the shortcut for Gx avoids writing the full G. Here n=3 and 1−d=41.
Write the three equations, with P from the first example: π1−43π3=121, π2−83π1=121, π3−83π1−43π2=121. Row i of P lists which pages send surfers to page i. Page 1 receives all of page 3; page 2 receives half of page 1; page 3 receives half of page 1 and all of page 2.
Express π1 and π2 in terms of π3.π1=121+43π3. Then π2=121+83π1=9611+329π3. Equations 1 and 2 solve for the first two unknowns directly.
Substitute into equation 3.π3=121+83(121+43π3)+43(9611+329π3). The constants add to 121+321+12811=38477, and the π3 terms to 329π3+12827π3=12863π3. Common denominator 384 for the constants: 32+12+33=77.
Solve for π3.(1−12863)π3=38477, so 12865π3=38477 and π3=19577. Multiply both sides by 65128.
Back-substitute.π1=121+43⋅19577=19574 and π2=9611+329⋅19577=19544. Plug π3 into the formulas of step 3.
Check.19574+44+77=1, and every entry is at least n1−d=121≈0.083. The theorem promises a probability vector with entries at least n1−d.
Answer.π=(74,44,77)/195≈(0.379,0.226,0.395). Compare the undamped ranking (2,1,2)/5=(0.4,0.2,0.4): damping pulls the answer toward equal shares. (The undamped matrix has eigenvalues 1 and 2−1±i, which have size 0.707.)
Worked example
A spider trap: page 1 links to 2 and 3, page 2 links to 1, page 3 links only to itself. Find the undamped stationary distribution and the PageRank with d=0.85.
Solution.
Write P.P=02121100001. Column 3 is (0,0,1) because page 3 sends the surfer back to itself.
Undamped stationary vectors.Pπ=π: the first equation is π1=π2, the second is π2=21π1. Together, π1=π2=0. The third equation then holds for any π3, and π3=1. Page 1 and 2 keep losing half their chance to page 3, which never returns it.
Read the meaning.π=(0,0,1): page 3 gets all the rank. Starting anywhere, the surfer ends in the trap. The trap is a set of pages with no links out, which breaks the “every page can reach every page” requirement.
Damp it. Solve (I−0.85P)π=30.151 exactly. The solution is π=(74,57,380)/511≈(0.145,0.112,0.744). The same method as the previous example, with d=17/20; the unique solution is guaranteed by the theorem. Check: 74+57+380=511 and every entry exceeds 0.05.
Answer. Undamped: (0,0,1). Damped with d=0.85: about (0.145,0.112,0.744). Damping lets the other pages keep a share. The trap still takes about three quarters of the rank. Real search engines had to deal with exactly this: people built groups of pages that link only to each other to inflate their rank, which is why ranking involves much more than this one matrix.
Examples and non-examples
The worked examples sit beside the ideas they practise, above. A few more.
A non-example: a vector that is stationary but not reached. For P=[0110], the vector (21,21) is stationary, and it is a probability vector. But Pkx0 does not approach it from x0=(1,0), because the eigenvalue −1 does not fade. “Stationary exists” and “the chain converges to it” are different statements.
A non-example: a matrix that is not stochastic.[0.50.50.20.7] has columns adding to 1 and 0.9. After many steps the total chance leaks away, and the eigenvalues are not 1 and a smaller number: the eigenvalue-1 theorem needs every column sum to be exactly 1.
Convergence is about the matrix, not the starting point. For the four-page web every starting probability vector leads to the same π, because the hypotheses hold. The visualizer starts at page 1. Starting at page 3 changes the path and leaves the limit the same.
Where it is used
Used inside maths· Inside maths: random walks on graphs, and the stationary distribution given by the degrees
On an undirected graph, let a walker at a vertex move to a uniformly random neighbour. Take the graph with four vertices A,B,C,D and edges AB, BC, CA, CD (a triangle with a tail). The degrees (numbers of edges at each vertex) are 2,2,3,1, adding to 8, which is twice the number of edges. Claim: πv=deg(v)/8, that is π=(2,2,3,1)/8, is stationary.
Proof for any such graph, with m edges. The chance of going from u to v is 1/deg(u) when u,v are neighbours. So (Pπ)v=∑u∼vdegu1⋅2mdegu=2m#{u:u∼v}=2mdegv=πv. The factor degu cancels, and the sum counts the neighbours of v. So a walker visits a vertex in proportion to its degree. Check on the triangle with a tail: the second entry of Pπ is 21⋅82+31⋅83=81+81=82. This is the idea of the CS 240 graph chapters turned into linear algebra: the adjacency structure, scaled, is a stochastic matrix. (The chain on this graph has eigenvalues 1, −21 and two others of size less than 1, so the walker’s chances settle.)
Used in the real world· PageRank: the 25 billion dollar eigenvector
The name PageRank plays on Larry Page’s surname as well as on “web page”. Bryan and Leise’s article (SIAM Review, 2006) is the standard mathematical account, and its title is where Spagnolie’s aside came from. The picture is the one in this chapter: the web is a directed graph, P puts weight 1/ℓ on each of a page’s ℓ links, d=0.85, and the ranking is the stationary vector of G. Each page’s score is πi. A page with few but important links in beats a page with many obscure ones. In the worked example, page 3 had the most links in, and page 1 still came first because page 3, the most-linked page, passed its entire share to page 1.
The real computation: about 50 to 100 sparse matrix-vector products, each costing one operation per link. At d=0.85 the bound 2dk is below 10−3 by k=50 whatever the size of the web, which is why the method scales to billions of pages. Search engines today combine PageRank-like scores with hundreds of other signals; the matrix described here is the original idea.
Used in the real world· Queueing: a shop with room for two customers
A tiny shop has room for two customers. Each minute, with chance 0.3 one customer walks in (if there is room), or with chance 0.5 one customer finishes and leaves (if anyone is there); otherwise nothing changes. The states are 0,1,2 customers, and
P=0.70.300.50.20.300.50.5.
Solving Pπ=π: the first row gives 0.3π0=0.5π1, so π1=0.6π0, and the last row gives 0.3π1=0.5π2, so π2=0.6π1=0.36π0. The total is 1.96π0=1, so π=(25,15,9)/49≈(0.51,0.31,0.18). The shop is empty half the time and full about 18 percent of the time, and the owner can find out whether a second server is worth hiring by changing the numbers and re-solving. Telephone exchanges and computer networks are analysed with the same equations, and the weather model of the worked example is a two-state version.
Used in the real world· Generating text: the ancestor of language models (CS 532 and AI)
Take the text “the cat sat on the mat . the cat ate the fish .” and make each distinct token a state. Count how often each token follows each token and divide by the totals. The token “the” is followed by “cat” twice, “mat” once and “fish” once, so column “the” has 21 for cat, 41 for mat and 41 for fish. “cat” is followed by “sat” or “ate”, each with chance 21. Every other token has one possible successor. Treating the text as a loop (the last ”.” leads back to “the”) makes the matrix stochastic, and its stationary distribution is exactly the word frequencies: “the” has 4/13, “cat” and ”.” have 2/13 each, and the other five tokens have 1/13 each. Walking the chain generates sentences like “the cat ate the mat .” that follow the statistics of the text, with no idea what they mean.
This is an n-gram model (here a bigram, the last 1 word). Claude Shannon showed text built this way in his 1948 paper on information theory. A modern language model, which predicts the next token from the last N tokens of its context window, is a Markov chain too, formally a chain whose state is the whole window. The states are far too many to write as a matrix, and the transition chances come from a neural network rather than a table. The object (a state, a next-state distribution, a sequence) is still the one in this chapter.
Beyond the course: for understanding· Markov chain Monte Carlo (a mention)
Often you want random samples from a hard distribution π (a Bayesian posterior over model weights, for example), and you cannot sample from it directly. The idea of Markov chain Monte Carlo is to build a chain whose stationary distribution is π and run it for a long time. The Metropolis–Hastings algorithm constructs such a chain by accepting or rejecting random moves so that the “detailed balance” condition holds (see the stretch problem below). The convergence theory is the theory of this chapter: the chain must be regular, and its second eigenvalue sets how long the walk takes to forget its start.
Traps
Watch out· Columns add to 1, not rows
In this book Pij is the chance of going from j to i, so the COLUMNS add to 1 and the chances are a column vector multiplied on the right. In many probability textbooks it is the other way: rows add to 1 and the chances form a row vector multiplied on the left. If you copy a formula between books, transpose it. Checking the column sums is the first thing to do with any new matrix.
Watch out· Stationary does not mean the chain converges
Pπ=π only says π is unchanged by a step. The bouncing chain [0110] has a stationary vector (21,21) and never approaches it. Convergence needs the other eigenvalues to have size less than 1, which is what regularity (or damping) gives.
Watch out· The eigenvector for 1 is of P, not of P-transpose
The proof that 1 is an eigenvalue uses the vector 1, which is an eigenvector of PT. It is not the stationary distribution. The stationary distribution is an eigenvector of P itself, and it is not all ones (that would give every page the same rank). P and PT share eigenvalues, not eigenvectors.
Watch out· Do not forget to scale
Solving (P−I)π=O gives a line of solutions, a null space. The stationary distribution is the one on that line whose entries add to 1. Writing (2,1) for the weather chain instead of (32,31) is the most common slip.
Watch out· Eigenvalues can be complex
The three-page web has eigenvalues 2−1±i. The theorem ∣λ∣≤1 includes them, and this is why the proof uses the size of a complex number. Complex eigenvalues are what make the bars in the visualizer rise and fall as they settle instead of approaching monotonically.
Watch out· Damping changes the answer
The PageRank vector is the stationary distribution of G, not of P. They are different vectors (for the four pages, (0.368,0.142,0.288,0.202) against (0.387,0.129,0.290,0.194)). The damped one is the ranking that is used in practice, and the small 1−d share of random jumping is what makes it exist and be unique.
Problems
check
Is [0.50.50.20.7] stochastic? Is [0.30.70.60.4]?
Solution
The first has column sums 1.0 and 0.9, so it is not stochastic. The second has column sums 1.0 and 1.0 and no negative entries, so it is.
check
Three pages: page 1 links to page 2, page 2 links to pages 1 and 3, page 3 links to page 1. Write P, and find the surfer’s chances after one step from page 2.
Solution
Column 1 is (0,1,0) (one link, to page 2). Column 2 is (21,0,21). Column 3 is (1,0,0). So P=01021021100. Starting at x0=(0,1,0), x1=Px0 is column 2: (21,0,21).
check
Find the stationary distribution of [0.90.10.50.5].
Solution
(P−I)π=O gives −0.1π1+0.5π2=0, so π1=5π2. A basis vector is (5,1); scaled to add to 1, π=(65,61). Check: 0.9⋅65+0.5⋅61=64.5+0.5=65.
use
For P=[0.70.30.20.8] find the stationary distribution, the other eigenvalue, and a formula for xk starting from x0=(1,0).
Solution
(P−I)π=O gives −0.3π1+0.2π2=0, so π2=1.5π1; the vector (2,3) scaled to sum 1 is π=(0.4,0.6). The trace is 1.5, so the other eigenvalue is 0.5, with eigenvector (1,−1) (check: P(1,−1)=(0.5,−0.5)). Write x0=(1,0)=π+0.6(1,−1). Then xk=π+0.6(0.5)k(1,−1)=(0.4+0.6⋅0.5k,0.6−0.6⋅0.5k). Check k=1: (0.7,0.3), the first column of P.
use
A walker on the path with three vertices 1−2−3 moves to a uniformly random neighbour. Write P, find the stationary distribution, and say what happens to Pkx0 from x0=(1,0,0).
Solution
Vertex 1 has one neighbour (2), vertex 2 has neighbours 1 and 3, vertex 3 has one neighbour (2). So P=01021021010. By the degree rule the stationary distribution is π=(1,2,1)/4; check: row 1 of Pπ is 21⋅42=41, row 2 is 41+41=21. From x0=(1,0,0) the walker alternates: x1=(0,1,0), x2=(21,0,21), x3=(0,1,0), x4=(21,0,21), and so on. It never settles because the graph is bipartite and the chain is periodic: the eigenvalues are 1,0,−1, and the eigenvalue −1 has size 1.
use
Pages: 1 links to 2; 2 links to 1 and 3; 3 links to 1. Find the PageRank vector with d=21.
Solution
P=01021021100 and n1−d=61. Solve (I−21P)π=611: π1−41π2−21π3=61, π2−21π1=61, π3−41π2=61. From the last two, π2=61+21π1 and π3=61+41π2=61+241+81π1=245+81π1. The first equation then reads π1−241−81π1−485−161π1=61, so 1613π1=61+241+485=488+2+5=4815, giving π1=4815⋅1316=135. Then π2=61+265=7813+15=3914 and π3=245+1045=31265+15=3910. So π=(15,14,10)/39, which adds to 1. (Undamped, it is (2,2,1)/5.)
prove
Prove that if P is a stochastic matrix then Pk is stochastic for every k≥1. Then prove that for a probability vector x and every k, ∥Pkx∥1=1.
Solution
For k=1 this is the hypothesis. If Pk is stochastic, then Pk+1=P⋅Pk is a product of two stochastic matrices, hence stochastic by the product theorem (the argument: its entries are sums of products of non-negative numbers, and its column sums are ∑i∑lPil(Pk)lj=∑l(Pk)lj∑iPil=∑l(Pk)lj=1). By induction every Pk is stochastic. For a probability vector x, Pkx is a probability vector by the theorem, and the 1-norm of a vector with non-negative entries is the sum of its entries, which is 1.
prove
Let M be a square matrix whose rows (not columns) each add to 1. Prove that 1 is an eigenvalue of M, with eigenvector 1.
Solution
Entry i of M1 is the sum of row i of M, which is 1. So M1=1 and 1=O, so 1 is an eigenvalue with eigenvector 1. (This is the row-stochastic convention of probability textbooks; for our column-stochastic matrices the same eigenvector belongs to PT, and the eigenvalue moves to P through the characteristic polynomial.)
prove
Prove that for a stochastic matrix P and a vector x with all entries non-negative, ∥Px∥1=∥x∥1. Where in the proof that ∥Px∥1≤∥x∥1 does the argument become an equality?
Solution
For x≥0 every entry of Px is non-negative, so ∥Px∥1 is the sum of the entries of Px, which is ∑jxj∑iPij=∑jxj=∥x∥1. In the earlier proof the only inequality was the triangle inequality ∣∑jPijxj∣≤∑jPij∣xj∣, and when every xj≥0 the terms Pijxj are non-negative, so the absolute values change nothing and the inequality is an equality.
prove
Let G=dP+n1−d11T with P stochastic. Prove that for every probability vector x, Gx=dPx+n1−d1, and that Gx is a probability vector.
Solution
Gx=dPx+n1−d1(1Tx). For a probability vector 1Tx=1, so Gx=dPx+n1−d1. The entries are non-negative (a sum of non-negative terms). They add to d⋅1+n⋅n1−d=1, using that Px adds to 1. So Gx is a probability vector. This is why one never needs to write the dense matrix G.
stretchthe idea behind Metropolis–Hastings
Let P be stochastic and π a probability vector satisfying detailed balance: Pijπj=Pjiπi for all i,j (the flow from j to i equals the flow from i to j). Prove that π is stationary. Use it to re-prove that πv=deg(v)/2m is stationary for the random walk on a graph.
Solution
(Pπ)i=∑jPijπj=∑jPjiπi=πi∑jPji. The last sum is the total chance of leaving state i for some state j, which is column i of P added up: it equals 1. So (Pπ)i=πi. For the graph walk, Pij=1/deg(j) when i∼j, and Pijπj=degj1⋅2mdegj=2m1, which is symmetric in i and j. So detailed balance holds (for non-neighbours both sides are 0) and π is stationary.
stretch
For the two-state chain P=[1−aab1−b] with 0<a,b≤1, find the stationary distribution, the other eigenvalue, and decide when Pkx0 converges for every probability vector x0.
Solution
(P−I)π=O gives −aπ1+bπ2=0, so π∝(b,a) and π=a+b(b,a). The trace is 2−a−b, so the other eigenvalue is 1−a−b, with eigenvector (1,−1). Its size is less than 1 exactly when 0<a+b<2. Since a,b>0 the sum is positive, and a+b=2 only for a=b=1, the bouncing chain, where the eigenvalue is −1. So Pkx0→π for every x0 unless a=b=1. (Check with the weather chain, a=0.2, b=0.4: π=(0.4,0.2)/0.6=(32,31) and the other eigenvalue is 0.4.)
Summary
A Markov chain moves among states with fixed chances Pij (from j to i). The chances of being in each state evolve by xk+1=Pxk.
A stochastic matrix has non-negative entries and columns adding to 1. It keeps probability vectors probability vectors, because the total of the entries of Px is 1TPx=1Tx.
A stationary distribution is a probability vector with Pπ=π: an eigenvector for eigenvalue 1. Every stochastic matrix has eigenvalue 1 (because PT1=1 and P, PT share eigenvalues) and no eigenvalue larger than 1 in size (the 1-norm never grows).
If the other eigenvalues have size less than 1, repeated steps kill their parts and Pkx0→π at the rate ∣λ2∣k. This holds for regular chains (Perron–Frobenius, stated here without proof). The power method computes π using only matrix-times-vector.
The real web has dead ends, cycles, disconnected pieces and spider traps. Teleporting with chance 1−d gives the Google matrixG=dP+n1−d11T, with d=0.85.
For G we proved everything: a unique π with entries at least n1−d, error at most 2dk from any start, and every other eigenvalue of size at most d. The bound does not depend on the size of the web.
The four-page web has π=(12,4,9,6)/31 undamped and about (0.368,0.142,0.288,0.202) at d=0.85. A random walk on an undirected graph has πv=deg(v)/2m.
Text generated word by word from counts (an n-gram model) is a Markov chain, as is a language model on its context window; Markov chain Monte Carlo designs a chain with the stationary distribution you want.
Watch and read
The same ideas explained by someone else. Use these when an explanation here does not land.
MATH 431: A Markov chain is a sequence of random states in which the next state depends only on the current one. The identity x_{k+1} = P x_k below is the law of total probability: P(next is i) is the sum over j of P(next is i given now j) times P(now is j). If you meet conditional probability in 431, this is the same sum written as a matrix product.
CS 240: A directed graph is the picture of a web: pages are vertices, links are arrows. The matrix here is the graph's adjacency matrix with each column divided by the number of out-arrows. The random walk on an undirected graph, below, has a stationary distribution given by the degrees.
CS 532 / AI: A language model that predicts the next word from the last N words is a Markov chain on the last N words. The n-gram model below is its simplest ancestor. Markov chain Monte Carlo, which samples from hard probability distributions, designs a chain whose stationary distribution is the one you want.