Source code for kaxanuk.attribution_analysis.entities.attribution_inputs

"""
Attribution inputs
==================

:class:`AttributionInputs` holds everything the attribution stage needs, already
loaded and in attribution format, so that file-based runs and in-memory runs
(e.g. a Backtest-Engine result) share one computation,
:func:`~kaxanuk.attribution_analysis.performance_attribution.attribute`.
"""

import dataclasses
from collections.abc import Mapping

from kaxanuk.attribution_analysis.entities.factor_returns import FactorReturns
from kaxanuk.attribution_analysis.entities.portfolio_weights_arrow import PortfolioWeights
from kaxanuk.attribution_analysis.entities.returns_table import ReturnsTable


[docs] @dataclasses.dataclass(frozen=True, slots=True, kw_only=True) class AttributionInputs: """ Loaded inputs of one attribution run, before date alignment. Every table has a ``date`` column of type ``timestamp(ns)``. Attributes ---------- asset_returns : ReturnsTable Simple per-ticker returns. The first row is the first price date and has no return (null), as ``pct_change`` leaves it; it is never a period. When the weights hold the cash ticker, a zero-return cash column is present. portfolio_weights : PortfolioWeights Start-of-day portfolio weights (the book held going into each date). benchmark_returns : ReturnsTable or None, default None Reported benchmark return: ``date`` plus one return column. ``None`` when there is none (e.g. a Backtest-Engine run without a benchmark). When given, it bounds the aligned date window, and textbook Brinson-Fachler and the active factor model compare their holdings benchmark return with it (a warning when they differ); without it they skip that check. The factor model stores it but never uses it, so its numbers do not depend on it beyond the window. benchmark_weights : PortfolioWeights or None Start-of-day benchmark weights; required by Brinson-Fachler and by the active factor model. benchmark_weights_bound_window : bool, default True Whether the dates of `benchmark_weights` bound the aligned date window. True is the file pipeline's behaviour: the benchmark weights file bounds the window of every run. With False they bound it only when Brinson-Fachler runs (it needs them on every date); the active factor model alone then leaves the portfolio's window as it is and drops the dates without benchmark weights from the active results only. factor_returns : Mapping[str, FactorReturns] Per-factor tables (``date`` x ticker), simple or log as loaded. May be empty when the factor model does not run. Every table given bounds the aligned date window, whether or not the factor model runs. segment_map : Mapping[str, str] or None Ticker-to-segment map for textbook Brinson-Fachler; ``None`` makes every ticker its own segment. """ asset_returns: ReturnsTable portfolio_weights: PortfolioWeights benchmark_returns: ReturnsTable | None = None benchmark_weights: PortfolioWeights | None = None benchmark_weights_bound_window: bool = True factor_returns: Mapping[str, FactorReturns] = dataclasses.field(default_factory=dict) segment_map: Mapping[str, str] | None = None