Markov chains: transition matrices, stationary distributions and absorption
Ten problems on Markov chains: two-step probabilities from P², the distribution after n steps as a row vector times Pⁿ, the stationary distribution from πP = π, the two-state chain in closed form, how many steps until it has mixed from the second eigenvalue, expected return times 1/πᵢ and a hitting time, gambler's ruin and a checkout funnel with the fundamental matrix (I − Q)⁻¹, a random walk on a graph with detailed balance and a chain that is not reversible, and PageRank with damping, with worked solutions and the mistakes of multiplying P by a column vector, reading the stationary distribution off a right eigenvector, expecting a periodic chain to converge, using 1/πⱼ as a hitting time and multiplying win probabilities in gambler's ruin.
Before you start
A Markov chain is a random process that forgets its past: where it goes next depends only on where it is now. All of its behavior is stored in one matrix of transition probabilities, and nearly every question about it is a matrix computation. The distribution after n steps is a row vector times ,Pn, the long-run behavior is the left eigenvector of P for the eigenvalue ,1, the speed at which the chain forgets its start is set by the next-largest eigenvalue, and the expected time until something happens comes from one matrix inverse. The same objects sit under reinforcement learning, where a policy turns an environment into a Markov chain, under Markov chain Monte Carlo, whose samplers are designed through detailed balance, and under PageRank. These ten problems compute each of them by hand on small chains.
A chain on states 1,…,K has transition matrix P with .Pij=Pr(Xt+1=j∣Xt=i). Every entry is ≥0 and every row sums to .1.
Distributions are row vectors: if Xt has distribution ,πt, then πt+1=πtP and .πn=π0Pn. The n-step probability Pr(Xn=j∣X0=i) is the entry .(Pn)ij.
A stationary distribution satisfies πP=π with πi≥0 and :∑iπi=1: it is a left eigenvector of P for the eigenvalue ,1, that is an eigenvector of ,P⊤, scaled to sum to .1. If every state can reach every other and the chain is aperiodic, π is unique, π0Pn→π from any start, and the expected time to return to state i is .1/πi.
Every eigenvalue of P has .∣λ∣≤1. For a chain that converges, the distance from π shrinks roughly like ,∣λ2∣n, where λ2 is the eigenvalue of next-largest size.
Detailed balance. If πiPij=πjPji for all ,i,j, then π is stationary and the chain is called reversible.
Absorbing chains. Order the states transient first and write .P=(Q0RI). The fundamental matrix N=(I−Q)−1 has Nij = expected number of visits to transient j starting from ;i; the row sums N1 are the expected numbers of steps before absorption, and B=NR holds the absorption probabilities.
Each day is sunny (S) or rainy (R). After a sunny day the next is sunny with probability ;0.9; after a rainy day it is sunny with probability .0.5. Write the transition matrix, and find the probability that it rains two days after a sunny day.
·
A walker on three states moves with
P=0.50.2500.50.50.500.250.5
and starts in state .1. Find its distribution after one, two and three steps.
··
Find the stationary distribution of the chain in Problem 2, and compare the distributions after one, two and three steps with it.
··
For the two-state chain P=(1−aba1−b) with ,0<a+b<2, find the stationary distribution, the eigenvalues of P and a formula for .(Pn)12. Check it against Problem 1.
··
For the weather chain of Problem 1, how many days ahead must you look before the probability of rain is within 0.001 of its long-run value ,61, starting from a sunny day? Starting from a rainy day?
··
In the weather chain, find the expected number of days from one rainy day to the next, and the expected number of days from a sunny day until the first rainy day. Check the first answer by conditioning on the first step.
···
A gambler has 1 chip and bets one chip at a time on fair coin flips, stopping at 0 or 4 chips. With transient states 1,2,3 (the number of chips), compute the fundamental matrix, the expected number of bets, and the probability of reaching 4 chips, from each starting amount.
···
Users on a site move between Browse and Cart each minute until they Buy or Leave. From Browse they stay with probability ,0.5, go to Cart with 0.3 and leave with .0.2. From Cart they go back to Browse with ,0.2, buy with 0.5 and leave with .0.3. For a user who starts browsing, find the probability of a purchase and the expected number of minutes before buying or leaving.
···
A random walker on the graph with vertices A,B,C,D and edges ,AB,,BC,,CD,,DA,AC moves to a uniformly chosen neighbor at each step. Show that πv=deg(v)/(2∣E∣) satisfies detailed balance, and find the expected return time to .A. Then show that the chain on three states with P12=P23=P31=32 and P21=P32=P13=31 has a uniform stationary distribution but is not reversible.
···
Three web pages link as ,1→2,,1→3,2→3 and .3→1. A surfer follows a uniformly chosen link with probability d=0.85 and otherwise jumps to a uniformly chosen page, so
G=dP+31−d11⊤.
Find the PageRank vector ,πG=π, and compare it with the stationary distribution of the link chain P alone.
Answers
;P=(0.90.50.10.5); the probability of rain two days after a sunny day is 0.14
;π=(41,21,41); the distance from it halves each step
,π=(a+bb,a+ba), eigenvalues 1 and ,1−a−b,(Pn)12=a+ba(1−(1−a−b)n)
6 days from a sunny start, 8 days from a rainy start
6 days between rainy days; 10 days from a sunny day to the first rain
From 1 chip: 3 bets on average and probability 41 of reaching ;4; from :2:4 bets and ;21; from :3:3 bets and 43
;Pr(Buy)=4415≈0.341; expected time 0.441+0.3=2265≈2.95 minutes
Detailed balance holds with π=(0.3,0.2,0.3,0.2) and the return time to A is ;310; the rotating chain has π=(31,31,31) but π1P12=92=91=π2P21
,π≈(0.388,0.215,0.397), exactly ;17691(686,380,703); without damping π=(0.4,0.2,0.4)
Worked solutions
Problem 1
Each day is sunny (S) or rainy (R). After a sunny day the next is sunny with probability ;0.9; after a rainy day it is sunny with probability .0.5. Write the transition matrix, and find the probability that it rains two days after a sunny day.
,P=(0.90.50.10.5), states in the order S, RRow i is the distribution of tomorrow given today is ;i; each row sums to .1.
Pr(X2=R∣X0=S)=PSSPSR+PSRPRRCondition on tomorrow's weather: sunny then rainy, or rainy then rainy.
=0.9⋅0.1+0.1⋅0.5=0.09+0.05=0.14The two paths are disjoint, so their probabilities add.
P2=(0.860.700.140.30)Step 2 is the (S,R) entry of :P2: row S of P times column .R. Its rows still sum to .1.
;P=(0.90.50.10.5); the probability of rain two days after a sunny day is 0.14After a rainy day it is :0.30: the chain still remembers the start after two steps, but less than after one (0.1 against ).0.5).
Problem 2
A walker on three states moves with
P=0.50.2500.50.50.500.250.5
and starts in state .1. Find its distribution after one, two and three steps.
π0=(1,0,0)Certain to be in state ;1; a row vector.
π1=π0P=(0.5,0.5,0)Multiplying a one-hot row vector by P picks out a row of .P.
π2=0.5(0.5,0.5,0)+0.5(0.25,0.5,0.25)=(0.375,0.5,0.125)π1P is the mixture of the rows of ,P, weighted by .π1.
π3=0.375(0.5,0.5,0)+0.5(0.25,0.5,0.25)+0.125(0,0.5,0.5)=(0.3125,0.5,0.1875)Each entry is still a sum of nonnegative terms, and the total stays .1.
,π1=(21,21,0),,π2=(83,21,81),π3=(165,21,163)Writing P times the column (1,0,0)⊤ instead gives the first column of ,P, which is not a distribution (Mistake 1).
Problem 3
Find the stationary distribution of the chain in Problem 2, and compare the distributions after one, two and three steps with it.
,π1=0.5π1+0.25π2,,π2=0.5π1+0.5π2+0.5π3,π3=0.25π2+0.5π3πP=π entry by entry: column j of P gives the equation for .πj. Here πi are the entries of .π.
π2=2π1 and π2=2π3From the first and third equations; the middle one then holds automatically, since one equation of πP=π is always redundant.
,π1+2π1+π1=1, so π1=41The normalization replaces the redundant equation.
,π0P−π=(41,0,−41),,π0P2−π=(81,0,−81),π0P3−π=(161,0,−161)The distributions of Problem 2 minus the stationary one. The gap halves at every step.
;π=(41,21,41); the distance from it halves each stepThe eigenvalues of P are ,1,21 and ,0, and the halving is .λ2=21. The eigenvector that np.linalg.eig(P) returns for λ=1 is ,(1,1,1), which is not π (Mistake 2).
Problem 4
For the two-state chain P=(1−aba1−b) with ,0<a+b<2, find the stationary distribution, the eigenvalues of P and a formula for .(Pn)12. Check it against Problem 1.
,π1a=π2b, so π=(a+bb,a+ba)In a two-state chain the flow from 1 to 2 must equal the flow back.
λ1=1 and λ2=trP−1=1−a−bEvery stochastic matrix has ,P1=1, so 1 is an eigenvalue, and the two eigenvalues add to the trace .2−a−b.
Pn=a+b1(bbaa)+a+bλ2n(a−b−ab)The first matrix has both rows equal to ;π; the second is the part that decays. At n=0 the sum is ,I, and multiplying by P multiplies the second part by .λ2.
(Pn)12=a+ba(1−λ2n)The (1,2) entry of step 3.
,π=(a+bb,a+ba), eigenvalues 1 and ,1−a−b,(Pn)12=a+ba(1−(1−a−b)n)Problem 1 has ,a=0.1,:b=0.5:,π=(65,61),,λ2=0.4, and .(P2)SR=61(1−0.16)=0.14. At a=b=1 the chain alternates, ,λ2=−1, and Pn never settles (Mistake 3).
Problem 5
For the weather chain of Problem 1, how many days ahead must you look before the probability of rain is within 0.001 of its long-run value ,61, starting from a sunny day? Starting from a rainy day?
From sunny: (Pn)SR−61=−61(0.4)nProblem 4, step 4, with .λ2=0.4.
61(0.4)n<0.001 needs ,0.4n<0.006, so n>ln0.4ln0.006≈5.58Dividing by ln0.4<0 flips the inequality.
From rainy: (Pn)RR−61=65(0.4)nThe (2,2) entry of Problem 4, step 3: .a+ba+a+bbλ2n.
65(0.4)n<0.001 needs ,0.4n<0.0012, so n>ln0.4ln0.0012≈7.34A larger starting gap, the same rate.
6 days from a sunny start, 8 days from a rainy startCheck: 61(0.4)6≈0.00068 and ,65(0.4)8≈0.00055, while 61(0.4)5≈0.0017 and 65(0.4)7≈0.0014 are still too big. Each day multiplies the gap by .∣λ2∣=0.4.
Problem 6
In the weather chain, find the expected number of days from one rainy day to the next, and the expected number of days from a sunny day until the first rainy day. Check the first answer by conditioning on the first step.
Return time to R =1/πR=6 daysThe chain spends a fraction 61 of its days in R, so visits are 6 days apart on average.
From S, each day is rainy with probability ,0.1, independently, until the first one: hS→R=1/0.1=10The number of steps until the first success is geometric with mean .1/p.
Return time to R =1+0.5⋅0+0.5⋅hS→ROne step, then either it rains again at once (probability )0.5) or the chain is in S and must still reach R.
=1+0.5⋅10=6Agrees with step 1.
6 days between rainy days; 10 days from a sunny day to the first rain1/πR is a return time, not the time to get there from somewhere else: using 6 for the trip from S is Mistake 4. Likewise the return time to S is .1/πS=56=1+0.1⋅2.
Problem 7
A gambler has 1 chip and bets one chip at a time on fair coin flips, stopping at 0 or 4 chips. With transient states 1,2,3 (the number of chips), compute the fundamental matrix, the expected number of bets, and the probability of reaching 4 chips, from each starting amount.
,Q=0210210210210,R=21000021Rows of Q are moves among 1,2,3 chips; the columns of R are absorption at 0 and at .4.
N=(I−Q)−1=1.510.51210.511.5Check: ;(I−Q)N=I; for example row 1 of I−Q is ,(1,−21,0), and with the first column of N it gives .1.5−0.5=1.
N1=(3,4,3)Expected number of bets. It matches the formula k(4−k) for a fair game.
B=NR=0.750.50.250.250.50.75Row k is (ruin, reach )4) from k chips; each row sums to .1.
From 1 chip: 3 bets on average and probability 41 of reaching ;4; from :2:4 bets and ;21; from :3:3 bets and 43A fair game keeps the expected fortune constant: .41⋅4=1. Three straight wins, ,81, is only one of the ways to get there (Mistake 5).
Problem 8
Users on a site move between Browse and Cart each minute until they Buy or Leave. From Browse they stay with probability ,0.5, go to Cart with 0.3 and leave with .0.2. From Cart they go back to Browse with ,0.2, buy with 0.5 and leave with .0.3. For a user who starts browsing, find the probability of a purchase and the expected number of minutes before buying or leaving.
,Q=(0.50.20.30),R=(00.50.20.3)Transient states Browse, Cart; absorbing states Buy, Leave. Each row of (Q∣R) sums to .1.
,I−Q=(0.5−0.2−0.31),det=0.5−0.06=0.44A 2×2 inverse needs the determinant.
N=0.441(10.20.30.5)≈(2.2730.4550.6821.136)Swap the diagonal, negate the off-diagonal, divide by the determinant.
Pr(Buy)=N11R11+N12R21=0+0.440.3⋅0.5=4415First column of .B=NR. Only Cart leads to Buy, so the answer is the expected number of Cart visits times .0.5.
;Pr(Buy)=4415≈0.341; expected time 0.441+0.3=2265≈2.95 minutesThe leave probability is ,4429≈0.659, and the two add to .1. A user who has reached the cart buys with probability ,4425≈0.568, which is the number to optimize.
Problem 9
A random walker on the graph with vertices A,B,C,D and edges ,AB,,BC,,CD,,DA,AC moves to a uniformly chosen neighbor at each step. Show that πv=deg(v)/(2∣E∣) satisfies detailed balance, and find the expected return time to .A. Then show that the chain on three states with P12=P23=P31=32 and P21=P32=P13=31 has a uniform stationary distribution but is not reversible.
Degrees ,A:3,,B:2,,C:3,,D:2, and ,2∣E∣=10, so the candidate is π=(0.3,0.2,0.3,0.2)Each edge adds 1 to two degrees.
For an edge :uv:πuPuv=10degu⋅degu1=101=πvPvuThe degree cancels; for non-neighbors both sides are .0.
Detailed balance implies πP=πSum πiPij=πjPji over :i: the right side gives .πj∑iPji=πj.
Return time to A=1/0.3=310The busier vertices are visited more often.
Detailed balance holds with π=(0.3,0.2,0.3,0.2) and the return time to A is ;310; the rotating chain has π=(31,31,31) but π1P12=92=91=π2P21Its columns also sum to ,1, so the uniform π is stationary, yet probability circulates 1→2→3→1 on net. Detailed balance is sufficient for stationarity, not necessary; MCMC samplers are built to satisfy it because it is easy to check one pair at a time.
Problem 10
Three web pages link as ,1→2,,1→3,2→3 and .3→1. A surfer follows a uniformly chosen link with probability d=0.85 and otherwise jumps to a uniformly chosen page, so
G=dP+31−d11⊤.
Find the PageRank vector ,πG=π, and compare it with the stationary distribution of the link chain P alone.
P=0010.5000.510Page 1 splits its probability over two links.
πj=0.05+0.85∑iπiPijπ31−d11⊤=0.051⊤ because .∑iπi=1.
,π1=0.05+0.85π3,,π2=0.05+0.425π1,π3=0.05+0.85(0.5π1+π2)Column by column.
Substitute π2 and π3=(π1−0.05)/0.85 into :π1+π2+π3=1:2.21125π1=0.8575Multiply through by 0.85 first.
,π≈(0.388,0.215,0.397), exactly ;17691(686,380,703); without damping π=(0.4,0.2,0.4)Page 3 ranks first because it collects links from both other pages. The jumps pull every score toward ,31, and they make the chain converge at the rate 0.85⋅∣λ2(P)∣=0.85⋅0.707≈0.601 per step, since P's other eigenvalues are .−21±21i.
Where this goes wrong
1. Multiplying P by a column vector
Most linear algebra applies a matrix to a column vector, and the start distribution gets written as one.
,π0=(1,0), sunny today, and P=(0.90.50.10.5)Right so far: Problem 1.
“Tomorrow's distribution is .Pπ0⊤.”With rows indexed by today, the distribution must multiply from the left: .π1=π0P.
Pπ0⊤=(0.9,0.5)⊤Its entries add to ,1.4, so it is not a distribution: it is the first column of ,P, the probability of sun tomorrow from each state today. The correct .π1=(0.9,0.1).
2. The stationary distribution read off a right eigenvector
np.linalg.eig(P) returns eigenvectors, and the one for λ=1 looks like the answer.
P from Problem 2 has eigenvalue 1Right so far: every stochastic matrix does.
“Take the eigenvector of P for λ=1 and normalize it.”That solves ;Pv=v; the stationary distribution solves ,πP=π, an eigenvector of .P⊤.
π=(31,31,31)P1=1 for every stochastic matrix, so this is always the uniform vector. Check: .(31,31,31)P=(0.25,0.5,0.25)=(31,31,31). Use np.linalg.eig(P.T), which gives .π=(41,21,41).
3. Expecting every chain to converge to π
A stationary distribution exists, so the chain is assumed to approach it.
P=(0110) has π=(21,21)Right so far: ,πP=π, and it is Problem 4 with .a=b=1.
“So π0Pn→(21,21) from .π0=(1,0).”Convergence needs the chain to be aperiodic as well; this one returns to each state only at even times.
π100≈(21,21)πn alternates between (0,1) and ,(1,0), so .π100=(1,0). The eigenvalues are 1 and ,−1, and ∣λ2∣=1 means the gap never shrinks. Only the long-run average of the πn is .(21,21). Adding a self-loop, as PageRank's jumps do, removes the period.
4. Using 1/πⱼ as the time to reach j
The return time 1/πj is the one expected-time formula that needs no linear algebra.
πR=61 in the weather chainRight so far: Problem 4.
“Starting from a sunny day, rain comes after 1/πR days on average.”1/πR is the mean time from R back to R. From S the chain must first leave S, which it does slowly.
hS→R=6 daysFrom S the wait is geometric with success probability ,0.1, so .hS→R=10. The return time 6 averages that with the immediate repeat: 1+0.5⋅10=6 (Problem 6).
5. Multiplying the win probabilities in gambler's ruin
Reaching 4 chips from 1 takes three more wins than losses, and three wins in a row is the obvious way.
Each bet is won with probability ,21, independentlyRight so far: Problem 7.
“Reaching 4 chips from 1 needs three wins, so the probability is .(21)3.”That counts only the path win–win–win. A loss followed by two wins and so on can still reach ,4, as long as the gambler never hits 0 first.
Pr(reach 4∣start at 1)=81The fundamental matrix gives :41: the first row of N is ,(1.5,1,0.5), and .Pr=N13⋅21=0.25. Fairness confirms it: the expected final fortune 4⋅41=1 equals the starting 1 chip, while 4⋅81=0.5 would lose chips on average in a fair game.