Course contents 20 / 22

20 / 22 · Random Processes and Sampling

Markov chains

Random processes with a one-step memory: the Markov property and the transition matrix, how the distribution evolves and settles at a stationary distribution, convergence and the ergodic theorem, n-gram language models and PageRank as Markov chains, and hitting probabilities in the gambler's ruin.

On this page 11

Our sampling-based guarantees so far mostly assumed independent draws. Many random processes are not like that, and their dependence unfolds in time: tomorrow’s weather depends on today’s, the next word on the previous ones, the next page a reader visits on the current page, the next state of a sampler on the current state. A Markov chain is the simplest model of such dependence: the future depends on the past only through the present. It is simple enough to analyse completely and rich enough to describe n-gram language models, web search and the samplers of the last two notes of the course.

Notation. X0,X1,X2,…X_0, X_1, X_2, \dots take values in a finite set of states {1,…,K}\{1, \dots, K\}. PP is the K×KK \times K transition matrix, Pij=P(Xt+1=j∣Xt=i)P_{ij} = P(X_{t+1} = j \mid X_t = i); each row sums to 1. A distribution over states is a row vector π\pi.

The Markov property#

A sequence is a Markov chain if

P(Xt+1=j∣Xt=i,Xt−1,…,X0)=P(Xt+1=j∣Xt=i)=Pij.P(X_{t+1} = j \mid X_t = i, X_{t-1}, \dots, X_0) = P(X_{t+1} = j \mid X_t = i) = P_{ij} .

The first equality is the Markov property: the present state contains everything about the past that matters for the future. The second is a separate assumption, time homogeneity: the transition rule PijP_{ij} is the same at every time tt. All chains in this note are finite and time-homogeneous. The reverse process that generates samples in a diffusion model is Markov but not homogeneous, since its transition rule changes from step to step (MCMC note). A three-state weather model, with sunny days followed by sunny days 70% of the time, is a Markov chain with

P=(0.70.20.10.30.40.30.20.30.5)P = \begin{pmatrix} 0.7 & 0.2 & 0.1 \\ 0.3 & 0.4 & 0.3 \\ 0.2 & 0.3 & 0.5 \end{pmatrix}

(rows and columns: sunny, cloudy, rainy). By the law of total probability, the distribution of the next state is the current distribution times the matrix:

πt+1=πtP,πt=π0Pt.\pi_{t+1} = \pi_t P, \qquad \pi_t = \pi_0 P^t .

The entry (Pt)ij(P^t)_{ij} is the probability of being in jj after tt steps from ii. Starting from a sunny day:

Figure

A Markov chain for the weather

The transition graph and the distributions by dayThree circles with arrows both ways; a table of the distributions for days 0, 1, 2 and 10.0.20.30.10.20.30.3sunny0.7cloudy0.4rainy0.5sunnycloudyrainyday 01.000.000.001day 10.700.200.102day 20.570.250.183day 100.4570.2830.2614The transition graph and the distributions by dayThree circles with arrows both ways; a table of the distributions for days 0, 1, 2 and 10.0.20.30.10.20.30.3sunny0.7cloudy0.4rainy0.5sunnycloudyrainyday 01.000.000.001day 10.700.200.102day 20.570.250.183day 100.4570.2830.2614
Three weather states; an arrow from state i to j carries the transition probability Pᵢⱼ, and the probability of staying is inside the circle. If day 0 is sunny (1), the distribution of the next day is the "sunny" row of the matrix (2), that of day 2 is π₁P (3); by day 10 it barely depends on the start and equals the stationary distribution 21/46, 13/46, 12/46 (4).

Stationary distributions#

A distribution π\pi with

πP=π\pi P = \pi

is stationary: if the chain starts in it, it stays in it. It is a left eigenvector of PP with eigenvalue 1, normalized to sum to 1, and it can be found by solving the linear system πP=π\pi P = \pi, ∑iπi=1\sum_i \pi_i = 1. For the weather chain π=(21/46,13/46,12/46)=(0.457,0.283,0.261)\pi = (21/46, 13/46, 12/46) = (0.457, 0.283, 0.261): in the long run 46% of days are sunny.

The figure suggests more: from a sunny start the distribution approaches π\pi within ten days. That holds for every starting distribution under two conditions:

  • Irreducible: every state can be reached from every other.
  • Aperiodic: the chain does not cycle with a fixed period. A chain that alternates deterministically between two states has the stationary distribution (1/2,1/2)(1/2, 1/2) but never approaches it from a fixed start.

For a finite irreducible aperiodic chain, πt→π\pi_t \to \pi from any start, and the ergodic theorem, the law of large numbers for Markov chains, says that the share of time the chain spends in each state converges to π\pi along a single long run. The speed is set by the second largest absolute eigenvalue of PP: the distance to π\pi shrinks roughly like ∣λ2∣t\lvert \lambda_2 \rvert^t, here 0.473t0.473^t, so ten steps reduce it by a factor of about 1,800. The number of steps needed to get close is the mixing time, the central quantity for the samplers of the MCMC note.

Language models as Markov chains#

A bigram language model is a Markov chain whose states are words: PijP_{ij} is the probability that word jj follows word ii, estimated from counts. Generating text from it is running the chain. For a bigram chain fitted to one long text, the stationary distribution is approximately the unigram word frequency: each word is followed by a word, and preceded by one, as often as it occurs, except at the two ends of the text, so the unigram frequencies satisfy the stationarity equation up to these boundary terms. Text made of separate sentences needs an end-of-sentence token that leads back to a start state, or the chain stops. An n-gram model with n>2n > 2 is a Markov chain whose states are contexts of n−1n - 1 words; its stationary distribution is over contexts, and the unigram frequency is its marginal for a single position of the context. Shannon’s 1948 paper generated “English” this way, from letter and word chains of increasing order, and the n-gram language models note builds such models from data.

A transformer language model is a Markov chain too, but on a far larger state: the whole context window. The Markov property is a statement about what the state contains, and making the state richer is how sequence models acquire longer memory.

PageRank#

A reader who follows a random link from each page performs a random walk, a Markov chain on the pages. Pages that many walks visit often are important; their importance is the stationary distribution. Two fixes make the chain well defined, irreducible and aperiodic: with probability 0.150.15 the reader jumps to a random page instead of following a link (and always jumps from a page without links). The stationary distribution of that chain is PageRank, the ranking signal behind the early Google search engine:

Figure

PageRank is the stationary distribution of a random walk

The link graph of five pagesPage circles sized by PageRank; A and C are large, D and E small.A0.367B0.186C0.375D0.043E0.030The link graph of five pagesPage circles sized by PageRank; A and C are large, D and E small.A0.367B0.186C0.375D0.043E0.030
Five pages and the links between them. A random surfer follows a random link from the page with probability 0.85 and jumps to a random page with probability 0.15. The share of time a long walk spends on each page is its PageRank (circle area): A 0.367, B 0.186, C 0.375, D 0.043, E 0.030. Four pages link to C, yet A is nearly as important: C's only link goes to A and passes all of C's weight to it.

The example shows what distinguishes PageRank from counting links: a page is important if important pages link to it. C has the most incoming links, but A, with links from C and D, is almost as important, because C sends all its weight to A.

Hitting probabilities: the gambler’s ruin#

Some chains have absorbing states that are never left, and the question is which one is reached first. A gambler with 3 coins bets one coin at a time on a fair game and stops at 0 or at 10. Let hah_a be the probability of reaching 10 from aa; conditioning on the first bet gives ha=12ha+1+12ha−1h_a = \frac12 h_{a+1} + \frac12 h_{a-1} with h0=0h_0 = 0, h10=1h_{10} = 1, so ha=a/10h_a = a/10 and h3=0.3h_3 = 0.3. The expected number of bets is a(10−a)=21a(10 - a) = 21. A slightly unfavourable game, winning each bet with probability 0.490.49, lowers the chance to 0.2590.259; starting from 50 coins with a target of 100 it is only 0.1190.119, not one half: over a long game a small edge compounds. The same first-step equations give hitting times and absorption probabilities for any chain, from customer funnels to the expected length of a random walk.

What changed#

Independent samplesMarkov chains
Dependencenonethe next state depends on the current one: PijP_{ij}
Distribution over timeconstantπt=π0Pt\pi_t = \pi_0 P^t
Long runthe law of large numbersstationary π=πP\pi = \pi P; the ergodic theorem for time averages
Speed1/n1/\sqrt nalso the mixing time, set by ∣λ2∣\lvert \lambda_2 \rvert
Examplesn-gram models, PageRank, random walks, MCMC

Limitations#

  • The Markov property is a modelling choice. Real processes have longer memory; enlarging the state (the last nn words, the full context) restores the property at the cost of a much larger state space.
  • Convergence can be slow. Chains with bottlenecks, such as two clusters of states joined by rare transitions, can take a very long time to mix, and a single run may show no sign of it.
  • Finite states hide subtleties. Infinite or continuous state spaces need conditions (recurrence, positive recurrence) that the finite case gets for free.

What you should remember#

  • Markov property: the future depends on the past only through the present; Pij=P(Xt+1=j∣Xt=i)P_{ij} = P(X_{t+1} = j \mid X_t = i).
  • Distributions evolve by πt+1=πtP\pi_{t+1} = \pi_t P; (Pt)ij(P^t)_{ij} is the tt-step transition probability.
  • Stationary: πP=π\pi P = \pi. For finite irreducible aperiodic chains, πt→π\pi_t \to \pi from any start and time averages converge to π\pi.
  • The second largest eigenvalue in absolute value sets the speed: distance to π\pi ≈∣λ2∣t\approx \lvert \lambda_2 \rvert^t.
  • n-gram models and PageRank are Markov chains; PageRank is the stationary distribution of a random surfer with teleportation.
  • Absorption probabilities and hitting times solve first-step equations; in the fair gambler’s ruin with target NN, ha=a/Nh_a = a/N.

Code#

The weather chain, its stationary distribution and a long run, PageRank, and the gambler’s ruin:

import numpy as np

# the weather chain: distributions after 1, 2 and 10 days from a sunny start
P = np.array([[0.7, 0.2, 0.1], [0.3, 0.4, 0.3], [0.2, 0.3, 0.5]])
pi = np.array([1.0, 0.0, 0.0])
for t in range(11):
    if t in (1, 2, 10):
        print(t, pi.round(3))
    pi = pi @ P
# 1 [0.7 0.2 0.1] | 2 [0.57 0.25 0.18] | 10 [0.457 0.283 0.261]

# the stationary distribution solves pi P = pi with sum 1; the eigenvalues set the speed
A = np.vstack([P.T - np.eye(3), np.ones(3)])
stationary = np.linalg.lstsq(A, np.array([0, 0, 0, 1.0]), rcond=None)[0]
print(stationary.round(4), (np.array([21, 13, 12]) / 46).round(4))   # [0.4565 0.2826 0.2609] twice
print(np.sort(np.abs(np.linalg.eigvals(P))).round(3))                # [0.127 0.473 1.   ]

# the ergodic theorem: the share of days in each state along one long run
rng = np.random.default_rng(0)
x, counts = 0, np.zeros(3)
for _ in range(100_000):
    counts[x] += 1
    x = rng.choice(3, p=P[x])
print((counts / counts.sum()).round(3))                               # [0.459 0.281 0.259]

# PageRank of five pages by power iteration, damping 0.85
links = {0: [1, 2], 1: [2], 2: [0], 3: [0, 2], 4: [3, 2]}
M = np.zeros((5, 5))
for i, outs in links.items():
    M[i, outs] = 1 / len(outs)
G = 0.85 * M + 0.15 / 5
r = np.full(5, 0.2)
for _ in range(200):
    r = r @ G
print(r.round(3))                                                     # [0.367 0.186 0.375 0.043 0.03 ]

# gambler's ruin: the probability of reaching N before 0 from a, winning each bet with probability p
ruin = lambda p, a, N: a / N if p == 0.5 else (1 - ((1 - p) / p) ** a) / (1 - ((1 - p) / p) ** N)
print(ruin(0.5, 3, 10), round(ruin(0.49, 3, 10), 3), round(ruin(0.49, 50, 100), 3))   # 0.3 0.259 0.119

Next#

The ergodic theorem says that a long run of a chain samples its stationary distribution. Turned around, this is a way to sample from a distribution that cannot be sampled directly: build a chain whose stationary distribution is the target. Before that idea, the next note collects the direct methods of Monte Carlo: inverse transforms, rejection sampling and importance sampling, and the variance that decides whether they work.

References#