Tomorrow’s weather depends only on today’s. Watch a token hop through 3000 days, and the share of days of each kind settle.
Where a random walk settles
Weather that hops between three states, 3000 days (seed 5200).
Today is sunny. Each day the weather hops by the chances on the arrows; the bars count the share of days in each state.
Predict first. If the walk started on a rainy day, would the share of rainy days after 3000 days be higher, the same, or lower than from a sunny start?
Describe this picture
Three states, sunny, cloudy and rainy, drawn as circles with an arrow each way between every two, labelled with the chance of that hop (the matrix below), and the chance of staying written by each circle. A token sits on today’s state. Beside or below them, three bars, “share of days”, show the share of days spent so far in each state, with dotted marks at 0.45, 0.35 and 0.20. Before the clip a question asks: if the walk started on a rainy day, would the share of rainy days after 3000 days be higher, the same, or lower than from a sunny start? Three small bar drawings answer it, and a tap picks one and starts the clip. The walk starts on a sunny day (seed 5200): the first 30 days hop one by one, then the rest pour in. The bars swing at first, then settle near the dotted marks: 0.437, 0.352 and 0.211 after 3000 days; your guess is drawn as a dotted outline on the rainy bar. Readouts give the day and the three shares. After the clip three buttons, “Start on a day that is” sunny, cloudy or rainy, redo the 3000 days from that start with the same random hops.
Long-run shares
Before the clip you guessed. The start does not matter: from a rainy day or a sunny one, the walk ends up spending about 45 % of its days sunny, 35 % cloudy and 20 % rainy. Choose a different start after the clip and watch the bars go to the same places: from a sunny start the clip’s 3000 days came out 0.437, 0.352 and 0.211, and from a rainy start 0.437, 0.352 and 0.212.
The walk forgets because of how it is built. Each day it hops to tomorrow’s state with chances that depend on today’s state alone, not on any earlier day. A sequence of random states with that property is a Markov chain. I write for the state on day .
Notice that “settled” does not mean the token stops. It keeps hopping every day. What settles is the share of time it spends in each state.
The table of hops
The chances of each hop fit in one table, the transition matrix:
Rows and columns are in the order sunny, cloudy, rainy. Column lists where a day of type goes next, so each column adds up to 1. The entry is the chance of going from state to state :
After a sunny day: sunny again with chance 0.8, cloudy 0.1, rainy 0.1. Many probability books write the same table turned on its side, with rows adding to 1; this site uses columns, as the linear algebra course does.
Each column is one level of a probability tree from Conditional probability. The chance of rain two days after a sunny day adds the three paths through tomorrow:
Many steps: matrix powers
Keep the chances of each state on day in a vector . Adding over the paths, as above, is exactly multiplying by the matrix:
Start on a sunny day, :
| Day | Sunny | Cloudy | Rainy |
|---|---|---|---|
| 0 | 1 | 0 | 0 |
| 1 | 0.8 | 0.1 | 0.1 |
| 2 | 0.67 | 0.18 | 0.15 |
| 3 | 0.587 | 0.238 | 0.175 |
| 10 | 0.4543 | 0.3459 | 0.1998 |
By day 10 the chances are within 0.005 of .
The steady state
The shares the walk settles on are a vector that one more day does not change:
Solve it from the rainy row: gives , and with the three adding to 1, . The sunny row, , gives , so and . This is the steady state, . These long-run shares are also called the chain’s stationary distribution.
In the language of linear algebra, is an eigenvector of for the eigenvalue 1. The matrix’s other eigenvalues, 0.6 and 0.5, set how fast the start is forgotten: the gap from the steady state shrinks by a factor of about 0.6 a day, so after 10 days it is about of what it was.
The steady state also says how often the walk comes back. If 20 % of days are rainy, rainy days come on average every days. For a chain like this one, where every state can be reached from every other, the mean time between visits to state is : days for sunny, for cloudy, 5 for rainy.
A chain does not always settle. The table swaps two states every day. It has a steady state, , but a walk started in one state alternates for ever and its chances never approach it.
States you never leave
Some chains have absorbing states: once entered, never left. A gambler with 2 coins bets 1 coin at a time on a fair flip, and stops on reaching 4 coins or 0. The states are 0 to 4 coins; 0 and 4 absorb.
From 2 coins, reaching 4 has chance , by symmetry. From 1 coin it is , and from 3 it is . In a fair game, the chance of reaching the goal is the fraction of the way there you start.
How many bets does the game last? Let be the hops among the states that do not absorb, 1, 2 and 3. The matrix counts the expected visits to each state before the game ends:
Each column belongs to one starting state, and its sum is the expected number of bets: 3 from 1 coin, 4 from 2 coins, 3 from 3 coins.
With a coin that the gambler wins only 40 % of the time, the chance of reaching 4 from 2 coins falls to ; from 1 coin it is , and from 3 coins . A small edge against you adds up over many bets.
Worked example
1. The weather from a sunny start. , , , and by day 10 . The steady state is . Sunny days return every days on average, cloudy every , rainy every 5.
2. The fair gambler. From 2 coins: reaches 4 with chance after 4 bets on average. From 3 coins: chance , 3 bets on average.
3. The unfair gambler. With a 40 % coin, from 2 coins: reaches 4 with chance .
Try it yourself. Which add up to 1 in this site’s tables, rows or columns? Columns. In the long run, how many days pass between rainy days on average? Five.
Where you’ll meet this
Weather and queues. Simple weather generators and models of queues, where the state is the number of people waiting, are Markov chains.
Text and speech. Predicting the next word from the last few is a Markov chain over words. Speech recognisers used hidden Markov models for decades: a chain of sounds that is seen only through noisy measurements.
Ranking web pages. A random surfer clicking links is a Markov chain over pages, and the steady state ranks the pages: a page visited often in the long run is important.
Simulation. Many computer methods draw samples from a complicated distribution by running a Markov chain whose steady state is that distribution.
Where this leads · eigenvectors
The steady state is an eigenvector for the eigenvalue 1, and the second-largest eigenvalue sets how fast the start is forgotten. Markov matrices and Diagonalisation and matrix powers in the linear algebra course explain why.
Reference card
| Quantity | Formula | Notes |
|---|---|---|
| Markov property | only today matters | |
| Transition matrix | , columns add to 1 | column : where goes |
| steps | paths added by matrix products | |
| Steady state | , entries add to 1 | eigenvector for 1 |
| Mean return time | rainy: 5 days | |
| Absorbing chain | column sums: expected steps | |
| Fair ruin | reach the goal with chance start/goal |