Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(fundmath): reject non-finite hurdle and waterfall inputs (#1919)

Rudy Mizrahi Celekli01024b8a849a
diff --git a/agent/src/quantlib/fundmath.py b/agent/src/quantlib/fundmath.py
index 4ed64df71..aee2c5233 100644
--- a/agent/src/quantlib/fundmath.py
+++ b/agent/src/quantlib/fundmath.py
@@ -952,19 +952,19 @@ def preferred_return_amount(
Raises:
TypeError: If ``series`` is not a ``CashFlowSeries``.
- ValueError: If the rate is negative, the compounding convention is
+ ValueError: If the rate is negative or non-finite, the compounding convention is
unknown, ``as_of`` precedes a contribution, or ``as_of`` is omitted
for an empty series.
"""
_require_series(series)
- if rate < 0.0:
- raise ValueError(f"preferred rate must be non-negative, got {rate!r}")
+ if not math.isfinite(rate) or rate < 0.0:
+ raise ValueError(f"preferred rate must be finite and non-negative, got {rate!r}")
if compounding not in PREFERRED_COMPOUNDING:
raise ValueError(
f"compounding={compounding!r} is not one of {PREFERRED_COMPOUNDING}"
)
- if days_per_year <= 0:
- raise ValueError(f"days_per_year must be positive, got {days_per_year!r}")
+ if not math.isfinite(days_per_year) or days_per_year <= 0:
+ raise ValueError(f"days_per_year must be finite and positive, got {days_per_year!r}")
contributions = tuple(series.filter(kind=contribution_kinds))
if not contributions:
@@ -1160,7 +1160,7 @@ def waterfall_split(
A :class:`WaterfallResult` whose tiers sum exactly to ``distributable``.
Raises:
- ValueError: If any amount is negative, if ``carry_rate`` is outside
+ ValueError: If any amount is negative or non-finite, if ``carry_rate`` is outside
``[0, 1)``, if ``catch_up_rate`` is outside ``[0, 1]``, or if a
non-zero ``catch_up_rate`` does not exceed ``carry_rate``.
"""
@@ -1169,8 +1169,8 @@ def waterfall_split(
("contributed_capital", contributed_capital),
("preferred_amount", preferred_amount),
):
- if value < 0.0:
- raise ValueError(f"{label} must be non-negative, got {value!r}")
+ if not math.isfinite(value) or value < 0.0:
+ raise ValueError(f"{label} must be finite and non-negative, got {value!r}")
if not 0.0 <= carry_rate < 1.0:
raise ValueError(f"carry_rate must be in [0, 1), got {carry_rate!r}")
if not 0.0 <= catch_up_rate <= 1.0:
diff --git a/agent/tests/quantlib/test_fundmath_nonfinite_waterfall_inputs.py b/agent/tests/quantlib/test_fundmath_nonfinite_waterfall_inputs.py
new file mode 100644
index 000000000..dbd0ba2a2
--- /dev/null
+++ b/agent/tests/quantlib/test_fundmath_nonfinite_waterfall_inputs.py
@@ -0,0 +1,54 @@
+"""Non-finite hurdle and waterfall inputs must raise, not mis-split profit."""
+
+from __future__ import annotations
+
+import math
+from datetime import date
+
+import pytest
+
+from src.entities.cashflow import CashFlow, CashFlowSeries
+from src.quantlib.fundmath import european_waterfall, preferred_return_amount, waterfall_split
+
+
+def _series() -> CashFlowSeries:
+ return CashFlowSeries(
+ (
+ CashFlow(date(2020, 1, 1), -100.0, "contribution", "USD"),
+ CashFlow(date(2025, 1, 1), 300.0, "distribution", "USD"),
+ )
+ )
+
+
[email protected]("rate", [math.nan, math.inf])
+def test_non_finite_preferred_rate_raises(rate: float) -> None:
+ with pytest.raises(ValueError, match="preferred rate"):
+ preferred_return_amount(_series(), rate=rate)
+ with pytest.raises(ValueError, match="preferred rate"):
+ european_waterfall(_series(), preferred_rate=rate, carry_rate=0.2)
+
+
[email protected]("days_per_year", [math.nan, math.inf])
+def test_non_finite_day_basis_raises(days_per_year: float) -> None:
+ with pytest.raises(ValueError, match="days_per_year"):
+ preferred_return_amount(_series(), rate=0.08, days_per_year=days_per_year)
+
+
+ "amounts",
+ [
+ (math.nan, 100.0, 8.0),
+ (150.0, math.nan, 8.0),
+ (150.0, 100.0, math.nan),
+ (150.0, 100.0, math.inf),
+ ],
+)
+def test_non_finite_waterfall_amounts_raise(amounts: tuple[float, float, float]) -> None:
+ with pytest.raises(ValueError, match="finite and non-negative"):
+ waterfall_split(*amounts, carry_rate=0.2)
+
+
+def test_finite_inputs_keep_their_split() -> None:
+ result = european_waterfall(_series(), preferred_rate=0.08, carry_rate=0.2)
+ assert result.gp_total > 0.0
+ assert math.isclose(result.lp_total + result.gp_total, 300.0)