Spaces:
Configuration error
Configuration error
Niranjan-Murugarasu024
feat: TradeSense AI Chairman Edition production release ready for Hugging Face Spaces
0125724 Download backend/tests/quant/test_falsification_engine.py from BlueMen/tradesenseAi: direct link, hf CLI and curl.
- Browser
- Download file 4.1 kB
-
https://huggingface.co/spaces/BlueMen/tradesenseAi/resolve/main/backend/tests/quant/test_falsification_engine.py
- Command line
-
hf download hf://spaces/BlueMen/tradesenseAi/backend/tests/quant/test_falsification_engine.py
-
curl -L -o test_falsification_engine.py https://huggingface.co/spaces/BlueMen/tradesenseAi/resolve/main/backend/tests/quant/test_falsification_engine.py
4.1 kB
| """ | |
| TradeSense AI — Quantitative Falsification Unit Tests | |
| Validates: | |
| - Leave-One-Asset-Out (LOAO) Jackknife stability matrix (-TSLA, -NVDA, etc.) | |
| - Monte Carlo Random-Ranking Placebo test | |
| - Competing rule-based momentum baseline comparisons | |
| - Ex-ante cross-sectional dispersion conditioning | |
| """ | |
| from datetime import date, timedelta | |
| import numpy as np | |
| import pandas as pd | |
| import pytest | |
| from quant.research.falsification_engine import ( | |
| FalsificationEngine, | |
| LOAOAssetResult, | |
| PlaceboTestResult, | |
| BaselineComparisonResult, | |
| DispersionRegimeResult, | |
| ) | |
| def generate_synthetic_universe(tickers: list, n_rows: int = 400) -> dict: | |
| np.random.seed(42) | |
| start_date = date(2020, 1, 1) | |
| dates = [start_date + timedelta(days=i) for i in range(n_rows)] | |
| universe = {} | |
| for idx, tk in enumerate(tickers): | |
| drift = 0.0003 + (idx * 0.0002) | |
| vol = 0.01 + (idx * 0.003) | |
| rets = np.random.normal(drift, vol, n_rows) | |
| close = 100.0 * np.cumprod(1.0 + rets) | |
| high = close * 1.01 | |
| low = close * 0.99 | |
| open_p = (high + low) / 2.0 | |
| vol_shares = np.random.randint(1000000, 5000000, n_rows) | |
| universe[tk] = pd.DataFrame({ | |
| "session": dates, | |
| "Open": open_p, | |
| "High": high, | |
| "Low": low, | |
| "Close": close, | |
| "Volume": vol_shares, | |
| }) | |
| return universe | |
| class TestFalsificationEngine: | |
| def test_evaluate_loao_jackknife_runs_and_computes_retention(self): | |
| tickers = ["AAPL", "MSFT", "NVDA", "TSLA", "AMZN", "QQQ", "SPY"] | |
| universe = generate_synthetic_universe(tickers, n_rows=400) | |
| full_sr, loao_res, min_sr, med_sr, stab_ratio = FalsificationEngine.evaluate_loao_jackknife( | |
| universe_dfs=universe, | |
| horizon_sessions=5, | |
| lookback_sessions=60, | |
| top_k=2, | |
| bottom_k=2, | |
| ) | |
| assert len(loao_res) == 6 # 6 alpha tickers tested | |
| assert np.isfinite(full_sr) | |
| assert np.isfinite(min_sr) | |
| assert np.isfinite(stab_ratio) | |
| for r in loao_res: | |
| assert r.omitted_asset in {"AAPL", "MSFT", "NVDA", "TSLA", "AMZN", "QQQ"} | |
| assert np.isfinite(r.spread_net_sharpe) | |
| def test_run_random_ranking_placebo_computes_empirical_p_value(self): | |
| np.random.seed(42) | |
| T, N = 200, 6 | |
| daily_returns = np.random.normal(0.0005, 0.01, (T, N)) | |
| observed_sr = 2.5 | |
| res: PlaceboTestResult = FalsificationEngine.run_random_ranking_placebo( | |
| daily_realized_returns_matrix=daily_returns, | |
| observed_sharpe=observed_sr, | |
| horizon_sessions=5, | |
| n_permutations=50, | |
| random_seed=42, | |
| ) | |
| assert res.n_permutations == 50 | |
| assert 0.0 <= res.empirical_p_value <= 1.0 | |
| assert np.isfinite(res.placebo_mean_sharpe) | |
| def test_evaluate_rule_based_momentum_baselines(self): | |
| tickers = ["AAPL", "MSFT", "NVDA", "TSLA", "AMZN", "QQQ", "SPY"] | |
| universe = generate_synthetic_universe(tickers, n_rows=400) | |
| baselines = FalsificationEngine.evaluate_rule_based_momentum_baselines( | |
| universe_dfs=universe, | |
| conformal_sharpe=2.0, | |
| horizon_sessions=5, | |
| ) | |
| assert len(baselines) == 4 | |
| names = {b.strategy_name for b in baselines} | |
| assert "Naive_21D_Return_Ranking" in names | |
| assert "Short_Term_5D_Momentum" in names | |
| assert "Medium_Term_63D_Momentum" in names | |
| assert "Volatility_Adjusted_21D_Momentum" in names | |
| def test_evaluate_dispersion_conditioning(self): | |
| tickers = ["AAPL", "MSFT", "NVDA", "TSLA", "AMZN", "QQQ", "SPY"] | |
| universe = generate_synthetic_universe(tickers, n_rows=400) | |
| regimes = FalsificationEngine.evaluate_dispersion_conditioning( | |
| universe_dfs=universe, | |
| horizon_sessions=5, | |
| lookback_sessions=60, | |
| ) | |
| assert len(regimes) == 2 | |
| for reg in regimes: | |
| assert reg.n_sessions > 0 | |
| assert np.isfinite(reg.spread_net_sharpe) | |