States and a transition matrix
A Markov chain is a process that moves between a list of states. The chance of each next state depends only on the state now, not on how the chain got there. That is the Markov property, and the same chances apply at every step.
Take tomorrow's weather as sunny, cloudy or rainy, with these chances given today's:
| today, tomorrow | sunny | cloudy | rainy |
|---|---|---|---|
| sunny | 0.7 | 0.2 | 0.1 |
| cloudy | 0.3 | 0.4 | 0.3 |
| rainy | 0.2 | 0.3 | 0.5 |
The numbers are invented. Each entry is a conditional probability (lesson 5.1.3). The table is laid out like case study 5.1.6's rating table: rows are the state now, columns the state one step later.
Rule: Transition matrix
has one row and one column per state. Tomorrow's state is one of the states, so each row adds up to 1. The columns need not.
The weather chain as a diagram. Each arrow is a move with its chance, written near the state it leaves. The arrows leaving a state, its loop included, add up to 1.
The random walk of lesson 5.11.1 is a Markov chain too. Its states are the whole numbers, and from it moves to or , whatever its path so far.
Two steps ahead
To go from sunny to rainy in two days, the chain passes through some state tomorrow. The law of total probability (lesson 5.1.3) adds over the three:
That is row sunny of dotted with column rainy: entry (sunny, rainy) of . Case study 5.1.6 found two-year default chances the same way, and steps work like years there.
Rule: k steps ahead
Entry of the matrix power (lesson 3.6.2). The rows of add up to 1 as well.
Try it
The first line checks the table: entries at least 0, rows adding up to 1.
P[0] @ P[:, 2] is the two-day sum above, and P @ P gives every two-day chance at once. Its first row ends in 0.18.
The distribution after steps
Often the state now is itself uncertain. Write its chances as a row vector , such as for "sunny today". By total probability, tomorrow's chances are , as for case study 5.1.6's loan book.
Rule: Distribution after k steps
is a row vector: entry is , and the entries add up to 1. It multiplies from the left.
From a sunny day and from a rainy one:
| days | from sunny | from rainy |
|---|---|---|
| 1 | ||
| 2 | ||
| 3 | ||
| 5 | ||
| 10 | ||
| 20 |
After a few weeks the start is forgotten: both columns reach . Those are also the long-run shares of days. In one seeded run of 50,000 days, 46.06% are sunny, 28.21% cloudy and 25.73% rainy. That is a law of large numbers for chains, like lesson 5.7.2's for independent draws.
Try it
s0 @ matrix_power(P, k) is , printed for both starts.
The loop then runs the chain for 50,000 seeded days. Each day a uniform number picks the next state from the current row, as in case study 5.1.6, and days counts the visits.
The stationary distribution
A distribution that one more step leaves unchanged is stationary. Started there, the chain stays there: every is the same.
Rule: Stationary distribution
is a row vector of probabilities that the step maps to itself. For the weather chain it is the vector both starts settled on.
By linear equations
Write , and for the stationary chances of sunny, cloudy and rainy. Then says that each state's chance tomorrow, added up over where it comes from, equals its chance today:
The three equations add up to , because the rows of add up to 1. So one of them is redundant: replace it by . Elimination (lesson 3.3.1) gives .
As an eigenvector
Transpose both sides: . So the column is an eigenvector of with eigenvalue 1 (lesson 3.6.1), scaled so its entries add up to 1.
Rule: Stationary distribution as an eigenvector
Every transition matrix has the eigenvalue 1, because its rows add up to 1: . has the same eigenvalues as . No eigenvalue is larger than 1 in size.
The weather chain's eigenvalues are 1, 0.4732 and 0.1268. As in lesson 3.6.2, the largest one takes over and the rest dies away. The distance from shrinks by the factor 0.4732 a day in the long run, which is why three weeks were plenty.
Try it
The first lines replace the last equation of by the row of ones and solve; times 46, the answer is 21, 13 and 12. Then gives back.
np.linalg.eig(P.T) gives the same vector, once its column for eigenvalue 1 is divided by its sum, which also fixes its sign. The last line shows the trap: the eigenvector of itself for eigenvalue 1, scaled by its first entry, is .
The two towns as a Markov chain
Lesson 3.6.2 followed the shares of people in two towns. Each year 10% of town A moves to B, and 20% of town B moves to A. Read those shares as probabilities for one person, and becomes a Markov chain.
Lesson 3.6.2's had its columns adding up to 1. A transition matrix has rows adding up to 1, so here :
The eigenvector of that lesson 3.6.2 found, , is the chain's stationary distribution: its long-run distribution. A person living in A today is still there, or back there, in 10 years with probability . The other eigenvalue, 0.7, sets the speed, as there.
Rule: Two states
is the chance of leaving state 1 and of leaving state 2, with . The long run favours the state that is harder to leave. The gap to is multiplied by at each step.
Try it
The shortcut for the towns, and , then two starts: half the people in each town, and one person in A. After 10 years the first gives lesson 3.6.2's .
The last line checks that is .
When the long run does not settle
A stationary distribution always exists for a chain with finitely many states, but the chain need not settle on it. Two cases break the weather chain's behaviour.
- A chain that swings. moves to the other state every step. is stationary, but from state 1 the distribution goes , , , … for ever. Its eigenvalues are 1 and : the second has size 1 and never dies away.
- A chain with two ends. The walk of lesson 5.11.1 between 0 and 4 stops at either end. From 1 it ends at 0 with probability 0.75 and at 4 with 0.25; from 3 the other way round. Every is stationary, so the long run depends on the start.
Rule: When a chain settles
Such a chain is called regular, and its is unique (stated here, not proved). The weather chain is regular with . Neither the swinging chain nor the chain with two ends is.
Try it
Four steps of the swinging chain from state 1, then and the eigenvalues of .
Then the walk between 0 and 4 as a 5-state chain, G. After 60 steps each row has its weight only at the ends, and three different distributions are all stationary.
Absorbing states
A state that the chain never leaves, with , is absorbing. Default in case study 5.1.6's rating table is one. There, after 100 years, column D of was above 0.95 for every rating.
The long run is therefore dull: the only stationary distribution is , everyone in default. The useful questions are how long absorption takes and, when there are several absorbing states, which one comes first.
How long until absorption
Let hold the moves among the states that are not absorbing: for the rating table, the top-left block, A, B and C. Let be the expected number of steps to absorption from state .
Condition on the first step, as in lesson 5.11.1's gambler's ruin. One step is taken, and then the chain is in state with chance , an entry of , or absorbed:
Rule: Time to absorption
holds the moves among the non-absorbing states and is a column of ones. Entry of , the fundamental matrix, is the expected number of visits to from ; its row sums are the times .
For the rating table, the expected time to default is 36.80 years from A, 29.71 from B and 20.21 from C. In 20,000 seeded B borrowers it was 29.36, and the last one defaulted in year 266.
The rate in the long run
Case study 5.1.6 found the chance of default in each year, given none before: rising for A and B, falling for C. In the long run all three settle on one rate. The survivors' mix of ratings settles, and from then on survival falls by the same factor each year: the largest eigenvalue of , .
Default in year for borrowers still standing, by starting rating. Whatever the start, the rate settles on , set by the largest eigenvalue of the block .
Try it
np.linalg.solve(np.eye(3) - Q, np.ones(3)) gives without forming the inverse. Then and , the default rate in year 50 for each rating, and column D of .
The simulation moves 20,000 B borrowers a year at a time, with the seed 5125, until all have defaulted.
Which absorbing state
With two absorbing states, let be the chance of ending in the chosen one, say the top, from state . The first step gives , where is the chance of moving straight into the top.
Rule: Chance of each end
holds the one-step chances of entering the chosen absorbing state from each non-absorbing state. It is lesson 5.11.1's first-step analysis, written with matrices.
For the walk between 0 and 4, is the middle block of G, and only state 3 can step straight to 4: . Then and : lesson 5.11.1's and with .
Try it
The chances of ending at 4 and the expected number of steps, from 1, 2 and 3, then the fundamental matrix. Its rows add up to 3, 4 and 3.
Case study 5.11.4 prices options on a binomial tree, where the stock price itself moves as a Markov chain from node to node.
Common mistakes
- Rows, then a row vector on the left. With rows adding up to 1, the next distribution is , not . Lesson 3.6.2's layout is the transpose: check which way a table adds up.
- A matrix power is not a power of each entry. is
matrix_power(P, k);P**kraises each entry. - The eigenvector of , not of . For eigenvalue 1, itself gives : it only says the rows add up to 1.
- Scale to add up to 1. An eigenvector can come out with any length and either sign; dividing by its sum fixes both.
- Stationary is not always the long run. A swinging chain never settles, and with two absorbing states the end depends on the start.
- The Markov property is an assumption. Real ratings show momentum (case study 5.1.6's desk notes), which a single matrix cannot capture.
Markov chains
0 of 11 correct
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
How to type answers
- Powers with
^:x^7,2^(1/3),a^-2. - Multiply with
*or by writing side by side:3x^2,2(x+1). ln(x)is the natural log,log(x, 2)is log base 2,eis Euler's number,sqrt(x)is a square root.sin(x),cos(x)andtan(x)take radians; writepifor π andsin(x)^2for sin²x.- Your first answer to each question is saved: it counts towards your score and brings the question back for review.
- Exact answers like
ln(20)/ln(3)are best. Decimals need about 4 significant figures.
Summary card
- A Markov chain moves between states with chances that depend only on the state now. Its transition matrix has , and each row adds up to 1.
- steps ahead: . A distribution moves as a row vector, .
- The stationary distribution solves with entries adding up to 1. Replace one equation by the sum, or take the eigenvector of with eigenvalue 1 and scale it. Weather: ; lesson 3.6.2's towns: .
- A regular chain settles on from every start, the gap shrinking like . A swinging chain, or one with several absorbing states, does not.
- Absorbing chains: steps to absorption, for the chance of each end. Rating table: 29.71 years to default from B, and a long-run default rate of for every rating.