From 26937f035e5f9d735d7b8201aa67983e83858810 Mon Sep 17 00:00:00 2001 From: Gahow Wang Date: Tue, 7 Apr 2026 16:31:51 +0800 Subject: [PATCH] Split proxy leakage tests --- tests/test_factor_attribution.py | 43 ++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/tests/test_factor_attribution.py b/tests/test_factor_attribution.py index 7094a83..818b30b 100644 --- a/tests/test_factor_attribution.py +++ b/tests/test_factor_attribution.py @@ -333,7 +333,7 @@ class LocalFactorConstructionTests(unittest.TestCase): 0.0, ) - def test_build_proxy_core_factors_does_not_use_future_prices(self): + def test_build_proxy_core_factors_market_branch_does_not_use_future_benchmark_prices(self): prices = self._make_price_frame(benchmark="000300.SS") mutated = prices.copy() future_start = prices.index[280] @@ -343,25 +343,9 @@ class LocalFactorConstructionTests(unittest.TestCase): changed = build_proxy_core_factors(mutated, benchmark="000300.SS", market="cn") comparison_end = prices.index[279] - pd.testing.assert_frame_equal(original.loc[:comparison_end], changed.loc[:comparison_end]) pd.testing.assert_series_equal( - original["SMB_PROXY"], - changed["SMB_PROXY"], - check_names=False, - ) - pd.testing.assert_series_equal( - original["HML_PROXY"], - changed["HML_PROXY"], - check_names=False, - ) - pd.testing.assert_series_equal( - original["RMW_PROXY"], - changed["RMW_PROXY"], - check_names=False, - ) - pd.testing.assert_series_equal( - original["CMA_PROXY"], - changed["CMA_PROXY"], + original.loc[:comparison_end, "MKT"], + changed.loc[:comparison_end, "MKT"], check_names=False, ) self.assertGreater( @@ -369,6 +353,27 @@ class LocalFactorConstructionTests(unittest.TestCase): 0.0, ) + def test_build_proxy_core_factors_proxy_columns_do_not_use_future_stock_prices(self): + prices = self._make_price_frame(benchmark="000300.SS") + mutated = prices.copy() + future_start = prices.index[280] + mutated.loc[future_start:, "C"] = mutated.loc[future_start:, "C"] * 0.35 + mutated.loc[future_start:, "D"] = mutated.loc[future_start:, "D"] * 1.6 + + original = build_proxy_core_factors(prices, benchmark="000300.SS", market="cn") + changed = build_proxy_core_factors(mutated, benchmark="000300.SS", market="cn") + + comparison_end = prices.index[279] + proxy_columns = ["SMB_PROXY", "HML_PROXY", "RMW_PROXY", "CMA_PROXY"] + pd.testing.assert_frame_equal( + original.loc[:comparison_end, proxy_columns], + changed.loc[:comparison_end, proxy_columns], + ) + self.assertGreater( + (original.loc[future_start:, proxy_columns] - changed.loc[future_start:, proxy_columns]).abs().sum().sum(), + 0.0, + ) + def test_build_proxy_core_factors_falls_back_to_equal_weight_market_when_benchmark_missing(self): prices_with_benchmark = self._make_price_frame(benchmark="CN_BENCH") prices = prices_with_benchmark.drop(columns=["CN_BENCH"])