"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(quantlib): scale Carino coincidence by remaining wealth (#1943)
Rudy Mizrahi Celeklibcc42e020a40
diff --git a/agent/src/quantlib/attribution.py b/agent/src/quantlib/attribution.pyindex a7b220734..c726a2ef8 100644--- a/agent/src/quantlib/attribution.py+++ b/agent/src/quantlib/attribution.py@@ -252,7 +252,9 @@ def brinson_fachler(is non-finite or negative."""ordered: list[str] = list(portfolio_weights)- ordered.extend(sector for sector in benchmark_weights if sector not in portfolio_weights)+ ordered.extend(+ sector for sector in benchmark_weights if sector not in portfolio_weights+ )if not ordered:raise ValueError("brinson_fachler needs at least one sector")@@ -285,13 +287,19 @@ def brinson_fachler(r_b = benchmark_returns.get(sector)if r_p is None:if w_p != 0.0:- raise ValueError(f"sector {sector!r} has portfolio weight {w_p!r} but no portfolio return")+ raise ValueError(+ f"sector {sector!r} has portfolio weight {w_p!r} but no portfolio return"+ )if r_b is None:- raise ValueError(f"sector {sector!r} has no portfolio return and no benchmark return")+ raise ValueError(+ f"sector {sector!r} has no portfolio return and no benchmark return"+ )r_p = r_bif r_b is None:if w_b != 0.0:- raise ValueError(f"sector {sector!r} has benchmark weight {w_b!r} but no benchmark return")+ raise ValueError(+ f"sector {sector!r} has benchmark weight {w_b!r} but no benchmark return"+ )r_b = r_pfor side, value in (("portfolio", r_p), ("benchmark", r_b)):if not math.isfinite(value):@@ -361,9 +369,16 @@ def carino_factor(portfolio_return: float, benchmark_return: float) -> float:"Carino linking needs returns above -100%; got portfolio "f"{portfolio_return!r} and benchmark {benchmark_return!r}")- if abs(portfolio_return - benchmark_return) < _CARINO_LIMIT_EPS:+ # Near coincidence is relative to the remaining wealth, not to the+ # returns' absolute scale: two returns close to -100% can differ by a+ # tiny absolute amount while their wealth relatives differ severalfold.+ if abs(portfolio_return - benchmark_return) < _CARINO_LIMIT_EPS * min(+ portfolio_wealth, benchmark_wealth+ ):return 2.0 / (portfolio_wealth + benchmark_wealth)- return (math.log(portfolio_wealth) - math.log(benchmark_wealth)) / (portfolio_return - benchmark_return)+ return (math.log(portfolio_wealth) - math.log(benchmark_wealth)) / (+ portfolio_return - benchmark_return+ )def carino_link(periods: Sequence[BrinsonResult]) -> LinkedAttribution:@@ -397,7 +412,8 @@ def carino_link(periods: Sequence[BrinsonResult]) -> LinkedAttribution:total_factor = carino_factor(total_portfolio_return, total_benchmark_return)scaling_factors = tuple(- carino_factor(period.portfolio_return, period.benchmark_return) / total_factor for period in periods+ carino_factor(period.portfolio_return, period.benchmark_return) / total_factor+ for period in periods)order: list[str] = []diff --git a/agent/tests/quantlib/test_attribution_relative_limit.py b/agent/tests/quantlib/test_attribution_relative_limit.pynew file mode 100644index 000000000..2075c3293--- /dev/null+++ b/agent/tests/quantlib/test_attribution_relative_limit.py@@ -0,0 +1,27 @@+"""Independent high-precision checks of the Carino near-coincidence limit."""++import pytest++from src.quantlib.attribution import carino_factor+++ "portfolio_wealth,benchmark_wealth", [(1e-10, 2e-10), (1e-12, 8e-12)]+)+def test_carino_limit_is_relative_to_remaining_wealth(+ portfolio_wealth: float, benchmark_wealth: float+) -> None:+ """Small return differences near total loss need the logarithmic factor."""+ from decimal import Decimal, localcontext++ portfolio_return = portfolio_wealth - 1.0+ benchmark_return = benchmark_wealth - 1.0+ # Use exact supplied floats and independent high-precision logarithms.+ with localcontext() as ctx:+ ctx.prec = 60+ rp = Decimal.from_float(portfolio_return)+ rb = Decimal.from_float(benchmark_return)+ expected = float(((1 + rp).ln() - (1 + rb).ln()) / (rp - rb))+ assert carino_factor(portfolio_return, benchmark_return) == pytest.approx(+ expected, rel=1e-12+ )