diff --git a/tests/test_factor_attribution.py b/tests/test_factor_attribution.py index 3437a76..7094a83 100644 --- a/tests/test_factor_attribution.py +++ b/tests/test_factor_attribution.py @@ -337,26 +337,60 @@ class LocalFactorConstructionTests(unittest.TestCase): 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 + mutated.loc[future_start:, "000300.SS"] = mutated.loc[future_start:, "000300.SS"] * 1.4 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] 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"], + check_names=False, + ) self.assertGreater( - (original.loc[future_start:] - changed.loc[future_start:]).abs().sum().sum(), + (original.loc[future_start:, "MKT"] - changed.loc[future_start:, "MKT"]).abs().sum(), 0.0, ) def test_build_proxy_core_factors_falls_back_to_equal_weight_market_when_benchmark_missing(self): - prices = self._make_price_frame(benchmark="CN_BENCH").drop(columns=["CN_BENCH"]) + prices_with_benchmark = self._make_price_frame(benchmark="CN_BENCH") + prices = prices_with_benchmark.drop(columns=["CN_BENCH"]) factors = build_proxy_core_factors(prices, benchmark="000300.SS", market="cn") + reference = build_proxy_core_factors(prices_with_benchmark, benchmark="CN_BENCH", market="cn") expected_market = prices.pct_change().mean(axis=1) pd.testing.assert_series_equal(factors["MKT"], expected_market, check_names=False) + self.assertListEqual( + list(factors.columns), + ["MKT", "SMB_PROXY", "HML_PROXY", "RMW_PROXY", "CMA_PROXY"], + ) + self.assertTrue(factors.iloc[260:][["SMB_PROXY", "HML_PROXY", "RMW_PROXY", "CMA_PROXY"]].notna().all().all()) + self.assertGreater( + factors.iloc[260:][["SMB_PROXY", "HML_PROXY", "RMW_PROXY", "CMA_PROXY"]].abs().sum().sum(), + 0.0, + ) + pd.testing.assert_frame_equal( + factors[["SMB_PROXY", "HML_PROXY", "RMW_PROXY", "CMA_PROXY"]], + reference[["SMB_PROXY", "HML_PROXY", "RMW_PROXY", "CMA_PROXY"]], + ) def _make_price_frame(self, benchmark: str) -> pd.DataFrame: dates = pd.date_range("2025-01-01", periods=320, freq="B")