Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix: Count incomplete factor exposure rows as unmatched weight rather than measuring missing factors as zero.

Rudy Celeklia4e53143129b
diff --git a/README.md b/README.md
index b359b3ac8..1eef093f0 100644
--- a/README.md
+++ b/README.md
@@ -853,7 +853,7 @@ that is a bug, not a pattern.
| `risk` · `var_backtest` | VaR / CVaR / EVT and their backtests |
| `attribution` | Brinson-Fachler decomposition |
| `performance` · `fundmath` | TWR / MWR / Modified Dietz; XIRR / MOIC / DPI / TVPI |
-| `factormodel` · `eventstudy` | Factor regressions, event studies |
+| `factormodel` · `eventstudy` | Factor regressions, event studies; portfolio style exposure reports incomplete factor rows as unmatched weight. |
| `multipletesting` · `crossvalidation` | Deflated significance, purged CV |
| `impact` | Market-impact models |
| `volatility` | Heston (1993) stochastic-volatility pricing |
diff --git a/agent/src/quantlib/factormodel.py b/agent/src/quantlib/factormodel.py
index b610bd135..675c9d368 100644
--- a/agent/src/quantlib/factormodel.py
+++ b/agent/src/quantlib/factormodel.py
@@ -507,6 +507,10 @@ def portfolio_style_exposure(
if exposures.shape[1] == 0:
raise ValueError("exposures has no factor columns")
+ # A row missing any factor cannot describe this asset's style. Exclude
+ # it as a whole and report its absolute weight rather than summing NaNs
+ # into an apparently measured zero exposure.
+ exposures = exposures.loc[np.isfinite(exposures.to_numpy(dtype=float)).all(axis=1)]
matched = weights.index.intersection(exposures.index)
unmatched_weight = float(weights.drop(matched).abs().sum())
result = exposures.loc[matched].mul(weights.loc[matched], axis=0).sum()
diff --git a/agent/tests/test_style_missing_exposures_regressions.py b/agent/tests/test_style_missing_exposures_regressions.py
new file mode 100644
index 000000000..90dbc7d03
--- /dev/null
+++ b/agent/tests/test_style_missing_exposures_regressions.py
@@ -0,0 +1,51 @@
+import numpy as np
+import pandas as pd
+import pytest
+from src.quantlib.factormodel import portfolio_style_exposure
+
+
[email protected]("missing", [np.nan, np.inf, -np.inf])
+def test_incomplete_exposure_rows_are_reported_as_unmatched(missing):
+ exposures = pd.DataFrame(
+ {"value": [2.0, missing], "size": [3.0, 4.0]}, index=["a", "b"]
+ )
+ actual = portfolio_style_exposure(pd.Series({"a": 0.6, "b": 0.4}), exposures)
+ assert actual["value"] == pytest.approx(1.2)
+ assert actual["size"] == pytest.approx(1.8)
+ assert actual["unmatched_weight"] == pytest.approx(0.4)
+
+
+def test_benchmark_missing_exposures_are_also_reported():
+ exposures = pd.DataFrame({"value": [2.0, np.nan]}, index=["a", "b"])
+ actual = portfolio_style_exposure(
+ pd.Series({"a": 1.0}), exposures, benchmark=pd.Series({"b": 1.0})
+ )
+ assert actual["value"] == 2.0
+ assert actual["unmatched_weight"] == 1.0
+
+
+def test_quantlib_tool_reports_unmeasured_style_weight():
+ import json
+ from src.tools.quantlib_tool import QuantlibCallTool
+
+ result = json.loads(
+ QuantlibCallTool().execute(
+ action="call",
+ module="factormodel",
+ function="portfolio_style_exposure",
+ kwargs={
+ "holdings": {"a": 1.0},
+ "exposures": {
+ "__dataframe__": {
+ "index": ["a"],
+ "columns": ["value"],
+ "data": [[None]],
+ }
+ },
+ },
+ )
+ )
+ assert result["ok"] is True
+ encoded = result["result"]["__series__"]
+ values = dict(zip(encoded["index"], encoded["values"]))
+ assert values["unmatched_weight"] == 1.0