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 transient states first and the absorbing states last. The transition matrix has the block form
where () holds transitions among transient states and () transitions from transient to absorbing states. Since absorption is certain, .
The fundamental matrix
The entry is the expected number of visits to transient state starting from transient state (the series counts visits at time ). From :
- expected steps to absorption: (row sums of );
- absorption probabilities: , where is the probability of ending in absorbing state from transient state .
Example: a drunkard's walk
A walker on positions moves left or right with probability ; positions 0 and 4 are absorbing (home and the pub).
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 ; from position 1 it ends at 0 with probability . 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, and give the conversion rate from each page and the expected number of page views.
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 shows the effect on overall conversion: a small what-if model.
Checking by simulation
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 each step and is otherwise absorbed. Find and the expected time to absorption.
Answer
, expected time 5 steps.
Solution
, . The time is geometric with , mean 5.
Exercise 2: Interpreting
In the drunkard's walk, how many times does a walker starting at position 2 visit position 1, on average?
Answer
Solution
The entry of (row for state 2, column for state 1) is 1.
Exercise 3: Longer walk (code)
For a walk on with absorbing ends, compute the expected time to absorption from each interior position and compare with .
Answer
They agree: the expected time from is .
Solution
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…
The expected number of steps to absorption from each transient state is…
- the diagonal of
- the row sums of
- the column sums of