Lesson 14 · Probability and information
Memoryless randomness: Markov chains
The problem
A town has three pizza shops: A, B and C. In the eigenvectors lesson, we tracked the whole town. Each week some share of each shop's customers stayed and some switched.
Now follow one customer. Each week she orders from one shop. You can't say for sure where she'll go next week. But you can say how likely each shop is.
This lesson answers three questions about her:
- Where might she be next week?
- Where might she be in 10 weeks, or 50?
- In the long run, what share of weeks does she spend at each shop?
The tool for this is a Markov chain. Reinforcement learning is built on it, so it comes up again and again.
A Markov chain
Here is her rule. Each week, she picks next week's shop at random. The chances depend on the shop she's at this week:
- At A, she stays with chance 0.8. She moves to B with chance 0.1 and to C with chance 0.1.
- At B, she moves to A with chance 0.2, stays with chance 0.7, and moves to C with chance 0.1.
- At C, she moves to A with chance 0.1, to B with chance 0.3, and stays with chance 0.6.
A process like this is a Markov chain. It has a set of states (the three shops). It moves in steps (weeks). At each step it picks the next state at random, using chances set by the current state.
Only now matters
Look at what the rule leaves out. It doesn't care how long she's been at B, or where she was before that. Only this week's shop matters.
Predict first
Two customers are at B this week. One has been at B for a year. The other came from C last week. Who is more likely to be at A next week?
This "only now matters" rule is the Markov property. In symbols, with for the shop in week :
Read it as: once you know this week's shop , knowing the earlier weeks doesn't change the chance of shop next week. The bar means "given", as in conditional probability.
Memory can live in the state
The Markov property doesn't stop a model from remembering things. If last week matters, put it in the state. For example, a state could be "(this week's shop, last week's shop)". Then the chain is Markov again. RL uses this trick all the time: the state should hold everything that matters for what happens next.
The transition matrix
Put the nine chances in a table. Rows are this week's shop. Columns are next week's shop. This is the transition matrix :
It's the same matrix as in the eigenvectors lesson. There, row B said "70% of B's customers stay". Here it says "a customer at B stays with chance 0.7". Same numbers, read for one person instead of a crowd.
Pick a shop below. Its arrows light up in the diagram, and its row lights up in the table. Each arrow is one entry of , and thicker arrows are more likely moves.
| this week ↓ next → | A | B | C |
|---|---|---|---|
| A | 0.8 | 0.1 | 0.1 |
| B | 0.2 | 0.7 | 0.1 |
| C | 0.1 | 0.3 | 0.6 |
From B, next week: A 0.2 · B 0.7 · C 0.1
Row B adds up to 0.2 + 0.7 + 0.1 = 1
Two things are true of every transition matrix:
- Every entry is between 0 and 1, because it's a probability.
- Every row adds up to 1, because she has to go somewhere. Each row is a distribution over next week's shop.
Each entry is a conditional probability: . Columns don't need to add up to 1. Column B adds up to .
Following one customer
Start her at B and roll the dice each week. The figure draws her path one week at a time. The green ring shows where she is, and the green arrow is her latest move.
The bars count the share of weeks she has spent at each shop so far. The dashed lines mark (0.45, 0.35, 0.20). Press play, or drag the scrubber to week 400.
Week 0: at B
Recent weeks: B
Share of weeks at each shop (weeks 0 to 0)
- A0.00
- B1.00
- C0.00
Dashed lines: d = (0.45, 0.35, 0.20)
Notice three things:
- She tends to stay put for a few weeks at a time, because the "stay" chances are large.
- In the first 20 weeks, the shares can be far from the dashed lines.
- By week 400, they're close. Every new customer gets a different path, but the shares end up near (0.45, 0.35, 0.20).
Why those numbers? To answer that, stop following single paths and track the chances instead.
Pushing probabilities forward
Say she's at B in week 0. We can write that as a distribution over (A, B, C):
Next week's chances are row B of : . What about week 2? She could be at any shop in week 1, so we add up every route.
Worked example
Her chance of being at A in week 2, starting from B
Route through A: she's at A in week 1 with chance 0.2, then stays with chance 0.8. That's .
Do the same for B and C and you get . It still adds up to 1.
Look at the sum for A: this week's chances, times column A of , added up. That's a matrix–vector product, with the distribution as a row vector on the left:
Read it as: to get the chance of shop next week, go over every shop . Multiply "chance she's at now" by "chance leads to ", then add.
Apply it times and you get , using the matrix power .
Row vectors or column vectors?
This book keeps as a row vector, with on the right. That matches the table: rows are "from", columns are "to". The eigenvectors lesson used the same row form, then transposed it to with as a column. Both give the same numbers. Some books write transition matrices the other way round, with columns as "from", so always check which convention you're reading.
Step the figure forward a week at a time. The shop circles show this week's chances, and the plot traces them over 30 weeks. The readout shows the sum for shop A.
Week 0: d = (0.00, 1.00, 0.00)
Next week at A: 0.00 × 0.8 + 1.00 × 0.2 + 0.00 × 0.1 = 0.20
Largest gap from (0.45, 0.35, 0.20): 0.6500
Predict first
She starts at shop A. After 30 weeks, does knowing she started at A help you guess where she is now?
Where it settles: the stationary distribution
From every start, the chances settle at . Once there, they stay there. Check it:
Worked example
Check that d · T = d
A: .
A distribution that one more step leaves unchanged is a stationary distribution:
"Stationary" doesn't mean she stops moving. She still switches shops every few weeks. It means the chances stop changing.
This is the equation from the eigenvectors lesson. is an eigenvector of with eigenvalue 1. Pushing a distribution forward week after week is power iteration, which is why it lands on that eigenvector.
Solving for d directly
You can also solve for by hand. Write out one shop at a time, and move everything to the left:
For example, line A starts as , then moves to the left.
Add the three lines together. Each column adds up to 0, so you get . One equation is the other two in disguise. So these equations can't pin down : any multiple of , like , solves them too.
The missing fact is that the chances add up to 1. So replace line C with it:
Now there are three different equations for three unknowns: a linear system with one answer, . Pushing forward week after week needs no algebra and works for huge chains. Solving gives the exact answer in one go for small ones.
Three views, one answer
The same numbers (0.45, 0.35, 0.20) now answer three different questions:
| Question | Where we saw it |
|---|---|
| What shares of a whole town end up at each shop? | Eigenvectors lesson: the crowd |
| What's the chance one customer is at each shop, far in the future? | for large |
| What share of her weeks does one customer spend at each shop? | The walk figure's bars |
The first two match because the crowd is many customers, each following the same rule. The third matches because one customer, over many weeks, samples the chain again and again. It's like the law of large numbers, except that the weeks aren't independent. Her weeks are linked, since she tends to stay put, so the shares settle more slowly than independent samples would.
Why the start is forgotten
The eigenvectors lesson split the start "certainly at B" into three parts, one per eigenvector of :
Each week multiplies each part by its eigenvalue: 1, 0.6 and 0.5. So after weeks:
The first part never changes. The other two fade away. Switch the figure above to "Fading parts" to watch them shrink.
The 0.6 part shrinks slowest, so each week the gap shrinks by about 0.6. Starting from B, by week 10 every chance is within 0.002 of . By week 30, the gap is under one ten-millionth.
Mixing: how fast the start is forgotten
How quickly a chain forgets its start is called mixing. A chain that forgets fast "mixes fast".
What sets the speed? Say every shop gets stickier. With extra chance , she stays put. Otherwise, she moves as before. The new matrix is:
Here is the identity matrix, which means "stay where you are". For , row A becomes .
Predict first
Make every shop stickier with s = 0.5. What happens to the long-run shares (0.45, 0.35, 0.20)?
Move the slider and watch the chance of being at B, starting from B. The dashed grey line is the original chain.
With s = 0.50: within 0.01 of d after 17 weeks
Dashed grey: the original chain (s = 0), 8 weeks
The gap shrinks by a factor of 0.80 a week (= s + (1 − s) × 0.6). The long-run shares stay (0.45, 0.35, 0.20).
Adding stickiness turns each eigenvalue into . The eigenvalue 1 stays 1, so stays put. The 0.6 moves towards 1, so the gap shrinks more slowly:
| Stickiness | Gap multiplied by, each week | Weeks until within 0.01 (from B) |
|---|---|---|
| 0 | 0.6 | 8 |
| 0.5 | 0.8 | 17 |
| 0.8 | 0.92 | 45 |
| 0.9 | 0.96 | 91 |
So the second-largest eigenvalue sets the mixing speed. The closer it is to 1, the longer the chain remembers its start.
This matters in RL. An agent learns from its own experience, which comes from a Markov chain. If that chain mixes slowly, the agent spends a long time near where it started. Its experience then gives a lopsided picture of the world.
Markov chains come back in reinforcement learning. A Markov decision process is a Markov chain in which an agent chooses actions that change the transition chances. Once the agent fixes a policy, it's an ordinary Markov chain again, with its own stationary distribution.
DeeperWhen the long-run answer isn't uniqueTwo small chains that never settle on one d: one gets stuck, one goes round in circles. · 4 min
The pizza chain settles on one from every start. That needs two things, and small chains can break either one.
A chain that gets stuck. Say shops A and B form one group and C is on its own:
Start at A and she never reaches C. Start at C and she never leaves. Both and satisfy , so the long-run answer depends on the start. The fix is to make sure every state can reach every other state, maybe in several steps. Such a chain is called irreducible.
A chain that cycles. Now say she always moves one step round: A to B, B to C, C to A.
Start at A and the chances go forever. They never settle.
There is a stationary distribution, , and she does spend a third of her weeks at each shop. But never gets there. The other two eigenvalues of this have size 1, so those parts never shrink. Any chance of staying put breaks the cycle. Such a chain is called aperiodic.
No arrow leaves the A–B pair, and C only loops back to itself.
A chain that's both irreducible and aperiodic, with finitely many states, always settles on one from every start. The pizza chain has both: every shop can reach every other, and every shop has a chance of staying.
DigressionText as a Markov chainMake the states words, and a Markov chain writes (bad) sentences. · 2 min
Let the states be words. From a pile of text, count how often each word follows each other word. Turn the counts into chances, and you have a transition matrix. Row "pizza" might say "is" with chance 0.3, "with" with chance 0.2, and so on.
Now start at a word and walk the chain. You get text in which every pair of neighbouring words looks plausible. But the whole sentence wanders, because each word only "remembers" the word before it.
That's a tiny language model. Modern language models still pick the next word from a distribution. But they let the choice depend on thousands of earlier words, not one.
Common misconceptions
"Rows or columns?" In this book, row of is "from state ", so rows add up to 1. The distribution multiplies from the left: . If you see columns adding up to 1, that book uses the transposed convention and writes .
"Markov means the process has no memory." It means the next step depends only on the current state. You can put as much memory as you like into the state.
"Stationary means she stops moving." She keeps switching shops forever. Only the chances stop changing.
"The visit shares should match d after a few weeks." One customer's shares settle slowly, because her weeks are linked. The chances settle much faster than one customer's visit counts.
Recap
- A Markov chain moves between states at random. The next state depends only on the current one (the Markov property).
- The transition matrix holds the chance of each move. Row is the distribution of the next state, given state , so each row adds up to 1.
- One step pushes a distribution forward: , and steps give .
- The stationary distribution solves . It's an eigenvector of with eigenvalue 1, and the long-run share of time in each state.
- The second-largest eigenvalue sets how fast the start is forgotten (mixing). Stickier chains mix more slowly.
Section 2 shows how to compute these in PyTorch.