Markov Chains
The next step depends only on now
A Markov chain is a random process with a short memory: the next state depends on the state you are in now, not on the path that brought you here.
That is the whole idea, and it is already enough to be useful. Tomorrow’s weather, the next square in a board game, and the next letter in a word can all be written this way. We name the places you can be, we say how likely each jump is, and we let time run.
States and transitions
A state is just a label for “where we are”: sunny, cloudy, or rainy; a tile on a board; the letter we have just written.
A transition is a jump from one state to another. Each jump has a transition probability. From sunny weather, perhaps there is a \(0.7\) chance of another sunny day, a \(0.2\) chance of cloud, and a \(0.1\) chance of rain. Those three numbers must add up to \(1\), because something happens tomorrow.
The picture is already the model. You do not need last Tuesday. If it is rainy today, the chance of sun tomorrow is \(0.2\), whether the week began in a heatwave or a storm.
The transition matrix
It is handy to put every probability in a table. Each row is “where we are now”. Each column is “where we might go”. For the weather in the diagram:
\[ P=\begin{pmatrix} 0.7 & 0.2 & 0.1 \\ 0.3 & 0.4 & 0.3 \\ 0.2 & 0.3 & 0.5 \end{pmatrix} \]
The first row is Sunny, the second Cloudy, the third Rainy. Each row sums to \(1\). This table is the transition matrix. If you are in Sunny today, you can read the first row and roll the chances for tomorrow.
Memoryless, in everyday words
The Markov property is the memoryless rule: “what happens next depends only on now.”
A board game is the same shape. You are on a square. You roll the die. The next square depends on this square and the roll, not on how many times you already visited the start. Snakes and ladders is a Markov chain with a slightly colourful transition table.
The same idea shows up in language. If you are writing a word and the current letter is t, a simple model asks only “what usually comes after t?” — perhaps h. It does not look back at the whole sentence. A bigram model, which predicts the next letter or word from the one you just saw, is a Markov chain. That is the plan for the makemore labs on the Deep Learning labs index: count letter pairs, turn the counts into a probability matrix, and sample new names.
A walk through the weather
The cell below starts in the sun and steps through twelve days, using the matrix above. Then it walks for \(20{,}000\) days and reports how often each kind of day appeared. After that it shows \(P\), \(P^2\) and \(P^{20}\). The cell is live Python in the browser; change a probability (keep each row summing to \(1\)) and run it again.
One run with this seed begins Sunny → Sunny → Cloudy → Rainy → Cloudy → Cloudy, then Rainy → Sunny → Cloudy → Rainy → Cloudy → Cloudy.
Over many days the shares are about \(0.449\) sunny, \(0.284\) cloudy, \(0.267\) rainy. They are already close to the numbers in every row of \(P^{20}\).
The long-run mix, in plain words
If you wait long enough, the chain often settles into a stationary distribution: a stable mix of states. Start in a heatwave or start in a storm; after many days, the chance it is sunny today is about the same.
In the matrix, that is what the powers are doing. \(P^2\) is “two days ahead”. \(P^{20}\) is “twenty days ahead”. In this example every row of \(P^{20}\) is about \((0.457,\ 0.283,\ 0.261)\): sunny, cloudy, rainy. The starting day has been forgotten. That row is the stationary distribution.
Not every chain settles as neatly (a chain that just flips back and forth is a usual exception), but the weather here does, and that is the picture to keep.
Summary
| Word | In one line |
|---|---|
| State | A label for where we are now |
| Transition probability | The chance of a particular next jump |
| Transition matrix \(P\) | A table of those chances; each row sums to \(1\) |
| Markov property | The next step depends only on now, not on the path |
| Stationary distribution | The long-run mix the chain forgets its start into |
FAQ
Is this the same as a neural network? No. A Markov chain is a probability table over states. A network can learn a similar table, which is what the planned makemore bigram net does, but the chain itself is just the table and the memoryless rule.
What if I need more memory? Then a single state is no longer “the last letter”. You can pack more history into the state (the last two letters, say), or leave the simple chain behind. The next page up, Markov Decision Processes, adds choices and rewards; it still keeps the “next step depends on now” idea.
Why do the rows of \(P^{20}\) look the same? Because, twenty days out, the starting weather has worn off. Each row is answering “from this start, where are we after twenty days?”, and those answers have met in the middle.
See also
- Probability and statistics essentials
- Maths for machine learning
- Markov Decision Processes — the same memoryless idea, plus actions and rewards
- Deep Learning labs — planned makemore labs; a bigram model is a Markov chain
References
- [1] Andrey Markov’s 1906–1913 work on chained events is the historical source of the name. A clear modern classroom account is in Charles M. Grinstead and J. Laurie Snell, Introduction to Probability, American Mathematical Society, Chapter 11, Dartmouth / AMS open textbook (open textbook).
- [2] Andrej Karpathy, The spelled-out intro to language modeling: building makemore, YouTube. The planned lab split is on the Deep Learning labs index.

