Skip to content

Markov chains

A chain that forgets its past is described by one table of hop chances; its long-run share of time in each state ignores the start.

Helps to know firstConditional probability

Helps to know first
This topic

First, the picture

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.

day
1
sunny
1.000
cloudy
0.000
rainy
0.000

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?

0.00 / 12.50 s
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 XkX_k for the state on day kk.

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:

M=[0.80.20.10.10.70.30.10.10.6].\mathbf{M}=\begin{bmatrix}0.8&0.2&0.1\\0.1&0.7&0.3\\0.1&0.1&0.6\end{bmatrix}.

Rows and columns are in the order sunny, cloudy, rainy. Column qq lists where a day of type qq goes next, so each column adds up to 1. The entry mpqm_{pq} is the chance of going from state qq to state pp:

Pr⁡{Xk+1=p∣Xk=q, and any earlier days}=mpq.\Pr\{X_{k+1}=p\mid X_k=q,\ \text{and any earlier days}\}=m_{pq}.

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:

0.8×0.1+0.1×0.1+0.1×0.6=0.15.0.8\times0.1+0.1\times0.1+0.1\times0.6=0.15.

Many steps: matrix powers

Keep the chances of each state on day kk in a vector uk\mathbf{u}_k. Adding over the paths, as above, is exactly multiplying by the matrix:

uk+1=M uk,uk=Mk u0.\mathbf{u}_{k+1}=\mathbf{M}\,\mathbf{u}_k,\qquad \mathbf{u}_k=\mathbf{M}^k\,\mathbf{u}_0.

Start on a sunny day, u0=(1,0,0)\mathbf{u}_0=(1,0,0):

DaySunnyCloudyRainy
0100
10.80.10.1
20.670.180.15
30.5870.2380.175
100.45430.34590.1998

By day 10 the chances are within 0.005 of (0.45,0.35,0.20)(0.45,0.35,0.20).

The steady state

The shares the walk settles on are a vector s\mathbf{s} that one more day does not change:

M s=s,s1+s2+s3=1.\mathbf{M}\,\mathbf{s}=\mathbf{s},\qquad s_1+s_2+s_3=1.

Solve it from the rainy row: 0.1s1+0.1s2+0.6s3=s30.1s_1+0.1s_2+0.6s_3=s_3 gives s1+s2=4s3s_1+s_2=4s_3, and with the three adding to 1, s3=0.2s_3=0.2. The sunny row, 0.8s1+0.2s2+0.1s3=s10.8s_1+0.2s_2+0.1s_3=s_1, gives s1−s2=0.1s_1-s_2=0.1, so s1=0.45s_1=0.45 and s2=0.35s_2=0.35. This is the steady state, s=(0.45,0.35,0.20)\mathbf{s}=(0.45,0.35,0.20). These long-run shares are also called the chain’s stationary distribution.

In the language of linear algebra, s\mathbf{s} is an eigenvector of M\mathbf{M} 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 0.610=0.0060.6^{10}=0.006 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 1/0.2=51/0.2=5 days. For a chain like this one, where every state can be reached from every other, the mean time between visits to state pp is 1/sp1/s_p: 2.222.22 days for sunny, 2.862.86 for cloudy, 5 for rainy.

A chain does not always settle. The table [0110]\begin{bmatrix}0&1\\1&0\end{bmatrix} swaps two states every day. It has a steady state, (12,12)(\tfrac12,\tfrac12), 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 0.50.5, by symmetry. From 1 coin it is 0.250.25, and from 3 it is 0.750.75. 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 Q\mathbf{Q} be the hops among the states that do not absorb, 1, 2 and 3. The matrix (I−Q)−1(\mathbf{I}-\mathbf{Q})^{-1} counts the expected visits to each state before the game ends:

(I−Q)−1=[1.510.51210.511.5].(\mathbf{I}-\mathbf{Q})^{-1}=\begin{bmatrix}1.5&1&0.5\\1&2&1\\0.5&1&1.5\end{bmatrix}.

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 0.30770.3077; from 1 coin it is 0.12310.1231, and from 3 coins 0.58460.5846. A small edge against you adds up over many bets.

Worked example

1. The weather from a sunny start. u1=(0.8,0.1,0.1)\mathbf{u}_1=(0.8,0.1,0.1), u2=(0.67,0.18,0.15)\mathbf{u}_2=(0.67,0.18,0.15), u3=(0.587,0.238,0.175)\mathbf{u}_3=(0.587,0.238,0.175), and by day 10 (0.4543,0.3459,0.1998)(0.4543,0.3459,0.1998). The steady state (0.45,0.35,0.20)(0.45,0.35,0.20) is (9,7,4)/20(9,7,4)/20. Sunny days return every 2.222.22 days on average, cloudy every 2.862.86, rainy every 5.

2. The fair gambler. From 2 coins: reaches 4 with chance 0.50.5 after 4 bets on average. From 3 coins: chance 0.750.75, 3 bets on average.

3. The unfair gambler. With a 40 % coin, from 2 coins: reaches 4 with chance 0.30770.3077.

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

QuantityFormulaNotes
Markov propertyPr⁡{Xk+1=p∣Xk=q,… }=mpq\Pr\{X_{k+1}=p\mid X_k=q,\dots\}=m_{pq}only today matters
Transition matrixM\mathbf{M}, columns add to 1column qq: where qq goes
kk stepsuk=Mku0\mathbf{u}_k=\mathbf{M}^k\mathbf{u}_0paths added by matrix products
Steady stateMs=s\mathbf{M}\mathbf{s}=\mathbf{s}, entries add to 1eigenvector for 1
Mean return time1/sp1/s_prainy: 5 days
Absorbing chain(I−Q)−1(\mathbf{I}-\mathbf{Q})^{-1}column sums: expected steps
Fair ruinreach the goal with chance start/goal

Keep reading with a free account.

By creating an account you accept the terms of use.

Sign in

Already have an account? Use the service you signed up with.

Or keep reading: get new lessons by email

Are you 13 or over?

Accounts and email updates are for people aged 13 or over. We keep only your answer, never your date of birth.

End of the lesson

Where to go next.

Phasorium

Account

Sign inKeep your place on any device

Subjects

Site

Look