SigmaField
Mathematical Foundations for Quant Finance
0 of 256 lessons
      1. 5.11.1 Random walksConcept · 30 min
      2. 5.11.2 Markov chainsConcept · 35 min
      3. 5.11.3 Practice set: discrete-time processesPractice set · 25 min
      4. 5.11.4 Case study: pricing on a multi-step binomial treeCase study · 35 min

Level 5, Probability and statistics 5.11 Discrete-time processes Lesson 2 of 4

ConceptLesson 5.11.2 in Level 5, Probability and statistics. About 35 minutes.

Markov chains

States and a transition matrix

A Markov chain is a process X0,X1,X2,…X_0, X_1, X_2, \ldots 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, tomorrowsunnycloudyrainy
sunny0.70.20.1
cloudy0.30.40.3
rainy0.20.30.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

pij=P(Xn+1=j∣Xn=i)pij≥0,∑jpij=1\begin{gathered} p_{ij} = P(X_{n+1} = j \mid X_n = i) \\[4pt] p_{ij} \ge 0, \quad \textstyle\sum_j p_{ij} = 1 \end{gathered}

PP 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 kk it moves to k+1k + 1 or k−1k - 1, 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:

0.7×0.1+0.2×0.3+0.1×0.5=0.180.7 \times 0.1 + 0.2 \times 0.3 + 0.1 \times 0.5 = 0.18

That is row sunny of PP dotted with column rainy: entry (sunny, rainy) of P2P^2. Case study 5.1.6 found two-year default chances the same way, and kk steps work like kk years there.

Rule: k steps ahead

P(Xn+k=j∣Xn=i)=(Pk)ijP(X_{n+k} = j \mid X_n = i) = (P^k)_{ij}

Entry (i,j)(i, j) of the matrix power PkP^k (lesson 3.6.2). The rows of PkP^k 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.

⌘/Ctrl + Enter runs the cell

The distribution after kk steps

Often the state now is itself uncertain. Write its chances as a row vector s0\mathbf{s}_0, such as (1,0,0)(1, 0, 0) for "sunny today". By total probability, tomorrow's chances are s1=s0P\mathbf{s}_1 = \mathbf{s}_0P, as for case study 5.1.6's loan book.

Rule: Distribution after k steps

sk+1=skPsk=s0Pk\begin{gathered} \mathbf{s}_{k + 1} = \mathbf{s}_k P \\[4pt] \mathbf{s}_k = \mathbf{s}_0 P^k \end{gathered}

sk\mathbf{s}_k is a row vector: entry jj is P(Xk=j)P(X_k = j), and the entries add up to 1. It multiplies PP from the left.

From a sunny day and from a rainy one:

days kksk\mathbf{s}_k from sunnysk\mathbf{s}_k from rainy
1(0.7,0.2,0.1)(0.7, 0.2, 0.1)(0.2,0.3,0.5)(0.2, 0.3, 0.5)
2(0.57,0.25,0.18)(0.57, 0.25, 0.18)(0.33,0.31,0.36)(0.33, 0.31, 0.36)
3(0.51,0.268,0.222)(0.51, 0.268, 0.222)(0.396,0.298,0.306)(0.396, 0.298, 0.306)
5(0.4685,0.2794,0.2521)(0.4685, 0.2794, 0.2521)(0.4429,0.2862,0.2708)(0.4429, 0.2862, 0.2708)
10(0.4568,0.2825,0.2607)(0.4568, 0.2825, 0.2607)(0.4562,0.2827,0.2611)(0.4562, 0.2827, 0.2611)
20(0.4565,0.2826,0.2609)(0.4565, 0.2826, 0.2609)(0.4565,0.2826,0.2609)(0.4565, 0.2826, 0.2609)

After a few weeks the start is forgotten: both columns reach (0.4565,0.2826,0.2609)(0.4565, 0.2826, 0.2609). 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 s0Pk\mathbf{s}_0P^k, 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.

⌘/Ctrl + Enter runs the cell

The stationary distribution

A distribution that one more step leaves unchanged is stationary. Started there, the chain stays there: every sk\mathbf{s}_k is the same.

Rule: Stationary distribution

πP=ππj≥0,∑jπj=1\begin{gathered} \boldsymbol{\pi} P = \boldsymbol{\pi} \\[4pt] \pi_j \ge 0, \quad \textstyle\sum_j \pi_j = 1 \end{gathered}

π\boldsymbol{\pi} is a row vector of probabilities that the step PP maps to itself. For the weather chain it is the vector both starts settled on.

By linear equations

Write πS\pi_S, πC\pi_C and πR\pi_R for the stationary chances of sunny, cloudy and rainy. Then πP=π\boldsymbol{\pi}P = \boldsymbol{\pi} says that each state's chance tomorrow, added up over where it comes from, equals its chance today:

πS=0.7πS+0.3πC+0.2πRπC=0.2πS+0.4πC+0.3πRπR=0.1πS+0.3πC+0.5πR\begin{aligned} \pi_S &= 0.7\pi_S + 0.3\pi_C + 0.2\pi_R \\ \pi_C &= 0.2\pi_S + 0.4\pi_C + 0.3\pi_R \\ \pi_R &= 0.1\pi_S + 0.3\pi_C + 0.5\pi_R \end{aligned}

The three equations add up to πS+πC+πR=πS+πC+πR\pi_S + \pi_C + \pi_R = \pi_S + \pi_C + \pi_R, because the rows of PP add up to 1. So one of them is redundant: replace it by πS+πC+πR=1\pi_S + \pi_C + \pi_R = 1. Elimination (lesson 3.3.1) gives π=(2146,1346,623)=(0.4565,0.2826,0.2609)\boldsymbol{\pi} = \left(\frac{21}{46}, \frac{13}{46}, \frac{6}{23}\right) = (0.4565, 0.2826, 0.2609).

As an eigenvector

Transpose both sides: P⊤π⊤=π⊤P^\top\boldsymbol{\pi}^\top = \boldsymbol{\pi}^\top. So the column π⊤\boldsymbol{\pi}^\top is an eigenvector of P⊤P^\top with eigenvalue 1 (lesson 3.6.1), scaled so its entries add up to 1.

Rule: Stationary distribution as an eigenvector

P⊤π⊤=1⋅π⊤scale to ∑jπj=1\begin{gathered} P^\top \boldsymbol{\pi}^\top = 1 \cdot \boldsymbol{\pi}^\top \\[4pt] \text{scale to } \textstyle\sum_j \pi_j = 1 \end{gathered}

Every transition matrix has the eigenvalue 1, because its rows add up to 1: P1=1P\mathbf{1} = \mathbf{1}. P⊤P^\top has the same eigenvalues as PP. 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 π\boldsymbol{\pi} 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 (P⊤−I)π⊤=0(P^\top - I)\boldsymbol{\pi}^\top = \mathbf{0} by the row of ones and solve; times 46, the answer is 21, 13 and 12. Then πP\boldsymbol{\pi}P gives π\boldsymbol{\pi} 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 PP itself for eigenvalue 1, scaled by its first entry, is (1,1,1)(1, 1, 1).

⌘/Ctrl + Enter runs the cell

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 MM becomes a Markov chain.

Lesson 3.6.2's MM had its columns adding up to 1. A transition matrix has rows adding up to 1, so here P=M⊤P = M^\top:

P=(0.90.10.20.8)P = \begin{pmatrix} 0.9 & 0.1 \\ 0.2 & 0.8 \end{pmatrix}

The eigenvector of λ=1\lambda = 1 that lesson 3.6.2 found, (23,13)\left(\frac{2}{3}, \frac{1}{3}\right), 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 23+13(0.7)10=0.6761\frac{2}{3} + \frac{1}{3}(0.7)^{10} = 0.6761. The other eigenvalue, 0.7, sets the speed, as there.

Rule: Two states

P=(1−aab1−b)π=(ba+b, aa+b)λ2=1−a−b\begin{gathered} P = \begin{pmatrix} 1 - a & a \\ b & 1 - b \end{pmatrix} \\[4pt] \boldsymbol{\pi} = \left(\frac{b}{a + b},\ \frac{a}{a + b}\right) \\[4pt] \lambda_2 = 1 - a - b \end{gathered}

aa is the chance of leaving state 1 and bb of leaving state 2, with a+b>0a + b > 0. The long run favours the state that is harder to leave. The gap to π\boldsymbol{\pi} is multiplied by λ2\lambda_2 at each step.

Try it

The shortcut for the towns, a=0.1a = 0.1 and b=0.2b = 0.2, 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 (0.662,0.338)(0.662, 0.338).

The last line checks that PP is M⊤M^\top.

⌘/Ctrl + Enter runs the cell

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. F=(0110)F = \begin{pmatrix} 0 & 1 \\ 1 & 0 \end{pmatrix} moves to the other state every step. (12,12)\left(\frac{1}{2}, \frac{1}{2}\right) is stationary, but from state 1 the distribution goes (0,1)(0, 1), (1,0)(1, 0), (0,1)(0, 1), … for ever. Its eigenvalues are 1 and −1-1: 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 (1−w,0,0,0,w)(1 - w, 0, 0, 0, w) is stationary, so the long run depends on the start.

Rule: When a chain settles

some Pk has every entry>0⇒ sk→π from every start\begin{gathered} \text{some } P^k \text{ has every entry} > 0 \\[4pt] \Rightarrow\ \mathbf{s}_k \to \boldsymbol{\pi} \text{ from every start} \end{gathered}

Such a chain is called regular, and its π\boldsymbol{\pi} is unique (stated here, not proved). The weather chain is regular with k=1k = 1. Neither the swinging chain nor the chain with two ends is.

Try it

Four steps of the swinging chain from state 1, then (12,12)F\left(\frac{1}{2}, \frac{1}{2}\right)F and the eigenvalues of FF.

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.

⌘/Ctrl + Enter runs the cell

Absorbing states

A state that the chain never leaves, with pii=1p_{ii} = 1, is absorbing. Default in case study 5.1.6's rating table is one. There, after 100 years, column D of P100P^{100} was above 0.95 for every rating.

The long run is therefore dull: the only stationary distribution is (0,0,0,1)(0, 0, 0, 1), 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 QQ hold the moves among the states that are not absorbing: for the rating table, the top-left 3×33 \times 3 block, A, B and C. Let tit_i be the expected number of steps to absorption from state ii.

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 jj with chance qijq_{ij}, an entry of QQ, or absorbed:

ti=1+∑jqij tj(I−Q) t=1\begin{gathered} t_i = 1 + \textstyle\sum_j q_{ij}\,t_j \\[4pt] (I - Q)\,\mathbf{t} = \mathbf{1} \end{gathered}

Rule: Time to absorption

t=(I−Q)−1 1\mathbf{t} = (I - Q)^{-1}\,\mathbf{1}

QQ holds the moves among the non-absorbing states and 1\mathbf{1} is a column of ones. Entry (i,j)(i, j) of (I−Q)−1(I - Q)^{-1}, the fundamental matrix, is the expected number of visits to jj from ii; its row sums are the times tit_i.

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 QQ, λ1=0.9666\lambda_1 = 0.9666.

Default in year kk for borrowers still standing, by starting rating. Whatever the start, the rate settles on 1−λ1=3.34%1 - \lambda_1 = 3.34\%, set by the largest eigenvalue of the block QQ.

Try it

np.linalg.solve(np.eye(3) - Q, np.ones(3)) gives t\mathbf{t} without forming the inverse. Then λ1\lambda_1 and 1−λ11 - \lambda_1, the default rate in year 50 for each rating, and column D of P100P^{100}.

The simulation moves 20,000 B borrowers a year at a time, with the seed 5125, until all have defaulted.

⌘/Ctrl + Enter runs the cell

Which absorbing state

With two absorbing states, let hih_i be the chance of ending in the chosen one, say the top, from state ii. The first step gives hi=ui+∑jqijhjh_i = u_i + \sum_j q_{ij}h_j, where uiu_i is the chance of moving straight into the top.

Rule: Chance of each end

h=(I−Q)−1 u\mathbf{h} = (I - Q)^{-1}\,\mathbf{u}

u\mathbf{u} 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, QQ is the middle 3×33 \times 3 block of G, and only state 3 can step straight to 4: u=(0,0,0.5)\mathbf{u} = (0, 0, 0.5). Then h=(0.25,0.5,0.75)\mathbf{h} = (0.25, 0.5, 0.75) and t=(3,4,3)\mathbf{t} = (3, 4, 3): lesson 5.11.1's ab\frac{a}{b} and a(b−a)a(b - a) with b=4b = 4.

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.

⌘/Ctrl + Enter runs the cell

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

  1. Rows, then a row vector on the left. With rows adding up to 1, the next distribution is sP\mathbf{s}P, not PsP\mathbf{s}. Lesson 3.6.2's layout is the transpose: check which way a table adds up.
  2. A matrix power is not a power of each entry. PkP^k is matrix_power(P, k); P**k raises each entry.
  3. The eigenvector of P⊤P^\top, not of PP. For eigenvalue 1, PP itself gives (1,1,1)(1, 1, 1): it only says the rows add up to 1.
  4. Scale to add up to 1. An eigenvector can come out with any length and either sign; dividing by its sum fixes both.
  5. Stationary is not always the long run. A swinging chain never settles, and with two absorbing states the end depends on the start.
  6. 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. 1.
  2. 2.
  3. 3.
  4. 4.
  5. 5.
  6. 6.
  7. 7.
  8. 8.
  9. 9.
  10. 10.
  11. 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, e is Euler's number, sqrt(x) is a square root.
  • sin(x), cos(x) and tan(x) take radians; write pi for π and sin(x)^2 for 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 pij=P(Xn+1=j∣Xn=i)p_{ij} = P(X_{n+1} = j \mid X_n = i), and each row adds up to 1.
  • kk steps ahead: (Pk)ij(P^k)_{ij}. A distribution moves as a row vector, sk=s0Pk\mathbf{s}_k = \mathbf{s}_0P^k.
  • The stationary distribution solves πP=π\boldsymbol{\pi}P = \boldsymbol{\pi} with entries adding up to 1. Replace one equation by the sum, or take the eigenvector of P⊤P^\top with eigenvalue 1 and scale it. Weather: (0.4565,0.2826,0.2609)(0.4565, 0.2826, 0.2609); lesson 3.6.2's towns: (23,13)\left(\frac{2}{3}, \frac{1}{3}\right).
  • A regular chain settles on π\boldsymbol{\pi} from every start, the gap shrinking like ∣λ2∣k\lvert\lambda_2\rvert^k. A swinging chain, or one with several absorbing states, does not.
  • Absorbing chains: t=(I−Q)−11\mathbf{t} = (I - Q)^{-1}\mathbf{1} steps to absorption, h=(I−Q)−1u\mathbf{h} = (I - Q)^{-1}\mathbf{u} for the chance of each end. Rating table: 29.71 years to default from B, and a long-run default rate of 1−λ1=3.34%1 - \lambda_1 = 3.34\% for every rating.

Free further reading

Optional. This lesson covers what you need; these add more examples and practice. All are free to read.