"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix: Keep CDS premium dates on the requested frequency and price a final stub for fractional maturities.
Rudy Celekli939e96448815
diff --git a/README.md b/README.mdindex b359b3ac8..ab1c643bf 100644--- a/README.md+++ b/README.md@@ -848,7 +848,7 @@ that is a bug, not a pattern.|--------|----------------|| `options` | Black-Scholes price + greeks, implied-volatility inversion || `fixedincome` | Bond math, Nelson-Siegel / Svensson curve fitting |-| `credit` | Altman Z-score, Merton / KMV distance-to-default |+| `credit` | Altman Z-score, Merton / KMV distance-to-default; CDS premium dates retain their configured frequency, with a final stub when maturity falls between coupons. || `timeseries` | Stationarity, cointegration, GARCH, bootstrap || `risk` · `var_backtest` | VaR / CVaR / EVT and their backtests || `attribution` | Brinson-Fachler decomposition |diff --git a/agent/src/quantlib/credit.py b/agent/src/quantlib/credit.pyindex fc0a52ffb..04fce97f8 100644--- a/agent/src/quantlib/credit.py+++ b/agent/src/quantlib/credit.py@@ -824,8 +824,12 @@ def cds_price(# Implied hazard rate lambda ≈ s / LGDlambda_hazard = float(s_dec / lgd) if lgd > 0 else 0.0- n_periods = max(1, int(round(tenor_years * payment_frequency)))- t_grid = np.linspace(tenor_years / n_periods, tenor_years, n_periods)+ n_periods = max(1, int(np.ceil(tenor_years * payment_frequency)))+ # Keep regular premium dates fixed at the requested frequency; only the+ # last period is a stub when maturity falls between coupon dates.+ t_grid = np.r_[+ np.arange(1, n_periods, dtype=float) / payment_frequency, tenor_years+ ]t_prev = np.r_[0.0, t_grid[:-1]]dts = t_grid - t_prevt_mid = 0.5 * (t_prev + t_grid)diff --git a/agent/tests/test_cds_final_stub_regressions.py b/agent/tests/test_cds_final_stub_regressions.pynew file mode 100644index 000000000..b792db29e--- /dev/null+++ b/agent/tests/test_cds_final_stub_regressions.py@@ -0,0 +1,38 @@+import numpy as np+import pytest+from src.quantlib.credit import cds_price++[email protected]("tenor", [0.1, 0.6, 1.1, 1.25])+def test_cds_regular_coupon_dates_and_final_stub(tenor):+ frequency = 4+ regular = np.arange(1, int(np.ceil(tenor * frequency)), dtype=float) / frequency+ dates = np.r_[regular, tenor]+ lengths = np.diff(np.r_[0.0, dates])+ expected = float(np.sum(lengths * np.exp(-0.07 * dates)))+ actual = cds_price(+ 0.0, tenor_years=tenor, risk_free_rate=0.07, payment_frequency=frequency+ )+ assert actual["rpv01"] == pytest.approx(expected, abs=1e-14)+ assert actual["premium_leg_pv"] == pytest.approx(0.01 * expected)+++def test_quantlib_tool_prices_final_coupon_stub():+ import json+ from src.tools.quantlib_tool import QuantlibCallTool++ result = json.loads(+ QuantlibCallTool().execute(+ action="call",+ module="credit",+ function="cds_price",+ kwargs={"spread_bps": 0.0, "tenor_years": 0.6, "risk_free_rate": 0.07},+ )+ )+ expected = (+ 0.25 * np.exp(-0.07 * 0.25)+ + 0.25 * np.exp(-0.07 * 0.5)+ + 0.1 * np.exp(-0.07 * 0.6)+ )+ assert result["ok"] is True+ assert result["result"]["rpv01"] == pytest.approx(expected)