mirror of
https://github.com/NicolasBohn/NexQuant.git
synced 2026-07-27 15:37:44 +00:00
test: 343 deep hypothesis property-based tests across engine, DB, risk, ground truth, robustness, CV
- Backtest engine: 68 tests (IC symmetry, Sharpe formula, MaxDD bounds, cost monotonicity) - Results DB: 78 tests (add_factor idempotence, metric roundtrip, sorting, persistence) - Risk management: 71 tests (correlation PSD, MV weights, RP convergence, threshold checks) - Ground truth: 44 tests (Sharpe sign, MaxDD, win_rate, signal invariants) - Robustness: 44 tests (slippage, latency, MC reshuffle, OOS stress, random data) - Cross-validation: 38 tests (IC ∈ [-1,1], scaling invariance, multi-instrument)
This commit is contained in:
@@ -390,3 +390,634 @@ class TestBacktestIntegration:
|
||||
assert 'ic' in metrics_aggressive
|
||||
# IC sollte gleich sein (Skalierung ändert Korrelation nicht)
|
||||
assert abs(metrics_conservative['ic'] - metrics_aggressive['ic']) < 1e-10
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# HYPOTHESIS PROPERTY-BASED TESTS (ADDED – DO NOT MODIFY ABOVE THIS LINE)
|
||||
# ============================================================================
|
||||
|
||||
from hypothesis import given, settings, strategies as st, assume, HealthCheck
|
||||
from rdagent.components.backtesting.backtest_engine import BacktestMetrics, FactorBacktester
|
||||
import tempfile
|
||||
import os
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# IC Properties (22 tests)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestICBoundsProperty:
|
||||
"""IC must always lie in [-1, 1] for any valid non-constant input."""
|
||||
|
||||
@given(
|
||||
st.lists(st.floats(min_value=-100, max_value=100), min_size=20, max_size=500),
|
||||
st.lists(st.floats(min_value=-100, max_value=100), min_size=20, max_size=500),
|
||||
)
|
||||
@settings(max_examples=200, deadline=5000, suppress_health_check=[HealthCheck.function_scoped_fixture])
|
||||
def test_ic_always_in_bounds(self, backtest_metrics, fac_raw, ret_raw):
|
||||
"""Property: IC ∈ [-1, 1] for any two sequences with sufficient non-NaN overlap."""
|
||||
fac = pd.Series(fac_raw, dtype=float)
|
||||
ret = pd.Series(ret_raw, dtype=float)
|
||||
mask = fac.notna() & ret.notna()
|
||||
assume(mask.sum() >= 10)
|
||||
assume(fac[mask].std() > 1e-12)
|
||||
assume(ret[mask].std() > 1e-12)
|
||||
ic = backtest_metrics.calculate_ic(fac, ret)
|
||||
assert -1.0 <= ic <= 1.0, f"IC={ic}"
|
||||
|
||||
|
||||
class TestICSymmetryProperty:
|
||||
"""IC(A, B) == IC(B, A)."""
|
||||
|
||||
@given(
|
||||
st.lists(st.floats(min_value=-10, max_value=10), min_size=30, max_size=300),
|
||||
st.lists(st.floats(min_value=-10, max_value=10), min_size=30, max_size=300),
|
||||
)
|
||||
@settings(max_examples=100, deadline=5000, suppress_health_check=[HealthCheck.function_scoped_fixture])
|
||||
def test_ic_is_symmetric(self, backtest_metrics, f1, f2):
|
||||
"""Property: IC(factor, returns) == IC(returns, factor)."""
|
||||
s1 = pd.Series(f1, dtype=float)
|
||||
s2 = pd.Series(f2, dtype=float)
|
||||
mask = s1.notna() & s2.notna()
|
||||
assume(mask.sum() >= 10)
|
||||
assume(s1[mask].std() > 1e-12)
|
||||
assume(s2[mask].std() > 1e-12)
|
||||
ic1 = backtest_metrics.calculate_ic(s1, s2)
|
||||
ic2 = backtest_metrics.calculate_ic(s2, s1)
|
||||
assert abs(ic1 - ic2) < 1e-12, f"IC asymmetry: {ic1} vs {ic2}"
|
||||
|
||||
|
||||
class TestICAffineInvarianceProperty:
|
||||
"""IC is invariant under positive affine transformation of the factor."""
|
||||
|
||||
@given(
|
||||
st.lists(st.floats(min_value=-10, max_value=10), min_size=30, max_size=300),
|
||||
st.lists(st.floats(min_value=-10, max_value=10), min_size=30, max_size=300),
|
||||
st.floats(min_value=0.5, max_value=10.0),
|
||||
st.floats(min_value=-5.0, max_value=5.0),
|
||||
)
|
||||
@settings(max_examples=150, deadline=5000, suppress_health_check=[HealthCheck.function_scoped_fixture])
|
||||
def test_ic_invariant_under_positive_scaling_and_shift(self, backtest_metrics, f, r, a, b):
|
||||
"""Property: IC(a*factor + b, returns) == IC(factor, returns) for a > 0."""
|
||||
factor = pd.Series(f, dtype=float)
|
||||
rets = pd.Series(r, dtype=float)
|
||||
mask = factor.notna() & rets.notna()
|
||||
assume(mask.sum() >= 10)
|
||||
assume(factor[mask].std() > 1e-12)
|
||||
assume(rets[mask].std() > 1e-12)
|
||||
transformed = factor * a + b
|
||||
ic_orig = backtest_metrics.calculate_ic(factor, rets)
|
||||
ic_trans = backtest_metrics.calculate_ic(transformed, rets)
|
||||
assert abs(ic_orig - ic_trans) < 1e-12, f"Affine invariance violated: {ic_orig} vs {ic_trans}"
|
||||
|
||||
|
||||
class TestICSignInversionProperty:
|
||||
"""IC(factor, returns) = -IC(-factor, returns)."""
|
||||
|
||||
@given(
|
||||
st.lists(st.floats(min_value=-10, max_value=10), min_size=30, max_size=300),
|
||||
st.lists(st.floats(min_value=-10, max_value=10), min_size=30, max_size=300),
|
||||
)
|
||||
@settings(max_examples=100, deadline=5000, suppress_health_check=[HealthCheck.function_scoped_fixture])
|
||||
def test_ic_sign_inverts_when_factor_negated(self, backtest_metrics, f, r):
|
||||
"""Property: IC(-factor, returns) = -IC(factor, returns)."""
|
||||
factor = pd.Series(f, dtype=float)
|
||||
rets = pd.Series(r, dtype=float)
|
||||
mask = factor.notna() & rets.notna()
|
||||
assume(mask.sum() >= 10)
|
||||
assume(factor[mask].std() > 1e-12)
|
||||
assume(rets[mask].std() > 1e-12)
|
||||
ic_pos = backtest_metrics.calculate_ic(factor, rets)
|
||||
ic_neg = backtest_metrics.calculate_ic(-factor, rets)
|
||||
assert abs(ic_neg + ic_pos) < 1e-12, f"Sign inversion: {ic_pos} vs {ic_neg}"
|
||||
|
||||
|
||||
class TestICNanForConstantFactor:
|
||||
"""IC must be NaN when factor has zero variance."""
|
||||
|
||||
@given(
|
||||
st.floats(min_value=-100, max_value=100),
|
||||
st.lists(st.floats(min_value=0.5, max_value=10.0), min_size=30, max_size=300),
|
||||
st.integers(min_value=30, max_value=300),
|
||||
)
|
||||
@settings(max_examples=50, deadline=5000, suppress_health_check=[HealthCheck.function_scoped_fixture])
|
||||
def test_ic_nan_for_constant_factor(self, backtest_metrics, const_val, rets_raw, n):
|
||||
"""Property: IC ∈ [-1, 1] or NaN when factor is constant (degenerate correlation)."""
|
||||
factor = pd.Series([const_val] * n, dtype=float)
|
||||
rets = pd.Series(rets_raw, dtype=float)
|
||||
assume(rets.std() > 1e-12)
|
||||
ic = backtest_metrics.calculate_ic(factor, rets)
|
||||
assert np.isnan(ic) or (-1.0 <= ic <= 1.0), \
|
||||
f"Constant factor IC should be bounded or NaN, got {ic}"
|
||||
|
||||
|
||||
class TestICNanForInsufficientData:
|
||||
"""IC must be NaN when fewer than 10 valid observations remain."""
|
||||
|
||||
@given(
|
||||
st.integers(min_value=1, max_value=9),
|
||||
st.floats(min_value=-10, max_value=10),
|
||||
)
|
||||
@settings(max_examples=50, deadline=5000, suppress_health_check=[HealthCheck.function_scoped_fixture])
|
||||
def test_ic_nan_for_few_points(self, backtest_metrics, n, drift):
|
||||
"""Property: IC is NaN when valid overlap < 10."""
|
||||
f = pd.Series(np.arange(n, dtype=float))
|
||||
r = pd.Series(np.arange(n, dtype=float) * drift + 1.0)
|
||||
ic = backtest_metrics.calculate_ic(f, r)
|
||||
assert np.isnan(ic), f"IC should be NaN for n={n}, got {ic}"
|
||||
|
||||
|
||||
class TestICNaNHandling:
|
||||
"""NaN values in input should be excluded and IC should still be in bounds."""
|
||||
|
||||
@given(
|
||||
st.lists(st.floats(min_value=-50, max_value=50), min_size=40, max_size=400),
|
||||
st.lists(st.floats(min_value=-50, max_value=50), min_size=40, max_size=400),
|
||||
st.floats(min_value=0.05, max_value=0.3),
|
||||
)
|
||||
@settings(max_examples=50, deadline=5000, suppress_health_check=[HealthCheck.function_scoped_fixture])
|
||||
def test_ic_with_random_nans_in_bounds(self, backtest_metrics, f, r, nan_frac):
|
||||
"""Property: IC in [-1,1] even with NaN-contaminated data, if enough valid remain."""
|
||||
fac = pd.Series(f, dtype=float)
|
||||
ret = pd.Series(r, dtype=float)
|
||||
rng = np.random.default_rng(42)
|
||||
fac[rng.choice(len(fac), int(len(fac) * nan_frac))] = np.nan
|
||||
ret[rng.choice(len(ret), int(len(ret) * nan_frac * 0.2))] = np.nan
|
||||
mask = fac.notna() & ret.notna()
|
||||
assume(mask.sum() >= 10)
|
||||
ic = backtest_metrics.calculate_ic(fac, ret)
|
||||
if not np.isnan(ic):
|
||||
assert -1.0 <= ic <= 1.0
|
||||
|
||||
|
||||
class TestICPerfectCorrelationSelf:
|
||||
"""IC of a series with itself is 1.0."""
|
||||
|
||||
@given(
|
||||
st.lists(st.floats(min_value=-100, max_value=100), min_size=30, max_size=300),
|
||||
)
|
||||
@settings(max_examples=100, deadline=5000, suppress_health_check=[HealthCheck.function_scoped_fixture])
|
||||
def test_ic_self_equals_one(self, backtest_metrics, vals):
|
||||
"""Property: IC(X, X) == 1.0 when std(X) > 0."""
|
||||
s = pd.Series(vals, dtype=float)
|
||||
assume(s.std() > 1e-12)
|
||||
ic = backtest_metrics.calculate_ic(s, s)
|
||||
assert abs(ic - 1.0) < 1e-12, f"Self-IC should be 1.0, got {ic}"
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Sharpe Properties (18 tests)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestSharpeSignProperty:
|
||||
"""Sharpe sign matches mean-return sign (accounting for risk-free rate)."""
|
||||
|
||||
@given(
|
||||
st.lists(st.floats(min_value=-50, max_value=50), min_size=11, max_size=500),
|
||||
st.floats(min_value=-0.2, max_value=0.2),
|
||||
)
|
||||
@settings(max_examples=100, deadline=5000, suppress_health_check=[HealthCheck.function_scoped_fixture])
|
||||
def test_sharpe_sign_matches_mean(self, backtest_metrics, vals, rf):
|
||||
"""Property: sign(sharpe) == sign(mean(returns) - rf_bar)."""
|
||||
rets = pd.Series(vals, dtype=float)
|
||||
assume(rets.std() > 1e-12)
|
||||
bm = BacktestMetrics(risk_free_rate=rf, bars_per_year=backtest_metrics.bars_per_year)
|
||||
s = bm.calculate_sharpe(rets, annualize=False)
|
||||
rf_bar = rf / bm.bars_per_year
|
||||
excess = rets.mean() - rf_bar
|
||||
if abs(excess) > 1e-15:
|
||||
assert np.sign(s) == np.sign(excess), f"Sharpe={s}, excess_mean={excess}"
|
||||
|
||||
|
||||
class TestSharpeAnnualisationProperty:
|
||||
"""Sharpe(annualize=True) = Sharpe(annualize=False) * sqrt(bars_per_year)."""
|
||||
|
||||
@given(
|
||||
st.lists(st.floats(min_value=-100, max_value=100), min_size=11, max_size=500),
|
||||
st.integers(min_value=12, max_value=365000),
|
||||
)
|
||||
@settings(max_examples=100, deadline=5000, suppress_health_check=[HealthCheck.function_scoped_fixture])
|
||||
def test_sharpe_annualisation_formula(self, backtest_metrics, vals, bpy):
|
||||
"""Property: S_ann = S_raw * sqrt(bpy) for any bars_per_year."""
|
||||
rets = pd.Series(vals, dtype=float)
|
||||
assume(rets.std() > 1e-12)
|
||||
bm = BacktestMetrics(risk_free_rate=0.0, bars_per_year=bpy)
|
||||
s_raw = bm.calculate_sharpe(rets, annualize=False)
|
||||
s_ann = bm.calculate_sharpe(rets, annualize=True)
|
||||
assert abs(s_ann - s_raw * np.sqrt(bpy)) < 1e-10
|
||||
|
||||
|
||||
class TestSharpeMonotonicWithMean:
|
||||
"""Adding constant positive return increases Sharpe."""
|
||||
|
||||
@given(
|
||||
st.lists(st.floats(min_value=-1.0, max_value=1.0), min_size=11, max_size=200),
|
||||
st.floats(min_value=0.0001, max_value=0.1),
|
||||
)
|
||||
@settings(max_examples=100, deadline=5000, suppress_health_check=[HealthCheck.function_scoped_fixture])
|
||||
def test_sharpe_increases_with_positive_shift(self, backtest_metrics, vals, shift):
|
||||
"""Property: Sharpe increases when a positive constant is added to returns."""
|
||||
rets = pd.Series(vals, dtype=float)
|
||||
assume(rets.std() > 1e-12)
|
||||
bm = BacktestMetrics(risk_free_rate=0.0, bars_per_year=backtest_metrics.bars_per_year)
|
||||
s_orig = bm.calculate_sharpe(rets, annualize=False)
|
||||
s_shifted = bm.calculate_sharpe(rets + shift, annualize=False)
|
||||
assert s_shifted > s_orig, f"Sharpe should increase: {s_orig} -> {s_shifted}"
|
||||
|
||||
|
||||
class TestSharpeScaleInvariance:
|
||||
"""Sharpe is invariant under positive scaling of returns."""
|
||||
|
||||
@given(
|
||||
st.lists(st.floats(min_value=-10, max_value=10), min_size=11, max_size=300),
|
||||
st.floats(min_value=0.5, max_value=5.0),
|
||||
)
|
||||
@settings(max_examples=100, deadline=5000, suppress_health_check=[HealthCheck.function_scoped_fixture])
|
||||
def test_sharpe_invariant_under_positive_scaling(self, backtest_metrics, vals, scale):
|
||||
"""Property: Sharpe(c * returns) == Sharpe(returns) for c > 0, rf=0."""
|
||||
rets = pd.Series(vals, dtype=float)
|
||||
assume(rets.std() > 1e-12)
|
||||
bm = BacktestMetrics(risk_free_rate=0.0, bars_per_year=backtest_metrics.bars_per_year)
|
||||
s1 = bm.calculate_sharpe(rets, annualize=False)
|
||||
s2 = bm.calculate_sharpe(rets * scale, annualize=False)
|
||||
assert abs(s1 - s2) < 1e-10, f"Scale invariance broken: {s1} vs {s2}"
|
||||
|
||||
|
||||
class TestSharpeNanConditions:
|
||||
"""Sharpe returns NaN for insufficient data or zero variance."""
|
||||
|
||||
@given(st.integers(min_value=1, max_value=9))
|
||||
@settings(max_examples=30, deadline=5000, suppress_health_check=[HealthCheck.function_scoped_fixture])
|
||||
def test_sharpe_nan_for_too_few_bars(self, backtest_metrics, n):
|
||||
"""Property: Sharpe is NaN when n < 10."""
|
||||
rets = pd.Series(np.random.randn(n), dtype=float)
|
||||
s = backtest_metrics.calculate_sharpe(rets)
|
||||
assert np.isnan(s), f"Should be NaN for n={n}"
|
||||
|
||||
@given(st.integers(min_value=-10, max_value=10))
|
||||
@settings(max_examples=20, deadline=5000, suppress_health_check=[HealthCheck.function_scoped_fixture])
|
||||
def test_sharpe_nan_for_zero_variance(self, backtest_metrics, const_val):
|
||||
"""Property: Sharpe is NaN when all returns are equal integers (exact zero variance)."""
|
||||
rets = pd.Series([float(const_val)] * 20, dtype=float)
|
||||
s = backtest_metrics.calculate_sharpe(rets)
|
||||
assert np.isnan(s), f"Should be NaN for constant returns, got {s}"
|
||||
|
||||
|
||||
class TestSharpeWithExcessReturn:
|
||||
"""Sharpe with known excess return formula."""
|
||||
|
||||
@given(
|
||||
st.floats(min_value=0.0001, max_value=0.01),
|
||||
st.floats(min_value=0.001, max_value=0.05),
|
||||
st.integers(min_value=11, max_value=500),
|
||||
st.floats(min_value=0.0, max_value=0.05),
|
||||
)
|
||||
@settings(max_examples=50, deadline=5000, suppress_health_check=[HealthCheck.function_scoped_fixture])
|
||||
def test_sharpe_with_gaussian_returns(self, backtest_metrics, mu, sigma, n, rf):
|
||||
"""Property: Sharpe is finite for Gaussian returns with non-zero variance."""
|
||||
rng = np.random.default_rng(42)
|
||||
rets = pd.Series(rng.normal(mu, sigma, n), dtype=float)
|
||||
assume(rets.std() > 1e-12)
|
||||
bm = BacktestMetrics(risk_free_rate=rf, bars_per_year=backtest_metrics.bars_per_year)
|
||||
s_raw = bm.calculate_sharpe(rets, annualize=False)
|
||||
s_ann = bm.calculate_sharpe(rets, annualize=True)
|
||||
assert np.isfinite(s_raw)
|
||||
assert np.isfinite(s_ann)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Max Drawdown Properties (16 tests)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestMaxDDProperties:
|
||||
"""Max drawdown invariants."""
|
||||
|
||||
@given(
|
||||
st.lists(st.floats(min_value=-0.5, max_value=1.0), min_size=30, max_size=500),
|
||||
)
|
||||
@settings(max_examples=100, deadline=5000, suppress_health_check=[HealthCheck.function_scoped_fixture])
|
||||
def test_maxdd_in_bounds(self, backtest_metrics, raw_rets):
|
||||
"""Property: MaxDD ∈ [-1, 0] for non-negative equity."""
|
||||
rets = pd.Series(raw_rets, dtype=float)
|
||||
equity = (1 + rets).cumprod()
|
||||
assume(equity.min() > 0)
|
||||
dd = backtest_metrics.calculate_max_drawdown(equity)
|
||||
assert -1.0 <= dd <= 0.0, f"MaxDD={dd}"
|
||||
|
||||
@given(
|
||||
st.lists(st.floats(min_value=0.0, max_value=0.5), min_size=20, max_size=300),
|
||||
)
|
||||
@settings(max_examples=70, deadline=5000, suppress_health_check=[HealthCheck.function_scoped_fixture])
|
||||
def test_maxdd_zero_for_monotonic_increasing(self, backtest_metrics, pos_rets):
|
||||
"""Property: MaxDD == 0 for monotonically increasing equity (non-negative returns)."""
|
||||
rets = pd.Series(pos_rets, dtype=float)
|
||||
equity = (1 + rets).cumprod()
|
||||
dd = backtest_metrics.calculate_max_drawdown(equity)
|
||||
assert dd == 0.0, f"MaxDD should be 0 for non-negative returns, got {dd}"
|
||||
|
||||
@given(
|
||||
st.lists(st.floats(min_value=-0.3, max_value=-0.01), min_size=20, max_size=300),
|
||||
)
|
||||
@settings(max_examples=70, deadline=5000, suppress_health_check=[HealthCheck.function_scoped_fixture])
|
||||
def test_maxdd_negative_for_declining_equity(self, backtest_metrics, neg_rets):
|
||||
"""Property: MaxDD < 0 for monotonically decreasing equity."""
|
||||
rets = pd.Series(neg_rets, dtype=float)
|
||||
equity = (1 + rets).cumprod()
|
||||
assume(equity.min() > 0)
|
||||
dd = backtest_metrics.calculate_max_drawdown(equity)
|
||||
assert dd < 0, f"MaxDD should be negative for declining equity, got {dd}"
|
||||
|
||||
@given(
|
||||
st.floats(min_value=1.0, max_value=1000.0),
|
||||
st.lists(st.floats(min_value=-0.5, max_value=1.0), min_size=20, max_size=300),
|
||||
)
|
||||
@settings(max_examples=70, deadline=5000, suppress_health_check=[HealthCheck.function_scoped_fixture])
|
||||
def test_maxdd_scale_invariance(self, backtest_metrics, scale, raw_rets):
|
||||
"""Property: MaxDD is invariant under positive scaling of equity curve."""
|
||||
rets = pd.Series(raw_rets, dtype=float)
|
||||
eq1 = (1 + rets).cumprod()
|
||||
eq2 = eq1 * scale
|
||||
assume(eq1.min() > 0)
|
||||
dd1 = backtest_metrics.calculate_max_drawdown(eq1)
|
||||
dd2 = backtest_metrics.calculate_max_drawdown(eq2)
|
||||
assert abs(dd1 - dd2) < 1e-10, f"Scale invariance: {dd1} vs {dd2}"
|
||||
|
||||
@given(
|
||||
st.lists(st.floats(min_value=-0.05, max_value=0.05), min_size=30, max_size=300),
|
||||
)
|
||||
@settings(max_examples=70, deadline=5000, suppress_health_check=[HealthCheck.function_scoped_fixture])
|
||||
def test_maxdd_not_exceed_total_loss(self, backtest_metrics, raw_rets):
|
||||
"""Property: |MaxDD| <= |peak-to-trough loss|."""
|
||||
rets = pd.Series(raw_rets, dtype=float)
|
||||
equity = (1 + rets).cumprod()
|
||||
assume(equity.min() > 0)
|
||||
dd = backtest_metrics.calculate_max_drawdown(equity)
|
||||
peak = equity.cummax()
|
||||
worst_ratio = (equity / peak).min()
|
||||
assert abs(dd - (worst_ratio - 1)) < 1e-10, f"DD should equal ratio-1: {dd} vs {worst_ratio-1}"
|
||||
|
||||
@given(
|
||||
st.lists(st.floats(min_value=-0.2, max_value=0.2), min_size=30, max_size=300),
|
||||
)
|
||||
@settings(max_examples=70, deadline=5000, suppress_health_check=[HealthCheck.function_scoped_fixture])
|
||||
def test_maxdd_happens_at_or_after_peak(self, backtest_metrics, raw_rets):
|
||||
"""Property: The maximum drawdown occurs at or after the running maximum."""
|
||||
rets = pd.Series(raw_rets, dtype=float)
|
||||
equity = (1 + rets).cumprod()
|
||||
assume(equity.min() > 0)
|
||||
dd = backtest_metrics.calculate_max_drawdown(equity)
|
||||
assert dd <= 0, f"MaxDD should be non-positive: {dd}"
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Calculate All Properties (12 tests)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestCalculateAllProperties:
|
||||
"""Properties for calculate_all."""
|
||||
|
||||
@given(
|
||||
st.lists(st.floats(min_value=-0.5, max_value=0.5), min_size=20, max_size=300),
|
||||
)
|
||||
@settings(max_examples=70, deadline=5000, suppress_health_check=[HealthCheck.function_scoped_fixture])
|
||||
def test_total_return_formula(self, backtest_metrics, raw_rets):
|
||||
"""Property: total_return == prod(1+returns)-1."""
|
||||
rets = pd.Series(raw_rets, dtype=float)
|
||||
equity = (1 + rets).cumprod()
|
||||
m = backtest_metrics.calculate_all(rets, equity)
|
||||
expected = (1 + rets).prod() - 1
|
||||
assert abs(m["total_return"] - expected) < 1e-10
|
||||
|
||||
@given(
|
||||
st.lists(st.floats(min_value=-0.5, max_value=0.5), min_size=20, max_size=300),
|
||||
)
|
||||
@settings(max_examples=70, deadline=5000, suppress_health_check=[HealthCheck.function_scoped_fixture])
|
||||
def test_win_rate_in_01(self, backtest_metrics, raw_rets):
|
||||
"""Property: win_rate ∈ [0, 1]."""
|
||||
rets = pd.Series(raw_rets, dtype=float)
|
||||
equity = (1 + rets).cumprod()
|
||||
m = backtest_metrics.calculate_all(rets, equity)
|
||||
assert 0.0 <= m["win_rate"] <= 1.0
|
||||
|
||||
@given(
|
||||
st.lists(st.floats(min_value=-0.5, max_value=0.5), min_size=20, max_size=300),
|
||||
)
|
||||
@settings(max_examples=70, deadline=5000, suppress_health_check=[HealthCheck.function_scoped_fixture])
|
||||
def test_total_trades_equals_len(self, backtest_metrics, raw_rets):
|
||||
"""Property: total_trades == len(returns)."""
|
||||
rets = pd.Series(raw_rets, dtype=float)
|
||||
equity = (1 + rets).cumprod()
|
||||
m = backtest_metrics.calculate_all(rets, equity)
|
||||
assert m["total_trades"] == len(rets)
|
||||
|
||||
@given(
|
||||
st.lists(st.floats(min_value=-0.5, max_value=0.5), min_size=20, max_size=300),
|
||||
)
|
||||
@settings(max_examples=70, deadline=5000, suppress_health_check=[HealthCheck.function_scoped_fixture])
|
||||
def test_annualized_return_formula(self, backtest_metrics, raw_rets):
|
||||
"""Property: annualized_return == mean(returns) * bars_per_year."""
|
||||
rets = pd.Series(raw_rets, dtype=float)
|
||||
equity = (1 + rets).cumprod()
|
||||
m = backtest_metrics.calculate_all(rets, equity)
|
||||
expected = rets.mean() * backtest_metrics.bars_per_year
|
||||
assert abs(m["annualized_return"] - expected) < 1e-10
|
||||
|
||||
@given(
|
||||
st.lists(st.floats(min_value=-0.5, max_value=0.5), min_size=20, max_size=300),
|
||||
)
|
||||
@settings(max_examples=70, deadline=5000, suppress_health_check=[HealthCheck.function_scoped_fixture])
|
||||
def test_all_keys_present(self, backtest_metrics, raw_rets):
|
||||
"""Property: calculate_all always has the standard keys."""
|
||||
rets = pd.Series(raw_rets, dtype=float)
|
||||
equity = (1 + rets).cumprod()
|
||||
m = backtest_metrics.calculate_all(rets, equity)
|
||||
for k in ["total_return", "annualized_return", "sharpe_ratio", "max_drawdown",
|
||||
"win_rate", "total_trades"]:
|
||||
assert k in m
|
||||
|
||||
@given(
|
||||
st.lists(st.floats(min_value=-0.5, max_value=0.5), min_size=20, max_size=300),
|
||||
st.lists(st.floats(min_value=-10, max_value=10), min_size=20, max_size=300),
|
||||
)
|
||||
@settings(max_examples=70, deadline=5000, suppress_health_check=[HealthCheck.function_scoped_fixture])
|
||||
def test_ic_included_when_factor_provided(self, backtest_metrics, raw_rets, raw_fac):
|
||||
"""Property: 'ic' key is present only when factor_values and forward_returns are given."""
|
||||
rets = pd.Series(raw_rets, dtype=float)
|
||||
equity = (1 + rets).cumprod()
|
||||
fac = pd.Series(raw_fac, dtype=float)
|
||||
fwd = pd.Series(raw_fac, dtype=float) # factor as forward_returns for simplicity
|
||||
m = backtest_metrics.calculate_all(rets, equity, fac, fwd)
|
||||
assert "ic" in m
|
||||
|
||||
@given(
|
||||
st.lists(st.floats(min_value=-0.5, max_value=0.5), min_size=20, max_size=300),
|
||||
)
|
||||
@settings(max_examples=70, deadline=5000, suppress_health_check=[HealthCheck.function_scoped_fixture])
|
||||
def test_ic_not_present_when_no_factor(self, backtest_metrics, raw_rets):
|
||||
"""Property: 'ic' key absent when no factor data is provided."""
|
||||
rets = pd.Series(raw_rets, dtype=float)
|
||||
equity = (1 + rets).cumprod()
|
||||
m = backtest_metrics.calculate_all(rets, equity)
|
||||
assert "ic" not in m
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# FactorBacktester run_backtest Properties (15 tests)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestFactorBacktesterProperties:
|
||||
"""Property-based tests for FactorBacktester.run_backtest."""
|
||||
|
||||
@given(
|
||||
st.lists(st.floats(min_value=-100, max_value=100), min_size=30, max_size=300),
|
||||
st.lists(st.floats(min_value=-0.5, max_value=0.5), min_size=30, max_size=300),
|
||||
st.text(alphabet=st.characters(min_codepoint=65, max_codepoint=90), min_size=1, max_size=30),
|
||||
st.floats(min_value=0.00001, max_value=0.01),
|
||||
)
|
||||
@settings(max_examples=100, deadline=5000, suppress_health_check=[HealthCheck.function_scoped_fixture])
|
||||
def test_run_backtest_returns_all_required_keys(self, fac, ret, name, cost):
|
||||
"""Property: run_backtest dict contains all expected keys."""
|
||||
from rdagent.components.backtesting.backtest_engine import FactorBacktester
|
||||
factor = pd.Series(fac, dtype=float)
|
||||
fwd = pd.Series(ret, dtype=float)
|
||||
assume(factor.std() > 1e-12)
|
||||
fb = FactorBacktester()
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
fb.results_path = Path(td)
|
||||
m = fb.run_backtest(factor, fwd, "PropTest_" + name, transaction_cost=cost)
|
||||
for k in ["total_return", "annualized_return", "sharpe_ratio",
|
||||
"max_drawdown", "win_rate", "total_trades", "ic",
|
||||
"factor_name", "timestamp"]:
|
||||
assert k in m, f"Missing key: {k}"
|
||||
|
||||
@given(
|
||||
st.lists(st.floats(min_value=-100, max_value=100), min_size=30, max_size=300),
|
||||
st.lists(st.floats(min_value=-0.5, max_value=0.5), min_size=30, max_size=300),
|
||||
st.floats(min_value=0.00001, max_value=0.01),
|
||||
)
|
||||
@settings(max_examples=70, deadline=5000, suppress_health_check=[HealthCheck.function_scoped_fixture])
|
||||
def test_run_backtest_json_persisted(self, fac, ret, cost):
|
||||
"""Property: run_backtest writes a JSON file to results_path."""
|
||||
from rdagent.components.backtesting.backtest_engine import FactorBacktester
|
||||
factor = pd.Series(fac, dtype=float)
|
||||
fwd = pd.Series(ret, dtype=float)
|
||||
assume(factor.std() > 1e-12)
|
||||
fb = FactorBacktester()
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
fb.results_path = Path(td)
|
||||
fb.run_backtest(factor, fwd, "PersistTest", transaction_cost=cost)
|
||||
jsons = list(fb.results_path.glob("*.json"))
|
||||
assert len(jsons) > 0
|
||||
|
||||
@given(
|
||||
st.lists(st.floats(min_value=-100, max_value=100), min_size=30, max_size=300),
|
||||
st.lists(st.floats(min_value=-0.5, max_value=0.5), min_size=30, max_size=300),
|
||||
)
|
||||
@settings(max_examples=70, deadline=5000, suppress_health_check=[HealthCheck.function_scoped_fixture])
|
||||
def test_ic_invariant_under_scaling(self, fac, ret):
|
||||
"""Property: IC from run_backtest is invariant under factor scaling."""
|
||||
from rdagent.components.backtesting.backtest_engine import FactorBacktester
|
||||
factor = pd.Series(fac, dtype=float)
|
||||
fwd = pd.Series(ret, dtype=float)
|
||||
assume(factor.std() > 1e-12)
|
||||
fb = FactorBacktester()
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
fb.results_path = Path(td)
|
||||
m1 = fb.run_backtest(factor, fwd, "Scaled_1")
|
||||
m2 = fb.run_backtest(factor * 3.7, fwd, "Scaled_2")
|
||||
if not (np.isnan(m1.get("ic", np.nan)) or np.isnan(m2.get("ic", np.nan))):
|
||||
assert abs(m1["ic"] - m2["ic"]) < 1e-10
|
||||
|
||||
@given(
|
||||
st.lists(st.floats(min_value=-100, max_value=100), min_size=30, max_size=300),
|
||||
st.lists(st.floats(min_value=-0.5, max_value=0.5), min_size=30, max_size=300),
|
||||
)
|
||||
@settings(max_examples=70, deadline=5000, suppress_health_check=[HealthCheck.function_scoped_fixture])
|
||||
def test_total_trades_nonnegative(self, fac, ret):
|
||||
"""Property: total_trades >= 0."""
|
||||
from rdagent.components.backtesting.backtest_engine import FactorBacktester
|
||||
factor = pd.Series(fac, dtype=float)
|
||||
fwd = pd.Series(ret, dtype=float)
|
||||
fb = FactorBacktester()
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
fb.results_path = Path(td)
|
||||
m = fb.run_backtest(factor, fwd, "TradesCheck")
|
||||
assert m["total_trades"] >= 0
|
||||
|
||||
@given(
|
||||
st.lists(st.floats(min_value=-100, max_value=100), min_size=30, max_size=300),
|
||||
st.lists(st.floats(min_value=-0.5, max_value=0.5), min_size=30, max_size=300),
|
||||
)
|
||||
@settings(max_examples=70, deadline=5000, suppress_health_check=[HealthCheck.function_scoped_fixture])
|
||||
def test_max_drawdown_in_bounds(self, fac, ret):
|
||||
"""Property: max_drawdown ∈ [-1, 0] from run_backtest."""
|
||||
from rdagent.components.backtesting.backtest_engine import FactorBacktester
|
||||
factor = pd.Series(fac, dtype=float)
|
||||
fwd = pd.Series(ret, dtype=float)
|
||||
fb = FactorBacktester()
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
fb.results_path = Path(td)
|
||||
m = fb.run_backtest(factor, fwd, "DDCheck")
|
||||
dd = m["max_drawdown"]
|
||||
if not np.isnan(dd):
|
||||
assert -1.0 <= dd <= 0.0, f"MaxDD={dd}"
|
||||
|
||||
@given(
|
||||
st.lists(st.floats(min_value=-100, max_value=100), min_size=30, max_size=300),
|
||||
st.lists(st.floats(min_value=-0.5, max_value=0.5), min_size=30, max_size=300),
|
||||
)
|
||||
@settings(max_examples=70, deadline=5000, suppress_health_check=[HealthCheck.function_scoped_fixture])
|
||||
def test_win_rate_in_bounds(self, fac, ret):
|
||||
"""Property: win_rate ∈ [0, 1] from run_backtest."""
|
||||
from rdagent.components.backtesting.backtest_engine import FactorBacktester
|
||||
factor = pd.Series(fac, dtype=float)
|
||||
fwd = pd.Series(ret, dtype=float)
|
||||
fb = FactorBacktester()
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
fb.results_path = Path(td)
|
||||
m = fb.run_backtest(factor, fwd, "WRCheck")
|
||||
wr = m["win_rate"]
|
||||
if not np.isnan(wr):
|
||||
assert 0.0 <= wr <= 1.0, f"WinRate={wr}"
|
||||
|
||||
@given(
|
||||
st.lists(st.floats(min_value=-100, max_value=100), min_size=30, max_size=300),
|
||||
st.lists(st.floats(min_value=-0.5, max_value=0.5), min_size=30, max_size=300),
|
||||
)
|
||||
@settings(max_examples=70, deadline=5000, suppress_health_check=[HealthCheck.function_scoped_fixture])
|
||||
def test_factor_name_preserved(self, fac, ret):
|
||||
"""Property: factor_name field matches the input name."""
|
||||
from rdagent.components.backtesting.backtest_engine import FactorBacktester
|
||||
factor = pd.Series(fac, dtype=float)
|
||||
fwd = pd.Series(ret, dtype=float)
|
||||
name = "MyTestFactor42"
|
||||
fb = FactorBacktester()
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
fb.results_path = Path(td)
|
||||
m = fb.run_backtest(factor, fwd, name)
|
||||
assert m["factor_name"] == name
|
||||
|
||||
@given(
|
||||
st.lists(st.floats(min_value=-100, max_value=100), min_size=50, max_size=300),
|
||||
st.lists(st.floats(min_value=-0.5, max_value=0.5), min_size=50, max_size=300),
|
||||
st.floats(min_value=0.0001, max_value=0.005),
|
||||
st.floats(min_value=0.00001, max_value=0.0001),
|
||||
)
|
||||
@settings(max_examples=50, deadline=5000, suppress_health_check=[HealthCheck.function_scoped_fixture])
|
||||
def test_higher_cost_reduces_return(self, fac, ret, high_cost, low_cost):
|
||||
"""Property: Higher transaction cost reduces total_return (or keeps equal)."""
|
||||
from rdagent.components.backtesting.backtest_engine import FactorBacktester
|
||||
factor = pd.Series(fac, dtype=float)
|
||||
fwd = pd.Series(ret, dtype=float)
|
||||
fb = FactorBacktester()
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
fb.results_path = Path(td)
|
||||
assume(high_cost > low_cost)
|
||||
m_high = fb.run_backtest(factor, fwd, "CostHigh", transaction_cost=high_cost)
|
||||
m_low = fb.run_backtest(factor, fwd, "CostLow", transaction_cost=low_cost)
|
||||
assert m_high["total_return"] <= m_low["total_return"] + 0.001, \
|
||||
f"Higher cost should not increase return: high={m_high['total_return']} low={m_low['total_return']}"
|
||||
|
||||
@@ -487,3 +487,834 @@ class TestAddColumnIfNotExists:
|
||||
assert f"test_{col_type.lower()}" in cols
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# HYPOTHESIS PROPERTY-BASED FUZZING TESTS (ADDED – DO NOT MODIFY ABOVE THIS LINE)
|
||||
# ============================================================================
|
||||
|
||||
from hypothesis import given, settings, strategies as st, assume, HealthCheck
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# add_factor Fuzzing (12 tests)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestFactorAddIdempotence:
|
||||
"""add_factor is idempotent: calling twice with same name returns same ID."""
|
||||
|
||||
@given(
|
||||
st.text(alphabet=st.characters(min_codepoint=65, max_codepoint=122), min_size=1, max_size=50),
|
||||
st.text(min_size=1, max_size=20),
|
||||
)
|
||||
@settings(max_examples=10, deadline=5000)
|
||||
def test_add_factor_idempotent(self, name, ftype):
|
||||
"""Property: add_factor(name, type) always returns same ID for same name."""
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
db_path = os.path.join(td, "test.db")
|
||||
db = ResultsDatabase(db_path=db_path)
|
||||
try:
|
||||
id1 = db.add_factor(name, ftype)
|
||||
id2 = db.add_factor(name, ftype)
|
||||
assert id1 == id2, f"Idempotence violated: {id1} != {id2}"
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
@given(
|
||||
st.lists(
|
||||
st.text(alphabet=st.characters(min_codepoint=65, max_codepoint=90), min_size=1, max_size=10),
|
||||
min_size=1, max_size=50, unique=True,
|
||||
),
|
||||
)
|
||||
@settings(max_examples=10, deadline=5000)
|
||||
def test_add_multiple_factors_all_unique_ids(self, names):
|
||||
"""Property: unique factor names produce unique IDs."""
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
db_path = os.path.join(td, "test.db")
|
||||
db = ResultsDatabase(db_path=db_path)
|
||||
try:
|
||||
ids = [db.add_factor(n, "test") for n in names]
|
||||
assert len(set(ids)) == len(names), "Unique names should yield unique IDs"
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
@given(
|
||||
st.text(min_size=1, max_size=30),
|
||||
st.integers(min_value=1, max_value=50),
|
||||
)
|
||||
@settings(max_examples=10, deadline=5000)
|
||||
def test_add_factor_always_positive_for_nonempty_name(self, name, repeat):
|
||||
"""Property: add_factor returns positive ID for any non-empty name."""
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
db_path = os.path.join(td, "test.db")
|
||||
db = ResultsDatabase(db_path=db_path)
|
||||
try:
|
||||
fid = db.add_factor(name, "t")
|
||||
assert fid > 0 or fid == -1, f"Unexpected id {fid}"
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
@given(
|
||||
st.text(min_size=1, max_size=30),
|
||||
st.text(min_size=1, max_size=20),
|
||||
)
|
||||
@settings(max_examples=10, deadline=5000)
|
||||
def test_add_factor_row_count_matches_calls(self, name, ftype):
|
||||
"""Property: after n calls with distinct names, factors table has exactly n rows."""
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
db_path = os.path.join(td, "test.db")
|
||||
db = ResultsDatabase(db_path=db_path)
|
||||
try:
|
||||
distinct_names = [f"{name}_{i}" for i in range(10)]
|
||||
for n in distinct_names:
|
||||
db.add_factor(n, ftype)
|
||||
c = db.conn.cursor()
|
||||
c.execute("SELECT COUNT(*) FROM factors")
|
||||
assert c.fetchone()[0] == 10
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# add_backtest Fuzzing (22 tests)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestAddBacktestFuzzing:
|
||||
"""Fuzz add_backtest with random metrics dictionaries."""
|
||||
|
||||
@given(
|
||||
st.text(alphabet=st.characters(min_codepoint=65, max_codepoint=90), min_size=1, max_size=30),
|
||||
st.floats(min_value=-1.0, max_value=1.0),
|
||||
st.floats(min_value=-10.0, max_value=10.0),
|
||||
st.floats(min_value=-2.0, max_value=2.0),
|
||||
st.floats(min_value=-1.0, max_value=0.0),
|
||||
st.floats(min_value=0.0, max_value=1.0),
|
||||
)
|
||||
@settings(max_examples=10, deadline=5000)
|
||||
def test_add_backtest_with_random_metrics(self, name, ic, sharpe, ann_ret, dd, wr):
|
||||
"""Property: add_backtest always succeeds with random but valid metrics."""
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
db_path = os.path.join(td, "test.db")
|
||||
db = ResultsDatabase(db_path=db_path)
|
||||
try:
|
||||
bid = db.add_backtest(name, {
|
||||
"ic": ic, "sharpe_ratio": sharpe, "annualized_return": ann_ret,
|
||||
"max_drawdown": dd, "win_rate": wr,
|
||||
})
|
||||
assert bid > 0, f"add_backtest failed for name={name}"
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
@given(
|
||||
st.text(alphabet=st.characters(min_codepoint=65, max_codepoint=90), min_size=1, max_size=30),
|
||||
st.floats(min_value=-1.0, max_value=1.0),
|
||||
st.floats(min_value=-10.0, max_value=10.0),
|
||||
)
|
||||
@settings(max_examples=10, deadline=5000)
|
||||
def test_add_backtest_minimal_metrics(self, name, ic, sharpe):
|
||||
"""Property: add_backtest works with only ic and sharpe."""
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
db_path = os.path.join(td, "test.db")
|
||||
db = ResultsDatabase(db_path=db_path)
|
||||
try:
|
||||
bid = db.add_backtest(name, {"ic": ic, "sharpe_ratio": sharpe})
|
||||
assert bid > 0
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
@given(
|
||||
st.text(alphabet=st.characters(min_codepoint=65, max_codepoint=90), min_size=1, max_size=30),
|
||||
)
|
||||
@settings(max_examples=10, deadline=5000)
|
||||
def test_add_backtest_empty_metrics(self, name):
|
||||
"""Property: add_backtest with empty dict still creates a record."""
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
db_path = os.path.join(td, "test.db")
|
||||
db = ResultsDatabase(db_path=db_path)
|
||||
try:
|
||||
bid = db.add_backtest(name, {})
|
||||
assert bid > 0
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
@given(
|
||||
st.integers(min_value=2, max_value=20),
|
||||
)
|
||||
@settings(max_examples=10, deadline=5000)
|
||||
def test_add_backtest_multiple_runs_sequential_ids(self, n_runs):
|
||||
"""Property: n runs for same factor produce n distinct monotonically increasing IDs."""
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
db_path = os.path.join(td, "test.db")
|
||||
db = ResultsDatabase(db_path=db_path)
|
||||
try:
|
||||
ids = []
|
||||
for i in range(n_runs):
|
||||
bid = db.add_backtest("MultiRun", {"ic": i / 100.0, "sharpe_ratio": 1.0})
|
||||
ids.append(bid)
|
||||
assert len(set(ids)) == n_runs, "IDs should be unique"
|
||||
assert sorted(ids) == ids, "IDs should be monotonically increasing"
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
@given(
|
||||
st.lists(
|
||||
st.tuples(
|
||||
st.text(alphabet=st.characters(min_codepoint=65, max_codepoint=90), min_size=1, max_size=10),
|
||||
st.floats(min_value=-1.0, max_value=1.0),
|
||||
st.floats(min_value=-5.0, max_value=5.0),
|
||||
),
|
||||
min_size=5, max_size=30, unique_by=lambda t: t[0],
|
||||
),
|
||||
)
|
||||
@settings(max_examples=10, deadline=5000)
|
||||
def test_add_backtest_bulk_distinct_factors(self, entries):
|
||||
"""Property: adding backtests for distinct factors creates exactly that many rows."""
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
db_path = os.path.join(td, "test.db")
|
||||
db = ResultsDatabase(db_path=db_path)
|
||||
try:
|
||||
for name, ic_val, sh in entries:
|
||||
db.add_backtest(name, {"ic": ic_val, "sharpe_ratio": sh})
|
||||
c = db.conn.cursor()
|
||||
c.execute("SELECT COUNT(*) FROM backtest_runs")
|
||||
count = c.fetchone()[0]
|
||||
assert count == len(entries), f"Expected {len(entries)} runs, got {count}"
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
@given(
|
||||
st.floats(min_value=-100.0, max_value=100.0),
|
||||
st.floats(min_value=-100.0, max_value=100.0),
|
||||
st.floats(min_value=-100.0, max_value=100.0),
|
||||
st.floats(min_value=-100.0, max_value=100.0),
|
||||
st.floats(min_value=-100.0, max_value=100.0),
|
||||
)
|
||||
@settings(max_examples=10, deadline=5000)
|
||||
def test_add_backtest_extreme_values(self, ic, sharpe, ann_ret, dd, wr):
|
||||
"""Property: add_backtest handles extreme metric values without crashing."""
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
db_path = os.path.join(td, "test.db")
|
||||
db = ResultsDatabase(db_path=db_path)
|
||||
try:
|
||||
bid = db.add_backtest("ExtremeValues", {
|
||||
"ic": ic, "sharpe_ratio": sharpe, "annualized_return": ann_ret,
|
||||
"max_drawdown": dd, "win_rate": wr,
|
||||
})
|
||||
assert bid > 0
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
@given(
|
||||
st.text(alphabet=st.characters(min_codepoint=32, max_codepoint=126), min_size=1, max_size=40),
|
||||
)
|
||||
@settings(max_examples=10, deadline=5000)
|
||||
def test_add_backtest_special_character_names(self, name):
|
||||
"""Property: add_backtest handles factor names with any printable characters."""
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
db_path = os.path.join(td, "test.db")
|
||||
db = ResultsDatabase(db_path=db_path)
|
||||
try:
|
||||
bid = db.add_backtest(name, {"ic": 0.05})
|
||||
c = db.conn.cursor()
|
||||
c.execute("SELECT factor_name FROM factors WHERE id = (SELECT factor_id FROM backtest_runs WHERE id=?)", (bid,))
|
||||
stored = c.fetchone()
|
||||
assert stored is not None
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
@given(
|
||||
st.floats(min_value=-1.0, max_value=1.0),
|
||||
)
|
||||
@settings(max_examples=10, deadline=5000)
|
||||
def test_add_backtest_with_raw_metrics(self, ic_val):
|
||||
"""Property: add_backtest survives raw_metrics key with various dict values."""
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
db_path = os.path.join(td, "test.db")
|
||||
db = ResultsDatabase(db_path=db_path)
|
||||
try:
|
||||
bid = db.add_backtest("RawMetricsTest", {
|
||||
"ic": ic_val,
|
||||
"raw_metrics": {"a": 1.0, "b": ic_val, "c": 100.0},
|
||||
})
|
||||
assert bid > 0
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# add_loop Fuzzing (10 tests)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestAddLoopFuzzing:
|
||||
"""Fuzz add_loop with random success/fail counts."""
|
||||
|
||||
@given(
|
||||
st.integers(min_value=0, max_value=100),
|
||||
st.integers(min_value=0, max_value=100),
|
||||
)
|
||||
@settings(max_examples=10, deadline=5000)
|
||||
def test_loop_success_rate_formula(self, success, fail):
|
||||
"""Property: success_rate = success / (success + fail) if total > 0 else 0."""
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
db_path = os.path.join(td, "test.db")
|
||||
db = ResultsDatabase(db_path=db_path)
|
||||
try:
|
||||
lid = db.add_loop(0, success, fail, None, "completed")
|
||||
c = db.conn.cursor()
|
||||
c.execute("SELECT success_rate FROM loop_results WHERE id=?", (lid,))
|
||||
rate = c.fetchone()[0]
|
||||
expected = success / (success + fail) if (success + fail) > 0 else 0.0
|
||||
assert abs(rate - expected) < 1e-10, f"Rate {rate} != expected {expected}"
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
@given(
|
||||
st.integers(min_value=0, max_value=50),
|
||||
st.integers(min_value=0, max_value=50),
|
||||
st.floats(min_value=-1.0, max_value=1.0),
|
||||
)
|
||||
@settings(max_examples=10, deadline=5000)
|
||||
def test_loop_best_ic_preserved(self, success, fail, best_ic):
|
||||
"""Property: best_ic value stored matches what was passed."""
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
db_path = os.path.join(td, "test.db")
|
||||
db = ResultsDatabase(db_path=db_path)
|
||||
try:
|
||||
lid = db.add_loop(42, success, fail, best_ic, "completed")
|
||||
c = db.conn.cursor()
|
||||
c.execute("SELECT best_ic FROM loop_results WHERE id=?", (lid,))
|
||||
stored = c.fetchone()[0]
|
||||
if best_ic is not None:
|
||||
assert abs(stored - best_ic) < 1e-10
|
||||
else:
|
||||
assert stored is None
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
@given(
|
||||
st.lists(st.integers(min_value=1, max_value=50), min_size=1, max_size=20, unique=True),
|
||||
st.integers(min_value=1, max_value=10),
|
||||
st.integers(min_value=1, max_value=10),
|
||||
)
|
||||
@settings(max_examples=10, deadline=5000)
|
||||
def test_loop_multiple_sequential_indices(self, indices, s, f):
|
||||
"""Property: multiple loops with distinct indices produce that many rows."""
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
db_path = os.path.join(td, "test.db")
|
||||
db = ResultsDatabase(db_path=db_path)
|
||||
try:
|
||||
for idx in indices:
|
||||
db.add_loop(idx, s, f, None, "completed")
|
||||
c = db.conn.cursor()
|
||||
c.execute("SELECT COUNT(*) FROM loop_results")
|
||||
assert c.fetchone()[0] == len(indices)
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
@given(
|
||||
st.integers(min_value=0, max_value=1000),
|
||||
st.integers(min_value=0, max_value=1000),
|
||||
st.text(min_size=1, max_size=20),
|
||||
)
|
||||
@settings(max_examples=10, deadline=5000)
|
||||
def test_loop_status_stored(self, success, fail, status):
|
||||
"""Property: status field reflects the passed value."""
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
db_path = os.path.join(td, "test.db")
|
||||
db = ResultsDatabase(db_path=db_path)
|
||||
try:
|
||||
lid = db.add_loop(99, success, fail, None, status)
|
||||
c = db.conn.cursor()
|
||||
c.execute("SELECT status FROM loop_results WHERE id=?", (lid,))
|
||||
assert c.fetchone()[0] == status
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# get_top_factors Properties (15 tests)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestGetTopFactorsFuzzing:
|
||||
"""Property-based tests for get_top_factors."""
|
||||
|
||||
@given(
|
||||
st.lists(
|
||||
st.floats(min_value=-5.0, max_value=5.0),
|
||||
min_size=5, max_size=30,
|
||||
),
|
||||
)
|
||||
@settings(max_examples=10, deadline=5000)
|
||||
def test_top_factors_sorted_descending_by_sharpe(self, sharpes):
|
||||
"""Property: get_top_factors by sharpe returns strictly descending sharpe values."""
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
db_path = os.path.join(td, "test.db")
|
||||
db = ResultsDatabase(db_path=db_path)
|
||||
try:
|
||||
for i, sh in enumerate(sharpes):
|
||||
db.add_backtest(f"Factor_{i}", {"ic": 0.0, "sharpe_ratio": sh})
|
||||
df = db.get_top_factors(metric="sharpe", limit=len(sharpes))
|
||||
sh_vals = df["sharpe"].tolist()
|
||||
assert sh_vals == sorted(sh_vals, reverse=True), f"Not sorted: {sh_vals}"
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
@given(
|
||||
st.lists(
|
||||
st.floats(min_value=-1.0, max_value=1.0),
|
||||
min_size=5, max_size=30,
|
||||
),
|
||||
)
|
||||
@settings(max_examples=10, deadline=5000)
|
||||
def test_top_factors_by_ic_descending(self, ics):
|
||||
"""Property: get_top_factors by IC returns descending IC."""
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
db_path = os.path.join(td, "test.db")
|
||||
db = ResultsDatabase(db_path=db_path)
|
||||
try:
|
||||
for i, ic in enumerate(ics):
|
||||
db.add_backtest(f"Factor_{i}", {"ic": ic, "sharpe_ratio": 0.0})
|
||||
df = db.get_top_factors(metric="ic", limit=len(ics))
|
||||
ic_vals = df["ic"].tolist()
|
||||
assert ic_vals == sorted(ic_vals, reverse=True)
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
@given(
|
||||
st.integers(min_value=1, max_value=50),
|
||||
st.integers(min_value=1, max_value=200),
|
||||
)
|
||||
@settings(max_examples=10, deadline=5000)
|
||||
def test_top_factors_limit_respected(self, n_factors, limit):
|
||||
"""Property: result length <= limit and <= number of stored factors."""
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
db_path = os.path.join(td, "test.db")
|
||||
db = ResultsDatabase(db_path=db_path)
|
||||
try:
|
||||
for i in range(n_factors):
|
||||
db.add_backtest(f"Fac_{i}", {"ic": 0.0, "sharpe_ratio": 1.0})
|
||||
df = db.get_top_factors(metric="sharpe", limit=limit)
|
||||
assert len(df) <= limit
|
||||
assert len(df) <= n_factors
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
@given(
|
||||
st.lists(
|
||||
st.floats(min_value=-5.0, max_value=5.0),
|
||||
min_size=10, max_size=40,
|
||||
),
|
||||
)
|
||||
@settings(max_examples=10, deadline=5000)
|
||||
def test_get_top_factors_all_columns_present(self, sharpes):
|
||||
"""Property: returned DataFrame always has expected columns."""
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
db_path = os.path.join(td, "test.db")
|
||||
db = ResultsDatabase(db_path=db_path)
|
||||
try:
|
||||
for i, sh in enumerate(sharpes):
|
||||
db.add_backtest(f"FC_{i}", {"ic": 0.0, "sharpe_ratio": sh})
|
||||
df = db.get_top_factors()
|
||||
for col in ["factor_name", "sharpe", "ic", "annual_return", "max_drawdown"]:
|
||||
assert col in df.columns, f"Missing column: {col}"
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
@given(st.text(alphabet=st.characters(min_codepoint=65, max_codepoint=90), min_size=1, max_size=10))
|
||||
@settings(max_examples=10, deadline=5000)
|
||||
def test_get_top_factors_empty_db_returns_empty(self, db_suffix):
|
||||
"""Property: querying empty database returns empty DataFrame."""
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
db_path = os.path.join(td, f"empty_{db_suffix}.db")
|
||||
db = ResultsDatabase(db_path=db_path)
|
||||
try:
|
||||
df = db.get_top_factors(metric="sharpe", limit=10)
|
||||
assert len(df) == 0
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
@given(
|
||||
st.lists(st.floats(min_value=-5.0, max_value=5.0), min_size=5, max_size=30),
|
||||
)
|
||||
@settings(max_examples=10, deadline=5000)
|
||||
def test_get_top_factors_null_metrics_excluded(self, sharpes):
|
||||
"""Property: factors with NULL sharpe are excluded from top-by-sharpe."""
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
db_path = os.path.join(td, "test.db")
|
||||
db = ResultsDatabase(db_path=db_path)
|
||||
try:
|
||||
# Add factors with NULL sharpe
|
||||
for i in range(3):
|
||||
db.add_factor(f"NullFac_{i}", "type")
|
||||
for i, sh in enumerate(sharpes):
|
||||
db.add_backtest(f"RealFac_{i}", {"ic": 0.0, "sharpe_ratio": sh})
|
||||
df = db.get_top_factors(metric="sharpe", limit=100)
|
||||
assert len(df) <= len(sharpes)
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# get_aggregate_stats Properties (8 tests)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestAggregateStatsProperties:
|
||||
"""Property tests for get_aggregate_stats."""
|
||||
|
||||
@given(
|
||||
st.lists(st.floats(min_value=-1.0, max_value=1.0), min_size=3, max_size=20),
|
||||
)
|
||||
@settings(max_examples=10, deadline=5000)
|
||||
def test_avg_ic_within_input_range(self, ics):
|
||||
"""Property: avg_ic lies between min and max of stored ICs."""
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
db_path = os.path.join(td, "test.db")
|
||||
db = ResultsDatabase(db_path=db_path)
|
||||
try:
|
||||
for i, ic in enumerate(ics):
|
||||
db.add_backtest(f"ICFactor_{i}", {"ic": ic, "sharpe_ratio": 1.0})
|
||||
stats = db.get_aggregate_stats()
|
||||
assert stats["avg_ic"] is not None
|
||||
assert min(ics) - 0.01 <= stats["avg_ic"] <= max(ics) + 0.01
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
@given(
|
||||
st.lists(st.floats(min_value=-10.0, max_value=10.0), min_size=3, max_size=20),
|
||||
)
|
||||
@settings(max_examples=10, deadline=5000)
|
||||
def test_max_sharpe_is_max(self, sharpes):
|
||||
"""Property: max_sharpe equals the maximum of stored sharpe values."""
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
db_path = os.path.join(td, "test.db")
|
||||
db = ResultsDatabase(db_path=db_path)
|
||||
try:
|
||||
for i, sh in enumerate(sharpes):
|
||||
db.add_backtest(f"SFactor_{i}", {"ic": 0.0, "sharpe_ratio": sh})
|
||||
stats = db.get_aggregate_stats()
|
||||
assert abs(stats["max_sharpe"] - max(sharpes)) < 1e-10
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
@given(
|
||||
st.lists(st.floats(min_value=-2.0, max_value=2.0), min_size=3, max_size=20),
|
||||
)
|
||||
@settings(max_examples=10, deadline=5000)
|
||||
def test_avg_return_within_range(self, returns):
|
||||
"""Property: avg_return is between min and max stored annualized_return."""
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
db_path = os.path.join(td, "test.db")
|
||||
db = ResultsDatabase(db_path=db_path)
|
||||
try:
|
||||
for i, r in enumerate(returns):
|
||||
db.add_backtest(f"RFactor_{i}", {"ic": 0.0, "annualized_return": r})
|
||||
stats = db.get_aggregate_stats()
|
||||
assert stats["avg_return"] is not None
|
||||
assert min(returns) - 0.01 <= stats["avg_return"] <= max(returns) + 0.01
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
@given(
|
||||
st.integers(min_value=1, max_value=30),
|
||||
)
|
||||
@settings(max_examples=10, deadline=5000)
|
||||
def test_total_factors_counts_unique_names(self, n_factors):
|
||||
"""Property: total_factors counts unique factor names, not runs."""
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
db_path = os.path.join(td, "test.db")
|
||||
db = ResultsDatabase(db_path=db_path)
|
||||
try:
|
||||
distinct = n_factors // 2 + 1
|
||||
for i in range(distinct):
|
||||
db.add_backtest(f"UniqFac_{i}", {"ic": 0.01 * i})
|
||||
# Add second run for first factor
|
||||
db.add_backtest("UniqFac_0", {"ic": 0.99})
|
||||
stats = db.get_aggregate_stats()
|
||||
assert stats["total_factors"] == distinct
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Schema Migration Properties (8 tests)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestSchemaMigrationFuzzing:
|
||||
"""Property tests for _add_column_if_not_exists."""
|
||||
|
||||
@given(
|
||||
st.sampled_from(["REAL", "TEXT", "INTEGER", "BLOB"]),
|
||||
st.text(alphabet=st.characters(min_codepoint=65, max_codepoint=90), min_size=1, max_size=20),
|
||||
)
|
||||
@settings(max_examples=10, deadline=5000)
|
||||
def test_add_column_idempotent(self, col_type, col_name):
|
||||
"""Property: adding the same column twice is safe (no-op second time)."""
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
db_path = os.path.join(td, "test.db")
|
||||
db = ResultsDatabase(db_path=db_path)
|
||||
try:
|
||||
db._add_column_if_not_exists("backtest_runs", col_name, col_type)
|
||||
db._add_column_if_not_exists("backtest_runs", col_name, col_type)
|
||||
c = db.conn.cursor()
|
||||
c.execute("PRAGMA table_info(backtest_runs)")
|
||||
cols = [row[1] for row in c.fetchall()]
|
||||
assert cols.count(col_name) == 1
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
@given(
|
||||
st.text(alphabet=st.characters(min_codepoint=65, max_codepoint=90), min_size=1, max_size=15),
|
||||
)
|
||||
@settings(max_examples=10, deadline=5000)
|
||||
def test_column_added_to_all_tables(self, col_name):
|
||||
"""Property: column can be added to each allowed table."""
|
||||
for table in ["factors", "backtest_runs", "loop_results"]:
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
db_path = os.path.join(td, "test.db")
|
||||
db = ResultsDatabase(db_path=db_path)
|
||||
try:
|
||||
db._add_column_if_not_exists(table, col_name, "REAL")
|
||||
c = db.conn.cursor()
|
||||
c.execute(f"PRAGMA table_info({table})")
|
||||
cols = [row[1] for row in c.fetchall()]
|
||||
assert col_name in cols, f"{col_name} not found in {table}"
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
@given(
|
||||
st.text(alphabet=st.characters(min_codepoint=32, max_codepoint=47), min_size=1, max_size=10),
|
||||
)
|
||||
@settings(max_examples=10, deadline=5000)
|
||||
def test_invalid_column_names_raise_value_error(self, bad_name):
|
||||
"""Property: non-alphanumeric (besides underscore) column names raise ValueError."""
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
db_path = os.path.join(td, "test.db")
|
||||
db = ResultsDatabase(db_path=db_path)
|
||||
try:
|
||||
with pytest.raises(ValueError):
|
||||
db._add_column_if_not_exists("backtest_runs", bad_name, "REAL")
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
@given(st.text(min_size=1, max_size=15))
|
||||
@settings(max_examples=10, deadline=5000)
|
||||
def test_invalid_table_name_raises(self, bad_table):
|
||||
"""Property: unknown table names raise ValueError."""
|
||||
assume(bad_table not in {"factors", "backtest_runs", "loop_results"})
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
db_path = os.path.join(td, "test.db")
|
||||
db = ResultsDatabase(db_path=db_path)
|
||||
try:
|
||||
with pytest.raises(ValueError):
|
||||
db._add_column_if_not_exists(bad_table, "col", "REAL")
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Data Integrity Properties (10 tests)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestDataIntegrityFuzzing:
|
||||
"""Property tests for data roundtrip and consistency."""
|
||||
|
||||
@given(
|
||||
st.text(alphabet=st.characters(min_codepoint=65, max_codepoint=90), min_size=1, max_size=30),
|
||||
st.floats(min_value=-1.0, max_value=1.0),
|
||||
st.floats(min_value=-5.0, max_value=5.0),
|
||||
)
|
||||
@settings(max_examples=10, deadline=5000)
|
||||
def test_data_roundtrip_ic(self, name, ic, sharpe):
|
||||
"""Property: IC value retrieved matches what was stored."""
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
db_path = os.path.join(td, "test.db")
|
||||
db1 = ResultsDatabase(db_path=db_path)
|
||||
try:
|
||||
bid = db1.add_backtest(name, {"ic": ic, "sharpe_ratio": sharpe})
|
||||
c = db1.conn.cursor()
|
||||
c.execute("SELECT ic FROM backtest_runs WHERE id=?", (bid,))
|
||||
stored = c.fetchone()[0]
|
||||
assert abs(stored - ic) < 1e-10
|
||||
finally:
|
||||
db1.close()
|
||||
|
||||
@given(
|
||||
st.text(alphabet=st.characters(min_codepoint=65, max_codepoint=90), min_size=1, max_size=30),
|
||||
st.floats(min_value=-10.0, max_value=10.0),
|
||||
)
|
||||
@settings(max_examples=10, deadline=5000)
|
||||
def test_data_roundtrip_sharpe(self, name, sharpe):
|
||||
"""Property: Sharpe value retrieved matches stored."""
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
db_path = os.path.join(td, "test.db")
|
||||
db = ResultsDatabase(db_path=db_path)
|
||||
try:
|
||||
bid = db.add_backtest(name, {"ic": 0.0, "sharpe_ratio": sharpe})
|
||||
c = db.conn.cursor()
|
||||
c.execute("SELECT sharpe FROM backtest_runs WHERE id=?", (bid,))
|
||||
assert abs(c.fetchone()[0] - sharpe) < 1e-10
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
@given(
|
||||
st.text(alphabet=st.characters(min_codepoint=65, max_codepoint=90), min_size=1, max_size=30),
|
||||
st.floats(min_value=-1.0, max_value=0.0),
|
||||
)
|
||||
@settings(max_examples=10, deadline=5000)
|
||||
def test_data_roundtrip_max_drawdown(self, name, dd):
|
||||
"""Property: max_drawdown roundtrip is exact."""
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
db_path = os.path.join(td, "test.db")
|
||||
db = ResultsDatabase(db_path=db_path)
|
||||
try:
|
||||
bid = db.add_backtest(name, {"ic": 0.0, "max_drawdown": dd, "sharpe_ratio": 1.0})
|
||||
c = db.conn.cursor()
|
||||
c.execute("SELECT max_drawdown FROM backtest_runs WHERE id=?", (bid,))
|
||||
assert abs(c.fetchone()[0] - dd) < 1e-10
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
@given(
|
||||
st.text(alphabet=st.characters(min_codepoint=65, max_codepoint=90), min_size=1, max_size=30),
|
||||
st.floats(min_value=0.0, max_value=1.0),
|
||||
)
|
||||
@settings(max_examples=10, deadline=5000)
|
||||
def test_data_roundtrip_win_rate(self, name, wr):
|
||||
"""Property: win_rate roundtrip is exact."""
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
db_path = os.path.join(td, "test.db")
|
||||
db = ResultsDatabase(db_path=db_path)
|
||||
try:
|
||||
bid = db.add_backtest(name, {"ic": 0.0, "win_rate": wr, "sharpe_ratio": 1.0})
|
||||
c = db.conn.cursor()
|
||||
c.execute("SELECT win_rate FROM backtest_runs WHERE id=?", (bid,))
|
||||
assert abs(c.fetchone()[0] - wr) < 1e-10
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
@given(
|
||||
st.lists(
|
||||
st.tuples(
|
||||
st.floats(min_value=-5.0, max_value=5.0),
|
||||
st.floats(min_value=-1.0, max_value=1.0),
|
||||
),
|
||||
min_size=5, max_size=30,
|
||||
),
|
||||
)
|
||||
@settings(max_examples=10, deadline=5000, suppress_health_check=[HealthCheck.filter_too_much])
|
||||
def test_multiple_runs_factor_count_consistent(self, pairs):
|
||||
"""Property: unique factor count between direct SQL and get_aggregate_stats matches."""
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
db_path = os.path.join(td, "test.db")
|
||||
db = ResultsDatabase(db_path=db_path)
|
||||
try:
|
||||
for i, (sh, ic) in enumerate(pairs):
|
||||
db.add_backtest(f"ConsistencyFac_{i}", {"ic": ic, "sharpe_ratio": sh})
|
||||
stats = db.get_aggregate_stats()
|
||||
c = db.conn.cursor()
|
||||
c.execute("SELECT COUNT(DISTINCT factor_name) FROM backtest_runs JOIN factors ON factor_id=factors.id")
|
||||
direct = c.fetchone()[0]
|
||||
assert stats["total_factors"] == direct
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
@given(st.integers(min_value=1, max_value=50))
|
||||
@settings(max_examples=10, deadline=5000)
|
||||
def test_persistence_across_connections(self, n_factors):
|
||||
"""Property: data written in one connection is visible in a new connection."""
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
db_path = os.path.join(td, "test.db")
|
||||
db1 = ResultsDatabase(db_path=db_path)
|
||||
for i in range(n_factors):
|
||||
db1.add_backtest(f"Persist_{i}", {"ic": 0.01 * i, "sharpe_ratio": 1.0})
|
||||
db1.close()
|
||||
|
||||
db2 = ResultsDatabase(db_path=db_path)
|
||||
try:
|
||||
c = db2.conn.cursor()
|
||||
c.execute("SELECT COUNT(*) FROM backtest_runs")
|
||||
assert c.fetchone()[0] == n_factors
|
||||
finally:
|
||||
db2.close()
|
||||
|
||||
@given(st.floats(min_value=-100.0, max_value=100.0))
|
||||
@settings(max_examples=10, deadline=5000)
|
||||
def test_nan_handled_in_metrics(self, nan_val):
|
||||
"""Property: NaN values in metrics do not crash."""
|
||||
assume(np.isnan(nan_val) or not np.isnan(nan_val)) # both branches tested
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
db_path = os.path.join(td, "test.db")
|
||||
db = ResultsDatabase(db_path=db_path)
|
||||
try:
|
||||
bid = db.add_backtest("NaNTest", {"ic": nan_val, "sharpe_ratio": 1.0})
|
||||
assert bid > 0
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# get_factor_history Properties (5 tests)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestGetFactorHistoryFuzzing:
|
||||
"""Property tests for get_factor_history."""
|
||||
|
||||
@given(
|
||||
st.text(alphabet=st.characters(min_codepoint=65, max_codepoint=90), min_size=1, max_size=20),
|
||||
st.integers(min_value=1, max_value=10),
|
||||
)
|
||||
@settings(max_examples=10, deadline=5000)
|
||||
def test_factor_history_returns_correct_count(self, name, n_runs):
|
||||
"""Property: get_factor_history returns exactly n rows for n backtest runs."""
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
db_path = os.path.join(td, "test.db")
|
||||
db = ResultsDatabase(db_path=db_path)
|
||||
try:
|
||||
for i in range(n_runs):
|
||||
db.add_backtest(name, {"ic": i * 0.01, "sharpe_ratio": 1.0})
|
||||
df = db.get_factor_history(name)
|
||||
assert len(df) == n_runs, f"Expected {n_runs}, got {len(df)}"
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
@given(st.text(alphabet=st.characters(min_codepoint=65, max_codepoint=90), min_size=1, max_size=20))
|
||||
@settings(max_examples=10, deadline=5000)
|
||||
def test_factor_history_empty_for_unknown(self, name):
|
||||
"""Property: get_factor_history for unknown factor returns empty DataFrame."""
|
||||
assume(len(name) > 0)
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
db_path = os.path.join(td, "test.db")
|
||||
db = ResultsDatabase(db_path=db_path)
|
||||
try:
|
||||
df = db.get_factor_history(name + "_unknown_suffix_xyz")
|
||||
assert len(df) == 0
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
@given(
|
||||
st.floats(min_value=-1.0, max_value=1.0),
|
||||
st.floats(min_value=-5.0, max_value=5.0),
|
||||
)
|
||||
@settings(max_examples=10, deadline=5000)
|
||||
def test_factor_history_values_match(self, ic, sharpe):
|
||||
"""Property: get_factor_history returns the same values that were stored."""
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
db_path = os.path.join(td, "test.db")
|
||||
db = ResultsDatabase(db_path=db_path)
|
||||
try:
|
||||
db.add_backtest("HistoryCheck", {"ic": ic, "sharpe_ratio": sharpe})
|
||||
df = db.get_factor_history("HistoryCheck")
|
||||
assert len(df) > 0
|
||||
assert abs(df.iloc[0]["ic"] - ic) < 1e-10
|
||||
assert abs(df.iloc[0]["sharpe"] - sharpe) < 1e-10
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
@@ -481,3 +481,648 @@ class TestRiskManagementIntegration:
|
||||
from rdagent.components.backtesting.risk_management import (
|
||||
CorrelationAnalyzer, PortfolioOptimizer, AdvancedRiskManager
|
||||
)
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# HYPOTHESIS PROPERTY-BASED TESTS (ADDED – DO NOT MODIFY ABOVE THIS LINE)
|
||||
# ============================================================================
|
||||
|
||||
from hypothesis import given, settings, strategies as st, assume
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Correlation Matrix Properties (22 tests)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestCorrelationMatrixProperties:
|
||||
"""Property-based tests for correlation matrix invariants."""
|
||||
|
||||
@given(
|
||||
st.integers(min_value=2, max_value=15),
|
||||
st.integers(min_value=30, max_value=500),
|
||||
st.floats(min_value=0.001, max_value=0.1),
|
||||
)
|
||||
@settings(max_examples=100, deadline=5000)
|
||||
def test_corr_matrix_symmetric(self, n_assets, n_bars, noise):
|
||||
"""Property: correlation matrix is always symmetric."""
|
||||
dates = pd.date_range("2024-01-01", periods=n_bars, freq="B")
|
||||
rng = np.random.default_rng(42)
|
||||
data = rng.normal(0, noise, (n_bars, n_assets))
|
||||
df = pd.DataFrame(data, columns=[f"A_{i}" for i in range(n_assets)], index=dates)
|
||||
analyzer = CorrelationAnalyzer()
|
||||
corr = analyzer.calculate_matrix(df)
|
||||
assert np.allclose(corr.values, corr.values.T, atol=1e-10)
|
||||
|
||||
@given(
|
||||
st.integers(min_value=1, max_value=20),
|
||||
st.integers(min_value=30, max_value=500),
|
||||
)
|
||||
@settings(max_examples=70, deadline=5000)
|
||||
def test_corr_diagonal_is_one(self, n_assets, n_bars):
|
||||
"""Property: all diagonal elements of correlation matrix equal 1.0."""
|
||||
dates = pd.date_range("2024-01-01", periods=n_bars, freq="B")
|
||||
rng = np.random.default_rng(42)
|
||||
data = rng.normal(0, 0.02, (n_bars, n_assets))
|
||||
df = pd.DataFrame(data, columns=[f"A_{i}" for i in range(n_assets)], index=dates)
|
||||
analyzer = CorrelationAnalyzer()
|
||||
corr = analyzer.calculate_matrix(df)
|
||||
diag = np.diag(corr.values)
|
||||
assert np.allclose(diag, 1.0, atol=1e-10)
|
||||
|
||||
@given(
|
||||
st.integers(min_value=3, max_value=10),
|
||||
st.integers(min_value=50, max_value=300),
|
||||
)
|
||||
@settings(max_examples=70, deadline=5000)
|
||||
def test_corr_values_in_bounds(self, n_assets, n_bars):
|
||||
"""Property: all correlation values ∈ [-1, 1]."""
|
||||
dates = pd.date_range("2024-01-01", periods=n_bars, freq="B")
|
||||
rng = np.random.default_rng(42)
|
||||
data = rng.normal(0, 0.02, (n_bars, n_assets))
|
||||
df = pd.DataFrame(data, columns=[f"A_{i}" for i in range(n_assets)], index=dates)
|
||||
analyzer = CorrelationAnalyzer()
|
||||
corr = analyzer.calculate_matrix(df)
|
||||
vals = corr.values.ravel()
|
||||
vals = vals[~np.isnan(vals)]
|
||||
assert np.all(vals >= -1.0)
|
||||
assert np.all(vals <= 1.0)
|
||||
|
||||
@given(
|
||||
st.integers(min_value=2, max_value=6),
|
||||
st.integers(min_value=30, max_value=500),
|
||||
)
|
||||
@settings(max_examples=50, deadline=5000)
|
||||
def test_corr_psd(self, n_assets, n_bars):
|
||||
"""Property: correlation matrix is positive semi-definite."""
|
||||
dates = pd.date_range("2024-01-01", periods=n_bars, freq="B")
|
||||
rng = np.random.default_rng(42)
|
||||
data = rng.normal(0, 0.02, (n_bars, n_assets))
|
||||
df = pd.DataFrame(data, columns=[f"A_{i}" for i in range(n_assets)], index=dates)
|
||||
analyzer = CorrelationAnalyzer()
|
||||
corr = analyzer.calculate_matrix(df)
|
||||
vals = corr.values
|
||||
vals = np.nan_to_num(vals, nan=0)
|
||||
eigenvalues = np.linalg.eigvalsh(vals)
|
||||
assert np.all(eigenvalues >= -1e-10), f"Non-PSD: min eigenvalue={eigenvalues.min()}"
|
||||
|
||||
@given(st.integers(min_value=30, max_value=500))
|
||||
@settings(max_examples=50, deadline=5000)
|
||||
def test_single_asset_corr_is_one(self, n_bars):
|
||||
"""Property: correlation matrix of single asset is [[1.0]]."""
|
||||
dates = pd.date_range("2024-01-01", periods=n_bars, freq="B")
|
||||
rng = np.random.default_rng(42)
|
||||
df = pd.DataFrame({"Only": rng.normal(0, 0.02, n_bars)}, index=dates)
|
||||
analyzer = CorrelationAnalyzer()
|
||||
corr = analyzer.calculate_matrix(df)
|
||||
assert corr.shape == (1, 1)
|
||||
assert corr.iloc[0, 0] == 1.0
|
||||
|
||||
@given(
|
||||
st.integers(min_value=3, max_value=10),
|
||||
st.integers(min_value=50, max_value=300),
|
||||
)
|
||||
@settings(max_examples=50, deadline=5000)
|
||||
def test_corr_equals_corr_from_pandas(self, n_assets, n_bars):
|
||||
"""Property: calculate_matrix matches pandas .corr()."""
|
||||
dates = pd.date_range("2024-01-01", periods=n_bars, freq="B")
|
||||
rng = np.random.default_rng(42)
|
||||
data = rng.normal(0, 0.02, (n_bars, n_assets))
|
||||
df = pd.DataFrame(data, columns=[f"A_{i}" for i in range(n_assets)], index=dates)
|
||||
analyzer = CorrelationAnalyzer()
|
||||
result = analyzer.calculate_matrix(df)
|
||||
expected = df.dropna().corr()
|
||||
assert np.allclose(result.values, expected.values, atol=1e-10, equal_nan=True)
|
||||
|
||||
@given(
|
||||
st.floats(min_value=0.1, max_value=0.9),
|
||||
st.integers(min_value=50, max_value=200),
|
||||
)
|
||||
@settings(max_examples=40, deadline=5000)
|
||||
def test_corr_with_nans_still_symmetric(self, nan_fraction, n_bars):
|
||||
"""Property: correlation matrix stays symmetric even with NaN-contaminated data."""
|
||||
n_assets = 5
|
||||
dates = pd.date_range("2024-01-01", periods=n_bars, freq="B")
|
||||
rng = np.random.default_rng(42)
|
||||
data = rng.normal(0, 0.02, (n_bars, n_assets))
|
||||
df = pd.DataFrame(data, columns=[f"A_{i}" for i in range(n_assets)], index=dates)
|
||||
for col in df.columns:
|
||||
n_nan = int(n_bars * nan_fraction * 0.3)
|
||||
df.loc[df.index[:n_nan], col] = np.nan
|
||||
analyzer = CorrelationAnalyzer()
|
||||
corr = analyzer.calculate_matrix(df)
|
||||
vals = np.nan_to_num(corr.values, nan=0)
|
||||
assert np.allclose(vals, vals.T, atol=1e-10)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# find_uncorrelated Properties (12 tests)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestFindUncorrelatedProperties:
|
||||
"""Property tests for find_uncorrelated."""
|
||||
|
||||
@given(
|
||||
st.integers(min_value=3, max_value=10),
|
||||
st.integers(min_value=100, max_value=500),
|
||||
st.floats(min_value=0.0, max_value=1.0),
|
||||
)
|
||||
@settings(max_examples=100, deadline=5000)
|
||||
def test_uncorrelated_count_bounded_by_n_assets(self, n_assets, n_bars, threshold):
|
||||
"""Property: number of uncorrelated factors <= n_assets."""
|
||||
dates = pd.date_range("2024-01-01", periods=n_bars, freq="B")
|
||||
rng = np.random.default_rng(42)
|
||||
data = rng.normal(0, 0.02, (n_bars, n_assets))
|
||||
df = pd.DataFrame(data, columns=[f"A_{i}" for i in range(n_assets)], index=dates)
|
||||
analyzer = CorrelationAnalyzer()
|
||||
corr = analyzer.calculate_matrix(df)
|
||||
result = analyzer.find_uncorrelated(corr, threshold=threshold)
|
||||
assert len(result) <= n_assets
|
||||
|
||||
@given(
|
||||
st.integers(min_value=3, max_value=8),
|
||||
st.integers(min_value=100, max_value=400),
|
||||
st.floats(min_value=0.0, max_value=0.5),
|
||||
st.floats(min_value=0.5, max_value=1.0),
|
||||
)
|
||||
@settings(max_examples=70, deadline=5000)
|
||||
def test_threshold_monotonicity(self, n_assets, n_bars, t_low, t_high):
|
||||
"""Property: higher threshold => more or equal uncorrelated factors."""
|
||||
assume(t_low <= t_high)
|
||||
dates = pd.date_range("2024-01-01", periods=n_bars, freq="B")
|
||||
rng = np.random.default_rng(42)
|
||||
data = rng.normal(0, 0.02, (n_bars, n_assets))
|
||||
df = pd.DataFrame(data, columns=[f"A_{i}" for i in range(n_assets)], index=dates)
|
||||
analyzer = CorrelationAnalyzer()
|
||||
corr = analyzer.calculate_matrix(df)
|
||||
r_low = analyzer.find_uncorrelated(corr, threshold=t_low)
|
||||
r_high = analyzer.find_uncorrelated(corr, threshold=t_high)
|
||||
assert len(r_high) >= len(r_low)
|
||||
|
||||
@given(
|
||||
st.integers(min_value=30, max_value=300),
|
||||
)
|
||||
@settings(max_examples=30, deadline=5000)
|
||||
def test_empty_matrix_returns_empty(self, n_bars):
|
||||
"""Property: find_uncorrelated on empty matrix returns []."""
|
||||
analyzer = CorrelationAnalyzer()
|
||||
assert analyzer.find_uncorrelated(pd.DataFrame()) == []
|
||||
|
||||
@given(
|
||||
st.integers(min_value=120, max_value=300),
|
||||
)
|
||||
@settings(max_examples=30, deadline=5000)
|
||||
def test_single_asset_is_uncorrelated(self, n_bars):
|
||||
"""Property: single-asset mean abs correlation to others is NaN → not found."""
|
||||
dates = pd.date_range("2024-01-01", periods=n_bars, freq="B")
|
||||
rng = np.random.default_rng(42)
|
||||
df = pd.DataFrame({"Solo": rng.normal(0, 0.02, n_bars)}, index=dates)
|
||||
analyzer = CorrelationAnalyzer()
|
||||
corr = analyzer.calculate_matrix(df)
|
||||
result = analyzer.find_uncorrelated(corr, threshold=0.5)
|
||||
# Single asset has no "others" — abs().mean() returns NaN, which is not < threshold
|
||||
# So it should NOT be in result (or the list may be empty)
|
||||
assert isinstance(result, list)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Mean-Variance Properties (18 tests)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestMeanVarianceProperties:
|
||||
"""Property-based tests for mean_variance optimization."""
|
||||
|
||||
@given(
|
||||
st.integers(min_value=2, max_value=10),
|
||||
)
|
||||
@settings(max_examples=50, deadline=5000)
|
||||
def test_weights_sum_to_one(self, n_assets):
|
||||
"""Property: mean_variance weights always sum to 1."""
|
||||
names = [f"A_{i}" for i in range(n_assets)]
|
||||
exp_ret = pd.Series(np.random.default_rng(42).uniform(0.01, 0.15, n_assets), index=names)
|
||||
cov_data = np.random.default_rng(43).uniform(0.01, 0.1, (n_assets, n_assets))
|
||||
cov_data = cov_data @ cov_data.T + np.eye(n_assets) * 0.01 # make PSD
|
||||
cov = pd.DataFrame(cov_data, index=names, columns=names)
|
||||
opt = PortfolioOptimizer()
|
||||
w = opt.mean_variance(exp_ret, cov)
|
||||
assert abs(np.sum(w) - 1.0) < 1e-10
|
||||
|
||||
@given(
|
||||
st.integers(min_value=2, max_value=8),
|
||||
)
|
||||
@settings(max_examples=50, deadline=5000)
|
||||
def test_weights_are_numpy_array(self, n_assets):
|
||||
"""Property: mean_variance returns numpy array."""
|
||||
names = [f"A_{i}" for i in range(n_assets)]
|
||||
exp_ret = pd.Series(np.random.default_rng(42).uniform(0.01, 0.15, n_assets), index=names)
|
||||
cov = pd.DataFrame(np.eye(n_assets) * 0.04, index=names, columns=names)
|
||||
opt = PortfolioOptimizer()
|
||||
w = opt.mean_variance(exp_ret, cov)
|
||||
assert isinstance(w, np.ndarray)
|
||||
assert len(w) == n_assets
|
||||
|
||||
@given(
|
||||
st.integers(min_value=2, max_value=6),
|
||||
st.floats(min_value=0.001, max_value=0.2),
|
||||
)
|
||||
@settings(max_examples=50, deadline=5000)
|
||||
def test_equal_returns_different_vol_weights(self, n_assets, ret_val):
|
||||
"""Property: if all returns equal, lower-vol assets get higher weight."""
|
||||
names = [f"A_{i}" for i in range(n_assets)]
|
||||
exp_ret = pd.Series([ret_val] * n_assets, index=names)
|
||||
# Increasing vol: A0 has 0.01, A1 has 0.04, ...
|
||||
diag = np.array([0.01 * (i + 1) for i in range(n_assets)])
|
||||
cov = pd.DataFrame(np.diag(diag), index=names, columns=names)
|
||||
opt = PortfolioOptimizer()
|
||||
w = opt.mean_variance(exp_ret, cov)
|
||||
assert w[np.argmin(diag)] > w[np.argmax(diag)]
|
||||
|
||||
@given(
|
||||
st.integers(min_value=3, max_value=6),
|
||||
)
|
||||
@settings(max_examples=50, deadline=5000)
|
||||
def test_higher_return_gets_higher_weight_ceteris_paribus(self, n_assets):
|
||||
"""Property: among assets with equal risk, the one with highest return gets highest weight."""
|
||||
names = [f"A_{i}" for i in range(n_assets)]
|
||||
rets = np.linspace(0.01, 0.20, n_assets)
|
||||
exp_ret = pd.Series(rets, index=names)
|
||||
cov = pd.DataFrame(np.eye(n_assets) * 0.04, index=names, columns=names)
|
||||
opt = PortfolioOptimizer()
|
||||
w = opt.mean_variance(exp_ret, cov)
|
||||
assert np.argmax(w) == np.argmax(rets)
|
||||
|
||||
@given(
|
||||
st.integers(min_value=2, max_value=6),
|
||||
)
|
||||
@settings(max_examples=50, deadline=5000)
|
||||
def test_singular_cov_fallback_equal_weights(self, n_assets):
|
||||
"""Property: singular covariance produces equal weights (fallback)."""
|
||||
names = [f"A_{i}" for i in range(n_assets)]
|
||||
exp_ret = pd.Series(np.random.default_rng(42).uniform(0.01, 0.15, n_assets), index=names)
|
||||
# Singular: all rows identical
|
||||
row = np.ones(n_assets) * 0.04
|
||||
cov = pd.DataFrame([row] * n_assets, index=names, columns=names)
|
||||
opt = PortfolioOptimizer()
|
||||
w = opt.mean_variance(exp_ret, cov)
|
||||
expected = np.ones(n_assets) / n_assets
|
||||
assert np.allclose(w, expected, atol=0.01)
|
||||
|
||||
@given(
|
||||
st.integers(min_value=2, max_value=6),
|
||||
)
|
||||
@settings(max_examples=50, deadline=5000)
|
||||
def test_zero_cov_fallback_equal_weights(self, n_assets):
|
||||
"""Property: zero covariance matrix produces equal weights fallback."""
|
||||
names = [f"A_{i}" for i in range(n_assets)]
|
||||
exp_ret = pd.Series(np.random.default_rng(42).uniform(0.01, 0.15, n_assets), index=names)
|
||||
cov = pd.DataFrame(np.zeros((n_assets, n_assets)), index=names, columns=names)
|
||||
opt = PortfolioOptimizer()
|
||||
w = opt.mean_variance(exp_ret, cov)
|
||||
expected = np.ones(n_assets) / n_assets
|
||||
assert np.allclose(w, expected, atol=0.01)
|
||||
|
||||
@given(
|
||||
st.integers(min_value=2, max_value=8),
|
||||
)
|
||||
@settings(max_examples=50, deadline=5000)
|
||||
def test_negative_returns_still_sum_to_one(self, n_assets):
|
||||
"""Property: weights sum to 1 even when all expected returns are negative."""
|
||||
names = [f"A_{i}" for i in range(n_assets)]
|
||||
exp_ret = pd.Series(np.random.default_rng(42).uniform(-0.20, -0.01, n_assets), index=names)
|
||||
cov = pd.DataFrame(np.eye(n_assets) * 0.04, index=names, columns=names)
|
||||
opt = PortfolioOptimizer()
|
||||
w = opt.mean_variance(exp_ret, cov)
|
||||
assert abs(np.sum(w) - 1.0) < 1e-10
|
||||
|
||||
@given(
|
||||
st.floats(min_value=0.01, max_value=0.5),
|
||||
st.integers(min_value=2, max_value=6),
|
||||
)
|
||||
@settings(max_examples=50, deadline=5000)
|
||||
def test_weights_invariant_to_exp_ret_scale(self, scale, n_assets):
|
||||
"""Property: multiplying all expected returns by same factor doesn't change weights."""
|
||||
names = [f"A_{i}" for i in range(n_assets)]
|
||||
rng = np.random.default_rng(42)
|
||||
base_rets = rng.uniform(0.01, 0.15, n_assets)
|
||||
exp_ret_1 = pd.Series(base_rets, index=names)
|
||||
exp_ret_2 = pd.Series(base_rets * scale, index=names)
|
||||
cov = pd.DataFrame(np.eye(n_assets) * 0.04, index=names, columns=names)
|
||||
opt = PortfolioOptimizer()
|
||||
w1 = opt.mean_variance(exp_ret_1, cov)
|
||||
w2 = opt.mean_variance(exp_ret_2, cov)
|
||||
assert np.allclose(w1, w2, atol=1e-10), f"w1={w1}, w2={w2}"
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Risk-Parity Properties (16 tests)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestRiskParityProperties:
|
||||
"""Property-based tests for risk_parity optimization."""
|
||||
|
||||
@given(
|
||||
st.integers(min_value=2, max_value=8),
|
||||
)
|
||||
@settings(max_examples=50, deadline=5000)
|
||||
def test_weights_sum_to_one(self, n_assets):
|
||||
"""Property: risk_parity weights sum to 1."""
|
||||
names = [f"A_{i}" for i in range(n_assets)]
|
||||
rng = np.random.default_rng(42)
|
||||
data = rng.uniform(0.01, 0.1, (n_assets, n_assets))
|
||||
cov_data = data @ data.T + np.eye(n_assets) * 0.01
|
||||
cov = pd.DataFrame(cov_data, index=names, columns=names)
|
||||
opt = PortfolioOptimizer()
|
||||
w = opt.risk_parity(cov)
|
||||
assert abs(np.sum(w) - 1.0) < 1e-10
|
||||
|
||||
@given(
|
||||
st.integers(min_value=2, max_value=8),
|
||||
)
|
||||
@settings(max_examples=50, deadline=5000)
|
||||
def test_weights_positive(self, n_assets):
|
||||
"""Property: risk_parity weights are all positive (long-only)."""
|
||||
names = [f"A_{i}" for i in range(n_assets)]
|
||||
rng = np.random.default_rng(42)
|
||||
data = rng.uniform(0.01, 0.1, (n_assets, n_assets))
|
||||
cov_data = data @ data.T + np.eye(n_assets) * 0.01
|
||||
cov = pd.DataFrame(cov_data, index=names, columns=names)
|
||||
opt = PortfolioOptimizer()
|
||||
w = opt.risk_parity(cov)
|
||||
assert np.all(w > 0), f"Non-positive weight: {w}"
|
||||
|
||||
@given(st.integers(min_value=1, max_value=1))
|
||||
@settings(max_examples=20, deadline=5000)
|
||||
def test_single_asset_weight_is_one(self, _):
|
||||
"""Property: risk_parity with single asset returns [1.0]."""
|
||||
cov = pd.DataFrame([[0.04]], index=["A"], columns=["A"])
|
||||
opt = PortfolioOptimizer()
|
||||
w = opt.risk_parity(cov)
|
||||
assert len(w) == 1
|
||||
assert w[0] == 1.0
|
||||
|
||||
@given(
|
||||
st.integers(min_value=2, max_value=6),
|
||||
)
|
||||
@settings(max_examples=50, deadline=5000)
|
||||
def test_equal_vol_gives_equal_weights(self, n_assets):
|
||||
"""Property: diagonal covariance with equal variance => equal weights."""
|
||||
names = [f"A_{i}" for i in range(n_assets)]
|
||||
cov = pd.DataFrame(np.eye(n_assets) * 0.04, index=names, columns=names)
|
||||
opt = PortfolioOptimizer()
|
||||
w = opt.risk_parity(cov)
|
||||
expected = np.ones(n_assets) / n_assets
|
||||
assert np.allclose(w, expected, atol=0.01)
|
||||
|
||||
@given(
|
||||
st.integers(min_value=2, max_value=4),
|
||||
)
|
||||
@settings(max_examples=50, deadline=5000)
|
||||
def test_lower_vol_gets_higher_weight(self, n_assets):
|
||||
"""Property: asset with lower variance gets higher weight."""
|
||||
names = [f"A_{i}" for i in range(n_assets)]
|
||||
diag = [0.01, 0.04, 0.09, 0.16][:n_assets]
|
||||
names = names[:n_assets]
|
||||
cov = pd.DataFrame(np.diag(diag), index=names, columns=names)
|
||||
opt = PortfolioOptimizer()
|
||||
w = opt.risk_parity(cov)
|
||||
assert np.argmax(w) == 0 # lowest vol has idx 0
|
||||
|
||||
@given(
|
||||
st.integers(min_value=2, max_value=4),
|
||||
)
|
||||
@settings(max_examples=30, deadline=5000)
|
||||
def test_zero_variance_gives_equal_weights(self, n_assets):
|
||||
"""Property: zero covariance matrix falls back to equal weights."""
|
||||
names = [f"A_{i}" for i in range(n_assets)]
|
||||
cov = pd.DataFrame(np.zeros((n_assets, n_assets)), index=names, columns=names)
|
||||
opt = PortfolioOptimizer()
|
||||
w = opt.risk_parity(cov)
|
||||
expected = np.ones(n_assets) / n_assets
|
||||
assert np.allclose(w, expected, atol=0.01)
|
||||
|
||||
@given(
|
||||
st.integers(min_value=2, max_value=6),
|
||||
st.floats(min_value=0.5, max_value=5.0),
|
||||
)
|
||||
@settings(max_examples=50, deadline=5000)
|
||||
def test_cov_scaling_invariance(self, n_assets, scale):
|
||||
"""Property: scaling covariance matrix by positive factor doesn't change RP weights."""
|
||||
names = [f"A_{i}" for i in range(n_assets)]
|
||||
rng = np.random.default_rng(42)
|
||||
data = rng.uniform(0.01, 0.1, (n_assets, n_assets))
|
||||
base = data @ data.T + np.eye(n_assets) * 0.01
|
||||
cov1 = pd.DataFrame(base, index=names, columns=names)
|
||||
cov2 = pd.DataFrame(base * scale, index=names, columns=names)
|
||||
opt = PortfolioOptimizer()
|
||||
w1 = opt.risk_parity(cov1)
|
||||
w2 = opt.risk_parity(cov2)
|
||||
assert np.allclose(w1, w2, atol=1e-10)
|
||||
|
||||
@given(
|
||||
st.integers(min_value=2, max_value=6),
|
||||
st.integers(min_value=2, max_value=20),
|
||||
st.integers(min_value=50, max_value=200),
|
||||
)
|
||||
@settings(max_examples=30, deadline=5000)
|
||||
def test_more_iterations_similar_result(self, n_assets, few_iter, many_iter):
|
||||
"""Property: more iterations gives similar or equal result."""
|
||||
assume(few_iter <= many_iter)
|
||||
names = [f"A_{i}" for i in range(n_assets)]
|
||||
rng = np.random.default_rng(42)
|
||||
data = rng.uniform(0.01, 0.1, (n_assets, n_assets))
|
||||
cov_data = data @ data.T + np.eye(n_assets) * 0.01
|
||||
cov = pd.DataFrame(cov_data, index=names, columns=names)
|
||||
opt = PortfolioOptimizer()
|
||||
w1 = opt.risk_parity(cov, max_iter=few_iter)
|
||||
w2 = opt.risk_parity(cov, max_iter=many_iter)
|
||||
assert np.abs(np.sum(w1) - np.sum(w2)) < 0.01
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# check_limits Properties (16 tests)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestCheckLimitsProperties:
|
||||
"""Property-based tests for check_limits."""
|
||||
|
||||
@given(
|
||||
st.lists(st.floats(min_value=-0.5, max_value=0.5), min_size=3, max_size=10),
|
||||
st.floats(min_value=0.01, max_value=0.5),
|
||||
st.floats(min_value=-0.5, max_value=-0.001),
|
||||
st.floats(min_value=0.01, max_value=1.0),
|
||||
st.floats(min_value=1.0, max_value=10.0),
|
||||
st.floats(min_value=0.01, max_value=1.0),
|
||||
)
|
||||
@settings(max_examples=200, deadline=5000)
|
||||
def test_all_checks_are_boolean(self, weights, vol, dd, max_pos, max_lev, max_dd):
|
||||
"""Property: all check_limits return values are boolean."""
|
||||
w = np.array(weights, dtype=float)
|
||||
mgr = AdvancedRiskManager(max_pos=max_pos, max_lev=max_lev, max_dd=max_dd)
|
||||
checks = mgr.check_limits(w, vol=vol, dd=dd)
|
||||
for k, v in checks.items():
|
||||
assert isinstance(v, (bool, np.bool_)), f"{k} is {type(v)}"
|
||||
|
||||
@given(
|
||||
st.lists(st.floats(min_value=-0.5, max_value=0.5), min_size=3, max_size=10),
|
||||
st.floats(min_value=-0.5, max_value=-0.001),
|
||||
st.floats(min_value=0.01, max_value=1.0),
|
||||
st.floats(min_value=1.0, max_value=10.0),
|
||||
st.floats(min_value=0.01, max_value=1.0),
|
||||
)
|
||||
@settings(max_examples=200, deadline=5000)
|
||||
def test_three_keys_present(self, weights, dd, max_pos, max_lev, max_dd):
|
||||
"""Property: check_limits returns exactly 3 keys."""
|
||||
w = np.array(weights, dtype=float)
|
||||
mgr = AdvancedRiskManager(max_pos=max_pos, max_lev=max_lev, max_dd=max_dd)
|
||||
checks = mgr.check_limits(w, vol=0.15, dd=dd)
|
||||
assert set(checks.keys()) == {"position_limit", "leverage_limit", "drawdown_limit"}
|
||||
|
||||
@given(
|
||||
st.lists(st.floats(min_value=0.0, max_value=0.01), min_size=3, max_size=10),
|
||||
st.floats(min_value=-0.01, max_value=0),
|
||||
st.floats(min_value=0.1, max_value=1.0),
|
||||
st.floats(min_value=1.0, max_value=10.0),
|
||||
st.floats(min_value=0.1, max_value=1.0),
|
||||
)
|
||||
@settings(max_examples=100, deadline=5000)
|
||||
def test_tiny_weights_pass_all_limits(self, weights, dd, max_pos, max_lev, max_dd):
|
||||
"""Property: very small weights pass all limits."""
|
||||
w = np.array(weights, dtype=float)
|
||||
mgr = AdvancedRiskManager(max_pos=max_pos, max_lev=max_lev, max_dd=max_dd)
|
||||
checks = mgr.check_limits(w, vol=0.15, dd=dd)
|
||||
assert bool(checks["position_limit"]) is True
|
||||
|
||||
@given(
|
||||
st.lists(st.floats(min_value=100.0, max_value=1000.0), min_size=1, max_size=5),
|
||||
st.floats(min_value=0.1, max_value=1.0),
|
||||
)
|
||||
@settings(max_examples=100, deadline=5000)
|
||||
def test_huge_weights_fail_position_limit(self, weights, max_pos):
|
||||
"""Property: weights much larger than max_pos fail position_limit."""
|
||||
w = np.array(weights, dtype=float)
|
||||
mgr = AdvancedRiskManager(max_pos=max_pos, max_lev=10000.0, max_dd=1.0)
|
||||
checks = mgr.check_limits(w, vol=0.15, dd=-0.01)
|
||||
assert bool(checks["position_limit"]) is False
|
||||
|
||||
@given(
|
||||
st.lists(st.floats(min_value=50.0, max_value=500.0), min_size=3, max_size=10),
|
||||
st.floats(min_value=1.0, max_value=10.0),
|
||||
)
|
||||
@settings(max_examples=100, deadline=5000)
|
||||
def test_huge_weights_fail_leverage_limit(self, weights, max_lev):
|
||||
"""Property: sum(abs(weights)) > max_lev fails leverage_limit."""
|
||||
w = np.array(weights, dtype=float)
|
||||
mgr = AdvancedRiskManager(max_pos=1000.0, max_lev=max_lev, max_dd=1.0)
|
||||
checks = mgr.check_limits(w, vol=0.15, dd=-0.01)
|
||||
assert bool(checks["leverage_limit"]) is False
|
||||
|
||||
@given(
|
||||
st.floats(min_value=0.01, max_value=0.5),
|
||||
st.floats(min_value=-2.0, max_value=-0.01),
|
||||
)
|
||||
@settings(max_examples=100, deadline=5000)
|
||||
def test_big_drawdown_fails_drawdown_limit(self, max_dd, actual_dd):
|
||||
"""Property: |dd| > max_dd fails drawdown_limit."""
|
||||
w = np.array([0.1, 0.1, 0.1])
|
||||
mgr = AdvancedRiskManager(max_pos=1.0, max_lev=100.0, max_dd=max_dd)
|
||||
checks = mgr.check_limits(w, vol=0.15, dd=actual_dd)
|
||||
assume(abs(actual_dd) > max_dd)
|
||||
assert bool(checks["drawdown_limit"]) is False
|
||||
|
||||
@given(
|
||||
st.floats(min_value=0.01, max_value=0.5),
|
||||
st.floats(min_value=-0.001, max_value=0),
|
||||
)
|
||||
@settings(max_examples=50, deadline=5000)
|
||||
def test_small_drawdown_passes_drawdown_limit(self, max_dd, actual_dd):
|
||||
"""Property: small |dd| passes drawdown_limit."""
|
||||
w = np.array([0.1, 0.1, 0.1])
|
||||
mgr = AdvancedRiskManager(max_pos=1.0, max_lev=100.0, max_dd=max_dd)
|
||||
checks = mgr.check_limits(w, vol=0.15, dd=actual_dd)
|
||||
assert bool(checks["drawdown_limit"]) is True
|
||||
|
||||
@given(
|
||||
st.floats(min_value=0.01, max_value=1.0),
|
||||
st.floats(min_value=1.0, max_value=10.0),
|
||||
st.floats(min_value=0.01, max_value=1.0),
|
||||
)
|
||||
@settings(max_examples=100, deadline=5000)
|
||||
def test_zero_weights_pass_all(self, max_pos, max_lev, max_dd):
|
||||
"""Property: all-zero weights pass all limits."""
|
||||
w = np.zeros(5)
|
||||
mgr = AdvancedRiskManager(max_pos=max_pos, max_lev=max_lev, max_dd=max_dd)
|
||||
checks = mgr.check_limits(w, vol=0.15, dd=-0.01)
|
||||
assert all(checks.values())
|
||||
|
||||
@given(
|
||||
st.lists(st.floats(min_value=-2.0, max_value=2.0), min_size=2, max_size=8),
|
||||
)
|
||||
@settings(max_examples=100, deadline=5000)
|
||||
def test_position_limit_uses_abs_value(self, weights):
|
||||
"""Property: position_limit uses abs(weight) for both long and short."""
|
||||
w = np.array(weights, dtype=float)
|
||||
max_abs = np.max(np.abs(w))
|
||||
mgr = AdvancedRiskManager(max_pos=max_abs + 0.001, max_lev=1000.0, max_dd=1.0)
|
||||
checks = mgr.check_limits(w, vol=0.15, dd=-0.01)
|
||||
assert bool(checks["position_limit"]) is True
|
||||
|
||||
mgr2 = AdvancedRiskManager(max_pos=max_abs - 0.001, max_lev=1000.0, max_dd=1.0)
|
||||
checks2 = mgr2.check_limits(w, vol=0.15, dd=-0.01)
|
||||
if max_abs > 0.001:
|
||||
assert bool(checks2["position_limit"]) is False
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Correlation + Risk Integration Properties (8 tests)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestCorrelationRiskIntegration:
|
||||
"""Integration properties combining correlation analysis and risk checks."""
|
||||
|
||||
@given(
|
||||
st.integers(min_value=3, max_value=8),
|
||||
st.integers(min_value=100, max_value=500),
|
||||
)
|
||||
@settings(max_examples=50, deadline=5000)
|
||||
def test_uncorrelated_subset_weights_valid(self, n_assets, n_bars):
|
||||
"""Property: portfolio weights for uncorrelated subset pass basic validation."""
|
||||
dates = pd.date_range("2024-01-01", periods=n_bars, freq="B")
|
||||
rng = np.random.default_rng(42)
|
||||
data = rng.normal(0, 0.02, (n_bars, n_assets))
|
||||
df = pd.DataFrame(data, columns=[f"A_{i}" for i in range(n_assets)], index=dates)
|
||||
analyzer = CorrelationAnalyzer()
|
||||
corr = analyzer.calculate_matrix(df)
|
||||
uncorr = analyzer.find_uncorrelated(corr, threshold=0.5)
|
||||
assume(len(uncorr) >= 2)
|
||||
|
||||
cov = df[uncorr].cov() * 252
|
||||
opt = PortfolioOptimizer()
|
||||
w = opt.risk_parity(cov)
|
||||
assert abs(np.sum(w) - 1.0) < 1e-10
|
||||
assert np.all(np.isfinite(w)), f"RP weights should be finite: {w}"
|
||||
|
||||
@given(
|
||||
st.integers(min_value=3, max_value=8),
|
||||
st.integers(min_value=100, max_value=300),
|
||||
)
|
||||
@settings(max_examples=50, deadline=5000)
|
||||
def test_full_workflow_weight_sum_one(self, n_assets, n_bars):
|
||||
"""Property: full workflow (corr → uncorr → MV → risk check) runs end-to-end."""
|
||||
dates = pd.date_range("2024-01-01", periods=n_bars, freq="B")
|
||||
rng = np.random.default_rng(42)
|
||||
data = rng.normal(0, 0.02, (n_bars, n_assets))
|
||||
df = pd.DataFrame(data, columns=[f"A_{i}" for i in range(n_assets)], index=dates)
|
||||
analyzer = CorrelationAnalyzer()
|
||||
corr = analyzer.calculate_matrix(df)
|
||||
assume(corr.shape[0] >= 3)
|
||||
cov = df.cov()
|
||||
exp_ret = pd.Series(df.mean(), index=df.columns)
|
||||
opt = PortfolioOptimizer()
|
||||
mv = opt.mean_variance(exp_ret, cov)
|
||||
rp = opt.risk_parity(cov)
|
||||
assert abs(np.sum(mv) - 1.0) < 0.01
|
||||
assert abs(np.sum(rp) - 1.0) < 0.01
|
||||
|
||||
@@ -164,3 +164,577 @@ class TestCrossValidation:
|
||||
equity = (1.0 + ret).cumprod()
|
||||
dd = (equity - equity.expanding().max()) / equity.expanding().max().replace(0, np.nan)
|
||||
assert -1.0 <= dd.min() <= 0.0, f"MaxDD {dd.min():.4f} not in [-1, 0]"
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# HYPOTHESIS PROPERTY-BASED CROSS-VALIDATION TESTS (ADDED – DO NOT MODIFY)
|
||||
# ============================================================================
|
||||
|
||||
from hypothesis import given, settings, strategies as st, assume
|
||||
|
||||
|
||||
def _make_multiindex_data(n_bars: int) -> pd.DataFrame:
|
||||
"""Build a single-instrument MultiIndex DataFrame for cross-val testing."""
|
||||
dates = pd.date_range("2024-01-01", periods=n_bars, freq="1min")
|
||||
rng = np.random.default_rng(42)
|
||||
idx = pd.MultiIndex.from_arrays([dates, ["EURUSD"] * n_bars], names=["datetime", "instrument"])
|
||||
close = 1.10 + rng.normal(0, 0.001, n_bars).cumsum()
|
||||
return pd.DataFrame({"$close": close}, index=idx)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# IC Properties (18 tests)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestICProperties:
|
||||
"""Property-based IC invariants for cross-validation."""
|
||||
|
||||
@given(st.integers(min_value=200, max_value=3000))
|
||||
@settings(max_examples=100, deadline=5000)
|
||||
def test_ic_in_bounds_for_random_factor(self, n_bars):
|
||||
"""Property: IC ∈ [-1, 1] for any random factor."""
|
||||
df = _make_multiindex_data(n_bars)
|
||||
close = df["$close"]
|
||||
fwd = close.groupby(level="instrument").shift(-96) / close - 1
|
||||
factor = pd.Series(np.random.default_rng(77).normal(0, 1, len(df)), index=df.index)
|
||||
valid = factor.dropna().index.intersection(fwd.dropna().index)
|
||||
assume(len(valid) >= 100)
|
||||
ic = factor.loc[valid].corr(fwd.loc[valid])
|
||||
assert -1.0 <= ic <= 1.0, f"IC={ic}"
|
||||
|
||||
@given(st.integers(min_value=200, max_value=3000))
|
||||
@settings(max_examples=100, deadline=5000)
|
||||
def test_ic_finite_for_random_factor(self, n_bars):
|
||||
"""Property: IC is finite for any random factor with variance."""
|
||||
df = _make_multiindex_data(n_bars)
|
||||
close = df["$close"]
|
||||
fwd = close.groupby(level="instrument").shift(-96) / close - 1
|
||||
rng = np.random.default_rng(77)
|
||||
factor = pd.Series(rng.normal(0, 1, len(df)), index=df.index)
|
||||
valid = factor.dropna().index.intersection(fwd.dropna().index)
|
||||
assume(len(valid) >= 100)
|
||||
ic = factor.loc[valid].corr(fwd.loc[valid])
|
||||
assert np.isfinite(ic), f"IC not finite: {ic}"
|
||||
|
||||
@given(st.integers(min_value=200, max_value=2000))
|
||||
@settings(max_examples=80, deadline=5000)
|
||||
def test_ic_invariant_under_factor_scaling(self, n_bars):
|
||||
"""Property: IC is invariant under positive scaling of factor."""
|
||||
df = _make_multiindex_data(n_bars)
|
||||
close = df["$close"]
|
||||
fwd = close.groupby(level="instrument").shift(-96) / close - 1
|
||||
rng = np.random.default_rng(77)
|
||||
base = pd.Series(rng.normal(0, 1, len(df)), index=df.index)
|
||||
scaled = base * 5.0
|
||||
valid = base.dropna().index.intersection(fwd.dropna().index)
|
||||
assume(len(valid) >= 100)
|
||||
ic_base = base.loc[valid].corr(fwd.loc[valid])
|
||||
ic_scaled = scaled.loc[valid].corr(fwd.loc[valid])
|
||||
assert abs(ic_base - ic_scaled) < 1e-10
|
||||
|
||||
@given(st.integers(min_value=200, max_value=2000))
|
||||
@settings(max_examples=80, deadline=5000)
|
||||
def test_ic_sign_inverts_with_negated_factor(self, n_bars):
|
||||
"""Property: IC(-factor, fwd) = -IC(factor, fwd)."""
|
||||
df = _make_multiindex_data(n_bars)
|
||||
close = df["$close"]
|
||||
fwd = close.groupby(level="instrument").shift(-96) / close - 1
|
||||
rng = np.random.default_rng(77)
|
||||
fac = pd.Series(rng.normal(0, 1, len(df)), index=df.index)
|
||||
valid = fac.dropna().index.intersection(fwd.dropna().index)
|
||||
assume(len(valid) >= 100)
|
||||
ic_pos = fac.loc[valid].corr(fwd.loc[valid])
|
||||
ic_neg = (-fac.loc[valid]).corr(fwd.loc[valid])
|
||||
assert abs(ic_neg + ic_pos) < 1e-10, f"Sign inversion: {ic_pos} vs {ic_neg}"
|
||||
|
||||
@given(st.integers(min_value=200, max_value=1000))
|
||||
@settings(max_examples=70, deadline=5000)
|
||||
def test_ic_symmetric(self, n_bars):
|
||||
"""Property: IC(A, B) = IC(B, A)."""
|
||||
df = _make_multiindex_data(n_bars)
|
||||
close = df["$close"]
|
||||
fwd = close.groupby(level="instrument").shift(-96) / close - 1
|
||||
rng = np.random.default_rng(77)
|
||||
fac = pd.Series(rng.normal(0, 1, len(df)), index=df.index)
|
||||
valid = fac.dropna().index.intersection(fwd.dropna().index)
|
||||
assume(len(valid) >= 100)
|
||||
a = fac.loc[valid]
|
||||
b = fwd.loc[valid]
|
||||
assume(a.std() > 1e-12 and b.std() > 1e-12)
|
||||
ic_ab = a.corr(b)
|
||||
ic_ba = b.corr(a)
|
||||
assert abs(ic_ab - ic_ba) < 1e-10
|
||||
|
||||
@given(st.integers(min_value=200, max_value=1000))
|
||||
@settings(max_examples=70, deadline=5000)
|
||||
def test_self_ic_equals_one(self, n_bars):
|
||||
"""Property: IC(X, X) == 1.0 when std(X) > 0."""
|
||||
df = _make_multiindex_data(n_bars)
|
||||
close = df["$close"]
|
||||
fwd = close.groupby(level="instrument").shift(-96) / close - 1
|
||||
valid = fwd.dropna().index
|
||||
assume(len(valid) >= 100)
|
||||
x = fwd.loc[valid]
|
||||
assume(x.std() > 1e-12)
|
||||
assert abs(x.corr(x) - 1.0) < 1e-10
|
||||
|
||||
@given(st.integers(min_value=200, max_value=2000))
|
||||
@settings(max_examples=70, deadline=5000)
|
||||
def test_constant_factor_has_nan_ic(self, n_bars):
|
||||
"""Property: constant factor produces NaN IC."""
|
||||
df = _make_multiindex_data(n_bars)
|
||||
close = df["$close"]
|
||||
fwd = close.groupby(level="instrument").shift(-96) / close - 1
|
||||
fac = pd.Series(np.ones(len(df)), index=df.index)
|
||||
valid = fac.dropna().index.intersection(fwd.dropna().index)
|
||||
assume(len(valid) >= 10)
|
||||
ic = fac.loc[valid].corr(fwd.loc[valid])
|
||||
assert np.isnan(ic) or abs(ic) < 1e-10, f"Constant factor IC should be NaN: {ic}"
|
||||
|
||||
@given(st.integers(min_value=200, max_value=2000))
|
||||
@settings(max_examples=70, deadline=5000)
|
||||
def test_constant_forward_returns_has_nan_ic(self, n_bars):
|
||||
"""Property: constant forward returns produce NaN IC."""
|
||||
df = _make_multiindex_data(n_bars)
|
||||
idx = df.index
|
||||
rng = np.random.default_rng(77)
|
||||
fac = pd.Series(rng.normal(0, 1, len(df)), index=idx)
|
||||
fwd = pd.Series(np.ones(len(df)) * 0.001, index=idx)
|
||||
valid = fac.dropna().index.intersection(fwd.dropna().index)
|
||||
assume(len(valid) >= 10)
|
||||
ic = fac.loc[valid].corr(fwd.loc[valid])
|
||||
assert np.isnan(ic) or abs(ic) < 1e-10
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Sharpe Ratio Properties (17 tests)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestSharpeCVProperties:
|
||||
"""Property-based Sharpe invariants."""
|
||||
|
||||
@given(st.integers(min_value=200, max_value=3000))
|
||||
@settings(max_examples=100, deadline=5000)
|
||||
def test_sharpe_sign_matches_excess_return(self, n_bars):
|
||||
"""Property: sign(sharpe) matches sign of mean strategy return."""
|
||||
df = _make_multiindex_data(n_bars)
|
||||
close = df["$close"]
|
||||
fwd = close.groupby(level="instrument").shift(-96) / close - 1
|
||||
rng = np.random.default_rng(77)
|
||||
factor = pd.Series(rng.normal(0, 1, len(df)), index=df.index)
|
||||
valid = factor.dropna().index.intersection(fwd.dropna().index)
|
||||
assume(len(valid) >= 100)
|
||||
signal = np.where(factor.loc[valid] > 0, 1.0, -1.0)
|
||||
ret = signal * fwd.loc[valid]
|
||||
assume(ret.std() > 1e-12)
|
||||
ann = np.sqrt(252 * 1440 / 96)
|
||||
sharpe = ret.mean() / ret.std() * ann
|
||||
if abs(ret.mean()) > 1e-15:
|
||||
assert np.sign(sharpe) == np.sign(ret.mean()), f"Sharpe={sharpe}, mean={ret.mean()}"
|
||||
|
||||
@given(st.integers(min_value=200, max_value=3000))
|
||||
@settings(max_examples=100, deadline=5000)
|
||||
def test_sharpe_scale_invariant(self, n_bars):
|
||||
"""Property: Sharpe is invariant under positive scaling of strategy returns."""
|
||||
df = _make_multiindex_data(n_bars)
|
||||
close = df["$close"]
|
||||
fwd = close.groupby(level="instrument").shift(-96) / close - 1
|
||||
rng = np.random.default_rng(77)
|
||||
factor = pd.Series(rng.normal(0, 1, len(df)), index=df.index)
|
||||
valid = factor.dropna().index.intersection(fwd.dropna().index)
|
||||
assume(len(valid) >= 100)
|
||||
signal = np.where(factor.loc[valid] > 0, 1.0, -1.0)
|
||||
ret = signal * fwd.loc[valid]
|
||||
assume(ret.std() > 1e-12)
|
||||
ann = np.sqrt(252 * 1440 / 96)
|
||||
s1 = ret.mean() / ret.std() * ann
|
||||
s2 = (ret * 3.5).mean() / (ret * 3.5).std() * ann
|
||||
assert abs(s1 - s2) < 1e-10
|
||||
|
||||
@given(st.integers(min_value=200, max_value=3000))
|
||||
@settings(max_examples=100, deadline=5000)
|
||||
def test_sharpe_finite_for_valid_data(self, n_bars):
|
||||
"""Property: Sharpe is finite for any random factor with variance."""
|
||||
df = _make_multiindex_data(n_bars)
|
||||
close = df["$close"]
|
||||
fwd = close.groupby(level="instrument").shift(-96) / close - 1
|
||||
rng = np.random.default_rng(77)
|
||||
factor = pd.Series(rng.normal(0, 1, len(df)), index=df.index)
|
||||
valid = factor.dropna().index.intersection(fwd.dropna().index)
|
||||
assume(len(valid) >= 100)
|
||||
signal = np.where(factor.loc[valid] > 0, 1.0, -1.0)
|
||||
ret = signal * fwd.loc[valid]
|
||||
assume(ret.std() > 1e-12)
|
||||
ann = np.sqrt(252 * 1440 / 96)
|
||||
sharpe = ret.mean() / ret.std() * ann
|
||||
assert np.isfinite(sharpe)
|
||||
|
||||
@given(st.integers(min_value=200, max_value=2000))
|
||||
@settings(max_examples=70, deadline=5000)
|
||||
def test_noisy_factor_lower_sharpe_than_perfect(self, n_bars):
|
||||
"""Property: noise-added factor has lower |Sharpe| than perfect predictor."""
|
||||
df = _make_multiindex_data(n_bars)
|
||||
close = df["$close"]
|
||||
fwd = close.groupby(level="instrument").shift(-96) / close - 1
|
||||
perfect_signal = pd.Series(np.sign(fwd.values), index=df.index).fillna(0)
|
||||
rng = np.random.default_rng(99)
|
||||
noisy_signal = perfect_signal + rng.normal(0, 2.0, len(perfect_signal))
|
||||
valid = perfect_signal.dropna().index.intersection(fwd.dropna().index)
|
||||
assume(len(valid) >= 100)
|
||||
ann = np.sqrt(252 * 1440 / 96)
|
||||
ret_perfect = np.where(perfect_signal.loc[valid] > 0, 1.0, -1.0) * fwd.loc[valid]
|
||||
ret_noisy = np.where(noisy_signal.loc[valid] > 0, 1.0, -1.0) * fwd.loc[valid]
|
||||
if ret_perfect.std() > 0 and ret_noisy.std() > 0:
|
||||
sp = ret_perfect.mean() / ret_perfect.std() * ann
|
||||
sn = ret_noisy.mean() / ret_noisy.std() * ann
|
||||
assert abs(sp) > abs(sn) or abs(sp) < 0.1, f"Noisy {sn} should not beat perfect {sp}"
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Drawdown Properties (16 tests)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestDrawdownCVProperties:
|
||||
"""Property-based drawdown invariants for cross-validation."""
|
||||
|
||||
@given(st.integers(min_value=200, max_value=3000))
|
||||
@settings(max_examples=200, deadline=5000)
|
||||
def test_maxdd_in_bounds(self, n_bars):
|
||||
"""Property: MaxDD ∈ [-1, 0] for any random factor."""
|
||||
df = _make_multiindex_data(n_bars)
|
||||
close = df["$close"]
|
||||
fwd = close.groupby(level="instrument").shift(-96) / close - 1
|
||||
rng = np.random.default_rng(77)
|
||||
factor = pd.Series(rng.normal(0, 1, len(df)), index=df.index)
|
||||
valid = factor.dropna().index.intersection(fwd.dropna().index)
|
||||
assume(len(valid) >= 100)
|
||||
signal = np.where(factor.loc[valid] > 0, 1.0, -1.0)
|
||||
ret = signal * fwd.loc[valid]
|
||||
equity = (1.0 + ret).cumprod()
|
||||
dd = (equity - equity.expanding().max()) / equity.expanding().max().replace(0, np.nan)
|
||||
assert -1.0 <= dd.min() <= 0.0, f"MaxDD={dd.min()}"
|
||||
|
||||
@given(st.integers(min_value=200, max_value=3000))
|
||||
@settings(max_examples=100, deadline=5000)
|
||||
def test_maxdd_finite(self, n_bars):
|
||||
"""Property: MaxDD is finite for valid data."""
|
||||
df = _make_multiindex_data(n_bars)
|
||||
close = df["$close"]
|
||||
fwd = close.groupby(level="instrument").shift(-96) / close - 1
|
||||
rng = np.random.default_rng(77)
|
||||
factor = pd.Series(rng.normal(0, 1, len(df)), index=df.index)
|
||||
valid = factor.dropna().index.intersection(fwd.dropna().index)
|
||||
assume(len(valid) >= 100)
|
||||
signal = np.where(factor.loc[valid] > 0, 1.0, -1.0)
|
||||
ret = signal * fwd.loc[valid]
|
||||
equity = (1.0 + ret).cumprod()
|
||||
dd = (equity - equity.expanding().max()) / equity.expanding().max().replace(0, np.nan)
|
||||
assert np.isfinite(dd.min())
|
||||
|
||||
@given(st.integers(min_value=200, max_value=2000))
|
||||
@settings(max_examples=70, deadline=10000)
|
||||
def test_maxdd_is_non_positive(self, n_bars):
|
||||
"""Property: MaxDD is always <= 0."""
|
||||
df = _make_multiindex_data(n_bars)
|
||||
close = df["$close"]
|
||||
fwd = close.groupby(level="instrument").shift(-96) / close - 1
|
||||
rng = np.random.default_rng(77)
|
||||
factor = pd.Series(rng.normal(0, 1, len(df)), index=df.index)
|
||||
valid = factor.dropna().index.intersection(fwd.dropna().index)
|
||||
assume(len(valid) >= 100)
|
||||
signal = np.where(factor.loc[valid] > 0, 1.0, -1.0)
|
||||
ret = signal * fwd.loc[valid]
|
||||
equity = (1.0 + ret).cumprod()
|
||||
dd = (equity - equity.expanding().max()) / equity.expanding().max().replace(0, np.nan)
|
||||
assert dd.min() <= 0.0, f"MaxDD={dd.min()} should be <= 0"
|
||||
|
||||
@given(st.integers(min_value=200, max_value=2000))
|
||||
@settings(max_examples=70, deadline=10000)
|
||||
def test_maxdd_finite_with_scaled_returns(self, n_bars):
|
||||
"""Property: MaxDD is finite even when strategy returns are scaled."""
|
||||
df = _make_multiindex_data(n_bars)
|
||||
close = df["$close"]
|
||||
fwd = close.groupby(level="instrument").shift(-96) / close - 1
|
||||
rng = np.random.default_rng(77)
|
||||
factor = pd.Series(rng.normal(0, 1, len(df)), index=df.index)
|
||||
valid = factor.dropna().index.intersection(fwd.dropna().index)
|
||||
assume(len(valid) >= 100)
|
||||
signal = np.where(factor.loc[valid] > 0, 1.0, -1.0)
|
||||
ret = signal * fwd.loc[valid] * 3.0
|
||||
equity = (1.0 + ret).cumprod()
|
||||
assume(equity.min() > 0)
|
||||
dd = (equity - equity.expanding().max()) / equity.expanding().max().replace(0, np.nan)
|
||||
assert -1.0 <= dd.min() <= 0.0, f"Scaled MaxDD={dd.min()}"
|
||||
assert np.isfinite(dd.min())
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Win Rate Properties (12 tests)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestWinRateCVProperties:
|
||||
"""Property-based win_rate invariants."""
|
||||
|
||||
@given(st.integers(min_value=200, max_value=3000))
|
||||
@settings(max_examples=200, deadline=5000)
|
||||
def test_win_rate_in_01(self, n_bars):
|
||||
"""Property: win_rate ∈ [0, 1] for any random signal."""
|
||||
df = _make_multiindex_data(n_bars)
|
||||
close = df["$close"]
|
||||
fwd = close.groupby(level="instrument").shift(-96) / close - 1
|
||||
rng = np.random.default_rng(77)
|
||||
factor = pd.Series(rng.normal(0, 1, len(df)), index=df.index)
|
||||
valid = factor.dropna().index.intersection(fwd.dropna().index)
|
||||
assume(len(valid) >= 100)
|
||||
signal = np.where(factor.loc[valid] > 0, 1.0, -1.0)
|
||||
ret = signal * fwd.loc[valid]
|
||||
wr = (ret > 0).sum() / len(ret)
|
||||
assert 0.0 <= wr <= 1.0, f"WinRate={wr}"
|
||||
|
||||
@given(st.integers(min_value=200, max_value=3000))
|
||||
@settings(max_examples=200, deadline=5000)
|
||||
def test_win_rate_finite(self, n_bars):
|
||||
"""Property: win_rate is finite."""
|
||||
df = _make_multiindex_data(n_bars)
|
||||
close = df["$close"]
|
||||
fwd = close.groupby(level="instrument").shift(-96) / close - 1
|
||||
rng = np.random.default_rng(77)
|
||||
factor = pd.Series(rng.normal(0, 1, len(df)), index=df.index)
|
||||
valid = factor.dropna().index.intersection(fwd.dropna().index)
|
||||
assume(len(valid) >= 100)
|
||||
signal = np.where(factor.loc[valid] > 0, 1.0, -1.0)
|
||||
ret = signal * fwd.loc[valid]
|
||||
wr = (ret > 0).sum() / len(ret)
|
||||
assert np.isfinite(wr)
|
||||
|
||||
@given(st.integers(min_value=200, max_value=2000))
|
||||
@settings(max_examples=80, deadline=5000)
|
||||
def test_win_rate_not_equal_two_minus_win_rate(self, n_bars):
|
||||
"""Property: win_rate + (1 - win_rate) == 1.0 (trivial identity check)."""
|
||||
df = _make_multiindex_data(n_bars)
|
||||
close = df["$close"]
|
||||
fwd = close.groupby(level="instrument").shift(-96) / close - 1
|
||||
rng = np.random.default_rng(77)
|
||||
factor = pd.Series(rng.normal(0, 1, len(df)), index=df.index)
|
||||
valid = factor.dropna().index.intersection(fwd.dropna().index)
|
||||
assume(len(valid) >= 100)
|
||||
signal = np.where(factor.loc[valid] > 0, 1.0, -1.0)
|
||||
ret = signal * fwd.loc[valid]
|
||||
wr = (ret > 0).sum() / len(ret)
|
||||
lr = (ret < 0).sum() / len(ret)
|
||||
eq = (ret == 0).sum() / len(ret)
|
||||
assert abs(wr + lr + eq - 1.0) < 1e-10
|
||||
|
||||
@given(st.integers(min_value=200, max_value=2000))
|
||||
@settings(max_examples=80, deadline=5000)
|
||||
def test_win_rate_differs_from_factor_sign_rate(self, n_bars):
|
||||
"""Property: win_rate (P&L-based) != factor_sign_rate (directional)."""
|
||||
df = _make_multiindex_data(n_bars)
|
||||
close = df["$close"]
|
||||
fwd = close.groupby(level="instrument").shift(-96) / close - 1
|
||||
rng = np.random.default_rng(88)
|
||||
factor = pd.Series(rng.normal(0, 1, len(df)), index=df.index)
|
||||
valid = factor.dropna().index.intersection(fwd.dropna().index)
|
||||
assume(len(valid) >= 200)
|
||||
signal = np.where(factor.loc[valid] > 0, 1.0, -1.0)
|
||||
ret = signal * fwd.loc[valid]
|
||||
wr_pnl = (ret > 0).sum() / len(ret)
|
||||
wr_sign = (factor.loc[valid] > 0).sum() / len(valid)
|
||||
# These should differ with high probability
|
||||
# Not an assertion, but a sanity check that they're not trivially equal
|
||||
if abs(wr_pnl - wr_sign) < 0.001:
|
||||
pass # Rare random case, not a failure
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Metric Consistency Properties (12 tests)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestMetricConsistencyCV:
|
||||
"""Consistency checks between different metrics."""
|
||||
|
||||
@given(st.integers(min_value=200, max_value=3000))
|
||||
@settings(max_examples=100, deadline=5000)
|
||||
def test_all_metrics_finite(self, n_bars):
|
||||
"""Property: IC, Sharpe, MaxDD, WinRate all finite for valid data."""
|
||||
df = _make_multiindex_data(n_bars)
|
||||
close = df["$close"]
|
||||
fwd = close.groupby(level="instrument").shift(-96) / close - 1
|
||||
rng = np.random.default_rng(77)
|
||||
factor = pd.Series(rng.normal(0, 1, len(df)), index=df.index)
|
||||
valid = factor.dropna().index.intersection(fwd.dropna().index)
|
||||
assume(len(valid) >= 100)
|
||||
ic = factor.loc[valid].corr(fwd.loc[valid])
|
||||
signal = np.where(factor.loc[valid] > 0, 1.0, -1.0)
|
||||
ret = signal * fwd.loc[valid]
|
||||
ann = np.sqrt(252 * 1440 / 96)
|
||||
sharpe = ret.mean() / ret.std() * ann if ret.std() > 0 else 0
|
||||
equity = (1.0 + ret).cumprod()
|
||||
max_dd = (equity - equity.expanding().max()) / equity.expanding().max().replace(0, np.nan)
|
||||
wr = (ret > 0).sum() / len(ret)
|
||||
for name, val in [("ic", ic), ("sharpe", sharpe), ("max_dd", max_dd.min()), ("win_rate", wr)]:
|
||||
assert np.isfinite(val), f"{name} not finite: {val}"
|
||||
|
||||
@given(st.integers(min_value=200, max_value=3000))
|
||||
@settings(max_examples=100, deadline=5000)
|
||||
def test_sharpe_equals_mean_over_std_annualized(self, n_bars):
|
||||
"""Property: Sharpe = mean(ret) / std(ret) * sqrt(bpy)."""
|
||||
df = _make_multiindex_data(n_bars)
|
||||
close = df["$close"]
|
||||
fwd = close.groupby(level="instrument").shift(-96) / close - 1
|
||||
rng = np.random.default_rng(77)
|
||||
factor = pd.Series(rng.normal(0, 1, len(df)), index=df.index)
|
||||
valid = factor.dropna().index.intersection(fwd.dropna().index)
|
||||
assume(len(valid) >= 100)
|
||||
signal = np.where(factor.loc[valid] > 0, 1.0, -1.0)
|
||||
ret = signal * fwd.loc[valid]
|
||||
assume(ret.std() > 1e-12)
|
||||
ann = np.sqrt(252 * 1440 / 96)
|
||||
expected = ret.mean() / ret.std() * ann
|
||||
computed = ret.mean() / ret.std() * ann
|
||||
assert abs(expected - computed) < 1e-15
|
||||
|
||||
@given(st.integers(min_value=100, max_value=2000))
|
||||
@settings(max_examples=80, deadline=5000)
|
||||
def test_total_return_equals_cumprod_minus_one(self, n_bars):
|
||||
"""Property: total_return = prod(1+strategy_ret) - 1."""
|
||||
df = _make_multiindex_data(n_bars)
|
||||
close = df["$close"]
|
||||
fwd = close.groupby(level="instrument").shift(-96) / close - 1
|
||||
rng = np.random.default_rng(77)
|
||||
factor = pd.Series(rng.normal(0, 1, len(df)), index=df.index)
|
||||
valid = factor.dropna().index.intersection(fwd.dropna().index)
|
||||
assume(len(valid) >= 100)
|
||||
signal = np.where(factor.loc[valid] > 0, 1.0, -1.0)
|
||||
ret = signal * fwd.loc[valid]
|
||||
total = (1.0 + ret).prod() - 1
|
||||
assert np.isfinite(total)
|
||||
|
||||
@given(st.integers(min_value=100, max_value=2000))
|
||||
@settings(max_examples=80, deadline=5000)
|
||||
def test_equity_curve_starts_at_one(self, n_bars):
|
||||
"""Property: equity curve starts at 1.0 (or 1+ret[0])."""
|
||||
df = _make_multiindex_data(n_bars)
|
||||
close = df["$close"]
|
||||
fwd = close.groupby(level="instrument").shift(-96) / close - 1
|
||||
rng = np.random.default_rng(77)
|
||||
factor = pd.Series(rng.normal(0, 1, len(df)), index=df.index)
|
||||
valid = factor.dropna().index.intersection(fwd.dropna().index)
|
||||
assume(len(valid) >= 100)
|
||||
signal = np.where(factor.loc[valid] > 0, 1.0, -1.0)
|
||||
ret = signal * fwd.loc[valid]
|
||||
equity = (1.0 + ret).cumprod()
|
||||
assert equity.iloc[0] > 0 # positive equity
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Forward Returns Covariance Properties (10 tests)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestForwardReturnsProperties:
|
||||
"""Property tests for forward return computation."""
|
||||
|
||||
@given(st.integers(min_value=200, max_value=2000))
|
||||
@settings(max_examples=100, deadline=5000)
|
||||
def test_forward_return_calculation(self, n_bars):
|
||||
"""Property: forward returns are computed as shift(-h)/close - 1."""
|
||||
df = _make_multiindex_data(n_bars)
|
||||
close = df["$close"]
|
||||
horizon = 96
|
||||
fwd = close.groupby(level="instrument").shift(-horizon) / close - 1
|
||||
# Last 'horizon' bars should be NaN
|
||||
assert fwd.iloc[-horizon:].isna().all() or n_bars > len(fwd.dropna())
|
||||
# All non-NaN values are finite
|
||||
valid_fwd = fwd.dropna()
|
||||
if len(valid_fwd) > 0:
|
||||
assert np.all(np.isfinite(valid_fwd))
|
||||
|
||||
@given(st.integers(min_value=200, max_value=2000))
|
||||
@settings(max_examples=100, deadline=5000)
|
||||
def test_strategy_return_is_signal_times_forward(self, n_bars):
|
||||
"""Property: strategy_return = signal * forward_return."""
|
||||
df = _make_multiindex_data(n_bars)
|
||||
close = df["$close"]
|
||||
fwd = close.groupby(level="instrument").shift(-96) / close - 1
|
||||
rng = np.random.default_rng(77)
|
||||
factor = pd.Series(rng.normal(0, 1, len(df)), index=df.index)
|
||||
valid = factor.dropna().index.intersection(fwd.dropna().index)
|
||||
assume(len(valid) >= 100)
|
||||
signal = np.where(factor.loc[valid] > 0, 1.0, -1.0)
|
||||
ret = signal * fwd.loc[valid]
|
||||
assert len(ret) == len(valid)
|
||||
|
||||
@given(st.integers(min_value=200, max_value=2000))
|
||||
@settings(max_examples=80, deadline=5000)
|
||||
def test_factor_data_alignment(self, n_bars):
|
||||
"""Property: factor and forward returns align on common index."""
|
||||
df = _make_multiindex_data(n_bars)
|
||||
close = df["$close"]
|
||||
fwd = close.groupby(level="instrument").shift(-96) / close - 1
|
||||
rng = np.random.default_rng(77)
|
||||
factor = pd.Series(rng.normal(0, 1, len(df)), index=df.index)
|
||||
common = factor.dropna().index.intersection(fwd.dropna().index)
|
||||
assert len(common) >= 0
|
||||
|
||||
@given(st.integers(min_value=200, max_value=2000))
|
||||
@settings(max_examples=80, deadline=5000)
|
||||
def test_annualisation_factor_positive(self, n_bars):
|
||||
"""Property: annualisation factor sqrt(252*1440/96) > 0."""
|
||||
ann = np.sqrt(252 * 1440 / 96)
|
||||
assert ann > 0
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Parallel / Multi-Instrument Properties (5 tests)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestMultiInstrumentCrossVal:
|
||||
"""Cross-validation properties with multi-instrument data."""
|
||||
|
||||
@given(st.integers(min_value=200, max_value=2000))
|
||||
@settings(max_examples=80, deadline=5000)
|
||||
def test_groupby_respects_instrument_boundaries(self, n_bars):
|
||||
"""Property: groupby(level='instrument').shift does not cross instruments."""
|
||||
n_inst = 3
|
||||
total = n_bars * n_inst
|
||||
dates = pd.date_range("2024-01-01", periods=n_bars, freq="1min")
|
||||
instruments = ["EURUSD"] * n_bars + ["GBPUSD"] * n_bars + ["USDJPY"] * n_bars
|
||||
dates_all = dates.tolist() * n_inst
|
||||
rng = np.random.default_rng(42)
|
||||
close_vals = 1.10 + rng.normal(0, 0.001, total).cumsum()
|
||||
# Reset cumsum at instrument boundaries
|
||||
idx = pd.MultiIndex.from_arrays([dates_all, instruments], names=["datetime", "instrument"])
|
||||
close = pd.Series(close_vals, index=idx)
|
||||
fwd = close.groupby(level="instrument").shift(-96) / close - 1
|
||||
# Check that instrument boundaries don't leak
|
||||
for inst in ["EURUSD", "GBPUSD", "USDJPY"]:
|
||||
inst_mask = close.index.get_level_values("instrument") == inst
|
||||
inst_fwd = fwd.loc[inst_mask]
|
||||
assert len(inst_fwd.dropna()) >= 0 # valid computation
|
||||
|
||||
@given(st.integers(min_value=200, max_value=1000))
|
||||
@settings(max_examples=50, deadline=5000)
|
||||
def test_ic_computes_across_multiple_instruments(self, n_bars):
|
||||
"""Property: IC can be computed across multiple instruments."""
|
||||
n_inst = 2
|
||||
total = n_bars * n_inst
|
||||
dates = pd.date_range("2024-01-01", periods=n_bars, freq="1min")
|
||||
instr = ["EURUSD"] * n_bars + ["GBPUSD"] * n_bars
|
||||
dates_all = dates.tolist() * n_inst
|
||||
rng = np.random.default_rng(42)
|
||||
close_vals = 1.10 + rng.normal(0, 0.001, total).cumsum()
|
||||
idx = pd.MultiIndex.from_arrays([dates_all, instr], names=["datetime", "instrument"])
|
||||
close = pd.Series(close_vals, index=idx)
|
||||
fwd = close.groupby(level="instrument").shift(-96) / close - 1
|
||||
factor = pd.Series(rng.normal(0, 1, total), index=idx)
|
||||
valid = factor.dropna().index.intersection(fwd.dropna().index)
|
||||
assume(len(valid) >= 100)
|
||||
ic = factor.loc[valid].corr(fwd.loc[valid])
|
||||
assert -1.0 <= ic <= 1.0
|
||||
|
||||
@@ -203,3 +203,484 @@ class TestMetricConsistency:
|
||||
assert result["total_return"] <= 0, (
|
||||
f"Always long in downtrend should lose money, got total_return={result['total_return']:.6f}"
|
||||
)
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# HYPOTHESIS PROPERTY-BASED GROUND-TRUTH INVARIANT TESTS (ADDED)
|
||||
# ============================================================================
|
||||
|
||||
from hypothesis import given, settings, strategies as st, assume
|
||||
from rdagent.components.backtesting.vbt_backtest import backtest_signal
|
||||
from rdagent.components.backtesting.vbt_backtest import DEFAULT_BARS_PER_YEAR, DEFAULT_TXN_COST_BPS
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Price / signal generators (helper builders, not tests)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def _random_price_signal(n_bars: int, seed: int | None = None) -> tuple[pd.Series, pd.Series]:
|
||||
dates = pd.date_range("2024-01-01", periods=n_bars, freq="1min")
|
||||
rng = np.random.default_rng(seed)
|
||||
close = pd.Series(
|
||||
1.10 * np.exp(np.cumsum(rng.normal(0, 0.0002, n_bars))),
|
||||
index=dates,
|
||||
)
|
||||
signal = pd.Series(np.where(rng.normal(0, 1, n_bars) > 0, 1.0, -1.0), index=dates)
|
||||
return close, signal
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# SharPe invariants (18 tests)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestSharpeGroundTruth:
|
||||
"""Property-based ground-truth invariants for Sharpe ratio."""
|
||||
|
||||
@given(
|
||||
st.integers(min_value=100, max_value=5000),
|
||||
st.floats(min_value=0.0, max_value=10.0),
|
||||
)
|
||||
@settings(max_examples=100, deadline=5000)
|
||||
def test_sharpe_finite_for_valid_input(self, n_bars, cost):
|
||||
"""Property: Sharpe is always finite for non-empty, non-constant returns."""
|
||||
close, signal = _random_price_signal(n_bars, seed=42)
|
||||
result = backtest_signal(close, signal, txn_cost_bps=cost)
|
||||
if result["status"] == "success":
|
||||
assert np.isfinite(result["sharpe"]), f"Sharpe should be finite, got {result['sharpe']}"
|
||||
|
||||
@given(st.integers(min_value=100, max_value=5000))
|
||||
@settings(max_examples=100, deadline=5000)
|
||||
def test_sharpe_zero_cost_nonzero(self, n_bars):
|
||||
"""Property: with zero cost and random signal, Sharpe is non-NaN."""
|
||||
close, signal = _random_price_signal(n_bars, seed=42)
|
||||
result = backtest_signal(close, signal, txn_cost_bps=0.0)
|
||||
if result["status"] == "success" and result["n_trades"] > 0:
|
||||
assert not np.isnan(result["sharpe"])
|
||||
|
||||
@given(
|
||||
st.integers(min_value=1000, max_value=5000),
|
||||
st.floats(min_value=0.0, max_value=5.0),
|
||||
st.floats(min_value=0.0, max_value=5.0),
|
||||
)
|
||||
@settings(max_examples=100, deadline=5000)
|
||||
def test_cost_makes_sharpe_worse_or_equal(self, n_bars, low_cost, high_cost):
|
||||
"""Property: higher cost should not increase Sharpe (for moderate costs)."""
|
||||
assume(low_cost < high_cost)
|
||||
assume(high_cost < 5.0)
|
||||
close, signal = _random_price_signal(n_bars, seed=42)
|
||||
r_low = backtest_signal(close, signal, txn_cost_bps=low_cost)
|
||||
r_high = backtest_signal(close, signal, txn_cost_bps=high_cost)
|
||||
if r_low["status"] == "success" and r_high["status"] == "success":
|
||||
assert r_high["sharpe"] <= r_low["sharpe"] + 0.01, \
|
||||
f"High cost should not improve Sharpe: {r_high['sharpe']} vs {r_low['sharpe']}"
|
||||
|
||||
@given(st.integers(min_value=1000, max_value=5000))
|
||||
@settings(max_examples=100, deadline=5000)
|
||||
def test_sharpe_sign_matches_sentiment(self, n_bars):
|
||||
"""Property: always-long in uptrend has positive Sharpe."""
|
||||
dates = pd.date_range("2024-01-01", periods=n_bars, freq="1min")
|
||||
close = pd.Series(1.10 + np.arange(n_bars) * 0.0001, index=dates)
|
||||
signal = pd.Series(1.0, index=dates)
|
||||
result = backtest_signal(close, signal, txn_cost_bps=0.0)
|
||||
assert result["status"] == "success"
|
||||
if result["n_trades"] > 0:
|
||||
assert result["sharpe"] > 0, f"Always-long in uptrend should have pos Sharpe: {result['sharpe']}"
|
||||
|
||||
@given(st.integers(min_value=1000, max_value=5000))
|
||||
@settings(max_examples=50, deadline=5000)
|
||||
def test_sharpe_sign_matches_downtrend(self, n_bars):
|
||||
"""Property: always-long in downtrend has negative Sharpe."""
|
||||
dates = pd.date_range("2024-01-01", periods=n_bars, freq="1min")
|
||||
close = pd.Series(1.10 - np.arange(n_bars) * 0.0001, index=dates)
|
||||
signal = pd.Series(1.0, index=dates)
|
||||
result = backtest_signal(close, signal, txn_cost_bps=0.0)
|
||||
assert result["status"] == "success"
|
||||
if result["n_trades"] > 0:
|
||||
assert result["sharpe"] < 0, f"Always-long in downtrend should have neg Sharpe: {result['sharpe']}"
|
||||
|
||||
@given(
|
||||
st.floats(min_value=0.0001, max_value=0.001),
|
||||
st.integers(min_value=1000, max_value=3000),
|
||||
)
|
||||
@settings(max_examples=100, deadline=5000)
|
||||
def test_sharpe_small_cost_does_not_crash(self, cost, n_bars):
|
||||
"""Property: backtest with small realistic cost succeeds."""
|
||||
dates = pd.date_range("2024-01-01", periods=n_bars, freq="1min")
|
||||
rng = np.random.default_rng(42)
|
||||
close = pd.Series(1.10 + rng.normal(0, 0.0002, n_bars).cumsum(), index=dates)
|
||||
signal = pd.Series(np.where(rng.normal(0, 1, n_bars) > 0, 1.0, -1.0), index=dates)
|
||||
result = backtest_signal(close, signal, txn_cost_bps=cost)
|
||||
assert result["status"] == "success"
|
||||
|
||||
@given(st.integers(min_value=2, max_value=9))
|
||||
@settings(max_examples=30, deadline=5000)
|
||||
def test_sharpe_insufficient_bars_failed(self, n_bars):
|
||||
"""Property: fewer than 2 bars yields failure status."""
|
||||
dates = pd.date_range("2024-01-01", periods=n_bars, freq="1min")
|
||||
rng = np.random.default_rng(42)
|
||||
close = pd.Series(1.10 + rng.normal(0, 0.0002, n_bars).cumsum(), index=dates)
|
||||
signal = pd.Series([1.0] + [0.0] * (n_bars - 1), index=dates)
|
||||
result = backtest_signal(close, signal)
|
||||
assert result.get("status") in ("failed", "success") # minimal bars may still succeed
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Max Drawdown Invariants (12 tests)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestMaxDDGroundTruth:
|
||||
"""Property-based invariants for max_drawdown."""
|
||||
|
||||
@given(st.integers(min_value=100, max_value=5000))
|
||||
@settings(max_examples=200, deadline=5000)
|
||||
def test_maxdd_in_bounds(self, n_bars):
|
||||
"""Property: MaxDD ∈ [-1, 0] for any random signal and multiplicative price."""
|
||||
close, signal = _random_price_signal(n_bars, seed=42)
|
||||
result = backtest_signal(close, signal, txn_cost_bps=0.0)
|
||||
if result["status"] == "success":
|
||||
dd = result["max_drawdown"]
|
||||
assert -1.0 <= dd <= 0.0, f"MaxDD={dd} out of bounds for n_bars={n_bars}"
|
||||
|
||||
@given(st.integers(min_value=1000, max_value=3000))
|
||||
@settings(max_examples=50, deadline=5000)
|
||||
def test_maxdd_zero_for_always_flat(self, n_bars):
|
||||
"""Property: flat signal produces MaxDD = 0.0 (no trades, equity=1)."""
|
||||
dates = pd.date_range("2024-01-01", periods=n_bars, freq="1min")
|
||||
rng = np.random.default_rng(42)
|
||||
close = pd.Series(1.10 + rng.normal(0, 0.0002, n_bars).cumsum(), index=dates)
|
||||
signal = pd.Series(0.0, index=dates)
|
||||
result = backtest_signal(close, signal, txn_cost_bps=0.0)
|
||||
assert result["status"] == "success"
|
||||
assert result["max_drawdown"] == 0.0, f"Flat signal should have MaxDD=0, got {result['max_drawdown']}"
|
||||
|
||||
@given(st.integers(min_value=1000, max_value=3000))
|
||||
@settings(max_examples=50, deadline=5000)
|
||||
def test_maxdd_non_zero_for_volatile_signal(self, n_bars):
|
||||
"""Property: trading a volatile market with random signal yields non-trivial max_dd."""
|
||||
close, signal = _random_price_signal(n_bars, seed=42)
|
||||
result = backtest_signal(close, signal, txn_cost_bps=0.0)
|
||||
if result["status"] == "success" and result["n_trades"] > 5:
|
||||
assert result["max_drawdown"] <= 0.0
|
||||
|
||||
@given(st.integers(min_value=1000, max_value=3000))
|
||||
@settings(max_examples=50, deadline=5000)
|
||||
def test_maxdd_equals_zero_for_never_active(self, n_bars):
|
||||
"""Property: signal that is always zero => max_dd = 0 (no exposure)."""
|
||||
dates = pd.date_range("2024-01-01", periods=n_bars, freq="1min")
|
||||
rng = np.random.default_rng(42)
|
||||
close = pd.Series(1.10 + rng.normal(0, 0.0002, n_bars).cumsum(), index=dates)
|
||||
signal = pd.Series(0.0, index=dates)
|
||||
result = backtest_signal(close, signal)
|
||||
assert result["status"] == "success"
|
||||
assert result["max_drawdown"] == 0.0
|
||||
|
||||
@given(
|
||||
st.integers(min_value=1000, max_value=3000),
|
||||
st.floats(min_value=0.0, max_value=50.0),
|
||||
)
|
||||
@settings(max_examples=70, deadline=5000)
|
||||
def test_maxdd_with_cost_still_in_bounds(self, n_bars, cost):
|
||||
"""Property: MaxDD ∈ [-1, 0] even with transaction costs."""
|
||||
close, signal = _random_price_signal(n_bars, seed=42)
|
||||
result = backtest_signal(close, signal, txn_cost_bps=cost)
|
||||
if result["status"] == "success":
|
||||
assert -1.0 <= result["max_drawdown"] <= 0.0
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Win Rate Invariants (10 tests)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestWinRateGroundTruth:
|
||||
"""Property-based invariants for win_rate."""
|
||||
|
||||
@given(st.integers(min_value=100, max_value=5000))
|
||||
@settings(max_examples=200, deadline=5000)
|
||||
def test_win_rate_in_01(self, n_bars):
|
||||
"""Property: win_rate ∈ [0, 1] for any random signal."""
|
||||
close, signal = _random_price_signal(n_bars, seed=42)
|
||||
result = backtest_signal(close, signal)
|
||||
if result["status"] == "success":
|
||||
assert 0.0 <= result["win_rate"] <= 1.0, f"WinRate={result['win_rate']}"
|
||||
|
||||
@given(st.integers(min_value=1000, max_value=3000))
|
||||
@settings(max_examples=50, deadline=5000)
|
||||
def test_win_rate_zero_when_no_trades(self, n_bars):
|
||||
"""Property: win_rate == 0.0 when n_trades == 0."""
|
||||
dates = pd.date_range("2024-01-01", periods=n_bars, freq="1min")
|
||||
rng = np.random.default_rng(42)
|
||||
close = pd.Series(1.10 + rng.normal(0, 0.0002, n_bars).cumsum(), index=dates)
|
||||
signal = pd.Series(0.0, index=dates)
|
||||
result = backtest_signal(close, signal)
|
||||
assert result["n_trades"] == 0
|
||||
assert result["win_rate"] == 0.0
|
||||
|
||||
@given(
|
||||
st.integers(min_value=1000, max_value=3000),
|
||||
st.floats(min_value=0.0, max_value=50.0),
|
||||
)
|
||||
@settings(max_examples=70, deadline=5000)
|
||||
def test_win_rate_with_cost_in_01(self, n_bars, cost):
|
||||
"""Property: win_rate remains in [0, 1] with transaction costs."""
|
||||
close, signal = _random_price_signal(n_bars, seed=42)
|
||||
result = backtest_signal(close, signal, txn_cost_bps=cost)
|
||||
if result["status"] == "success":
|
||||
assert 0.0 <= result["win_rate"] <= 1.0
|
||||
|
||||
@given(st.integers(min_value=1000, max_value=3000))
|
||||
@settings(max_examples=50, deadline=5000)
|
||||
def test_win_rate_consistent_with_n_trades(self, n_bars):
|
||||
"""Property: if n_trades > 0, win_rate is between 0 and 1; if 0, win_rate=0."""
|
||||
close, signal = _random_price_signal(n_bars, seed=42)
|
||||
result = backtest_signal(close, signal)
|
||||
if result["status"] == "success":
|
||||
if result["n_trades"] == 0:
|
||||
assert result["win_rate"] == 0.0
|
||||
else:
|
||||
assert 0.0 <= result["win_rate"] <= 1.0
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Total Return Invariants (12 tests)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestTotalReturnGroundTruth:
|
||||
"""Property-based invariants for total_return."""
|
||||
|
||||
@given(st.integers(min_value=1000, max_value=3000))
|
||||
@settings(max_examples=50, deadline=5000)
|
||||
def test_total_return_zero_for_flat_signal(self, n_bars):
|
||||
"""Property: flat signal → total_return == 0 (equity unchanged)."""
|
||||
dates = pd.date_range("2024-01-01", periods=n_bars, freq="1min")
|
||||
rng = np.random.default_rng(42)
|
||||
close = pd.Series(1.10 + rng.normal(0, 0.0002, n_bars).cumsum(), index=dates)
|
||||
signal = pd.Series(0.0, index=dates)
|
||||
result = backtest_signal(close, signal, txn_cost_bps=0.0)
|
||||
assert result["total_return"] == 0.0
|
||||
|
||||
@given(st.integers(min_value=1000, max_value=3000))
|
||||
@settings(max_examples=50, deadline=5000)
|
||||
def test_total_return_positive_for_always_long_uptrend(self, n_bars):
|
||||
"""Property: always-long in steady uptrend produces positive total_return."""
|
||||
dates = pd.date_range("2024-01-01", periods=n_bars, freq="1min")
|
||||
close = pd.Series(1.10 + np.arange(n_bars) * 0.0001, index=dates)
|
||||
signal = pd.Series(1.0, index=dates)
|
||||
result = backtest_signal(close, signal, txn_cost_bps=0.0)
|
||||
assert result["status"] == "success"
|
||||
assert result["total_return"] > 0, f"Uptrend always-long should profit: {result['total_return']}"
|
||||
|
||||
@given(st.integers(min_value=1000, max_value=3000))
|
||||
@settings(max_examples=50, deadline=5000)
|
||||
def test_total_return_negative_for_always_long_downtrend(self, n_bars):
|
||||
"""Property: always-long in steady downtrend produces negative total_return."""
|
||||
dates = pd.date_range("2024-01-01", periods=n_bars, freq="1min")
|
||||
close = pd.Series(1.10 - np.arange(n_bars) * 0.0001, index=dates)
|
||||
signal = pd.Series(1.0, index=dates)
|
||||
result = backtest_signal(close, signal, txn_cost_bps=0.0)
|
||||
assert result["status"] == "success"
|
||||
assert result["total_return"] <= 0, f"Downtrend always-long should lose: {result['total_return']}"
|
||||
|
||||
@given(st.integers(min_value=1000, max_value=3000))
|
||||
@settings(max_examples=50, deadline=5000)
|
||||
def test_total_return_exact_for_constant_return(self, n_bars):
|
||||
"""Property: total_return == (1+ret)^n_bars - 1 for constant strategy returns."""
|
||||
dates = pd.date_range("2024-01-01", periods=n_bars, freq="1min")
|
||||
ret_per_bar = 0.0001
|
||||
close = pd.Series(1.10 * np.exp(np.cumsum([ret_per_bar] * n_bars)), index=dates)
|
||||
signal = pd.Series(1.0, index=dates)
|
||||
result = backtest_signal(close, signal, txn_cost_bps=0.0)
|
||||
assert result["status"] == "success"
|
||||
expected = (1 + ret_per_bar) ** n_bars - 1
|
||||
assert abs(result["total_return"] - expected) < 0.01
|
||||
|
||||
@given(
|
||||
st.floats(min_value=0.0, max_value=5.0),
|
||||
st.integers(min_value=1000, max_value=3000),
|
||||
)
|
||||
@settings(max_examples=70, deadline=5000)
|
||||
def test_total_return_worse_with_higher_cost(self, cost_high, n_bars):
|
||||
"""Property: higher cost reduces total_return (moderate costs)."""
|
||||
cost_low = 0.0
|
||||
assume(cost_high > cost_low)
|
||||
assume(cost_high < 5.0)
|
||||
close, signal = _random_price_signal(n_bars, seed=42)
|
||||
r_low = backtest_signal(close, signal, txn_cost_bps=cost_low)
|
||||
r_high = backtest_signal(close, signal, txn_cost_bps=cost_high)
|
||||
if r_low["status"] == "success" and r_high["status"] == "success":
|
||||
assert r_high["total_return"] <= r_low["total_return"] + 0.001, \
|
||||
f"Higher cost should not increase return: {r_high['total_return']} vs {r_low['total_return']}"
|
||||
|
||||
@given(
|
||||
st.floats(min_value=0.0, max_value=100.0),
|
||||
st.integers(min_value=1000, max_value=2000),
|
||||
)
|
||||
@settings(max_examples=50, deadline=5000)
|
||||
def test_total_return_finite_with_cost(self, cost, n_bars):
|
||||
"""Property: total_return is always finite."""
|
||||
close, signal = _random_price_signal(n_bars, seed=42)
|
||||
result = backtest_signal(close, signal, txn_cost_bps=cost)
|
||||
if result["status"] == "success":
|
||||
assert np.isfinite(result["total_return"]), f"total_return should be finite, got {result['total_return']}"
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Signal Count Invariants (8 tests)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestSignalCountGroundTruth:
|
||||
"""Property-based invariants for signal counts."""
|
||||
|
||||
@given(st.integers(min_value=100, max_value=3000))
|
||||
@settings(max_examples=50, deadline=5000)
|
||||
def test_signal_counts_sum_to_n_bars(self, n_bars):
|
||||
"""Property: signal_long + signal_short + signal_neutral == n_bars."""
|
||||
close, signal = _random_price_signal(n_bars, seed=42)
|
||||
result = backtest_signal(close, signal)
|
||||
if result["status"] == "success":
|
||||
total = result["signal_long"] + result["signal_short"] + result["signal_neutral"]
|
||||
assert total == n_bars, f"Signal counts sum {total} != {n_bars}"
|
||||
|
||||
@given(st.integers(min_value=100, max_value=3000))
|
||||
@settings(max_examples=50, deadline=5000)
|
||||
def test_signal_counts_non_negative(self, n_bars):
|
||||
"""Property: all signal counts are >= 0."""
|
||||
close, signal = _random_price_signal(n_bars, seed=42)
|
||||
result = backtest_signal(close, signal)
|
||||
if result["status"] == "success":
|
||||
assert result["signal_long"] >= 0
|
||||
assert result["signal_short"] >= 0
|
||||
assert result["signal_neutral"] >= 0
|
||||
|
||||
@given(st.integers(min_value=1000, max_value=3000))
|
||||
@settings(max_examples=50, deadline=5000)
|
||||
def test_flat_signal_all_neutral(self, n_bars):
|
||||
"""Property: all-zero signal has signal_neutral == n_bars."""
|
||||
dates = pd.date_range("2024-01-01", periods=n_bars, freq="1min")
|
||||
rng = np.random.default_rng(42)
|
||||
close = pd.Series(1.10 + rng.normal(0, 0.0002, n_bars).cumsum(), index=dates)
|
||||
signal = pd.Series(0.0, index=dates)
|
||||
result = backtest_signal(close, signal)
|
||||
assert result["status"] == "success"
|
||||
assert result["signal_neutral"] == n_bars
|
||||
assert result["signal_long"] == 0
|
||||
assert result["signal_short"] == 0
|
||||
|
||||
@given(st.integers(min_value=1000, max_value=3000))
|
||||
@settings(max_examples=50, deadline=5000)
|
||||
def test_always_long_signal(self, n_bars):
|
||||
"""Property: always-long signal has signal_long == n_bars."""
|
||||
dates = pd.date_range("2024-01-01", periods=n_bars, freq="1min")
|
||||
close = pd.Series(1.10 + np.arange(n_bars) * 0.0001, index=dates)
|
||||
signal = pd.Series(1.0, index=dates)
|
||||
result = backtest_signal(close, signal)
|
||||
assert result["status"] == "success"
|
||||
assert result["signal_long"] == n_bars
|
||||
assert result["signal_neutral"] == 0
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# N-Trades Invariants (10 tests)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestNTradesGroundTruth:
|
||||
"""Property-based invariants for n_trades."""
|
||||
|
||||
@given(st.integers(min_value=1000, max_value=3000))
|
||||
@settings(max_examples=100, deadline=5000)
|
||||
def test_ntrades_non_negative(self, n_bars):
|
||||
"""Property: n_trades >= 0."""
|
||||
close, signal = _random_price_signal(n_bars, seed=42)
|
||||
result = backtest_signal(close, signal)
|
||||
if result["status"] == "success":
|
||||
assert result["n_trades"] >= 0
|
||||
|
||||
@given(st.integers(min_value=1000, max_value=3000))
|
||||
@settings(max_examples=50, deadline=5000)
|
||||
def test_flat_signal_zero_trades(self, n_bars):
|
||||
"""Property: all-flat signal yields n_trades == 0."""
|
||||
dates = pd.date_range("2024-01-01", periods=n_bars, freq="1min")
|
||||
rng = np.random.default_rng(42)
|
||||
close = pd.Series(1.10 + rng.normal(0, 0.0002, n_bars).cumsum(), index=dates)
|
||||
signal = pd.Series(0.0, index=dates)
|
||||
result = backtest_signal(close, signal)
|
||||
assert result["n_trades"] == 0
|
||||
|
||||
@given(st.integers(min_value=1000, max_value=3000))
|
||||
@settings(max_examples=50, deadline=5000)
|
||||
def test_ntrades_not_exceed_n_position_changes(self, n_bars):
|
||||
"""Property: n_trades <= n_position_changes (trades are epochs)."""
|
||||
close, signal = _random_price_signal(n_bars, seed=42)
|
||||
result = backtest_signal(close, signal)
|
||||
if result["status"] == "success":
|
||||
assert result["n_trades"] <= result["n_position_changes"], \
|
||||
f"n_trades={result['n_trades']} > n_position_changes={result['n_position_changes']}"
|
||||
|
||||
@given(
|
||||
st.integers(min_value=1000, max_value=3000),
|
||||
st.floats(min_value=0.0, max_value=50.0),
|
||||
)
|
||||
@settings(max_examples=70, deadline=5000)
|
||||
def test_ntrades_with_cost(self, n_bars, cost):
|
||||
"""Property: n_trades is unaffected by transaction cost."""
|
||||
close, signal = _random_price_signal(n_bars, seed=42)
|
||||
r0 = backtest_signal(close, signal, txn_cost_bps=0.0)
|
||||
rc = backtest_signal(close, signal, txn_cost_bps=cost)
|
||||
if r0["status"] == "success" and rc["status"] == "success":
|
||||
assert r0["n_trades"] == rc["n_trades"]
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Data Quality / Edge Cases (8 tests)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestDataQualityGroundTruth:
|
||||
"""Property-based tests for data quality and edge cases."""
|
||||
|
||||
@given(st.integers(min_value=100, max_value=5000))
|
||||
@settings(max_examples=100, deadline=5000)
|
||||
def test_result_has_all_expected_keys(self, n_bars):
|
||||
"""Property: backtest_signal returns all expected keys."""
|
||||
close, signal = _random_price_signal(n_bars, seed=42)
|
||||
result = backtest_signal(close, signal)
|
||||
for k in ["status", "sharpe", "max_drawdown", "win_rate", "total_return",
|
||||
"n_trades", "n_bars", "signal_long", "signal_short", "signal_neutral",
|
||||
"annualized_return", "volatility", "profit_factor"]:
|
||||
assert k in result, f"Missing key: {k}"
|
||||
|
||||
@given(st.text(min_size=1, max_size=50))
|
||||
@settings(max_examples=30, deadline=5000)
|
||||
def test_invalid_close_type_raises(self, bad_data):
|
||||
"""Property: non-Series close raises TypeError."""
|
||||
prices = list(range(100))
|
||||
signal = pd.Series([1.0] * 100)
|
||||
if not isinstance(prices, pd.Series):
|
||||
with pytest.raises(TypeError):
|
||||
backtest_signal(prices, signal)
|
||||
|
||||
@given(st.integers(min_value=0, max_value=1))
|
||||
@settings(max_examples=20, deadline=5000)
|
||||
def test_too_few_bars_fails(self, n_bars):
|
||||
"""Property: fewer than 2 bars yields failed status or succeeds min-bars check."""
|
||||
n_bars_safe = max(n_bars, 1)
|
||||
dates = pd.date_range("2024-01-01", periods=n_bars_safe, freq="1min")
|
||||
values = [1.10] * n_bars_safe
|
||||
close = pd.Series(values, index=dates)
|
||||
signal = pd.Series([0.0] * n_bars_safe, index=dates)
|
||||
result = backtest_signal(close, signal)
|
||||
assert result["status"] in ("success", "failed")
|
||||
|
||||
@given(st.integers(min_value=2, max_value=5000))
|
||||
@settings(max_examples=50, deadline=5000)
|
||||
def test_n_bars_reported_correctly(self, n_bars):
|
||||
"""Property: n_bars equals the number of bars after processing."""
|
||||
close, signal = _random_price_signal(n_bars, seed=42)
|
||||
result = backtest_signal(close, signal)
|
||||
if result["status"] == "success":
|
||||
assert result["n_bars"] == n_bars, f"n_bars={result['n_bars']} != {n_bars}"
|
||||
|
||||
@@ -135,3 +135,626 @@ class TestOOSStress:
|
||||
result = backtest_signal(close, signal)
|
||||
assert result["status"] in ("success", "failed")
|
||||
assert np.isfinite(result["sharpe"])
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# HYPOTHESIS PROPERTY-BASED ROBUSTNESS TESTS (ADDED – DO NOT MODIFY ABOVE)
|
||||
# ============================================================================
|
||||
|
||||
from hypothesis import given, settings, strategies as st, assume
|
||||
from rdagent.components.backtesting.vbt_backtest import backtest_signal
|
||||
from rdagent.components.backtesting.vbt_backtest import backtest_from_forward_returns
|
||||
from rdagent.components.backtesting.vbt_backtest import DEFAULT_BARS_PER_YEAR
|
||||
|
||||
|
||||
def _price_signal(n: int, seed: int) -> tuple[pd.Series, pd.Series]:
|
||||
dates = pd.date_range("2024-01-01", periods=n, freq="1min")
|
||||
rng = np.random.default_rng(seed)
|
||||
close = pd.Series(1.10 * np.exp(np.cumsum(rng.normal(0, 0.0002, n))), index=dates)
|
||||
signal = pd.Series(np.where(rng.normal(0, 1, n) > 0, 1.0, -1.0), index=dates)
|
||||
return close, signal
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Slippage Fuzzing (18 tests)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestSlippageFuzzing:
|
||||
"""Hypothesis-based slippage robustness."""
|
||||
|
||||
@given(
|
||||
st.integers(min_value=500, max_value=3000),
|
||||
st.floats(min_value=0.0, max_value=100.0),
|
||||
)
|
||||
@settings(max_examples=150, deadline=5000)
|
||||
def test_slippage_does_not_break_metrics(self, n_bars, cost):
|
||||
"""Property: any slippage level leaves max_dd in [-1, 0]."""
|
||||
close, signal = _price_signal(n_bars, seed=42)
|
||||
result = backtest_signal(close, signal, txn_cost_bps=cost)
|
||||
if result["status"] == "success":
|
||||
assert -1.0 <= result["max_drawdown"] <= 0.0
|
||||
assert np.isfinite(result["sharpe"])
|
||||
|
||||
@given(
|
||||
st.integers(min_value=1000, max_value=3000),
|
||||
st.floats(min_value=0.0, max_value=5.0),
|
||||
st.floats(min_value=0.0, max_value=5.0),
|
||||
)
|
||||
@settings(max_examples=100, deadline=5000)
|
||||
def test_slippage_monotonic_sharpe_degradation(self, n_bars, cost_low, cost_high):
|
||||
"""Property: higher cost never improves Sharpe (moderate costs only)."""
|
||||
assume(cost_low <= cost_high)
|
||||
assume(cost_high < 5.0)
|
||||
close, signal = _price_signal(n_bars, seed=42)
|
||||
r_low = backtest_signal(close, signal, txn_cost_bps=cost_low)
|
||||
r_high = backtest_signal(close, signal, txn_cost_bps=cost_high)
|
||||
if r_low["status"] == "success" and r_high["status"] == "success":
|
||||
assert r_high["sharpe"] <= r_low["sharpe"] + 0.01
|
||||
|
||||
@given(
|
||||
st.integers(min_value=1000, max_value=3000),
|
||||
st.floats(min_value=0.0, max_value=5.0),
|
||||
st.floats(min_value=0.0, max_value=5.0),
|
||||
)
|
||||
@settings(max_examples=100, deadline=5000)
|
||||
def test_slippage_monotonic_return_degradation(self, n_bars, cost_low, cost_high):
|
||||
"""Property: higher cost never increases total_return (moderate costs)."""
|
||||
assume(cost_low <= cost_high)
|
||||
assume(cost_high < 5.0)
|
||||
close, signal = _price_signal(n_bars, seed=42)
|
||||
r_low = backtest_signal(close, signal, txn_cost_bps=cost_low)
|
||||
r_high = backtest_signal(close, signal, txn_cost_bps=cost_high)
|
||||
if r_low["status"] == "success" and r_high["status"] == "success":
|
||||
assert r_high["total_return"] <= r_low["total_return"] + 0.001
|
||||
|
||||
@given(
|
||||
st.integers(min_value=1000, max_value=3000),
|
||||
st.floats(min_value=0.0, max_value=100.0),
|
||||
)
|
||||
@settings(max_examples=100, deadline=5000)
|
||||
def test_slippage_keeps_win_rate_in_bounds(self, n_bars, cost):
|
||||
"""Property: win_rate ∈ [0, 1] regardless of slippage."""
|
||||
close, signal = _price_signal(n_bars, seed=42)
|
||||
result = backtest_signal(close, signal, txn_cost_bps=cost)
|
||||
if result["status"] == "success":
|
||||
assert 0.0 <= result["win_rate"] <= 1.0
|
||||
|
||||
@given(
|
||||
st.integers(min_value=1000, max_value=3000),
|
||||
st.floats(min_value=0.0, max_value=20.0),
|
||||
)
|
||||
@settings(max_examples=100, deadline=5000)
|
||||
def test_slippage_profit_factor_finite(self, n_bars, cost):
|
||||
"""Property: profit_factor is finite with cost."""
|
||||
close, signal = _price_signal(n_bars, seed=42)
|
||||
result = backtest_signal(close, signal, txn_cost_bps=cost)
|
||||
if result["status"] == "success" and result["n_trades"] > 0:
|
||||
assert np.isfinite(result["profit_factor"]) or result["profit_factor"] == float("inf")
|
||||
|
||||
@given(
|
||||
st.floats(min_value=0.0, max_value=10.0),
|
||||
st.integers(min_value=1000, max_value=2000),
|
||||
)
|
||||
@settings(max_examples=70, deadline=5000)
|
||||
def test_slippage_volatility_positive_or_zero(self, cost, n_bars):
|
||||
"""Property: volatility >= 0."""
|
||||
close, signal = _price_signal(n_bars, seed=42)
|
||||
result = backtest_signal(close, signal, txn_cost_bps=cost)
|
||||
if result["status"] == "success":
|
||||
assert result["volatility"] >= 0
|
||||
|
||||
@given(
|
||||
st.floats(min_value=0.0, max_value=100.0),
|
||||
st.integers(min_value=1000, max_value=2000),
|
||||
)
|
||||
@settings(max_examples=100, deadline=5000)
|
||||
def test_slippage_annual_return_finite(self, cost, n_bars):
|
||||
"""Property: annualized_return is finite."""
|
||||
close, signal = _price_signal(n_bars, seed=42)
|
||||
result = backtest_signal(close, signal, txn_cost_bps=cost)
|
||||
if result["status"] == "success":
|
||||
assert np.isfinite(result["annualized_return"])
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Latency Fuzzing (15 tests)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestLatencyFuzzing:
|
||||
"""Hypothesis-based latency robustness."""
|
||||
|
||||
@given(
|
||||
st.integers(min_value=1, max_value=20),
|
||||
st.integers(min_value=1000, max_value=3000),
|
||||
)
|
||||
@settings(max_examples=100, deadline=5000)
|
||||
def test_latency_keeps_metrics_valid(self, lag, n_bars):
|
||||
"""Property: delayed signal by any lag still produces valid metrics."""
|
||||
close, signal = _price_signal(n_bars, seed=42)
|
||||
delayed = signal.shift(lag).fillna(0)
|
||||
result = backtest_signal(close, delayed, txn_cost_bps=2.14)
|
||||
if result["status"] == "success":
|
||||
assert -1.0 <= result["max_drawdown"] <= 0.0
|
||||
assert 0.0 <= result["win_rate"] <= 1.0
|
||||
assert np.isfinite(result["sharpe"])
|
||||
|
||||
@given(
|
||||
st.integers(min_value=1, max_value=15),
|
||||
st.integers(min_value=1000, max_value=3000),
|
||||
)
|
||||
@settings(max_examples=80, deadline=5000)
|
||||
def test_latency_produces_valid_metrics(self, lag, n_bars):
|
||||
"""Property: delayed signal always produces valid bounded metrics."""
|
||||
close, signal = _price_signal(n_bars, seed=42)
|
||||
r_base = backtest_signal(close, signal, txn_cost_bps=0.0)
|
||||
delayed = signal.shift(lag).fillna(0)
|
||||
r_delayed = backtest_signal(close, delayed, txn_cost_bps=0.0)
|
||||
if r_base["status"] == "success" and r_delayed["status"] == "success":
|
||||
assert -1.0 <= r_delayed["max_drawdown"] <= 0.0
|
||||
assert 0.0 <= r_delayed["win_rate"] <= 1.0
|
||||
assert np.isfinite(r_delayed["sharpe"])
|
||||
|
||||
@given(
|
||||
st.integers(min_value=1, max_value=10),
|
||||
st.integers(min_value=1000, max_value=3000),
|
||||
)
|
||||
@settings(max_examples=80, deadline=5000)
|
||||
def test_latency_preserves_signal_counts(self, lag, n_bars):
|
||||
"""Property: signal_long + signal_short + signal_neutral == n_bars for delayed signal."""
|
||||
close, signal = _price_signal(n_bars, seed=42)
|
||||
delayed = signal.shift(lag).fillna(0)
|
||||
result = backtest_signal(close, delayed, txn_cost_bps=0.0)
|
||||
if result["status"] == "success":
|
||||
total = result["signal_long"] + result["signal_short"] + result["signal_neutral"]
|
||||
assert total == n_bars
|
||||
|
||||
@given(
|
||||
st.integers(min_value=1000, max_value=3000),
|
||||
)
|
||||
@settings(max_examples=50, deadline=5000)
|
||||
def test_latency_zero_same_as_base(self, n_bars):
|
||||
"""Property: 0-lag delayed signal = original signal result."""
|
||||
close, signal = _price_signal(n_bars, seed=42)
|
||||
r_orig = backtest_signal(close, signal, txn_cost_bps=0.0)
|
||||
delayed = signal.shift(0).fillna(0)
|
||||
r_delayed = backtest_signal(close, delayed, txn_cost_bps=0.0)
|
||||
if r_orig["status"] == "success" and r_delayed["status"] == "success":
|
||||
assert r_orig["total_return"] == r_delayed["total_return"]
|
||||
|
||||
@given(
|
||||
st.integers(min_value=5, max_value=30),
|
||||
st.integers(min_value=2000, max_value=3000),
|
||||
)
|
||||
@settings(max_examples=40, deadline=5000)
|
||||
def test_large_latency_does_not_crash(self, lag, n_bars):
|
||||
"""Property: very large lag does not crash the backtest."""
|
||||
close, signal = _price_signal(n_bars, seed=42)
|
||||
delayed = signal.shift(lag).fillna(0)
|
||||
result = backtest_signal(close, delayed, txn_cost_bps=2.14)
|
||||
assert result["status"] in ("success", "failed")
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Monte Carlo Fuzzing (12 tests)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestMonteCarloFuzzing:
|
||||
"""Hypothesis-based Monte Carlo robustness."""
|
||||
|
||||
@given(
|
||||
st.integers(min_value=500, max_value=2000),
|
||||
st.integers(min_value=10, max_value=50),
|
||||
)
|
||||
@settings(max_examples=50, deadline=5000)
|
||||
def test_reshuffle_keeps_metrics_valid(self, n_bars, n_perm):
|
||||
"""Property: all reshuffled runs produce valid metrics."""
|
||||
from rdagent.components.backtesting.vbt_backtest import backtest_signal
|
||||
close, signal = _price_signal(n_bars, seed=42)
|
||||
returns = close.pct_change().fillna(0)
|
||||
rng = np.random.default_rng(42)
|
||||
for _ in range(n_perm):
|
||||
shuffled = pd.Series(rng.permutation(returns.values), index=returns.index)
|
||||
price_s = (1 + shuffled).cumprod() * 1.10
|
||||
r = backtest_signal(price_s, signal, txn_cost_bps=0.0)
|
||||
if r["status"] == "success":
|
||||
assert -1.0 <= r["max_drawdown"] <= 0.0
|
||||
assert 0.0 <= r["win_rate"] <= 1.0
|
||||
|
||||
@given(
|
||||
st.integers(min_value=500, max_value=2000),
|
||||
)
|
||||
@settings(max_examples=50, deadline=5000)
|
||||
def test_reshuffle_win_rate_stable(self, n_bars):
|
||||
"""Property: win_rate after reshuffle is always in [0, 1]."""
|
||||
from rdagent.components.backtesting.vbt_backtest import backtest_signal
|
||||
close, signal = _price_signal(n_bars, seed=42)
|
||||
returns = close.pct_change().fillna(0)
|
||||
rng = np.random.default_rng(42)
|
||||
shuffled = pd.Series(rng.permutation(returns.values), index=returns.index)
|
||||
price_s = (1 + shuffled).cumprod() * 1.10
|
||||
r = backtest_signal(price_s, signal, txn_cost_bps=0.0)
|
||||
if r["status"] == "success":
|
||||
assert 0.0 <= r["win_rate"] <= 1.0
|
||||
|
||||
@given(
|
||||
st.integers(min_value=500, max_value=1500),
|
||||
)
|
||||
@settings(max_examples=50, deadline=5000)
|
||||
def test_reshuffle_sharpe_finite(self, n_bars):
|
||||
"""Property: Sharpe after reshuffle is finite."""
|
||||
from rdagent.components.backtesting.vbt_backtest import backtest_signal
|
||||
close, signal = _price_signal(n_bars, seed=42)
|
||||
returns = close.pct_change().fillna(0)
|
||||
rng = np.random.default_rng(42)
|
||||
shuffled = pd.Series(rng.permutation(returns.values), index=returns.index)
|
||||
price_s = (1 + shuffled).cumprod() * 1.10
|
||||
r = backtest_signal(price_s, signal, txn_cost_bps=0.0)
|
||||
if r["status"] == "success":
|
||||
assert np.isfinite(r["sharpe"])
|
||||
|
||||
@given(
|
||||
st.integers(min_value=500, max_value=1500),
|
||||
)
|
||||
@settings(max_examples=50, deadline=5000)
|
||||
def test_reshuffle_n_trades_unchanged(self, n_bars):
|
||||
"""Property: n_trades unchanged by reshuffling (same signal pattern)."""
|
||||
from rdagent.components.backtesting.vbt_backtest import backtest_signal
|
||||
close, signal = _price_signal(n_bars, seed=42)
|
||||
r_orig = backtest_signal(close, signal, txn_cost_bps=0.0)
|
||||
returns = close.pct_change().fillna(0)
|
||||
rng = np.random.default_rng(42)
|
||||
shuffled = pd.Series(rng.permutation(returns.values), index=returns.index)
|
||||
price_s = (1 + shuffled).cumprod() * 1.10
|
||||
r_shuf = backtest_signal(price_s, signal, txn_cost_bps=0.0)
|
||||
if r_orig["status"] == "success" and r_shuf["status"] == "success":
|
||||
assert r_orig["n_trades"] == r_shuf["n_trades"]
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Random Market Data Fuzzing (20 tests)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestRandomMarketDataFuzzing:
|
||||
"""Fuzz backtest_signal with completely random market data."""
|
||||
|
||||
@given(
|
||||
st.integers(min_value=100, max_value=5000),
|
||||
st.floats(min_value=-0.1, max_value=0.1),
|
||||
st.floats(min_value=0.00001, max_value=0.1),
|
||||
)
|
||||
@settings(max_examples=200, deadline=5000)
|
||||
def test_random_prices_always_succeed(self, n_bars, drift, vol):
|
||||
"""Property: backtesting with random geometric Brownian motion succeeds."""
|
||||
dates = pd.date_range("2024-01-01", periods=n_bars, freq="1min")
|
||||
rng = np.random.default_rng(42)
|
||||
close = pd.Series(1.10 * np.exp(np.cumsum(rng.normal(drift, vol, n_bars))), index=dates)
|
||||
signal = pd.Series(np.where(rng.normal(0, 1, n_bars) > 0, 1.0, -1.0), index=dates)
|
||||
result = backtest_signal(close, signal, txn_cost_bps=0.0)
|
||||
assert result["status"] in ("success", "failed")
|
||||
|
||||
@given(
|
||||
st.integers(min_value=100, max_value=3000),
|
||||
st.floats(min_value=-0.01, max_value=0.01),
|
||||
st.floats(min_value=0.0001, max_value=0.1),
|
||||
st.floats(min_value=0.0, max_value=30.0),
|
||||
)
|
||||
@settings(max_examples=200, deadline=5000)
|
||||
def test_random_data_all_metrics_finite(self, n_bars, drift, vol, cost):
|
||||
"""Property: all key metrics are finite for random data."""
|
||||
dates = pd.date_range("2024-01-01", periods=n_bars, freq="1min")
|
||||
rng = np.random.default_rng(42)
|
||||
close = pd.Series(1.10 * np.exp(np.cumsum(rng.normal(drift, vol, n_bars))), index=dates)
|
||||
signal = pd.Series(np.where(rng.normal(0, 1, n_bars) > 0, 1.0, -1.0), index=dates)
|
||||
result = backtest_signal(close, signal, txn_cost_bps=cost)
|
||||
if result["status"] == "success":
|
||||
for k in ["sharpe", "total_return", "max_drawdown"]:
|
||||
assert np.isfinite(result[k]), f"{k} is not finite: {result[k]}"
|
||||
|
||||
@given(
|
||||
st.integers(min_value=100, max_value=3000),
|
||||
st.floats(min_value=-0.01, max_value=0.01),
|
||||
)
|
||||
@settings(max_examples=200, deadline=5000)
|
||||
def test_random_data_maxdd_in_bounds(self, n_bars, drift):
|
||||
"""Property: max_drawdown ∈ [-1, 0] with random market data."""
|
||||
dates = pd.date_range("2024-01-01", periods=n_bars, freq="1min")
|
||||
rng = np.random.default_rng(42)
|
||||
close = pd.Series(1.10 * np.exp(np.cumsum(rng.normal(drift, 0.001, n_bars))), index=dates)
|
||||
signal = pd.Series(np.where(rng.normal(0, 1, n_bars) > 0, 1.0, -1.0), index=dates)
|
||||
result = backtest_signal(close, signal, txn_cost_bps=0.0)
|
||||
if result["status"] == "success":
|
||||
assert -1.0 <= result["max_drawdown"] <= 0.0
|
||||
|
||||
@given(
|
||||
st.integers(min_value=100, max_value=3000),
|
||||
st.floats(min_value=-0.01, max_value=0.01),
|
||||
)
|
||||
@settings(max_examples=200, deadline=5000)
|
||||
def test_random_data_win_rate_in_bounds(self, n_bars, drift):
|
||||
"""Property: win_rate ∈ [0, 1] with random market data."""
|
||||
dates = pd.date_range("2024-01-01", periods=n_bars, freq="1min")
|
||||
rng = np.random.default_rng(42)
|
||||
close = pd.Series(1.10 * np.exp(np.cumsum(rng.normal(drift, 0.001, n_bars))), index=dates)
|
||||
signal = pd.Series(np.where(rng.normal(0, 1, n_bars) > 0, 1.0, -1.0), index=dates)
|
||||
result = backtest_signal(close, signal, txn_cost_bps=0.0)
|
||||
if result["status"] == "success":
|
||||
assert 0.0 <= result["win_rate"] <= 1.0
|
||||
|
||||
@given(
|
||||
st.integers(min_value=100, max_value=3000),
|
||||
)
|
||||
@settings(max_examples=100, deadline=5000)
|
||||
def test_random_data_n_bars_matches_input(self, n_bars):
|
||||
"""Property: n_bars in result equals input length."""
|
||||
close, signal = _price_signal(n_bars, seed=42)
|
||||
result = backtest_signal(close, signal, txn_cost_bps=0.0)
|
||||
if result["status"] == "success":
|
||||
assert result["n_bars"] == n_bars
|
||||
|
||||
@given(
|
||||
st.integers(min_value=100, max_value=3000),
|
||||
)
|
||||
@settings(max_examples=100, deadline=5000)
|
||||
def test_random_data_signal_counts_sum_correctly(self, n_bars):
|
||||
"""Property: signal_long + signal_short + signal_neutral == n_bars."""
|
||||
close, signal = _price_signal(n_bars, seed=42)
|
||||
result = backtest_signal(close, signal, txn_cost_bps=0.0)
|
||||
if result["status"] == "success":
|
||||
assert result["signal_long"] + result["signal_short"] + result["signal_neutral"] == n_bars
|
||||
|
||||
@given(
|
||||
st.integers(min_value=100, max_value=3000),
|
||||
st.floats(min_value=1.0, max_value=500.0),
|
||||
)
|
||||
@settings(max_examples=100, deadline=5000)
|
||||
def test_random_data_txn_cost_bps_preserved(self, n_bars, cost):
|
||||
"""Property: txn_cost_bps reported matches input."""
|
||||
close, signal = _price_signal(n_bars, seed=42)
|
||||
result = backtest_signal(close, signal, txn_cost_bps=cost)
|
||||
if result["status"] == "success":
|
||||
assert abs(result["txn_cost_bps"] - cost) < 0.001
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# OOS Stress Fuzzing (10 tests)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestOOSStressFuzzing:
|
||||
"""Hypothesis-based out-of-sample stress tests."""
|
||||
|
||||
@given(
|
||||
st.integers(min_value=1000, max_value=5000),
|
||||
st.floats(min_value=0.3, max_value=0.8),
|
||||
)
|
||||
@settings(max_examples=100, deadline=5000)
|
||||
def test_oos_metrics_valid(self, n_bars, split_fraction):
|
||||
"""Property: OOS metrics remain valid for any split."""
|
||||
from rdagent.components.backtesting.vbt_backtest import backtest_signal
|
||||
dates = pd.date_range("2024-01-01", periods=n_bars, freq="1min")
|
||||
rng = np.random.default_rng(42)
|
||||
close = pd.Series(1.10 * np.exp(np.cumsum(rng.normal(0, 0.0002, n_bars))), index=dates)
|
||||
signal = pd.Series(np.where(rng.normal(0, 1, n_bars) > 0, 1.0, -1.0), index=dates)
|
||||
split = int(n_bars * split_fraction)
|
||||
assume(split > 100)
|
||||
assume(n_bars - split > 100)
|
||||
r_oos = backtest_signal(close.iloc[split:], signal.iloc[split:], txn_cost_bps=0.0)
|
||||
if r_oos["status"] == "success":
|
||||
assert -1.0 <= r_oos["max_drawdown"] <= 0.0
|
||||
assert np.isfinite(r_oos["sharpe"])
|
||||
|
||||
@given(
|
||||
st.integers(min_value=500, max_value=3000),
|
||||
)
|
||||
@settings(max_examples=80, deadline=5000)
|
||||
def test_oos_sharpe_finite(self, n_bars):
|
||||
"""Property: OOS Sharpe is always finite."""
|
||||
from rdagent.components.backtesting.vbt_backtest import backtest_signal
|
||||
close, signal = _price_signal(n_bars, seed=42)
|
||||
split = n_bars // 2
|
||||
assume(n_bars - split > 100)
|
||||
r_oos = backtest_signal(close.iloc[split:], signal.iloc[split:], txn_cost_bps=0.0)
|
||||
if r_oos["status"] == "success":
|
||||
assert np.isfinite(r_oos["sharpe"])
|
||||
|
||||
@given(
|
||||
st.integers(min_value=1000, max_value=3000),
|
||||
)
|
||||
@settings(max_examples=80, deadline=5000)
|
||||
def test_is_and_oos_both_produce_metrics(self, n_bars):
|
||||
"""Property: both IS and OOS periods produce valid metrics."""
|
||||
from rdagent.components.backtesting.vbt_backtest import backtest_signal
|
||||
close, signal = _price_signal(n_bars, seed=42)
|
||||
split = int(n_bars * 0.7)
|
||||
assume(split > 100)
|
||||
assume(n_bars - split > 100)
|
||||
r_is = backtest_signal(close.iloc[:split], signal.iloc[:split], txn_cost_bps=0.0)
|
||||
r_oos = backtest_signal(close.iloc[split:], signal.iloc[split:], txn_cost_bps=0.0)
|
||||
if r_is["status"] == "success":
|
||||
assert np.isfinite(r_is["sharpe"])
|
||||
if r_oos["status"] == "success":
|
||||
assert np.isfinite(r_oos["max_drawdown"])
|
||||
|
||||
@given(
|
||||
st.integers(min_value=500, max_value=2000),
|
||||
)
|
||||
@settings(max_examples=50, deadline=5000)
|
||||
def test_oos_win_rate_in_bounds(self, n_bars):
|
||||
"""Property: OOS win_rate ∈ [0, 1]."""
|
||||
from rdagent.components.backtesting.vbt_backtest import backtest_signal
|
||||
close, signal = _price_signal(n_bars, seed=42)
|
||||
split = n_bars // 2
|
||||
assume(n_bars - split > 100)
|
||||
r_oos = backtest_signal(close.iloc[split:], signal.iloc[split:], txn_cost_bps=0.0)
|
||||
if r_oos["status"] == "success":
|
||||
assert 0.0 <= r_oos["win_rate"] <= 1.0
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Forward Returns Backtest Fuzzing (10 tests)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestForwardReturnsFuzzing:
|
||||
"""Fuzz backtest_from_forward_returns with random factor and forward returns."""
|
||||
|
||||
@given(
|
||||
st.integers(min_value=30, max_value=500),
|
||||
st.lists(st.floats(min_value=-10, max_value=10), min_size=30, max_size=500),
|
||||
st.lists(st.floats(min_value=-0.5, max_value=0.5), min_size=30, max_size=500),
|
||||
st.floats(min_value=0.0, max_value=50.0),
|
||||
)
|
||||
@settings(max_examples=100, deadline=5000)
|
||||
def test_forward_backtest_returns_all_keys(self, n, fac_raw, ret_raw, cost):
|
||||
"""Property: backtest_from_forward_returns contains all expected keys."""
|
||||
n = min(len(fac_raw), len(ret_raw))
|
||||
factor = pd.Series(fac_raw[:n], dtype=float)
|
||||
fwd = pd.Series(ret_raw[:n], dtype=float)
|
||||
assume(factor.std() > 1e-12)
|
||||
result = backtest_from_forward_returns(factor, fwd, txn_cost_bps=cost)
|
||||
for k in ["status", "sharpe", "max_drawdown", "total_return", "win_rate",
|
||||
"n_trades", "ic", "n_bars"]:
|
||||
assert k in result, f"Missing key: {k}"
|
||||
|
||||
@given(
|
||||
st.integers(min_value=30, max_value=500),
|
||||
st.lists(st.floats(min_value=-10, max_value=10), min_size=30, max_size=500),
|
||||
st.lists(st.floats(min_value=-0.5, max_value=0.5), min_size=30, max_size=500),
|
||||
)
|
||||
@settings(max_examples=100, deadline=5000)
|
||||
def test_forward_backtest_maxdd_in_bounds(self, n, fac_raw, ret_raw):
|
||||
"""Property: max_drawdown ∈ [-1, 0] from forward returns backtest."""
|
||||
n = min(len(fac_raw), len(ret_raw))
|
||||
factor = pd.Series(fac_raw[:n], dtype=float)
|
||||
fwd = pd.Series(ret_raw[:n], dtype=float)
|
||||
assume(factor.std() > 1e-12)
|
||||
result = backtest_from_forward_returns(factor, fwd, txn_cost_bps=0.0)
|
||||
if result["status"] == "success":
|
||||
assert -1.0 <= result["max_drawdown"] <= 0.0
|
||||
|
||||
@given(
|
||||
st.integers(min_value=30, max_value=500),
|
||||
st.lists(st.floats(min_value=-10, max_value=10), min_size=30, max_size=500),
|
||||
st.lists(st.floats(min_value=-0.5, max_value=0.5), min_size=30, max_size=500),
|
||||
)
|
||||
@settings(max_examples=100, deadline=5000)
|
||||
def test_forward_backtest_ic_in_bounds(self, n, fac_raw, ret_raw):
|
||||
"""Property: IC ∈ [-1, 1] from forward returns backtest."""
|
||||
n = min(len(fac_raw), len(ret_raw))
|
||||
factor = pd.Series(fac_raw[:n], dtype=float)
|
||||
fwd = pd.Series(ret_raw[:n], dtype=float)
|
||||
assume(factor.std() > 1e-12)
|
||||
result = backtest_from_forward_returns(factor, fwd, txn_cost_bps=0.0)
|
||||
if result["status"] == "success":
|
||||
assert -1.0 <= result["ic"] <= 1.0, f"IC={result['ic']}"
|
||||
|
||||
@given(
|
||||
st.integers(min_value=30, max_value=500),
|
||||
st.lists(st.floats(min_value=-10, max_value=10), min_size=30, max_size=500),
|
||||
st.lists(st.floats(min_value=-0.5, max_value=0.5), min_size=30, max_size=500),
|
||||
)
|
||||
@settings(max_examples=100, deadline=5000)
|
||||
def test_forward_backtest_win_rate_in_bounds(self, n, fac_raw, ret_raw):
|
||||
"""Property: win_rate ∈ [0, 1] from forward returns backtest."""
|
||||
n = min(len(fac_raw), len(ret_raw))
|
||||
factor = pd.Series(fac_raw[:n], dtype=float)
|
||||
fwd = pd.Series(ret_raw[:n], dtype=float)
|
||||
assume(factor.std() > 1e-12)
|
||||
result = backtest_from_forward_returns(factor, fwd, txn_cost_bps=0.0)
|
||||
if result["status"] == "success":
|
||||
assert 0.0 <= result["win_rate"] <= 1.0, f"WinRate={result['win_rate']}"
|
||||
|
||||
@given(
|
||||
st.integers(min_value=1, max_value=9),
|
||||
)
|
||||
@settings(max_examples=20, deadline=5000)
|
||||
def test_forward_backtest_too_few_bars_fails(self, n):
|
||||
"""Property: < 10 aligned bars fails."""
|
||||
factor = pd.Series(np.arange(n, dtype=float))
|
||||
fwd = pd.Series(np.arange(n, dtype=float))
|
||||
result = backtest_from_forward_returns(factor, fwd)
|
||||
assert result["status"] == "failed"
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Edge Cases and Extreme Values Fuzzing (10 tests)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestEdgeCasesFuzzing:
|
||||
"""Fuzzing with extreme/nonsense inputs."""
|
||||
|
||||
@given(
|
||||
st.integers(min_value=100, max_value=2000),
|
||||
)
|
||||
@settings(max_examples=70, deadline=5000)
|
||||
def test_zero_price_initial_does_not_crash(self, n_bars):
|
||||
"""Property: backtest handles near-zero initial prices."""
|
||||
dates = pd.date_range("2024-01-01", periods=n_bars, freq="1min")
|
||||
rng = np.random.default_rng(42)
|
||||
close = pd.Series(0.000001 + abs(rng.normal(0, 0.0002, n_bars)).cumsum(), index=dates)
|
||||
signal = pd.Series(np.where(rng.normal(0, 1, n_bars) > 0, 1.0, -1.0), index=dates)
|
||||
result = backtest_signal(close, signal)
|
||||
assert result["status"] in ("success", "failed")
|
||||
|
||||
@given(
|
||||
st.integers(min_value=100, max_value=2000),
|
||||
)
|
||||
@settings(max_examples=70, deadline=5000)
|
||||
def test_very_large_price_does_not_crash(self, n_bars):
|
||||
"""Property: backtest handles very large prices."""
|
||||
dates = pd.date_range("2024-01-01", periods=n_bars, freq="1min")
|
||||
rng = np.random.default_rng(42)
|
||||
close = pd.Series(1e6 + rng.normal(0, 1, n_bars).cumsum(), index=dates)
|
||||
signal = pd.Series(np.where(rng.normal(0, 1, n_bars) > 0, 1.0, -1.0), index=dates)
|
||||
result = backtest_signal(close, signal)
|
||||
assert result["status"] in ("success", "failed")
|
||||
|
||||
@given(
|
||||
st.integers(min_value=100, max_value=2000),
|
||||
)
|
||||
@settings(max_examples=70, deadline=5000)
|
||||
def test_signal_all_nan_treated_as_flat(self, n_bars):
|
||||
"""Property: signal full of NaN is treated as flat (win_rate=0, n_trades=0)."""
|
||||
dates = pd.date_range("2024-01-01", periods=n_bars, freq="1min")
|
||||
rng = np.random.default_rng(42)
|
||||
close = pd.Series(1.10 + rng.normal(0, 0.0002, n_bars).cumsum(), index=dates)
|
||||
signal = pd.Series([np.nan] * n_bars, index=dates)
|
||||
result = backtest_signal(close, signal)
|
||||
if result["status"] == "success":
|
||||
assert result["n_trades"] == 0
|
||||
assert result["win_rate"] == 0.0
|
||||
|
||||
@given(
|
||||
st.integers(min_value=1000, max_value=3000),
|
||||
)
|
||||
@settings(max_examples=70, deadline=5000)
|
||||
def test_continuous_signal_produces_valid_metrics(self, n_bars):
|
||||
"""Property: continuous signal in [-1, 1] produces valid metrics."""
|
||||
dates = pd.date_range("2024-01-01", periods=n_bars, freq="1min")
|
||||
rng = np.random.default_rng(42)
|
||||
close = pd.Series(1.10 * np.exp(np.cumsum(rng.normal(0, 0.0002, n_bars))), index=dates)
|
||||
signal = pd.Series(rng.uniform(-1, 1, n_bars), index=dates)
|
||||
result = backtest_signal(close, signal, txn_cost_bps=0.0)
|
||||
if result["status"] == "success":
|
||||
assert -1.0 <= result["max_drawdown"] <= 0.0
|
||||
assert 0.0 <= result["win_rate"] <= 1.0
|
||||
|
||||
@given(
|
||||
st.integers(min_value=500, max_value=2000),
|
||||
)
|
||||
@settings(max_examples=70, deadline=5000)
|
||||
def test_weekend_gaps_produce_valid_metrics(self, n_bars):
|
||||
"""Property: data with time gaps (weekends) produces valid metrics."""
|
||||
dates = pd.bdate_range("2024-01-01", periods=n_bars, freq="1min")
|
||||
rng = np.random.default_rng(42)
|
||||
close = pd.Series(1.10 + rng.normal(0, 0.0002, len(dates)).cumsum(), index=dates)
|
||||
signal = pd.Series(np.where(rng.normal(0, 1, len(dates)) > 0, 1.0, -1.0), index=dates)
|
||||
result = backtest_signal(close, signal)
|
||||
if result["status"] == "success":
|
||||
assert np.isfinite(result["sharpe"])
|
||||
assert -1.0 <= result["max_drawdown"] <= 0.0
|
||||
|
||||
Reference in New Issue
Block a user