TExcelTAD
Home › Probability Theory › Ergodic Chains and Stationary Distributions

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 w\mathbf{w} is stationary for PP if

wP=w\mathbf{w}P = \mathbf{w}

If the chain starts with distribution w\mathbf{w}, it has distribution w\mathbf{w} at every later time. Algebraically, w\mathbf{w} is a left eigenvector of PP with eigenvalue 1, normalised to sum to 1. To compute it, solve w(P−I)=0\mathbf{w}(P - I) = 0 together with ∑iwi=1\sum_i w_i = 1.

The fundamental limit theorem

For a regular chain (some power of PP has all entries positive):

  1. there is a unique stationary distribution w\mathbf{w}, with all entries positive;
  2. Pn→WP^n \to W, the matrix whose rows all equal w\mathbf{w};
  3. for every starting distribution u\mathbf{u}, uPn→w\mathbf{u}P^n \to \mathbf{w}.

For an ergodic but periodic chain, (1) still holds and the long-run fraction of time in each state still converges to w\mathbf{w} (the ergodic theorem), even though PnP^n itself oscillates.

python · try it yourself
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 7/187/18 of the days, cloudy 1/31/3 and rainy 5/185/18.

How fast is the convergence?

The distance from uPn\mathbf{u}P^n to w\mathbf{w} shrinks roughly like ∣λ2∣n\lvert \lambda_2 \rvert^n, where λ2\lambda_2 is the second-largest eigenvalue in absolute value. The spectral gap 1−∣λ2∣1 - \lvert \lambda_2 \rvert 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).

python · try it yourself
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 wipij=wjpjiw_i p_{ij} = w_j p_{ji} for all i,ji, j (the flow from ii to jj equals the flow back), then w\mathbf{w} is stationary. Such chains are reversible. Random walks on undirected graphs are reversible, with wiw_i proportional to the degree of vertex ii: 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.

python · try it yourself
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

0.2/0.21≈0.9520.2/0.21 \approx 0.952.

Solution

For a two-state chain with pup,down=ap_{\text{up,down}} = a and pdown,up=bp_{\text{down,up}} = b, w=(ba+b,aa+b)\mathbf{w} = \left(\frac{b}{a + b}, \frac{a}{a + b}\right). Detailed balance: wup⋅0.01=wdown⋅0.2w_{\text{up}} \cdot 0.01 = w_{\text{down}} \cdot 0.2.

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

1/21/2.

Solution

Degrees: centre 4, each leaf 1, total 8. The centre gets 4/84/8. (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
python · try it yourself
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 w\mathbf{w} satisfies…

  1. Pw=0P\mathbf{w} = 0
  2. wP=w\mathbf{w}P = \mathbf{w}
  3. w=P2\mathbf{w} = P^2
  4. wP=0\mathbf{w}P = 0

For a random walk on a connected undirected graph, the stationary probability of a vertex is proportional to…

  1. 1
  2. its degree
  3. its distance to vertex 0
  4. the number of triangles

What controls how fast uPnuP^n approaches ww for a regular chain?

  1. The largest eigenvalue
  2. The second-largest eigenvalue modulus
  3. The trace of PP
  4. The number of states only