AI Knowledge Tree

Markov chains and stationary distributions

Define a Markov chain by its transition matrix and find its stationary distribution.

Many processes evolve in steps where the next state depends only on the current one, not the full history: a random walk, a shuffled card deck, a token sequence under a simple model. A Markov chain captures this, and its long-run behavior is read off from the eigenstructure of a single matrix, tying probability back to linear algebra.

The Markov property

A sequence of random variables X0,X1,X2,…X_0, X_1, X_2, \dots taking values in a finite state set is a Markov chain if the future depends on the past only through the present[1]:

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

This memorylessness is the defining assumption. The one-step dynamics are collected in the transition matrix PP with entries Pij=P(Xt+1=j∣Xt=i)P_{ij} = P(X_{t+1} = j \mid X_t = i). Each row sums to 1 (from state ii the chain must go somewhere), making PP a row-stochastic matrix.

Distributions evolve by matrix multiplication

Represent the distribution over states at time tt as a row vector π(t)\pi^{(t)}, with πi(t)=P(Xt=i)\pi^{(t)}_i = P(X_t = i). One step forward uses the law of total probability:

πj(t+1)=∑iP(Xt=i) Pij=∑iπi(t)Pij,\pi^{(t+1)}_j = \sum_i P(X_t = i)\,P_{ij} = \sum_i \pi^{(t)}_i P_{ij},

which is the vector-matrix product π(t+1)=π(t)P\pi^{(t+1)} = \pi^{(t)} P. After nn steps,

π(n)=π(0)Pn.\pi^{(n)} = \pi^{(0)} P^n.

The nn-step transition probabilities are the entries of PnP^n. Evolving the chain is repeatedly multiplying by PP.

Stationary distribution

A distribution π\pi is stationary if it is unchanged by a step[2]:

πP=π,∑iπi=1,πi≥0.\pi P = \pi, \qquad \sum_i \pi_i = 1, \quad \pi_i \ge 0.

The equation πP=π\pi P = \pi says π\pi is a left eigenvector of PP with eigenvalue 1. Every row-stochastic matrix has eigenvalue 1: the all-ones column vector 1\mathbf{1} satisfies P1=1P\mathbf{1} = \mathbf{1} (rows sum to 1), so 1 is an eigenvalue, and left and right eigenvalues coincide. All other eigenvalues have magnitude ≤1\le 1 (Gershgorin / Perron-Frobenius).

Convergence to stationarity

If the chain is irreducible (every state reachable from every other) and aperiodic (no forced cycles), the Perron-Frobenius theorem guarantees a unique stationary distribution π\pi, and the chain converges to it from any start:

π(0)Pn→n→∞π.\pi^{(0)} P^n \xrightarrow{n \to \infty} \pi.

The rate is governed by the second-largest eigenvalue magnitude ∣λ2∣|\lambda_2|: the distance to π\pi shrinks roughly like ∣λ2∣n|\lambda_2|^n. The spectral gap 1−∣λ2∣1 - |\lambda_2| is the mixing speed. This is the engine behind MCMC sampling and PageRank, both of which construct a chain whose stationary distribution is the target.

The gap is what separates a chain that forgets its start quickly from one that clings to it. Take two chains on three states, both with stationary distribution near uniform, differing only in how readily they leave the current state:

  • The weather chain in the code below has ∣λ2∣=0.45|\lambda_2| = 0.45, so a gap of 0.550.55. Starting certain it is sunny, the total-variation-scale L1L^1 distance to π\pi falls from 0.470.47 at one step to 0.100.10 at three steps, and it is within 0.010.01 of π\pi after 6 steps. The start is forgotten in under a week.
  • A sticky chain that stays put with probability 0.980.98 each step (off-diagonal 0.010.01) has ∣λ2∣=0.97|\lambda_2| = 0.97, a gap of only 0.030.03. From the same sunny start it needs 161 steps to reach the same L1L^1 distance of 0.010.01. Same stationary distribution, same three states; a 0.550.55-vs-0.030.03 gap is the whole difference between mixing in 6 steps and mixing in 161.
Starting certain it is sunny, the weather distribution relaxes to the stationary distribution (dotted) within a few steps; the rate is set by the second eigenvalue.
Starting certain it is sunny, the weather distribution relaxes to the stationary distribution (dotted) within a few steps; the rate is set by the second eigenvalue.

Check it on arrays

python · numpy
import numpy as np

# A 3-state weather chain (rows: from-state, cols: to-state)
P = np.array([
    [0.7, 0.2, 0.1],   # sunny ->
    [0.3, 0.4, 0.3],   # cloudy ->
    [0.2, 0.45, 0.35], # rainy ->
])
print("rows sum to 1:", np.allclose(P.sum(axis=1), 1.0))

# Evolve a distribution; watch it converge
pi = np.array([1.0, 0.0, 0.0])     # start certain it's sunny
for n in [1, 5, 20, 100]:
    print(f"n={n:>3}:", np.round(pi @ np.linalg.matrix_power(P, n), 5))

# Stationary distribution = left eigenvector of P with eigenvalue 1
vals, vecs = np.linalg.eig(P.T)        # left eigvecs of P = right eigvecs of P^T
idx = np.argmin(np.abs(vals - 1.0))
stat = np.real(vecs[:, idx])
stat = stat / stat.sum()
print("stationary (eigen):", np.round(stat, 5))
print("check pi P == pi:", np.allclose(stat @ P, stat))

# Second-largest |eigenvalue| controls mixing speed
mags = np.sort(np.abs(vals))[::-1]
print("|lambda_2| =", round(mags[1], 4), " spectral gap", round(1 - mags[1], 4))

The evolving distribution settles onto the stationary vector, which is exactly the left eigenvector of PP for eigenvalue 1, and the second eigenvalue magnitude reports how fast it got there.

Exercises

Exercise 1. A two-state chain has transition matrix P=(0.90.10.50.5)P = \begin{pmatrix} 0.9 & 0.1 \\ 0.5 & 0.5 \end{pmatrix}. Find its stationary distribution by solving πP=π\pi P = \pi with π0+π1=1\pi_0 + \pi_1 = 1.

Solution

Step 1. Write out πP=π\pi P = \pi component-wise with π=(π0,π1)\pi = (\pi_0, \pi_1):

0.9 π0+0.5 π1=π0,0.1 π0+0.5 π1=π1.0.9\,\pi_0 + 0.5\,\pi_1 = \pi_0, \qquad 0.1\,\pi_0 + 0.5\,\pi_1 = \pi_1.

Step 2. The first equation gives 0.5 π1=0.1 π00.5\,\pi_1 = 0.1\,\pi_0, so π1=0.2 π0\pi_1 = 0.2\,\pi_0. (The second equation is the same relation, since rows of πP=π\pi P = \pi are dependent.)

Step 3. Normalize with π0+π1=1\pi_0 + \pi_1 = 1: π0+0.2 π0=1\pi_0 + 0.2\,\pi_0 = 1, so π0=1/1.2=5/6\pi_0 = 1/1.2 = 5/6 and π1=1/6\pi_1 = 1/6. The chain spends 5/65/6 of its time in state 0, the stickier state.

Exercise 2. Prove that every row-stochastic matrix PP has eigenvalue 1.

Solution

Step 1. Row-stochastic means each row of PP sums to 1: ∑jPij=1\sum_j P_{ij} = 1 for every ii.

Step 2. Let 1\mathbf{1} be the all-ones column vector. The ii-th entry of P1P\mathbf{1} is ∑jPij⋅1=∑jPij=1\sum_j P_{ij} \cdot 1 = \sum_j P_{ij} = 1, so P1=1P\mathbf{1} = \mathbf{1}.

Step 3. That is the eigenvalue equation P1=1⋅1P\mathbf{1} = 1 \cdot \mathbf{1}, so 11 is an eigenvalue of PP with right eigenvector 1\mathbf{1}. Since a matrix and its transpose share eigenvalues, PP also has a left eigenvector for eigenvalue 1, which is the stationary distribution.

Exercise 3 (code). For the same 2×22 \times 2 chain, confirm the stationary distribution from Exercise 1 by power iteration (evolve an arbitrary start many steps) and by the left-eigenvector route, and check the two agree.

Solution
python · numpy
import numpy as np

P = np.array([[0.9, 0.1],
              [0.5, 0.5]])

# Power iteration from an arbitrary start
pi = np.array([0.3, 0.7])
for _ in range(200):
    pi = pi @ P
power_stat = pi

# Left eigenvector of P for eigenvalue 1 (= right eigvec of P^T)
vals, vecs = np.linalg.eig(P.T)
idx = np.argmin(np.abs(vals - 1.0))
eig_stat = np.real(vecs[:, idx])
eig_stat = eig_stat / eig_stat.sum()

expected = np.array([5 / 6, 1 / 6])
assert np.allclose(power_stat, expected, atol=1e-6)
assert np.allclose(eig_stat, expected, atol=1e-9)
assert np.allclose(power_stat @ P, power_stat)     # pi P == pi
print("power:", np.round(power_stat, 5), " eigen:", np.round(eig_stat, 5))

Both routes land on (5/6,1/6)(5/6, 1/6) and the result is fixed by PP, confirming the hand computation.

The final A2 lesson connects this curvature-of-the- log-likelihood idea to geometry: Fisher information.


References

  1. [1]Durrett, R. (2019). Probability: Theory and Examples (5th ed.). Cambridge University Press. Ch. 5, Markov chains and the Markov property.↩
  2. [2]Billingsley, P. (1995). Probability and Measure (3rd ed.). Wiley. Stationary distributions and the long-run behavior of Markov chains.↩