Lab · 回測過度擬合
Deflated Sharpe Ratio 計算器
一個回測的 Sharpe 比率,在把為了找到它而嘗試過的設定數量算進去之後,有多大機率是真的?一個零依賴的 Python 套件與 CLI,逐項對照原始論文中的每一個範例檢核。
一個回測抵達時,帶著一個 Sharpe 比率、一個樣本長度,以及所剩無幾的其他資訊。對它進行折減 問的是另一個問題:一場這種規模的搜尋,在背後毫無技巧的情況下,多常會產生一個這麼好的 數字。在下方填入回測內容,每一項數量都會隨著輸入即時更新。
Deflated Sharpe Ratio calculator
Length bounds
The second is the data needed before a search that size stops producing 2.50 by chance.
Bailey and López de Prado (2014): a treasury seasonality backtest, best of 100 configurations. Published answer: SR₀ 0.1132 per day and DSR 0.9004, short of the 95% bar.
Deflated Sharpe Ratio
0.9004
below the 95% bar
Undeflated PSR
1.0000
probability the true Sharpe ratio beats zero, before the search is counted
Selection threshold SR₀
1.79
annualised, against 2.50 observed
Trials this result survives
46
at 95% confidence
Track record needed to pass
8.21 y
holding the trial count at 100
Sharpe ratio needed to pass
2.71
annualised, same sample and trials
Reproduce this result in the package
both snippets track the inputs above
dsr deflate --sharpe 2.5000 --annual --periods-per-year 250 --n-obs 1250 --trials 100 --trial-variance 0.5000 --skew -3.0000 --kurtosis 10.0000
from deflated_sharpe import deflated_sharpe_ratio
result = deflated_sharpe_ratio(
sharpe=0.15811388, # per period
n_obs=1250,
n_trials=100,
var_trials=0.0020000000,
skew=-3.0000,
kurtosis=10.0000,
)
result.dsr # 0.9004數學原理
所有數量皆為逐期:日頻 Sharpe 比率是日報酬均值除以其標準差,而 T 計數的是日頻觀測值。 Probabilistic Sharpe Ratio(Bailey 與 López de Prado 2012)是真實 Sharpe 比率超過基準 SR* 的 機率,並計入報酬的偏度 γ₃ 與原始峰度 γ₄:
若嘗試了 N 個不具技巧的策略,且其估計 Sharpe 比率的變異數為 V,則其中最佳者的期望表現為
其中 γ ≈ 0.5772 為 Euler–Mascheroni 常數。Deflated Sharpe Ratio 即是以該期望最大值作為基準的 Probabilistic Sharpe Ratio, ,因此 它同時修正搜尋與非常態報酬。
同一套代數推導出兩個長度下界。最短績效紀錄長度(minimum track record length)是 PSR 達到 所選信心水準 1 − α 時的觀測數,而最短回測長度(minimum backtest length)是資料的年數,超過 此年數後 N 次試驗便不再靠機運產生年化 Sharpe 比率 E[max](Bailey、Borwein、López de Prado 與 Zhu 2014):
那四條式子產生上方計算器中的每一個數字。計算器開啟時載入的是 Bailey 與 López de Prado(2014)的實作範例:五年日報酬上的年化 Sharpe 比率 2.5,選自 100 個設定,其 Sharpe 比率的變異數為 0.5,偏度 −3 且峰度 10。該回測達到 0.9004,未及 95% 的門檻,而同一組報酬 若作為 46 次試驗中的最佳者則會通過。
為何 Sharpe 比率需要折減
一位研究者嘗試一百個版本的策略並回報其中最佳者,等於做了一百次實驗,然後發表最幸運的 那一次。即使沒有任何版本具備技巧,其中最佳者仍會呈現一個像樣的 Sharpe 比率,而更長的 搜尋會產生看起來更漂亮的勝出者。Bailey 與 López de Prado(2014)的 Deflated Sharpe Ratio 估計的是:一旦把那場搜尋計入,被選中策略的真實 Sharpe 比率高於零的機率。
下方的示範讓這個問題變得具體。其中每個策略的真實 Sharpe 比率都恰為零,然而六十個之中的 最佳者通常會呈現高於 1 的年化 Sharpe 比率。
Demo: the best of N strategies with no skill
Every strategy below has a true Sharpe ratio of exactly zero over 250 daily returns, and they share a market factor. The best one still looks tradeable. SR₀ is where the best is expected to land by luck alone.
best, annualised
1.90
participation ratio
3.8
績效紀錄需要多長
即使年化 Sharpe 比率相同,頻率仍然重要,因為每年較少的觀測值攜帶較少資訊,而厚尾的影響 又比頻率更大。具備 HFR 對沖基金指數動差的月度報酬,需要將近五年才能顯示 Sharpe 比率 2 勝過 1,日報酬則不到三年。
Figure: minimum track record length, years
How long a track record must be before an observed annualised Sharpe ratio is significantly above a benchmark at 95%. The outlined cell is the example in Bailey and López de Prado (2012).
| observed SR ↓ / benchmark → | 0.0 | 0.5 | 1.0 | 1.5 | 2.0 | 2.5 |
|---|---|---|---|---|---|---|
| 0.5 | 11 | · | · | · | · | · |
| 1.0 | 2.71 | 11 | · | · | · | · |
| 1.5 | 1.21 | 2.72 | 11 | · | · | · |
| 2.0 | 0.69 | 1.22 | 2.73 | 11 | · | · |
| 2.5 | 0.44 | 0.69 | 1.22 | 2.74 | 11 | · |
| 3.0 | 0.31 | 0.44 | 0.69 | 1.23 | 2.76 | 11 |
A dot means the observed Sharpe ratio does not exceed the benchmark, so no track record is long enough. Hover a cell to read it.
真正重要的試驗次數
N 應為嘗試過的相異設定數目,包含那些提早放棄的。由於策略變體彼此相關,把它換成特徵值 參與比之類的有效計數看起來更為謹慎,但當 V 是從試驗本身量測而來時,這等於對相關性修正了 兩次。一個共享因子會讓每個試驗的 Sharpe 比率一起移動,因而已經壓縮了它們的離散度,這使得 相關性在 N 被動到之前就已落在 √V 之內。
在第一個示範中執行批次即可檢核這一點。使用相異計數時,無技巧語料庫的最佳者在任何相關性 之下都約有一半的時候越過 SR₀,這正是一個期望最大值該有的行為。改用參與比時,在套件的 參考模擬中有 73% 到 85% 的相關虛無語料庫越過它,因此以那種方式建立的門檻會放行大部分的 純雜訊。
驗證
此套件重現三篇原始論文中印出的每一個數值範例,而每一個範例都是一項測試,在 Linux、macOS 與 Windows 上以 Python 3.10 至 3.13 執行。
| 範例 | 論文 | 套件 |
|---|---|---|
| DSR,100 次試驗,偏度 −3,峰度 10 | SR₀ ≈ 0.1132, DSR 0.9004 | 0.1132, 0.9004 |
| 同一策略在 46 次試驗之後 | 0.9505 | 0.9505 |
| 常態報酬在 88 次試驗之後 | 0.9505 | 0.9505 |
| PSR,24 個月期間的月度 SR 0.458 | 0.982 常態, 0.913 非常態 | 0.982, 0.913 |
| MinTRL,SR 2 對比 1,日 / 週 / 月 | 2.73 / 2.83 / 3.24 年 | 2.73 / 2.83 / 3.24 |
| MinTRL,月度,HFR 指數動差 | 4.99 年 | 4.99 |
| 五年資料所容許的試驗次數 | 45 | 45 |
其餘部分由 Monte Carlo 測試檢核。PSR 在約 5% 的無技巧樣本上越過 0.95,而期望最大值與模擬 出的最大值相符,在十次試驗時高估 2.5%,自一百次起則低於 1%。最短績效紀錄長度恰好落在 PSR 達到其信心水準之處。
DSR 不會告訴你的事
高於 0.95 的 DSR 表示該次選擇不太可能單由搜尋與報酬的形狀來解釋。它對交易成本、容量、 資料中的前視、狀態改變,或從未被記錄下來的試驗,一概沒有說法。低於 0.95 的 DSR 同樣不能 證明技巧不存在,在短樣本上尤其如此。
每一條式子也都假設報酬在序列上獨立。跨越數根 K 棒持有的部位會讓報酬產生自我相關,因此 套件提供一個 AR(1) 有效樣本數,可用以取代原始觀測數傳入。
可重現性
DaruFinance / deflated-sharpe
Python · 開源參考實作
最小調用
from deflated_sharpe import deflated_sharpe_ratio, to_period_sharpe, to_period_variance
result = deflated_sharpe_ratio(
sharpe=to_period_sharpe(2.5, 250), # annualised 2.5 on daily data
n_obs=1250, # five years
n_trials=100, # configurations tried
var_trials=to_period_variance(0.5, 250), # variance of their Sharpe ratios
skew=-3, kurtosis=10, # raw kurtosis: normal = 3
)
result.dsr # 0.9004, below the 95% bar
# or from the command line
# dsr deflate --sharpe 2.5 --annual --periods-per-year 250 --n-obs 1250 \
# --trials 100 --trial-variance 0.5 --skew -3 --kurtosis 10參考文獻
- [1]Bailey, D. H. & López de Prado, M. (2014). The Deflated Sharpe Ratio: correcting for selection bias, backtest overfitting and non-normality. Journal of Portfolio Management 40(5), 94–107.
- [2]Bailey, D. H. & López de Prado, M. (2012). The Sharpe Ratio Efficient Frontier. Journal of Risk 15(2), 3–44.
- [3]Bailey, D. H., Borwein, J. M., López de Prado, M. & Zhu, Q. J. (2014). Pseudo-mathematics and financial charlatanism: the effects of backtest overfitting on out-of-sample performance. Notices of the American Mathematical Society 61(5), 458–471.
- [4]Lo, A. W. (2002). The statistics of Sharpe ratios. Financial Analysts Journal 58(4), 36–52.

