Python 多重檢驗檢查:100,000 個零優勢策略中約 5,000 個會偶然通過 p < 0.05
About 5,000 of 100,000 Zero-Edge Strategies Pass p < 0.05: A Multiple-Testing Check in Python
文章以零均值日收益策略説明,對 100,000 個零優勢策略逐一作單側檢驗時,p < 0.05 預期會有約 5,000 個僅憑偶然通過。按全體測試數作 Bonferroni 校正,門檻為 0.05/100,000 = 5e-7;若再檢驗 5,000 個留存策略,未校正的 p < 0.05 預期仍會有約 250 個偶然通過,按留存數調整則為 1e-5。
Suppose you backtest 100,000 strategies that have zero edge. On average about 5,000 of them pass a one-sided p < 0.05 test by luck alone. At p < 0.01 about 1,000 still pass, and at p < 0.001 about 100. A Bonferroni cut for the whole scan, 0.05/100,000 = 5e-7, brings the expected number of lucky passes down to 0.05. (100,000 is my own example scale.)
A post on X describes an open strategy library, "every single one fully backtested on years of real market data." I haven't checked that claim. A long list of backtested winners tells you nothing about edge until you know N.
Reproduce it
Each row is one strategy with zero-mean daily returns. A one-sided t-test checks whether the mean is above zero.
import numpy as np
from scipy import stats
rng = np.random.default_rng(0)
N, days = 100_000, 252
r = rng.standard_normal((N, days), dtype=np.float32) * 0.01 # zero edge
t = r.mean(1) / (r.std(1, ddof=1) / np.sqrt(days))
p = stats.t.sf(t, df=days - 1) # one-sided, H1: mean > 0
for a in (0.05, 0.01, 0.001, 0.05 / N):
print(f"alpha={a:.0e} expected={N*a:,.2f} passed={(p < a).sum():,}")
The expected column is N × alpha, so the number of lucky winners grows linearly with N. Under independence, the count at 0.05 has a standard deviation of about 69.
Count N honestly
N covers every rule, parameter set, market and timeframe you tried, including the ones you dropped. If N × threshold comes out in the thousands, a leaderboard with thousands of winners is exactly what noise alone would give you.
Correct the holdout by survivor count
A holdout period that played no part in selection removes most of the lucky winners, but with thousands of survivors it still lets hundreds through at an uncorrected threshold. Re-test the 5,000 zero-edge survivors at an uncorrected p < 0.05 and about 250 pass again. Divide by the survivor count instead, 0.05/5,000 = 1e-5, and the expected number of false survivors drops to about 0.05.
Limits
These are expected values for simulated noise (zero edge, normally distributed returns, no trading costs), and I didn't run the code, so the passed counts will differ from the expected column and change with the seed. Real strategies are often highly correlated, which puts the effective number of independent tests below N: the expected count is still N × alpha, but a single scan's count swings more, and 0.05/N ends up stricter than it needs to be.
For your own scan: count N, multiply it by your threshold before you look at the leaderboard, and re-test every survivor on an unused holdout at 0.05 divided by the number of survivors you carry into it.
This article was drafted with AI assistance.
python #statistics #trading #datascience
來源:DEV Community · dev.to