Markov Chains

A Markov chain is a process that moves between a fixed set of states, where the chance of moving to each state next depends only on the current state. A transition matrix holds these chances, and multiplying the current distribution by the matrix predicts the next one.

Before this lesson: Matrix Multiplication, Probability Rules and Conditional Probability

The idea

Picture a town with two coffee shops, Brew and Daily. Each week, every customer buys coffee at one of them. Some stay loyal and some switch. If you know where people buy this week and how often they switch, you can predict next week.

The shops are the states. Each week is a step. The key assumption is that where a customer goes next week depends only on where they went this week, not on earlier weeks. A process with this “no memory” property is a Markov chain.

Transition diagram with two states, Brew and Daily. An arrow loops from Brew back to Brew labeled 0.8, an arrow goes from Brew to Daily labeled 0.2, an arrow goes from Daily to Brew labeled 0.3, and an arrow loops from Daily back to Daily labeled 0.7.
The arrows leaving each state add to 1: 0.8 + 0.2 for Brew and 0.3 + 0.7 for Daily.

Transition matrix and state vectors

The transition matrix P has one row and one column for each state. The entry in row i, column j is the probability of moving from state i to state j in one step. Each entry is between 0 and 1, and each row adds to 1, because a customer in state i has to go somewhere.

P = \begin{bmatrix} p_{11} & p_{12} \\ p_{21} & p_{22} \end{bmatrix} \quad \text{(row = from, column = to)}

A state vector (or distribution vector) is a row vector that gives the probability of being in each state at one step. Its entries are nonnegative and add to 1. The starting vector is called X_0.

X_{n+1} = X_n P \qquad X_n = X_0 P^n

The entry in row i, column j of P^n is the probability of moving from state i to state j in exactly n steps. This course writes state vectors as rows, so the vector goes on the left of P.

Markov chain examples

Example 1: Writing a transition matrix

Each week, 80% of Brew customers return to Brew and the rest switch to Daily. Of Daily customers, 30% switch to Brew and the rest stay. Write the transition matrix with the states in the order Brew (B), Daily (D).

p_{BB} = 0.8

80% of Brew customers stay at Brew.

p_{BD} = 1 - 0.8 = 0.2

The rest of row B switch to Daily, since the row adds to 1.

p_{DB} = 0.3

30% of Daily customers switch to Brew.

p_{DD} = 1 - 0.3 = 0.7

The rest of row D stay at Daily.

P = \begin{bmatrix} 0.8 & 0.2 \\ 0.3 & 0.7 \end{bmatrix}

Put the “from” states on the rows and the “to” states on the columns.

Answer: P has rows (0.8, 0.2) and (0.3, 0.7). Each row adds to 1, so P is a valid transition matrix. The diagram above shows the same numbers.

Example 2: The distribution after one step

This week, half of the customers buy at Brew and half at Daily, so X_0 = \begin{bmatrix} 0.5 & 0.5 \end{bmatrix}. Use P from Example 1 to find next week’s distribution.

X_1 = X_0 P

The next state vector is the current one times P.

X_1 = \begin{bmatrix} 0.5 & 0.5 \end{bmatrix} \begin{bmatrix} 0.8 & 0.2 \\ 0.3 & 0.7 \end{bmatrix}

Substitute X_0 and P.

\text{Brew} = 0.5(0.8) + 0.5(0.3)

Multiply the row vector by column 1 (see matrix multiplication).

= 0.40 + 0.15

Multiply.

= 0.55

Add.

\text{Daily} = 0.5(0.2) + 0.5(0.7)

Multiply the row vector by column 2.

= 0.10 + 0.35

Multiply.

= 0.45

Add.

X_1 = \begin{bmatrix} 0.55 & 0.45 \end{bmatrix}

Write the new state vector.

0.55 + 0.45 = 1

Check: the entries add to 1.

Answer: next week, 55% of customers buy at Brew and 45% at Daily.

Example 3: Two steps with the matrix squared

A customer buys at Brew this week. What is the probability that they buy at Brew two weeks from now? Use P from Example 1.

P^2 = P \cdot P

Two steps use the matrix squared.

p^{(2)}_{BB} = 0.8(0.8) + 0.2(0.3)

Row B of P times column B of P.

= 0.64 + 0.06

Multiply.

= 0.70

Add.

p^{(2)}_{BD} = 0.8(0.2) + 0.2(0.7)

Row B times column D.

= 0.16 + 0.14

Multiply.

= 0.30

Add.

p^{(2)}_{DB} = 0.3(0.8) + 0.7(0.3)

Row D times column B.

= 0.24 + 0.21

Multiply.

= 0.45

Add.

p^{(2)}_{DD} = 0.3(0.2) + 0.7(0.7)

Row D times column D.

= 0.06 + 0.49

Multiply.

= 0.55

Add.

P^2 = \begin{bmatrix} 0.70 & 0.30 \\ 0.45 & 0.55 \end{bmatrix}

Write the two-step matrix.

The tree below shows where 0.70 comes from. A Brew customer can stay at Brew both weeks, or switch to Daily and then come back. Multiply along each branch and add the branches that end at Brew (see conditional probability).

Tree diagram starting at Brew. After week 1 the branches are Brew with probability 0.8 and Daily with probability 0.2. After week 2, from Brew: Brew 0.8 and Daily 0.2; from Daily: Brew 0.3 and Daily 0.7. The branch products are 0.64, 0.16, 0.06 and 0.14. The two branches that end at Brew, 0.64 and 0.06, are drawn in thick blue and add to 0.70.
The two paths that end at Brew add to 0.64 + 0.06 = 0.70, the row B, column B entry of P squared.

Answer: 0.70. The same number is the row B, column B entry of P^2.

Example 4: A three-state weather model

A town’s weather each day is sunny (S), cloudy (C) or rainy (R). Today is cloudy. Find the probabilities for the day after tomorrow.

P = \begin{bmatrix} 0.6 & 0.3 & 0.1 \\ 0.4 & 0.4 & 0.2 \\ 0.2 & 0.4 & 0.4 \end{bmatrix} \quad \text{(order S, C, R)}
X_0 = \begin{bmatrix} 0 & 1 & 0 \end{bmatrix}

Today is cloudy for certain.

X_1 = \begin{bmatrix} 0.4 & 0.4 & 0.2 \end{bmatrix}

Multiplying by P picks out row C.

\text{S} = 0.4(0.6) + 0.4(0.4) + 0.2(0.2)

Multiply X_1 by column S.

= 0.24 + 0.16 + 0.04

Multiply.

= 0.44

Add.

\text{C} = 0.4(0.3) + 0.4(0.4) + 0.2(0.4)

Multiply X_1 by column C.

= 0.12 + 0.16 + 0.08

Multiply.

= 0.36

Add.

\text{R} = 0.4(0.1) + 0.4(0.2) + 0.2(0.4)

Multiply X_1 by column R.

= 0.04 + 0.08 + 0.08

Multiply.

= 0.20

Add.

X_2 = \begin{bmatrix} 0.44 & 0.36 & 0.20 \end{bmatrix}

Write the state vector.

0.44 + 0.36 + 0.20 = 1

Check: the entries add to 1.

Answer: the day after tomorrow is sunny with probability 0.44, cloudy with probability 0.36 and rainy with probability 0.20.

Example 5: Streaming subscribers (word problem)

A streaming service has 1,000 subscribers. Each month, 90% of subscribers keep the service and 10% cancel. Of people who have cancelled, 15% sign up again each month. How many are subscribed after two months?

P = \begin{bmatrix} 0.9 & 0.1 \\ 0.15 & 0.85 \end{bmatrix}

States in the order Subscribed, Cancelled; 1 − 0.15 = 0.85 stay cancelled.

N_0 = \begin{bmatrix} 1000 & 0 \end{bmatrix}

Use counts instead of probabilities; the rule is the same.

N_1 = \begin{bmatrix} 900 & 100 \end{bmatrix}

Multiply by P: 1000(0.9) = 900 stay and 1000(0.1) = 100 cancel.

\text{Subscribed} = 900(0.9) + 100(0.15)

Multiply N_1 by column 1.

= 810 + 15

Multiply.

= 825

Add.

\text{Cancelled} = 900(0.1) + 100(0.85)

Multiply N_1 by column 2.

= 90 + 85

Multiply.

= 175

Add.

825 + 175 = 1000

Check: the total is still 1,000 people.

Answer: after two months, about 825 people are subscribed and 175 have cancelled.

Common mistakes

MistakeExample of the errorFix
Putting the “from” states on the columnsWriting column B as (0.8, 0.2) and multiplying X_0 PWith row vectors, row i lists the chances of leaving state i. Each row must add to 1.
Rows that don’t add to 1A row (0.8, 0.3)The chances out of one state cover every outcome, so each row adds to exactly 1.
Multiplying in the wrong orderComputing P X_0 with a row vector X_0A row vector goes on the left: X_1 = X_0 P.
Squaring each entry to get two stepsUsing 0.8^2 = 0.64 as the two-step chance of B to BMultiply the matrices: P^2 gives 0.64 + 0.06 = 0.70.
Adding entries across a columnExpecting column B, 0.8 + 0.3, to equal 1Only the rows of a transition matrix add to 1. Columns can add to anything from 0 to the number of states.
Thinking the next state depends on the whole historyUsing last month and this month to predict next monthIn a Markov chain, only the current state matters.

Related ideas

Some chains have states that can never be left once entered; absorbing Markov chains finds where such a chain ends up and how many steps that takes on average. Multiplying a state vector by the transition matrix again and again is also the idea behind the power method in Numerical Methods.

Long-run behavior

Keep multiplying the coffee-shop vector by P and it settles near 60% Brew and 40% Daily, whatever the starting split. That limit is the steady-state vector, and finding it is the topic of the next lesson, Regular Markov Chains.

Connection to linear algebra

A steady-state vector X satisfies X P = X, so it is an eigenvector (for eigenvalue 1) of the transpose of P. See eigenvalues and eigenvectors and diagonalization, which gives a fast way to find large powers of P.

Other matrix models

The Leontief input-output model also uses a matrix to describe how parts of a system feed each other. You can also estimate Markov chain probabilities with probability simulations.

Practice problems

  1. Is \begin{bmatrix} 0.5 & 0.5 \\ 0.6 & 0.3 \end{bmatrix} a transition matrix? Explain.
  2. At a gym, 70% of members who work out this week also work out next week. Of members who skip this week, 40% work out next week. Write the transition matrix with states in the order Workout, Skip.
  3. Use the matrix from Problem 2 with X_0 = \begin{bmatrix} 0.6 & 0.4 \end{bmatrix}. Find X_1.
  4. Find P^2 for P = \begin{bmatrix} 0.9 & 0.1 \\ 0.5 & 0.5 \end{bmatrix}.
  5. In Problem 4, what is the probability of moving from state 2 to state 1 in two steps?
  6. Let P = \begin{bmatrix} 0.5 & 0.5 \\ 0.2 & 0.8 \end{bmatrix} and X_0 = \begin{bmatrix} 1 & 0 \end{bmatrix}. Find X_2.
  7. Find a and b so that \begin{bmatrix} 0.35 & a \\ b & 0.6 \end{bmatrix} is a transition matrix.
  8. Let P = \begin{bmatrix} 0.5 & 0.3 & 0.2 \\ 0.1 & 0.8 & 0.1 \\ 0 & 0.4 & 0.6 \end{bmatrix} and X_0 = \begin{bmatrix} 0.2 & 0.5 & 0.3 \end{bmatrix}. Find X_1.
  9. In a town of 5,000 voters, 85% of party A voters vote A again in the next election and 15% switch to B. Of party B voters, 10% switch to A and 90% stay. If 3,000 vote A and 2,000 vote B now, how many vote A next time?
  10. In Example 1, a customer buys at Daily this week. Use a tree to find the probability that they buy at Daily two weeks from now.
Show answers

1.

0.5 + 0.5 = 1

Row 1 adds to 1.

0.6 + 0.3 = 0.9

Row 2 adds to 0.9, not 1.

Answer: No. Row 2 adds to 0.9, so it is not a transition matrix.

2.

p_{WW} = 0.7,\ p_{WS} = 1 - 0.7 = 0.3

Row Workout: 70% work out again; the rest skip.

p_{SW} = 0.4,\ p_{SS} = 1 - 0.4 = 0.6

Row Skip: 40% work out; the rest skip again.

Answer: P = \begin{bmatrix} 0.7 & 0.3 \\ 0.4 & 0.6 \end{bmatrix}

3.

\text{W} = 0.6(0.7) + 0.4(0.4)

Multiply X_0 by column 1.

= 0.42 + 0.16 = 0.58

Multiply, then add.

\text{S} = 0.6(0.3) + 0.4(0.6)

Multiply X_0 by column 2.

= 0.18 + 0.24 = 0.42

Multiply, then add.

Answer: X_1 = \begin{bmatrix} 0.58 & 0.42 \end{bmatrix}

4.

0.9(0.9) + 0.1(0.5) = 0.81 + 0.05 = 0.86

Row 1 times column 1.

0.9(0.1) + 0.1(0.5) = 0.09 + 0.05 = 0.14

Row 1 times column 2.

0.5(0.9) + 0.5(0.5) = 0.45 + 0.25 = 0.70

Row 2 times column 1.

0.5(0.1) + 0.5(0.5) = 0.05 + 0.25 = 0.30

Row 2 times column 2.

Answer: P^2 = \begin{bmatrix} 0.86 & 0.14 \\ 0.70 & 0.30 \end{bmatrix}

5.

p^{(2)}_{21} = 0.70

Read row 2, column 1 of P^2 from Problem 4.

Answer: 0.70

6.

X_1 = \begin{bmatrix} 0.5 & 0.5 \end{bmatrix}

Starting in state 1 picks out row 1 of P.

0.5(0.5) + 0.5(0.2) = 0.25 + 0.10 = 0.35

Multiply X_1 by column 1.

0.5(0.5) + 0.5(0.8) = 0.25 + 0.40 = 0.65

Multiply X_1 by column 2.

Answer: X_2 = \begin{bmatrix} 0.35 & 0.65 \end{bmatrix}

7.

a = 1 - 0.35 = 0.65

Row 1 must add to 1.

b = 1 - 0.6 = 0.4

Row 2 must add to 1.

Answer: a = 0.65 and b = 0.4

8.

0.2(0.5) + 0.5(0.1) + 0.3(0) = 0.10 + 0.05 + 0 = 0.15

Multiply X_0 by column 1.

0.2(0.3) + 0.5(0.8) + 0.3(0.4) = 0.06 + 0.40 + 0.12 = 0.58

Multiply X_0 by column 2.

0.2(0.2) + 0.5(0.1) + 0.3(0.6) = 0.04 + 0.05 + 0.18 = 0.27

Multiply X_0 by column 3.

0.15 + 0.58 + 0.27 = 1

Check: the entries add to 1.

Answer: X_1 = \begin{bmatrix} 0.15 & 0.58 & 0.27 \end{bmatrix}

9.

P = \begin{bmatrix} 0.85 & 0.15 \\ 0.1 & 0.9 \end{bmatrix}

States in the order A, B.

3000(0.85) + 2000(0.1)

Multiply the counts by column A.

= 2550 + 200

Multiply.

= 2750

Add.

Answer: 2,750 vote A (and 5,000 − 2,750 = 2,250 vote B).

10.

0.3(0.2) = 0.06

Branch Daily → Brew → Daily.

0.7(0.7) = 0.49

Branch Daily → Daily → Daily.

0.06 + 0.49 = 0.55

Add the branches that end at Daily.

Answer: 0.55, the row D, column D entry of P^2 in Example 3.

Markov chains FAQ

What is a Markov chain in simple terms?

It is a process that moves from state to state in steps, where the chance of the next state depends only on the current state. Weather, customer loyalty and board games are common examples.

What is a transition matrix?

It is a square matrix whose row i, column j entry is the probability of moving from state i to state j in one step. Every entry is between 0 and 1, and each row adds to 1.

How do you find the state after n steps?

Multiply the starting row vector by P one step at a time, or compute X_n = X_0 P^n. Row i of P^n gives the n-step chances from state i.

What is the Markov property?

It is the “no memory” rule: given the current state, the past states give no extra information about the next state.

Summary

  • A Markov chain moves between states, and the next state depends only on the current one.
  • The transition matrix P lists the chance of going from row state i to column state j; each row adds to 1.
  • A state vector is a row of probabilities that add to 1, and X_{n+1} = X_n P.
  • The entries of P^n are n-step transition probabilities, and X_n = X_0 P^n.
  • Counts work the same way as probabilities: multiply the row of counts by P.

Previous lesson: Loan Amortization

Next lesson: Regular Markov Chains

Back to the Finite Math and Business Calculus Index