Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(quantlib): refuse a bare-date as_of in american_waterfall

Yufeng He133203067064
diff --git a/agent/src/quantlib/fundmath.py b/agent/src/quantlib/fundmath.py
index 64597a082..4ed64df71 100644
--- a/agent/src/quantlib/fundmath.py
+++ b/agent/src/quantlib/fundmath.py
@@ -2047,17 +2047,25 @@ def american_waterfall(
An :class:`AmericanWaterfallResult` aggregating every deal.
Raises:
- TypeError: If ``deals`` is not a mapping, or a value is not a
- ``CashFlowSeries``.
+ TypeError: If ``deals`` is not a mapping, a value is not a
+ ``CashFlowSeries``, or ``as_of`` is neither a mapping nor None.
ValueError: If ``deals`` is empty, spans more than one currency, or
any deal raises the errors :func:`european_waterfall` already
documents (bad rate, contribution after ``as_of``, sign-
convention violation, and so on).
"""
deals = _validate_deals(deals)
+ if as_of is not None and not isinstance(as_of, Mapping):
+ # A bare date silently fell back to per-deal latest-flow accrual here,
+ # which reads like an honored override. Refuse it like a bad `deals`.
+ raise TypeError(
+ "as_of must be a mapping of deal_id -> date, or None; got "
+ f"{type(as_of).__name__}. Pass a single date per deal as "
+ "{deal_id: date}, or None to accrue to each deal's latest flow date."
+ )
per_deal: dict[str, WaterfallResult] = {}
for deal_id, series in deals.items():
- deal_as_of = as_of.get(deal_id) if isinstance(as_of, Mapping) else None
+ deal_as_of = as_of.get(deal_id) if as_of is not None else None
per_deal[deal_id] = european_waterfall(
series,
preferred_rate=preferred_rate,
diff --git a/agent/tests/quantlib/test_fundmath.py b/agent/tests/quantlib/test_fundmath.py
index 64e658d46..d2387030f 100644
--- a/agent/tests/quantlib/test_fundmath.py
+++ b/agent/tests/quantlib/test_fundmath.py
@@ -1079,6 +1079,36 @@ def test_american_waterfall_refuses_a_non_mapping():
american_waterfall([("only", None)], preferred_rate=0.08, carry_rate=0.20)
+def test_american_waterfall_refuses_a_bare_date_as_of():
+ """A bare date is out of contract (as_of is deal_id -> date). Silently
+ treating it as None would accrue the preferred to each deal's own latest
+ flow date while looking like the override was honored.
+ """
+ series = make_series(
+ [("2020-01-01", -100.0, "capital_call"), ("2021-01-01", 150.0, "distribution")]
+ )
+ with pytest.raises(TypeError, match="as_of"):
+ american_waterfall(
+ {"A": series}, preferred_rate=0.08, carry_rate=0.20, as_of=dt.date(2024, 1, 1)
+ )
+
+
+def test_american_waterfall_honors_as_of_per_deal():
+ """100 contributed on 2020-01-01 at an 8% pref, measured to 2024-01-01
+ (1461 days), owes 100 * (1.08 ** (1461 / 365) - 1) = 36.0776 of preferred.
+ """
+ series = make_series(
+ [("2020-01-01", -100.0, "capital_call"), ("2021-01-01", 150.0, "distribution")]
+ )
+ result = american_waterfall(
+ {"A": series},
+ preferred_rate=0.08,
+ carry_rate=0.20,
+ as_of={"A": dt.date(2024, 1, 1)},
+ )
+ assert result.deals["A"].preferred_amount == pytest.approx(36.07758522909388)
+
+
def test_american_waterfall_refuses_mixed_currencies():
deals = {
"A": make_series(