"""
Abstract interface for Brinson-Fachler return attribution using PyArrow Tables.
Defines the contract that concrete Brinson-Fachler implementations must
satisfy: computing single-period attribution for a given date and
aggregating single-period results into a time series.
"""
import abc
import pyarrow as pa
from kaxanuk.attribution_analysis.exceptions import AttributionArgumentError
[docs]
class BrinsonFachlerArrowInterface(metaclass=abc.ABCMeta):
"""
Abstract interface for Brinson-Fachler attribution using PyArrow Tables.
Defines the contract for computing single-period and multi-period
Brinson-Fachler return attribution, decomposing active return into
allocation, selection, and interaction effects.
Parameters
----------
returns_investable_assets : pa.Table, optional
Asset (or group) returns with a date column and one float64 column
per asset. Implementations may take separate portfolio and benchmark
returns instead.
complete_portfolio_weights : pa.Table
Portfolio weights with a date column and one float64 column per asset.
Required; ``None`` raises.
complete_benchmark_weights : pa.Table
Benchmark weights with a date column and one float64 column per asset.
Required; ``None`` raises.
Raises
------
AttributionArgumentError
If a weights table is missing (also a ``ValueError``).
"""
def __init__(
self,
returns_investable_assets: pa.Table | None = None,
complete_portfolio_weights: pa.Table | None = None,
complete_benchmark_weights: pa.Table | None = None,
) -> None:
if complete_portfolio_weights is None or complete_benchmark_weights is None:
raise AttributionArgumentError(
"complete_portfolio_weights and complete_benchmark_weights are required"
)
self.returns_investable_assets = returns_investable_assets
self.complete_portfolio_weights = complete_portfolio_weights
self.complete_benchmark_weights = complete_benchmark_weights
[docs]
@abc.abstractmethod
def brinson_fachler_model(
self,
date: object
) -> tuple[pa.Table, pa.Table]:
"""
Calculate the Brinson-Fachler attribution model for a single date.
Decomposes the active return (portfolio minus benchmark) into
allocation, selection, and interaction effects for each asset.
Parameters
----------
date : object
The date to attribute. For a table whose date column is a date or
timestamp type: a ``datetime.date``, ``datetime.datetime``,
``pd.Timestamp``, ``np.datetime64``, ``pa.Scalar`` or ISO-8601 string,
converted to that table's own date type (a number is not a date);
otherwise a value of the column (e.g. a ``str`` for string columns). A
date absent from a table gives zero weights / missing returns there.
Returns
-------
detail_table : pa.Table
Per-group attribution with columns: ``asset``, ``portfolio_weights``,
``benchmark_weights``, ``returns_data`` (portfolio-side group
return), ``benchmark_returns_data`` (benchmark-side group return),
``portfolio_returns``, ``benchmark_returns``, ``alpha``,
``allocation``, ``selection``, ``interaction``.
totals_table : pa.Table
Single-row summary with columns: ``date``, ``portfolio_returns``,
``benchmark_returns``, ``alpha``, ``allocation``, ``selection``,
``interaction``.
"""
...
[docs]
@abc.abstractmethod
def time_series_calculation(
self
) -> None:
"""
Calculate the Brinson-Fachler attribution across all available dates.
Attributes every date of the portfolio weights that the benchmark
weights and the returns also hold (each date converted to every
table's own date type, as in ``brinson_fachler_model``), computing the
single-period model for each, and concatenates the totals into a
time-series table.
Implementations store the totals time series (columns ``date``,
``portfolio_returns``, ``benchmark_returns``, ``alpha``, ``allocation``,
``selection``, ``interaction``) and the per-date detail tables on the
instance; nothing is returned.
"""
...
[docs]
@abc.abstractmethod
def compute_cumulative_indexes(
self
) -> pa.Table:
"""
Accumulate the per-period attribution totals over time.
Pure computation over the time series produced by
``time_series_calculation``: every numeric attribution column is
replaced by its cumulative value (linked, or a running sum, per the
implementation's linking method). Implementations must not plot or
produce any other side effect here.
Returns
-------
pa.Table
Cumulative attribution indexes keyed by ``date``, with columns
``portfolio_returns``, ``benchmark_returns``, ``alpha``,
``allocation``, ``selection``, ``interaction``.
"""
...