Backtesting Essentials · Lesson 5
Sampling error and confidence intervals
Every number a backtest measures comes with an error bar. How wide it is, and what a 95% confidence interval does and doesn't promise.
Know the basics? Read the lesson notesA strategy whose true win rate we know
Here is a made-up trading strategy. Each trade wins 1R or loses 1R (R is a fixed amount of money, say €10). We built it, so we know something you never know in real life: in the long run it wins 55% of its trades. That is its true win rate, the share of trades it wins.
A backtest runs a strategy over a stretch of trades to see what it would have done. Run one: 100 trades. Then run a few more.
One strategy, 100 trades a backtest (simulated)
Top: this backtest's 100 trades, a filled square for a win. Bottom: the win rate each backtest measured, against the true 55%.
The gap between the number a backtest measures and the true one is called sampling error. It comes from which trades you happened to get: your sample. Nothing about the strategy changed between runs.
How far can luck move it?
Next you'll run 1,000 backtests of this strategy, 100 trades each. Distances here are in points: one point is one percentage point of the win rate, so 55% to 58% is 3 points. Most of the 1,000 measured win rates will land within how many points of 55%?
1,000 backtests of one strategy
Same strategy, same true 55%, 1,000 separate backtests of 100 trades. Run them and watch where the measured win rates pile up.
The win rate measured by each of 1,000 backtests (simulated)
Each bar counts the backtests that measured that win rate. Dashed line: the true 55%. Shaded: the band you picked.
How wide is that? The standard deviation of the 1,000 win rates, how far a typical one lands from the middle, is 5.0 points. The standard deviation of a measured number has its own name: its standard error. On a bell curve (lesson A5), about 95 in 100 results land within two standard deviations of the middle. Here two standard errors is 2 × 5 = 10 points either side: the band that held most of yours.
One backtest is all you get
In real life you don't get 1,000 backtests, and nobody tells you the true win rate. You get one. Say it's your first: 54 wins out of 100, so 54%.
You can still work out its error bar. The standard error depends on only two things: the number of trades and the win rate. Your backtest gives you both: 100 trades, and its measured 54%, standing in for the true rate you can't see. On the last screen the 1,000 backtests measured that spread: 5.0 points. A formula gets the same 5.0 from your one backtest's two numbers (the theory card, soon, shows how; you can check it there). Two standard errors either side gives a range: 54% plus or minus about 10 points.
What does your one backtest tell you about the strategy's true win rate?
A bar for every backtest
Now draw a bar like yours around each of the 1,000 backtests: its own measured win rate, plus or minus 1.96 of its own standard errors. 1.96 is the precise version of "about 2"; a theory card later says where it comes from. Each bar is 18 to 20 points wide; widths differ a little because each bar uses its own measured rate.
How many of the 1,000 bars will miss the true 55%, landing wholly to one side of it?
The coverage machine
Each bar below is one backtest's 95% confidence interval, with a dot at the win rate it measured. Grey bars cross the dashed line at 55%: they caught the true rate. Coloured bars missed it. Drop the first 100, then the rest.
1,000 backtests, one bar each (simulated)
Each bar: a measured win rate (the dot) ± 1.96 standard errors. Dashed line: the true 55%. The picture holds the latest 100 bars; the counts below cover every bar dropped.
A bar misses when its backtest was unusually lucky or unlucky: its win rate landed so far from 55%, about 9 or 10 points or more, that its bar can't reach back. That is what a 95% confidence interval means: a range made by a method that catches the true value about 95 times in 100.
Wider bars, fewer misses
The 95% is a choice, called the confidence level. Change it and every bar changes width: more standard errors either side for a higher level, fewer for a lower one. Slide to 99%, then to 80%.
The same 1,000 backtests at another confidence level
Each bar: a measured win rate ± z standard errors (z from the slider). Coloured: bars that missed 55%. The picture holds the first 100 bars; the counts below cover all 1,000.
At 99% each bar stretches 2.58 standard errors either side (about 13 points) and 12 of 1,000 missed; at 80%, 1.28 (about 6 points), and 188 missed. A bar that never misses would run from 0% to 100% and tell you nothing. 95% is the usual middle ground, a convention rather than a law.
What the 95% is about
On the machine, every bar either caught 55% or missed it. None caught it "95% of the way". In real life you see only your one bar, and never the true rate, so you can't tell which kind yours is.
A friend's backtest of 100 trades gives a 95% confidence interval for its win rate of 48% to 68%. What does the 95% tell them?
How wide the bar is: the standard error
The standard error of a measured number is how far luck typically moves it from the true value: its standard deviation across repeat backtests. It has a formula, so you never need the 1,000 backtests.
Start with an average. The bell curve lesson showed that the total of n trades spreads by σ × √n. An average is the total divided by n, so it spreads by σ × √n ÷ n = σ ÷ √n. A win rate is an average too: count a win as 1 and a loss as 0, and their average is the share of wins. One such trade has σ = √(p(1 − p)). For p = 0.55: wins are 1 and losses 0, so the average is 0.55. A win sits 0.45 from it and a loss 0.55; the squared gaps, 0.45² and 0.55², weighted by 55% and 45%, average 0.2475, which is 0.55 × 0.45. Its square root is σ.
- SE
- the standard error: the standard deviation of the measured number across repeat backtests
- σ
- the standard deviation of one trade's result (σ is the Greek letter sigma)
- n
- the number of trades
- p
- the win rate as a fraction (55% = 0.55). The true one is unknown, so in practice the measured one stands in for it
p = 0.55 and n = 100: 0.55 × 0.45 = 0.2475; ÷ 100 = 0.002475; √0.002475 = 0.0497. Turn a fraction into points by × 100: 0.0497 = 5.0 points. Your 1,000 measured win rates spread by 5.0 points. With 400 trades: 0.2475 ÷ 400 = 0.00062, √0.00062 = 0.0249, 2.5 points. Four times the trades, half the standard error, because of the √n.
- each trade's result doesn't change the next one's (the trades are independent);
- the true win rate stays the same through the test;
- the number was measured once, as planned, not picked as the best of many.
Results come in clumps (lesson C2, Volatility clustering) or the market changes under the rule (lesson C3, Regimes and decay): then the spread is wider than the formula says. Picking the best of many backtests picks the lucky ones (lesson B7, The best of many).
Try it on new numbers. A backtest won 60% of 400 trades. What is the standard error of its win rate, in points?
The confidence interval
A confidence interval is the measured number plus or minus a few standard errors. How many sets its confidence level: the share of backtests whose interval catches the true value. The number comes from the bell: across repeat backtests a measured number piles into a bell around the true value, and on a bell 95 in 100 results land within 1.96 standard deviations of the middle (the 68–95–99.7 rule's "95 within two", made exact: 95.4 in 100 land within 2 standard deviations, 95.0 within 1.96).
- estimate
- the number you measured: a win rate, an average trade
- z
- standard errors either side, set by the confidence level: 1.28 for 80%, 1.64 for 90%, 1.96 for 95%, 2.58 for 99%
- SE
- its standard error, from the last card
54 wins of 100: SE = √(0.54 × 0.46 ÷ 100) = √0.002484 = 0.0498, 5.0 points. 1.96 × 4.98 = 9.8 points. The interval: 54 − 9.8 = 44.2% to 54 + 9.8 = 63.8%. It caught 55%. Across all 1,000 backtests, 943 intervals caught it.
- the standard error holds (the last card's list);
- there are enough trades for the bell: for a win rate, at least about 10 wins and 10 losses;
- "95%" is read as the method's record, not as a chance for one interval you have already drawn.
With few trades, or a win rate near 0% or 100%, the bar catches less often than it says and can even run past 100% (lesson C4, Bootstrap and permutation tests, builds intervals without the bell). Keeping the best of many backtests breaks it too (lesson B7, The best of many).
The same works for an average trade. A backtest of 64 trades made +0.3R a trade on average, and one trade's result has a standard deviation of 1.6R. What is the 95% confidence interval for its true average trade?
What this means for your backtest
Three habits follow. Turn them over one at a time.
- Read every number with its error bar.
A win rate of 54% over 100 trades means "somewhere from about 44% to 64%". The same goes for the average trade: its standard error is σ ÷ √n. A number given without its trade count can't be read at all.
- Four times the trades halves the bar.
Over 100 trades a win rate's 95% bar is about ±10 points; over 400, about ±5; over 1,600, about ±2.5. Small tests give wide bars, and no amount of care in the testing narrows them.
- If the bar includes "no edge", this backtest can't tell an edge from none.
A strategy has an edge when it makes money on average. With trades that win or lose the same amount, no edge means a 50% win rate. Our strategy truly has one (55%), yet 807 of the 1,000 backtests had a 95% bar that included 50% (the last line under the confidence-level slider, at 95%). Turning "can't tell" into a yes-or-no test is the next lesson, B6, Hypothesis tests.
"My strategy wins 70% of the time"
What is the lower end of the 95% confidence interval for its true win rate? Type a percentage.
Hint: the standard error is √(p(1 − p) ÷ n) with p = 0.7 and n = 50. Turn it into points, multiply by 1.96, and take that from 70.
A narrower bar
About how many trades do you need?
Two backtests
Whose 95% confidence interval for the true win rate stays wholly above 50%?
Lesson done
Every number a backtest measures, a win rate or an average trade, would come out different on another run of trades. Its standard error says by how much, and a 95% confidence interval, the number plus or minus 1.96 standard errors, comes from a method that catches the true value about 95 times in 100. Four times the trades halves it.
Key ideas
- Sampling error
- the gap between a measured number and the true one, from which trades you happened to get
- Standard error (SE)
- how far luck typically moves a measured number: its standard deviation across repeat backtests
- Confidence interval
- the measured number ± z standard errors: a range worked out from one sample
- Confidence level
- the share of samples whose interval catches the true value (95% is usual); a record of the method, not a chance for one interval
- Point
- one percentage point: 55% to 58% is 3 points
Formulas
SE of an average = σ ÷ √n · SE of a win rate = √(p(1 − p) ÷ n)
confidence interval = estimate ± z × SE, with z = 1.96 for 95%
Other levels: z = 1.28 for 80%, 1.64 for 90%, 2.58 for 99%. Four times the trades, half the standard error.