Practice / Probability for ML

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 nn steps is a row vector times PnP^n, the long-run behavior is the left eigenvector of PP for the eigenvalue 11, 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,…,K1, \dots, K has transition matrix PP with Pij=Pr⁡(Xt+1=j∣Xt=i)P_{ij} = \Pr(X_{t+1} = j \mid X_t = i). Every entry is ≥0\ge 0 and every row sums to 11.
  • Distributions are row vectors: if XtX_t has distribution πt\pi_t, then πt+1=πtP\pi_{t+1} = \pi_tP and πn=π0Pn\pi_n = \pi_0P^n. The nn-step probability Pr⁡(Xn=j∣X0=i)\Pr(X_n = j \mid X_0 = i) is the entry (Pn)ij(P^n)_{ij}.
  • A stationary distribution satisfies πP=π\pi P = \pi with πi≥0\pi_i \ge 0 and ∑iπi=1\sum_i\pi_i = 1: it is a left eigenvector of PP for the eigenvalue 11, that is an eigenvector of P⊤P^\top, scaled to sum to 11. If every state can reach every other and the chain is aperiodic, π\pi is unique, π0Pn→π\pi_0P^n \to \pi from any start, and the expected time to return to state ii is 1/πi1/\pi_i.
  • Every eigenvalue of PP has ∣λ∣≤1|\lambda| \le 1. For a chain that converges, the distance from π\pi shrinks roughly like ∣λ2∣n|\lambda_2|^n, where λ2\lambda_2 is the eigenvalue of next-largest size.
  • Detailed balance. If πiPij=πjPji\pi_iP_{ij} = \pi_jP_{ji} for all i,ji, j, then π\pi is stationary and the chain is called reversible.
  • Absorbing chains. Order the states transient first and write P=(QR0I)P = \begin{pmatrix} Q & R \\ 0 & I\end{pmatrix}. The fundamental matrix N=(I−Q)−1N = (I - Q)^{-1} has NijN_{ij} = expected number of visits to transient jj starting from ii; the row sums N1N\mathbf 1 are the expected numbers of steps before absorption, and B=NRB = NR holds the absorption probabilities.

Builds on: Discrete random variables: expectation, variance and the binomial, geometric and Poisson, Eigenvalues and eigenvectors by hand

Problems

  1. ·

    Each day is sunny (S) or rainy (R). After a sunny day the next is sunny with probability 0.90.9; after a rainy day it is sunny with probability 0.50.5. Write the transition matrix, and find the probability that it rains two days after a sunny day.

  2. ·

    A walker on three states moves with

    P=(0.50.500.250.50.2500.50.5)P = \begin{pmatrix} 0.5 & 0.5 & 0 \\ 0.25 & 0.5 & 0.25 \\ 0 & 0.5 & 0.5 \end{pmatrix}

    and starts in state 11. Find its distribution after one, two and three steps.

  3. ··

    Find the stationary distribution of the chain in Problem 2, and compare the distributions after one, two and three steps with it.

  4. ··

    For the two-state chain P=(1−aab1−b)P = \begin{pmatrix} 1 - a & a \\ b & 1 - b\end{pmatrix} with 0<a+b<20 < a + b < 2, find the stationary distribution, the eigenvalues of PP and a formula for (Pn)12(P^n)_{12}. Check it against Problem 1.

  5. ··

    For the weather chain of Problem 1, how many days ahead must you look before the probability of rain is within 0.0010.001 of its long-run value 16\tfrac16, starting from a sunny day? Starting from a rainy day?

  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.

  7. ···

    A gambler has 11 chip and bets one chip at a time on fair coin flips, stopping at 00 or 44 chips. With transient states 1,2,31, 2, 3 (the number of chips), compute the fundamental matrix, the expected number of bets, and the probability of reaching 44 chips, from each starting amount.

  8. ···

    Users on a site move between Browse and Cart each minute until they Buy or Leave. From Browse they stay with probability 0.50.5, go to Cart with 0.30.3 and leave with 0.20.2. From Cart they go back to Browse with 0.20.2, buy with 0.50.5 and leave with 0.30.3. For a user who starts browsing, find the probability of a purchase and the expected number of minutes before buying or leaving.

  9. ···

    A random walker on the graph with vertices A,B,C,DA, B, C, D and edges ABAB, BCBC, CDCD, DADA, ACAC moves to a uniformly chosen neighbor at each step. Show that πv=deg⁡(v)/(2∣E∣)\pi_v = \deg(v)/(2|E|) satisfies detailed balance, and find the expected return time to AA. Then show that the chain on three states with P12=P23=P31=23P_{12} = P_{23} = P_{31} = \tfrac23 and P21=P32=P13=13P_{21} = P_{32} = P_{13} = \tfrac13 has a uniform stationary distribution but is not reversible.

  10. ···

    Three web pages link as 1→21 \to 2, 1→31 \to 3, 2→32 \to 3 and 3→13 \to 1. A surfer follows a uniformly chosen link with probability d=0.85d = 0.85 and otherwise jumps to a uniformly chosen page, so

    G=dP+1−d311⊤.G = dP + \frac{1 - d}{3}\mathbf{1}\mathbf{1}^\top .

    Find the PageRank vector πG=π\pi G = \pi, and compare it with the stationary distribution of the link chain PP alone.

Worked solutions

Problem 1

Each day is sunny (S) or rainy (R). After a sunny day the next is sunny with probability 0.90.9; after a rainy day it is sunny with probability 0.50.5. Write the transition matrix, and find the probability that it rains two days after a sunny day.

  1. P=(0.90.10.50.5)P = \begin{pmatrix} 0.9 & 0.1 \\ 0.5 & 0.5\end{pmatrix}, states in the order S, RRow ii is the distribution of tomorrow given today is ii; each row sums to 11.
  2. Pr⁡(X2=R∣X0=S)=PSSPSR+PSRPRR\Pr(X_2 = R \mid X_0 = S) = P_{SS}P_{SR} + P_{SR}P_{RR}Condition on tomorrow's weather: sunny then rainy, or rainy then rainy.
  3. =0.9⋅0.1+0.1⋅0.5=0.09+0.05=0.14= 0.9\cdot0.1 + 0.1\cdot0.5 = 0.09 + 0.05 = 0.14The two paths are disjoint, so their probabilities add.
  4. P2=(0.860.140.700.30)P^2 = \begin{pmatrix} 0.86 & 0.14 \\ 0.70 & 0.30\end{pmatrix}Step 2 is the (S,R)(S, R) entry of P2P^2: row SS of PP times column RR. Its rows still sum to 11.
  5. P=(0.90.10.50.5)P = \begin{pmatrix} 0.9 & 0.1 \\ 0.5 & 0.5\end{pmatrix}; the probability of rain two days after a sunny day is 0.140.14After a rainy day it is 0.300.30: the chain still remembers the start after two steps, but less than after one (0.10.1 against 0.50.5).

Problem 2

A walker on three states moves with

P=(0.50.500.250.50.2500.50.5)P = \begin{pmatrix} 0.5 & 0.5 & 0 \\ 0.25 & 0.5 & 0.25 \\ 0 & 0.5 & 0.5 \end{pmatrix}

and starts in state 11. Find its distribution after one, two and three steps.

  1. π0=(1,0,0)\pi_0 = (1, 0, 0)Certain to be in state 11; a row vector.
  2. π1=π0P=(0.5,0.5,0)\pi_1 = \pi_0P = (0.5, 0.5, 0)Multiplying a one-hot row vector by PP picks out a row of PP.
  3. π2=0.5 (0.5,0.5,0)+0.5 (0.25,0.5,0.25)=(0.375,0.5,0.125)\pi_2 = 0.5\,(0.5, 0.5, 0) + 0.5\,(0.25, 0.5, 0.25) = (0.375, 0.5, 0.125)π1P\pi_1P is the mixture of the rows of PP, weighted by π1\pi_1.
  4. π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)\pi_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 11.
  5. π1=(12,12,0)\pi_1 = (\tfrac12, \tfrac12, 0), π2=(38,12,18)\pi_2 = (\tfrac38, \tfrac12, \tfrac18), π3=(516,12,316)\pi_3 = (\tfrac{5}{16}, \tfrac12, \tfrac{3}{16})Writing PP times the column (1,0,0)⊤(1, 0, 0)^\top instead gives the first column of PP, 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. π1=0.5π1+0.25π2\pi_1 = 0.5\pi_1 + 0.25\pi_2, π2=0.5π1+0.5π2+0.5π3\pi_2 = 0.5\pi_1 + 0.5\pi_2 + 0.5\pi_3, π3=0.25π2+0.5π3\pi_3 = 0.25\pi_2 + 0.5\pi_3πP=π\pi P = \pi entry by entry: column jj of PP gives the equation for πj\pi_j. Here πi\pi_i are the entries of π\pi.
  2. π2=2π1\pi_2 = 2\pi_1 and π2=2π3\pi_2 = 2\pi_3From the first and third equations; the middle one then holds automatically, since one equation of πP=π\pi P = \pi is always redundant.
  3. π1+2π1+π1=1\pi_1 + 2\pi_1 + \pi_1 = 1, so π1=14\pi_1 = \tfrac14The normalization replaces the redundant equation.
  4. π0P−π=(14,0,−14)\pi_0P - \pi = (\tfrac14, 0, -\tfrac14), π0P2−π=(18,0,−18)\pi_0P^2 - \pi = (\tfrac18, 0, -\tfrac18), π0P3−π=(116,0,−116)\pi_0P^3 - \pi = (\tfrac{1}{16}, 0, -\tfrac{1}{16})The distributions of Problem 2 minus the stationary one. The gap halves at every step.
  5. π=(14,12,14)\pi = (\tfrac14, \tfrac12, \tfrac14); the distance from it halves each stepThe eigenvalues of PP are 11, 12\tfrac12 and 00, and the halving is λ2=12\lambda_2 = \tfrac12. The eigenvector that np.linalg.eig(P) returns for λ=1\lambda = 1 is (1,1,1)(1, 1, 1), which is not π\pi (Mistake 2).

Problem 4

For the two-state chain P=(1−aab1−b)P = \begin{pmatrix} 1 - a & a \\ b & 1 - b\end{pmatrix} with 0<a+b<20 < a + b < 2, find the stationary distribution, the eigenvalues of PP and a formula for (Pn)12(P^n)_{12}. Check it against Problem 1.

  1. π1a=π2b\pi_1a = \pi_2b, so π=(ba+b,aa+b)\pi = \Big(\dfrac{b}{a + b}, \dfrac{a}{a + b}\Big)In a two-state chain the flow from 11 to 22 must equal the flow back.
  2. λ1=1\lambda_1 = 1 and λ2=tr⁡P−1=1−a−b\lambda_2 = \operatorname{tr}P - 1 = 1 - a - bEvery stochastic matrix has P1=1P\mathbf 1 = \mathbf 1, so 11 is an eigenvalue, and the two eigenvalues add to the trace 2−a−b2 - a - b.
  3. Pn=1a+b(baba)+λ2na+b(a−a−bb)P^n = \dfrac{1}{a + b}\begin{pmatrix} b & a \\ b & a\end{pmatrix} + \dfrac{\lambda_2^n}{a + b}\begin{pmatrix} a & -a \\ -b & b\end{pmatrix}The first matrix has both rows equal to π\pi; the second is the part that decays. At n=0n = 0 the sum is II, and multiplying by PP multiplies the second part by λ2\lambda_2.
  4. (Pn)12=aa+b(1−λ2n)(P^n)_{12} = \dfrac{a}{a + b}\big(1 - \lambda_2^n\big)The (1,2)(1, 2) entry of step 3.
  5. π=(ba+b,aa+b)\pi = \big(\tfrac{b}{a+b}, \tfrac{a}{a+b}\big), eigenvalues 11 and 1−a−b1 - a - b, (Pn)12=aa+b(1−(1−a−b)n)(P^n)_{12} = \tfrac{a}{a+b}(1 - (1 - a - b)^n)Problem 1 has a=0.1a = 0.1, b=0.5b = 0.5: π=(56,16)\pi = (\tfrac56, \tfrac16), λ2=0.4\lambda_2 = 0.4, and (P2)SR=16(1−0.16)=0.14(P^2)_{SR} = \tfrac16(1 - 0.16) = 0.14. At a=b=1a = b = 1 the chain alternates, λ2=−1\lambda_2 = -1, and PnP^n 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.0010.001 of its long-run value 16\tfrac16, starting from a sunny day? Starting from a rainy day?

  1. From sunny: (Pn)SR−16=−16(0.4)n(P^n)_{SR} - \tfrac16 = -\tfrac16(0.4)^nProblem 4, step 4, with λ2=0.4\lambda_2 = 0.4.
  2. 16(0.4)n<0.001\tfrac16(0.4)^n < 0.001 needs 0.4n<0.0060.4^n < 0.006, so n>ln⁡0.006ln⁡0.4≈5.58n > \dfrac{\ln 0.006}{\ln 0.4} \approx 5.58Dividing by ln⁡0.4<0\ln 0.4 < 0 flips the inequality.
  3. From rainy: (Pn)RR−16=56(0.4)n(P^n)_{RR} - \tfrac16 = \tfrac56(0.4)^nThe (2,2)(2, 2) entry of Problem 4, step 3: aa+b+ba+bλ2n\tfrac{a}{a+b} + \tfrac{b}{a+b}\lambda_2^n.
  4. 56(0.4)n<0.001\tfrac56(0.4)^n < 0.001 needs 0.4n<0.00120.4^n < 0.0012, so n>ln⁡0.0012ln⁡0.4≈7.34n > \dfrac{\ln 0.0012}{\ln 0.4} \approx 7.34A larger starting gap, the same rate.
  5. 66 days from a sunny start, 88 days from a rainy startCheck: 16(0.4)6≈0.00068\tfrac16(0.4)^6 \approx 0.00068 and 56(0.4)8≈0.00055\tfrac56(0.4)^8 \approx 0.00055, while 16(0.4)5≈0.0017\tfrac16(0.4)^5 \approx 0.0017 and 56(0.4)7≈0.0014\tfrac56(0.4)^7 \approx 0.0014 are still too big. Each day multiplies the gap by ∣λ2∣=0.4|\lambda_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.

  1. Return time to R =1/πR=6= 1/\pi_R = 6 daysThe chain spends a fraction 16\tfrac16 of its days in R, so visits are 66 days apart on average.
  2. From S, each day is rainy with probability 0.10.1, independently, until the first one: hS→R=1/0.1=10h_{S \to R} = 1/0.1 = 10The number of steps until the first success is geometric with mean 1/p1/p.
  3. Return time to R =1+0.5⋅0+0.5⋅hS→R= 1 + 0.5\cdot0 + 0.5\cdot h_{S \to R}One step, then either it rains again at once (probability 0.50.5) or the chain is in S and must still reach R.
  4. =1+0.5⋅10=6= 1 + 0.5\cdot10 = 6Agrees with step 1.
  5. 66 days between rainy days; 1010 days from a sunny day to the first rain1/πR1/\pi_R is a return time, not the time to get there from somewhere else: using 66 for the trip from S is Mistake 4. Likewise the return time to S is 1/πS=65=1+0.1⋅21/\pi_S = \tfrac65 = 1 + 0.1\cdot 2.

Problem 7

A gambler has 11 chip and bets one chip at a time on fair coin flips, stopping at 00 or 44 chips. With transient states 1,2,31, 2, 3 (the number of chips), compute the fundamental matrix, the expected number of bets, and the probability of reaching 44 chips, from each starting amount.

  1. Q=(0120120120120)Q = \begin{pmatrix} 0 & \tfrac12 & 0 \\ \tfrac12 & 0 & \tfrac12 \\ 0 & \tfrac12 & 0\end{pmatrix}, R=(12000012)R = \begin{pmatrix} \tfrac12 & 0 \\ 0 & 0 \\ 0 & \tfrac12\end{pmatrix}Rows of QQ are moves among 1,2,31, 2, 3 chips; the columns of RR are absorption at 00 and at 44.
  2. N=(I−Q)−1=(1.510.51210.511.5)N = (I - Q)^{-1} = \begin{pmatrix} 1.5 & 1 & 0.5 \\ 1 & 2 & 1 \\ 0.5 & 1 & 1.5\end{pmatrix}Check: (I−Q)N=I(I - Q)N = I; for example row 11 of I−QI - Q is (1,−12,0)(1, -\tfrac12, 0), and with the first column of NN it gives 1.5−0.5=11.5 - 0.5 = 1.
  3. N1=(3,4,3)N\mathbf 1 = (3, 4, 3)Expected number of bets. It matches the formula k(4−k)k(4 - k) for a fair game.
  4. B=NR=(0.750.250.50.50.250.75)B = NR = \begin{pmatrix} 0.75 & 0.25 \\ 0.5 & 0.5 \\ 0.25 & 0.75\end{pmatrix}Row kk is (ruin, reach 44) from kk chips; each row sums to 11.
  5. From 11 chip: 33 bets on average and probability 14\tfrac14 of reaching 44; from 22: 44 bets and 12\tfrac12; from 33: 33 bets and 34\tfrac34A fair game keeps the expected fortune constant: 14⋅4=1\tfrac14\cdot4 = 1. Three straight wins, 18\tfrac18, 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.50.5, go to Cart with 0.30.3 and leave with 0.20.2. From Cart they go back to Browse with 0.20.2, buy with 0.50.5 and leave with 0.30.3. For a user who starts browsing, find the probability of a purchase and the expected number of minutes before buying or leaving.

  1. Q=(0.50.30.20)Q = \begin{pmatrix} 0.5 & 0.3 \\ 0.2 & 0\end{pmatrix}, R=(00.20.50.3)R = \begin{pmatrix} 0 & 0.2 \\ 0.5 & 0.3\end{pmatrix}Transient states Browse, Cart; absorbing states Buy, Leave. Each row of (Q∣R)(Q \mid R) sums to 11.
  2. I−Q=(0.5−0.3−0.21)I - Q = \begin{pmatrix} 0.5 & -0.3 \\ -0.2 & 1\end{pmatrix}, det⁡=0.5−0.06=0.44\det = 0.5 - 0.06 = 0.44A 2×22\times2 inverse needs the determinant.
  3. N=10.44(10.30.20.5)≈(2.2730.6820.4551.136)N = \dfrac{1}{0.44}\begin{pmatrix} 1 & 0.3 \\ 0.2 & 0.5\end{pmatrix} \approx \begin{pmatrix} 2.273 & 0.682 \\ 0.455 & 1.136\end{pmatrix}Swap the diagonal, negate the off-diagonal, divide by the determinant.
  4. Pr⁡(Buy)=N11R11+N12R21=0+0.30.44⋅0.5=1544\Pr(\text{Buy}) = N_{11}R_{11} + N_{12}R_{21} = 0 + \dfrac{0.3}{0.44}\cdot0.5 = \dfrac{15}{44}First column of B=NRB = NR. Only Cart leads to Buy, so the answer is the expected number of Cart visits times 0.50.5.
  5. Pr⁡(Buy)=1544≈0.341\Pr(\text{Buy}) = \tfrac{15}{44} \approx 0.341; expected time 1+0.30.44=6522≈2.95\tfrac{1 + 0.3}{0.44} = \tfrac{65}{22} \approx 2.95 minutesThe leave probability is 2944≈0.659\tfrac{29}{44} \approx 0.659, and the two add to 11. A user who has reached the cart buys with probability 2544≈0.568\tfrac{25}{44} \approx 0.568, which is the number to optimize.

Problem 9

A random walker on the graph with vertices A,B,C,DA, B, C, D and edges ABAB, BCBC, CDCD, DADA, ACAC moves to a uniformly chosen neighbor at each step. Show that πv=deg⁡(v)/(2∣E∣)\pi_v = \deg(v)/(2|E|) satisfies detailed balance, and find the expected return time to AA. Then show that the chain on three states with P12=P23=P31=23P_{12} = P_{23} = P_{31} = \tfrac23 and P21=P32=P13=13P_{21} = P_{32} = P_{13} = \tfrac13 has a uniform stationary distribution but is not reversible.

  1. Degrees A: 3A{:}\ 3, B: 2B{:}\ 2, C: 3C{:}\ 3, D: 2D{:}\ 2, and 2∣E∣=102|E| = 10, so the candidate is π=(0.3,0.2,0.3,0.2)\pi = (0.3, 0.2, 0.3, 0.2)Each edge adds 11 to two degrees.
  2. For an edge uvuv: πuPuv=deg⁡u10⋅1deg⁡u=110=πvPvu\pi_uP_{uv} = \dfrac{\deg u}{10}\cdot\dfrac{1}{\deg u} = \dfrac{1}{10} = \pi_vP_{vu}The degree cancels; for non-neighbors both sides are 00.
  3. Detailed balance implies πP=π\pi P = \piSum πiPij=πjPji\pi_iP_{ij} = \pi_jP_{ji} over ii: the right side gives πj∑iPji=πj\pi_j\sum_iP_{ji} = \pi_j.
  4. Return time to AA =1/0.3=103= 1/0.3 = \tfrac{10}{3}The busier vertices are visited more often.
  5. Detailed balance holds with π=(0.3,0.2,0.3,0.2)\pi = (0.3, 0.2, 0.3, 0.2) and the return time to AA is 103\tfrac{10}{3}; the rotating chain has π=(13,13,13)\pi = (\tfrac13, \tfrac13, \tfrac13) but π1P12=29≠19=π2P21\pi_1P_{12} = \tfrac29 \ne \tfrac19 = \pi_2P_{21}Its columns also sum to 11, so the uniform π\pi is stationary, yet probability circulates 1→2→3→11 \to 2 \to 3 \to 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→21 \to 2, 1→31 \to 3, 2→32 \to 3 and 3→13 \to 1. A surfer follows a uniformly chosen link with probability d=0.85d = 0.85 and otherwise jumps to a uniformly chosen page, so

G=dP+1−d311⊤.G = dP + \frac{1 - d}{3}\mathbf{1}\mathbf{1}^\top .

Find the PageRank vector πG=π\pi G = \pi, and compare it with the stationary distribution of the link chain PP alone.

  1. P=(00.50.5001100)P = \begin{pmatrix} 0 & 0.5 & 0.5 \\ 0 & 0 & 1 \\ 1 & 0 & 0\end{pmatrix}Page 11 splits its probability over two links.
  2. πj=0.05+0.85∑iπiPij\pi_j = 0.05 + 0.85\sum_i\pi_iP_{ij}π 1−d311⊤=0.05 1⊤\pi\,\tfrac{1-d}{3}\mathbf 1\mathbf 1^\top = 0.05\,\mathbf 1^\top because ∑iπi=1\sum_i\pi_i = 1.
  3. π1=0.05+0.85π3\pi_1 = 0.05 + 0.85\pi_3, π2=0.05+0.425π1\pi_2 = 0.05 + 0.425\pi_1, π3=0.05+0.85(0.5π1+π2)\pi_3 = 0.05 + 0.85(0.5\pi_1 + \pi_2)Column by column.
  4. Substitute π2\pi_2 and π3=(π1−0.05)/0.85\pi_3 = (\pi_1 - 0.05)/0.85 into π1+π2+π3=1\pi_1 + \pi_2 + \pi_3 = 1: 2.21125 π1=0.85752.21125\,\pi_1 = 0.8575Multiply through by 0.850.85 first.
  5. π≈(0.388,0.215,0.397)\pi \approx (0.388, 0.215, 0.397), exactly 11769(686,380,703)\tfrac{1}{1769}(686, 380, 703); without damping π=(0.4,0.2,0.4)\pi = (0.4, 0.2, 0.4)Page 33 ranks first because it collects links from both other pages. The jumps pull every score toward 13\tfrac13, and they make the chain converge at the rate 0.85⋅∣λ2(P)∣=0.85⋅0.707≈0.6010.85\cdot|\lambda_2(P)| = 0.85\cdot0.707 \approx 0.601 per step, since PP's other eigenvalues are −12±12i-\tfrac12 \pm \tfrac12 i.

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.

  1. π0=(1,0)\pi_0 = (1, 0), sunny today, and P=(0.90.10.50.5)P = \begin{pmatrix} 0.9 & 0.1 \\ 0.5 & 0.5\end{pmatrix}Right so far: Problem 1.
  2. “Tomorrow's distribution is Pπ0⊤P\pi_0^\top.”With rows indexed by today, the distribution must multiply from the left: π1=π0P\pi_1 = \pi_0P.
  3. Pπ0⊤=(0.9,0.5)⊤P\pi_0^\top = (0.9, 0.5)^\topIts entries add to 1.41.4, so it is not a distribution: it is the first column of PP, the probability of sun tomorrow from each state today. The correct π1=(0.9,0.1)\pi_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\lambda = 1 looks like the answer.

  1. PP from Problem 2 has eigenvalue 11Right so far: every stochastic matrix does.
  2. “Take the eigenvector of PP for λ=1\lambda = 1 and normalize it.”That solves Pv=vPv = v; the stationary distribution solves πP=π\pi P = \pi, an eigenvector of P⊤P^\top.
  3. π=(13,13,13)\pi = (\tfrac13, \tfrac13, \tfrac13)P1=1P\mathbf 1 = \mathbf 1 for every stochastic matrix, so this is always the uniform vector. Check: (13,13,13)P=(0.25,0.5,0.25)≠(13,13,13)(\tfrac13, \tfrac13, \tfrac13)P = (0.25, 0.5, 0.25) \ne (\tfrac13, \tfrac13, \tfrac13). Use np.linalg.eig(P.T), which gives π=(14,12,14)\pi = (\tfrac14, \tfrac12, \tfrac14).

3. Expecting every chain to converge to π

A stationary distribution exists, so the chain is assumed to approach it.

  1. P=(0110)P = \begin{pmatrix} 0 & 1 \\ 1 & 0\end{pmatrix} has π=(12,12)\pi = (\tfrac12, \tfrac12)Right so far: πP=π\pi P = \pi, and it is Problem 4 with a=b=1a = b = 1.
  2. “So π0Pn→(12,12)\pi_0P^n \to (\tfrac12, \tfrac12) from π0=(1,0)\pi_0 = (1, 0).”Convergence needs the chain to be aperiodic as well; this one returns to each state only at even times.
  3. π100≈(12,12)\pi_{100} \approx (\tfrac12, \tfrac12)πn\pi_n alternates between (0,1)(0, 1) and (1,0)(1, 0), so π100=(1,0)\pi_{100} = (1, 0). The eigenvalues are 11 and −1-1, and ∣λ2∣=1|\lambda_2| = 1 means the gap never shrinks. Only the long-run average of the πn\pi_n is (12,12)(\tfrac12, \tfrac12). 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/πj1/\pi_j is the one expected-time formula that needs no linear algebra.

  1. πR=16\pi_R = \tfrac16 in the weather chainRight so far: Problem 4.
  2. “Starting from a sunny day, rain comes after 1/πR1/\pi_R days on average.”1/πR1/\pi_R is the mean time from R back to R. From S the chain must first leave S, which it does slowly.
  3. hS→R=6h_{S \to R} = 6 daysFrom S the wait is geometric with success probability 0.10.1, so hS→R=10h_{S \to R} = 10. The return time 66 averages that with the immediate repeat: 1+0.5⋅10=61 + 0.5\cdot10 = 6 (Problem 6).

5. Multiplying the win probabilities in gambler's ruin

Reaching 44 chips from 11 takes three more wins than losses, and three wins in a row is the obvious way.

  1. Each bet is won with probability 12\tfrac12, independentlyRight so far: Problem 7.
  2. “Reaching 44 chips from 11 needs three wins, so the probability is (12)3(\tfrac12)^3.”That counts only the path win–win–win. A loss followed by two wins and so on can still reach 44, as long as the gambler never hits 00 first.
  3. Pr⁡(reach 4∣start at 1)=18\Pr(\text{reach } 4 \mid \text{start at } 1) = \tfrac18The fundamental matrix gives 14\tfrac14: the first row of NN is (1.5,1,0.5)(1.5, 1, 0.5), and Pr⁡=N13⋅12=0.25\Pr = N_{13}\cdot\tfrac12 = 0.25. Fairness confirms it: the expected final fortune 4⋅14=14\cdot\tfrac14 = 1 equals the starting 11 chip, while 4⋅18=0.54\cdot\tfrac18 = 0.5 would lose chips on average in a fair game.

Print this set: markov-chains-and-stationary-distributions.pdf (problems, answers, and worked solutions on separate pages).

Read the guide: Markov chains: multiply row vectors by P, solve πP = π, and get absorption from (I − Q)⁻¹