Skip to content
AI360XpertBeta

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.

Three states linked by transition probabilities whose rows sum to 1 settle to stationary pi equals pi P, with the self-loop holding mass in place
Three states linked by transition probabilities whose rows sum to 1 settle to stationary pi equals pi P, with the self-loop holding mass in place

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 [0,0.6,0.4][0, 0.6, 0.4] meaning 0 percent chance at AA, 60 at BB, 40 at CC. 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: AA, BB, and CC. Every bell, each student follows the signs from wherever they stand. From AA, six in ten head to BB and four in ten to CC. From BB, four in ten drift back to AA while six in ten stay, lounging by the fountain. From CC, half go to AA and half to BB.

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 AA, BB, CC. The transition matrix PP collects one-step odds, with PijP_{ij} meaning the chance of moving from ii to jj. In plain words: each row is a full set of exit odds from one state, so each row sums to 1.

The diagram uses:

P=[00.60.40.40.600.50.50]P = \begin{bmatrix} 0 & 0.6 & 0.4 \\ 0.4 & 0.6 & 0 \\ 0.5 & 0.5 & 0 \end{bmatrix}

Rows are AA, BB, CC in order. Check them: 0+0.6+0.4=10 + 0.6 + 0.4 = 1, 0.4+0.6+0=10.4 + 0.6 + 0 = 1, 0.5+0.5+0=10.5 + 0.5 + 0 = 1. The 0.60.6 self-loop at BB is the focal loop in the picture. It holds mass in place each step, which slows mixing and raises the long-run share of BB.

A distribution πt\pi_t steps forward by right multiplication:

πt+1=πtP\pi_{t+1} = \pi_t P

Here πt\pi_t is the row distribution at time tt. Stationarity means the distribution reproduces itself, written π=πP\pi = \pi P. That equation is a balance condition: inflows equal outflows at every state.

Worked example

Start everyone at AA, so π0=[1,0,0]\pi_0 = [1, 0, 0]. One step picks out row AA:

π1=[0,0.6,0.4]\pi_1 = [0, 0.6, 0.4]

Nobody stays at AA. Sixty percent sit at BB, forty at CC.

Step again with π1\pi_1. Only BB and CC contribute, weighted 0.6 and 0.4:

π2=0.6×[0.4,0.6,0]+0.4×[0.5,0.5,0]=[0.24+0.20,0.36+0.20,0]=[0.44,0.56,0]\pi_2 = 0.6 \times [0.4, 0.6, 0] + 0.4 \times [0.5, 0.5, 0] = [0.24 + 0.20, 0.36 + 0.20, 0] = [0.44, 0.56, 0]

So after two bells, 44 percent are at AA and 56 at BB, none at CC. CC empties because nothing points into it from BB and its own residents all left.

Keep stepping and the distribution settles near π≈[0.294,0.588,0.118]\pi \approx [0.294, 0.588, 0.118]. You can verify it: 0.4×0.588+0.5×0.118=0.2940.4 \times 0.588 + 0.5 \times 0.118 = 0.294, and 0.6×0.294+0.6×0.588+0.5×0.118=0.5880.6 \times 0.294 + 0.6 \times 0.588 + 0.5 \times 0.118 = 0.588, with the third entry 0.4×0.294=0.1180.4 \times 0.294 = 0.118. Rows sum to 1 and πP=π\pi P = \pi holds within rounding.

Code

The point is the check: stepping a distribution by PP 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 rounding

The third line is the stationarity test. If your PP 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

PijP_{ij} goes from ii to jj, rows out and columns in. Transpose it and every flow reverses: your π1\pi_1 from AA becomes a column instead of row AA, and the numbers stop summing correctly after a few steps.

Fix the convention once. Distributions are rows here and step right (πP\pi P). If a library uses columns (PπP \pi), 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 π\pi, 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 π\pi 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 PP holds those odds with each row summing to 1.
  • Distributions step by πt+1=πtP\pi_{t+1} = \pi_t P, so one multiplication moves the whole crowd.
  • From [1,0,0][1, 0, 0], this chain gives [0,0.6,0.4][0, 0.6, 0.4] then [0.44,0.56,0][0.44, 0.56, 0].
  • Stationarity π=πP\pi = \pi P means inflows balance outflows; here about [0.294,0.588,0.118][0.294, 0.588, 0.118].
  • The self-loop at BB holds mass and lifts its long-run share.
  • Rows point out, columns point in. Transposing silently reverses every flow.