Skip to content
LeetQuant
QT04 Random Walks and Optimal Stopping

QT04 · Random Walks and Optimal Stopping · Lesson 3 of 4

Hitting Times via Martingales

Build martingales from random walks, apply the optional stopping theorem (and know when it fails), and use it with Wald's identities to get ruin probabilities, expected durations and variances in a few lines.

18 min

The idea

First-step analysis solves hitting problems with a system of linear equations, one per state. A martingale does the same job with one equation. If MnM_n is a martingale and you stop at a well-behaved time τ\tau, then

E[Mτ]=M0.\mathbb{E}[M_\tau] = M_0 .

At the stopping time you usually know where the walk is (on one of the barriers), so this single equation pins down the unknown probability or expectation. The skill is picking the right martingale.

Martingales from a random walk

Let Sn=X1+⋯+XnS_n = X_1 + \dots + X_n with i.i.d. steps Xi=+1X_i = +1 with probability pp and −1-1 with probability q=1−pq = 1 - p. The step has mean μ=p−q\mu = p - q and variance σ2=1−μ2=4pq\sigma^2 = 1 - \mu^2 = 4pq.

MartingaleWhyUsed for
Sn−nμS_n - n\musubtract the driftexpected durations (Wald)
(Sn−nμ)2−nσ2(S_n - n\mu)^2 - n\sigma^2subtract the variancedurations of fair walks, variances
(q/p)Sn(q/p)^{S_n}E[(q/p)X]=p⋅qp+q⋅pq=1\mathbb{E}[(q/p)^{X}] = p\cdot\tfrac qp + q\cdot\tfrac pq = 1ruin probabilities of biased walks
eθSn/φ(θ)ne^{\theta S_n}/\varphi(\theta)^n, φ(θ)=peθ+qe−θ\varphi(\theta) = pe^{\theta} + qe^{-\theta}normalise by the MGFdistribution of τ\tau

For the fair walk (p=12p = \tfrac12) the first two become SnS_n and Sn2−nS_n^2 - n.

To check a candidate, compute E[Mn+1∣Fn]\mathbb{E}[M_{n+1} \mid \mathcal{F}_n] by writing Sn+1=Sn+Xn+1S_{n+1} = S_n + X_{n+1} and averaging over the one new step.

The optional stopping theorem

Let MM be a martingale and τ\tau a stopping time. Then E[Mτ]=M0\mathbb{E}[M_\tau] = M_0 if any one of these holds:

  1. τ\tau is bounded: τ≤K\tau \le K for some constant KK.
  2. τ<∞\tau < \infty almost surely and the stopped process is bounded: ∣Mn∧τ∣≤C|M_{n \wedge \tau}| \le C for all nn.
  3. E[τ]<∞\mathbb{E}[\tau] < \infty and the increments are bounded: ∣Mn+1−Mn∣≤C|M_{n+1} - M_n| \le C.

For a walk between two barriers, condition 2 covers SnS_n and (q/p)Sn(q/p)^{S_n} (both bounded until absorption). For Sn−nμS_n - n\mu and Sn2−nS_n^2 - n, use condition 3 or apply the theorem to the bounded time n∧τn \wedge \tau and let n→∞n \to \infty.

When it fails. A fair walk started at 0 hits +1+1 eventually with probability 1. Stop there: Sτ=1S_\tau = 1, so E[Sτ]=1≠0\mathbb{E}[S_\tau] = 1 \ne 0. None of the conditions holds: τ\tau is unbounded, Sn∧τS_{n \wedge \tau} is unbounded below, and E[τ]=∞\mathbb{E}[\tau] = \infty. This last fact drops out of the failure itself: if E[τ]\mathbb{E}[\tau] were finite, condition 3 would force E[Sτ]=0\mathbb{E}[S_\tau] = 0.

The doubling strategy (double your stake after each loss, stop at the first win) is the same trap. It wins one unit with probability 1, but only by allowing unbounded losses along the way and unbounded time. With any credit limit or time limit, condition 1 or 2 applies and the expected gain is exactly zero.

Gambler's ruin by martingales

Start at 0 with absorbing barriers at −a-a and +b+b, and let P=P(hit b first)P = P(\text{hit } b \text{ first}).

Fair walk. From SnS_n: bP−a(1−P)=0bP - a(1 - P) = 0, so P=aa+bP = \dfrac{a}{a + b}. From Sn2−nS_n^2 - n: E[τ]=E[Sτ2]=b2P+a2(1−P)=ab\mathbb{E}[\tau] = \mathbb{E}[S_\tau^2] = b^2 P + a^2(1 - P) = ab.

Biased walk. Put r=q/pr = q/p. From rSnr^{S_n}:

P rb+(1−P) r−a=1⟹P=1−r−arb−r−a.P\,r^{b} + (1 - P)\,r^{-a} = 1 \quad\Longrightarrow\quad P = \frac{1 - r^{-a}}{r^{b} - r^{-a}} .

Then Wald's identity (optional stopping for Sn−nμS_n - n\mu) gives the duration without solving any recurrence:

E[Sτ]=μ E[τ]⟹E[τ]=bP−a(1−P)p−q.\mathbb{E}[S_\tau] = \mu\,\mathbb{E}[\tau] \quad\Longrightarrow\quad \mathbb{E}[\tau] = \frac{bP - a(1 - P)}{p - q} .

One barrier. Let b→∞b \to \infty in the formula for PP with p>qp > q (so r<1r < 1): the probability of ever falling to −a-a is ra=(q/p)ar^{a} = (q/p)^{a}. With p<qp < q, the probability of ever rising to +b+b is (p/q)b(p/q)^{b}. And with p>qp > q, the time to reach +b+b has

E[τb]=bp−q.\mathbb{E}[\tau_b] = \frac{b}{p - q} .

To justify this, apply Wald at the bounded time n∧τbn \wedge \tau_b: μ E[n∧τb]=E[Sn∧τb]≤b\mu\,\mathbb{E}[n \wedge \tau_b] = \mathbb{E}[S_{n \wedge \tau_b}] \le b, so E[τb]≤b/μ<∞\mathbb{E}[\tau_b] \le b/\mu < \infty by monotone convergence, and then Wald applies at τb\tau_b itself, where Sτb=bS_{\tau_b} = b exactly.

Worked example

a biased walk between two barriers

A walk starts at 0 and steps up with probability 23\tfrac23, down with probability 13\tfrac13. It stops at +4+4 or −2-2. Find the probability it stops at +4+4 and the expected number of steps.

Show solution

Here r=q/p=12r = q/p = \tfrac12, so rSn=2−Snr^{S_n} = 2^{-S_n} is a martingale. It is bounded between 2−42^{-4} and 222^{2} until absorption, so optional stopping applies:

P⋅116+(1−P)⋅4=1⟹3=P(4−116)=6316P⟹P=4863=1621.P \cdot \tfrac1{16} + (1 - P) \cdot 4 = 1 \quad\Longrightarrow\quad 3 = P\Big(4 - \tfrac1{16}\Big) = \tfrac{63}{16}P \quad\Longrightarrow\quad P = \tfrac{48}{63} = \tfrac{16}{21} .

For the duration, μ=13\mu = \tfrac13 and

E[Sτ]=4⋅1621−2⋅521=5421=187,E[τ]=18/71/3=547≈7.71.\mathbb{E}[S_\tau] = 4 \cdot \tfrac{16}{21} - 2 \cdot \tfrac{5}{21} = \tfrac{54}{21} = \tfrac{18}{7}, \qquad \mathbb{E}[\tau] = \frac{18/7}{1/3} = \frac{54}{7} \approx 7.71 .

Solving the seven first-step equations with exact fractions gives the same 1621\tfrac{16}{21} and 547\tfrac{54}{7}.

Sanity checks. PP is well above the fair-walk value 26=13\tfrac{2}{6} = \tfrac13, as the drift should make it. The duration is below the one-barrier value 4/μ=124/\mu = 12, because the lower barrier sometimes ends the game early.

Wald's second identity: variances

Optional stopping for (Sn−nμ)2−nσ2(S_n - n\mu)^2 - n\sigma^2 gives Wald's second identity: if E[τ]<∞\mathbb{E}[\tau] < \infty,

E[(Sτ−μτ)2]=σ2 E[τ].\mathbb{E}\big[(S_\tau - \mu\tau)^2\big] = \sigma^2\,\mathbb{E}[\tau] .

For a one-sided hit with p>qp > q, Sτb=b=μ E[τb]S_{\tau_b} = b = \mu\,\mathbb{E}[\tau_b], so the left side is E[(μ Eτb−μτb)2]=μ2Var⁡(τb)\mathbb{E}[(\mu\,\mathbb{E}\tau_b - \mu\tau_b)^2] = \mu^2 \operatorname{Var}(\tau_b). Hence

Var⁡(τb)=σ2 bμ3=4pq b(p−q)3.\operatorname{Var}(\tau_b) = \frac{\sigma^2\, b}{\mu^3} = \frac{4pq\, b}{(p - q)^3} .

The cube in the denominator matters: a small edge makes the hitting time not just long but extremely variable.

Worked example

how long to make five dollars?

You win each $1 bet with probability 0.60.6 and have unlimited credit. Find the mean and standard deviation of the number of bets until you are first $5 up.

Show solution

μ=0.2\mu = 0.2 and σ2=4×0.6×0.4=0.96\sigma^2 = 4 \times 0.6 \times 0.4 = 0.96.

E[τ5]=50.2=25,Var⁡(τ5)=0.96×50.23=4.80.008=600.\mathbb{E}[\tau_5] = \frac{5}{0.2} = 25, \qquad \operatorname{Var}(\tau_5) = \frac{0.96 \times 5}{0.2^3} = \frac{4.8}{0.008} = 600 .

The standard deviation is 600≈24.5\sqrt{600} \approx 24.5 bets, about the same as the mean: the distribution has a long right tail from paths that dip early. An exact computation of the distribution of τ5\tau_5 gives mean 2525 and variance 600600.

Also: the probability of ever being $2 down is (0.4/0.6)2=49(0.4/0.6)^2 = \tfrac49, even though you are certain to be $5 up eventually.

Waiting for a pattern: the casino martingale

How many fair coin flips until HTH first appears? Imagine a gambler arriving before every flip with $1, betting it all that the next flip starts the pattern (H), and if he wins, betting everything on the next letter, and so on. Each bet is fair, so the casino's net takings are a martingale with mean zero, and optional stopping applies because the waiting time has finite mean.

When HTH first completes at time τ\tau, the house has received τ\tau dollars. Who is still holding money?

  • The gambler who arrived at flip τ−2\tau - 2 has won three doubles: $8.
  • The gambler who arrived at flip τ−1\tau - 1 bet on H and lost.
  • The gambler who arrived at flip τ\tau won one bet on H: $2.

Everyone else has lost. So E[τ]−(8+2)=0\mathbb{E}[\tau] - (8 + 2) = 0 and E[τ]=10\mathbb{E}[\tau] = 10.

In general E[τ]=∑2k\mathbb{E}[\tau] = \sum 2^{k} over every kk for which the length-kk prefix of the pattern equals its length-kk suffix (the whole pattern included). HTH has overlaps of length 3 and 1, giving 8+28 + 2. HHH has 3, 2 and 1, giving 1414; HHT has only 3, giving 88.

Knowledge check

A fair ±1 walk starts at 0 and stops at −3 or +7. What is the expected number of steps?

Knowledge check

A walk steps up with probability 0.6 and down with probability 0.4, starting at 0 with no barriers. What is the probability it ever reaches −2?

Knowledge check

A fair walk from 0 is stopped when it first hits +1, so E[S at the stopping time] = 1, not 0. Why does optional stopping fail?

Key takeaways

  • Pick a martingale, stop it, and read off one equation: SnS_n and (q/p)Sn(q/p)^{S_n} give probabilities; Sn−nμS_n - n\mu and Sn2−nS_n^2 - n give durations.
  • Optional stopping needs a bounded time, a bounded stopped process, or a finite mean time with bounded steps. The fair walk hitting +1+1 and the doubling strategy fail all three.
  • Fair walk between −a-a and bb: P(b)=aa+bP(b) = \tfrac{a}{a + b}, E[τ]=ab\mathbb{E}[\tau] = ab. Biased: solve Prb+(1−P)r−a=1P r^{b} + (1 - P) r^{-a} = 1, then E[τ]=E[Sτ]/(p−q)\mathbb{E}[\tau] = \mathbb{E}[S_\tau]/(p - q).
  • One barrier with p>qp > q: E[τb]=b/(p−q)\mathbb{E}[\tau_b] = b/(p - q), Var⁡(τb)=4pqb/(p−q)3\operatorname{Var}(\tau_b) = 4pqb/(p - q)^3, and P(ever hit −a)=(q/p)aP(\text{ever hit } -a) = (q/p)^{a}.
  • Pattern waiting times: sum 2k2^{k} over the self-overlaps of the pattern (HTH: 10, HHH: 14).

Practice problems

Finished reading?

Answer the 3 knowledge checks to complete this lesson (0 done).