Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Appendix: Ergodic Markov Chains and Stationary Distributions

Motivating Example: Long-Run Behaviour in a Cycling System

Consider a simple weather model with two states: Sunny (SS) and Rainy (RR). Each day, sunny weather persists with probability 0.7 and turns rainy with probability 0.3; rainy weather persists with probability 0.4 and turns sunny with probability 0.6. The transition matrix is:

P=(0.70.30.60.4)P = \begin{pmatrix} 0.7 & 0.3 \\ 0.6 & 0.4 \end{pmatrix}

Unlike the maze from Appendix A2, this chain has no absorbing state. The system cycles indefinitely between SS and RR. Yet in the long run, the fraction of days that are sunny stabilises, independently of whether today is sunny or rainy.

This appendix develops the theory behind this observation: when does a Markov chain converge to a unique long-run distribution, and how is that distribution computed?

Theory

Definition: Irreducible Markov Chain


A Markov chain is irreducible if every state is reachable from every other state. That is, for all states ii and jj, there exists t1t \geq 1 such that (Pt)ij>0(P^t)_{ij} > 0.


Irreducibility means the chain cannot be split into disconnected parts that never communicate. For example, a chain where state 1 can only reach states 1 and 2, while state 3 can only reach itself, is not irreducible.

Definition: Aperiodic Markov Chain


A state ii has period di=gcd{t1:(Pt)ii>0}d_i = \gcd\{t \geq 1 : (P^t)_{ii} > 0\}. A state is aperiodic if di=1d_i = 1. A Markov chain is aperiodic if all states are aperiodic.


Intuitively, an aperiodic chain does not get “trapped” in a cycle that forces it to return to a state only at fixed multiples of some period d>1d > 1.

Definition: Ergodic Markov Chain


A finite Markov chain is ergodic if it is both irreducible and aperiodic.


Theorem: Ergodic Theorem for Finite Markov Chains


If a finite Markov chain with transition matrix PP is ergodic, then:

  1. There exists a unique stationary distribution πRn\pi \in \mathbb{R}^n satisfying:

    πP=πandi=1nπi=1,πi>0 i.\pi P = \pi \qquad \text{and} \qquad \sum_{i=1}^n \pi_i = 1,\quad \pi_i > 0\ \forall i.
  2. For any initial distribution π(0)\pi^{(0)}:

    limtπ(0)Pt=π.\lim_{t \to \infty} \pi^{(0)} P^t = \pi.
  3. The long-run fraction of time spent in state ii equals πi\pi_i, regardless of the starting state.


The stationary distribution π\pi is the left eigenvector of PP corresponding to eigenvalue 1. It can be computed by solving the linear system:

π(PI)=0subject toi=1nπi=1.\pi (P - I) = 0 \qquad \text{subject to} \qquad \sum_{i=1}^n \pi_i = 1.

Equivalently, since (PI)(P - I) is singular, one replaces any one equation with the normalisation constraint and solves the resulting non-singular system.

Example: Stationary Distribution of a 2-State Chain

Consider a Markov chain with two states {A,B}\{A, B\} and transition matrix:

P=(1ααβ1β)P = \begin{pmatrix} 1 - \alpha & \alpha \\ \beta & 1 - \beta \end{pmatrix}

where 0<α,β<10 < \alpha, \beta < 1. This chain is irreducible (both states communicate) and aperiodic (self-loops exist), hence ergodic.

The stationary distribution satisfies πP=π\pi P = \pi:

πA(1α)+πBβ=πAπAα=πBβ\pi_A (1 - \alpha) + \pi_B \beta = \pi_A \qquad \Rightarrow \qquad \pi_A \alpha = \pi_B \beta

Combined with πA+πB=1\pi_A + \pi_B = 1:

πA=βα+β,πB=αα+β.\pi_A = \frac{\beta}{\alpha + \beta}, \qquad \pi_B = \frac{\alpha}{\alpha + \beta}.

For the weather example in Motivating Example: Long-Run Behaviour in a Cycling System with α=0.3\alpha = 0.3 and β=0.6\beta = 0.6:

πS=0.60.90.667,πR=0.30.90.333.\pi_S = \frac{0.6}{0.9} \approx 0.667, \qquad \pi_R = \frac{0.3}{0.9} \approx 0.333.

This means that in the long run, approximately 2 out of every 3 days are sunny, regardless of the weather on the first day.

Example: Verifying Ergodicity and Computing the Stationary Distribution

Consider the 3×33 \times 3 transition matrix:

P=(00.60.40.300.70.50.50)P = \begin{pmatrix} 0 & 0.6 & 0.4 \\ 0.3 & 0 & 0.7 \\ 0.5 & 0.5 & 0 \end{pmatrix}

Step 1: Check irreducibility. Inspect PP and P2P^2:

P2=PP=(0.380.200.420.350.530.120.150.300.55)P^2 = P \cdot P = \begin{pmatrix} 0.38 & 0.20 & 0.42 \\ 0.35 & 0.53 & 0.12 \\ 0.15 & 0.30 & 0.55 \end{pmatrix}

All entries of P2P^2 are positive, confirming that every state can reach every other in at most 2 steps. The chain is irreducible.

Step 2: Check aperiodicity. Since P2P^2 has strictly positive diagonal entries (di=1d_i = 1 for all ii), the chain is aperiodic.

Step 3: Solve πP=π\pi P = \pi. We solve:

(π1π2π3)(00.60.40.300.70.50.50)=(π1π2π3)\begin{align*} \begin{pmatrix} \pi_1 & \pi_2 & \pi_3 \end{pmatrix} \begin{pmatrix} 0 & 0.6 & 0.4 \\ 0.3 & 0 & 0.7 \\ 0.5 & 0.5 & 0 \end{pmatrix} \\ &= \begin{pmatrix} \pi_1 & \pi_2 & \pi_3 \end{pmatrix} \end{align*}

This gives:

0.3π2+0.5π3=π1,0.6π1+0.5π3=π2,0.4π1+0.7π2=π30.3\pi_2 + 0.5\pi_3 = \pi_1, \quad 0.6\pi_1 + 0.5\pi_3 = \pi_2, \quad 0.4\pi_1 + 0.7\pi_2 = \pi_3

Replace the third equation with π1+π2+π3=1\pi_1 + \pi_2 + \pi_3 = 1 and solve to obtain:

π=(65227,  80227,  82227)(0.286,  0.352,  0.361).\pi = \left(\frac{65}{227},\; \frac{80}{227},\; \frac{82}{227}\right) \approx (0.286,\; 0.352,\; 0.361).

Exercises

Programming

Computing the Stationary Distribution with NumPy

The stationary distribution π\pi satisfies πP=π\pi P = \pi, which is equivalent to π(PI)=0\pi (P - I) = 0. To compute it numerically, we replace one equation with the normalisation constraint iπi=1\sum_i \pi_i = 1:

import numpy as np

def stationary_distribution(P):
    """Compute the stationary distribution of an ergodic transition matrix."""
    n = P.shape[0]
    A = (P - np.eye(n)).T
    # Replace last row with normalisation constraint
    A[-1, :] = 1.0
    b = np.zeros(n)
    b[-1] = 1.0
    return np.linalg.solve(A, b)

# Weather example
P_weather = np.array([[0.7, 0.3],
                       [0.6, 0.4]])

pi = stationary_distribution(P_weather)
print(f"Stationary distribution: Sunny={pi[0]:.4f}, Rainy={pi[1]:.4f}")
Stationary distribution: Sunny=0.6667, Rainy=0.3333

Verifying Convergence

We can verify the Ergodic Theorem numerically by powering the transition matrix:

# Confirm convergence: P^t rows all approach pi as t grows large
P_power = np.linalg.matrix_power(P_weather, 50)
print("P^50 (rows should all equal stationary distribution):")
print(np.round(P_power, 6))
P^50 (rows should all equal stationary distribution):
[[0.666667 0.333333]
 [0.666667 0.333333]]

Stationary Distribution of a 3-State Chain

P_3 = np.array([[0.0, 0.6, 0.4],
                 [0.3, 0.0, 0.7],
                 [0.5, 0.5, 0.0]])

pi_3 = stationary_distribution(P_3)
print(f"Stationary distribution: {np.round(pi_3, 4)}")

# Verify: pi @ P should equal pi
print(f"Verification pi @ P: {np.round(pi_3 @ P_3, 4)}")
Stationary distribution: [0.2863 0.3524 0.3612]
Verification pi @ P: [0.2863 0.3524 0.3612]

Visualising Convergence from Different Starting States

import matplotlib.pyplot as plt

T = 30
pi_0_sunny = np.array([1.0, 0.0])  # start Sunny
pi_0_rainy = np.array([0.0, 1.0])  # start Rainy

dist_sunny = [pi_0_sunny @ np.linalg.matrix_power(P_weather, t) for t in range(T)]
dist_rainy = [pi_0_rainy @ np.linalg.matrix_power(P_weather, t) for t in range(T)]

plt.figure()
plt.plot([d[0] for d in dist_sunny], label="Start Sunny", color="orange")
plt.plot([d[0] for d in dist_rainy], label="Start Rainy", color="blue")
plt.axhline(pi[0], color="black", linestyle="--", label=f"Stationary $\\pi_S={pi[0]:.3f}$")
plt.xlabel("Time step $t$")
plt.ylabel("Probability of being Sunny")
plt.title("Convergence to stationary distribution")
plt.legend()
plt.ylim(0, 1);
<Figure size 640x480 with 1 Axes>

Notable Research

The theory of ergodic Markov chains builds on the foundational work of Andrei Markov himself, who first studied the long-run behaviour of dependent random sequences in the early twentieth century.

The modern treatment of ergodic chains and stationary distributions is presented in the classical reference Kemeny & Snell, 1976, which establishes the Ergodic Theorem in the finite-state setting and its connections to the fundamental matrix theory of absorbing chains.

The application of ergodic theory to repeated games and direct reciprocity is central to the analysis of reactive strategies in the Prisoner’s Dilemma, as explored in Direct Reciprocity. The computation of long-run payoffs from stationary distributions provides the mathematical foundation for Press and Dyson’s influential analysis of memory-one strategies Press & Dyson, 2012.

Conclusion

This appendix introduced ergodic Markov chains and the Ergodic Theorem, which guarantees convergence to a unique stationary distribution for chains that are irreducible and aperiodic. These tools are essential for analysing systems that cycle indefinitely rather than being absorbed.

The key concepts are summarised in Table A3.1.

Table A3.1:Summary of ergodic Markov chains

ConceptDescription
Irreducible ChainEvery state is reachable from every other state
Aperiodic ChainNo state is locked into returning only at multiples of a fixed period
Ergodic ChainIrreducible and aperiodic; converges to a unique stationary distribution
Stationary DistributionπP=π\pi P = \pi, πi=1\sum \pi_i = 1; gives long-run fraction of time in each state
Ergodic Theoremπ(0)Ptπ\pi^{(0)} P^t \to \pi for any initial distribution π(0)\pi^{(0)}

Solutions

References
  1. Kemeny, J. G., & Snell, J. L. (1976). Finite Markov chains. Springer-Verlag.
  2. Press, W. H., & Dyson, F. J. (2012). Iterated Prisoner’s Dilemma contains strategies that dominate any evolutionary opponent. Proceedings of the National Academy of Sciences, 109(26), 10409–10413.