"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
feat(quantlib): add ISDA standard model Credit Default Swap pricing engine
santhreal407387abe411
diff --git a/agent/src/quantlib/credit.py b/agent/src/quantlib/credit.pyindex 03d03cea5..04ec1882e 100644--- a/agent/src/quantlib/credit.py+++ b/agent/src/quantlib/credit.py@@ -48,6 +48,9 @@ __all__ = ["edf_reference_band","credit_spread_analysis","spread_term_structure",+ "hazard_rate_to_survival_probability",+ "survival_probability_to_hazard_rate",+ "cds_price",]@@ -673,3 +676,142 @@ def spread_term_structure()records.append(pd.Series(row, name=issuer))return pd.DataFrame(records)+++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).++ Args:+ hazard_rate: Annualised default intensity / hazard rate lambda >= 0.+ tenor_years: Time horizon in years >= 0.++ Returns:+ Survival probability in [0.0, 1.0].+ """+ if hazard_rate < 0.0 or tenor_years < 0.0:+ raise ValueError(f"hazard_rate and tenor_years must be non-negative, got {hazard_rate}, {tenor_years}")+ return float(np.exp(-hazard_rate * tenor_years))+++def survival_probability_to_hazard_rate(survival_prob: float, tenor_years: float) -> float:+ """Convert a survival probability Q(T) to implied constant hazard rate lambda = -ln(Q(T)) / T.++ Args:+ survival_prob: Survival probability in (0.0, 1.0].+ tenor_years: Time horizon in years > 0.++ Returns:+ Annualised hazard rate lambda.+ """+ if survival_prob <= 0.0 or survival_prob > 1.0:+ raise ValueError(f"survival_prob must be in (0.0, 1.0], got {survival_prob}")+ if tenor_years <= 0.0:+ raise ValueError(f"tenor_years must be strictly positive, got {tenor_years}")+ return float(-np.log(survival_prob) / tenor_years)+++def cds_price(+ spread_bps: float,+ recovery_rate: float = 0.40,+ tenor_years: float = 5.0,+ risk_free_rate: float = 0.03,+ coupon_bps: float = 100.0,+ notional: float = 10_000_000.0,+ payment_frequency: int = 4,+) -> dict:+ """ISDA standard model single-name Credit Default Swap (CDS) valuation engine.++ Computes the implied hazard rate, survival probability curve, Risky Present Value+ of a Basis Point (RPV01), protection leg PV, premium leg PV, fair par spread,+ and mark-to-market (MTM) upfront cash payment.++ Args:+ spread_bps: Market quoted par CDS spread in basis points (e.g. 150.0 for 150 bps).+ recovery_rate: Expected recovery rate on default in [0.0, 1.0) (standard senior unsecured = 0.40).+ tenor_years: Contract maturity in years > 0 (standard benchmark = 5.0 years).+ risk_free_rate: Continuously compounded annual risk-free discount rate.+ coupon_bps: Fixed running coupon in basis points (standard 100 bps IG, 500 bps HY).+ notional: Contract notional amount in currency units.+ payment_frequency: Premium coupon payment frequency per year (standard quarterly = 4).++ Returns:+ dict with keys:+ * ``hazard_rate`` (float): Implied constant hazard rate (default intensity).+ * ``survival_probability`` (float): Probability of survival to maturity Q(T).+ * ``default_probability`` (float): Cumulative probability of default 1 - Q(T).+ * ``rpv01`` (float): Risky PV01 (present value of 1 bp coupon per dollar notional).+ * ``protection_leg_pv`` (float): Present value of default protection per dollar notional.+ * ``premium_leg_pv`` (float): Present value of fixed running premium per dollar notional.+ * ``par_spread_bps`` (float): Model par spread in basis points.+ * ``upfront_pct`` (float): Upfront payment as percentage of notional.+ * ``upfront_amount`` (float): Net upfront cash payment (positive = buyer pays seller).+ * ``buyer_mtm`` (float): Mark-to-market value for the protection buyer.++ Raises:+ ValueError: If spread_bps < 0, recovery_rate not in [0, 1), tenor_years <= 0, or notional <= 0.+ """+ if spread_bps < 0.0:+ raise ValueError(f"spread_bps must be non-negative, got {spread_bps}")+ if not (0.0 <= recovery_rate < 1.0):+ raise ValueError(f"recovery_rate must be in [0.0, 1.0), got {recovery_rate}")+ if tenor_years <= 0.0:+ raise ValueError(f"tenor_years must be strictly positive, got {tenor_years}")+ if notional <= 0.0:+ raise ValueError(f"notional must be strictly positive, got {notional}")+ if payment_frequency <= 0:+ raise ValueError(f"payment_frequency must be positive, got {payment_frequency}")++ s_dec = spread_bps / 10_000.0+ c_dec = coupon_bps / 10_000.0+ lgd = 1.0 - recovery_rate++ # Implied hazard rate lambda ≈ s / LGD+ lambda_hazard = float(s_dec / lgd) if lgd > 0 else 0.0++ dt = 1.0 / payment_frequency+ n_periods = int(round(tenor_years * payment_frequency))+ t_grid = np.linspace(dt, tenor_years, n_periods)++ # Survival probabilities Q(t) = exp(-lambda * t)+ q_grid = np.exp(-lambda_hazard * t_grid)+ q_prev = np.r_[1.0, q_grid[:-1]]++ # Discount factors D(t) = exp(-r * t)+ df_grid = np.exp(-risk_free_rate * t_grid)+ df_mid = np.exp(-risk_free_rate * (t_grid - 0.5 * dt))++ # Premium leg / RPV01: sum(dt * D(t_i) * Q(t_i)) + accrual on default+ # Accrual on default approx: sum( 0.5 * dt * D(t_mid) * (Q(t_{i-1}) - Q(t_i)) )+ premium_cashflow = np.sum(dt * df_grid * q_grid)+ accrual_cashflow = np.sum(0.5 * dt * df_mid * (q_prev - q_grid))+ rpv01 = float(premium_cashflow + accrual_cashflow)++ # Protection leg: sum( LGD * D(t_mid) * (Q(t_{i-1}) - Q(t_i)) )+ default_prob_steps = q_prev - q_grid+ prot_leg_pv = float(lgd * np.sum(df_mid * default_prob_steps))++ # Par spread in bps+ par_spread = float((prot_leg_pv / rpv01) * 10_000.0) if rpv01 > 0 else spread_bps++ # Premium leg PV for running coupon c+ prem_leg_pv = float(c_dec * rpv01)++ # Upfront percentage and amount+ upfront_pct = float(prot_leg_pv - prem_leg_pv)+ upfront_amt = float(upfront_pct * notional)++ survival_T = float(np.exp(-lambda_hazard * tenor_years))+ default_prob_T = float(1.0 - survival_T)++ return {+ "hazard_rate": lambda_hazard,+ "survival_probability": survival_T,+ "default_probability": default_prob_T,+ "rpv01": rpv01,+ "protection_leg_pv": prot_leg_pv,+ "premium_leg_pv": prem_leg_pv,+ "par_spread_bps": par_spread,+ "upfront_pct": upfront_pct,+ "upfront_amount": upfront_amt,+ "buyer_mtm": upfront_amt,+ }diff --git a/agent/tests/quantlib/test_credit.py b/agent/tests/quantlib/test_credit.pyindex 91bc92dc1..f93a70d18 100644--- a/agent/tests/quantlib/test_credit.py+++ b/agent/tests/quantlib/test_credit.py@@ -20,14 +20,17 @@ from src.quantlib.credit import (ZONE_LABELS_ZH,AltmanZScore,altman_z_score,+ cds_price,credit_spread_analysis,distance_to_default,edf_reference_band,+ hazard_rate_to_survival_probability,kmv_default_point,kmv_distance_to_default,merton_asset_solve,merton_model,spread_term_structure,+ survival_probability_to_hazard_rate,)# One balance sheet, reused so the three variants can be compared like for like.@@ -496,3 +499,70 @@ def test_spread_term_structure_validates_its_inputs():spread_term_structure({}, {1: 0.02})with pytest.raises(ValueError, match="unknown input_unit"):spread_term_structure({"x": {1: 0.025}}, {1: 0.02}, input_unit="ratio")+++# --------------------------------------------------------------------------+# CDS Pricing & Hazard Rate Tests+# --------------------------------------------------------------------------+++def test_hazard_rate_and_survival_prob_roundtrip():+ lambda_val = 0.025+ T = 5.0+ q = hazard_rate_to_survival_probability(lambda_val, T)+ assert 0.0 < q < 1.0+ recovered_lambda = survival_probability_to_hazard_rate(q, T)+ assert recovered_lambda == pytest.approx(lambda_val, rel=1e-12)+++def test_cds_price_fair_par_spread_zero_upfront():+ # When the fixed running coupon equals the model par spread, upfront payment is identically 0.+ spread = 150.0+ res_par = cds_price(+ spread_bps=spread,+ coupon_bps=spread,+ recovery_rate=0.40,+ tenor_years=5.0,+ risk_free_rate=0.03,+ )+ par_bps = res_par["par_spread_bps"]+ result = cds_price(+ spread_bps=spread,+ coupon_bps=par_bps,+ recovery_rate=0.40,+ tenor_years=5.0,+ risk_free_rate=0.03,+ )+ assert result["upfront_pct"] == pytest.approx(0.0, abs=1e-12)+ assert result["upfront_amount"] == pytest.approx(0.0, abs=1e-6)+ assert result["par_spread_bps"] == pytest.approx(spread, rel=0.05)+ assert result["survival_probability"] == pytest.approx(np.exp(-result["hazard_rate"] * 5.0), rel=1e-12)+ assert result["default_probability"] == pytest.approx(1.0 - result["survival_probability"], rel=1e-12)++def test_cds_price_buyer_mtm_positive_when_spread_above_coupon():+ # Quoted spread 250 bps > 100 bps running coupon -> protection buyer has positive MTM+ result = cds_price(+ spread_bps=250.0,+ coupon_bps=100.0,+ recovery_rate=0.40,+ tenor_years=5.0,+ notional=10_000_000.0,+ )+ assert result["upfront_amount"] > 0.0+ assert result["buyer_mtm"] > 0.0+ assert result["protection_leg_pv"] > result["premium_leg_pv"]+++def test_cds_price_input_validation():+ with pytest.raises(ValueError, match="spread_bps must be non-negative"):+ cds_price(spread_bps=-10.0)+ with pytest.raises(ValueError, match="recovery_rate"):+ cds_price(spread_bps=100.0, recovery_rate=1.0)+ with pytest.raises(ValueError, match="tenor_years"):+ cds_price(spread_bps=100.0, tenor_years=0.0)+ with pytest.raises(ValueError, match="notional"):+ cds_price(spread_bps=100.0, notional=-1000.0)+ with pytest.raises(ValueError, match="hazard_rate"):+ hazard_rate_to_survival_probability(-0.01, 5.0)+ with pytest.raises(ValueError, match="survival_prob"):+ survival_probability_to_hazard_rate(1.5, 5.0)