Ergodic Chains and Stationary Distributions
A chain in which every state can be reached from every other never stops moving. Instead of being absorbed, it settles into a statistical equilibrium: a stationary distribution that describes the long-run fraction of time spent in each state, whatever the starting point.
Stationary distributions
A probability row vector is stationary for if
If the chain starts with distribution , it has distribution at every later time. Algebraically, is a left eigenvector of with eigenvalue 1, normalised to sum to 1. To compute it, solve together with .
The fundamental limit theorem
For a regular chain (some power of has all entries positive):
- there is a unique stationary distribution , with all entries positive;
- , the matrix whose rows all equal ;
- for every starting distribution , .
For an ergodic but periodic chain, (1) still holds and the long-run fraction of time in each state still converges to (the ergodic theorem), even though itself oscillates.
import numpy as np
from sympy import Matrix, Rational, symbols, solve
# exact stationary distribution of the weather chain
P = Matrix([[Rational(6, 10), Rational(3, 10), Rational(1, 10)],
[Rational(3, 10), Rational(4, 10), Rational(3, 10)],
[Rational(2, 10), Rational(3, 10), Rational(5, 10)]])
w = Matrix([symbols("w1 w2 w3")])
sol = solve(list(w * P - w) + [sum(w) - 1], list(w))
print("stationary:", sol)
Pn = np.linalg.matrix_power(np.array(P.tolist(), dtype=float), 30)
print("P^30 rows:\n", np.round(Pn, 4))
vals, vecs = np.linalg.eig(np.array(P.T.tolist(), dtype=float))
v = np.real(vecs[:, np.argmin(np.abs(vals - 1))])
print("eigenvector method:", np.round(v / v.sum(), 4))stationary: {w1: 7/18, w2: 1/3, w3: 5/18}
P^30 rows:
[[0.3889 0.3333 0.2778]
[0.3889 0.3333 0.2778]
[0.3889 0.3333 0.2778]]
eigenvector method: [0.3889 0.3333 0.2778]
All three methods agree: in the long run it is sunny of the days, cloudy and rainy .
How fast is the convergence?
The distance from to shrinks roughly like , where is the second-largest eigenvalue in absolute value. The spectral gap measures how fast the chain forgets its start. Card shuffling's "seven riffles" and PageRank's convergence speed are both spectral-gap statements (Card Shuffling).
import numpy as np
P = np.array([[0.6, 0.3, 0.1],
[0.3, 0.4, 0.3],
[0.2, 0.3, 0.5]])
w = np.array([14, 12, 10]) / 36
lams = sorted(np.abs(np.linalg.eigvals(P)), reverse=True)
print("eigenvalue moduli:", np.round(lams, 4).tolist())
u = np.array([0.0, 0.0, 1.0])
for n in [1, 2, 4, 8, 16]:
dist = np.abs(u @ np.linalg.matrix_power(P, n) - w).sum() / 2
print(f"n={n:2d} distance to stationary {dist:.2e} |lambda_2|^n = {lams[1] ** n:.2e}")eigenvalue moduli: [1.0, 0.4, 0.1]
n= 1 distance to stationary 2.22e-01 |lambda_2|^n = 4.00e-01
n= 2 distance to stationary 8.22e-02 |lambda_2|^n = 1.60e-01
n= 4 distance to stationary 1.28e-02 |lambda_2|^n = 2.56e-02
n= 8 distance to stationary 3.28e-04 |lambda_2|^n = 6.55e-04
n=16 distance to stationary 2.15e-07 |lambda_2|^n = 4.29e-07
Reversibility and detailed balance
If for all (the flow from to equals the flow back), then is stationary. Such chains are reversible. Random walks on undirected graphs are reversible, with proportional to the degree of vertex : a walker spends time in proportion to how connected a node is. Detailed balance is also how MCMC algorithms such as Metropolis–Hastings are designed: choose transition probabilities so that the target distribution satisfies it.
import numpy as np
# the walk on a small undirected graph: move to a uniformly chosen neighbour
edges = [(0, 1), (0, 2), (1, 2), (2, 3), (3, 4)]
n = 5
A = np.zeros((n, n))
for a, b in edges:
A[a, b] = A[b, a] = 1
deg = A.sum(axis=1)
P = A / deg[:, None]
w = deg / deg.sum()
print("degrees:", deg.astype(int).tolist(), " w = deg / sum:", np.round(w, 3).tolist())
print("w P == w:", np.allclose(w @ P, w))
print("detailed balance:", np.allclose(w[:, None] * P, (w[:, None] * P).T))degrees: [2, 2, 3, 2, 1] w = deg / sum: [0.2, 0.2, 0.3, 0.2, 0.1]
w P == w: True
detailed balance: True
In software
PageRank is the stationary distribution of a random surfer with teleportation, which makes the chain regular (PageRank). MCMC samplers in Bayesian statistics (PyMC, Stan) run reversible chains to sample posteriors. Load balancers and caches are analysed by the stationary distribution of their state chains, and "mixing time" bounds tell how long randomized algorithms must run.
Exercises
Exercise 1: A two-state chain
A server is up or down. If up, it goes down the next hour with probability 0.01; if down, it comes back up with probability 0.2. Find the long-run availability.
Answer
.
Solution
For a two-state chain with and , . Detailed balance: .
Exercise 2: Degree rule
A random walk on a star graph with one centre and 4 leaves: what fraction of time is spent at the centre?
Answer
.
Solution
Degrees: centre 4, each leaf 1, total 8. The centre gets . (The walk alternates between centre and leaves, so this chain is periodic, but the time fraction still converges.)
Exercise 3: Ergodic theorem (code)
Simulate the star-graph walk for 100,000 steps and measure the fraction of time at the centre.
Answer
Exactly or very nearly 0.5.
Solution
import random
rng = random.Random(5)
pos, centre_time = 0, 0 # 0 = centre, 1..4 = leaves
steps = 100_000
for _ in range(steps):
pos = rng.randint(1, 4) if pos == 0 else 0
centre_time += pos == 0
print(centre_time / steps)0.5
The walk alternates strictly, so the fraction is exactly 0.5 for an even number of steps: an extreme example of periodicity.
Test yourself
A stationary distribution satisfies…
For a random walk on a connected undirected graph, the stationary probability of a vertex is proportional to…
- 1
- its degree
- its distance to vertex 0
- the number of triangles
What controls how fast approaches for a regular chain?
- The largest eigenvalue
- The second-largest eigenvalue modulus
- The trace of
- The number of states only