TExcelTAD
Home › Probability Theory › Random Walks

Random Walks

A random walk adds up independent steps: Sn=X1+⋯+XnS_n = X_1 + \cdots + X_n. The simplest one steps +1+1 or −1-1 with probability 1/21/2 each. It models a gambler's fortune, a stock price in the simplest models, a molecule diffusing, and the difference between two candidates' vote counts as ballots are counted. Its behaviour is full of surprises.

Distance from the start

The walk has E[Sn]=0E[S_n] = 0 and Var⁡(Sn)=n\operatorname{Var}(S_n) = n, so after nn steps it is typically about n\sqrt{n} from the start, and by the CLT Sn/nS_n/\sqrt{n} is approximately standard normal. Exact probabilities come from counting paths: S2m=0S_{2m} = 0 requires mm up-steps among 2m2m, so

u2m=Pr⁡[S2m=0]=(2mm)2−2m≈1πmu_{2m} = \Pr[S_{2m} = 0] = \binom{2m}{m} 2^{-2m} \approx \frac{1}{\sqrt{\pi m}}

using Stirling's formula. The chance of being back at 0 at a given time decays slowly, like 1/m1/\sqrt{m}.

python · try it yourself
from math import comb, sqrt, pi
import numpy as np

for m in [1, 5, 50, 500]:
    exact = comb(2 * m, m) / 4 ** m
    print(f"P(S_{2 * m} = 0) = {exact:.5f}   approx 1/sqrt(pi m) = {1 / sqrt(pi * m):.5f}")

rng = np.random.default_rng(1)
steps = rng.choice([-1, 1], size=(20_000, 400))
S = steps.cumsum(axis=1)
print("typical |S_400| (root mean square):", round(float(np.sqrt((S[:, -1] ** 2).mean())), 2), " sqrt(400) = 20")
P(S_2 = 0) = 0.50000   approx 1/sqrt(pi m) = 0.56419
P(S_10 = 0) = 0.24609   approx 1/sqrt(pi m) = 0.25231
P(S_100 = 0) = 0.07959   approx 1/sqrt(pi m) = 0.07979
P(S_1000 = 0) = 0.02523   approx 1/sqrt(pi m) = 0.02523
typical |S_400| (root mean square): 20.05  sqrt(400) = 20

Recurrence: Pólya's theorem

Does a walk return to its start with certainty? Let u=∑mu2mu = \sum_m u_{2m} be the expected number of returns. A walk that returns with probability f<1f < 1 makes a geometric number of returns with finite mean, while f=1f = 1 means infinitely many returns. So the walk is recurrent exactly when ∑u2m=∞\sum u_{2m} = \infty. In one dimension u2m∼1/πmu_{2m} \sim 1/\sqrt{\pi m}, whose sum diverges: the walk returns to 0 with probability 1, infinitely often.

In dd dimensions (each coordinate step ±1\pm 1 in a random direction), u2m∼c/md/2u_{2m} \sim c/m^{d/2}. The sum diverges for d=1,2d = 1, 2 and converges for d≥3d \ge 3. This is Pólya's theorem: "a drunk man will find his way home, but a drunk bird may get lost forever." In three dimensions, the return probability is only about 0.34.

python · try it yourself
import random

rng = random.Random(2)
def returns(d, steps=2000):
    pos = [0] * d
    for _ in range(steps):
        i = rng.randrange(d)
        pos[i] += rng.choice((-1, 1))
        if not any(pos):
            return True
    return False

for d in [1, 2, 3]:
    runs = 2000
    print(f"d={d}: returned within 2000 steps in {sum(returns(d) for _ in range(runs)) / runs:.3f} of walks")
d=1: returned within 2000 steps in 0.981 of walks
d=2: returned within 2000 steps in 0.691 of walks
d=3: returned within 2000 steps in 0.350 of walks

In one dimension almost every walk returns quickly; in two dimensions returns are certain but can take very long, so a finite simulation catches only some; in three dimensions about a third ever return.

The expected return time is infinite

Even in one dimension, the time TT until the first return to 0 has infinite mean: Pr⁡[T>2m]=u2m≈1/πm\Pr[T > 2m] = u_{2m} \approx 1/\sqrt{\pi m} decays too slowly for ∑Pr⁡[T>k]\sum \Pr[T > k] to converge. Returns are certain but can take extremely long, which leads to the long leads of the arc sine law.

python · try it yourself
import random

rng = random.Random(3)
def first_return(cap=100_000):
    s, t = 0, 0
    while True:
        s += 1 if rng.random() < 0.5 else -1
        t += 1
        if s == 0 or t >= cap:
            return t

times = sorted(first_return() for _ in range(2000))
print("median return time:", times[len(times) // 2])
print("mean of the sample:", round(sum(times) / len(times)))
print("longest:", times[-1])
median return time: 2
mean of the sample: 710
longest: 100000

Half the walks return within 2 steps, yet the sample mean is huge and keeps growing with the sample size: a heavy-tailed distribution.

In software

Random walks model diffusion in physics simulations, stock prices in finance (geometric random walks), and exploration in reinforcement learning. Graph algorithms use random walks to sample nodes, estimate PageRank, and build node embeddings (DeepWalk, node2vec). The n\sqrt{n} law tells how far a random-walk-based search drifts in nn steps.

Exercises

Exercise 1: Back to zero

What is the probability that a simple random walk is at 0 after 6 steps?

Answer

20/64=5/1620/64 = 5/16.

Solution

(63)2−6=20/64\binom{6}{3} 2^{-6} = 20/64.

Exercise 2: Where after 100 steps?

Approximately, what is the probability that a simple random walk is more than 20 steps from the start after 100 steps?

Answer

About 0.05.

Solution

S100S_{100} has standard deviation 10, so this is Pr⁡[∣Z∣>2]≈0.046\Pr[\lvert Z \rvert > 2] \approx 0.046. (With parity and continuity corrections the exact value is about 0.035.)

Exercise 3: Expected number of returns (code)

Compute the expected number of returns to 0 in the first 2N2N steps, ∑m=1Nu2m\sum_{m=1}^{N} u_{2m}, for N=10,100,1000,10000N = 10, 100, 1000, 10000, and compare with 2N/π2\sqrt{N/\pi}.

Answer

It grows like 2N/π2\sqrt{N/\pi}, without bound.

Solution

Use the ratio u2m=u2m−2⋅2m−12mu_{2m} = u_{2m-2} \cdot \frac{2m - 1}{2m} to avoid huge binomials.

python · try it yourself
import math

u, total = 1.0, 0.0
for m in range(1, 10_001):
    u *= (2 * m - 1) / (2 * m)
    total += u
    if m in (10, 100, 1000, 10_000):
        print(m, round(total, 3), round(2 * math.sqrt(m / math.pi), 3))
10 2.7 3.568
100 10.326 11.284
1000 34.696 35.682
10000 111.842 112.838

Test yourself

After nn steps, a simple random walk is typically how far from the start?

  1. about nn
  2. about n\sqrt{n}
  3. about log⁡n\log n
  4. 0

In which dimensions is the simple random walk recurrent (returns with probability 1)?

  1. Only 1
  2. 1 and 2
  3. 1, 2 and 3
  4. All dimensions