{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "66b1484c",
      "metadata": {},
      "source": [
        "# Factor Attribution\n",
        "\n",
        "In this tutorial we are going to show how to run a factor-based attribution (return and risk) using the Reporting API. This API is currently under active development.\n",
        "\n",
        "The steps are as follows:\n",
        "- Uploading a set of portfolios\n",
        "- Organizing the portfolios into a hierarchy\n",
        "- Creating a factor model\n",
        "- Running a factor return attribution report\n",
        "- Pulling out different aggregations and drill-downs\n",
        "- Running a factor risk attribution report"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "b1275c1e",
      "metadata": {},
      "source": [
        "## Imports & Setup\n",
        "\n",
        "For this tutorial notebook, you will need to import the following packages."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "id": "9e479577",
      "metadata": {},
      "outputs": [],
      "source": [
        "import datetime as dt\n",
        "import polars as pl\n",
        "\n",
        "from bayesline.apiclient import BayeslineApiClient\n",
        "from bayesline.api.equity import (\n",
        "    FactorRiskModelSettings,\n",
        "    UniverseSettings,\n",
        "    ExposureSettings,\n",
        "    ContinuousExposureGroupSettings,\n",
        "    CategoricalExposureGroupSettings,\n",
        "    ModelConstructionSettings,\n",
        "    PortfolioHierarchySettings,\n",
        "    ReturnAttributionReportSettings,\n",
        "    XSRReportSettings,\n",
        ")"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "a8b965fb",
      "metadata": {},
      "source": [
        "We will also need to have a Bayesline API client configured."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "4652c791",
      "metadata": {
        "tags": [
          "skip-execution"
        ]
      },
      "outputs": [],
      "source": [
        "bln = BayeslineApiClient.new_client(\n",
        "    endpoint=\"https://[ENDPOINT]\",\n",
        "    api_key=\"[API-KEY]\",\n",
        ")"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "f1584f6d",
      "metadata": {},
      "source": [
        "We will first upload a portfolio and set a hierarchy. The steps followed here are the same as in the [Portfolio Hierarchies Tutorial](https://docs.bayesline.com/0.24.2/notebooks/tutorial_portfolio_hierarchies.html)."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "id": "cd7f4d11",
      "metadata": {},
      "outputs": [],
      "source": [
        "uploader = bln.equity.uploaders.get_data_type(\"portfolios\").create_or_replace_dataset(\"portfolio-attribution-demo\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 4,
      "id": "fd939dd3",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/html": [
              "<div><style>\n",
              ".dataframe > thead > tr,\n",
              ".dataframe > tbody > tr {\n",
              "  text-align: right;\n",
              "  white-space: pre-wrap;\n",
              "}\n",
              "</style>\n",
              "<small>shape: (20, 7)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>portfolio_id</th><th>asset_id</th><th>asset_id_type</th><th>date</th><th>currency</th><th>share_qty</th><th>nav</th></tr><tr><td>str</td><td>str</td><td>str</td><td>date</td><td>str</td><td>f64</td><td>f64</td></tr></thead><tbody><tr><td>&quot;AGTHX&quot;</td><td>&quot;02079K305&quot;</td><td>&quot;cusip9&quot;</td><td>2026-01-01</td><td>null</td><td>null</td><td>0.5</td></tr><tr><td>&quot;AGTHX&quot;</td><td>&quot;02079K305&quot;</td><td>&quot;cusip9&quot;</td><td>2026-01-31</td><td>null</td><td>null</td><td>0.55</td></tr><tr><td>&quot;AGTHX&quot;</td><td>&quot;2588173&quot;</td><td>&quot;sedol7&quot;</td><td>2026-01-01</td><td>null</td><td>null</td><td>0.5</td></tr><tr><td>&quot;AGTHX&quot;</td><td>&quot;2588173&quot;</td><td>&quot;sedol7&quot;</td><td>2026-01-31</td><td>null</td><td>null</td><td>0.45</td></tr><tr><td>&quot;FCNTX&quot;</td><td>&quot;67066G10&quot;</td><td>&quot;cusip8&quot;</td><td>2026-01-01</td><td>null</td><td>null</td><td>1.0</td></tr><tr><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td></tr><tr><td>&quot;SPX&quot;</td><td>&quot;2588173&quot;</td><td>&quot;sedol7&quot;</td><td>2026-01-31</td><td>null</td><td>null</td><td>0.2</td></tr><tr><td>&quot;SPX&quot;</td><td>&quot;67066G10&quot;</td><td>&quot;cusip8&quot;</td><td>2026-01-01</td><td>null</td><td>null</td><td>0.3</td></tr><tr><td>&quot;SPX&quot;</td><td>&quot;67066G10&quot;</td><td>&quot;cusip8&quot;</td><td>2026-01-31</td><td>null</td><td>null</td><td>0.25</td></tr><tr><td>&quot;SPX&quot;</td><td>&quot;85371710&quot;</td><td>&quot;cusip8&quot;</td><td>2026-01-01</td><td>null</td><td>null</td><td>0.3</td></tr><tr><td>&quot;SPX&quot;</td><td>&quot;85371710&quot;</td><td>&quot;cusip8&quot;</td><td>2026-01-31</td><td>null</td><td>null</td><td>0.25</td></tr></tbody></table></div>"
            ],
            "text/plain": [
              "shape: (20, 7)\n",
              "┌──────────────┬───────────┬───────────────┬────────────┬──────────┬───────────┬──────┐\n",
              "│ portfolio_id ┆ asset_id  ┆ asset_id_type ┆ date       ┆ currency ┆ share_qty ┆ nav  │\n",
              "│ ---          ┆ ---       ┆ ---           ┆ ---        ┆ ---      ┆ ---       ┆ ---  │\n",
              "│ str          ┆ str       ┆ str           ┆ date       ┆ str      ┆ f64       ┆ f64  │\n",
              "╞══════════════╪═══════════╪═══════════════╪════════════╪══════════╪═══════════╪══════╡\n",
              "│ AGTHX        ┆ 02079K305 ┆ cusip9        ┆ 2026-01-01 ┆ null     ┆ null      ┆ 0.5  │\n",
              "│ AGTHX        ┆ 02079K305 ┆ cusip9        ┆ 2026-01-31 ┆ null     ┆ null      ┆ 0.55 │\n",
              "│ AGTHX        ┆ 2588173   ┆ sedol7        ┆ 2026-01-01 ┆ null     ┆ null      ┆ 0.5  │\n",
              "│ AGTHX        ┆ 2588173   ┆ sedol7        ┆ 2026-01-31 ┆ null     ┆ null      ┆ 0.45 │\n",
              "│ FCNTX        ┆ 67066G10  ┆ cusip8        ┆ 2026-01-01 ┆ null     ┆ null      ┆ 1.0  │\n",
              "│ …            ┆ …         ┆ …             ┆ …          ┆ …        ┆ …         ┆ …    │\n",
              "│ SPX          ┆ 2588173   ┆ sedol7        ┆ 2026-01-31 ┆ null     ┆ null      ┆ 0.2  │\n",
              "│ SPX          ┆ 67066G10  ┆ cusip8        ┆ 2026-01-01 ┆ null     ┆ null      ┆ 0.3  │\n",
              "│ SPX          ┆ 67066G10  ┆ cusip8        ┆ 2026-01-31 ┆ null     ┆ null      ┆ 0.25 │\n",
              "│ SPX          ┆ 85371710  ┆ cusip8        ┆ 2026-01-01 ┆ null     ┆ null      ┆ 0.3  │\n",
              "│ SPX          ┆ 85371710  ┆ cusip8        ┆ 2026-01-31 ┆ null     ┆ null      ┆ 0.25 │\n",
              "└──────────────┴───────────┴───────────────┴────────────┴──────────┴───────────┴──────┘"
            ]
          },
          "execution_count": 4,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "portfolio_df = pl.DataFrame({\n",
        "    \"portfolio_id\": [\n",
        "        \"AGTHX\", \"AGTHX\", \"AGTHX\", \"AGTHX\",\n",
        "        \"FCNTX\", \"FCNTX\",\n",
        "        \"VADGX\", \"VADGX\", \"VADGX\", \"VADGX\", \"VADGX\", \"VADGX\",\n",
        "        \"SPX\", \"SPX\", \"SPX\", \"SPX\", \"SPX\", \"SPX\", \"SPX\", \"SPX\",\n",
        "    ],\n",
        "    \"asset_id\": [\n",
        "        # AGTHX\n",
        "        \"02079K305\", \"02079K305\", \n",
        "        \"2588173\", \"2588173\",\n",
        "\n",
        "        # FCNTX\n",
        "        \"67066G10\", \"67066G10\",\n",
        "\n",
        "        # VADGX\n",
        "        \"02079K305\", \"02079K305\", \n",
        "        \"2588173\", \"2588173\", \n",
        "        \"67066G10\", \"67066G10\",\n",
        "\n",
        "        # SPX\n",
        "        \"02079K305\", \"02079K305\", \n",
        "        \"2588173\", \"2588173\",\n",
        "        \"67066G10\", \"67066G10\",\n",
        "        \"85371710\", \"85371710\",\n",
        "\n",
        "    ],  \n",
        "    \"asset_id_type\": [\n",
        "        # AGTHX\n",
        "        \"cusip9\", \"cusip9\", \"sedol7\", \"sedol7\",\n",
        "\n",
        "        # FCNTX\n",
        "        \"cusip8\", \"cusip8\",\n",
        "\n",
        "        # VADGX\n",
        "        \"cusip9\", \"cusip9\", \"sedol7\", \"sedol7\", \"cusip8\", \"cusip8\",\n",
        "\n",
        "        # SPX\n",
        "        \"cusip9\", \"cusip9\", \"sedol7\", \"sedol7\", \"cusip8\", \"cusip8\", \"cusip8\", \"cusip8\",\n",
        "        \n",
        "    ],\n",
        "    \"date\": [\n",
        "        # AGTHX\n",
        "        dt.date(2026, 1, 1), dt.date(2026, 1, 31), \n",
        "        dt.date(2026, 1, 1), dt.date(2026, 1, 31),\n",
        "\n",
        "        # FCNTX\n",
        "        dt.date(2026, 1, 1), dt.date(2026, 1, 31),\n",
        "\n",
        "        # VADGX\n",
        "        dt.date(2026, 1, 1), dt.date(2026, 1, 31), \n",
        "        dt.date(2026, 1, 1), dt.date(2026, 1, 31), \n",
        "        dt.date(2026, 1, 1), dt.date(2026, 1, 31), \n",
        "        \n",
        "        # SPX\n",
        "        dt.date(2026, 1, 1), dt.date(2026, 1, 31), \n",
        "        dt.date(2026, 1, 1), dt.date(2026, 1, 31), \n",
        "        dt.date(2026, 1, 1), dt.date(2026, 1, 31), \n",
        "        dt.date(2026, 1, 1), dt.date(2026, 1, 31), \n",
        "    ],\n",
        "    \"currency\": [None] * 20,\n",
        "    \"share_qty\": [None] * 20,\n",
        "    \"nav\": [\n",
        "        # AGTHX\n",
        "        0.5, 0.55, \n",
        "        0.5, 0.45,\n",
        "\n",
        "        # FCNTX\n",
        "        1.0, 1.0,\n",
        "\n",
        "        # VADGX\n",
        "        0.3, 0.5, \n",
        "        0.4, 0.2,\n",
        "        0.3, 0.25,\n",
        "\n",
        "        # SPX\n",
        "        0.3, 0.3, \n",
        "        0.4, 0.2,\n",
        "        0.3, 0.25,\n",
        "        0.3, 0.25,\n",
        "    ],\n",
        "}).with_columns(pl.col(\"currency\").cast(pl.String), pl.col(\"share_qty\").cast(pl.Float64))\n",
        "\n",
        "portfolio_df"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 5,
      "id": "341987db",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "UploadCommitResult(version=1, committed_names=[])"
            ]
          },
          "execution_count": 5,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "uploader.fast_commit(portfolio_df, mode=\"append\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 6,
      "id": "931fadfa",
      "metadata": {
        "lines_to_next_cell": 2
      },
      "outputs": [
        {
          "data": {
            "text/html": [
              "<div><style>\n",
              ".dataframe > thead > tr,\n",
              ".dataframe > tbody > tr {\n",
              "  text-align: right;\n",
              "  white-space: pre-wrap;\n",
              "}\n",
              "</style>\n",
              "<small>shape: (4, 5)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>fund</th><th>book</th><th>ticker</th><th>portfolio_id</th><th>benchmark_id</th></tr><tr><td>str</td><td>str</td><td>str</td><td>str</td><td>null</td></tr></thead><tbody><tr><td>&quot;some_fund&quot;</td><td>&quot;book_1&quot;</td><td>&quot;AGTHX&quot;</td><td>&quot;AGTHX&quot;</td><td>null</td></tr><tr><td>&quot;some_fund&quot;</td><td>&quot;book_1&quot;</td><td>&quot;FCNTX&quot;</td><td>&quot;FCNTX&quot;</td><td>null</td></tr><tr><td>&quot;some_fund&quot;</td><td>&quot;book_2&quot;</td><td>&quot;VADGX&quot;</td><td>&quot;VADGX&quot;</td><td>null</td></tr><tr><td>&quot;some_fund&quot;</td><td>&quot;book_2&quot;</td><td>&quot;SPX&quot;</td><td>&quot;SPX&quot;</td><td>null</td></tr></tbody></table></div>"
            ],
            "text/plain": [
              "shape: (4, 5)\n",
              "┌───────────┬────────┬────────┬──────────────┬──────────────┐\n",
              "│ fund      ┆ book   ┆ ticker ┆ portfolio_id ┆ benchmark_id │\n",
              "│ ---       ┆ ---    ┆ ---    ┆ ---          ┆ ---          │\n",
              "│ str       ┆ str    ┆ str    ┆ str          ┆ null         │\n",
              "╞═══════════╪════════╪════════╪══════════════╪══════════════╡\n",
              "│ some_fund ┆ book_1 ┆ AGTHX  ┆ AGTHX        ┆ null         │\n",
              "│ some_fund ┆ book_1 ┆ FCNTX  ┆ FCNTX        ┆ null         │\n",
              "│ some_fund ┆ book_2 ┆ VADGX  ┆ VADGX        ┆ null         │\n",
              "│ some_fund ┆ book_2 ┆ SPX    ┆ SPX          ┆ null         │\n",
              "└───────────┴────────┴────────┴──────────────┴──────────────┘"
            ]
          },
          "execution_count": 6,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "portfolio_hierarchy_df = pl.DataFrame(\n",
        "    {\n",
        "        \"fund\": [\"some_fund\", \"some_fund\", \"some_fund\", \"some_fund\"],\n",
        "        \"book\": [\"book_1\", \"book_1\", \"book_2\", \"book_2\"],\n",
        "        \"ticker\": [\"AGTHX\", \"FCNTX\", \"VADGX\", \"SPX\"],\n",
        "        \"portfolio_id\": [\"AGTHX\", \"FCNTX\", \"VADGX\", \"SPX\"],\n",
        "    }\n",
        ")\n",
        "portfoliohierarchy_settings = PortfolioHierarchySettings.from_polars(\n",
        "    portfolio_hierarchy_df, \n",
        "    portfolio_source=\"portfolio-attribution-demo\",\n",
        ")\n",
        "portfoliohierarchy_settings.to_polars()"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "e6855e85",
      "metadata": {},
      "source": [
        "Now that we have the portfolio hiearachy in place, we define a basic factor model."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 7,
      "id": "8cb72a14",
      "metadata": {},
      "outputs": [],
      "source": [
        "factorriskmodel_settings = FactorRiskModelSettings(\n",
        "    universe=UniverseSettings(),\n",
        "    exposures=ExposureSettings(\n",
        "        exposures=[\n",
        "            ContinuousExposureGroupSettings(hierarchy=\"market\"),\n",
        "            CategoricalExposureGroupSettings(hierarchy=\"trbc\"),\n",
        "            ContinuousExposureGroupSettings(hierarchy=\"style\"),\n",
        "        ]\n",
        "    ),\n",
        "    modelconstruction=ModelConstructionSettings(\n",
        "        estimation_universe=None,\n",
        "        zero_sum_constraints={\"trbc\": \"mcap_weighted\"}\n",
        "    ),\n",
        ")"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "a5f90576",
      "metadata": {},
      "source": [
        "We then define and load the report. We have different multi-period aggregation settings available, which we do not go into here."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 8,
      "id": "8f5661e9",
      "metadata": {},
      "outputs": [],
      "source": [
        "report_settings = ReturnAttributionReportSettings(\n",
        "    portfolio_hierarchy_settings=portfoliohierarchy_settings,\n",
        "    factor_model_settings=factorriskmodel_settings,\n",
        "    normalize_holdings=False, \n",
        "    return_aggregation_type=\"arithmetic\",\n",
        ")\n",
        "report_engine = bln.equity.reports.load(\n",
        "    report_settings.with_dataset(\"bayesline/Bayesline-US-All-1y\")\n",
        ")\n",
        "report = report_engine.calculate(start_date=\"2026-01-05\", end_date=\"2026-01-30\")"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "26124e9e",
      "metadata": {},
      "source": [
        "Let's first look at the return attribution from all sources at the ticker level (recall that ticker is defined in the portfolio hierarchy). For a specific ticker, we want to know the return attribution to different assets at each date."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 9,
      "id": "b8a7515a",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/html": [
              "<div><style>\n",
              ".dataframe > thead > tr,\n",
              ".dataframe > tbody > tr {\n",
              "  text-align: right;\n",
              "  white-space: pre-wrap;\n",
              "}\n",
              "</style>\n",
              "<small>shape: (76, 4)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>ticker</th><th>date</th><th>input_asset_id</th><th>PnL</th></tr><tr><td>str</td><td>date</td><td>str</td><td>f32</td></tr></thead><tbody><tr><td>&quot;AGTHX&quot;</td><td>2026-01-05</td><td>&quot;02079K305&quot;</td><td>0.002221</td></tr><tr><td>&quot;AGTHX&quot;</td><td>2026-01-05</td><td>&quot;2588173&quot;</td><td>-0.000093</td></tr><tr><td>&quot;AGTHX&quot;</td><td>2026-01-05</td><td>&quot;67066G10&quot;</td><td>0.0</td></tr><tr><td>&quot;AGTHX&quot;</td><td>2026-01-05</td><td>&quot;85371710&quot;</td><td>0.0</td></tr><tr><td>&quot;AGTHX&quot;</td><td>2026-01-06</td><td>&quot;02079K305&quot;</td><td>-0.003514</td></tr><tr><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td></tr><tr><td>&quot;AGTHX&quot;</td><td>2026-01-29</td><td>&quot;85371710&quot;</td><td>0.0</td></tr><tr><td>&quot;AGTHX&quot;</td><td>2026-01-30</td><td>&quot;02079K305&quot;</td><td>-0.000399</td></tr><tr><td>&quot;AGTHX&quot;</td><td>2026-01-30</td><td>&quot;2588173&quot;</td><td>-0.003319</td></tr><tr><td>&quot;AGTHX&quot;</td><td>2026-01-30</td><td>&quot;67066G10&quot;</td><td>0.0</td></tr><tr><td>&quot;AGTHX&quot;</td><td>2026-01-30</td><td>&quot;85371710&quot;</td><td>0.0</td></tr></tbody></table></div>"
            ],
            "text/plain": [
              "shape: (76, 4)\n",
              "┌────────┬────────────┬────────────────┬───────────┐\n",
              "│ ticker ┆ date       ┆ input_asset_id ┆ PnL       │\n",
              "│ ---    ┆ ---        ┆ ---            ┆ ---       │\n",
              "│ str    ┆ date       ┆ str            ┆ f32       │\n",
              "╞════════╪════════════╪════════════════╪═══════════╡\n",
              "│ AGTHX  ┆ 2026-01-05 ┆ 02079K305      ┆ 0.002221  │\n",
              "│ AGTHX  ┆ 2026-01-05 ┆ 2588173        ┆ -0.000093 │\n",
              "│ AGTHX  ┆ 2026-01-05 ┆ 67066G10       ┆ 0.0       │\n",
              "│ AGTHX  ┆ 2026-01-05 ┆ 85371710       ┆ 0.0       │\n",
              "│ AGTHX  ┆ 2026-01-06 ┆ 02079K305      ┆ -0.003514 │\n",
              "│ …      ┆ …          ┆ …              ┆ …         │\n",
              "│ AGTHX  ┆ 2026-01-29 ┆ 85371710       ┆ 0.0       │\n",
              "│ AGTHX  ┆ 2026-01-30 ┆ 02079K305      ┆ -0.000399 │\n",
              "│ AGTHX  ┆ 2026-01-30 ┆ 2588173        ┆ -0.003319 │\n",
              "│ AGTHX  ┆ 2026-01-30 ┆ 67066G10       ┆ 0.0       │\n",
              "│ AGTHX  ┆ 2026-01-30 ┆ 85371710       ┆ 0.0       │\n",
              "└────────┴────────────┴────────────────┴───────────┘"
            ]
          },
          "execution_count": 9,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# for a specific ticker, the return attribution across dates and assets\n",
        "(\n",
        "    report.accessor.get_data(\n",
        "        [(\"ticker\", \"AGTHX\")],  # filters: [ticker=AGTHX]\n",
        "        expand=(\"date\", \"input_asset_id\",), # combinations define the rows\n",
        "        value_cols=(\"PnL\",),  # exclude benchmark / active\n",
        "    )\n",
        "    .select(\"ticker\", \"date\", \"input_asset_id\", \"PnL\")\n",
        ")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 10,
      "id": "a5e03b9a",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/html": [
              "<div><style>\n",
              ".dataframe > thead > tr,\n",
              ".dataframe > tbody > tr {\n",
              "  text-align: right;\n",
              "  white-space: pre-wrap;\n",
              "}\n",
              "</style>\n",
              "<small>shape: (76, 4)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>ticker</th><th>date</th><th>input_asset_id</th><th>PnL</th></tr><tr><td>str</td><td>date</td><td>str</td><td>f32</td></tr></thead><tbody><tr><td>&quot;AGTHX&quot;</td><td>2026-01-05</td><td>&quot;02079K305&quot;</td><td>0.002221</td></tr><tr><td>&quot;AGTHX&quot;</td><td>2026-01-05</td><td>&quot;2588173&quot;</td><td>-0.000093</td></tr><tr><td>&quot;AGTHX&quot;</td><td>2026-01-05</td><td>&quot;67066G10&quot;</td><td>0.0</td></tr><tr><td>&quot;AGTHX&quot;</td><td>2026-01-05</td><td>&quot;85371710&quot;</td><td>0.0</td></tr><tr><td>&quot;AGTHX&quot;</td><td>2026-01-06</td><td>&quot;02079K305&quot;</td><td>-0.003514</td></tr><tr><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td></tr><tr><td>&quot;AGTHX&quot;</td><td>2026-01-29</td><td>&quot;85371710&quot;</td><td>0.0</td></tr><tr><td>&quot;AGTHX&quot;</td><td>2026-01-30</td><td>&quot;02079K305&quot;</td><td>-0.000399</td></tr><tr><td>&quot;AGTHX&quot;</td><td>2026-01-30</td><td>&quot;2588173&quot;</td><td>-0.003319</td></tr><tr><td>&quot;AGTHX&quot;</td><td>2026-01-30</td><td>&quot;67066G10&quot;</td><td>0.0</td></tr><tr><td>&quot;AGTHX&quot;</td><td>2026-01-30</td><td>&quot;85371710&quot;</td><td>0.0</td></tr></tbody></table></div>"
            ],
            "text/plain": [
              "shape: (76, 4)\n",
              "┌────────┬────────────┬────────────────┬───────────┐\n",
              "│ ticker ┆ date       ┆ input_asset_id ┆ PnL       │\n",
              "│ ---    ┆ ---        ┆ ---            ┆ ---       │\n",
              "│ str    ┆ date       ┆ str            ┆ f32       │\n",
              "╞════════╪════════════╪════════════════╪═══════════╡\n",
              "│ AGTHX  ┆ 2026-01-05 ┆ 02079K305      ┆ 0.002221  │\n",
              "│ AGTHX  ┆ 2026-01-05 ┆ 2588173        ┆ -0.000093 │\n",
              "│ AGTHX  ┆ 2026-01-05 ┆ 67066G10       ┆ 0.0       │\n",
              "│ AGTHX  ┆ 2026-01-05 ┆ 85371710       ┆ 0.0       │\n",
              "│ AGTHX  ┆ 2026-01-06 ┆ 02079K305      ┆ -0.003514 │\n",
              "│ …      ┆ …          ┆ …              ┆ …         │\n",
              "│ AGTHX  ┆ 2026-01-29 ┆ 85371710       ┆ 0.0       │\n",
              "│ AGTHX  ┆ 2026-01-30 ┆ 02079K305      ┆ -0.000399 │\n",
              "│ AGTHX  ┆ 2026-01-30 ┆ 2588173        ┆ -0.003319 │\n",
              "│ AGTHX  ┆ 2026-01-30 ┆ 67066G10       ┆ 0.0       │\n",
              "│ AGTHX  ┆ 2026-01-30 ┆ 85371710       ┆ 0.0       │\n",
              "└────────┴────────────┴────────────────┴───────────┘"
            ]
          },
          "execution_count": 10,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# for a specific ticker, the return attribution across dates and assets\n",
        "(\n",
        "    report.accessor.get_data(\n",
        "        [(\"ticker\", \"AGTHX\")],  # filters: [ticker=AGTHX]\n",
        "        expand=(\"date\", \"input_asset_id\",), # combinations define the rows\n",
        "        value_cols=(\"PnL\",),  # exclude benchmark / active\n",
        "    )\n",
        "    .select(\"ticker\", \"date\", \"input_asset_id\", \"PnL\")\n",
        ")"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "6950a24a",
      "metadata": {},
      "source": [
        "Using different settings, we can split out the returns across all sources (factors and idio) at a different level. Below we show the returns on a specific date, for a specific book, split out by factor."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 11,
      "id": "5d66f0d6",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/html": [
              "<div><style>\n",
              ".dataframe > thead > tr,\n",
              ".dataframe > tbody > tr {\n",
              "  text-align: right;\n",
              "  white-space: pre-wrap;\n",
              "}\n",
              "</style>\n",
              "<small>shape: (22, 6)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>date</th><th>book</th><th>type</th><th>factor_group</th><th>factor</th><th>PnL</th></tr><tr><td>date</td><td>str</td><td>str</td><td>str</td><td>str</td><td>f32</td></tr></thead><tbody><tr><td>2026-01-05</td><td>&quot;book_1&quot;</td><td>&quot;Factors&quot;</td><td>&quot;market&quot;</td><td>&quot;Market&quot;</td><td>0.014189</td></tr><tr><td>2026-01-05</td><td>&quot;book_1&quot;</td><td>&quot;Factors&quot;</td><td>&quot;style&quot;</td><td>&quot;Dividend&quot;</td><td>0.000737</td></tr><tr><td>2026-01-05</td><td>&quot;book_1&quot;</td><td>&quot;Factors&quot;</td><td>&quot;style&quot;</td><td>&quot;Growth&quot;</td><td>0.001491</td></tr><tr><td>2026-01-05</td><td>&quot;book_1&quot;</td><td>&quot;Factors&quot;</td><td>&quot;style&quot;</td><td>&quot;Leverage&quot;</td><td>-0.000592</td></tr><tr><td>2026-01-05</td><td>&quot;book_1&quot;</td><td>&quot;Factors&quot;</td><td>&quot;style&quot;</td><td>&quot;Momentum&quot;</td><td>-0.002769</td></tr><tr><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td></tr><tr><td>2026-01-05</td><td>&quot;book_1&quot;</td><td>&quot;Factors&quot;</td><td>&quot;trbc&quot;</td><td>&quot;Institutions, Associations &amp; O…</td><td>0.0</td></tr><tr><td>2026-01-05</td><td>&quot;book_1&quot;</td><td>&quot;Factors&quot;</td><td>&quot;trbc&quot;</td><td>&quot;Real Estate&quot;</td><td>0.0</td></tr><tr><td>2026-01-05</td><td>&quot;book_1&quot;</td><td>&quot;Factors&quot;</td><td>&quot;trbc&quot;</td><td>&quot;Technology&quot;</td><td>-0.005175</td></tr><tr><td>2026-01-05</td><td>&quot;book_1&quot;</td><td>&quot;Factors&quot;</td><td>&quot;trbc&quot;</td><td>&quot;Utilities&quot;</td><td>0.0</td></tr><tr><td>2026-01-05</td><td>&quot;book_1&quot;</td><td>&quot;Idiosyncratic&quot;</td><td>&quot;&quot;</td><td>&quot;&quot;</td><td>-0.022578</td></tr></tbody></table></div>"
            ],
            "text/plain": [
              "shape: (22, 6)\n",
              "┌────────────┬────────┬───────────────┬──────────────┬─────────────────────────────────┬───────────┐\n",
              "│ date       ┆ book   ┆ type          ┆ factor_group ┆ factor                          ┆ PnL       │\n",
              "│ ---        ┆ ---    ┆ ---           ┆ ---          ┆ ---                             ┆ ---       │\n",
              "│ date       ┆ str    ┆ str           ┆ str          ┆ str                             ┆ f32       │\n",
              "╞════════════╪════════╪═══════════════╪══════════════╪═════════════════════════════════╪═══════════╡\n",
              "│ 2026-01-05 ┆ book_1 ┆ Factors       ┆ market       ┆ Market                          ┆ 0.014189  │\n",
              "│ 2026-01-05 ┆ book_1 ┆ Factors       ┆ style        ┆ Dividend                        ┆ 0.000737  │\n",
              "│ 2026-01-05 ┆ book_1 ┆ Factors       ┆ style        ┆ Growth                          ┆ 0.001491  │\n",
              "│ 2026-01-05 ┆ book_1 ┆ Factors       ┆ style        ┆ Leverage                        ┆ -0.000592 │\n",
              "│ 2026-01-05 ┆ book_1 ┆ Factors       ┆ style        ┆ Momentum                        ┆ -0.002769 │\n",
              "│ …          ┆ …      ┆ …             ┆ …            ┆ …                               ┆ …         │\n",
              "│ 2026-01-05 ┆ book_1 ┆ Factors       ┆ trbc         ┆ Institutions, Associations & O… ┆ 0.0       │\n",
              "│ 2026-01-05 ┆ book_1 ┆ Factors       ┆ trbc         ┆ Real Estate                     ┆ 0.0       │\n",
              "│ 2026-01-05 ┆ book_1 ┆ Factors       ┆ trbc         ┆ Technology                      ┆ -0.005175 │\n",
              "│ 2026-01-05 ┆ book_1 ┆ Factors       ┆ trbc         ┆ Utilities                       ┆ 0.0       │\n",
              "│ 2026-01-05 ┆ book_1 ┆ Idiosyncratic ┆              ┆                                 ┆ -0.022578 │\n",
              "└────────────┴────────┴───────────────┴──────────────┴─────────────────────────────────┴───────────┘"
            ]
          },
          "execution_count": 11,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# for a specific book on a specific date, the factor return attribution across assets\n",
        "(\n",
        "    report.accessor.get_data(\n",
        "        [(\"date\", \"2026-01-05\"), (\"book\", \"book_1\")],  # filters\n",
        "        expand=(\"type\", \"factor_group\", \"factor\",),\n",
        "        value_cols=(\"PnL\",),\n",
        "    )\n",
        "    .select(\"date\", \"book\", \"type\", \"factor_group\", \"factor\", \"PnL\")\n",
        ")"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "1cb3efb2",
      "metadata": {},
      "source": [
        "We can drill down further by including the asset dimension. This decomposes the returns of the assets in a specific book, on a specific date."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 12,
      "id": "cc974505",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/html": [
              "<div><style>\n",
              ".dataframe > thead > tr,\n",
              ".dataframe > tbody > tr {\n",
              "  text-align: right;\n",
              "  white-space: pre-wrap;\n",
              "}\n",
              "</style>\n",
              "<small>shape: (88, 7)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>date</th><th>book</th><th>type</th><th>factor_group</th><th>factor</th><th>input_asset_id</th><th>PnL</th></tr><tr><td>date</td><td>str</td><td>str</td><td>str</td><td>str</td><td>str</td><td>f32</td></tr></thead><tbody><tr><td>2026-01-05</td><td>&quot;book_1&quot;</td><td>&quot;Factors&quot;</td><td>&quot;market&quot;</td><td>&quot;Market&quot;</td><td>&quot;02079K305&quot;</td><td>0.003563</td></tr><tr><td>2026-01-05</td><td>&quot;book_1&quot;</td><td>&quot;Factors&quot;</td><td>&quot;market&quot;</td><td>&quot;Market&quot;</td><td>&quot;2588173&quot;</td><td>0.00346</td></tr><tr><td>2026-01-05</td><td>&quot;book_1&quot;</td><td>&quot;Factors&quot;</td><td>&quot;market&quot;</td><td>&quot;Market&quot;</td><td>&quot;67066G10&quot;</td><td>0.007166</td></tr><tr><td>2026-01-05</td><td>&quot;book_1&quot;</td><td>&quot;Factors&quot;</td><td>&quot;market&quot;</td><td>&quot;Market&quot;</td><td>&quot;85371710&quot;</td><td>0.0</td></tr><tr><td>2026-01-05</td><td>&quot;book_1&quot;</td><td>&quot;Factors&quot;</td><td>&quot;style&quot;</td><td>&quot;Dividend&quot;</td><td>&quot;02079K305&quot;</td><td>0.000104</td></tr><tr><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td></tr><tr><td>2026-01-05</td><td>&quot;book_1&quot;</td><td>&quot;Factors&quot;</td><td>&quot;trbc&quot;</td><td>&quot;Utilities&quot;</td><td>&quot;85371710&quot;</td><td>0.0</td></tr><tr><td>2026-01-05</td><td>&quot;book_1&quot;</td><td>&quot;Idiosyncratic&quot;</td><td>&quot;&quot;</td><td>&quot;&quot;</td><td>&quot;02079K305&quot;</td><td>-0.002482</td></tr><tr><td>2026-01-05</td><td>&quot;book_1&quot;</td><td>&quot;Idiosyncratic&quot;</td><td>&quot;&quot;</td><td>&quot;&quot;</td><td>&quot;2588173&quot;</td><td>-0.002861</td></tr><tr><td>2026-01-05</td><td>&quot;book_1&quot;</td><td>&quot;Idiosyncratic&quot;</td><td>&quot;&quot;</td><td>&quot;&quot;</td><td>&quot;67066G10&quot;</td><td>-0.017234</td></tr><tr><td>2026-01-05</td><td>&quot;book_1&quot;</td><td>&quot;Idiosyncratic&quot;</td><td>&quot;&quot;</td><td>&quot;&quot;</td><td>&quot;85371710&quot;</td><td>0.0</td></tr></tbody></table></div>"
            ],
            "text/plain": [
              "shape: (88, 7)\n",
              "┌────────────┬────────┬───────────────┬──────────────┬───────────┬────────────────┬───────────┐\n",
              "│ date       ┆ book   ┆ type          ┆ factor_group ┆ factor    ┆ input_asset_id ┆ PnL       │\n",
              "│ ---        ┆ ---    ┆ ---           ┆ ---          ┆ ---       ┆ ---            ┆ ---       │\n",
              "│ date       ┆ str    ┆ str           ┆ str          ┆ str       ┆ str            ┆ f32       │\n",
              "╞════════════╪════════╪═══════════════╪══════════════╪═══════════╪════════════════╪═══════════╡\n",
              "│ 2026-01-05 ┆ book_1 ┆ Factors       ┆ market       ┆ Market    ┆ 02079K305      ┆ 0.003563  │\n",
              "│ 2026-01-05 ┆ book_1 ┆ Factors       ┆ market       ┆ Market    ┆ 2588173        ┆ 0.00346   │\n",
              "│ 2026-01-05 ┆ book_1 ┆ Factors       ┆ market       ┆ Market    ┆ 67066G10       ┆ 0.007166  │\n",
              "│ 2026-01-05 ┆ book_1 ┆ Factors       ┆ market       ┆ Market    ┆ 85371710       ┆ 0.0       │\n",
              "│ 2026-01-05 ┆ book_1 ┆ Factors       ┆ style        ┆ Dividend  ┆ 02079K305      ┆ 0.000104  │\n",
              "│ …          ┆ …      ┆ …             ┆ …            ┆ …         ┆ …              ┆ …         │\n",
              "│ 2026-01-05 ┆ book_1 ┆ Factors       ┆ trbc         ┆ Utilities ┆ 85371710       ┆ 0.0       │\n",
              "│ 2026-01-05 ┆ book_1 ┆ Idiosyncratic ┆              ┆           ┆ 02079K305      ┆ -0.002482 │\n",
              "│ 2026-01-05 ┆ book_1 ┆ Idiosyncratic ┆              ┆           ┆ 2588173        ┆ -0.002861 │\n",
              "│ 2026-01-05 ┆ book_1 ┆ Idiosyncratic ┆              ┆           ┆ 67066G10       ┆ -0.017234 │\n",
              "│ 2026-01-05 ┆ book_1 ┆ Idiosyncratic ┆              ┆           ┆ 85371710       ┆ 0.0       │\n",
              "└────────────┴────────┴───────────────┴──────────────┴───────────┴────────────────┴───────────┘"
            ]
          },
          "execution_count": 12,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# for a specific book on a specific date, the factor return attribution by asset\n",
        "(\n",
        "    report.accessor.get_data(\n",
        "        [(\"date\", \"2026-01-05\"), (\"book\", \"book_1\")],  # filters\n",
        "        expand=(\"type\", \"factor_group\", \"factor\", \"input_asset_id\"),\n",
        "        value_cols=(\"PnL\",),\n",
        "    )\n",
        "    .select(\"date\", \"book\", \"type\", \"factor_group\", \"factor\", \"input_asset_id\", \"PnL\")\n",
        ")"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "211b9b0c",
      "metadata": {},
      "source": [
        "By using the filters, we can also isolate the specific return attribution."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 13,
      "id": "0d57774e",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/html": [
              "<div><style>\n",
              ".dataframe > thead > tr,\n",
              ".dataframe > tbody > tr {\n",
              "  text-align: right;\n",
              "  white-space: pre-wrap;\n",
              "}\n",
              "</style>\n",
              "<small>shape: (76, 4)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>fund</th><th>date</th><th>input_asset_id</th><th>PnL</th></tr><tr><td>str</td><td>date</td><td>str</td><td>f32</td></tr></thead><tbody><tr><td>&quot;some_fund&quot;</td><td>2026-01-05</td><td>&quot;02079K305&quot;</td><td>-0.005461</td></tr><tr><td>&quot;some_fund&quot;</td><td>2026-01-05</td><td>&quot;2588173&quot;</td><td>-0.00744</td></tr><tr><td>&quot;some_fund&quot;</td><td>2026-01-05</td><td>&quot;67066G10&quot;</td><td>-0.027575</td></tr><tr><td>&quot;some_fund&quot;</td><td>2026-01-05</td><td>&quot;85371710&quot;</td><td>0.0</td></tr><tr><td>&quot;some_fund&quot;</td><td>2026-01-06</td><td>&quot;02079K305&quot;</td><td>-0.024126</td></tr><tr><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td></tr><tr><td>&quot;some_fund&quot;</td><td>2026-01-29</td><td>&quot;85371710&quot;</td><td>0.0</td></tr><tr><td>&quot;some_fund&quot;</td><td>2026-01-30</td><td>&quot;02079K305&quot;</td><td>0.026471</td></tr><tr><td>&quot;some_fund&quot;</td><td>2026-01-30</td><td>&quot;2588173&quot;</td><td>-0.00128</td></tr><tr><td>&quot;some_fund&quot;</td><td>2026-01-30</td><td>&quot;67066G10&quot;</td><td>0.023653</td></tr><tr><td>&quot;some_fund&quot;</td><td>2026-01-30</td><td>&quot;85371710&quot;</td><td>0.0</td></tr></tbody></table></div>"
            ],
            "text/plain": [
              "shape: (76, 4)\n",
              "┌───────────┬────────────┬────────────────┬───────────┐\n",
              "│ fund      ┆ date       ┆ input_asset_id ┆ PnL       │\n",
              "│ ---       ┆ ---        ┆ ---            ┆ ---       │\n",
              "│ str       ┆ date       ┆ str            ┆ f32       │\n",
              "╞═══════════╪════════════╪════════════════╪═══════════╡\n",
              "│ some_fund ┆ 2026-01-05 ┆ 02079K305      ┆ -0.005461 │\n",
              "│ some_fund ┆ 2026-01-05 ┆ 2588173        ┆ -0.00744  │\n",
              "│ some_fund ┆ 2026-01-05 ┆ 67066G10       ┆ -0.027575 │\n",
              "│ some_fund ┆ 2026-01-05 ┆ 85371710       ┆ 0.0       │\n",
              "│ some_fund ┆ 2026-01-06 ┆ 02079K305      ┆ -0.024126 │\n",
              "│ …         ┆ …          ┆ …              ┆ …         │\n",
              "│ some_fund ┆ 2026-01-29 ┆ 85371710       ┆ 0.0       │\n",
              "│ some_fund ┆ 2026-01-30 ┆ 02079K305      ┆ 0.026471  │\n",
              "│ some_fund ┆ 2026-01-30 ┆ 2588173        ┆ -0.00128  │\n",
              "│ some_fund ┆ 2026-01-30 ┆ 67066G10       ┆ 0.023653  │\n",
              "│ some_fund ┆ 2026-01-30 ┆ 85371710       ┆ 0.0       │\n",
              "└───────────┴────────────┴────────────────┴───────────┘"
            ]
          },
          "execution_count": 13,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# the idiosyncratic returns in a specific fund\n",
        "(\n",
        "    report.accessor.get_data(\n",
        "        [(\"fund\", \"some_fund\"), (\"type\", \"Idiosyncratic\")],  # filters\n",
        "        expand=(\"date\", \"input_asset_id\",),\n",
        "        value_cols=(\"PnL\",),\n",
        "    )\n",
        "    .select(\"fund\", \"date\", \"input_asset_id\", \"PnL\")\n",
        ")"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "094607e3",
      "metadata": {},
      "source": [
        "We can now to the same for risk attribution / decomposition"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 14,
      "id": "eb59eac9",
      "metadata": {},
      "outputs": [],
      "source": [
        "report_settings = XSRReportSettings(\n",
        "    portfolio_hierarchy_settings=portfoliohierarchy_settings,\n",
        "    factor_model_settings=factorriskmodel_settings,\n",
        "    normalize_holdings=False,\n",
        ")\n",
        "report_engine = bln.equity.reports.load(\n",
        "    report_settings.with_dataset(\"bayesline/Bayesline-US-All-1y\")\n",
        ")\n",
        "report = report_engine.calculate(start_date=\"2026-01-05\", end_date=\"2026-01-30\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 15,
      "id": "06b907fb",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/html": [
              "<div><style>\n",
              ".dataframe > thead > tr,\n",
              ".dataframe > tbody > tr {\n",
              "  text-align: right;\n",
              "  white-space: pre-wrap;\n",
              "}\n",
              "</style>\n",
              "<small>shape: (22, 14)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>ticker</th><th>date</th><th>type</th><th>factor_group</th><th>factor</th><th>Exposure</th><th>Volatility</th><th>Correlation</th><th>Contribution</th><th>Beta</th><th>RelativeExposure</th><th>RelativeContribution</th><th>ExposureRaw</th><th>PositionValue</th></tr><tr><td>str</td><td>date</td><td>str</td><td>str</td><td>str</td><td>f32</td><td>f32</td><td>f32</td><td>f32</td><td>f32</td><td>f32</td><td>f32</td><td>f32</td><td>f32</td></tr></thead><tbody><tr><td>&quot;AGTHX&quot;</td><td>2026-01-05</td><td>&quot;Factors&quot;</td><td>&quot;market&quot;</td><td>&quot;Market&quot;</td><td>0.99452</td><td>0.120739</td><td>0.550069</td><td>0.066051</td><td>0.0</td><td>1.0</td><td>0.277348</td><td>1.0</td><td>0.99452</td></tr><tr><td>&quot;AGTHX&quot;</td><td>2026-01-05</td><td>&quot;Factors&quot;</td><td>&quot;style&quot;</td><td>&quot;Dividend&quot;</td><td>0.013861</td><td>0.024763</td><td>-0.093918</td><td>-0.000032</td><td>0.0</td><td>1.0</td><td>-0.000135</td><td>0.013938</td><td>0.99452</td></tr><tr><td>&quot;AGTHX&quot;</td><td>2026-01-05</td><td>&quot;Factors&quot;</td><td>&quot;style&quot;</td><td>&quot;Growth&quot;</td><td>0.91989</td><td>0.014847</td><td>0.136754</td><td>0.001868</td><td>0.0</td><td>1.0</td><td>0.007843</td><td>0.924959</td><td>0.99452</td></tr><tr><td>&quot;AGTHX&quot;</td><td>2026-01-05</td><td>&quot;Factors&quot;</td><td>&quot;style&quot;</td><td>&quot;Leverage&quot;</td><td>-0.137832</td><td>0.025365</td><td>-0.404254</td><td>0.001413</td><td>0.0</td><td>1.0</td><td>0.005935</td><td>-0.138591</td><td>0.99452</td></tr><tr><td>&quot;AGTHX&quot;</td><td>2026-01-05</td><td>&quot;Factors&quot;</td><td>&quot;style&quot;</td><td>&quot;Momentum&quot;</td><td>1.10922</td><td>0.058227</td><td>0.30771</td><td>0.019874</td><td>0.0</td><td>1.0</td><td>0.083451</td><td>1.115332</td><td>0.99452</td></tr><tr><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td></tr><tr><td>&quot;AGTHX&quot;</td><td>2026-01-05</td><td>&quot;Factors&quot;</td><td>&quot;trbc&quot;</td><td>&quot;Institutions, Associations &amp; O…</td><td>0.0</td><td>0.213341</td><td>0.542226</td><td>0.0</td><td>0.0</td><td>NaN</td><td>0.0</td><td>0.0</td><td>0.99452</td></tr><tr><td>&quot;AGTHX&quot;</td><td>2026-01-05</td><td>&quot;Factors&quot;</td><td>&quot;trbc&quot;</td><td>&quot;Real Estate&quot;</td><td>0.0</td><td>0.075666</td><td>-0.244805</td><td>0.0</td><td>0.0</td><td>NaN</td><td>0.0</td><td>0.0</td><td>0.99452</td></tr><tr><td>&quot;AGTHX&quot;</td><td>2026-01-05</td><td>&quot;Factors&quot;</td><td>&quot;trbc&quot;</td><td>&quot;Technology&quot;</td><td>0.99452</td><td>0.051757</td><td>0.394833</td><td>0.020323</td><td>0.0</td><td>1.0</td><td>0.085338</td><td>1.0</td><td>0.99452</td></tr><tr><td>&quot;AGTHX&quot;</td><td>2026-01-05</td><td>&quot;Factors&quot;</td><td>&quot;trbc&quot;</td><td>&quot;Utilities&quot;</td><td>0.0</td><td>0.111759</td><td>-0.158906</td><td>0.0</td><td>0.0</td><td>NaN</td><td>0.0</td><td>0.0</td><td>0.99452</td></tr><tr><td>&quot;AGTHX&quot;</td><td>2026-01-05</td><td>&quot;Idiosyncratic&quot;</td><td>&quot;&quot;</td><td>&quot;&quot;</td><td>0.99452</td><td>0.148321</td><td>0.6228</td><td>0.092374</td><td>0.0</td><td>NaN</td><td>0.38788</td><td>1.0</td><td>0.99452</td></tr></tbody></table></div>"
            ],
            "text/plain": [
              "shape: (22, 14)\n",
              "┌────────┬────────────┬────────────┬───────────┬───┬───────────┬───────────┬───────────┬───────────┐\n",
              "│ ticker ┆ date       ┆ type       ┆ factor_gr ┆ … ┆ RelativeE ┆ RelativeC ┆ ExposureR ┆ PositionV │\n",
              "│ ---    ┆ ---        ┆ ---        ┆ oup       ┆   ┆ xposure   ┆ ontributi ┆ aw        ┆ alue      │\n",
              "│ str    ┆ date       ┆ str        ┆ ---       ┆   ┆ ---       ┆ on        ┆ ---       ┆ ---       │\n",
              "│        ┆            ┆            ┆ str       ┆   ┆ f32       ┆ ---       ┆ f32       ┆ f32       │\n",
              "│        ┆            ┆            ┆           ┆   ┆           ┆ f32       ┆           ┆           │\n",
              "╞════════╪════════════╪════════════╪═══════════╪═══╪═══════════╪═══════════╪═══════════╪═══════════╡\n",
              "│ AGTHX  ┆ 2026-01-05 ┆ Factors    ┆ market    ┆ … ┆ 1.0       ┆ 0.277348  ┆ 1.0       ┆ 0.99452   │\n",
              "│ AGTHX  ┆ 2026-01-05 ┆ Factors    ┆ style     ┆ … ┆ 1.0       ┆ -0.000135 ┆ 0.013938  ┆ 0.99452   │\n",
              "│ AGTHX  ┆ 2026-01-05 ┆ Factors    ┆ style     ┆ … ┆ 1.0       ┆ 0.007843  ┆ 0.924959  ┆ 0.99452   │\n",
              "│ AGTHX  ┆ 2026-01-05 ┆ Factors    ┆ style     ┆ … ┆ 1.0       ┆ 0.005935  ┆ -0.138591 ┆ 0.99452   │\n",
              "│ AGTHX  ┆ 2026-01-05 ┆ Factors    ┆ style     ┆ … ┆ 1.0       ┆ 0.083451  ┆ 1.115332  ┆ 0.99452   │\n",
              "│ …      ┆ …          ┆ …          ┆ …         ┆ … ┆ …         ┆ …         ┆ …         ┆ …         │\n",
              "│ AGTHX  ┆ 2026-01-05 ┆ Factors    ┆ trbc      ┆ … ┆ NaN       ┆ 0.0       ┆ 0.0       ┆ 0.99452   │\n",
              "│ AGTHX  ┆ 2026-01-05 ┆ Factors    ┆ trbc      ┆ … ┆ NaN       ┆ 0.0       ┆ 0.0       ┆ 0.99452   │\n",
              "│ AGTHX  ┆ 2026-01-05 ┆ Factors    ┆ trbc      ┆ … ┆ 1.0       ┆ 0.085338  ┆ 1.0       ┆ 0.99452   │\n",
              "│ AGTHX  ┆ 2026-01-05 ┆ Factors    ┆ trbc      ┆ … ┆ NaN       ┆ 0.0       ┆ 0.0       ┆ 0.99452   │\n",
              "│ AGTHX  ┆ 2026-01-05 ┆ Idiosyncra ┆           ┆ … ┆ NaN       ┆ 0.38788   ┆ 1.0       ┆ 0.99452   │\n",
              "│        ┆            ┆ tic        ┆           ┆   ┆           ┆           ┆           ┆           │\n",
              "└────────┴────────────┴────────────┴───────────┴───┴───────────┴───────────┴───────────┴───────────┘"
            ]
          },
          "execution_count": 15,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# for a specific ticker and date, the risk attribution across the factors\n",
        "(\n",
        "    report.accessor.get_data(\n",
        "        [(\"ticker\", \"AGTHX\"), (\"date\", \"2026-01-05\")],  # filters: [ticker=AGTHX]\n",
        "        expand=(\"type\", \"factor_group\", \"factor\",), # combinations define the rows\n",
        "        value_cols=report.accessor.metric_cols,  # Exposure, Stand-alone Volatility, Variance Contribution\n",
        "    )\n",
        "    .select(\"ticker\", \"date\", \"type\", \"factor_group\", \"factor\", *report.accessor.metric_cols)\n",
        ")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 16,
      "id": "f8312d12",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/html": [
              "<div><style>\n",
              ".dataframe > thead > tr,\n",
              ".dataframe > tbody > tr {\n",
              "  text-align: right;\n",
              "  white-space: pre-wrap;\n",
              "}\n",
              "</style>\n",
              "<small>shape: (76, 13)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>ticker</th><th>factor</th><th>date</th><th>input_asset_id</th><th>Exposure</th><th>Volatility</th><th>Correlation</th><th>Contribution</th><th>Beta</th><th>RelativeExposure</th><th>RelativeContribution</th><th>ExposureRaw</th><th>PositionValue</th></tr><tr><td>str</td><td>str</td><td>date</td><td>str</td><td>f32</td><td>f32</td><td>f32</td><td>f32</td><td>f32</td><td>f32</td><td>f32</td><td>f32</td><td>f32</td></tr></thead><tbody><tr><td>&quot;AGTHX&quot;</td><td>&quot;Volatility&quot;</td><td>2026-01-05</td><td>&quot;02079K305&quot;</td><td>0.126743</td><td>0.137944</td><td>0.590229</td><td>0.010319</td><td>0.0</td><td>-2.773333</td><td>0.04333</td><td>0.250651</td><td>0.505655</td></tr><tr><td>&quot;AGTHX&quot;</td><td>&quot;Volatility&quot;</td><td>2026-01-05</td><td>&quot;2588173&quot;</td><td>-0.172444</td><td>0.137944</td><td>0.590229</td><td>-0.01404</td><td>0.0</td><td>3.773333</td><td>-0.058954</td><td>-0.352742</td><td>0.488865</td></tr><tr><td>&quot;AGTHX&quot;</td><td>&quot;Volatility&quot;</td><td>2026-01-05</td><td>&quot;67066G10&quot;</td><td>0.0</td><td>0.137944</td><td>0.590229</td><td>0.0</td><td>0.0</td><td>-0.0</td><td>0.0</td><td>0.418788</td><td>0.0</td></tr><tr><td>&quot;AGTHX&quot;</td><td>&quot;Volatility&quot;</td><td>2026-01-05</td><td>&quot;85371710&quot;</td><td>0.0</td><td>0.137944</td><td>0.590229</td><td>0.0</td><td>0.0</td><td>-0.0</td><td>0.0</td><td>0.0</td><td>0.0</td></tr><tr><td>&quot;AGTHX&quot;</td><td>&quot;Volatility&quot;</td><td>2026-01-06</td><td>&quot;02079K305&quot;</td><td>0.124401</td><td>0.137523</td><td>0.59078</td><td>0.010107</td><td>0.0</td><td>-2.640821</td><td>0.042514</td><td>0.247742</td><td>0.502141</td></tr><tr><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td></tr><tr><td>&quot;AGTHX&quot;</td><td>&quot;Volatility&quot;</td><td>2026-01-29</td><td>&quot;85371710&quot;</td><td>0.0</td><td>0.129204</td><td>0.546718</td><td>0.0</td><td>0.0</td><td>0.0</td><td>0.0</td><td>0.0</td><td>0.0</td></tr><tr><td>&quot;AGTHX&quot;</td><td>&quot;Volatility&quot;</td><td>2026-01-30</td><td>&quot;02079K305&quot;</td><td>0.063845</td><td>0.132348</td><td>0.548994</td><td>0.004639</td><td>0.0</td><td>22.176376</td><td>0.01882</td><td>0.118245</td><td>0.539936</td></tr><tr><td>&quot;AGTHX&quot;</td><td>&quot;Volatility&quot;</td><td>2026-01-30</td><td>&quot;2588173&quot;</td><td>-0.060966</td><td>0.132348</td><td>0.548994</td><td>-0.00443</td><td>0.0</td><td>-21.176376</td><td>-0.017972</td><td>-0.137044</td><td>0.444864</td></tr><tr><td>&quot;AGTHX&quot;</td><td>&quot;Volatility&quot;</td><td>2026-01-30</td><td>&quot;67066G10&quot;</td><td>0.0</td><td>0.132348</td><td>0.548994</td><td>0.0</td><td>0.0</td><td>0.0</td><td>0.0</td><td>0.406453</td><td>0.0</td></tr><tr><td>&quot;AGTHX&quot;</td><td>&quot;Volatility&quot;</td><td>2026-01-30</td><td>&quot;85371710&quot;</td><td>0.0</td><td>0.132348</td><td>0.548994</td><td>0.0</td><td>0.0</td><td>0.0</td><td>0.0</td><td>0.0</td><td>0.0</td></tr></tbody></table></div>"
            ],
            "text/plain": [
              "shape: (76, 13)\n",
              "┌────────┬────────────┬────────────┬───────────┬───┬───────────┬───────────┬───────────┬───────────┐\n",
              "│ ticker ┆ factor     ┆ date       ┆ input_ass ┆ … ┆ RelativeE ┆ RelativeC ┆ ExposureR ┆ PositionV │\n",
              "│ ---    ┆ ---        ┆ ---        ┆ et_id     ┆   ┆ xposure   ┆ ontributi ┆ aw        ┆ alue      │\n",
              "│ str    ┆ str        ┆ date       ┆ ---       ┆   ┆ ---       ┆ on        ┆ ---       ┆ ---       │\n",
              "│        ┆            ┆            ┆ str       ┆   ┆ f32       ┆ ---       ┆ f32       ┆ f32       │\n",
              "│        ┆            ┆            ┆           ┆   ┆           ┆ f32       ┆           ┆           │\n",
              "╞════════╪════════════╪════════════╪═══════════╪═══╪═══════════╪═══════════╪═══════════╪═══════════╡\n",
              "│ AGTHX  ┆ Volatility ┆ 2026-01-05 ┆ 02079K305 ┆ … ┆ -2.773333 ┆ 0.04333   ┆ 0.250651  ┆ 0.505655  │\n",
              "│ AGTHX  ┆ Volatility ┆ 2026-01-05 ┆ 2588173   ┆ … ┆ 3.773333  ┆ -0.058954 ┆ -0.352742 ┆ 0.488865  │\n",
              "│ AGTHX  ┆ Volatility ┆ 2026-01-05 ┆ 67066G10  ┆ … ┆ -0.0      ┆ 0.0       ┆ 0.418788  ┆ 0.0       │\n",
              "│ AGTHX  ┆ Volatility ┆ 2026-01-05 ┆ 85371710  ┆ … ┆ -0.0      ┆ 0.0       ┆ 0.0       ┆ 0.0       │\n",
              "│ AGTHX  ┆ Volatility ┆ 2026-01-06 ┆ 02079K305 ┆ … ┆ -2.640821 ┆ 0.042514  ┆ 0.247742  ┆ 0.502141  │\n",
              "│ …      ┆ …          ┆ …          ┆ …         ┆ … ┆ …         ┆ …         ┆ …         ┆ …         │\n",
              "│ AGTHX  ┆ Volatility ┆ 2026-01-29 ┆ 85371710  ┆ … ┆ 0.0       ┆ 0.0       ┆ 0.0       ┆ 0.0       │\n",
              "│ AGTHX  ┆ Volatility ┆ 2026-01-30 ┆ 02079K305 ┆ … ┆ 22.176376 ┆ 0.01882   ┆ 0.118245  ┆ 0.539936  │\n",
              "│ AGTHX  ┆ Volatility ┆ 2026-01-30 ┆ 2588173   ┆ … ┆ -21.17637 ┆ -0.017972 ┆ -0.137044 ┆ 0.444864  │\n",
              "│        ┆            ┆            ┆           ┆   ┆ 6         ┆           ┆           ┆           │\n",
              "│ AGTHX  ┆ Volatility ┆ 2026-01-30 ┆ 67066G10  ┆ … ┆ 0.0       ┆ 0.0       ┆ 0.406453  ┆ 0.0       │\n",
              "│ AGTHX  ┆ Volatility ┆ 2026-01-30 ┆ 85371710  ┆ … ┆ 0.0       ┆ 0.0       ┆ 0.0       ┆ 0.0       │\n",
              "└────────┴────────────┴────────────┴───────────┴───┴───────────┴───────────┴───────────┴───────────┘"
            ]
          },
          "execution_count": 16,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# for a specific ticker and factor, the risk attribution across dates and assets\n",
        "(\n",
        "    report.accessor.get_data(\n",
        "        [(\"ticker\", \"AGTHX\"), (\"factor\", \"Volatility\")],  # filters\n",
        "        expand=(\"date\", \"input_asset_id\",), # combinations define the rows\n",
        "        value_cols=report.accessor.metric_cols,  # Exposure, Stand-alone Volatility, Variance Contribution\n",
        "    )\n",
        "    .select(\"ticker\", \"factor\", \"date\", \"input_asset_id\", *report.accessor.metric_cols)\n",
        ")"
      ]
    }
  ],
  "metadata": {
    "jupytext": {
      "text_representation": {
        "extension": ".py",
        "format_name": "percent",
        "format_version": "1.3",
        "jupytext_version": "1.19.3"
      }
    },
    "kernelspec": {
      "display_name": ".venv",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.11.15"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}