Random Walks
A random walk adds up independent steps: . The simplest one steps or with probability 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 and , so after steps it is typically about from the start, and by the CLT is approximately standard normal. Exact probabilities come from counting paths: requires up-steps among , so
using Stirling's formula. The chance of being back at 0 at a given time decays slowly, like .
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 be the expected number of returns. A walk that returns with probability makes a geometric number of returns with finite mean, while means infinitely many returns. So the walk is recurrent exactly when . In one dimension , whose sum diverges: the walk returns to 0 with probability 1, infinitely often.
In dimensions (each coordinate step in a random direction), . The sum diverges for and converges for . 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.
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 until the first return to 0 has infinite mean: decays too slowly for to converge. Returns are certain but can take extremely long, which leads to the long leads of the arc sine law.
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 law tells how far a random-walk-based search drifts in steps.
Exercises
Exercise 1: Back to zero
What is the probability that a simple random walk is at 0 after 6 steps?
Answer
.
Solution
.
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
has standard deviation 10, so this is . (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 steps, , for , and compare with .
Answer
It grows like , without bound.
Solution
Use the ratio to avoid huge binomials.
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 steps, a simple random walk is typically how far from the start?
- about
- about
- about
- 0
In which dimensions is the simple random walk recurrent (returns with probability 1)?
- Only 1
- 1 and 2
- 1, 2 and 3
- All dimensions