Probability · stochastic processes Section 1.3 · item 31 of 33

Markov chains &
stationary distributions

A system that forgets. Where it goes next depends only on where it is now — not on the route it took to get here. That single restriction is severe enough to make almost everything computable, and loose enough to describe an enormous amount of the world.

Run one long enough and something surprising happens: it stops mattering where you started. The chain settles into a fixed long-run distribution over states, and that distribution is the object nearly every application is really after.

The Markov property

P(Xt+1 | Xt, Xt−1, …, X0) = P(Xt+1 | Xt)

The present state is a sufficient summary of the entire past. Everything you need to know about history is already encoded in where you are.

Run one and watch it forget

Four states a robot moves between. Pick where the whole probability mass starts, then step the chain forward. The solid bars are the current distribution; the outlined bars are the long-run one. Three of the four presets deliberately break a condition — those are the interesting ones.

start in
Step0
Transition matrix P
Distance from long-run
01Memorylessness

The one assumption

A stochastic process is a sequence of random variables indexed by time. A Markov chain is one where the conditional distribution of the next value, given the entire history, collapses to a dependence on the most recent value alone.

The greyed history is genuinely irrelevant — not approximately, exactly

What it buys

  • Tractability. A joint distribution over T steps that would need exponentially many parameters factorizes into T copies of one small matrix.
  • Simulation. Generating a path needs only the current state, so you can run one forever in constant memory.
  • Long-run answers. Questions about behaviour after infinite time reduce to linear algebra on a finite matrix.
  • Composability. Add actions and you have an MDP. Hide the states and you have an HMM. Both inherit the tractability.

What it costs

Real systems often remember. How long you've been in a state usually matters; a machine that has run for eight hours is not the same as one that just started, even if both are labelled "working".

The standard fix is to enlarge the state. If tomorrow depends on the last two days, define the state as the pair of the last two days — and the process becomes first-order Markov again. Any finite-order dependence can be absorbed this way. The cost is that the state space grows exponentially in how much history you fold in, which is precisely the trade every n-gram language model makes.
02The matrix

Everything in one table

For a finite state space, the whole chain is a square matrix. Entry P[i][j] is the probability of moving to state j given that you're in state i.

Properties

P[i][j] ≥ 0
Probabilities, so non-negative.
rows sum to 1
From any state you must go somewhere, possibly back to where you were. Called row-stochastic.
columns don't
A common early confusion. Only the rows are constrained.
self-loops
The diagonal is "stay put", and it's usually the largest entry in slow-moving systems.

Evolving a distribution

πt+1 = πt P

With π as a row vector multiplied on the left. Each new entry is a weighted average of where you might have been, weighted by the chance of arriving from there.

Powers give multi-step behaviour

πt = π0 Pt

The (i,j) entry of Pn is the probability of being in j exactly n steps after starting in i. That's a genuinely useful fact — questions about "where will it be in ten steps" are answered by matrix multiplication, not simulation.

Chapman–Kolmogorov: Pm+n = PmPn

Which says nothing more than: to get somewhere in m+n steps, be somewhere after m and continue. The Markov property is what makes that decomposition valid.

Watch the convention. Some texts use column-stochastic matrices with column vectors and write π' = Pπ. Both are correct and they are transposes of each other. Check which one a paper or library is using before you trust an index — mixing them produces plausible-looking nonsense.
03State types

The vocabulary that decides everything

Whether a chain has a unique long-run distribution — and whether it actually converges to it — comes down to two structural properties. These are the conditions the hero's four presets are testing.

TermMeaningWhy it matters
accessiblej is reachable from i in some number of stepsThe basic connectivity relation
communicatingEach is accessible from the otherPartitions the states into classes
irreducibleEvery state reaches every other — one classRequired for a unique stationary distribution
recurrentYou return with probability 1Guaranteed for all states of a finite irreducible chain
transientPositive chance of never returningThese get zero stationary probability
absorbingOnce entered, never left — P[i][i] = 1Breaks irreducibility. All mass ends up here
periodGCD of all return-time lengthsPeriod 1 means aperiodic
aperiodicReturns aren't locked to a rhythmRequired for convergence, not for existence
ergodicIrreducible and aperiodicThe good case. Everything works
A single self-loop anywhere guarantees aperiodicity in an irreducible chain. If you can return in 1 step and also in 2, the GCD is 1 and the whole chain is aperiodic. This is why adding a small "stay put" probability is the standard trick for fixing periodicity — and why the periodic preset in the hero has a completely empty diagonal.
Periodicity is a property of the class, not of one state. In an irreducible chain, every state shares the same period. So you cannot fix a periodic chain by patching one node's behaviour in isolation — though in practice, one self-loop is enough to make the whole thing aperiodic.
04Stationarity

The distribution that doesn't move

A stationary distribution is a probability vector that the chain leaves unchanged. Push it through one step and you get it back.

π P = π,   with   ∑i πi = 1,   πi ≥ 0

As linear algebra

π is a left eigenvector of P with eigenvalue 1, normalized to sum to one. Every stochastic matrix has such an eigenvalue — the all-ones column vector is the corresponding right eigenvector, since rows sum to 1.

As balance

Probability flowing into each state exactly equals probability flowing out. The system is in equilibrium — not static, since individual walkers keep moving, but macroscopically unchanging.

As time spent

πi is the long-run fraction of time a single walker spends in state i. It's also the reciprocal of the mean return time to i — a state you visit every ten steps on average carries 0.1 of the mass.

The fundamental theorem

For a finite chain that is irreducible and aperiodic:

  • A stationary distribution exists.
  • It is unique.
  • πt converges to it from any starting distribution.
  • Every πi is strictly positive.

Drop irreducibility and uniqueness goes. Drop aperiodicity and convergence goes, though a unique stationary distribution may still exist.

Stationary versus limiting

These are different and the periodic preset shows why. A deterministic four-cycle has a perfectly good stationary distribution — uniform, ¼ each. Start there and you stay there forever.

But start all the mass in one state and it will march around the cycle indefinitely, never converging. The stationary distribution exists; the chain never reaches it. What does converge is the time-average, which is why period is a problem for simulation but not for equilibrium.

# three ways to find it, all agreeing
import numpy as np

P = np.array([[0.30, 0.60, 0.08, 0.02],
              [0.15, 0.35, 0.48, 0.02],
              [0.10, 0.25, 0.60, 0.05],
              [0.40, 0.30, 0.10, 0.20]])

# 1 — power iteration: just run it forward
pi = np.ones(4) / 4
for _ in range(1000):
    pi = pi @ P

# 2 — eigenvector: left eigenvector of P for eigenvalue 1
vals, vecs = np.linalg.eig(P.T)
v = np.real(vecs[:, np.argmin(np.abs(vals - 1))])
pi_eig = v / v.sum()

# 3 — solve the linear system directly
#     (P^T - I)pi = 0, with the normalization row appended
A = np.vstack([P.T - np.eye(4), np.ones(4)])
b = np.array([0, 0, 0, 0, 1])
pi_solve = np.linalg.lstsq(A, b, rcond=None)[0]

Power iteration is the one to reach for on large sparse chains — it needs only matrix-vector products, never a factorization. That's exactly how PageRank is computed over billions of pages.

05Mixing

How fast does it forget?

Convergence is guaranteed for an ergodic chain, but the rate varies enormously and it's the rate that decides whether a method is practical.

Total variation distance

πtπTV = ½ ∑i |πt(i) − π(i)|

The standard measure of how far the current distribution is from equilibrium. It runs from 0 to 1 and it's the number displayed in the hero. Mixing time is how many steps it takes to get that below some small threshold.

The spectral gap

gap = 1 − |λ2|

Every stochastic matrix has λ1 = 1. The second-largest eigenvalue in absolute value controls convergence: the distance to stationarity decays roughly like |λ₂|ᵗ. A large gap means fast mixing. A gap near zero means a chain that crawls — and a λ2 of exactly −1 is what periodicity looks like in the spectrum.

Slow mixing is the practical failure mode of MCMC. A chain that must cross a low-probability valley to reach another mode may take astronomically long to do it — and from the inside, a stuck chain looks exactly like a converged one. Trace plots and R-hat across multiple independent chains exist because you cannot detect this from a single well-behaved-looking run.

Bottlenecks

Two well-connected regions joined by a narrow bridge. Fine locally, catastrophic globally. Formalized by the conductance of the graph.

Near-periodicity

Not periodic, but almost. The distribution oscillates while slowly damping, and naive convergence checks read the oscillation as noise.

Dimension

Random-walk proposals mix badly as dimension grows, which is the entire reason gradient-based samplers like HMC exist.

06Ergodicity

One walker, watched long enough

Everything so far tracked a distribution — probability mass spread across states. The ergodic theorem says you can get the same answer from a single walker: the fraction of time it spends in each state converges to the stationary distribution. Run one and watch.

Steps taken0
Max error
Solid = observed visit frequency · outlined = stationary distribution
This is the licence for MCMC, stated plainly. Time averages equal ensemble averages. You cannot compute the posterior directly, but you can build a chain whose stationary distribution is the posterior, run one walker for a long time, and average whatever you want over the states it visited. Every sampler in Bayesian computation rests on this theorem.
"Long enough" is doing real work in that sentence. The theorem is asymptotic — it promises convergence eventually, with no schedule attached. Early samples reflect the starting point rather than the target, which is why burn-in gets discarded, and successive samples are correlated, which is why effective sample size is much smaller than the number of iterations.
07Detailed balance

Designing a chain backwards

So far the chain came first and the stationary distribution was whatever fell out. MCMC inverts this: you know the distribution you want and need a chain that has it. Detailed balance is the tool that makes the inversion easy.

πi Pij = πj Pji   for every pair i, j
Pairwise flows cancel, so the totals must too

The flow of probability from i to j exactly matches the flow back. Sum over i and stationarity follows immediately — so detailed balance is a sufficient condition, and a much easier one to arrange than solving πP = π directly.

This is how Metropolis–Hastings is derived. Fix the target π, propose moves however you like, then choose the acceptance probability so that detailed balance holds. The famous min(1, ratio) rule is exactly the value that makes the two flows equal — and because it's a ratio of π values, the normalizing constant cancels. That cancellation is what lets you sample a posterior you cannot compute.
Sufficient, not necessary. Chains satisfying detailed balance are called reversible, and plenty of useful chains aren't. Non-reversible samplers can mix substantially faster precisely by maintaining a directional flow that detailed balance forbids.
08Beyond finite

Continuous states, continuous time

Continuous state space

The matrix becomes a transition kernel K(x, dy), sums become integrals, and stationarity reads ∫ π(x) K(x, A) dx = π(A). Irreducibility and aperiodicity get technical replacements (Harris recurrence), but the shape of the theory survives.

This is the setting every real MCMC sampler lives in — the states are parameter vectors in ℝᵈ, not four labelled boxes.

Continuous time

Transitions happen at random moments rather than on a clock. The generator matrix Q replaces P, its rows sum to zero, and P(t) = e^{Qt}. Stationarity becomes πQ = 0.

Memorylessness forces exponential holding times. If the future truly doesn't depend on how long you've already waited, the waiting-time distribution has no choice — the exponential is the only continuous distribution with that property. That single fact generates the Poisson process, birth-death chains, and most of queueing theory.

Poisson process

Counting events with exponential gaps. Arrivals at a queue, photons at a detector, failures in a fleet.

Birth–death chains

Transitions only to neighbouring states. Queue lengths, population sizes, inventory levels.

Random walks

On a line, a lattice, or a graph. The unbounded ones are recurrent in one and two dimensions but transient in three — a famous and genuinely surprising result.

09Hidden chains

When you can't see the state

A hidden Markov model puts a Markov chain underneath and lets you observe only a noisy signal emitted from each state. The chain is real; the states are not directly measurable.

Hidden states above, observations below · only the bottom row is measured
QuestionAlgorithmWhat it computes
How likely is this sequence?ForwardP(observations) by summing over every possible hidden path
What state am I in now?Forward (filtering)P(state at t | observations up to t) — the online case
What state was I in then?Forward–backwardP(state at t | all observations) — smoothing, with hindsight
What was the most likely path?ViterbiThe single best state sequence, by dynamic programming
What are the parameters?Baum–WelchEM for the transition and emission matrices from unlabelled data
The naive approach is exponential and the right one is linear. Summing over all state paths means Kᵀ possibilities. The forward algorithm collapses this to O(TK²) by noticing that everything about the past you need is the distribution over the current state — which is precisely the Markov property doing its work. Kalman filters are the same idea with continuous Gaussian states.
10Applications

Where this actually gets used

WhereStatesWhat the stationary distribution means
MCMCParameter valuesThe posterior itself. The chain is engineered to have it
PageRankWeb pagesLong-run fraction of time a random surfer spends on each page — the ranking
MDPs / RLEnvironment statesState visitation frequency under a policy. Central to policy gradient theory
n-gram language modelsRecent wordsUnigram frequencies. The chain is the model
Queueing theoryNumber waitingLong-run queue length distribution. Capacity planning
Credit ratingsRating gradesLong-run portfolio composition, and default probabilities via absorption
ReliabilityWorking / degraded / failedSteady-state availability. Failure is often an absorbing state
GeneticsAlleles, sequence motifsEquilibrium frequencies. HMMs for gene finding and alignment
Speech & handwritingPhonemes, strokesHMMs dominated both fields before deep learning arrived
Customer lifecycleActive / lapsed / churnedSteady-state mix, and expected time to churn via absorption

PageRank, in one sentence

Model a surfer who follows a random outbound link at each step. The stationary distribution of that chain is the ranking — pages are important if important pages point at them, which is circular in exactly the way an eigenvector resolves.

The clever part is the fix. A raw web graph is neither irreducible nor aperiodic — dangling pages and disconnected regions break both. So with probability 0.15 the surfer teleports to a page chosen at random. That single modification makes every state reachable from every other, guarantees a unique stationary distribution, and sets the mixing rate. Fifty years of Markov theory, applied as one damping constant.

11When it breaks

The assumption is usually wrong. Sometimes that's fine.

Duration matters and the state doesn't capture it. The chance a machine fails in the next hour depends on how long it has been running, but a state labelled "running" has already forgotten. Geometric holding times are baked into any finite Markov chain, and real durations rarely are.
Long-range dependence. Text, music and financial series all have structure spanning far more than one step. An n-gram model is a Markov chain, and its characteristic failure — locally fluent, globally incoherent — is precisely the Markov property showing through.
The environment isn't stationary. Transition probabilities that drift over time break the entire framework. A stationary distribution computed from last year's matrix describes a system that no longer exists.

Repairs, in order of cost

enlarge state
Fold the needed history in. Exact, and the state space grows exponentially.
higher order
Condition on the last k states. Same trade, stated differently.
semi-Markov
Let holding times have arbitrary distributions rather than geometric.
hidden states
An HMM's latent variable can carry information the observations don't.
learned state
An RNN or transformer hidden vector as a soft, continuous, learned summary of history. Strictly more general, entirely uninterpretable.
The honest framing: "Markov" is not a claim about reality, it's a claim about your state definition. Any process becomes Markov if the state is rich enough — in the limit, the state is the entire history. The engineering question is how much you can throw away before the answers stop being useful.
12Practice

Drills

#Do thisYou'll know it when
1Write a 3-state transition matrix by hand and evolve a distribution five steps on paperRow-stochastic and left-multiplication are automatic
2Find the stationary distribution three ways — power iteration, eigenvector, linear solveAll three agree, and you know when to use each
3Build a periodic chain and plot the distribution over timeYou can explain why a stationary distribution exists but is never reached
4Add one small self-loop to that chain and re-runYou watch aperiodicity restore convergence
5Simulate a single walker and plot visit frequencies against πThe ergodic theorem is something you've seen, not read
6Compute the second eigenvalue for a fast and a slow chainThe spectral gap predicts what you observe
7Build an absorbing chain and compute expected time to absorptionFundamental matrix methods make sense
8Verify detailed balance for a chain you constructed, then for one you didn'tYou see that stationarity is the weaker condition
9Implement PageRank on a twenty-node graph, with and without dampingYou can point at what teleportation actually fixes
10Fit a bigram model on text, then find its stationary distributionIt's the unigram frequency table, and you know why

The one-paragraph summary

A Markov chain is a stochastic process whose next state depends only on the current one, which for a finite state space means the whole thing is a row-stochastic transition matrix, distributions evolve by left-multiplication, and n-step behaviour is a matrix power. A stationary distribution satisfies πP = π — a left eigenvector for eigenvalue 1 — and can be read as an equilibrium of probability flow or as the long-run fraction of time spent in each state. If the chain is irreducible it has a unique one, and if it is also aperiodic then any starting distribution converges to it, at a rate governed by the second-largest eigenvalue. The ergodic theorem then says a single walker's visit frequencies converge to the same distribution, which is the entire justification for MCMC; detailed balance is the sufficient condition that lets you construct a chain with a chosen stationary distribution, and is where Metropolis–Hastings comes from. The memorylessness assumption is almost always false in detail, and the standard repair is to enlarge the state until it becomes true — which is a statement about your model, not about the world.