Markov Chains
Transition matrices, Chapman–Kolmogorov, classification of states, and stationary distributions.
Markov Chains
Intuition first
A Markov chain is a system that moves between states, where the next state depends only on the current one — not on how you got there. Today's weather determines tomorrow's probabilities; last week's weather adds nothing once today is known.
That restriction is the Markov property, and it is what makes the model tractable. Instead of tracking a whole history, you need a single matrix of transition probabilities.
Two questions matter. Short-term: where will the system be in steps? That is matrix powers. Long-term: does it settle into a stable pattern of state occupancies? That is a stationary distribution, and it turns out to be an eigenvector problem — which is why this lesson requires linear algebra.
Markov chains sit directly beneath hidden Markov models, PageRank, MCMC sampling, and the MDPs of reinforcement learning. This is the last lesson in the probability module and the bridge to all four.
The Markov property
The current state is a sufficient summary of the past.
| Symbol | Meaning | Read aloud |
|---|---|---|
| Xₜ | State at time t | X sub t |
| P | Transition matrix, with Pᵢⱼ = P(i → j) | P |
| π | State distribution, a row vector | pi |
| π* | Stationary distribution | pi star |
| Pᵏ | k-step transition matrix | P to the k |
The transition matrix
Each row is a probability distribution over destinations, so every row sums to 1. A matrix with this property is called row-stochastic.
Multi-step transitions
The Chapman–Kolmogorov equationsAdvanced
Two steps, from to , must pass through some intermediate state . Summing over all possibilities — the law of total probability:
The second factor dropped the conditioning on by the Markov property. The right-hand side is
which is precisely the definition of matrix multiplication. Induction extends it:
So the whole temporal structure of the chain is encoded in powers of one matrix, and questions about the future become linear algebra. This is why eigendecomposition is the natural tool.
If the state distribution at time is the row vector , then
Stationary distributions
A distribution is stationary if applying the chain leaves it unchanged:
Solved problem 1 · Weather chain, computed fully
Weather is Sunny or Rainy. If sunny today, tomorrow is sunny with probability 0.8. If rainy today, tomorrow is rainy with probability 0.6.
(a) Write . (b) Given sunny today, find the distribution in two days. (c) Find the stationary distribution. (d) Verify it.
Step 1 — part (a): build the matrix
Order the states (Sunny, Rainy). Sunny → sunny is 0.8, so sunny → rainy is 0.2. Rainy → rainy is 0.6, so rainy → sunny is 0.4.
Row sums: and ✓
Step 2 — part (b): one step
Start with — sunny with certainty.
Step 3 — two steps
First component:
Second component:
Step 4 — part (c): solve for the stationary distribution
Write with . The condition gives two equations; use the first:
Substitute :
Step 5 — part (d): verify
First component:
Second component:
Step 6 — watch the convergence
Converging geometrically. The rate is governed by the second-largest eigenvalue, here , so the error shrinks by a factor of 0.4 each step — visible in the sequence, where the gap from goes .
Answer
; after two days ; stationary . In the long run it is sunny two days in three regardless of today's weather.
Classification of states
| Term | Meaning |
|---|---|
| Accessible | reachable from in some number of steps |
| Communicating | and each accessible from the other |
| Irreducible | All states communicate — one class |
| Recurrent | Return is certain |
| Transient | Return has probability less than 1 |
| Absorbing | — once entered, never left |
| Period | gcd of return times; aperiodic if the period is 1 |
Where this leads
- Hidden Markov models add unobserved states emitting observations — the Viterbi and forward–backward algorithms operate on exactly this machinery.
- PageRank is the stationary distribution of a random surfer on the web graph, computed by power iteration, which is nothing but repeated multiplication by .
- MCMC reverses the problem: given a target , construct a whose stationary distribution is , then run it.
- MDPs add actions and rewards to a Markov chain, which is the entire foundation of reinforcement learning.
import numpy as np
P = np.array([[0.8, 0.2],
[0.4, 0.6]])
# Verify row-stochasticity.
assert np.allclose(P.sum(axis=1), 1.0)
# Forward iteration from a sunny start.
pi = np.array([1.0, 0.0])
print("step P(sunny) P(rainy) error from 2/3")
for t in range(6):
print(f"{t:4d} {pi[0]:8.4f} {pi[1]:8.4f} {abs(pi[0] - 2/3):.6f}")
pi = pi @ P
# Stationary distribution as the left eigenvector for eigenvalue 1.
vals, vecs = np.linalg.eig(P.T) # transpose for LEFT eigenvectors
i = np.argmin(np.abs(vals - 1.0))
stat = np.real(vecs[:, i]); stat /= stat.sum()
print(f"\nstationary (eigenvector) {stat}")
print(f"theory [{2/3:.6f} {1/3:.6f}]")
# Convergence rate is set by the second-largest eigenvalue modulus.
print(f"eigenvalues {np.sort(np.abs(vals))[::-1]} -> rate {np.sort(np.abs(vals))[::-1][1]:.2f}")
# A periodic chain: stationary distribution exists but is never reached.
Q = np.array([[0.0, 1.0], [1.0, 0.0]])
p = np.array([1.0, 0.0])
print("\nperiodic chain, never converges:")
for t in range(4):
print(f" step {t}: {p}")
p = p @ QExercise 1
A machine is Working or Broken. Working → broken with probability 0.1; broken → working with probability 0.5. What fraction of time is it working in the long run?
Show solutionHide solution
Let with . The stationarity condition on the first component:
Substitute :
Working about 83.3% of the time.
A useful cross-check: for a two-state chain the stationary probability of state 1 is ✓. The chain spends time in each state in inverse proportion to how readily it leaves.
Exercise 2
Explain why a chain with two absorbing states has no unique stationary distribution.
Show solutionHide solution
An absorbing state has , so once entered the chain never leaves.
With two absorbing states and , both and satisfy — each is stationary, since a chain sitting in an absorbing state stays there.
Worse, any mixture is also stationary:
gives an infinite family of stationary distributions.
The structural reason is reducibility. The convergence theorem requires irreducibility — every state reachable from every other. Here cannot be reached from or vice versa, so the state space splits into separate communicating classes and the chain has no single long-run behaviour. Which absorbing state you end up in depends on where you started.
The interesting question for such chains is therefore not "what is the stationary distribution" but "what is the probability of absorption into each state, given the start" — the absorption probability, computed by solving a linear system over the transient states. Gambler's ruin is the classic example, and the same structure appears in MDPs with terminal states.
That completes the core of the probability module. Next in the curriculum: Statistics and Inference, which uses everything here to reason from samples back to populations.