TExcelTAD
Home › Probability Theory › Absorbing Markov Chains

Absorbing Markov Chains

Many processes end: a game is won or lost, a customer churns or converts, a packet is delivered or dropped. Model the end states as absorbing states that the chain never leaves. Linear algebra then answers the natural questions exactly: how long until absorption, and where will the chain end up?

Canonical form

Order the states with the tt transient states first and the rr absorbing states last. The transition matrix has the block form

P=(QR0I)P = \begin{pmatrix} Q & R \\ 0 & I \end{pmatrix}

where QQ (t×tt \times t) holds transitions among transient states and RR (t×rt \times r) transitions from transient to absorbing states. Since absorption is certain, Qn→0Q^n \to 0.

The fundamental matrix

N=(I−Q)−1=I+Q+Q2+⋯N = (I - Q)^{-1} = I + Q + Q^2 + \cdots

The entry nijn_{ij} is the expected number of visits to transient state jj starting from transient state ii (the series counts visits at time 0,1,2,…0, 1, 2, \ldots). From NN:

  • expected steps to absorption: t=N1\mathbf{t} = N\mathbf{1} (row sums of NN);
  • absorption probabilities: B=NRB = NR, where bijb_{ij} is the probability of ending in absorbing state jj from transient state ii.

Example: a drunkard's walk

A walker on positions 0,1,2,3,40, 1, 2, 3, 4 moves left or right with probability 1/21/2; positions 0 and 4 are absorbing (home and the pub).

python · try it yourself
from sympy import Matrix, Rational, eye, ones

h = Rational(1, 2)
# transient states 1, 2, 3; absorbing states 0, 4
Q = Matrix([[0, h, 0],
            [h, 0, h],
            [0, h, 0]])
R = Matrix([[h, 0],
            [0, 0],
            [0, h]])
N = (eye(3) - Q).inv()
print("N =", N.tolist())
print("expected steps from 1, 2, 3:", (N * ones(3, 1)).T.tolist())
print("absorption probabilities [at 0, at 4]:", (N * R).tolist())
N = [[3/2, 1, 1/2], [1, 2, 1], [1/2, 1, 3/2]]
expected steps from 1, 2, 3: [[3, 4, 3]]
absorption probabilities [at 0, at 4]: [[3/4, 1/4], [1/2, 1/2], [1/4, 3/4]]

From the middle, the walk takes 4 steps on average and ends at either side with probability 1/21/2; from position 1 it ends at 0 with probability 3/43/4. These are gambler's ruin answers.

Example: a customer funnel

Visitors move between pages: Landing, Product, Cart, and two absorbing outcomes, Buy and Leave. With estimated transition probabilities, NN and BB give the conversion rate from each page and the expected number of page views.

python · try it yourself
import numpy as np

T = ["landing", "product", "cart"]
A = ["buy", "leave"]
Q = np.array([[0.10, 0.50, 0.00],
              [0.15, 0.20, 0.25],
              [0.00, 0.20, 0.10]])
R = np.array([[0.00, 0.40],
              [0.00, 0.40],
              [0.55, 0.15]])
N = np.linalg.inv(np.eye(3) - Q)
B = N @ R
steps = N.sum(axis=1)
for i, s in enumerate(T):
    print(f"from {s:8s}: P(buy) = {B[i, 0]:.3f}  expected page views = {steps[i]:.2f}")
print("rows of B sum to:", np.round(B.sum(axis=1), 6).tolist())
from landing : P(buy) = 0.128  expected page views = 2.32
from product : P(buy) = 0.231  expected page views = 2.18
from cart    : P(buy) = 0.662  expected page views = 1.60
rows of B sum to: [1.0, 1.0, 1.0]

Changing one probability (say, improving the cart page) and recomputing BB shows the effect on overall conversion: a small what-if model.

Checking by simulation

python · try it yourself
import random

rng = random.Random(2)
def walk(start):
    pos, steps = start, 0
    while 0 < pos < 4:
        pos += rng.choice((-1, 1))
        steps += 1
    return pos, steps

runs = 100_000
results = [walk(1) for _ in range(runs)]
print("from 1: P(end at 0) ~", round(sum(p == 0 for p, s in results) / runs, 4),
      " mean steps ~", round(sum(s for p, s in results) / runs, 3))
from 1: P(end at 0) ~ 0.7462  mean steps ~ 3.004

In software

Absorbing chains model user funnels, multi-stage pipelines with failures, retries that eventually succeed or give up, and board games such as Snakes and Ladders (the Mini Project). Survival analysis in reliability engineering often uses the same structure with "failed" as the absorbing state.

Exercises

Exercise 1: One transient state

A process stays in its only transient state with probability 0.80.8 each step and is otherwise absorbed. Find NN and the expected time to absorption.

Answer

N=5N = 5, expected time 5 steps.

Solution

Q=(0.8)Q = (0.8), N=1/(1−0.8)=5N = 1/(1 - 0.8) = 5. The time is geometric with p=0.2p = 0.2, mean 5.

Exercise 2: Interpreting NN

In the drunkard's walk, how many times does a walker starting at position 2 visit position 1, on average?

Answer
Solution

The entry n21n_{21} of NN (row for state 2, column for state 1) is 1.

Exercise 3: Longer walk (code)

For a walk on 0,…,100, \ldots, 10 with absorbing ends, compute the expected time to absorption from each interior position and compare with k(10−k)k(10 - k).

Answer

They agree: the expected time from kk is k(10−k)k(10 - k).

Solution
python · try it yourself
import numpy as np

n = 10
Q = np.zeros((n - 1, n - 1))
for i in range(n - 1):
    if i > 0:
        Q[i, i - 1] = 0.5
    if i < n - 2:
        Q[i, i + 1] = 0.5
t = np.linalg.solve(np.eye(n - 1) - Q, np.ones(n - 1))
print(np.round(t, 6).tolist())
print([k * (n - k) for k in range(1, n)])
[9.0, 16.0, 21.0, 24.0, 25.0, 24.0, 21.0, 16.0, 9.0]
[9, 16, 21, 24, 25, 24, 21, 16, 9]

Test yourself

In an absorbing chain, the fundamental matrix is…

  1. Q−1Q^{-1}
  2. (I−Q)−1(I - Q)^{-1}
  3. I−RI - R
  4. PnP^n

The expected number of steps to absorption from each transient state is…

  1. the diagonal of NN
  2. the row sums of NN
  3. the column sums of RR
  4. NRNR