.. _excel_report: The Excel Report ================ Every run of ``main()`` (and so of ``kaxanuk.attribution_analysis run``) writes one Excel workbook that explains the attribution in plain English, next to the tables it comes from. It looks like the KaxaNuk Backtest Engine's report, so a backtest (step 5) and its attribution (step 6) read alike. Where it is written ------------------- The workbook goes to the ``output_directory`` row of the parameters file (``Output`` when the row is missing or blank, relative to the folder you run from; it is created when missing). Its name is ``{portfolio_file_name}_{UTC time, %Y%m%d%H%M%S}_attribution_results.xlsx``, e.g. ``portfolio_20261020153012_attribution_results.xlsx``. A report never overwrites another: a second one in the same second gets ``_2`` before the extension. The report is written before the dashboard starts or the plots open. Nothing else is written: the charts are native Excel charts, not image files, and nothing is left in the working directory. (openpyxl, which writes the workbook, stages each sheet in a uniquely named file of the system's temporary folder and deletes it as it goes.) A workbook that fails while it is being saved is removed. ``main()`` writes it through its output handlers (see :ref:`output_handlers_ref`): by default an ``ExcelOutputHandler`` built from the configuration. ``run_attribution()`` never writes a file. To get the report of its result, or of a Backtest-Engine run, use the handler or ``write_excel_report()`` yourself (see :ref:`excel_report_from_python`). What it holds ------------- .. list-table:: :header-rows: 1 :widths: 25 75 * - Sheet - Contents * - ``summary`` - The report name; for a Backtest-Engine run, a line saying the results are hypothetical. The **headline figures** across the top: portfolio, benchmark and active return, tracking error, information ratio and maximum drawdown. The window, the methods and the benchmark. **Key findings**: sentences generated from the numbers, starting with the bottom line (portfolio against benchmark, total and a year), then the active risk (tracking error, information ratio, hit rate), how allocation, selection and interaction split the active return, which is largest and what selection comes to with interaction, the best and the worst calendar year, how much of the return the factors explain against the idiosyncratic residual, the largest positive factor and the largest drag, the active factor split and, for a Backtest-Engine run, the costs. Then the tables: performance statistics of the portfolio and the benchmark side by side with the relative ones; the cumulative Brinson-Fachler effects with selection including interaction; **Brinson-Fachler by calendar year**; the factor model's cumulative split and **by calendar year**; the contribution and share of each factor, **grouped into style and industry factors** with subtotals; the active factor model; the implementation costs; and a linked table of contents. Four native charts: the **bridge** from benchmark to portfolio return, the cumulative portfolio and benchmark return, the cumulative effects over time, and the factors ranked by contribution, gains and losses coloured apart. * - ``checks`` - The run's parameters (Brinson-Fachler formula, linking and segments, factor-model linking, the factor and return-component files attributed, the window); the **methodology**, as the CFA Institute's draft guide on attribution reporting lists it (holdings-based or hypothetical, gross of costs, daily periods and how they are linked, arithmetic active return, where the interaction and any residual go, the benchmark, the factor model, annualization); and every warning or note the run logged (``AttributionResult.diagnostics``), each with what happened, how many dates or rows it concerns, the first date and a code to find it in the logs. The note every run logs (its first date has no return) reads **Expected**, not Warning. * - ``how_to_read`` - Short definitions of the effects, the bridge chart, calendar years, tracking error, information ratio, beta, hit rate, factor contribution, the idiosyncratic residual, the return components, shares and ``n/m``, linking (Carino, GRAP, none), the active factor model, the holdings benchmark, the implementation table, VaR and the colours; and each sheet, linked. * - ``bf_per_period``, ``bf_cumulative``, ``bf_by_year`` - Brinson-Fachler ``per_period``, ``cumulative`` and ``by_year``. * - ``factor_contributions``, ``factor_cumulative``, ``factor_totals``, ``factor_by_year``, ``factor_daily_shares``, ``factor_shares`` - The factor model's ``contributions``, ``cumulative_contributions``, ``cumulative_totals``, ``by_year``, ``daily_shares`` and ``cumulative_shares``. * - ``active_returns``, ``active_contributions``, ``active_cumulative``, ``active_totals`` - The active factor model's tables, when ``active_factor_model`` is on. * - ``implementation`` - The Backtest-Engine implementation table, for a Backtest-Engine run. * - ``chart_data`` (hidden) - The values the bridge and the factor charts draw. Every data sheet is an Excel table with filters and frozen headers; every sheet links back to the summary, prints landscape one page wide with the report name and page number in the footer, and the workbook's properties name the report. The per-date, per-segment Brinson-Fachler ``details`` are not written: dates times segments can exceed Excel's 1,048,576 rows. Read them from ``result.brinson_fachler.details``. How to read the numbers ----------------------- - **The report recomputes no attribution number.** Every number on the ``summary`` is a value of the result: the last row of a cumulative table, a row of a ``by_year`` table, a share of ``cumulative_shares``, a summary, benchmark or relative statistic. The report adds only labelled presentation sums: each factor group's subtotal, selection plus interaction, the bridge's residual (shown only when it would not read 0.00%), and the implementation block (engine and holdings returns compounded, costs summed over the dates). - **Signs and colours.** Effects, contributions and active returns carry their sign (+0.50%, -1.20%) and a light blue or vermilion fill, colours told apart with any colour vision; values that read 0.00% stay unfilled. Returns themselves carry no forced sign. - **Shares.** A share divides by the cumulative return, so it is shown as ``n/m`` (not meaningful) when that return is under 1% in size or a share would exceed 200%, with a note; the contributions are always shown. - **Calendar years** are linked on their own dates, so a year's effects add up to its active return; years compound into the whole window rather than add up to it. - **The sentences cite the tables next to them**, rounded as the tables show them. A sentence is written only when the result has its numbers, and its wording follows the linking: with ``carino`` or ``grap`` the Brinson-Fachler returns are compounded; with ``none`` (and with the legacy formula, which is never linked) they are sums of the daily values, and the sentence says so. - **Number formats are declared, not guessed.** Returns and effects are percentages (two decimals, three for daily values); the Sharpe ratio, skewness and kurtosis are ratios; dates are real Excel dates. Null and NaN values are empty cells. - **Precision.** Values are written with 16 significant digits; Excel itself keeps 15. - **Linking none.** Each factor's cumulative contribution is then a running sum over every attribution date, including dates without a portfolio return (such as the first price date), while the shares and the totals cover only dates with one; the ``summary`` notes it under the table. .. _excel_report_from_python: From Python ----------- ``ExcelOutputHandler(output_directory, *, name="Portfolio", benchmark_name=None)`` writes the workbook of any ``AttributionResult`` and returns its path; ``write_excel_report(result, output_directory, *, name, benchmark_name)`` does the same as a function: .. code-block:: python from kaxanuk.attribution_analysis import run_attribution, run_attribution_from_backtest, write_excel_report from kaxanuk.attribution_analysis.output_handlers import ExcelOutputHandler result = run_attribution(configuration) handler = ExcelOutputHandler("Output", name="Global Equity", benchmark_name="MSCI World") path = handler.output_result(result=result) # A Backtest-Engine run (see "From a Backtest-Engine run"): the report adds the # implementation sheet and the sentence on costs result = run_attribution_from_backtest( backtest_result, bundle, settings=settings, factor_returns_directory="Input/Factor_Models", ) write_excel_report(result, "Output", name="liquid-golden-cross") ``name`` is the summary's title and the start of the file name (characters a file name cannot hold become ``_``). ``main()`` takes the same handlers: ``main(configuration, ..., output_handlers=[handler])`` writes with them instead of the default one, and ``output_handlers=()`` writes nothing. A handler of your own (another format, another destination) subclasses ``OutputHandlerInterface`` and implements ``output_result(*, result)``. **Large runs.** openpyxl writes every cell itself, through ``lxml`` (a dependency of this package): a ten-year daily run with 17 factors (about 180,000 cells) takes about 8 seconds to build and save, against more than 20 seconds with openpyxl's pure-Python writer.