Quant from Scratch by QuantWright

Introduction to Quant Finance · Lesson 4

Probability with coins

What a streak does and doesn't mean: after five heads in a row, is tails due?

Know the basics? Read the lesson notes

Five heads in a row

A coin is fair when heads and tails are equally likely. A chance says how often something happens if you repeat it many times. A fair coin's chance of heads is 1 in 2, written ½ or 50%: about half of all flips.

This fair coin has just landed heads five times in a row. What is the chance that the next flip is heads?

The last five flips

HHHHH?
Keep your guess in mind. On the next screen, 10,000 flips answer it.

Ask 10,000 flips

A computer flipped a simulated fair coin 10,000 times (simulated: made by a computer). Press play. Every time the five flips just before were all heads, it looks at the next flip and counts it.

10,000 simulated flips

Each square is one flip, in rows of 100: dark for heads, light for tails. Orange: the flips that came right after five heads (neighbouring ones join into a bar). The bars count what they were.

Flips0
Five heads just before0
Next was heads0
Next was tails0

Five heads in a row came up 319 times. The next flip was heads 165 times and tails 154 times: 51.7% heads, about half. It isn't exactly half because only 319 flips are counted, and luck moves a share that small by a few points either way (51.7% is normal for this). The reason it is exactly ½ comes on the theory card.

The coin has no memory. Each flip is a fresh 50–50, whatever came before. Flips like these are called independent: one result doesn't change the chance of the next. So the answer is exactly ½, whatever came before.

Then why does a streak feel rare?

A streak is a run of the same face, heads or tails, for as long as it lasts. Before the first flip, a streak of five heads needs five things to happen. A sequence is one particular order of H and T, like HHT. Each square below is one sequence the flips can make, all equally likely. Each extra flip splits every sequence into two, one ending in heads and one in tails, so the all-heads square gets half as big each time.

The figure also checks your 10,000 flips in windows: any few flips in a row (for five, flips 1 to 5, then 2 to 6, and so on). Slide to 5 flips.

Every way a few flips can go

Each square is one equally likely sequence of flips. Coloured: the one where every flip is heads.

Ways the flips can go2
All heads1 in 2
Your 10,000 flips: windows all heads–

Five flips can go 2 × 2 × 2 × 2 × 2 = 32 ways, and one of them is all heads: 1 in 32, about 3.1%. Of your 9,996 windows of five, 319 were all heads: 3.19%.

So five heads in a row is rare, counted before you start. That rareness is what makes the next flip feel special.

Six heads, or five heads then tails?

Counted from before the first flip, which is more likely: six heads (HHHHHH), or five heads and then a tails (HHHHHT)?

Six flips can go 2 × 2 × 2 × 2 × 2 × 2 = 64 ways, all equally likely. HHHHHH is one of them and HHHHHT is another: 1 in 64 each.
Six flips can go 64 ways, all equally likely, and HHHHHT is just one of them, like HHHHHH: 1 in 64 each. The rare part, five heads first, is the same for both.
Six flips can go 64 ways, all equally likely, and HHHHHH is just one of them, like HHHHHT: 1 in 64 each. The rare part, five heads first, is the same for both.

Believing that tails is "due" after a run of heads has a name: the gambler's fallacy. It costs money when someone acts on it, betting more after a run of losses because a win "must" be coming: the bigger bet faces the same 50–50.

How long do streaks get?

In T H H H T, the three heads are a streak of 3, and each T on its own is a streak of 1. In your 10,000 flips, how long do you think the longest streak was?

Keep your guess in mind. On the next screen you count every streak.

Count every streak

Press Count. The first bar counts every streak in your 10,000 flips, the next those of 2 or more, then 3 or more, and so on.

Streaks in 10,000 simulated flips, by length

Each bar: how many streaks were at least that long.

Streaks in all–
Longest streak–

Each bar is about half the one before. The first, 4,967 streaks, is about half the 10,000 flips, because a new streak starts each time the face changes, which happens on about half the flips (the theory card shows why). Then 2,530 of 2 or more, 1,237 of 3 or more. The reason is the halving from earlier: a streak goes one flip longer only if the next flip matches it, a 50–50. The bars run out at 12: your longest streak was 12 tails in a row, from flip 2,121.

These bars count every streak. Earlier you counted windows of five that were all heads (319), so the numbers differ. (Both are expected to land near 312, but they count different things.)

So the answer is about 12. Nothing was wrong with the coin: 10,000 flips give a long streak many chances to start.

Spot the real coin

Here are two sequences of 100 flips, read left to right, row by row. One is the first 100 of your 10,000 flips. The other was written by a made-up recipe that copies a habit people show when they make up "random" flips by hand: it avoids long runs, and never lets a streak pass 3. Which one is the real coin?

Two sequences of 100 flips

H: heads, T: tails. After you answer, each one's longest streak is coloured.

Sequence A

Sequence B

A looks random because it keeps switching, but its longest streak is 3. B has a streak of 9 heads, from flip 4 to flip 12: that is the real coin.
B has a streak of 9 heads, from flip 4 to flip 12. A never goes past 3 in a row, which a real coin rarely manages over 100 flips.

A real coin's 100 flips give a long streak many chances to start. Never going past 3 in a row is the giveaway of a made-up sequence.

For the precise: how often 100 real flips do thisIn 10,000 fresh sequences of 100 simulated fair flips, 80.3% had a streak of 6 or more (exactly, counting every case: 80.7%), and the longest streak averaged 6.98. Sequence A switches face 51 times in 99 chances, sequence B 46 times; a fair coin switches about half the time, so on this count alone the two are hard to tell apart. The streaks give it away.
Theory

Independent chances multiply

Two events (things that can happen, like heads on flip 1 and heads on flip 2) are independent when one happening doesn't change the chance of the other. Then the chance that both happen is the chance of the first times the chance of the second: the multiplication rule. It's the halving you saw in the squares. For k independent events with the same chance p, all happening means p multiplied by itself k times.

Why the next flip keeps its chance: of the sequences that start with five heads, the share that goes on with a sixth is (½)6 ÷ (½)5 = ½, which is p. Expecting a streak to end, or to go on, because of the flips before it is the gambler's fallacy.

P(A and B) = P(A) × P(B)
P(k in a row) = pk
P(the next one, after any streak) = p
P(…)
the chance of what is in the brackets, from 0 (never) to 1 (always)
A, B
two independent events
p
the chance of one event, as a share: ½ for heads, 0.6 for a trading rule that wins 60% of its trades
k
how many in a row
pk
p multiplied by itself k times: (½)3 = ½ × ½ × ½ = ⅛
Worked example: your flips

Five heads in a row: (½)5 = 1 ÷ 32 = 0.03125, about 3.1%. Your flips: 319 of 9,996 windows of five, 3.19%.

HHHHHH and HHHHHT: (½)6 = 1 ÷ 64 each. The next flip after five heads: ½. Yours came up heads 165 times in 319, 51.7%.

Holds when
  • the events are independent: one result doesn't change the next one's chance;
  • the chance p stays the same every time.
Breaks when

Results move together, as trades in the same market at the same time can, or a rule's chances shift as markets change.

Try it on new numbers. A made-up rule wins 60% of its trades, each independent of the others. What is the chance that its next three trades all win?

p3 = 0.6 × 0.6 × 0.6 = 0.216, which is 21.6%: about 1 time in 5, even though each trade wins more often than not.
Multiply the chance of one win by itself three times: 0.6 × 0.6 × 0.6 = 0.216, which is 21.6%. Adding (0.6 + 0.6 + 0.6) or using 60% alone gives a chance that is far too high.
Theory

How long the longest streak gets

The multiplication rule also counts streaks. In H H T H there are three streaks: HH, T and H. The first flip starts one, and so does each flip that differs from the one before it (flips 3 and 4 here). So a new streak starts wherever the face changes, which happens on about half the flips: chance ½. It reaches length k or more if the next k − 1 flips match it: chance (½)k−1. Together that is ½ × (½)k−1 = (½)k for each flip, so over n flips you expect about n × (½)k streaks of k or more.

The longest streak, also called the longest run, is about the length where you expect just one (“expect” means the average over many repeats): n × (½)k = 1, so 2k = n. Streaks longer than that are too rare to expect even once. The power of 2 that gives n (210 = 1,024, so for 1,024 it is 10) is called the logarithm base 2 of n, written log₂ n.

streaks of k or more ≈ n × (½)k
longest streak ≈ log₂ n
n
the number of flips (or of trades that win or lose 50–50)
k
a streak length
log₂ n
the power of 2 that gives n: 210 = 1,024, so log₂ 1,024 = 10
Worked example: your 10,000 flips

Streaks of 5 or more: 10,000 × (½)5 = 10,000 ÷ 32 = 312.5. You counted 309.

Longest: 213 = 8,192 and 214 = 16,384, so log₂ 10,000 is between 13 and 14 (13.3 on a calculator's log₂ key). Your longest was 12.

The rule is a rough guide: the real longest streak varies from run to run, so expect a figure within about 1 or 2 of it.

Holds when
  • the flips are independent and 50–50;
  • there are many of them (dozens or more), since it counts streaks on average.
Breaks when

Wins and losses aren't 50–50: then a losing streak goes on with the loss rate's chance instead of ½. Results that cluster together give longer streaks than this.

For the precise: closer than log₂ nOn 200 fresh sequences of 10,000 simulated flips, the longest streak averaged 13.88. Counting every case exactly (the script does), the average longest streak of heads or tails is 6.98 in 100 flips, 10.30 in 1,000 and 13.62 in 10,000: about log₂ n + ⅓. The longest streak of heads alone is about one shorter, about log₂ n − ⅔ (Schilling, 1990).

Try it on new numbers. About how long would you expect the longest streak to be in 1,000 flips of a fair coin?

Hint: find the power of 2 closest to 1,000.

210 = 1,024 is close to 1,000, so log₂ 1,000 ≈ 10. On 2,000 fresh sequences of 1,000 simulated flips, the longest streak averaged 10.3.
Look for the power of 2 near 1,000: 210 = 1,024, so log₂ 1,000 ≈ 10. Check: 1,000 × (½)10 = 1,000 ÷ 1,024, about one streak of 10 or more.

What this means when you test a rule

A backtest runs a trading rule over past prices to see what it would have done, trade by trade. Its wins and losses come in streaks too. Turn over three habits, one at a time.

  1. A losing streak doesn't make a win due.

    If the trades are independent, the next trade has the same chance after any streak. "It's due" is the gambler's fallacy.

Practice 1 of 3

Six losses in a row

The situationYour own research (made-up numbers): a rule loses 40% of its trades, and each trade is independent of the others.

What is the chance that its next six trades all lose? Answer in %.

Hint: the chance of one loss is 0.4. Multiply it by itself six times, then turn the share into a percent.

0.46 = 0.4 × 0.4 × 0.4 × 0.4 × 0.4 × 0.4 = 0.004096, which is 0.41%, about 1 in 244.
0.46 = 0.4 × 0.4 × 0.4 × 0.4 × 0.4 × 0.4 = 0.004096. As a percent that is 0.41%, about 1 in 244. (If you got 0.004, that's the share, not yet the percent.)
Rare for six trades picked in advance, but a long backtest has many places for such a run to start.
Practice 2 of 3

"It runs hot and cold"

The situationA friend backtested a made-up rule that wins half its trades. Over 1,000 trades they counted 30 streaks of 5 or more (winning or losing), and say the rule runs hot and cold.

About how many streaks of 5 or more would 1,000 flips of a fair coin give?

Hint: n × (½)k, with n = 1,000 and k = 5. (½)5 = 1 ÷ 32.

1,000 × (½)5 = 1,000 ÷ 32 = 31.25. Their 30 streaks are what a coin gives, so the streaks don't show the rule runs hot and cold.
1,000 × (½)5 = 1,000 ÷ 32 = 31.25. Their 30 streaks are about what a coin gives, so the streaks don't show the rule runs hot and cold.
Practice 3 of 3

Seven losses in a row

The situationIn your backtest of a made-up rule that wins 50% of its trades, each independent, the rule has just lost 7 in a row. A friend says the next trade will very likely win, because 8 losses in a row only happen 1 time in 256.

What is the chance that the next trade wins?

(½)8 = 1 in 256 is the chance of eight losses counted from before the first. Seven have already happened; the eighth is one trade, with its own chance: 50%. This is the gambler's fallacy.
With independent trades, a streak doesn't carry over: the next trade wins with its own chance, 50%. Expecting the run to go on is the gambler's fallacy too.
(½)8 = 1 in 256 is the chance of eight losses counted from before the first. Seven have already happened; the eighth is one trade, with its own chance: 50%.
✓

Lesson done

Independent events have no memory: after any streak, the next fair flip is still 50–50. Streaks themselves are normal, because each extra flip only halves the chance, so in n flips the longest is about log₂ n. Expecting a streak to end because it has gone on long is the gambler's fallacy.

Key ideas

Chance (probability)
how often something happens if you repeat it many times, from 0 (never) to 1 (always)
Fair coin
heads and tails equally likely: ½ each
Independent
one result doesn't change the chance of the next
Multiplication rule
for independent events, the chance that all happen is the product of their chances
Streak (run)
a run of the same result, for as long as it lasts
Gambler's fallacy
expecting a streak to end (or go on) because of the results before it
log₂ n
the power of 2 that gives n; about the longest streak in n fair flips
Backtest
a trading rule run over past prices to see what it would have done, trade by trade

Formulas

P(A and B) = P(A) × P(B)  ·  P(k in a row) = pk

streaks of k or more ≈ n × (½)k  ·  longest streak ≈ log₂ n

Lesson notes

A fair coin has no memory: after any streak the next flip is still 50–50. Streaks are normal: each extra flip halves the chance a streak goes on, so in n flips the longest is about log₂ n, the power of 2 that gives n.

Five heads in a row

A coin is fair when heads and tails are equally likely. A chance says how often something happens if you repeat it many times: a fair coin's chance of heads is 1 in 2, or 50%. In the lesson's simulation of 10,000 fair flips, 319 flips came right after five heads in a row (a run of seven heads counts at its sixth and seventh flips). The next flip was heads 165 times and tails 154 times: 51.7% heads (165 ÷ 319), about half. It isn't exactly half because 319 flips are few: luck moves a share that small by a few points. Flips are independent: one result doesn't change the chance of the next, so the answer is exactly ½.

Why a streak feels rare

A streak is a run of the same face, for as long as it lasts. A sequence is one order of H and T, like HHT. Five flips go 2 × 2 × 2 × 2 × 2 = 32 ways, all equally likely, and one is all heads: 1 in 32, 3.1%. Five heads is rare counted before you start, but that part is already behind you. A window is any five flips in a row (flips 1 to 5, then 2 to 6, and so on): 319 of the simulation's 9,996 windows were all heads, 3.19%. Six flips go 64 ways, so HHHHHH and HHHHHT are 1 in 64 each. The gambler's fallacy is believing the next flip must even things out after a run of heads, so that tails is "due". It costs money when someone bets more after a run of losses: the next flip still faces the same 50–50.

The same idea separates real from made-up flips. People making up "random" flips by hand avoid long runs. A sequence of 100 flips that never passes 3 in a row gives itself away, because a real coin's 100 flips usually hold a streak of 6 or more (80.3% of 10,000 simulated sequences did).

How long streaks get

In T H H H T, the three heads are a streak of 3. The simulation held 4,967 streaks, about half of 10,000, because a new streak starts whenever the face changes, on about half the flips. 2,530 were 2 flips or longer and 1,237 were 3 or longer: each count is about half the one before, because a streak goes one flip longer only if the next flip matches, a 50–50. The longest was 12 tails in a row. Streaks of 5 or more numbered 309, a different count from the 319 above (which counted flips right after five heads); both are expected near 312, the average over many repeats.

What this means when you test a rule

A backtest runs a trading rule over past prices to see what it would have done, trade by trade. If its trades are independent, a losing streak doesn't make a win due. Set a count of streaks of k or more beside n × (½)k before reading meaning into it. For k = 6 in 1,000 trades: 1,000 ÷ 64 = 15.6, about 16. The lesson states that a count from about 9 to 23 is what a coin ordinarily gives; one far above, such as 40, merits a second look.

Formula: independent chances multiply

Two events are independent when one doesn't change the chance of the other. Then the chance both happen is the first's chance times the second's. The next flip keeps its chance because, of the sequences starting with five heads, the share going on with a sixth is (½)6 ÷ (½)5 = ½.

P(A and B) = P(A) × P(B)
P(k in a row) = pk
P(the next one, after any streak) = p
P(…)
the chance of what is in the brackets, from 0 (never) to 1 (always)
A, B
two independent events
p
the chance of one event, as a share: ½ for heads, 0.6 for a trading rule that wins 60% of its trades
k
how many in a row
pk
p multiplied by itself k times: (½)3 = ½ × ½ × ½ = ⅛
Worked example: the lesson's flips

Five heads in a row: (½)5 = 1 ÷ 32 = 0.03125, about 3.1%. In the lesson's simulation: 319 of 9,996 windows of five, 3.19%.

HHHHHH and HHHHHT: (½)6 = 1 ÷ 64 each. The next flip after five heads: ½. In the simulation it came up heads 165 times in 319, 51.7%.

Holds whenBreaks when

Results move together, as trades in the same market at the same time can, or a rule's chances shift as markets change.

Formula: how long the longest streak gets

A new streak starts at a flip when the face changes: ½. It reaches length k if the next k − 1 flips match: (½)k−1. Together (½)k per flip, so n flips give about n × (½)k streaks of k or more. The longest is about where you expect one (expect: the average over many repeats): n × (½)k = 1, so 2k = n. That power of 2 is log₂ n.

streaks of k or more ≈ n × (½)k
longest streak ≈ log₂ n
n
the number of flips (or of trades that win or lose 50–50)
k
a streak length
log₂ n
the power of 2 that gives n: 210 = 1,024, so log₂ 1,024 = 10
Worked example: the lesson's 10,000 flips

Streaks of 5 or more: 10,000 × (½)5 = 10,000 ÷ 32 = 312.5. The simulation had 309 (a different count from the 319 flips that came right after five heads).

Longest: 213 = 8,192 and 214 = 16,384, so log₂ 10,000 is between 13 and 14 (13.3 on a calculator's log₂ key). The simulation's longest was 12.

The rule is a rough guide: the real longest streak varies from run to run, so expect a figure within about 1 or 2 of it.

Holds whenBreaks when

Wins and losses aren't 50–50: then a losing streak goes on with the loss rate's chance instead of ½. Results that cluster together give longer streaks than this.

One more worked example, on new numbers

A made-up rule wins 70% of its trades, each independent. Its next four all win with chance 0.7 × 0.7 × 0.7 × 0.7 = 0.49 × 0.49 = 0.2401, about 24%. In 2,000 fair flips, streaks of 7 or more: 2,000 ÷ 128 = 15.625. Longest: 210 = 1,024 and 211 = 2,048, so log₂ 2,000 is just under 11: about 11, give or take 1 or 2.

Common mistakes

Check yourself

  1. What is the chance that three fair flips in a row are all heads?
    AnswerFor independent events with the same chance p, all k happening has chance pk: (½)3 = ½ × ½ × ½ = ⅛, or 12.5%.
  2. A rule loses 30% of its independent trades. What is the chance its next five all lose?
    AnswerMultiply one loss's chance by itself five times: 0.3 × 0.3 × 0.3 × 0.3 × 0.3 = 0.00243, about 0.24%.
  3. About how many streaks of 3 or more would 1,600 fair flips give?
    AnswerStreaks of k or more ≈ n × (½)k = 1,600 × (½)3 = 1,600 ÷ 8 = 200.
  4. A 50–50 rule with independent trades has lost 7 in a row. A friend says a win is likely next, since 8 losses in a row happen 1 time in 256. Right?
    AnswerNo. 1 in 256 is counted from before the first trade. Seven have happened; the next trade has its own chance, 50%.

Try it yourself

Run the simulation. Every flip and number in this lesson comes from one Python script with a fixed random seed (below). Change the number of flips and watch the longest streak move with log₂ n.

By hand. Flip a real coin 100 times and write down H or T. Then ask a friend to write 100 made-up flips without a coin. Find the longest streak in each. The coin's is usually 6 or more; the made-up one's is usually shorter. In a spreadsheet, =IF(RAND()<0.5,"H","T") copied down 100 rows makes the coin.

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

Going further: streaks when the chances aren't 50–50

Trades rarely win exactly half the time. For a rule that loses a share q of its trades, a losing streak goes one trade longer with chance q, not ½, and starts after a win, with chance 1 − q. So the count of losing streaks of k or more is about n × (1 − q) × qk, and the longest losing streak is about the k where that count is 1. For q = 0.4 over 1,000 trades: 1,000 × 0.6 × 0.4k = 1 at k ≈ 7.0. On 2,000 made-up rules of 1,000 such trades, the longest losing streak averaged 7.02, from 4 in the luckiest rule to 14 in the unluckiest. The 0.41% chance of six losses in a row from practice 1 is rare for six trades you pick; somewhere in 1,000 trades, a run of 7 is ordinary.

For the precise: the drawsSeed 2026110907, one numpy Generator, in this order: the lesson's 10,000 flips (rng.random(10_000) < 0.5, 5,013 heads); the made-up sequence (first flip rng.random() < 0.5, then switch when rng.random() < 0.6 or the streak has reached 3); 10,000 fresh sequences of 100 flips, 2,000 of 1,000 and 200 of 10,000; then 2,000 rules × 1,000 trades with a loss when rng.random() < 0.4. "After five heads" counts every flip whose five flips before were all heads, so a run of seven heads counts at its sixth and seventh flips. Your 10,000 flips hold 4,967 streaks; the counts of k or more run 4,967, 2,530, 1,237, 638, 309, 152, 77, 45, 27, 11, 5, 2, against 10,000 × (½)k = 5,000, 2,500, 1,250, 625, 312.5, 156, 78, 39, 20, 9.8, 4.9, 2.4. The exact average longest streak comes from counting: the longest streak of n flips is at most m exactly when the n − 1 "same as the flip before" results contain no run of m.

Honest notes

Where this comes from