TExcelTAD
Home › Probability Theory › Mean First Passage Times

Mean First Passage Times

In an ergodic chain the walker keeps moving forever, so it makes sense to ask: starting from state ii, how many steps on average until it first reaches state jj? 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 jj, make jj absorbing and apply the absorbing chain machinery: the expected time to absorption from ii is the mean first passage time mijm_{ij}. Equivalently, by first-step analysis,

mij=1+∑k≠jpikmkj(i≠j)m_{ij} = 1 + \sum_{k \ne j} p_{ik} m_{kj} \qquad (i \ne j)

a linear system with one unknown per starting state.

python · try it yourself
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 jj, how long until the chain returns to jj? For an ergodic chain the answer is beautifully simple:

rj=1wjr_j = \frac{1}{w_j}

where w\mathbf{w} is the stationary distribution. If the chain spends a fraction wjw_j of its time in jj, visits must be spaced 1/wj1/w_j steps apart on average. For the weather chain, rainy days have w=5/18w = 5/18, so after a rainy day the next rainy day comes on average 18/5=3.618/5 = 3.6 days later.

python · try it yourself
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 n3n^3 steps, while walking back takes only order n2n^2. The commute time mij+mjim_{ij} + m_{ji} equals 2∣E∣2\lvert E \rvert times the effective resistance between ii and jj when the graph is viewed as an electrical network, one of the surprising bridges between probability and physics.

python · try it yourself
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 (0.5,0.3,0.2)(0.5, 0.3, 0.2). What is the mean recurrence time of the third state?

Answer

5 steps.

Solution

r3=1/w3=1/0.2r_3 = 1/w_3 = 1/0.2.

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: 1/0.011/0.01 and 1/0.21/0.2. A full cycle (up until failure, then down until recovery) lasts 100+5=105100 + 5 = 105 hours on average, of which 100 are up, matching the long-run availability 100/105≈0.952100/105 \approx 0.952 from Ergodic Chains.

Exercise 3: Path hitting time (code)

For a random walk on a path 0,1,…,n0, 1, \ldots, n (reflecting at 0), the expected time from 0 to nn is n2n^2. Check it for n=5,10,20n = 5, 10, 20 by solving the linear system.

Answer

25, 100 and 400.

Solution
python · try it yourself
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 ww, the mean recurrence time of state jj is…

  1. wjw_j
  2. 1/wj1/w_j
  3. 1−wj1 - w_j
  4. wj2w_j^2

How do you compute the mean first passage times to state jj?

  1. Make jj absorbing and compute expected absorption times
  2. Invert PP
  3. Take PnP^n for large nn
  4. Use the diagonal of PP