Quant from Scratch by QuantWright

Introduction to Quant Finance · Lesson 2

Random walks

Why a running total of coin flips looks like a price chart, and why its spread grows with the square root of time.

Know the basics? Read the lesson notes

One coin a day

Picture a made-up price that starts at €100. Every day a fair coin (heads and tails equally likely) decides its move: heads, it goes up €1; tails, it goes down €1. Nothing else moves it. Take a few days one at a time, then walk the rest.

Walk #1: a made-up price, one coin flip a day (simulated)

Heads: up €1. Tails: down €1. The dashed line is the €100 it started at.

Day0
Heads0
Tails0
Price€100

Walk #1 had 193 heads and 207 tails, so it ended at €100 + 193 − 207 = €86. The price on any day is the start plus every step so far. A running total of random steps like this is called a random walk.

Walk again

Same coin, same rules, new flips. Press Walk again to see what other walks of 400 days look like.

Walks of 400 days, each from €100 (simulated)

The newest walk is coloured; earlier ones stay in grey.

Walk#1
Highest€100
Lowest€86
Ends at€86

Walk #3 climbed from €93 on day 55 to €136 on day 360. Walk #4 fell from €120 on day 160 to €100 on day 340. On a chart, one looks like an uptrend and the other like a long slide from day 160 to day 340, yet every day of every walk was the same fair coin.

A coin has no memory, so a run of heads says nothing about the next flip. But the run stays in the running total, and nothing pulls the price back to €100. A walk keeps whatever luck it had, and luck that piles up one way looks like a trend.

1,000 walks at once

One walk tells you little. Here are 1,000, each with its own, separate coin flips, all starting at €100. To say how far they spread, the numbers under the chart show the typical distance from €100: square each walk's distance from €100 (multiply it by itself, so €3 below counts like €3 above) and average those squares. Then take the square root, the number that, multiplied by itself, gives that average: √100 = 10 because 10 × 10 = 100.

1,000 walks, the first 100 days (simulated)

Each faint line is one walk. Darker means more walks pass there.

Day0
Typical distance from €100€0

On day 1 every walk is exactly €1 away, so the typical distance is €1. On day 25 it is €5.03, and by day 100 it is €9.94: some walks are much further out, some are back at €100.

Four times as long

On day 100 the typical distance is about €10. Now the same 1,000 walks keep going to day 400: four times as many days. How far from €100 will a typical walk be on day 400?

Keep your guess in mind. On the next screen the walks keep going.

Twice as far, not four times

Here are the same walks on day 100. Keep them walking.

1,000 walks, 400 days (simulated)

The coloured curves sit the typical distance above and below €100, worked out from the day number: you'll see how in a moment.

Day100
Typical distance€9.94
Square root of the day10

Look at the pattern. On day 25 the typical distance is about €5, on day 100 about €10, on day 400 about €20. And 5 × 5 = 25, 10 × 10 = 100, 20 × 20 = 400: the typical distance is close to the square root of the number of days, written √days. The curves are €100 plus and minus √day. They mark the typical distance, not a limit: about 3 walks in 10 end further out (the next lesson, the bell curve, shows why).

Why not four times? Many of the extra days cancel each other: an up day undoes a down day. Only the part that doesn't cancel moves the walk away (the theory card shows why).

Bigger steps

So far every step was €1. Give the same 1,000 walks, with the same coin flips, bigger or smaller steps and watch the fan.

The same 1,000 walks with a different step size (simulated)

Curves: €100 plus and minus the step size × √day. Walks that leave the chart keep going off it.

Typical distance on day 100–
Typical distance on day 400–
Step size × √400–

With €2 steps, the typical distance on day 400 is €39.49, twice the €19.75 with €1 steps (to within rounding), because every move is twice as big. So the typical distance is about the step size × the square root of the number of days.

A strong uptrend

From the 1,000 walks, take the 100 that rose the most between day 250 and day 400. Each climbed between €18 and €36 in those 150 days. On a chart, every one of them looks like a strong uptrend.

The 100 walks that rose most from day 250 to day 400 (simulated)

Each line is one walk's change in price since day 250.

What will these 100 walks do over the next 100 days?

Keep your guess in mind. On the next screen they walk on.

The next 100 days

The same 100 walks, with the same coin as before. Let them walk to day 500.

The same 100 walks, days 250 to 500 (simulated)

Grey: the climb you picked them for. Coloured: what came next.

Rose, day 400 to 500–
Fell, day 400 to 500–
Ended level–
Average change–

The climb said nothing about what came next. Each day is still a fair coin, so the walks don't keep rising, and nothing pulls them back down either. They just spread out again from wherever they had got to.

A support level

Chart readers draw support: a level the price keeps falling to and bouncing up from. Here is walk #1 again. Slide the line and count its touches: days the price fell to the line and went up the next day. Touches count only if they are at least 10 days apart, so one wobble isn't counted many times.

Walk #1 and a level of your choice (simulated)

Dots mark the touches.

Touches0

At €95, walk #1 fell to the line and turned up 11 times. A chart reader would call that strong support. But the coin has no idea where €95 is. In 400 days walk #1 never left the 15 levels from €86 to €100, so it kept landing on the same ones.

This is not a fluke of walk #1: every one of the 1,000 walks has a level it touched at least 3 times, and 920 have one it touched 5 times or more.

Theory

The random walk and the square-root rule

A random walk is a running total of random steps. When each step is the same size, as likely up as down, and doesn't depend on the steps before it, the typical distance from the start grows with the square root of the number of steps. This is the square-root rule (for prices, the square-root-of-time rule). The typical distance is the square root of the average squared distance; squaring makes a distance below the start count like one above, so they don't cancel.

Pn = P0 + x1 + x2 + … + xn
dn = s × √n
Pn
the price after n steps; P0 is where it started
x1 … xn
the steps, each +s or −s
s
the step size
n
the number of steps (days)
dn
the typical distance from the start after n steps: the square root of the average squared distance (the next lesson gives it its textbook name)

Why the square root: take a walk that is D above the start (D is negative below it). One more step takes it to D + s or D − s, equally likely. Squared, those are D×D + 2×D×s + s×s and D×D − 2×D×s + s×s, which average D×D + s×s. So each step adds s×s to the squared distance on average: after n steps the average squared distance is n×s×s, and its square root is s × √n. Averaged over every possible walk, this is exact; a real fan of 1,000 walks only comes close (€19.75 against €20).

Worked example

Two €1 steps: HH, HT, TH and TT end at +2, 0, 0 and −2. Squared: 4, 0, 0, 4, which average 2 (= 2 steps × 1 × 1). The typical distance is √2 ≈ €1.41. Your fan: s = €1 and n = 400, so 1 × √400 = €20; the 1,000 walks measured €19.75.

Holds when
  • each step ignores the ones before it (the steps are independent);
  • up and down are equally likely, so the steps average 0;
  • the steps have the same typical size (if sizes differ, use their typical size for s).
Breaks when

Steps that lean one way add a drift, a steady push that grows with n, faster than √n (lesson A1, Expectancy). Real daily moves come in calm and wild spells, so the typical size changes over time (lesson C2, Volatility clustering).

A made-up price moves €2 a day, up or down on a fair coin. What is its typical distance from the start after 49 days?

s × √n = €2 × √49 = €2 × 7 = €14.
s × √n = €2 × √49 = €2 × 7 = €14, because 7 × 7 = 49. Not 2 × 49 = €98: most of the 49 steps cancel.

What this means for your backtest

A backtest runs a trading rule over past prices to see what it would have made. Three things follow from this lesson. Turn them over one at a time.

  1. A trend on a chart is not evidence on its own.

    Coin flips drew a €43 climb over 305 days. A rule that "rides trends" has to do better than it would on coin walks, where riding the past climb told you nothing.

Practice 1 of 3

A friend's coin-flip strategy

The situationA friend's strategy has no advantage: on average it makes nothing. Each trade wins or loses €50, 50–50, like a coin. Their account is a random walk with €50 steps.

After 100 trades, how far from where it started will the account typically be? Type the typical distance in euros.

Hint: the step size is €50 and there are 100 steps. Typical distance = step size × √steps.

€50 × √100 = €50 × 10 = €500.
€50 × √100 = €50 × 10 = €500. Not €5,000 (50 × 100): wins and losses mostly cancel.
Practice 2 of 3

Four times as many trades

The situationThe same friend plans to run the strategy for 400 trades instead of 100. They say luck will then swing the account four times as far, about €2,000.

What is luck's typical distance after 400 trades?

€50 × √400 = €50 × 20 = €1,000. Four times the trades, twice the distance, because √4 = 2.
That would be true only if every trade went the same way. Most cancel: €50 × √400 = €50 × 20 = €1,000, twice the €500 of 100 trades.
Luck per trade does shrink (€1,000 over 400 trades is €2.50 a trade, against €5 a trade over 100), but in euros it grows: €50 × √400 = €1,000.
Practice 3 of 3

"This market trends and respects levels"

The situationA course seller shows a chart: a steady climb over 200 days, and a level the price fell to and bounced up from four times. They call it proof that the market trends and respects levels.

What does the chart alone show?

Coin walks drew both on this page: walk #3 climbed €43 over 305 days, and every one of the 1,000 walks had a level touched 3 or more times.
Coin walks drew both on this page: walk #3 climbed €43 over 305 days, and every one of the 1,000 walks had a level touched 3 or more times. A chart can't tell this from chance.
The climb is no stronger evidence than the level: walk #3 climbed €43 over 305 days on pure coin flips.
It doesn't prove the market is a coin either. The test is whether a rule built on the pattern works on prices it was not drawn from.
✓

Lesson done

Add up random steps and you get a random walk: a line that climbs, falls and keeps returning to the same levels, all by chance. Its typical distance from the start grows with the square root of time, so four times as long means about twice as far. A trend or a support level on a chart is not evidence on its own.

Key ideas

Random walk
a running total of random steps: the price is the start plus every step so far
Independent steps
each step ignores the ones before it, like a fair coin
Typical distance
the square root of the average squared distance from the start (named in the next lesson)
Square root (√)
the number that, multiplied by itself, gives the number: √400 = 20
Square-root rule
with independent, same-size steps that average 0, the typical distance grows with √steps
Support
a level a price keeps falling to and bouncing up from; coin walks draw them too

Formulas

Pn = P0 + x1 + … + xn  ·  dn = s × √n

4 times the steps: 2 times the distance. 9 times the steps: 3 times.

Lesson notes

A random walk is a running total of random steps. Its typical distance from the start (square each walk's distance, average the squares, take the square root) grows with the square root of the number of steps: four times the steps, about twice as far. Trends and support levels on a chart can come from chance alone.

One coin a day

A made-up price starts at €100. Each day a fair coin (heads and tails equally likely) moves it: heads, up €1; tails, down €1. The price is the start plus every step so far: a random walk. The lesson's first simulated walk of 400 days had 193 heads and 207 tails, ending at €100 + 193 − 207 = €86.

Luck that piles up looks like a trend

With the same coin and new flips, simulated walk #3 climbed from €93 on day 55 to €136 on day 360: €43 in 305 days, an uptrend on a chart, from the same fair coin every day. A run of heads stays in the running total, and nothing pulls the price back to €100.

The square-root rule

Now take 1,000 walks, all starting at €100. Their typical distance from €100 was €1 on day 1, €5.03 on day 25, €9.94 on day 100 and €19.75 on day 400. Since 5 × 5 = 25, 10 × 10 = 100 and 20 × 20 = 400, it is close to √days, the number that multiplied by itself gives the days. Four times the days gives twice the distance, because many extra days cancel: an up day undoes a down day. √days marks the typical distance, not a limit: about 3 walks in 10 end further out than €20 on day 400. With €2 steps the day-400 distance is €39.49, twice the €19.75 with €1 steps (to within rounding). So it is about the step size × √days.

A trend doesn't continue

Take the 100 of the 1,000 walks that rose most from day 250 to day 400, each by €18 to €36. The lesson then let those same 100 walks continue from day 400 to day 500, with the same flips: 45 rose, 46 fell and 9 ended level, with an average change of €0.24. They neither kept rising nor fell back.

Support levels

Support is a level a price keeps falling to and bouncing up from. A touch is a day the price fell to the level and rose the next day (it stood at the level, €1 higher the day before and €1 higher the day after), counted only if at least 10 days after the last. Walk #1 had 11 touches at €95 in 400 days, but it never left the 15 levels from €86 to €100, so it kept landing on the same ones. Every one of the 1,000 walks had a level touched at least 3 times, and 920 had one touched 5 times or more.

What this means for a backtest

A backtest runs a trading rule over past prices to see what it would have made. A rule that "rides trends" must beat what it does on coin walks. Test a level on prices it wasn't drawn on. For an account with no advantage (it makes nothing on average) and independent trades of similar size, four times the trades puts luck about twice as far. This doesn't say real prices are coin flips.

Formula: the random walk and the square-root rule

Take a walk D above its start (negative below). One more step of size s gives D + s or D − s, equally likely. Squared, these average D×D + s×s (the 2×D×s parts cancel). So each step adds s×s to the average squared distance: after n steps n×s×s, with square root s × √n. That is exact when averaged over all equally likely walks; 1,000 walks come close (€19.75 against €20).

Pn = P0 + x1 + x2 + … + xn
dn = s × √n
Pn
the price after n steps; P0 is where it started
x1 … xn
the steps, each +s or −s
s
the step size
n
the number of steps (days)
dn
the typical distance from the start after n steps: the square root of the average squared distance
Worked example: the lesson's walks

Two €1 steps: HH, HT, TH and TT end at +2, 0, 0 and −2. Squared: 4, 0, 0, 4, which average 2 (= 2 steps × 1 × 1). The typical distance is √2 ≈ €1.41. In the lesson's 1,000 walks, s = €1 and n = 400, so 1 × √400 = €20; the 1,000 walks measured €19.75.

Holds whenBreaks when

Steps that lean one way add a drift, a steady push that grows with n, faster than √n. Real daily moves come in calm and wild spells, so the typical size changes over time.

One more worked example, on new numbers

A made-up price moves €3 a day, up or down on a fair coin. After 16 days its typical distance from the start is 3 × √16 = 3 × 4 = €12. After 64 days, four times as many, it is 3 × √64 = 3 × 8 = €24: twice as far.

Common mistakes

Check yourself

  1. A made-up price moves €5 a day, up or down on a fair coin. What is its typical distance from the start after 25 days?
    AnswerStep size × √days = €5 × √25 = €5 × 5 = €25.
  2. With €2 steps, after how many days is the typical distance €24?
    Answer2 × √n = 24, so √n = 12 and n = 12 × 12 = 144 days.
  3. With €1 steps the typical distance after 100 days is about €10. About how far after 400 days?
    AnswerAbout €20: four times the days gives twice the distance, because √4 = 2, as many of the extra days cancel.
  4. A chart shows a steady climb and a level the price bounced from four times. A seller calls it proof that the market trends. What does the chart alone show?
    AnswerLittle. Coin walks draw both: walk #3 climbed €43 in 305 days, and every one of the 1,000 walks had a level touched at least 3 times, and 920 had one touched 5 times or more. Four touches is fewer than that, so it is ordinary for a coin walk.

Try it yourself

Run the simulation. Every walk and number in this lesson comes from one Python script with a fixed random seed (below). Change the step size, the number of days or the number of walks and see what moves.

Walk by hand. Start at 100. Flip a coin 25 times, adding 1 for heads and taking 1 for tails, and write down where you end. Do it 10 times. Your typical distance should come out near √25 = 5. In a spreadsheet: put =IF(RAND()<0.5,1,-1) in A1:A400, 100 in B1, =B1+A1 in B2 and copy it down to B401, then chart column B. Press F9 to walk again.

The code behind every number (Python, seed 2026110908)
Loading…

Going further: a coin that leans

Give the coin a small lean: 52% heads, 48% tails. The average step is now 0.52 × €1 − 0.48 × €1 = +€0.04, so the middle of the fan drifts up by €0.04 a day, while the spread still grows about like √days.

After 100 days the drift is €4 and the spread about €10: the lean hides inside luck. After 625 days both are about €25. After 2,500 days the drift is €100 and the spread about €50. Drift grows with time itself; luck grows only with its square root. Given enough steps, a real lean shows through. How many steps that takes is the subject of lesson A10, Sample size.

For the preciseTypical distance dn = √(average of (Pn − P0)2) over the 1,000 walks, dividing by 1,000. Exactly, the expected squared distance is n × s2, because (S + x)2 = S2 + 2Sx + x2 and 2Sx averages 0 when x is ±s with equal chance, whatever S is. Measured: €1.00 on day 1, €1.98 on day 4, €5.03 on day 25, €9.94 on day 100, €19.75 on day 400 (ratio 1.99). The plain average of the distances is smaller, about 0.8 × s × √n (approaching √(2n ÷ π) × s for large n): measured €7.89 on day 100 and €15.86 on day 400, the same doubling. With a lean, the spread is s × √(n × (1 − m2)) for average step m × s: 9.99, 24.98 and 49.96 for the three lengths above.
For the precise: how the walks were picked and countedWalks #2 to #4 were not picked by eye: the text describes, among walks #1 to #4, the one with the biggest climb (walk #3, +€43 from day 55 to day 360) and the one with the biggest fall (walk #4, −€20 from day 160 to day 340). The 100 risers are the walks with the biggest change from day 250 to day 400 (+€18 to +€36, average +€22.62; ties by walk number); over days 400 to 500, 45 rose, 46 fell and 9 ended level, average +€0.24. The 100 walks that fell most from day 250 to day 400 did not bounce back either: average change over the next 100 days +€0.86 (53 rose, 39 fell). A touch of level L is a day t with price L on day t and L + 1 on days t − 1 and t + 1, counted from day 1 onward when at least 10 days after the level's previous counted touch. Walk #1's touches by level, €86 to €100: 2, 4, 2, 5, 4, 5, 6, 8, 10, 11, 5, 3, 1, 0, 0. Most-touched level per walk: at least 3 touches in 1,000 walks, at least 4 in 993, at least 5 in 920 (median 6). With €2 steps, 10 of the 1,000 walks fall below €0 at some point in 400 days; with €1.50 steps or less, none do. Prices with €1 steps stayed between €37 and €156.

Honest notes

Where this comes from