Source code for kaxanuk.attribution_analysis.modules.reconciliation
"""
Cross-checks between attribution outputs and independent inputs.
They warn instead of raising: a holdings-based benchmark return differs from a
published index return for legitimate reasons (pricing source, intraday
rebalances, fees). A large gap usually means the benchmark weights do not
cover the whole benchmark.
"""
import logging
import math
import pyarrow as pa
from kaxanuk.attribution_analysis.entities.attribution_result import DiagnosticCode
from kaxanuk.attribution_analysis.modules.diagnostics import log_diagnostic
logger = logging.getLogger(__name__)
BENCHMARK_RETURN_TOLERANCE = 1e-4
[docs]
def compare_benchmark_returns(
holdings_returns: pa.Table,
reported_returns: pa.Table,
date_column: str = "date",
tolerance: float = BENCHMARK_RETURN_TOLERANCE,
) -> float | None:
"""
Compare the holdings-based benchmark return with the reported series.
Null and non-finite (NaN, inf) values on either side are skipped.
Parameters
----------
holdings_returns : pa.Table
Brinson-Fachler per-date totals with `date_column` and ``benchmark_returns``.
reported_returns : pa.Table
Reported benchmark returns: `date_column` plus exactly one return column.
date_column : str, default ``"date"``
Date column name in both tables.
tolerance : float, default ``BENCHMARK_RETURN_TOLERANCE``
Absolute daily difference above which a date counts as divergent.
Returns
-------
float or None
Largest absolute daily difference over dates where both values exist;
None when the reported table is unusable or no date overlaps.
Notes
-----
Inputs are expected to be validated upstream (date column present, numeric return column;
in main() the reported table is a ReturnsTable).
"""
reported_columns = [c for c in reported_returns.column_names if c != date_column]
if len(reported_columns) != 1:
log_diagnostic(
logger,
DiagnosticCode.BENCHMARK_CHECK_BAD_RETURN_COLUMNS,
"Benchmark cross-check skipped: the reported benchmark returns must have one return column, found %d",
len(reported_columns),
count=len(reported_columns),
)
return None
reported = dict(zip(
reported_returns.column(date_column).to_pylist(),
reported_returns.column(reported_columns[0]).to_pylist(),
strict=True,
))
diffs = []
for date, value in zip(
holdings_returns.column(date_column).to_pylist(),
holdings_returns.column("benchmark_returns").to_pylist(),
strict=True,
):
reported_value = reported.get(date)
if value is None or reported_value is None:
continue
if not (math.isfinite(value) and math.isfinite(reported_value)):
continue
diffs.append(abs(value - reported_value))
if not diffs:
log_diagnostic(
logger,
DiagnosticCode.BENCHMARK_CHECK_NO_COMMON_DATES,
"Benchmark cross-check skipped: no comparable dates (common dates with finite values on both sides)",
)
return None
worst = max(diffs)
n_divergent = sum(diff > tolerance for diff in diffs)
if n_divergent:
log_diagnostic(
logger,
DiagnosticCode.BENCHMARK_RETURNS_DIFFER,
"Holdings-based benchmark return differs from the reported benchmark return by more than "
"%.1e on %d of %d dates (largest %.6f). Check that the benchmark weights cover the "
"whole benchmark and that their prices match the index.",
tolerance, n_divergent, len(diffs), worst,
count=n_divergent,
total=len(diffs),
details={"largest_difference": worst, "tolerance": tolerance},
)
return worst