Source code for kaxanuk.attribution_analysis.modules.attribution_visualizers_arrow

"""
Attribution visualization module — Plotly charts using PyArrow tables.

Provides chart-creation functions that return Plotly Figure objects
for use in the Dash dashboard, without any pandas dependency.
All data extraction uses pa.Table / pyarrow.compute and plain Python lists.
"""

import math

import plotly.graph_objects as go
import pyarrow as pa
from plotly.subplots import make_subplots

from kaxanuk.attribution_analysis.dashboard.chart_colors import CHART_COLORS
from kaxanuk.attribution_analysis.dashboard.constants import PROFESSIONAL_COLORS
from kaxanuk.attribution_analysis.entities.attribution_options import (
    AGGREGATE_FACTOR_COLUMNS,
    COMPONENT_COLUMNS,
    FACTOR_DISPLAY_NAMES,
    INDUSTRY_FACTORS,
    STYLE_FACTORS,
)


# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------

def _ffill(values: list) -> list:
    """Forward-fill None / NaN values in a Python list."""
    result = []
    last_valid = 0.0
    for v in values:
        if v is None or (isinstance(v, float) and math.isnan(v)):
            result.append(last_valid)
        else:
            last_valid = v
            result.append(v)
    return result


def _rolling_mean(values: list, window: int) -> list:
    """
    Trailing rolling mean with a partial window at the start.

    Parameters
    ----------
    values : list
        Values to smooth; None counts as 0.0.
    window : int
        Number of trailing points averaged (values below 1 mean no
        smoothing). The first ``window - 1`` points average every point
        available so far (``min_periods=1``).

    Returns
    -------
    list
        One mean per input value.
    """
    window = max(window, 1)
    result = []
    running_sum = 0.0
    for i, v in enumerate(values):
        running_sum += v if v is not None else 0.0
        if i >= window:
            old = values[i - window]
            running_sum -= old if old is not None else 0.0
        result.append(running_sum / min(i + 1, window))
    return result


def _get_non_date_columns(table: pa.Table, date_column: str) -> list[str]:
    return [c for c in table.column_names if c != date_column]


def _extract_clean_series(
    table: pa.Table,
    date_column: str,
    value_column: str,
) -> tuple[list, list]:
    """Return (dates, values) with null rows removed."""
    dates = table.column(date_column).to_pylist()
    values = table.column(value_column).to_pylist()
    clean_dates, clean_values = [], []
    for d, v in zip(dates, values, strict=False):
        if v is not None:
            clean_dates.append(d)
            clean_values.append(v)
    return clean_dates, clean_values


# ---------------------------------------------------------------------------
# Chart functions
# ---------------------------------------------------------------------------

def _factor_panel_columns(simulated_rets: pa.Table, date_column: str) -> tuple[list[str], list[str]]:
    """
    Split the factor columns of ``simulated_rets`` into the style/other and industry panels.

    Parameters
    ----------
    simulated_rets : pa.Table
        Cumulative contribution per factor, one column per factor plus the date.
    date_column : str
        Name of the date column.

    Returns
    -------
    tuple[list[str], list[str]]
        ``(style_and_other, industry)``. Style and other: every column that is
        neither an aggregate (``AGGREGATE_FACTOR_COLUMNS``), a return component
        (``COMPONENT_COLUMNS``, drawn in panel 1 through the totals) nor a known
        industry, the names in ``STYLE_FACTORS`` first (in that order), then the rest
        alphabetically. Industry: names in ``INDUSTRY_FACTORS`` (in that order)
        with at least one non-zero, non-null value.
    """
    columns = [
        c for c in _get_non_date_columns(simulated_rets, date_column)
        if c not in AGGREGATE_FACTOR_COLUMNS and c not in COMPONENT_COLUMNS
    ]
    style = [c for c in STYLE_FACTORS if c in columns]
    other = sorted(c for c in columns if c not in STYLE_FACTORS and c not in INDUSTRY_FACTORS)
    industry = [
        c for c in INDUSTRY_FACTORS
        if c in columns and any(v is not None and v != 0 for v in simulated_rets.column(c).to_pylist())
    ]
    return style + other, industry


def _display_name(key: str) -> str:
    """Chart label for a factor column or decomposition key (see ``FACTOR_DISPLAY_NAMES``)."""
    return FACTOR_DISPLAY_NAMES.get(key, key)


_TOTALS_SERIES = (
    ("portfolio_returns", "Portfolio"),
    ("factor_returns", "Factors"),
    ("idiosyncratic_returns", "Idiosyncratic"),
    ("carry_returns", "Carry"),
    ("dividends_returns", "Dividends"),
)
"""
Totals lines in legend order. A series whose column is absent (a component not supplied) is not
drawn; colours follow the position here, so the first three keep their colours either way.
"""


def _totals_traces(cumulative_totals: pa.Table, date_column: str, width: float = 2) -> list[go.Scatter]:
    """Portfolio / Factors / Idiosyncratic (plus Carry / Dividends when present) traces from ``cumulative_totals``."""
    dates = cumulative_totals.column(date_column).to_pylist()
    present = set(cumulative_totals.column_names)
    return [
        go.Scatter(
            x=dates, y=cumulative_totals.column(column).to_pylist(),
            mode='lines', name=name,
            line={'color': CHART_COLORS[i % len(CHART_COLORS)], 'width': width},
            hovertemplate=f'<b>{name}</b><br>Date: %{{x}}<br>Return: %{{y:.4f}}<extra></extra>',
        )
        for i, (column, name) in enumerate(_TOTALS_SERIES)
        if column in present
    ]


[docs] def create_factor_attribution_chart( simulated_rets: pa.Table, date_column: str = "date", cumulative_totals: pa.Table | None = None, ) -> go.Figure: """ Create the 3-panel factor attribution line chart. Parameters ---------- simulated_rets : pa.Table Cumulative contribution per factor (``FactorModelArrowAttribution.simulated_rets``). date_column : str, default "date" Name of the date column in both tables. cumulative_totals : pa.Table or None, default None ``FactorModelArrowAttribution.cumulative_totals()``. Drawn in panel 1 as ``Portfolio`` / ``Factors`` / ``Idiosyncratic`` (plus ``Carry`` / ``Dividends`` when present, which panel 1's title then names); panel 1 is empty when None. Returns ------- go.Figure Panels: (1) the model's cumulative portfolio / factor / idiosyncratic return, plus carry / dividends when supplied, (2) style and other factors (every factor that is neither an aggregate, a return component nor an industry), (3) industry factors with a non-zero value. Aggregate files (``AGGREGATE_FACTOR_COLUMNS``) are never drawn. """ dates = simulated_rets.column(date_column).to_pylist() style_cols, industry_cols = _factor_panel_columns(simulated_rets, date_column) # Panel 1's title names the components drawn in it, as the matplotlib ``attribution_plots`` does. drawn_components = [] if cumulative_totals is None else [ name for column, name in _TOTALS_SERIES[3:] if column in cumulative_totals.column_names ] fig = make_subplots( rows=3, cols=1, subplot_titles=( " / ".join(["Total", "Factor", "Idiosyncratic", *drawn_components]), "Style and other factors", "Industry Returns", ), shared_xaxes=True, vertical_spacing=0.08, ) if cumulative_totals is not None: for trace in _totals_traces(cumulative_totals, date_column): fig.add_trace(trace, row=1, col=1) for color_idx, col in enumerate(style_cols): fig.add_trace( go.Scatter( x=dates, y=simulated_rets.column(col).to_pylist(), mode='lines', name=col, line={'color': CHART_COLORS[color_idx % len(CHART_COLORS)], 'width': 2}, hovertemplate=f'<b>{col}</b><br>Date: %{{x}}<br>Return: %{{y:.4f}}<extra></extra>', ), row=2, col=1, ) for color_idx, col in enumerate(industry_cols): fig.add_trace( go.Scatter( x=dates, y=simulated_rets.column(col).to_pylist(), mode='lines', name=col, line={'color': CHART_COLORS[color_idx % len(CHART_COLORS)], 'width': 1.5}, hovertemplate=f'<b>{col}</b><br>Date: %{{x}}<br>Return: %{{y:.4f}}<extra></extra>', ), row=3, col=1, ) for row in range(1, 4): fig.add_hline(y=0, line_dash="solid", line_color="black", line_width=1, row=row, col=1) fig.update_yaxes(tickformat='.1%', row=row, col=1) fig.update_layout( height=900, template='plotly_white', hovermode='x unified', legend={ 'orientation': 'v', 'yanchor': 'top', 'y': 1, 'xanchor': 'left', 'x': 1.02, 'font': {'size': 10, 'family': 'Segoe UI'}, }, margin={'l': 60, 'r': 160, 't': 60, 'b': 40}, ) return fig
[docs] def create_factor_attribution_summary_chart( cumulative_totals: pa.Table, date_column: str = "date", ) -> go.Figure: """ Create the compact headline chart of the model's cumulative totals. Parameters ---------- cumulative_totals : pa.Table ``FactorModelArrowAttribution.cumulative_totals()``: columns ``portfolio_returns``, ``factor_returns``, ``idiosyncratic_returns``, optionally ``carry_returns`` / ``dividends_returns``. date_column : str, default "date" Name of the date column. Returns ------- go.Figure Lines named ``Portfolio``, ``Factors`` and ``Idiosyncratic``, plus ``Carry`` and ``Dividends`` when the totals have those columns. """ fig = go.Figure() for trace in _totals_traces(cumulative_totals, date_column): fig.add_trace(trace) fig.add_hline(y=0, line_dash="solid", line_color="black", line_width=1) fig.update_layout( title="Cumulative Factor Attribution", xaxis_title="Date", yaxis_title="Cumulative Return", yaxis={'tickformat': '.1%'}, template='plotly_white', height=500, autosize=True, hovermode='x unified', legend={'yanchor': "top", 'y': 0.99, 'xanchor': "left", 'x': 0.01}, ) return fig
[docs] def create_stacked_decomposition_chart( pct_df_returns: pa.Table, date_column: str = "date", days_of_smoothing: int = 45, ) -> go.Figure: """Create a normalized stacked area chart of factor return decomposition. Each factor's share = abs(factor) / sum(abs(all factors)), always sums to 100%, smoothed with a trailing ``days_of_smoothing`` mean that uses a partial window at the start, so the chart begins at the first date. The residual column ``f_idio_returns`` is labelled "Idiosyncratic" and the return components ``f_carry`` / ``f_dividends`` "Carry" / "Dividends". Implemented with pure pyarrow + Python lists (no pandas). """ factor_cols = _get_non_date_columns(pct_df_returns, date_column) dates = pct_df_returns.column(date_column).to_pylist() n_rows = len(dates) # Extract and forward-fill each column columns_data = {} for col in factor_cols: columns_data[col] = _ffill(pct_df_returns.column(col).to_pylist()) # Compute absolute values and row-wise sums abs_total = [0.0] * n_rows abs_columns = {} for col in factor_cols: abs_vals = [abs(v) if v is not None else 0.0 for v in columns_data[col]] abs_columns[col] = abs_vals for i in range(n_rows): abs_total[i] += abs_vals[i] # Normalize: each column = abs(col) / abs_total pct_columns = {} for col in factor_cols: pct_columns[col] = [ abs_columns[col][i] / abs_total[i] if abs_total[i] != 0 else 0.0 for i in range(n_rows) ] # Rolling mean smoothed = {col: _rolling_mean(pct_columns[col], days_of_smoothing) for col in factor_cols} # Build chart num_cols = len(factor_cols) colors = [CHART_COLORS[i % len(CHART_COLORS)] for i in range(num_cols)] fig = go.Figure() for i, col in enumerate(factor_cols): hex_color = colors[i] rgb = tuple(int(hex_color[j:j + 2], 16) for j in (1, 3, 5)) rgba_fill = f'rgba({rgb[0]}, {rgb[1]}, {rgb[2]}, 0.75)' fig.add_trace(go.Scatter( x=dates, y=smoothed[col], mode='lines', name=_display_name(col), stackgroup='one', groupnorm='percent', line={'width': 0.5, 'color': hex_color}, fillcolor=rgba_fill, hovertemplate=f'<b>{_display_name(col)}</b><br>Date: %{{x}}<br>Share: %{{y:.1f}}%<extra></extra>', )) fig.update_layout( title=f"Return Decomposition (Rolling {days_of_smoothing}d)", xaxis_title="Date", yaxis_title="Share of Total Return", yaxis={'tickformat': '.0%', 'range': [0, 100]}, template='plotly_white', height=500, hovermode='x unified', legend={ 'orientation': 'v', 'yanchor': 'top', 'y': 1, 'xanchor': 'left', 'x': 1.02, 'font': {'size': 10, 'family': 'Segoe UI'}, }, margin={'l': 60, 'r': 160, 't': 60, 'b': 40}, ) return fig
[docs] def create_decomposition_bar_chart( total_pct: dict, ) -> go.Figure: """Create a horizontal bar chart of end-of-period factor contributions. Parameters ---------- total_pct : dict {factor_name: contribution_fraction} — e.g. from FactorModelArrowAttribution.cumulative_pct_decomp(). The residual key ``idio_returns`` is labelled "Idiosyncratic" and the return components ``f_carry`` / ``f_dividends`` "Carry" / "Dividends". """ sorted_items = sorted(total_pct.items(), key=lambda x: x[1]) names = [_display_name(item[0]) for item in sorted_items] values = [item[1] for item in sorted_items] colors = ['#DC2626' if v < 0 else '#059669' for v in values] fig = go.Figure(go.Bar( x=values, y=names, orientation='h', marker_color=colors, text=[f"{v:.2%}" for v in values], textposition='outside', hovertemplate='<b>%{y}</b><br>Contribution: %{x:.2%}<extra></extra>', )) fig.update_layout( title="Factor Contributions to Strategy Returns", xaxis_title="Contribution", xaxis={'tickformat': '.0%'}, template='plotly_white', height=max(350, len(values) * 45 + 100), autosize=True, margin={'l': 180, 'r': 80, 't': 60, 'b': 40}, ) return fig
[docs] def create_brinson_fachler_chart( brinson_fachler_indexes: pa.Table, date_column: str = "date", ) -> go.Figure: """Create the 3-panel Brinson-Fachler attribution chart. Panels: 1. Strategy vs Benchmark Returns 2. Cumulative Alpha 3. Allocation, Selection, Interaction Effects """ dates = brinson_fachler_indexes.column(date_column).to_pylist() all_cols = _get_non_date_columns(brinson_fachler_indexes, date_column) fig = make_subplots( rows=3, cols=1, subplot_titles=( "Strategy vs Benchmark Returns", "Cumulative Alpha", "Allocation, Selection and Interaction Effects", ), shared_xaxes=True, vertical_spacing=0.08, ) for i, col in enumerate(['portfolio_returns', 'benchmark_returns']): if col in all_cols: fig.add_trace( go.Scatter( x=dates, y=brinson_fachler_indexes.column(col).to_pylist(), mode='lines', name=col, line={'color': CHART_COLORS[i], 'width': 2}, ), row=1, col=1, ) if 'alpha' in all_cols: fig.add_trace( go.Scatter( x=dates, y=brinson_fachler_indexes.column('alpha').to_pylist(), mode='lines', name='alpha', line={'color': CHART_COLORS[2], 'width': 2}, ), row=2, col=1, ) for i, col in enumerate(['allocation', 'selection', 'interaction']): if col in all_cols: fig.add_trace( go.Scatter( x=dates, y=brinson_fachler_indexes.column(col).to_pylist(), mode='lines', name=col, line={'color': CHART_COLORS[i + 3], 'width': 2}, ), row=3, col=1, ) for row in range(1, 4): fig.add_hline(y=0, line_dash="solid", line_color="black", line_width=1, row=row, col=1) fig.update_yaxes(tickformat='.2%', row=row, col=1) fig.update_layout( height=900, template='plotly_white', hovermode='x unified', legend={ 'orientation': 'v', 'yanchor': 'top', 'y': 1, 'xanchor': 'left', 'x': 1.02, 'font': {'size': 10, 'family': 'Segoe UI'}, }, margin={'l': 60, 'r': 160, 't': 60, 'b': 40}, ) return fig
[docs] def create_returns_distribution( portfolio_returns: pa.Table, date_column: str = "date", value_column: str = "portfolio_returns", ) -> go.Figure: """Create returns distribution analysis (histogram + time series).""" clean_dates, clean_returns = _extract_clean_series( portfolio_returns, date_column, value_column, ) fig = make_subplots( rows=1, cols=2, subplot_titles=('Returns Distribution', 'Returns Over Time'), column_widths=[0.4, 0.6], ) fig.add_trace( go.Histogram( x=clean_returns, nbinsx=50, name='Returns Distribution', marker={'color': PROFESSIONAL_COLORS["kaxanuk_azul"], 'opacity': 0.7}, hovertemplate='Return: %{x:.2%}<br>Count: %{y}<extra></extra>', ), row=1, col=1, ) fig.add_trace( go.Scatter( x=clean_dates, y=clean_returns, mode='lines', name='Daily Returns', line={'color': PROFESSIONAL_COLORS["kaxanuk_azul"], 'width': 1}, hovertemplate='Date: %{x}<br>Return: %{y:.2%}<extra></extra>', ), row=1, col=2, ) fig.add_hline(y=0, line_dash="dash", line_color="red", row=1, col=2) fig.update_layout( height=400, template='plotly_white', showlegend=False, ) fig.update_xaxes(title_text="Return", row=1, col=1) fig.update_yaxes(title_text="Frequency", row=1, col=1) fig.update_xaxes(title_text="Date", row=1, col=2) fig.update_yaxes(title_text="Return", row=1, col=2, tickformat='.1%') return fig
[docs] def create_drawdown_chart( portfolio_returns: pa.Table, date_column: str = "date", value_column: str = "portfolio_returns", ) -> go.Figure: """Create drawdown analysis chart. Computes cumulative returns, running max, and drawdown entirely with Python lists (no pandas). """ clean_dates, clean_returns = _extract_clean_series( portfolio_returns, date_column, value_column, ) # Cumulative product: (1 + r_0) * (1 + r_1) * ... cumulative = [] cum_val = 1.0 for r in clean_returns: cum_val *= (1.0 + r) cumulative.append(cum_val) # Running (expanding) max running_max = [] max_val = float('-inf') for c in cumulative: max_val = max(max_val, c) running_max.append(max_val) # Drawdown = cumulative / running_max - 1 drawdown = [ (c / m - 1.0) if m != 0 else 0.0 for c, m in zip(cumulative, running_max, strict=False) ] fig = go.Figure() fig.add_trace(go.Scatter( x=clean_dates, y=drawdown, fill='tozeroy', fillcolor='rgba(255, 0, 0, 0.3)', line={'color': 'red', 'width': 2}, name='Portfolio Drawdown', hovertemplate='Date: %{x}<br>Drawdown: %{y:.2%}<extra></extra>', )) fig.add_hline(y=0, line_dash="solid", line_color="black", line_width=1) fig.update_layout( title="Drawdown Analysis", xaxis_title="Date", yaxis_title="Drawdown", yaxis={'tickformat': '.1%'}, template='plotly_white', height=400, legend={ 'orientation': 'h', 'yanchor': 'bottom', 'y': 1.02, 'xanchor': 'right', 'x': 1, }, ) return fig