feat(metrics): Deflated Sharpe Ratio (Bailey & Lopez de Prado)
Aggiunge expected_max_sharpe e deflated_sharpe_ratio per correggere multiple testing nella valutazione di strategie. Considera skewness, kurtosis e numero di trial. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
|
||||
from multi_swarm.metrics.dsr import deflated_sharpe_ratio, expected_max_sharpe
|
||||
|
||||
|
||||
def test_expected_max_sharpe_grows_with_n_trials():
|
||||
e1 = expected_max_sharpe(n_trials=1, sharpe_var=1.0)
|
||||
e10 = expected_max_sharpe(n_trials=10, sharpe_var=1.0)
|
||||
e100 = expected_max_sharpe(n_trials=100, sharpe_var=1.0)
|
||||
assert e1 < e10 < e100
|
||||
|
||||
|
||||
def test_dsr_zero_when_sharpe_equals_expected_max():
|
||||
np.random.seed(0)
|
||||
returns = pd.Series(np.random.normal(0, 0.01, 500))
|
||||
_dsr, p = deflated_sharpe_ratio(
|
||||
returns, n_trials=10, periods_per_year=8760, sharpe_var=0.0
|
||||
)
|
||||
# Con sharpe_var=0 e Sharpe stimato vicino a 0, p-value deve essere alto.
|
||||
assert 0.0 <= p <= 1.0
|
||||
|
||||
|
||||
def test_dsr_significant_for_strong_sharpe():
|
||||
np.random.seed(42)
|
||||
returns = pd.Series(np.random.normal(0.005, 0.005, 1000))
|
||||
dsr, p = deflated_sharpe_ratio(
|
||||
returns, n_trials=5, periods_per_year=8760, sharpe_var=1.0
|
||||
)
|
||||
# Sharpe atteso > 0 e p-value basso
|
||||
assert dsr > 0
|
||||
assert p < 0.5
|
||||
Reference in New Issue
Block a user