Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(quantlib): merge int and float tenor keys in spread term structure

Lanre Shittufec0f7474d09
diff --git a/agent/src/quantlib/credit.py b/agent/src/quantlib/credit.py
index 8ebe27fc0..fc0a52ffb 100644
--- a/agent/src/quantlib/credit.py
+++ b/agent/src/quantlib/credit.py
@@ -695,13 +695,21 @@ def spread_term_structure(
for tenor, yld in tenor_curve.items():
reference = curve.get(tenor, np.nan)
spread = (yld - reference) * multiplier
- row[f"{tenor}Y_spread_bp"] = (
+ row[f"{_format_tenor(tenor)}Y_spread_bp"] = (
spread if decimals is None else round(spread, decimals)
)
records.append(pd.Series(row, name=issuer))
return pd.DataFrame(records)
+def _format_tenor(tenor: float) -> str:
+ """Canonical column-name spelling for a tenor, so ``5`` and ``5.0`` from
+ different issuers land in the same column instead of silently splitting
+ the grid into duplicate NaN-filled columns."""
+ value = float(tenor)
+ return str(int(value)) if value.is_integer() else str(value)
+
+
def hazard_rate_to_survival_probability(hazard_rate: float, tenor_years: float) -> float:
"""Convert a constant hazard rate lambda to cumulative survival probability Q(T) = exp(-lambda * T).
diff --git a/agent/tests/quantlib/test_credit.py b/agent/tests/quantlib/test_credit.py
index d86de08ad..a62725269 100644
--- a/agent/tests/quantlib/test_credit.py
+++ b/agent/tests/quantlib/test_credit.py
@@ -497,6 +497,21 @@ def test_spread_term_structure_rounding_is_controllable():
] == pytest.approx(50.123)
+def test_spread_term_structure_merges_int_and_float_tenor_keys():
+ issuers = {
+ "IssuerA": {5: 0.045, 10: 0.052},
+ "IssuerB": {5.0: 0.048, 10.0: 0.055},
+ }
+ curve = {5: 0.03, 10: 0.035}
+ out = spread_term_structure(issuers, curve)
+
+ assert list(out.columns) == ["5Y_spread_bp", "10Y_spread_bp"]
+ assert out.loc["IssuerA", "5Y_spread_bp"] == pytest.approx(150.0)
+ assert out.loc["IssuerB", "5Y_spread_bp"] == pytest.approx(180.0)
+ assert out.loc["IssuerA", "10Y_spread_bp"] == pytest.approx(170.0)
+ assert out.loc["IssuerB", "10Y_spread_bp"] == pytest.approx(200.0)
+
+
def test_spread_term_structure_validates_its_inputs():
with pytest.raises(ValueError, match="at least one entry"):
spread_term_structure({}, {1: 0.02})