Markov Chains

Many random processes unfold over time: the weather changes day by day, a stock price moves tick by tick, a customer navigates through a website page by page. A Markov chain is the simplest model for such sequential randomness. It captures the idea that the future depends on the present but not on the distant past.

The example program for this chapter is in the file 10_markov_chains.lisp.

Andrey Markov and the Origin of Markov Chains

The Russian mathematician Andrey Markov introduced these chains in 1906 as a way to study sequences of random variables that are not independent. Independence had been the dominant assumption in classical probability theory (as in Bernoulli’s theorem and the Central Limit Theorem), but many real phenomena are far from independent. Markov wanted to find the weakest form of dependence for which the great theorems of probability could still be proved.

His original motivating example was surprisingly literary: he analyzed the sequence of consonants and vowels in Pushkin’s novel-in-verse Eugene Onegin. He found that consonants and vowels are not independent: a vowel is more likely to be followed by a consonant than by another vowel. But he could still describe the sequence with a two-state Markov chain and could prove a version of the Law of Large Numbers for it. This was the birth of a new branch of probability theory.

Markov chains have since found applications in essentially every scientific field. Statistical mechanics, queueing theory, population genetics, computer science, natural language processing, and machine learning all use Markov chains as fundamental modeling tools.

The Markov Property

A Markov chain is a sequence of random variables Code Test taking values in a state space. The defining property is the Markov property:

math

The next state depends only on the current state, not on the history of how we got there. This is sometimes called “memorylessness of the future given the present.”

The Markov property is a very strong assumption. It says that the current state contains all the information needed to predict the future. In a weather model, this would mean that today’s weather (Sunny or Rainy) tells us as much about tomorrow’s weather as any longer history. In reality, richer models could take into account humidity, pressure, season, and so on, but the Markov idealization is often a useful approximation.

When the Markov property does not hold in the raw data, a common trick is to expand the state space: define the state to include enough recent history that the resulting sequence is Markovian. For example, a language model that depends on the last two words can be seen as a Markov chain whose “state” is a pair of consecutive words.

For a finite state space, the chain is described by a transition matrix Code Test, where the entry Code Test is the probability of moving from state Code Test to state Code Test in one step. Each row of Code Test is a probability distribution: the entries are non-negative and Code Test.

A Weather Model

Our example uses a simple weather model with two states: Sunny and Rainy. The transition matrix is:

  • From Sunny: 80% chance of staying Sunny, 20% chance of becoming Rainy
  • From Rainy: 40% chance of becoming Sunny, 60% chance of staying Rainy
1 (let* ((P (make-transition-matrix
2             '((0.8 0.2)   ; from Sunny: 80% stay Sunny, 20% -> Rainy
3               (0.4 0.6)))) ; from Rainy: 40% -> Sunny, 60% stay Rainy
4        (start #(1.0d0 0.0d0)))  ; start certainly Sunny
5   ...)

If today is Sunny, there is an 80% chance tomorrow is Sunny too. But if today is Rainy, there is still a 40% chance of a Sunny tomorrow. The weather has persistence: Sunny days tend to follow Sunny days, and Rainy days tend to follow Rainy days.

Evolving the Distribution

If the state distribution at time Code Test is a row vector Code Test, then one step of the chain is a matrix multiplication:

math

After Code Test steps, the distribution is Code Test. The program computes this by repeated multiplication:

 1 (defun step-distribution (dist P)
 2   "Advance one step: v_{t+1} = v_t P. Returns a new distribution vector."
 3   (let* ((n (length dist))
 4          (result (make-array n :initial-element 0.0d0)))
 5     (dotimes (j n)
 6       (setf (aref result j)
 7             (reduce #'+ (loop for i below n
 8                               collect (* (aref dist i) (aref (aref P i) j))))))
 9     result))
10 
11 (defun iterate-chain (dist P steps)
12   "Compute v_steps = v_0 P^steps by repeated multiplication."
13   (let ((d (copy-seq dist)))
14     (dotimes (s steps)
15       (setf d (step-distribution d P)))
16     d))

The Code Test-step transition matrix Code Test has a direct probabilistic meaning: the Code Test entry is Code Test, the probability of being in state Code Test after Code Test steps starting from state Code Test. Powers of the transition matrix are one of the primary computational objects when working with Markov chains.

These powers compose in the obvious way. The Chapman-Kolmogorov equations state that

math

which is just Code Test read entry by entry: to travel from Code Test to Code Test in Code Test steps, pass through some intermediate state Code Test at time Code Test and sum over all the ways to do it. This identity is the Markov-chain backbone, and it is the discrete-state ancestor of the same-named equations that govern continuous-time and continuous-state Markov processes.

Classification of States

Not every state in a Markov chain behaves the same way. A rich vocabulary describes the possible behaviors.

A state Code Test communicates with state Code Test if there is a positive-probability path from Code Test to Code Test and from Code Test to Code Test. Communication is an equivalence relation; the state space partitions into communicating classes. A chain is irreducible if it has just one communicating class, meaning every state can eventually reach every other.

A state is recurrent if the chain returns to it with probability 1, and transient otherwise. In a finite Markov chain, recurrence is equivalent to the state being reachable from itself in the long run.

A state is absorbing if once the chain enters it, it never leaves. In matrix terms, an absorbing state has a Code Test on the diagonal of Code Test. Absorbing chains have their own rich theory, useful for modeling processes that eventually terminate.

The period of a state is the greatest common divisor of the return times to that state. A state is aperiodic if its period is Code Test. Aperiodicity means the chain does not get locked into a deterministic cycle. A chain is ergodic if it is irreducible and aperiodic.

Our weather chain is both irreducible (Sunny and Rainy each reach the other) and aperiodic (the chain can stay in the same state, so no forced cycles). It is therefore ergodic, which as we will see next gives it a unique long-run behavior.

The Stationary Distribution

A stationary distribution Code Test is a row vector that satisfies:

math

If the chain starts distributed as Code Test, it stays distributed as Code Test forever. The stationary distribution is a fixed point of the chain’s evolution.

For an irreducible chain (you can get from any state to any other state) that is aperiodic (not locked into a deterministic cycle), the stationary distribution exists, is unique, and the chain converges to it from any starting state:

math

This is the ergodic theorem for Markov chains. No matter where you start, the chain eventually settles into its equilibrium distribution. It generalizes the Law of Large Numbers: the long-run fraction of time the chain spends in state Code Test is Code Test, regardless of the starting state.

Detailed Balance and Reversibility

A stronger condition than the stationarity equation Code Test is detailed balance:

math

If Code Test and Code Test satisfy detailed balance, then Code Test is a stationary distribution (summing over Code Test gives the ordinary stationarity equation). A chain that satisfies detailed balance is called reversible: viewed in reverse time, it has the same statistical properties.

Detailed balance is a much easier condition to check than the full stationarity equation. Many designed Markov chains, especially those used in Markov chain Monte Carlo, are constructed to satisfy detailed balance for a target distribution Code Test.

Finding the Stationary Distribution

The program finds the stationary distribution two ways. The first is by long iteration: just run the chain for 10,000 steps and read off the distribution:

1 (defun stationary-by-iteration (dist P steps)
2   "Approximate the stationary distribution by long-run simulation of v_t P."
3   (iterate-chain dist P steps))

The second is by solving the linear system Code Test directly. For a two-state chain with transition matrix Code Test, the stationary distribution is:

math
1 (defun stationary-by-linear-system (P)
2   "Solve pi = pi P exactly for a 2-state chain."
3   (let* ((a (aref (aref P 0) 0))   ; P[S->S]
4          (b (aref (aref P 1) 0))   ; P[R->S]
5          (denom (+ (- 1 a) b)))
6     (vector (/ b denom) (/ (- 1 a) denom))))

For larger chains, we solve the system Code Test subject to the constraint Code Test. This is a standard linear algebra problem and can also be posed as finding the left eigenvector of Code Test corresponding to eigenvalue Code Test.

Why the Chain Converges: The Spectral View

Why should Code Test settle down at all, and how fast? The answer lives in the eigenvalues of Code Test. Because every row of Code Test sums to Code Test, the all-ones column vector is a right eigenvector with eigenvalue Code Test, so Code Test is always an eigenvalue of a stochastic matrix. The Perron-Frobenius theorem supplies the rest: for an irreducible, aperiodic stochastic matrix the eigenvalue Code Test is simple, with no repeats, and every other eigenvalue satisfies Code Test. The left eigenvector for eigenvalue Code Test, normalized to sum to Code Test, is the stationary distribution Code Test, and its uniqueness is exactly the simplicity of that eigenvalue.

Convergence then follows by expanding the starting distribution along the eigenvectors of Code Test. Writing the eigenvalues as Code Test, the component along Code Test stays fixed while every other component is multiplied by its eigenvalue at each step:

math

since Code Test for Code Test. The slowest-decaying term is governed by the second-largest eigenvalue modulus Code Test, often called the SLEM. The distance to stationarity shrinks geometrically at rate Code Test: a value near Code Test means the chain forgets its start almost at once, a value near Code Test means slow mixing. For the weather chain the eigenvalues are Code Test and Code Test, so the deviation from Code Test falls by a factor of Code Test each step, which is why the printed distribution has already reached equilibrium by step Code Test.

The natural way to measure the remaining gap is the total variation distance

math

the largest difference in probability the two distributions assign to any event. The mixing time is the number of steps needed to push this distance below a small Code Test. Because the total variation distance decays like Code Test, the mixing time scales as Code Test up to constants. Controlling Code Test is the central problem in the design of Markov chain Monte Carlo samplers, where a chain that mixes slowly can make an otherwise correct algorithm useless in practice. Problems 10.9 and 10.10 measure this decay directly.

Running the Example

 1 === Markov Chain: Weather Model ===
 2 States: (Sunny Rainy)
 3 Transition matrix P (rows = from-state):
 4   Sunny -> [ 0.8,  0.2]
 5   Rainy -> [ 0.4,  0.6]
 6 
 7 Start distribution: [1, 0] (certainly Sunny).
 8 Iterating v_{t+1} = v_t P:
 9   steps=0   : [1.0000, 0.0000]
10   steps=1   : [0.8000, 0.2000]
11   steps=2   : [0.7200, 0.2800]
12   steps=5   : [0.6701, 0.3299]
13   steps=10  : [0.6667, 0.3333]
14   steps=50  : [0.6667, 0.3333]
15   steps=100 : [0.6667, 0.3333]
16 
17 Stationary distribution (by long iteration, t=10000):
18   [0.6667, 0.3333]
19 Stationary distribution (exact, 2-state closed form):
20   [0.6667, 0.3333]
21 Stationary distribution (general linear solve, any size):
22   [0.6667, 0.3333]
23 
24 Convergence to stationarity (total variation distance to pi):
25   t=0    TV=0.3333
26   t=1    TV=0.1333
27   t=2    TV=0.0533
28   t=3    TV=0.0213
29   t=4    TV=0.0085
30   t=5    TV=0.0034
31   t=10   TV=0.0000
32 Mixing time (TV <= 0.01) starting from [1,0]: 4 steps
33 
34 Simulating one 100000-step trajectory (start Sunny):
35   empirical P(Sunny) = 0.6627 (stationary 0.6667)
36 
37 The chain forgets its initial state and converges to the
38 unique stationary distribution (ergodic theorem for Markov chains).

Watch the distribution evolve. We start certainly Sunny: [1, 0]. After one step, there is an 80% chance of Sunny and 20% of Rainy. After two steps, the distribution has shifted further: [0.72, 0.28]. By step 10, the distribution has settled at approximately [2/3, 1/3], and it stays there forever.

The stationary distribution is [2/3, 1/3]: in the long run, about 67% of days are Sunny and 33% are Rainy. All three methods of finding it agree: long iteration, the two-state closed form, and a general linear solve that works for any number of states (it sets up Code Test with the last row replaced by the normalization Code Test and solves by Gaussian elimination).

The program also measures how fast the chain reaches equilibrium. The total variation distance to Code Test falls by a factor of Code Test at every step, which is exactly the second eigenvalue of Code Test predicted by the spectral view above, and the mixing time to get within Code Test is Code Test steps. A separate Code Test-step simulation of a single trajectory spends about Code Test of its time in the Sunny state, the same Code Test from a completely different computation (the exact fraction varies from run to run).

Hitting Times and First-Step Analysis

The stationary distribution answers questions about the long run. A different and equally practical question asks when something first happens: how many steps until the chain first reaches a state, or which of several absorbing states it ends up in. The standard tool is first-step analysis, which sets up equations by conditioning on the first transition, exactly the self-consistency trick we used for the geometric mean in Chapter 4.

Let Code Test be the expected number of steps to reach a target set Code Test starting from state Code Test. If Code Test then Code Test. Otherwise, conditioning on the first move,

math

where the Code Test counts the step just taken and the sum averages the remaining time over where that step lands. This is one linear equation per state, and solving the system gives every expected hitting time at once. Absorption probabilities obey the same kind of system with the Code Test removed: if Code Test is the probability of ending at a chosen absorbing state starting from Code Test, then Code Test, with boundary values Code Test at that target and Code Test at the other absorbing states. First-step analysis is the workhorse behind Problems 10.5 and 10.7, and it is how one computes expected waiting times, gambler’s-ruin probabilities, and the expected running time of randomized algorithms.

Applications of Markov Chains

Markov chains model a vast range of real-world processes. A partial list:

PageRank. Google’s original algorithm for ranking web pages models the web as a huge Markov chain: at each step, a “random surfer” clicks a link on the current page, with occasional teleportation to a random page to guarantee irreducibility. The stationary distribution assigns high weight to pages that many random surfers spend time on. PageRank is a computation of this stationary distribution.

Queueing systems. In operations research, the number of customers in a queue often forms a Markov chain. Its stationary distribution tells us the long-run behavior of the queue: average wait times, server utilization, and so on.

Hidden Markov models. In speech recognition, bioinformatics, and finance, we often observe some quantity that depends on an underlying Markov chain we cannot directly see. Hidden Markov models let us infer the underlying states from the observations.

Reinforcement learning. Modern reinforcement learning is built on Markov decision processes, which extend Markov chains with actions and rewards. An agent chooses actions to maximize expected cumulative reward in an environment whose state evolves as a Markov chain conditional on the agent’s actions.

Markov chain Monte Carlo (MCMC). To sample from a complicated target distribution Code Test, we can construct a Markov chain whose stationary distribution is exactly Code Test and run it for a long time. The Metropolis-Hastings algorithm and Gibbs sampling are the two workhorses of MCMC. This has become the dominant computational tool of Bayesian statistics.

Statistical mechanics. The state of a physical system at thermal equilibrium is distributed according to the Boltzmann distribution, which is the stationary distribution of many physical Markov chains. MCMC and physics share deep intellectual roots.

Beyond Discrete Time

The Markov chains we have discussed evolve at discrete time steps. There is a parallel theory of continuous-time Markov chains, in which transitions happen at random times drawn from exponential distributions. Continuous-time Markov chains describe systems like radioactive decay, chemical reactions, and queueing systems.

There is also a theory of general Markov processes with continuous state spaces, culminating in stochastic differential equations and Brownian motion. These lie beyond the scope of this book, but they build on the same central intuition: the future depends on the present, not on the past.

Why This Matters

The stationary distribution is particularly important. It tells you the long-run behavior of the system: what fraction of time the chain spends in each state, regardless of where it started. This makes Markov chains a powerful tool for analyzing systems that reach equilibrium, from physical systems to computer networks to economic models.

The convergence to stationarity is also the foundation of Markov chain Monte Carlo, which has become the computational engine of modern Bayesian statistics and countless applications in machine learning. Understanding Markov chains is the entry point to a huge portion of modern probabilistic modeling.

Problem Set

Problem 10.1. For the weather chain with transition matrix Code Test, verify by direct substitution that Code Test satisfies Code Test.

Problem 10.2 (Different starting state). Rerun the example program starting from Code Test (certainly Rainy). Does the chain still converge to the same stationary distribution? By what step is the distribution effectively at equilibrium? Explain in words why the answer does not depend on the starting state.

Problem 10.3 (A three-state chain). Consider a chain over states A, B, C with transition matrix

math

Extend the example program to handle three states and compute the stationary distribution by long iteration. Confirm your answer by verifying Code Test.

Problem 10.4 (Detailed balance). Does the two-state weather chain satisfy detailed balance for Code Test? Check the condition Code Test with numerical values. Every two-state chain with a stationary distribution satisfies detailed balance, so this should hold.

Problem 10.5 (An absorbing chain). Consider a chain over states Code Test where state Code Test and state Code Test are absorbing (all transitions from them stay in place) and states Code Test and Code Test each transition to either neighbor with probability Code Test. Starting in state Code Test, compute the probability of eventually being absorbed at state Code Test versus state Code Test.

Problem 10.6 (Periodic chain). Consider a chain over states Code Test where state Code Test always transitions to state Code Test and state Code Test always transitions to state Code Test. Starting from Code Test, iterate the chain. What do you observe? Explain why this chain does not converge to a stationary distribution, despite the fact that Code Test satisfies the stationarity equation.

Problem 10.7 (Expected hitting time). For the weather chain, starting Sunny, what is the expected number of days until the first Rainy day? Hint: the number of days is a geometric random variable with success probability Code Test (from the Sunny to Rainy transition). Confirm this both by formula and by simulation.

Problem 10.8 (Simulating the chain). Modify the program to simulate one long trajectory of the weather chain: at each step, draw a uniform random number and use it to determine the next state given the current state. Run for 100,000 steps and compute the empirical fraction of Sunny days. Compare against the stationary probability Code Test.

Problem 10.9 (Convergence rate). For the weather chain, plot the total variation distance Code Test between the distribution at step Code Test and the stationary distribution, as Code Test grows from Code Test to Code Test. How quickly does the distance shrink? The rate is controlled by the second eigenvalue of Code Test.

Problem 10.10 (Coding exercise). Add a function mixing-time that takes a Markov chain Code Test, a starting distribution, and a tolerance Code Test, and returns the smallest Code Test such that the total variation distance from stationarity is at most Code Test. Test it on the weather chain with Code Test.