Skip to content

"Vibe-Trading: Your Personal Trading Agent"

feat(quantlib): report unmatched_weight in FactorRiskDecomposition

santhreal3770d29cd669
diff --git a/agent/src/quantlib/factormodel.py b/agent/src/quantlib/factormodel.py
index 293f38030..4ad3702dc 100644
--- a/agent/src/quantlib/factormodel.py
+++ b/agent/src/quantlib/factormodel.py
@@ -183,6 +183,7 @@ class FactorRiskDecomposition:
asset_pcr: Percentage contribution to risk (PCR) per asset (sums to 1.0 when total_volatility > 0, otherwise zero).
specific_risk_contributions: Specific risk contribution per asset.
specific_pcr: Percentage contribution from specific risk per asset.
+ unmatched_weight: Portfolio weight in assets lacking factor exposure data.
"""
total_variance: float
@@ -202,6 +203,7 @@ class FactorRiskDecomposition:
asset_pcr: pd.Series
specific_risk_contributions: pd.Series
specific_pcr: pd.Series
+ unmatched_weight: float = 0.0
def standardise_exposures(
values: pd.Series,
@@ -618,6 +620,7 @@ def factor_risk_decomposition(
f"No matching assets between weights ({sorted(w_series.index)}) and exposures ({sorted(exposures.index)})"
)
+ unmatched_weight = float(w_series.drop(index=assets, errors="ignore").sum())
w = w_series.loc[assets]
X = exposures.loc[assets]
@@ -707,4 +710,5 @@ def factor_risk_decomposition(
asset_pcr=asset_pcr,
specific_risk_contributions=spec_rc,
specific_pcr=spec_pcr,
+ unmatched_weight=unmatched_weight,
)
diff --git a/agent/tests/quantlib/test_factormodel.py b/agent/tests/quantlib/test_factormodel.py
index 74124c745..484adc1f4 100644
--- a/agent/tests/quantlib/test_factormodel.py
+++ b/agent/tests/quantlib/test_factormodel.py
@@ -562,3 +562,13 @@ def test_factor_risk_decomposition_input_validation():
pd.DataFrame([[1.0], [1.0]], index=["A", "B"], columns=["F1"]),
pd.DataFrame([[-0.04]], index=["F1"], columns=["F1"]),
)
+
+
+def test_factor_risk_decomposition_reports_unmatched_weight():
+ weights = pd.Series([0.6, 0.4], index=["A", "UNCOVERED"])
+ exposures = pd.DataFrame([[1.0]], index=["A"], columns=["market"])
+ factor_cov = pd.DataFrame([[0.04]], index=["market"], columns=["market"])
+
+ decomp = factor_risk_decomposition(weights, exposures, factor_cov)
+ assert decomp.unmatched_weight == pytest.approx(0.4)
+ assert decomp.total_volatility == pytest.approx(0.6 * 0.20)