A walk of steps up and down
Toss a coin again and again. Each head moves a counter up by 1 and each tail moves it down by 1. The position of the counter after tosses is a random walk.
Write for the -th step: with probability and with probability , independently of the other steps. The walk starts at , and after steps it is at .
Rule: Random walk
Each step is independent of the others. With the walk is fair (or symmetric); otherwise it drifts up or down. Each is a random variable measured in steps.
The whole sequence is a process: one random variable for each time. One run of it is a path. The path below has 6 steps up and 4 down, so it ends at .
| step | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
|---|---|---|---|---|---|---|---|---|---|---|
| step | ||||||||||
| position | 1 | 0 | 0 | 1 | 2 | 3 | 2 | 1 | 2 |
Try it
rng.choice([-1, 1], size=10) draws ten fair steps with a fixed seed, and np.cumsum adds them up: each entry is the position after that many steps.
The last line counts the steps up and down and prints where the walk ends.
Where the walk can be after steps
If of the steps go up, the other go down, so . The number of steps up is a count of successes in independent trials, so (lesson 5.2.2).
For a fair walk of 4 steps, is 0 to 4 and is , , 0, 2 or 4:
| 0 | 2 | 4 | |||
|---|---|---|---|---|---|
| steps up | 0 | 1 | 2 | 3 | 4 |
Odd values are impossible. Each step changes the position by 1. So after an even number of steps the walk is at an even number, and after an odd number at an odd one. In particular can only happen when is even.
Rule: Distribution of the position
The second line needs between and with the same parity as , both even or both odd; otherwise . It is the binomial pmf at .
With the same five values have probabilities 0.0256, 0.1536, 0.3456, 0.3456 and 0.1296: the weight moves up. For a fair walk of 100 steps, : being back at the start is the single most likely position, but still unlikely.
Try it
binom.pmf(ups, n, p) gives the table for and , with ups for each value .
Then 100,000 seeded walks of 4 steps. In this run the shares are within 0.002 of , , , …, and no walk ends at 1.
Mean and variance: the spread
One step has mean . Its square is always 1, so , by the shortcut of lesson 5.3.2.
The position is a sum of independent steps. Means add (linearity, lesson 5.3.2), and so do variances, because the steps are independent.
Rule: Mean and variance after n steps
The variance grows in proportion to , so the standard deviation grows like . Four times as many steps give twice the spread. With steps of instead of , every position is multiplied by , and so is the standard deviation: for a fair walk.
| steps | 1 | 4 | 16 | 100 | 400 |
|---|---|---|---|---|---|
| standard deviation | 1 | 2 | 4 | 10 | 20 |
The figure shows eight fair walks of 400 steps. They spread out, but slowly: after 400 steps the standard deviation is 20 steps, not 400. By the central limit theorem (lesson 5.7.3), is close to normal for large . So about 95% of walks stay within (lesson 5.4.3's rule). For the exact chance of is 0.9648.
Eight seeded fair walks. The band of widens like a square root, not a straight line: after 400 steps it reaches only . Seven of the eight paths end inside it.
Try it
10,000 seeded walks of 100 steps, one per row of S. The columns for , 16 and 100 have means near 0 and variances near . In this run they are 3.92, 16.21 and 102.18, so the standard deviations are near 2, 4 and 10.
The last line compares the share of walks with with the exact 0.9648.
The average settles, the sum does not
The law of large numbers (lesson 5.7.2) says the average step settles on the mean step, 0 for a fair walk. The walk itself does the opposite. Its typical distance from the start, , grows like :
| steps | 10 | 100 | 1,000 |
|---|---|---|---|
| distance | 2.46 | 7.96 | 25.23 |
| average | 0.2461 | 0.0796 | 0.0252 |
Both hold at once: a distance of about , divided by , shrinks like . A fair walk does not come back towards 0 to make up for a run of heads. It has no reason to go either way.
Try it
The exact from the pmf: each times its binomial probability (LOTUS, lesson 5.3.1).
The last column, the distance over , settles near 0.8.
A walk with a drift
With each step has mean and variance . After 100 steps the mean is 10 and the standard deviation 9.95, about the same size, so is only 0.8173.
After 10,000 steps the mean is 1,000 and the standard deviation 99.5. The drift grows like and the spread only like , so in the long run the drift wins.
Try it
The mean and standard deviation for 100 and 10,000 steps, then : more than 50 of the 100 steps up.
In 10,000 seeded walks of 100 steps, the mean and standard deviation are both 9.94.
First passage: reaching a level
A first-passage question asks when the walk first reaches a level. Take a fair walk and the level . What is the chance that it gets there within 100 steps?
This is not . A walk can touch 10 at step 40 and end below it. Counting those paths too, the chance is 0.3197, almost twice as large.
Simulation answers such questions directly: run many walks and count those that reach the level. For an exact answer, keep the probability of every position. At each step move half of each probability up and half down, and set aside what arrives at .
| within steps | 100 | 1,000 | 10,000 |
|---|---|---|---|
| 0.3197 | 0.752 | 0.9203 |
A fair walk reaches in the end with probability 1, and any other level too. But the wait can be very long: about 8% of walks are still short of it after 10,000 steps. The average waiting time is in fact infinite, which is why the table, not a mean, describes it.
Try it
prob holds the probability of each position from to . Each step shifts half of it up and half down; whatever lands on joins reached and stops.
Then 20,000 seeded walks of 100 steps. In this run 0.3172 reach 10 at some step, against 0.3197, while 0.1841 end at 10 or above.
The gambler's ruin
A gambler starts with dollars and bets $1 at a time on a fair coin. She stops when she has dollars or nothing. Her fortune is a fair walk started at that stops at 0 or at . What is the chance she reaches first?
Call it . Condition on the first step: up to or down to , each with probability . By the law of total probability (lesson 5.1.3), this first-step analysis gives
with and . Each value is the average of its two neighbours, so the points lie on a straight line from 0 to 1.
Rule: Gambler's ruin, fair walk
For a fair walk started at , with . The second line comes from the same first-step analysis: each step adds 1, so , with .
From with , she reaches $10 with probability 0.3 and goes broke with probability 0.7. The game lasts bets on average. The fair game gives no edge: her expected fortune at the end is , what she started with.
Try it
The first-step equations for , one per starting point, solved at once with np.linalg.solve: first for , then for the mean number of steps . They give and .
game plays the walk for 100,000 seeded gamblers. In this run 0.2989 reach $10, after 20.99 bets on average.
A small edge decides a long game
The straight line holds only for a fair walk. With the same first-step analysis has a different solution, stated here: with ,
Start at 50 with . A fair coin gives 0.5. A coin with , an edge of one percentage point against the gambler, gives only 0.1192, and gives 0.0025.
The chance of reaching 100 before 0. Fair, it is a straight line; with a small bias against the walker the curve sags far below it, because a long game gives the bias many steps to act.
In a short game the edge matters less: from 5 with , still gives 0.4502. The longer the game, the more the drift of outweighs the spread, as in the section on drift.
Try it
reach_b is the formula, with for a fair walk. It is evaluated from 50 with and from 5 with .
Then 2,000 seeded gamblers with , from 50 to 100 or 0. In this run 0.1165 of them reach 100, against 0.1192, after 1,953.3 bets on average.
Lesson 5.11.2 writes the gambler's walk as a Markov chain and solves for the same way. Case study 5.11.4 uses a walk with steps of : the log of a stock price on a binomial tree.
Common mistakes
- The spread grows like , not . Variances add; standard deviations do not. 100 fair steps have standard deviation 10, not 100.
- Parity. After an odd number of steps the walk cannot be at 0, or at any even number.
- The average settles; the walk does not. for a fair walk, while grows like . A run of heads is not "made up" later.
- Reaching a level is not ending above it. Within 100 steps a fair walk reaches with probability 0.3197; it ends at 10 or above with probability only 0.1841.
- Certain is not soon. A fair walk reaches every level in the end, but the average wait is infinite.
- is for a fair walk only. One point of bias turns 0.5 into 0.1192 over a long game.
Random walks
0 of 12 correct
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
How to type answers
- Powers with
^:x^7,2^(1/3),a^-2. - Multiply with
*or by writing side by side:3x^2,2(x+1). ln(x)is the natural log,log(x, 2)is log base 2,eis Euler's number,sqrt(x)is a square root.sin(x),cos(x)andtan(x)take radians; writepifor π andsin(x)^2for sin²x.- Your first answer to each question is saved: it counts towards your score and brings the question back for review.
- Exact answers like
ln(20)/ln(3)are best. Decimals need about 4 significant figures.
Summary card
- A random walk is with independent steps of (probability ) or . The number of steps up is , so has the parity of .
- and . A fair walk has mean 0 and standard deviation ; steps of give . A drift grows like and wins in the long run.
- The average step settles on the mean; the distance grows like .
- First passage: reaching a level within steps is more likely than ending above it (0.3197 against 0.1841 for in 100 fair steps). A fair walk gets there in the end, after a wait with infinite mean.
- Gambler's ruin, fair walk from between 0 and : , after steps on average, both by first-step analysis. A small bias changes a long game completely.