Markov Chains
A system that leaps from state to state where the next destination only depends on where you are right now, not how you got there.
Why Does This Exist?
Search rankings, queueing, genetics, reinforcement learning, and text generation all ask the same question: if a system hops between states with fixed odds, where does it end up after many hops? You cannot answer by listing paths, because the path count explodes. You need an object that steps a whole distribution forward at once.
That object is the transition matrix, and the chain is the process it drives. Without it you simulate one walk at a time and hope. With it you compute where the mass goes, which states trap it, and whether a long-run balance exists at all.
This page teaches the chain and its balance. The sampling method built on top is Markov chain Monte Carlo. The continuous-time cousin with random arrival times is Poisson processes. The belief-update setting where chains meet data is Bayesian inference.
The one idea you need first
A distribution over states is a list of probabilities that sums to 1, such as meaning 0 percent chance at , 60 at , 40 at . A matrix product of a row distribution by a square matrix returns another row distribution. Chains are repeated applications of that step, which is why matrix multiplication is worth a glance first.
Think of It Like This
Three courtyards with signposted exits
A school has three courtyards: , , and . Every bell, each student follows the signs from wherever they stand. From , six in ten head to and four in ten to . From , four in ten drift back to while six in ten stay, lounging by the fountain. From , half go to and half to .
No one remembers yesterday. Your next courtyard depends only on the one you are in now. That forgetfulness is the Markov property, and it is what makes the maths tractable.
After many bells the crowds settle into a pattern that reproduces itself: the flows out of each courtyard exactly balance the flows in. Students still move every bell, but the headcounts stop changing. That balance is stationarity.
Where it stops: real students get bored and break routine. This model never does. Fixed odds forever is the assumption, and a changed sign breaks the prediction.
How It Actually Works
Label the states , , . The transition matrix collects one-step odds, with meaning the chance of moving from to . In plain words: each row is a full set of exit odds from one state, so each row sums to 1.
The diagram uses:
Rows are , , in order. Check them: , , . The self-loop at is the focal loop in the picture. It holds mass in place each step, which slows mixing and raises the long-run share of .
A distribution steps forward by right multiplication:
Here is the row distribution at time . Stationarity means the distribution reproduces itself, written . That equation is a balance condition: inflows equal outflows at every state.
Worked example
Start everyone at , so . One step picks out row :
Nobody stays at . Sixty percent sit at , forty at .
Step again with . Only and contribute, weighted 0.6 and 0.4:
So after two bells, 44 percent are at and 56 at , none at . empties because nothing points into it from and its own residents all left.
Keep stepping and the distribution settles near . You can verify it: , and , with the third entry . Rows sum to 1 and holds within rounding.
Code
The point is the check: stepping a distribution by reproduces the hand numbers, and the stationary guess reproduces itself.
P = [[0.0, 0.6, 0.4], [0.4, 0.6, 0.0], [0.5, 0.5, 0.0]]
def step(pi: list[float]) -> list[float]: return [pi[0] * P[0][j] + pi[1] * P[1][j] + pi[2] * P[2][j] for j in range(3)]
print(step([1.0, 0.0, 0.0])) # -> [0.0, 0.6, 0.4]print(step([0.0, 0.6, 0.4])) # -> [0.44, 0.56, 0.0]print(step([0.2941, 0.5882, 0.1176])) # -> [0.2941, 0.5882, 0.1176] within roundingThe third line is the stationarity test. If your rows do not sum to 1, that line drifts and the sums leak away from 1, which is how you catch a typo.
Watch Out For
Reading the matrix in the wrong direction
goes from to , rows out and columns in. Transpose it and every flow reverses: your from becomes a column instead of row , and the numbers stop summing correctly after a few steps.
Fix the convention once. Distributions are rows here and step right (). If a library uses columns (), transpose on import and recheck that each column now sums to 1 before trusting anything.
Assuming a stationary balance always exists and is unique
Some chains never settle. A deterministic flip-flop between two states oscillates forever. A chain with two disconnected islands keeps the starting island's mass forever, so the limit depends on where you began.
Before quoting , check irreducibility (you can reach every state from every state, possibly via others) and aperiodicity (no fixed cycle length). The diagram chain satisfies both, which is why its is meaningful. Without them, long-run averages can mislead.
The Quick Version
- A Markov chain moves by fixed odds that depend only on the current state.
- The transition matrix holds those odds with each row summing to 1.
- Distributions step by , so one multiplication moves the whole crowd.
- From , this chain gives then .
- Stationarity means inflows balance outflows; here about .
- The self-loop at holds mass and lifts its long-run share.
- Rows point out, columns point in. Transposing silently reverses every flow.