"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(quantlib): every fixed-income entry point rejects NaN and infinities (#1558)
Haozhe Wuf1c1eb5ba308
diff --git a/agent/src/quantlib/fixedincome.py b/agent/src/quantlib/fixedincome.pyindex 08c79835a..1b15cfe92 100644--- a/agent/src/quantlib/fixedincome.py+++ b/agent/src/quantlib/fixedincome.py@@ -118,6 +118,21 @@ def year_fraction(return total+def _require_finite(**values: float | np.ndarray) -> None:+ """Raise when a named numeric input holds NaN or an infinity.++ Every range check in this module is a comparison (``face <= 0``) and a+ comparison with NaN is False, so a NaN walked past each of them and came+ out as a NaN price, duration or curve.++ Raises:+ ValueError: Naming the first non-finite input.+ """+ for name, value in values.items():+ if not np.all(np.isfinite(np.asarray(value, dtype=float))):+ raise ValueError(f"{name} must be finite, got {value!r}")++def _is_leap(year: int) -> bool:"""Report whether a Gregorian year is a leap year.@@ -215,6 +230,7 @@ def bond_cashflows(ValueError: If ``face`` is not positive, if ``n_periods`` is not apositive integer, or if ``freq`` is not positive."""+ _require_finite(face=face, coupon_rate=coupon_rate)if face <= 0:raise ValueError(f"face must be positive, got {face}")if isinstance(n_periods, bool) or not isinstance(n_periods, (int, np.integer)) or n_periods <= 0:@@ -235,6 +251,7 @@ def _discount_factors(compounding: str,) -> np.ndarray:"""Compute discount factors for period indices under discrete or continuous compounding."""+ _require_finite(ytm=ytm)if compounding == "discrete":periodic = 1.0 + ytm / freqif periodic <= 0.0:@@ -540,6 +557,7 @@ def effective_duration(Raises:ValueError: If ``bump`` is not positive or the base price is zero."""+ _require_finite(yield_level=yield_level, bump=bump)if bump <= 0:raise ValueError(f"bump must be positive, got {bump}")base = reprice(yield_level)@@ -552,6 +570,7 @@ def effective_duration(def _decay_factors(tau: np.ndarray, lam: float) -> tuple[np.ndarray, np.ndarray]:"""Compute Nelson-Siegel slope and curvature loadings for decay parameter lam."""+ _require_finite(decay=lam)if lam <= 0:raise ValueError(f"decay parameter must be positive, got {lam}")x = tau / lam@@ -565,6 +584,7 @@ def _as_tau(tau: float | Sequence[float] | np.ndarray) -> tuple[np.ndarray, boolarr = np.asarray(tau, dtype=float)was_scalar = arr.ndim == 0arr = np.atleast_1d(arr)+ _require_finite(maturities=arr)if np.any(arr < 0):raise ValueError("maturities must be non-negative")return arr, was_scalar@@ -578,6 +598,7 @@ def nelson_siegel(lambda1: float,) -> float | np.ndarray:"""Compute Nelson-Siegel zero rates for given maturities and parameters."""+ _require_finite(beta0=beta0, beta1=beta1, beta2=beta2)arr, was_scalar = _as_tau(tau)slope, curve = _decay_factors(arr, lambda1)out = beta0 + beta1 * slope + beta2 * curve@@ -594,6 +615,7 @@ def svensson(lambda2: float,) -> float | np.ndarray:"""Compute Svensson zero rates for given maturities and parameters."""+ _require_finite(beta0=beta0, beta1=beta1, beta2=beta2, beta3=beta3)arr, was_scalar = _as_tau(tau)slope, curve1 = _decay_factors(arr, lambda1)_, curve2 = _decay_factors(arr, lambda2)diff --git a/agent/tests/quantlib/test_fixedincome.py b/agent/tests/quantlib/test_fixedincome.pyindex 063c8f9db..dd378079d 100644--- a/agent/tests/quantlib/test_fixedincome.py+++ b/agent/tests/quantlib/test_fixedincome.py@@ -561,3 +561,24 @@ def test_fit_yield_curve_rejects_nonfinite_observations(bad):yields[3] = badwith pytest.raises(ValueError, match="finite"):fit_yield_curve(maturities, yields, model="nelson_siegel")+++ ("call", "name"),+ [+ (lambda bad: bond_price(bad, 0.05, 0.05, 10), "face"),+ (lambda bad: bond_price(100.0, bad, 0.05, 10), "coupon_rate"),+ (lambda bad: bond_price(100.0, 0.05, bad, 10), "ytm"),+ (lambda bad: convexity(100.0, 0.05, bad, 10), "ytm"),+ (lambda bad: key_rate_duration(100.0, 0.05, bad, 10), "ytm"),+ (lambda bad: effective_duration(lambda y: bond_price(100.0, 0.05, y, 10), bad), "yield_level"),+ (lambda bad: nelson_siegel(bad, 0.04, -0.01, 0.01, 2.0), "maturities"),+ (lambda bad: nelson_siegel(5.0, 0.04, bad, 0.01, 2.0), "beta1"),+ (lambda bad: svensson(5.0, 0.04, -0.01, 0.01, 0.01, 2.0, bad), "decay"),+ ],+)[email protected]("bad", [np.nan, np.inf])+def test_a_non_finite_input_is_named_not_priced(call, name, bad):+ """Each range check is a comparison, and NaN fails none of them: it came out as a NaN price."""+ with pytest.raises(ValueError, match=f"{name} must be finite"):+ call(bad)