Mean First Passage Times
In an ergodic chain the walker keeps moving forever, so it makes sense to ask: starting from state , how many steps on average until it first reaches state ? These mean first passage times answer questions like "how long until a user who just signed up reaches the paid tier" or "how many moves until a random walk hits a target".
Making the target absorbing
To find the expected time to reach , make absorbing and apply the absorbing chain machinery: the expected time to absorption from is the mean first passage time . Equivalently, by first-step analysis,
a linear system with one unknown per starting state.
import numpy as np
P = np.array([[0.6, 0.3, 0.1],
[0.3, 0.4, 0.3],
[0.2, 0.3, 0.5]])
states = ["sun", "cloud", "rain"]
def first_passage_to(j):
others = [i for i in range(len(P)) if i != j]
Q = P[np.ix_(others, others)]
m = np.linalg.solve(np.eye(len(others)) - Q, np.ones(len(others)))
return dict(zip([states[i] for i in others], np.round(m, 4).tolist()))
for j, name in enumerate(states):
print(f"mean days until {name:5s}:", first_passage_to(j))mean days until sun : {'cloud': 3.8095, 'rain': 4.2857}
mean days until cloud: {'sun': 3.3333, 'rain': 3.3333}
mean days until rain : {'sun': 6.0, 'cloud': 4.6667}
Mean recurrence time
Starting from , how long until the chain returns to ? For an ergodic chain the answer is beautifully simple:
where is the stationary distribution. If the chain spends a fraction of its time in , visits must be spaced steps apart on average. For the weather chain, rainy days have , so after a rainy day the next rainy day comes on average days later.
import random
P = [[0.6, 0.3, 0.1], [0.3, 0.4, 0.3], [0.2, 0.3, 0.5]]
rng = random.Random(6)
state, last_rain, gaps = 2, 0, []
for t in range(1, 300_001):
state = rng.choices([0, 1, 2], weights=P[state])[0]
if state == 2:
gaps.append(t - last_rain)
last_rain = t
print("mean gap between rainy days:", round(sum(gaps) / len(gaps), 4), " 18/5 =", 18 / 5)mean gap between rainy days: 3.5774 18/5 = 3.6
Hitting times on graphs
For a random walk on a graph, the expected time to go from one vertex to another can be very different in the two directions. On a "lollipop" (a clique with a long path attached), walking from the clique to the end of the path takes on the order of steps, while walking back takes only order . The commute time equals times the effective resistance between and when the graph is viewed as an electrical network, one of the surprising bridges between probability and physics.
import numpy as np
def hitting_times(adj, target):
n = len(adj)
P = np.array([[1 / len(adj[i]) if j in adj[i] else 0 for j in range(n)] for i in range(n)])
others = [i for i in range(n) if i != target]
Q = P[np.ix_(others, others)]
m = np.linalg.solve(np.eye(n - 1) - Q, np.ones(n - 1))
return dict(zip(others, m))
# path 0 - 1 - 2 - ... - 9
path = {i: [j for j in (i - 1, i + 1) if 0 <= j < 10] for i in range(10)}
print("path, 0 -> 9:", round(hitting_times(path, 9)[0], 2), " (theory 81)")
# lollipop: clique on 0..5, path 5 - 6 - ... - 11
lol = {i: [j for j in range(6) if j != i] for i in range(6)}
for i in range(6, 12):
lol[i] = []
for a in range(5, 11):
lol[a].append(a + 1)
lol[a + 1].append(a)
print("lollipop, clique -> end:", round(hitting_times(lol, 11)[0], 1))
print("lollipop, end -> clique:", round(hitting_times(lol, 0)[11], 1))path, 0 -> 9: 81.0 (theory 81)
lollipop, clique -> end: 221.0
lollipop, end -> clique: 45.0
In software
Mean first passage times estimate time-to-conversion in user journeys, expected time until a system reaches a failure state, and the cover and hitting times of random-walk-based algorithms (random-walk crawlers, randomized SAT solvers such as WalkSAT, graph sampling). Recurrence times give the average spacing of events in any stationary process modelled by a chain.
Exercises
Exercise 1: Recurrence
A chain has stationary distribution . What is the mean recurrence time of the third state?
Answer
5 steps.
Solution
.
Exercise 2: First passage in a two-state chain
In the server chain (up to down with probability 0.01, down to up with probability 0.2), what is the mean time from "up" until the first failure, and from "down" until recovery?
Answer
100 hours and 5 hours.
Solution
Each is a geometric waiting time: and . A full cycle (up until failure, then down until recovery) lasts hours on average, of which 100 are up, matching the long-run availability from Ergodic Chains.
Exercise 3: Path hitting time (code)
For a random walk on a path (reflecting at 0), the expected time from 0 to is . Check it for by solving the linear system.
Answer
25, 100 and 400.
Solution
import numpy as np
for n in [5, 10, 20]:
Q = np.zeros((n, n)) # states 0..n-1, target n absorbing
Q[0, 1] = 1.0 # reflect at 0
for i in range(1, n):
Q[i, i - 1] = 0.5
if i + 1 < n:
Q[i, i + 1] = 0.5
m = np.linalg.solve(np.eye(n) - Q, np.ones(n))
print(n, round(m[0], 6))5 25.0
10 100.0
20 400.0
Test yourself
For an ergodic chain with stationary distribution , the mean recurrence time of state is…
How do you compute the mean first passage times to state ?
- Make absorbing and compute expected absorption times
- Invert
- Take for large
- Use the diagonal of