SigmaField
Mathematical Foundations for Quant Finance
0 of 256 lessons
      1. 5.11.1 Random walksConcept · 30 min
      2. 5.11.2 Markov chainsConcept · 35 min
      3. 5.11.3 Practice set: discrete-time processesPractice set · 25 min
      4. 5.11.4 Case study: pricing on a multi-step binomial treeCase study · 35 min

Level 5, Probability and statistics 5.11 Discrete-time processes Lesson 1 of 4

ConceptLesson 5.11.1 in Level 5, Probability and statistics. About 30 minutes.

Random walks

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 nn tosses is a random walk.

Write XiX_i for the ii-th step: +1+1 with probability pp and −1-1 with probability 1−p1 - p, independently of the other steps. The walk starts at S0=0S_0 = 0, and after nn steps it is at SnS_n.

Rule: Random walk

Sn=X1+X2+⋯+XnP(Xi=1)=p,P(Xi=−1)=1−p\begin{gathered} S_n = X_1 + X_2 + \cdots + X_n \\[4pt] P(X_i = 1) = p, \quad P(X_i = -1) = 1 - p \end{gathered}

Each step is independent of the others. With p=12p = \frac{1}{2} the walk is fair (or symmetric); otherwise it drifts up or down. Each SnS_n is a random variable measured in steps.

The whole sequence S0,S1,S2,…S_0, S_1, S_2, \ldots 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 S10=6−4=2S_{10} = 6 - 4 = 2.

step nn12345678910
step XnX_n+1+1−1-1−1-1+1+1+1+1+1+1+1+1−1-1−1-1+1+1
position SnS_n10−1-10123212

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.

⌘/Ctrl + Enter runs the cell

Where the walk can be after nn steps

If UU of the nn steps go up, the other n−Un - U go down, so Sn=U−(n−U)=2U−nS_n = U - (n - U) = 2U - n. The number of steps up is a count of successes in nn independent trials, so U∼Bin⁡(n,p)U \sim \operatorname{Bin}(n, p) (lesson 5.2.2).

For a fair walk of 4 steps, UU is 0 to 4 and S4S_4 is −4-4, −2-2, 0, 2 or 4:

S4S_4−4-4−2-2024
steps up UU01234
P(S4=k)P(S_4 = k)116\frac{1}{16}416\frac{4}{16}616\frac{6}{16}416\frac{4}{16}116\frac{1}{16}

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 Sn=0S_n = 0 can only happen when nn is even.

Rule: Distribution of the position

Sn=2U−n,U∼Bin⁡(n,p)P(Sn=k)=(nn+k2) pn+k2 (1−p)n−k2\begin{gathered} S_n = 2U - n, \quad U \sim \operatorname{Bin}(n, p) \\[4pt] P(S_n = k) = \binom{n}{\frac{n + k}{2}}\, p^{\frac{n + k}{2}}\,(1 - p)^{\frac{n - k}{2}} \end{gathered}

The second line needs kk between −n-n and nn with the same parity as nn, both even or both odd; otherwise P(Sn=k)=0P(S_n = k) = 0. It is the binomial pmf at U=n+k2U = \frac{n + k}{2}.

With p=0.6p = 0.6 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, P(S100=0)=(10050)/2100=0.0796P(S_{100} = 0) = \binom{100}{50}\big/2^{100} = 0.0796: 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 p=0.5p = 0.5 and p=0.6p = 0.6, with ups =n+k2= \frac{n + k}{2} for each value kk.

Then 100,000 seeded walks of 4 steps. In this run the shares are within 0.002 of 116\frac{1}{16}, 416\frac{4}{16}, 616\frac{6}{16}, …, and no walk ends at 1.

⌘/Ctrl + Enter runs the cell

Mean and variance: the n\sqrt{n} spread

One step has mean E[X]=1⋅p+(−1)(1−p)=2p−1\mathbb{E}[X] = 1 \cdot p + (-1)(1 - p) = 2p - 1. Its square is always 1, so Var⁡(X)=1−(2p−1)2=4p(1−p)\operatorname{Var}(X) = 1 - (2p - 1)^2 = 4p(1 - p), by the shortcut of lesson 5.3.2.

The position is a sum of nn 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

E[Sn]=n(2p−1)Var⁡(Sn)=4np(1−p)fair walk: E[Sn]=0,σSn=n\begin{gathered} \mathbb{E}[S_n] = n(2p - 1) \\[4pt] \operatorname{Var}(S_n) = 4np(1 - p) \\[4pt] \text{fair walk: } \mathbb{E}[S_n] = 0, \quad \sigma_{S_n} = \sqrt{n} \end{gathered}

The variance grows in proportion to nn, so the standard deviation grows like n\sqrt{n}. Four times as many steps give twice the spread. With steps of ±h\pm h instead of ±1\pm 1, every position is multiplied by hh, and so is the standard deviation: hnh\sqrt{n} for a fair walk.

steps nn1416100400
standard deviation n\sqrt{n}1241020

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), SnS_n is close to normal for large nn. So about 95% of walks stay within ±2n\pm 2\sqrt{n} (lesson 5.4.3's rule). For n=100n = 100 the exact chance of ∣S100∣≤20\lvert S_{100} \rvert \le 20 is 0.9648.

Eight seeded fair walks. The band of ±2n\pm 2\sqrt{n} widens like a square root, not a straight line: after 400 steps it reaches only ±40\pm 40. 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 n=4n = 4, 16 and 100 have means near 0 and variances near nn. 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 ∣S100∣≤20\lvert S_{100} \rvert \le 20 with the exact 0.9648.

⌘/Ctrl + Enter runs the cell

The average settles, the sum does not

The law of large numbers (lesson 5.7.2) says the average step Snn\frac{S_n}{n} settles on the mean step, 0 for a fair walk. The walk itself does the opposite. Its typical distance from the start, E∣Sn∣\mathbb{E}\lvert S_n \rvert, grows like n\sqrt{n}:

steps nn101001,000
distance E∣Sn∣\mathbb{E}\lvert S_n \rvert2.467.9625.23
average E∣Sn∣/n\mathbb{E}\lvert S_n \rvert / n0.24610.07960.0252

Both hold at once: a distance of about 0.8n0.8\sqrt{n}, divided by nn, shrinks like 1n\frac{1}{\sqrt{n}}. 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 E∣Sn∣\mathbb{E}\lvert S_n \rvert from the pmf: each ∣2k−n∣\lvert 2k - n \rvert times its binomial probability (LOTUS, lesson 5.3.1).

The last column, the distance over n\sqrt{n}, settles near 0.8.

⌘/Ctrl + Enter runs the cell

A walk with a drift

With p=0.55p = 0.55 each step has mean 0.10.1 and variance 4×0.55×0.45=0.994 \times 0.55 \times 0.45 = 0.99. After 100 steps the mean is 10 and the standard deviation 9.95, about the same size, so P(S100>0)P(S_{100} > 0) is only 0.8173.

After 10,000 steps the mean is 1,000 and the standard deviation 99.5. The drift grows like nn and the spread only like n\sqrt{n}, so in the long run the drift wins.

Try it

The mean n(2p−1)n(2p - 1) and standard deviation 4np(1−p)\sqrt{4np(1 - p)} for 100 and 10,000 steps, then P(S100>0)P(S_{100} > 0): 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.

⌘/Ctrl + Enter runs the cell

First passage: reaching a level

A first-passage question asks when the walk first reaches a level. Take a fair walk and the level +10+10. What is the chance that it gets there within 100 steps?

This is not P(S100≥10)=0.1841P(S_{100} \ge 10) = 0.1841. 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 +10+10.

within nn steps1001,00010,000
P(reach +10)P(\text{reach } {+10})0.31970.7520.9203

A fair walk reaches +10+10 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 −10,000-10{,}000 to +10+10. Each step shifts half of it up and half down; whatever lands on +10+10 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.

⌘/Ctrl + Enter runs the cell

The gambler's ruin

A gambler starts with aa dollars and bets $1 at a time on a fair coin. She stops when she has bb dollars or nothing. Her fortune is a fair walk started at aa that stops at 0 or at bb. What is the chance she reaches bb first?

Call it h(a)h(a). Condition on the first step: up to a+1a + 1 or down to a−1a - 1, each with probability 12\frac{1}{2}. By the law of total probability (lesson 5.1.3), this first-step analysis gives

h(a)=12 h(a+1)+12 h(a−1)h(a) = \tfrac{1}{2}\,h(a + 1) + \tfrac{1}{2}\,h(a - 1)

with h(0)=0h(0) = 0 and h(b)=1h(b) = 1. 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

P(reach b before 0)=abE[steps until a stop]=a(b−a)\begin{gathered} P(\text{reach } b \text{ before } 0) = \frac{a}{b} \\[4pt] \mathbb{E}[\text{steps until a stop}] = a(b - a) \end{gathered}

For a fair walk started at aa, with 0≤a≤b0 \le a \le b. The second line comes from the same first-step analysis: each step adds 1, so m(a)=1+12m(a+1)+12m(a−1)m(a) = 1 + \frac{1}{2}m(a + 1) + \frac{1}{2}m(a - 1), with m(0)=m(b)=0m(0) = m(b) = 0.

From a=3a = 3 with b=10b = 10, she reaches $10 with probability 0.3 and goes broke with probability 0.7. The game lasts 3×7=213 \times 7 = 21 bets on average. The fair game gives no edge: her expected fortune at the end is 0.3×10=30.3 \times 10 = 3, what she started with.

Try it

The first-step equations for b=10b = 10, one per starting point, solved at once with np.linalg.solve: first for hh, then for the mean number of steps mm. They give a10\frac{a}{10} and a(10−a)a(10 - a).

game plays the walk for 100,000 seeded gamblers. In this run 0.2989 reach $10, after 20.99 bets on average.

⌘/Ctrl + Enter runs the cell

A small edge decides a long game

The straight line ab\frac{a}{b} holds only for a fair walk. With p≠12p \neq \frac{1}{2} the same first-step analysis has a different solution, stated here: with ρ=1−pp\rho = \frac{1 - p}{p},

P(reach b before 0)=1−ρa1−ρbP(\text{reach } b \text{ before } 0) = \frac{1 - \rho^{a}}{1 - \rho^{b}}

Start at 50 with b=100b = 100. A fair coin gives 0.5. A coin with p=0.49p = 0.49, an edge of one percentage point against the gambler, gives only 0.1192, and p=0.47p = 0.47 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 b=10b = 10, p=0.49p = 0.49 still gives 0.4502. The longer the game, the more the drift of n(2p−1)n(2p - 1) outweighs the n\sqrt{n} spread, as in the section on drift.

Try it

reach_b is the formula, with ab\frac{a}{b} for a fair walk. It is evaluated from 50 with b=100b = 100 and from 5 with b=10b = 10.

Then 2,000 seeded gamblers with p=0.49p = 0.49, 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.

⌘/Ctrl + Enter runs the cell

Lesson 5.11.2 writes the gambler's walk as a Markov chain and solves for hh the same way. Case study 5.11.4 uses a walk with steps of ±σΔt\pm\sigma\sqrt{\Delta t}: the log of a stock price on a binomial tree.

Common mistakes

  1. The spread grows like n\sqrt{n}, not nn. Variances add; standard deviations do not. 100 fair steps have standard deviation 10, not 100.
  2. Parity. After an odd number of steps the walk cannot be at 0, or at any even number.
  3. The average settles; the walk does not. Snn→0\frac{S_n}{n} \to 0 for a fair walk, while ∣Sn∣\lvert S_n \rvert grows like n\sqrt{n}. A run of heads is not "made up" later.
  4. Reaching a level is not ending above it. Within 100 steps a fair walk reaches +10+10 with probability 0.3197; it ends at 10 or above with probability only 0.1841.
  5. Certain is not soon. A fair walk reaches every level in the end, but the average wait is infinite.
  6. ab\frac{a}{b} 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. 1.
  2. 2.
  3. 3.
  4. 4.
  5. 5.
  6. 6.
  7. 7.
  8. 8.
  9. 9.
  10. 10.
  11. 11.
  12. 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, e is Euler's number, sqrt(x) is a square root.
  • sin(x), cos(x) and tan(x) take radians; write pi for π and sin(x)^2 for 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 Sn=X1+⋯+XnS_n = X_1 + \cdots + X_n with independent steps of +1+1 (probability pp) or −1-1. The number of steps up is Bin⁡(n,p)\operatorname{Bin}(n, p), so Sn=2U−nS_n = 2U - n has the parity of nn.
  • E[Sn]=n(2p−1)\mathbb{E}[S_n] = n(2p - 1) and Var⁡(Sn)=4np(1−p)\operatorname{Var}(S_n) = 4np(1 - p). A fair walk has mean 0 and standard deviation n\sqrt{n}; steps of ±h\pm h give hnh\sqrt{n}. A drift grows like nn and wins in the long run.
  • The average step Snn\frac{S_n}{n} settles on the mean; the distance ∣Sn∣\lvert S_n \rvert grows like n\sqrt{n}.
  • First passage: reaching a level within nn steps is more likely than ending above it (0.3197 against 0.1841 for +10+10 in 100 fair steps). A fair walk gets there in the end, after a wait with infinite mean.
  • Gambler's ruin, fair walk from aa between 0 and bb: P(reach b first)=abP(\text{reach } b \text{ first}) = \frac{a}{b}, after a(b−a)a(b - a) steps on average, both by first-step analysis. A small bias changes a long game completely.

Free further reading

Optional. This lesson covers what you need; these add more examples and practice. All are free to read.