{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Factor Covariance Matrix Forecasts\n",
        "\n",
        "In this tutorial we are going to create several covariance matrices of factor returns. This covariance matrix follows from a standard factor model. We will also tie out the numbers against a pandas/numpy-based reimplementation. More specifically, we will:\n",
        "- Create a basic risk model.\n",
        "- Extract the factor returns.\n",
        "- Use the covariance matrix forecast report to compute the covariance matrix.\n",
        "- Use pandas to extract the factor volatility and correlation matrix time-series.\n",
        "- Replicate the volatility forecast.\n",
        "- Replicate the correlation forecast.\n",
        "\n",
        "Throughout this notebook we work with a randomly generated dataset. The results should generalize to real data, but for legal reasons we do not show any real data on our public API. Bayesline clients can run this notebook on real data."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Imports & Setup\n",
        "\n",
        "For this tutorial notebook, you will need to import the following packages."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "metadata": {},
      "outputs": [],
      "source": [
        "import pandas as pd\n",
        "import polars as pl\n",
        "import numpy as np\n",
        "\n",
        "from bayesline.api.equity import (\n",
        "    FactorCovarianceReportSettingsV2,\n",
        "    ExposureSettings,\n",
        "    CategoricalExposureGroupSettings,\n",
        "    ContinuousExposureGroupSettings,\n",
        "    FactorRiskModelSettings,\n",
        "    ModelConstructionSettings,\n",
        "    UniverseSettings,\n",
        ")\n",
        "from bayesline.apiclient import BayeslineApiClient"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We will also need to have a Bayesline API client configured."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "tags": [
          "skip-execution"
        ]
      },
      "outputs": [],
      "source": [
        "bln = BayeslineApiClient.new_client(\n",
        "    endpoint=\"https://[ENDPOINT]\",\n",
        "    api_key=\"[API-KEY]\",\n",
        ")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Creating the covariance matrix forecasts\n",
        "\n",
        "Let's first set up a basic risk model and use it to generate the forecasts. We choose to run with mostly default settings. The steps involved are:\n",
        "1. Creating the settings of the risk model.\n",
        "2. Loading the report model engine.\n",
        "3. Running the engine to generate the covariance report.\n",
        "\n",
        "The first step is creating the risk model settings."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "metadata": {},
      "outputs": [],
      "source": [
        "factorriskmodel_settings = FactorRiskModelSettings(\n",
        "    universe=UniverseSettings(dataset=\"Bayesline-US-All-1y\"),\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",
      "metadata": {},
      "source": [
        "Next, we create the report engine from the report settings. We run with the defaults here."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 4,
      "metadata": {},
      "outputs": [],
      "source": [
        "report_settings = FactorCovarianceReportSettingsV2(\n",
        "    factor_model_settings=factorriskmodel_settings\n",
        ")\n",
        "report_engine = bln.equity.reports.load(report_settings)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Let's see what these settings really are by printing them out."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 5,
      "metadata": {},
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "{\n",
            "  \"halflife_vol\": 60.0,\n",
            "  \"halflife_cor\": 120.0,\n",
            "  \"halflife_vra\": null,\n",
            "  \"nw_lags_vol\": 0,\n",
            "  \"nw_lags_vol_halflife_override\": null,\n",
            "  \"nw_lags_cor\": 0,\n",
            "  \"shrink_cor_method\": null,\n",
            "  \"shrink_cor_length\": null,\n",
            "  \"combine_standardized\": false\n",
            "}\n"
          ]
        }
      ],
      "source": [
        "print(report_settings.factor_cov_settings.model_dump_json(indent=2))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The different settings that jointly make up the covariance matrix are:\n",
        "1. **halflife_factor_vol** The halflife of the factor volatility. The default is a 60-day halflife.\n",
        "2. **halflife_factor_vra** The halflife of the cross-sectional factor volatility adjustment. The default is to not do any adjustment.\n",
        "3. **halflife_factor_cor** The halflife of the factor correlation. The default is a 120-day halflife.\n",
        "4. **nw_lags_factor_vol** The overlap or Newey-West lags to incluce on the factor volatility forecast. The default is zero, meaning no autocorrelation correction is performed.\n",
        "5. **nw_lags_factor_cor** The overlap or Newey-West lags to incluce on the factor correlation forecast. The default is zero, meaning no autocorrelation correction is performed.\n",
        "\n",
        "Now we get the actual time-series of the covariance matrices."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 6,
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th>Academic &amp; Educational Services</th>\n",
              "      <th>Basic Materials</th>\n",
              "      <th>Consumer Cyclicals</th>\n",
              "      <th>Consumer Non-Cyclicals</th>\n",
              "      <th>Dividend</th>\n",
              "      <th>Energy</th>\n",
              "      <th>Financials</th>\n",
              "      <th>Government Activity</th>\n",
              "      <th>Growth</th>\n",
              "      <th>Healthcare</th>\n",
              "      <th>...</th>\n",
              "      <th>Institutions, Associations &amp; Organizations</th>\n",
              "      <th>Leverage</th>\n",
              "      <th>Market</th>\n",
              "      <th>Momentum</th>\n",
              "      <th>Real Estate</th>\n",
              "      <th>Size</th>\n",
              "      <th>Technology</th>\n",
              "      <th>Utilities</th>\n",
              "      <th>Value</th>\n",
              "      <th>Volatility</th>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>date</th>\n",
              "      <th>factor</th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th rowspan=\"5\" valign=\"top\">2025-04-01</th>\n",
              "      <th>Academic &amp; Educational Services</th>\n",
              "      <td>0.025434</td>\n",
              "      <td>-0.001846</td>\n",
              "      <td>0.006746</td>\n",
              "      <td>0.002403</td>\n",
              "      <td>-0.003761</td>\n",
              "      <td>0.013763</td>\n",
              "      <td>0.002887</td>\n",
              "      <td>-0.013663</td>\n",
              "      <td>0.004263</td>\n",
              "      <td>-0.057754</td>\n",
              "      <td>...</td>\n",
              "      <td>0.001808</td>\n",
              "      <td>0.000835</td>\n",
              "      <td>0.003058</td>\n",
              "      <td>0.004084</td>\n",
              "      <td>-0.004142</td>\n",
              "      <td>0.000644</td>\n",
              "      <td>0.008898</td>\n",
              "      <td>0.004983</td>\n",
              "      <td>-0.004099</td>\n",
              "      <td>0.005047</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>Basic Materials</th>\n",
              "      <td>-0.001846</td>\n",
              "      <td>0.000134</td>\n",
              "      <td>-0.000490</td>\n",
              "      <td>-0.000174</td>\n",
              "      <td>0.000273</td>\n",
              "      <td>-0.000999</td>\n",
              "      <td>-0.000210</td>\n",
              "      <td>0.000992</td>\n",
              "      <td>-0.000309</td>\n",
              "      <td>0.004192</td>\n",
              "      <td>...</td>\n",
              "      <td>-0.000131</td>\n",
              "      <td>-0.000061</td>\n",
              "      <td>-0.000222</td>\n",
              "      <td>-0.000296</td>\n",
              "      <td>0.000301</td>\n",
              "      <td>-0.000047</td>\n",
              "      <td>-0.000646</td>\n",
              "      <td>-0.000362</td>\n",
              "      <td>0.000297</td>\n",
              "      <td>-0.000366</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>Consumer Cyclicals</th>\n",
              "      <td>0.006746</td>\n",
              "      <td>-0.000490</td>\n",
              "      <td>0.001789</td>\n",
              "      <td>0.000637</td>\n",
              "      <td>-0.000997</td>\n",
              "      <td>0.003650</td>\n",
              "      <td>0.000766</td>\n",
              "      <td>-0.003624</td>\n",
              "      <td>0.001131</td>\n",
              "      <td>-0.015318</td>\n",
              "      <td>...</td>\n",
              "      <td>0.000479</td>\n",
              "      <td>0.000221</td>\n",
              "      <td>0.000811</td>\n",
              "      <td>0.001083</td>\n",
              "      <td>-0.001099</td>\n",
              "      <td>0.000171</td>\n",
              "      <td>0.002360</td>\n",
              "      <td>0.001322</td>\n",
              "      <td>-0.001087</td>\n",
              "      <td>0.001339</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>Consumer Non-Cyclicals</th>\n",
              "      <td>0.002403</td>\n",
              "      <td>-0.000174</td>\n",
              "      <td>0.000637</td>\n",
              "      <td>0.000227</td>\n",
              "      <td>-0.000355</td>\n",
              "      <td>0.001300</td>\n",
              "      <td>0.000273</td>\n",
              "      <td>-0.001291</td>\n",
              "      <td>0.000403</td>\n",
              "      <td>-0.005456</td>\n",
              "      <td>...</td>\n",
              "      <td>0.000171</td>\n",
              "      <td>0.000079</td>\n",
              "      <td>0.000289</td>\n",
              "      <td>0.000386</td>\n",
              "      <td>-0.000391</td>\n",
              "      <td>0.000061</td>\n",
              "      <td>0.000841</td>\n",
              "      <td>0.000471</td>\n",
              "      <td>-0.000387</td>\n",
              "      <td>0.000477</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>Dividend</th>\n",
              "      <td>-0.003761</td>\n",
              "      <td>0.000273</td>\n",
              "      <td>-0.000997</td>\n",
              "      <td>-0.000355</td>\n",
              "      <td>0.000556</td>\n",
              "      <td>-0.002035</td>\n",
              "      <td>-0.000427</td>\n",
              "      <td>0.002020</td>\n",
              "      <td>-0.000630</td>\n",
              "      <td>0.008540</td>\n",
              "      <td>...</td>\n",
              "      <td>-0.000267</td>\n",
              "      <td>-0.000123</td>\n",
              "      <td>-0.000452</td>\n",
              "      <td>-0.000604</td>\n",
              "      <td>0.000612</td>\n",
              "      <td>-0.000095</td>\n",
              "      <td>-0.001316</td>\n",
              "      <td>-0.000737</td>\n",
              "      <td>0.000606</td>\n",
              "      <td>-0.000746</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>...</th>\n",
              "      <th>...</th>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th rowspan=\"5\" valign=\"top\">2026-03-31</th>\n",
              "      <th>Size</th>\n",
              "      <td>-0.000240</td>\n",
              "      <td>-0.000153</td>\n",
              "      <td>0.000119</td>\n",
              "      <td>-0.001072</td>\n",
              "      <td>-0.000078</td>\n",
              "      <td>-0.000152</td>\n",
              "      <td>0.001089</td>\n",
              "      <td>-0.001486</td>\n",
              "      <td>0.000043</td>\n",
              "      <td>-0.000503</td>\n",
              "      <td>...</td>\n",
              "      <td>-0.000727</td>\n",
              "      <td>0.000080</td>\n",
              "      <td>0.001238</td>\n",
              "      <td>-0.000183</td>\n",
              "      <td>-0.000323</td>\n",
              "      <td>0.000650</td>\n",
              "      <td>-0.000243</td>\n",
              "      <td>-0.001206</td>\n",
              "      <td>0.000216</td>\n",
              "      <td>0.001249</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>Technology</th>\n",
              "      <td>-0.001614</td>\n",
              "      <td>-0.004033</td>\n",
              "      <td>-0.002424</td>\n",
              "      <td>-0.003407</td>\n",
              "      <td>-0.000120</td>\n",
              "      <td>-0.002893</td>\n",
              "      <td>-0.000662</td>\n",
              "      <td>-0.001822</td>\n",
              "      <td>0.000206</td>\n",
              "      <td>-0.002584</td>\n",
              "      <td>...</td>\n",
              "      <td>-0.002089</td>\n",
              "      <td>-0.000592</td>\n",
              "      <td>0.001510</td>\n",
              "      <td>0.000687</td>\n",
              "      <td>-0.002019</td>\n",
              "      <td>-0.000243</td>\n",
              "      <td>0.003244</td>\n",
              "      <td>-0.001333</td>\n",
              "      <td>-0.000699</td>\n",
              "      <td>0.001884</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>Utilities</th>\n",
              "      <td>-0.002122</td>\n",
              "      <td>0.003419</td>\n",
              "      <td>-0.002619</td>\n",
              "      <td>0.006028</td>\n",
              "      <td>0.000259</td>\n",
              "      <td>0.005275</td>\n",
              "      <td>-0.001843</td>\n",
              "      <td>0.010601</td>\n",
              "      <td>-0.000310</td>\n",
              "      <td>0.000726</td>\n",
              "      <td>...</td>\n",
              "      <td>0.006940</td>\n",
              "      <td>-0.000910</td>\n",
              "      <td>-0.005571</td>\n",
              "      <td>0.003410</td>\n",
              "      <td>0.005160</td>\n",
              "      <td>-0.001206</td>\n",
              "      <td>-0.001333</td>\n",
              "      <td>0.016572</td>\n",
              "      <td>-0.001013</td>\n",
              "      <td>-0.003184</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>Value</th>\n",
              "      <td>0.000279</td>\n",
              "      <td>0.001366</td>\n",
              "      <td>0.001299</td>\n",
              "      <td>0.000277</td>\n",
              "      <td>0.000130</td>\n",
              "      <td>0.001123</td>\n",
              "      <td>-0.000056</td>\n",
              "      <td>0.000474</td>\n",
              "      <td>0.000067</td>\n",
              "      <td>0.000247</td>\n",
              "      <td>...</td>\n",
              "      <td>-0.000720</td>\n",
              "      <td>0.000383</td>\n",
              "      <td>0.000956</td>\n",
              "      <td>-0.000392</td>\n",
              "      <td>-0.000256</td>\n",
              "      <td>0.000216</td>\n",
              "      <td>-0.000699</td>\n",
              "      <td>-0.001013</td>\n",
              "      <td>0.001161</td>\n",
              "      <td>0.000255</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>Volatility</th>\n",
              "      <td>-0.009717</td>\n",
              "      <td>-0.003011</td>\n",
              "      <td>-0.003352</td>\n",
              "      <td>-0.007472</td>\n",
              "      <td>0.000124</td>\n",
              "      <td>-0.006026</td>\n",
              "      <td>0.005280</td>\n",
              "      <td>-0.030797</td>\n",
              "      <td>0.000076</td>\n",
              "      <td>-0.007280</td>\n",
              "      <td>...</td>\n",
              "      <td>-0.019289</td>\n",
              "      <td>-0.001040</td>\n",
              "      <td>0.017239</td>\n",
              "      <td>0.003792</td>\n",
              "      <td>-0.002445</td>\n",
              "      <td>0.001249</td>\n",
              "      <td>0.001884</td>\n",
              "      <td>-0.003184</td>\n",
              "      <td>0.000255</td>\n",
              "      <td>0.020740</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "<p>5271 rows × 21 columns</p>\n",
              "</div>"
            ],
            "text/plain": [
              "                                            Academic & Educational Services  \\\n",
              "date       factor                                                             \n",
              "2025-04-01 Academic & Educational Services                         0.025434   \n",
              "           Basic Materials                                        -0.001846   \n",
              "           Consumer Cyclicals                                      0.006746   \n",
              "           Consumer Non-Cyclicals                                  0.002403   \n",
              "           Dividend                                               -0.003761   \n",
              "...                                                                     ...   \n",
              "2026-03-31 Size                                                   -0.000240   \n",
              "           Technology                                             -0.001614   \n",
              "           Utilities                                              -0.002122   \n",
              "           Value                                                   0.000279   \n",
              "           Volatility                                             -0.009717   \n",
              "\n",
              "                                            Basic Materials  \\\n",
              "date       factor                                             \n",
              "2025-04-01 Academic & Educational Services        -0.001846   \n",
              "           Basic Materials                         0.000134   \n",
              "           Consumer Cyclicals                     -0.000490   \n",
              "           Consumer Non-Cyclicals                 -0.000174   \n",
              "           Dividend                                0.000273   \n",
              "...                                                     ...   \n",
              "2026-03-31 Size                                   -0.000153   \n",
              "           Technology                             -0.004033   \n",
              "           Utilities                               0.003419   \n",
              "           Value                                   0.001366   \n",
              "           Volatility                             -0.003011   \n",
              "\n",
              "                                            Consumer Cyclicals  \\\n",
              "date       factor                                                \n",
              "2025-04-01 Academic & Educational Services            0.006746   \n",
              "           Basic Materials                           -0.000490   \n",
              "           Consumer Cyclicals                         0.001789   \n",
              "           Consumer Non-Cyclicals                     0.000637   \n",
              "           Dividend                                  -0.000997   \n",
              "...                                                        ...   \n",
              "2026-03-31 Size                                       0.000119   \n",
              "           Technology                                -0.002424   \n",
              "           Utilities                                 -0.002619   \n",
              "           Value                                      0.001299   \n",
              "           Volatility                                -0.003352   \n",
              "\n",
              "                                            Consumer Non-Cyclicals  Dividend  \\\n",
              "date       factor                                                              \n",
              "2025-04-01 Academic & Educational Services                0.002403 -0.003761   \n",
              "           Basic Materials                               -0.000174  0.000273   \n",
              "           Consumer Cyclicals                             0.000637 -0.000997   \n",
              "           Consumer Non-Cyclicals                         0.000227 -0.000355   \n",
              "           Dividend                                      -0.000355  0.000556   \n",
              "...                                                            ...       ...   \n",
              "2026-03-31 Size                                          -0.001072 -0.000078   \n",
              "           Technology                                    -0.003407 -0.000120   \n",
              "           Utilities                                      0.006028  0.000259   \n",
              "           Value                                          0.000277  0.000130   \n",
              "           Volatility                                    -0.007472  0.000124   \n",
              "\n",
              "                                              Energy  Financials  \\\n",
              "date       factor                                                  \n",
              "2025-04-01 Academic & Educational Services  0.013763    0.002887   \n",
              "           Basic Materials                 -0.000999   -0.000210   \n",
              "           Consumer Cyclicals               0.003650    0.000766   \n",
              "           Consumer Non-Cyclicals           0.001300    0.000273   \n",
              "           Dividend                        -0.002035   -0.000427   \n",
              "...                                              ...         ...   \n",
              "2026-03-31 Size                            -0.000152    0.001089   \n",
              "           Technology                      -0.002893   -0.000662   \n",
              "           Utilities                        0.005275   -0.001843   \n",
              "           Value                            0.001123   -0.000056   \n",
              "           Volatility                      -0.006026    0.005280   \n",
              "\n",
              "                                            Government Activity    Growth  \\\n",
              "date       factor                                                           \n",
              "2025-04-01 Academic & Educational Services            -0.013663  0.004263   \n",
              "           Basic Materials                             0.000992 -0.000309   \n",
              "           Consumer Cyclicals                         -0.003624  0.001131   \n",
              "           Consumer Non-Cyclicals                     -0.001291  0.000403   \n",
              "           Dividend                                    0.002020 -0.000630   \n",
              "...                                                         ...       ...   \n",
              "2026-03-31 Size                                       -0.001486  0.000043   \n",
              "           Technology                                 -0.001822  0.000206   \n",
              "           Utilities                                   0.010601 -0.000310   \n",
              "           Value                                       0.000474  0.000067   \n",
              "           Volatility                                 -0.030797  0.000076   \n",
              "\n",
              "                                            Healthcare  ...  \\\n",
              "date       factor                                       ...   \n",
              "2025-04-01 Academic & Educational Services   -0.057754  ...   \n",
              "           Basic Materials                    0.004192  ...   \n",
              "           Consumer Cyclicals                -0.015318  ...   \n",
              "           Consumer Non-Cyclicals            -0.005456  ...   \n",
              "           Dividend                           0.008540  ...   \n",
              "...                                                ...  ...   \n",
              "2026-03-31 Size                              -0.000503  ...   \n",
              "           Technology                        -0.002584  ...   \n",
              "           Utilities                          0.000726  ...   \n",
              "           Value                              0.000247  ...   \n",
              "           Volatility                        -0.007280  ...   \n",
              "\n",
              "                                            Institutions, Associations & Organizations  \\\n",
              "date       factor                                                                        \n",
              "2025-04-01 Academic & Educational Services                                    0.001808   \n",
              "           Basic Materials                                                   -0.000131   \n",
              "           Consumer Cyclicals                                                 0.000479   \n",
              "           Consumer Non-Cyclicals                                             0.000171   \n",
              "           Dividend                                                          -0.000267   \n",
              "...                                                                                ...   \n",
              "2026-03-31 Size                                                              -0.000727   \n",
              "           Technology                                                        -0.002089   \n",
              "           Utilities                                                          0.006940   \n",
              "           Value                                                             -0.000720   \n",
              "           Volatility                                                        -0.019289   \n",
              "\n",
              "                                            Leverage    Market  Momentum  \\\n",
              "date       factor                                                          \n",
              "2025-04-01 Academic & Educational Services  0.000835  0.003058  0.004084   \n",
              "           Basic Materials                 -0.000061 -0.000222 -0.000296   \n",
              "           Consumer Cyclicals               0.000221  0.000811  0.001083   \n",
              "           Consumer Non-Cyclicals           0.000079  0.000289  0.000386   \n",
              "           Dividend                        -0.000123 -0.000452 -0.000604   \n",
              "...                                              ...       ...       ...   \n",
              "2026-03-31 Size                             0.000080  0.001238 -0.000183   \n",
              "           Technology                      -0.000592  0.001510  0.000687   \n",
              "           Utilities                       -0.000910 -0.005571  0.003410   \n",
              "           Value                            0.000383  0.000956 -0.000392   \n",
              "           Volatility                      -0.001040  0.017239  0.003792   \n",
              "\n",
              "                                            Real Estate      Size  Technology  \\\n",
              "date       factor                                                               \n",
              "2025-04-01 Academic & Educational Services    -0.004142  0.000644    0.008898   \n",
              "           Basic Materials                     0.000301 -0.000047   -0.000646   \n",
              "           Consumer Cyclicals                 -0.001099  0.000171    0.002360   \n",
              "           Consumer Non-Cyclicals             -0.000391  0.000061    0.000841   \n",
              "           Dividend                            0.000612 -0.000095   -0.001316   \n",
              "...                                                 ...       ...         ...   \n",
              "2026-03-31 Size                               -0.000323  0.000650   -0.000243   \n",
              "           Technology                         -0.002019 -0.000243    0.003244   \n",
              "           Utilities                           0.005160 -0.001206   -0.001333   \n",
              "           Value                              -0.000256  0.000216   -0.000699   \n",
              "           Volatility                         -0.002445  0.001249    0.001884   \n",
              "\n",
              "                                            Utilities     Value  Volatility  \n",
              "date       factor                                                            \n",
              "2025-04-01 Academic & Educational Services   0.004983 -0.004099    0.005047  \n",
              "           Basic Materials                  -0.000362  0.000297   -0.000366  \n",
              "           Consumer Cyclicals                0.001322 -0.001087    0.001339  \n",
              "           Consumer Non-Cyclicals            0.000471 -0.000387    0.000477  \n",
              "           Dividend                         -0.000737  0.000606   -0.000746  \n",
              "...                                               ...       ...         ...  \n",
              "2026-03-31 Size                             -0.001206  0.000216    0.001249  \n",
              "           Technology                       -0.001333 -0.000699    0.001884  \n",
              "           Utilities                         0.016572 -0.001013   -0.003184  \n",
              "           Value                            -0.001013  0.001161    0.000255  \n",
              "           Volatility                       -0.003184  0.000255    0.020740  \n",
              "\n",
              "[5271 rows x 21 columns]"
            ]
          },
          "execution_count": 6,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# generate the report data\n",
        "report = report_engine.calculate(start_date=None, end_date=None)\n",
        "\n",
        "# get the covariance matrix (and skip the first date)\n",
        "df_report = report.get_covariance().filter(pl.col(\"date\") > pl.col(\"date\").min())\n",
        "\n",
        "# convert to pandas\n",
        "df_vcov = df_report.to_pandas().set_index([\"date\", \"factor\"]).rename(columns=lambda c: c.split(\"^\")[0])\n",
        "df_vcov"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "For downstream comparisons, we split these into factor volatilities and correlations."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 7,
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>Academic &amp; Educational Services</th>\n",
              "      <th>Basic Materials</th>\n",
              "      <th>Consumer Cyclicals</th>\n",
              "      <th>Consumer Non-Cyclicals</th>\n",
              "      <th>Dividend</th>\n",
              "      <th>Energy</th>\n",
              "      <th>Financials</th>\n",
              "      <th>Government Activity</th>\n",
              "      <th>Growth</th>\n",
              "      <th>Healthcare</th>\n",
              "      <th>...</th>\n",
              "      <th>Institutions, Associations &amp; Organizations</th>\n",
              "      <th>Leverage</th>\n",
              "      <th>Market</th>\n",
              "      <th>Momentum</th>\n",
              "      <th>Real Estate</th>\n",
              "      <th>Size</th>\n",
              "      <th>Technology</th>\n",
              "      <th>Utilities</th>\n",
              "      <th>Value</th>\n",
              "      <th>Volatility</th>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>date</th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>2026-03-25</th>\n",
              "      <td>0.186803</td>\n",
              "      <td>0.135197</td>\n",
              "      <td>0.085725</td>\n",
              "      <td>0.103875</td>\n",
              "      <td>0.025571</td>\n",
              "      <td>0.169620</td>\n",
              "      <td>0.064445</td>\n",
              "      <td>0.546927</td>\n",
              "      <td>0.018382</td>\n",
              "      <td>0.111329</td>\n",
              "      <td>...</td>\n",
              "      <td>0.176007</td>\n",
              "      <td>0.026514</td>\n",
              "      <td>0.138444</td>\n",
              "      <td>0.069647</td>\n",
              "      <td>0.088610</td>\n",
              "      <td>0.024487</td>\n",
              "      <td>0.056239</td>\n",
              "      <td>0.127778</td>\n",
              "      <td>0.034710</td>\n",
              "      <td>0.134647</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2026-03-26</th>\n",
              "      <td>0.186054</td>\n",
              "      <td>0.135095</td>\n",
              "      <td>0.085208</td>\n",
              "      <td>0.103440</td>\n",
              "      <td>0.025459</td>\n",
              "      <td>0.174187</td>\n",
              "      <td>0.064205</td>\n",
              "      <td>0.544028</td>\n",
              "      <td>0.018778</td>\n",
              "      <td>0.113726</td>\n",
              "      <td>...</td>\n",
              "      <td>0.175040</td>\n",
              "      <td>0.026642</td>\n",
              "      <td>0.139284</td>\n",
              "      <td>0.071588</td>\n",
              "      <td>0.088279</td>\n",
              "      <td>0.024388</td>\n",
              "      <td>0.056906</td>\n",
              "      <td>0.127632</td>\n",
              "      <td>0.034534</td>\n",
              "      <td>0.136749</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2026-03-27</th>\n",
              "      <td>0.185076</td>\n",
              "      <td>0.136677</td>\n",
              "      <td>0.084701</td>\n",
              "      <td>0.106129</td>\n",
              "      <td>0.025305</td>\n",
              "      <td>0.176637</td>\n",
              "      <td>0.063979</td>\n",
              "      <td>0.541673</td>\n",
              "      <td>0.018925</td>\n",
              "      <td>0.114096</td>\n",
              "      <td>...</td>\n",
              "      <td>0.178136</td>\n",
              "      <td>0.026479</td>\n",
              "      <td>0.140880</td>\n",
              "      <td>0.071862</td>\n",
              "      <td>0.087787</td>\n",
              "      <td>0.024499</td>\n",
              "      <td>0.056771</td>\n",
              "      <td>0.128743</td>\n",
              "      <td>0.034337</td>\n",
              "      <td>0.136551</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2026-03-30</th>\n",
              "      <td>0.184248</td>\n",
              "      <td>0.137411</td>\n",
              "      <td>0.084451</td>\n",
              "      <td>0.105516</td>\n",
              "      <td>0.025218</td>\n",
              "      <td>0.176239</td>\n",
              "      <td>0.064102</td>\n",
              "      <td>0.539096</td>\n",
              "      <td>0.018884</td>\n",
              "      <td>0.114241</td>\n",
              "      <td>...</td>\n",
              "      <td>0.177478</td>\n",
              "      <td>0.026338</td>\n",
              "      <td>0.141172</td>\n",
              "      <td>0.073828</td>\n",
              "      <td>0.087746</td>\n",
              "      <td>0.024738</td>\n",
              "      <td>0.057125</td>\n",
              "      <td>0.127997</td>\n",
              "      <td>0.034140</td>\n",
              "      <td>0.138475</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2026-03-31</th>\n",
              "      <td>0.184671</td>\n",
              "      <td>0.137022</td>\n",
              "      <td>0.083946</td>\n",
              "      <td>0.107057</td>\n",
              "      <td>0.025234</td>\n",
              "      <td>0.183978</td>\n",
              "      <td>0.065007</td>\n",
              "      <td>0.537366</td>\n",
              "      <td>0.018777</td>\n",
              "      <td>0.113578</td>\n",
              "      <td>...</td>\n",
              "      <td>0.178359</td>\n",
              "      <td>0.026605</td>\n",
              "      <td>0.144843</td>\n",
              "      <td>0.074641</td>\n",
              "      <td>0.087296</td>\n",
              "      <td>0.025502</td>\n",
              "      <td>0.056956</td>\n",
              "      <td>0.128733</td>\n",
              "      <td>0.034073</td>\n",
              "      <td>0.144013</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "<p>5 rows × 21 columns</p>\n",
              "</div>"
            ],
            "text/plain": [
              "            Academic & Educational Services  Basic Materials  \\\n",
              "date                                                           \n",
              "2026-03-25                         0.186803         0.135197   \n",
              "2026-03-26                         0.186054         0.135095   \n",
              "2026-03-27                         0.185076         0.136677   \n",
              "2026-03-30                         0.184248         0.137411   \n",
              "2026-03-31                         0.184671         0.137022   \n",
              "\n",
              "            Consumer Cyclicals  Consumer Non-Cyclicals  Dividend    Energy  \\\n",
              "date                                                                         \n",
              "2026-03-25            0.085725                0.103875  0.025571  0.169620   \n",
              "2026-03-26            0.085208                0.103440  0.025459  0.174187   \n",
              "2026-03-27            0.084701                0.106129  0.025305  0.176637   \n",
              "2026-03-30            0.084451                0.105516  0.025218  0.176239   \n",
              "2026-03-31            0.083946                0.107057  0.025234  0.183978   \n",
              "\n",
              "            Financials  Government Activity    Growth  Healthcare  ...  \\\n",
              "date                                                               ...   \n",
              "2026-03-25    0.064445             0.546927  0.018382    0.111329  ...   \n",
              "2026-03-26    0.064205             0.544028  0.018778    0.113726  ...   \n",
              "2026-03-27    0.063979             0.541673  0.018925    0.114096  ...   \n",
              "2026-03-30    0.064102             0.539096  0.018884    0.114241  ...   \n",
              "2026-03-31    0.065007             0.537366  0.018777    0.113578  ...   \n",
              "\n",
              "            Institutions, Associations & Organizations  Leverage    Market  \\\n",
              "date                                                                         \n",
              "2026-03-25                                    0.176007  0.026514  0.138444   \n",
              "2026-03-26                                    0.175040  0.026642  0.139284   \n",
              "2026-03-27                                    0.178136  0.026479  0.140880   \n",
              "2026-03-30                                    0.177478  0.026338  0.141172   \n",
              "2026-03-31                                    0.178359  0.026605  0.144843   \n",
              "\n",
              "            Momentum  Real Estate      Size  Technology  Utilities     Value  \\\n",
              "date                                                                           \n",
              "2026-03-25  0.069647     0.088610  0.024487    0.056239   0.127778  0.034710   \n",
              "2026-03-26  0.071588     0.088279  0.024388    0.056906   0.127632  0.034534   \n",
              "2026-03-27  0.071862     0.087787  0.024499    0.056771   0.128743  0.034337   \n",
              "2026-03-30  0.073828     0.087746  0.024738    0.057125   0.127997  0.034140   \n",
              "2026-03-31  0.074641     0.087296  0.025502    0.056956   0.128733  0.034073   \n",
              "\n",
              "            Volatility  \n",
              "date                    \n",
              "2026-03-25    0.134647  \n",
              "2026-03-26    0.136749  \n",
              "2026-03-27    0.136551  \n",
              "2026-03-30    0.138475  \n",
              "2026-03-31    0.144013  \n",
              "\n",
              "[5 rows x 21 columns]"
            ]
          },
          "execution_count": 7,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# calculate actual factor volatilities from vcovs\n",
        "df_vol = df_vcov.groupby(level=\"date\").apply(\n",
        "    lambda df: pd.Series(np.diag(df) ** 0.5, df.index.droplevel(\"date\"))\n",
        ")\n",
        "df_vol.columns.name = None\n",
        "df_vol.tail()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 8,
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>Academic &amp; Educational Services</th>\n",
              "      <th>Basic Materials</th>\n",
              "      <th>Consumer Cyclicals</th>\n",
              "      <th>Consumer Non-Cyclicals</th>\n",
              "      <th>Dividend</th>\n",
              "      <th>Energy</th>\n",
              "      <th>Financials</th>\n",
              "      <th>Government Activity</th>\n",
              "      <th>Growth</th>\n",
              "      <th>Healthcare</th>\n",
              "      <th>...</th>\n",
              "      <th>Institutions, Associations &amp; Organizations</th>\n",
              "      <th>Leverage</th>\n",
              "      <th>Market</th>\n",
              "      <th>Momentum</th>\n",
              "      <th>Real Estate</th>\n",
              "      <th>Size</th>\n",
              "      <th>Technology</th>\n",
              "      <th>Utilities</th>\n",
              "      <th>Value</th>\n",
              "      <th>Volatility</th>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>date</th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>2026-03-25</th>\n",
              "      <td>0.186803</td>\n",
              "      <td>0.135197</td>\n",
              "      <td>0.085725</td>\n",
              "      <td>0.103875</td>\n",
              "      <td>0.025571</td>\n",
              "      <td>0.169620</td>\n",
              "      <td>0.064445</td>\n",
              "      <td>0.546927</td>\n",
              "      <td>0.018382</td>\n",
              "      <td>0.111329</td>\n",
              "      <td>...</td>\n",
              "      <td>0.176007</td>\n",
              "      <td>0.026514</td>\n",
              "      <td>0.138444</td>\n",
              "      <td>0.069647</td>\n",
              "      <td>0.088610</td>\n",
              "      <td>0.024487</td>\n",
              "      <td>0.056239</td>\n",
              "      <td>0.127778</td>\n",
              "      <td>0.034710</td>\n",
              "      <td>0.134647</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2026-03-26</th>\n",
              "      <td>0.186054</td>\n",
              "      <td>0.135095</td>\n",
              "      <td>0.085208</td>\n",
              "      <td>0.103440</td>\n",
              "      <td>0.025459</td>\n",
              "      <td>0.174187</td>\n",
              "      <td>0.064205</td>\n",
              "      <td>0.544028</td>\n",
              "      <td>0.018778</td>\n",
              "      <td>0.113726</td>\n",
              "      <td>...</td>\n",
              "      <td>0.175040</td>\n",
              "      <td>0.026642</td>\n",
              "      <td>0.139284</td>\n",
              "      <td>0.071588</td>\n",
              "      <td>0.088279</td>\n",
              "      <td>0.024388</td>\n",
              "      <td>0.056906</td>\n",
              "      <td>0.127632</td>\n",
              "      <td>0.034534</td>\n",
              "      <td>0.136749</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2026-03-27</th>\n",
              "      <td>0.185076</td>\n",
              "      <td>0.136677</td>\n",
              "      <td>0.084701</td>\n",
              "      <td>0.106129</td>\n",
              "      <td>0.025305</td>\n",
              "      <td>0.176637</td>\n",
              "      <td>0.063979</td>\n",
              "      <td>0.541673</td>\n",
              "      <td>0.018925</td>\n",
              "      <td>0.114096</td>\n",
              "      <td>...</td>\n",
              "      <td>0.178136</td>\n",
              "      <td>0.026479</td>\n",
              "      <td>0.140880</td>\n",
              "      <td>0.071862</td>\n",
              "      <td>0.087787</td>\n",
              "      <td>0.024499</td>\n",
              "      <td>0.056771</td>\n",
              "      <td>0.128743</td>\n",
              "      <td>0.034337</td>\n",
              "      <td>0.136551</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2026-03-30</th>\n",
              "      <td>0.184248</td>\n",
              "      <td>0.137411</td>\n",
              "      <td>0.084451</td>\n",
              "      <td>0.105516</td>\n",
              "      <td>0.025218</td>\n",
              "      <td>0.176239</td>\n",
              "      <td>0.064102</td>\n",
              "      <td>0.539096</td>\n",
              "      <td>0.018884</td>\n",
              "      <td>0.114241</td>\n",
              "      <td>...</td>\n",
              "      <td>0.177478</td>\n",
              "      <td>0.026338</td>\n",
              "      <td>0.141172</td>\n",
              "      <td>0.073828</td>\n",
              "      <td>0.087746</td>\n",
              "      <td>0.024738</td>\n",
              "      <td>0.057125</td>\n",
              "      <td>0.127997</td>\n",
              "      <td>0.034140</td>\n",
              "      <td>0.138475</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2026-03-31</th>\n",
              "      <td>0.184671</td>\n",
              "      <td>0.137022</td>\n",
              "      <td>0.083946</td>\n",
              "      <td>0.107057</td>\n",
              "      <td>0.025234</td>\n",
              "      <td>0.183978</td>\n",
              "      <td>0.065007</td>\n",
              "      <td>0.537366</td>\n",
              "      <td>0.018777</td>\n",
              "      <td>0.113578</td>\n",
              "      <td>...</td>\n",
              "      <td>0.178359</td>\n",
              "      <td>0.026605</td>\n",
              "      <td>0.144843</td>\n",
              "      <td>0.074641</td>\n",
              "      <td>0.087296</td>\n",
              "      <td>0.025502</td>\n",
              "      <td>0.056956</td>\n",
              "      <td>0.128733</td>\n",
              "      <td>0.034073</td>\n",
              "      <td>0.144013</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "<p>5 rows × 21 columns</p>\n",
              "</div>"
            ],
            "text/plain": [
              "            Academic & Educational Services  Basic Materials  \\\n",
              "date                                                           \n",
              "2026-03-25                         0.186803         0.135197   \n",
              "2026-03-26                         0.186054         0.135095   \n",
              "2026-03-27                         0.185076         0.136677   \n",
              "2026-03-30                         0.184248         0.137411   \n",
              "2026-03-31                         0.184671         0.137022   \n",
              "\n",
              "            Consumer Cyclicals  Consumer Non-Cyclicals  Dividend    Energy  \\\n",
              "date                                                                         \n",
              "2026-03-25            0.085725                0.103875  0.025571  0.169620   \n",
              "2026-03-26            0.085208                0.103440  0.025459  0.174187   \n",
              "2026-03-27            0.084701                0.106129  0.025305  0.176637   \n",
              "2026-03-30            0.084451                0.105516  0.025218  0.176239   \n",
              "2026-03-31            0.083946                0.107057  0.025234  0.183978   \n",
              "\n",
              "            Financials  Government Activity    Growth  Healthcare  ...  \\\n",
              "date                                                               ...   \n",
              "2026-03-25    0.064445             0.546927  0.018382    0.111329  ...   \n",
              "2026-03-26    0.064205             0.544028  0.018778    0.113726  ...   \n",
              "2026-03-27    0.063979             0.541673  0.018925    0.114096  ...   \n",
              "2026-03-30    0.064102             0.539096  0.018884    0.114241  ...   \n",
              "2026-03-31    0.065007             0.537366  0.018777    0.113578  ...   \n",
              "\n",
              "            Institutions, Associations & Organizations  Leverage    Market  \\\n",
              "date                                                                         \n",
              "2026-03-25                                    0.176007  0.026514  0.138444   \n",
              "2026-03-26                                    0.175040  0.026642  0.139284   \n",
              "2026-03-27                                    0.178136  0.026479  0.140880   \n",
              "2026-03-30                                    0.177478  0.026338  0.141172   \n",
              "2026-03-31                                    0.178359  0.026605  0.144843   \n",
              "\n",
              "            Momentum  Real Estate      Size  Technology  Utilities     Value  \\\n",
              "date                                                                           \n",
              "2026-03-25  0.069647     0.088610  0.024487    0.056239   0.127778  0.034710   \n",
              "2026-03-26  0.071588     0.088279  0.024388    0.056906   0.127632  0.034534   \n",
              "2026-03-27  0.071862     0.087787  0.024499    0.056771   0.128743  0.034337   \n",
              "2026-03-30  0.073828     0.087746  0.024738    0.057125   0.127997  0.034140   \n",
              "2026-03-31  0.074641     0.087296  0.025502    0.056956   0.128733  0.034073   \n",
              "\n",
              "            Volatility  \n",
              "date                    \n",
              "2026-03-25    0.134647  \n",
              "2026-03-26    0.136749  \n",
              "2026-03-27    0.136551  \n",
              "2026-03-30    0.138475  \n",
              "2026-03-31    0.144013  \n",
              "\n",
              "[5 rows x 21 columns]"
            ]
          },
          "execution_count": 8,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# calculate actual factor volatilities from vcovs\n",
        "df_vol = df_vcov.groupby(level=\"date\").apply(\n",
        "    lambda df: pd.Series(np.diag(df) ** 0.5, df.index.droplevel(\"date\"))\n",
        ")\n",
        "df_vol.columns.name = None\n",
        "df_vol.tail()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 9,
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th>Academic &amp; Educational Services</th>\n",
              "      <th>Basic Materials</th>\n",
              "      <th>Consumer Cyclicals</th>\n",
              "      <th>Consumer Non-Cyclicals</th>\n",
              "      <th>Dividend</th>\n",
              "      <th>Energy</th>\n",
              "      <th>Financials</th>\n",
              "      <th>Government Activity</th>\n",
              "      <th>Growth</th>\n",
              "      <th>Healthcare</th>\n",
              "      <th>...</th>\n",
              "      <th>Institutions, Associations &amp; Organizations</th>\n",
              "      <th>Leverage</th>\n",
              "      <th>Market</th>\n",
              "      <th>Momentum</th>\n",
              "      <th>Real Estate</th>\n",
              "      <th>Size</th>\n",
              "      <th>Technology</th>\n",
              "      <th>Utilities</th>\n",
              "      <th>Value</th>\n",
              "      <th>Volatility</th>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>date</th>\n",
              "      <th>factor</th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th rowspan=\"5\" valign=\"top\">2026-03-31</th>\n",
              "      <th>Size</th>\n",
              "      <td>-0.051064</td>\n",
              "      <td>-0.043888</td>\n",
              "      <td>0.055528</td>\n",
              "      <td>-0.392713</td>\n",
              "      <td>-0.120525</td>\n",
              "      <td>-0.032322</td>\n",
              "      <td>0.656639</td>\n",
              "      <td>-0.108453</td>\n",
              "      <td>0.090187</td>\n",
              "      <td>-0.173650</td>\n",
              "      <td>...</td>\n",
              "      <td>-0.159905</td>\n",
              "      <td>0.117890</td>\n",
              "      <td>0.335214</td>\n",
              "      <td>-0.096059</td>\n",
              "      <td>-0.144872</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>-0.167283</td>\n",
              "      <td>-0.367235</td>\n",
              "      <td>0.249031</td>\n",
              "      <td>0.340013</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>Technology</th>\n",
              "      <td>-0.153468</td>\n",
              "      <td>-0.516823</td>\n",
              "      <td>-0.506988</td>\n",
              "      <td>-0.558794</td>\n",
              "      <td>-0.083228</td>\n",
              "      <td>-0.276119</td>\n",
              "      <td>-0.178860</td>\n",
              "      <td>-0.059522</td>\n",
              "      <td>0.192184</td>\n",
              "      <td>-0.399519</td>\n",
              "      <td>...</td>\n",
              "      <td>-0.205639</td>\n",
              "      <td>-0.390468</td>\n",
              "      <td>0.183019</td>\n",
              "      <td>0.161592</td>\n",
              "      <td>-0.406044</td>\n",
              "      <td>-0.167283</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>-0.181858</td>\n",
              "      <td>-0.360320</td>\n",
              "      <td>0.229686</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>Utilities</th>\n",
              "      <td>-0.089278</td>\n",
              "      <td>0.193802</td>\n",
              "      <td>-0.242320</td>\n",
              "      <td>0.437409</td>\n",
              "      <td>0.079585</td>\n",
              "      <td>0.222716</td>\n",
              "      <td>-0.220199</td>\n",
              "      <td>0.153246</td>\n",
              "      <td>-0.128391</td>\n",
              "      <td>0.049687</td>\n",
              "      <td>...</td>\n",
              "      <td>0.302273</td>\n",
              "      <td>-0.265790</td>\n",
              "      <td>-0.298783</td>\n",
              "      <td>0.354842</td>\n",
              "      <td>0.459180</td>\n",
              "      <td>-0.367235</td>\n",
              "      <td>-0.181858</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>-0.230939</td>\n",
              "      <td>-0.171734</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>Value</th>\n",
              "      <td>0.044355</td>\n",
              "      <td>0.292505</td>\n",
              "      <td>0.454107</td>\n",
              "      <td>0.075867</td>\n",
              "      <td>0.151141</td>\n",
              "      <td>0.179119</td>\n",
              "      <td>-0.025266</td>\n",
              "      <td>0.025911</td>\n",
              "      <td>0.103999</td>\n",
              "      <td>0.063748</td>\n",
              "      <td>...</td>\n",
              "      <td>-0.118393</td>\n",
              "      <td>0.422054</td>\n",
              "      <td>0.193675</td>\n",
              "      <td>-0.154199</td>\n",
              "      <td>-0.086144</td>\n",
              "      <td>0.249031</td>\n",
              "      <td>-0.360320</td>\n",
              "      <td>-0.230939</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>0.051918</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>Volatility</th>\n",
              "      <td>-0.365376</td>\n",
              "      <td>-0.152597</td>\n",
              "      <td>-0.277303</td>\n",
              "      <td>-0.484649</td>\n",
              "      <td>0.034258</td>\n",
              "      <td>-0.227436</td>\n",
              "      <td>0.563958</td>\n",
              "      <td>-0.397952</td>\n",
              "      <td>0.028157</td>\n",
              "      <td>-0.445104</td>\n",
              "      <td>...</td>\n",
              "      <td>-0.750944</td>\n",
              "      <td>-0.271474</td>\n",
              "      <td>0.826436</td>\n",
              "      <td>0.352790</td>\n",
              "      <td>-0.194513</td>\n",
              "      <td>0.340013</td>\n",
              "      <td>0.229686</td>\n",
              "      <td>-0.171734</td>\n",
              "      <td>0.051918</td>\n",
              "      <td>1.000000</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "<p>5 rows × 21 columns</p>\n",
              "</div>"
            ],
            "text/plain": [
              "                       Academic & Educational Services  Basic Materials  \\\n",
              "date       factor                                                         \n",
              "2026-03-31 Size                              -0.051064        -0.043888   \n",
              "           Technology                        -0.153468        -0.516823   \n",
              "           Utilities                         -0.089278         0.193802   \n",
              "           Value                              0.044355         0.292505   \n",
              "           Volatility                        -0.365376        -0.152597   \n",
              "\n",
              "                       Consumer Cyclicals  Consumer Non-Cyclicals  Dividend  \\\n",
              "date       factor                                                             \n",
              "2026-03-31 Size                  0.055528               -0.392713 -0.120525   \n",
              "           Technology           -0.506988               -0.558794 -0.083228   \n",
              "           Utilities            -0.242320                0.437409  0.079585   \n",
              "           Value                 0.454107                0.075867  0.151141   \n",
              "           Volatility           -0.277303               -0.484649  0.034258   \n",
              "\n",
              "                         Energy  Financials  Government Activity    Growth  \\\n",
              "date       factor                                                            \n",
              "2026-03-31 Size       -0.032322    0.656639            -0.108453  0.090187   \n",
              "           Technology -0.276119   -0.178860            -0.059522  0.192184   \n",
              "           Utilities   0.222716   -0.220199             0.153246 -0.128391   \n",
              "           Value       0.179119   -0.025266             0.025911  0.103999   \n",
              "           Volatility -0.227436    0.563958            -0.397952  0.028157   \n",
              "\n",
              "                       Healthcare  ...  \\\n",
              "date       factor                  ...   \n",
              "2026-03-31 Size         -0.173650  ...   \n",
              "           Technology   -0.399519  ...   \n",
              "           Utilities     0.049687  ...   \n",
              "           Value         0.063748  ...   \n",
              "           Volatility   -0.445104  ...   \n",
              "\n",
              "                       Institutions, Associations & Organizations  Leverage  \\\n",
              "date       factor                                                             \n",
              "2026-03-31 Size                                         -0.159905  0.117890   \n",
              "           Technology                                   -0.205639 -0.390468   \n",
              "           Utilities                                     0.302273 -0.265790   \n",
              "           Value                                        -0.118393  0.422054   \n",
              "           Volatility                                   -0.750944 -0.271474   \n",
              "\n",
              "                         Market  Momentum  Real Estate      Size  Technology  \\\n",
              "date       factor                                                              \n",
              "2026-03-31 Size        0.335214 -0.096059    -0.144872  1.000000   -0.167283   \n",
              "           Technology  0.183019  0.161592    -0.406044 -0.167283    1.000000   \n",
              "           Utilities  -0.298783  0.354842     0.459180 -0.367235   -0.181858   \n",
              "           Value       0.193675 -0.154199    -0.086144  0.249031   -0.360320   \n",
              "           Volatility  0.826436  0.352790    -0.194513  0.340013    0.229686   \n",
              "\n",
              "                       Utilities     Value  Volatility  \n",
              "date       factor                                       \n",
              "2026-03-31 Size        -0.367235  0.249031    0.340013  \n",
              "           Technology  -0.181858 -0.360320    0.229686  \n",
              "           Utilities    1.000000 -0.230939   -0.171734  \n",
              "           Value       -0.230939  1.000000    0.051918  \n",
              "           Volatility  -0.171734  0.051918    1.000000  \n",
              "\n",
              "[5 rows x 21 columns]"
            ]
          },
          "execution_count": 9,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# calculate actual factor correlations from vcovs\n",
        "df_cor = df_vcov.groupby(level=\"date\").apply(\n",
        "    lambda df: df.droplevel(\"date\")\n",
        "    / np.outer(np.diag(df) ** 0.5, np.diag(df) ** 0.5)\n",
        ")\n",
        "df_cor.tail()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Manually replicating the covariance forecasts\n",
        "\n",
        "We can also estimated the risk model and get the returns directly. From these returns we can construct the covariance forecasts. Bayesline returns dataframes in `polars`, but they can be easily converted to `pandas` dataframes. We also remove the factor group (market, style, industry, etc.) for convenience."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 10,
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>Market</th>\n",
              "      <th>Dividend</th>\n",
              "      <th>Growth</th>\n",
              "      <th>Leverage</th>\n",
              "      <th>Momentum</th>\n",
              "      <th>Size</th>\n",
              "      <th>Value</th>\n",
              "      <th>Volatility</th>\n",
              "      <th>Academic &amp; Educational Services</th>\n",
              "      <th>Basic Materials</th>\n",
              "      <th>...</th>\n",
              "      <th>Consumer Non-Cyclicals</th>\n",
              "      <th>Energy</th>\n",
              "      <th>Financials</th>\n",
              "      <th>Government Activity</th>\n",
              "      <th>Healthcare</th>\n",
              "      <th>Industrials</th>\n",
              "      <th>Institutions, Associations &amp; Organizations</th>\n",
              "      <th>Real Estate</th>\n",
              "      <th>Technology</th>\n",
              "      <th>Utilities</th>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>date</th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>2026-03-25</th>\n",
              "      <td>0.007150</td>\n",
              "      <td>0.000044</td>\n",
              "      <td>-0.002162</td>\n",
              "      <td>-0.001435</td>\n",
              "      <td>0.002522</td>\n",
              "      <td>0.000975</td>\n",
              "      <td>0.000589</td>\n",
              "      <td>0.005781</td>\n",
              "      <td>0.012054</td>\n",
              "      <td>0.010456</td>\n",
              "      <td>...</td>\n",
              "      <td>0.003439</td>\n",
              "      <td>-0.007601</td>\n",
              "      <td>0.000979</td>\n",
              "      <td>-0.063025</td>\n",
              "      <td>0.007372</td>\n",
              "      <td>0.000312</td>\n",
              "      <td>-0.006510</td>\n",
              "      <td>-0.002908</td>\n",
              "      <td>-0.002001</td>\n",
              "      <td>-0.001520</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2026-03-26</th>\n",
              "      <td>-0.012332</td>\n",
              "      <td>0.000855</td>\n",
              "      <td>-0.002477</td>\n",
              "      <td>0.002234</td>\n",
              "      <td>-0.010419</td>\n",
              "      <td>-0.000891</td>\n",
              "      <td>0.000904</td>\n",
              "      <td>-0.016057</td>\n",
              "      <td>0.006894</td>\n",
              "      <td>0.007969</td>\n",
              "      <td>...</td>\n",
              "      <td>0.003670</td>\n",
              "      <td>0.025015</td>\n",
              "      <td>-0.002526</td>\n",
              "      <td>0.012508</td>\n",
              "      <td>0.014999</td>\n",
              "      <td>-0.002526</td>\n",
              "      <td>0.003506</td>\n",
              "      <td>0.003482</td>\n",
              "      <td>-0.006097</td>\n",
              "      <td>0.007255</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2026-03-27</th>\n",
              "      <td>-0.014925</td>\n",
              "      <td>0.000130</td>\n",
              "      <td>0.001790</td>\n",
              "      <td>-0.000079</td>\n",
              "      <td>0.005758</td>\n",
              "      <td>-0.002032</td>\n",
              "      <td>0.000554</td>\n",
              "      <td>-0.007520</td>\n",
              "      <td>0.004359</td>\n",
              "      <td>0.014580</td>\n",
              "      <td>...</td>\n",
              "      <td>0.015039</td>\n",
              "      <td>0.020015</td>\n",
              "      <td>-0.002635</td>\n",
              "      <td>0.018467</td>\n",
              "      <td>-0.008877</td>\n",
              "      <td>0.000675</td>\n",
              "      <td>0.021866</td>\n",
              "      <td>0.001636</td>\n",
              "      <td>-0.002802</td>\n",
              "      <td>0.012549</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2026-03-30</th>\n",
              "      <td>-0.010278</td>\n",
              "      <td>0.001058</td>\n",
              "      <td>-0.000956</td>\n",
              "      <td>0.000582</td>\n",
              "      <td>-0.010674</td>\n",
              "      <td>0.002496</td>\n",
              "      <td>-0.000524</td>\n",
              "      <td>-0.015705</td>\n",
              "      <td>0.006007</td>\n",
              "      <td>0.011824</td>\n",
              "      <td>...</td>\n",
              "      <td>0.001545</td>\n",
              "      <td>-0.008834</td>\n",
              "      <td>0.004623</td>\n",
              "      <td>0.015984</td>\n",
              "      <td>0.007900</td>\n",
              "      <td>-0.003383</td>\n",
              "      <td>0.007038</td>\n",
              "      <td>0.005313</td>\n",
              "      <td>-0.005094</td>\n",
              "      <td>0.001816</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2026-03-31</th>\n",
              "      <td>0.020540</td>\n",
              "      <td>0.001667</td>\n",
              "      <td>-0.000316</td>\n",
              "      <td>-0.002717</td>\n",
              "      <td>0.007813</td>\n",
              "      <td>0.003867</td>\n",
              "      <td>-0.001769</td>\n",
              "      <td>0.024225</td>\n",
              "      <td>-0.013630</td>\n",
              "      <td>-0.006333</td>\n",
              "      <td>...</td>\n",
              "      <td>-0.012291</td>\n",
              "      <td>-0.032145</td>\n",
              "      <td>0.007378</td>\n",
              "      <td>-0.023353</td>\n",
              "      <td>0.001579</td>\n",
              "      <td>-0.000816</td>\n",
              "      <td>-0.015080</td>\n",
              "      <td>-0.002195</td>\n",
              "      <td>0.002577</td>\n",
              "      <td>-0.011257</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "<p>5 rows × 21 columns</p>\n",
              "</div>"
            ],
            "text/plain": [
              "              Market  Dividend    Growth  Leverage  Momentum      Size  \\\n",
              "date                                                                     \n",
              "2026-03-25  0.007150  0.000044 -0.002162 -0.001435  0.002522  0.000975   \n",
              "2026-03-26 -0.012332  0.000855 -0.002477  0.002234 -0.010419 -0.000891   \n",
              "2026-03-27 -0.014925  0.000130  0.001790 -0.000079  0.005758 -0.002032   \n",
              "2026-03-30 -0.010278  0.001058 -0.000956  0.000582 -0.010674  0.002496   \n",
              "2026-03-31  0.020540  0.001667 -0.000316 -0.002717  0.007813  0.003867   \n",
              "\n",
              "               Value  Volatility  Academic & Educational Services  \\\n",
              "date                                                                \n",
              "2026-03-25  0.000589    0.005781                         0.012054   \n",
              "2026-03-26  0.000904   -0.016057                         0.006894   \n",
              "2026-03-27  0.000554   -0.007520                         0.004359   \n",
              "2026-03-30 -0.000524   -0.015705                         0.006007   \n",
              "2026-03-31 -0.001769    0.024225                        -0.013630   \n",
              "\n",
              "            Basic Materials  ...  Consumer Non-Cyclicals    Energy  \\\n",
              "date                         ...                                     \n",
              "2026-03-25         0.010456  ...                0.003439 -0.007601   \n",
              "2026-03-26         0.007969  ...                0.003670  0.025015   \n",
              "2026-03-27         0.014580  ...                0.015039  0.020015   \n",
              "2026-03-30         0.011824  ...                0.001545 -0.008834   \n",
              "2026-03-31        -0.006333  ...               -0.012291 -0.032145   \n",
              "\n",
              "            Financials  Government Activity  Healthcare  Industrials  \\\n",
              "date                                                                   \n",
              "2026-03-25    0.000979            -0.063025    0.007372     0.000312   \n",
              "2026-03-26   -0.002526             0.012508    0.014999    -0.002526   \n",
              "2026-03-27   -0.002635             0.018467   -0.008877     0.000675   \n",
              "2026-03-30    0.004623             0.015984    0.007900    -0.003383   \n",
              "2026-03-31    0.007378            -0.023353    0.001579    -0.000816   \n",
              "\n",
              "            Institutions, Associations & Organizations  Real Estate  \\\n",
              "date                                                                  \n",
              "2026-03-25                                   -0.006510    -0.002908   \n",
              "2026-03-26                                    0.003506     0.003482   \n",
              "2026-03-27                                    0.021866     0.001636   \n",
              "2026-03-30                                    0.007038     0.005313   \n",
              "2026-03-31                                   -0.015080    -0.002195   \n",
              "\n",
              "            Technology  Utilities  \n",
              "date                               \n",
              "2026-03-25   -0.002001  -0.001520  \n",
              "2026-03-26   -0.006097   0.007255  \n",
              "2026-03-27   -0.002802   0.012549  \n",
              "2026-03-30   -0.005094   0.001816  \n",
              "2026-03-31    0.002577  -0.011257  \n",
              "\n",
              "[5 rows x 21 columns]"
            ]
          },
          "execution_count": 10,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "risk_model = bln.equity.riskmodels.load(factorriskmodel_settings).get_model()\n",
        "df_factor_returns = risk_model.fret().tail(-1).to_pandas().set_index(\"date\").rename(columns=lambda c: c.split(\".\")[1])\n",
        "df_factor_returns.tail()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "From these returns we can run standard pandas functions to get the EWMAs."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 11,
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>Market</th>\n",
              "      <th>Dividend</th>\n",
              "      <th>Growth</th>\n",
              "      <th>Leverage</th>\n",
              "      <th>Momentum</th>\n",
              "      <th>Size</th>\n",
              "      <th>Value</th>\n",
              "      <th>Volatility</th>\n",
              "      <th>Academic &amp; Educational Services</th>\n",
              "      <th>Basic Materials</th>\n",
              "      <th>...</th>\n",
              "      <th>Consumer Non-Cyclicals</th>\n",
              "      <th>Energy</th>\n",
              "      <th>Financials</th>\n",
              "      <th>Government Activity</th>\n",
              "      <th>Healthcare</th>\n",
              "      <th>Industrials</th>\n",
              "      <th>Institutions, Associations &amp; Organizations</th>\n",
              "      <th>Real Estate</th>\n",
              "      <th>Technology</th>\n",
              "      <th>Utilities</th>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>date</th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>2026-03-25</th>\n",
              "      <td>0.138444</td>\n",
              "      <td>0.025571</td>\n",
              "      <td>0.018382</td>\n",
              "      <td>0.026514</td>\n",
              "      <td>0.069647</td>\n",
              "      <td>0.024487</td>\n",
              "      <td>0.034710</td>\n",
              "      <td>0.134647</td>\n",
              "      <td>0.186803</td>\n",
              "      <td>0.135197</td>\n",
              "      <td>...</td>\n",
              "      <td>0.103875</td>\n",
              "      <td>0.169620</td>\n",
              "      <td>0.064445</td>\n",
              "      <td>0.546928</td>\n",
              "      <td>0.111329</td>\n",
              "      <td>0.068609</td>\n",
              "      <td>0.176007</td>\n",
              "      <td>0.088610</td>\n",
              "      <td>0.056239</td>\n",
              "      <td>0.127778</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2026-03-26</th>\n",
              "      <td>0.139284</td>\n",
              "      <td>0.025459</td>\n",
              "      <td>0.018778</td>\n",
              "      <td>0.026642</td>\n",
              "      <td>0.071588</td>\n",
              "      <td>0.024388</td>\n",
              "      <td>0.034534</td>\n",
              "      <td>0.136749</td>\n",
              "      <td>0.186054</td>\n",
              "      <td>0.135095</td>\n",
              "      <td>...</td>\n",
              "      <td>0.103440</td>\n",
              "      <td>0.174187</td>\n",
              "      <td>0.064205</td>\n",
              "      <td>0.544028</td>\n",
              "      <td>0.113726</td>\n",
              "      <td>0.068333</td>\n",
              "      <td>0.175040</td>\n",
              "      <td>0.088279</td>\n",
              "      <td>0.056906</td>\n",
              "      <td>0.127632</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2026-03-27</th>\n",
              "      <td>0.140880</td>\n",
              "      <td>0.025305</td>\n",
              "      <td>0.018925</td>\n",
              "      <td>0.026479</td>\n",
              "      <td>0.071862</td>\n",
              "      <td>0.024499</td>\n",
              "      <td>0.034337</td>\n",
              "      <td>0.136551</td>\n",
              "      <td>0.185076</td>\n",
              "      <td>0.136677</td>\n",
              "      <td>...</td>\n",
              "      <td>0.106129</td>\n",
              "      <td>0.176637</td>\n",
              "      <td>0.063979</td>\n",
              "      <td>0.541673</td>\n",
              "      <td>0.114096</td>\n",
              "      <td>0.067926</td>\n",
              "      <td>0.178136</td>\n",
              "      <td>0.087787</td>\n",
              "      <td>0.056771</td>\n",
              "      <td>0.128743</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2026-03-30</th>\n",
              "      <td>0.141172</td>\n",
              "      <td>0.025218</td>\n",
              "      <td>0.018884</td>\n",
              "      <td>0.026338</td>\n",
              "      <td>0.073828</td>\n",
              "      <td>0.024738</td>\n",
              "      <td>0.034140</td>\n",
              "      <td>0.138475</td>\n",
              "      <td>0.184248</td>\n",
              "      <td>0.137411</td>\n",
              "      <td>...</td>\n",
              "      <td>0.105516</td>\n",
              "      <td>0.176239</td>\n",
              "      <td>0.064102</td>\n",
              "      <td>0.539096</td>\n",
              "      <td>0.114241</td>\n",
              "      <td>0.067771</td>\n",
              "      <td>0.177478</td>\n",
              "      <td>0.087746</td>\n",
              "      <td>0.057125</td>\n",
              "      <td>0.127997</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2026-03-31</th>\n",
              "      <td>0.144843</td>\n",
              "      <td>0.025234</td>\n",
              "      <td>0.018777</td>\n",
              "      <td>0.026605</td>\n",
              "      <td>0.074641</td>\n",
              "      <td>0.025502</td>\n",
              "      <td>0.034073</td>\n",
              "      <td>0.144013</td>\n",
              "      <td>0.184671</td>\n",
              "      <td>0.137022</td>\n",
              "      <td>...</td>\n",
              "      <td>0.107057</td>\n",
              "      <td>0.183978</td>\n",
              "      <td>0.065007</td>\n",
              "      <td>0.537366</td>\n",
              "      <td>0.113578</td>\n",
              "      <td>0.067373</td>\n",
              "      <td>0.178359</td>\n",
              "      <td>0.087296</td>\n",
              "      <td>0.056956</td>\n",
              "      <td>0.128733</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "<p>5 rows × 21 columns</p>\n",
              "</div>"
            ],
            "text/plain": [
              "              Market  Dividend    Growth  Leverage  Momentum      Size  \\\n",
              "date                                                                     \n",
              "2026-03-25  0.138444  0.025571  0.018382  0.026514  0.069647  0.024487   \n",
              "2026-03-26  0.139284  0.025459  0.018778  0.026642  0.071588  0.024388   \n",
              "2026-03-27  0.140880  0.025305  0.018925  0.026479  0.071862  0.024499   \n",
              "2026-03-30  0.141172  0.025218  0.018884  0.026338  0.073828  0.024738   \n",
              "2026-03-31  0.144843  0.025234  0.018777  0.026605  0.074641  0.025502   \n",
              "\n",
              "               Value  Volatility  Academic & Educational Services  \\\n",
              "date                                                                \n",
              "2026-03-25  0.034710    0.134647                         0.186803   \n",
              "2026-03-26  0.034534    0.136749                         0.186054   \n",
              "2026-03-27  0.034337    0.136551                         0.185076   \n",
              "2026-03-30  0.034140    0.138475                         0.184248   \n",
              "2026-03-31  0.034073    0.144013                         0.184671   \n",
              "\n",
              "            Basic Materials  ...  Consumer Non-Cyclicals    Energy  \\\n",
              "date                         ...                                     \n",
              "2026-03-25         0.135197  ...                0.103875  0.169620   \n",
              "2026-03-26         0.135095  ...                0.103440  0.174187   \n",
              "2026-03-27         0.136677  ...                0.106129  0.176637   \n",
              "2026-03-30         0.137411  ...                0.105516  0.176239   \n",
              "2026-03-31         0.137022  ...                0.107057  0.183978   \n",
              "\n",
              "            Financials  Government Activity  Healthcare  Industrials  \\\n",
              "date                                                                   \n",
              "2026-03-25    0.064445             0.546928    0.111329     0.068609   \n",
              "2026-03-26    0.064205             0.544028    0.113726     0.068333   \n",
              "2026-03-27    0.063979             0.541673    0.114096     0.067926   \n",
              "2026-03-30    0.064102             0.539096    0.114241     0.067771   \n",
              "2026-03-31    0.065007             0.537366    0.113578     0.067373   \n",
              "\n",
              "            Institutions, Associations & Organizations  Real Estate  \\\n",
              "date                                                                  \n",
              "2026-03-25                                    0.176007     0.088610   \n",
              "2026-03-26                                    0.175040     0.088279   \n",
              "2026-03-27                                    0.178136     0.087787   \n",
              "2026-03-30                                    0.177478     0.087746   \n",
              "2026-03-31                                    0.178359     0.087296   \n",
              "\n",
              "            Technology  Utilities  \n",
              "date                               \n",
              "2026-03-25    0.056239   0.127778  \n",
              "2026-03-26    0.056906   0.127632  \n",
              "2026-03-27    0.056771   0.128743  \n",
              "2026-03-30    0.057125   0.127997  \n",
              "2026-03-31    0.056956   0.128733  \n",
              "\n",
              "[5 rows x 21 columns]"
            ]
          },
          "execution_count": 11,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# calculate expected factor volatilities using the ewma\n",
        "df_vol_tieout = (\n",
        "    pd.DataFrame(df_factor_returns**2)\n",
        "    .ewm(halflife=report_settings.factor_cov_settings.halflife_vol)\n",
        "    .mean()\n",
        "    .astype(np.float32)\n",
        "    ** 0.5\n",
        "    * 252**0.5\n",
        ")\n",
        "df_vol_tieout.tail()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 12,
      "metadata": {},
      "outputs": [],
      "source": [
        "pd.testing.assert_frame_equal(df_vol, df_vol_tieout, check_column_type=False, check_categorical=False, check_like=True)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The correlation are a bit more involved. We need to create the outer products and then run EWMAs on each cell in the outer product matrix."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 13,
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th>Market</th>\n",
              "      <th>Dividend</th>\n",
              "      <th>Growth</th>\n",
              "      <th>Leverage</th>\n",
              "      <th>Momentum</th>\n",
              "      <th>Size</th>\n",
              "      <th>Value</th>\n",
              "      <th>Volatility</th>\n",
              "      <th>Academic &amp; Educational Services</th>\n",
              "      <th>Basic Materials</th>\n",
              "      <th>...</th>\n",
              "      <th>Consumer Non-Cyclicals</th>\n",
              "      <th>Energy</th>\n",
              "      <th>Financials</th>\n",
              "      <th>Government Activity</th>\n",
              "      <th>Healthcare</th>\n",
              "      <th>Industrials</th>\n",
              "      <th>Institutions, Associations &amp; Organizations</th>\n",
              "      <th>Real Estate</th>\n",
              "      <th>Technology</th>\n",
              "      <th>Utilities</th>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>date</th>\n",
              "      <th>factor</th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "      <th></th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th rowspan=\"5\" valign=\"top\">2025-04-01</th>\n",
              "      <th>Market</th>\n",
              "      <td>1.000000</td>\n",
              "      <td>-1.000000</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>-1.000000</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>-1.000000</td>\n",
              "      <td>...</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>-1.000000</td>\n",
              "      <td>-1.000000</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>-1.000000</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>1.000000</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>Dividend</th>\n",
              "      <td>-1.000000</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>-1.000000</td>\n",
              "      <td>-1.000000</td>\n",
              "      <td>-1.000000</td>\n",
              "      <td>-1.000000</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>-1.000000</td>\n",
              "      <td>-1.000000</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>...</td>\n",
              "      <td>-1.000000</td>\n",
              "      <td>-1.000000</td>\n",
              "      <td>-1.000000</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>-1.000000</td>\n",
              "      <td>-1.000000</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>-1.000000</td>\n",
              "      <td>-1.000000</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>Growth</th>\n",
              "      <td>1.000000</td>\n",
              "      <td>-1.000000</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>-1.000000</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>-1.000000</td>\n",
              "      <td>...</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>-1.000000</td>\n",
              "      <td>-1.000000</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>-1.000000</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>1.000000</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>Leverage</th>\n",
              "      <td>1.000000</td>\n",
              "      <td>-1.000000</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>-1.000000</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>-1.000000</td>\n",
              "      <td>...</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>-1.000000</td>\n",
              "      <td>-1.000000</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>-1.000000</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>1.000000</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>Momentum</th>\n",
              "      <td>1.000000</td>\n",
              "      <td>-1.000000</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>-1.000000</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>-1.000000</td>\n",
              "      <td>...</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>-1.000000</td>\n",
              "      <td>-1.000000</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>-1.000000</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>1.000000</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>...</th>\n",
              "      <th>...</th>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "      <td>...</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th rowspan=\"5\" valign=\"top\">2026-03-31</th>\n",
              "      <th>Industrials</th>\n",
              "      <td>0.170070</td>\n",
              "      <td>-0.044535</td>\n",
              "      <td>-0.001732</td>\n",
              "      <td>0.282298</td>\n",
              "      <td>0.075855</td>\n",
              "      <td>0.135843</td>\n",
              "      <td>0.312093</td>\n",
              "      <td>0.112918</td>\n",
              "      <td>0.055694</td>\n",
              "      <td>0.375272</td>\n",
              "      <td>...</td>\n",
              "      <td>0.137219</td>\n",
              "      <td>0.076103</td>\n",
              "      <td>0.085461</td>\n",
              "      <td>-0.026741</td>\n",
              "      <td>-0.059674</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>-0.077878</td>\n",
              "      <td>0.071112</td>\n",
              "      <td>-0.503669</td>\n",
              "      <td>0.036674</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>Institutions, Associations &amp; Organizations</th>\n",
              "      <td>-0.933890</td>\n",
              "      <td>-0.033733</td>\n",
              "      <td>0.117979</td>\n",
              "      <td>0.094535</td>\n",
              "      <td>0.157461</td>\n",
              "      <td>-0.159906</td>\n",
              "      <td>-0.118393</td>\n",
              "      <td>-0.750945</td>\n",
              "      <td>0.227706</td>\n",
              "      <td>0.111177</td>\n",
              "      <td>...</td>\n",
              "      <td>0.461493</td>\n",
              "      <td>0.291784</td>\n",
              "      <td>-0.292655</td>\n",
              "      <td>0.420177</td>\n",
              "      <td>0.212377</td>\n",
              "      <td>-0.077878</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>0.140786</td>\n",
              "      <td>-0.205639</td>\n",
              "      <td>0.302273</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>Real Estate</th>\n",
              "      <td>-0.148931</td>\n",
              "      <td>0.074664</td>\n",
              "      <td>-0.101923</td>\n",
              "      <td>0.084998</td>\n",
              "      <td>-0.011548</td>\n",
              "      <td>-0.144872</td>\n",
              "      <td>-0.086143</td>\n",
              "      <td>-0.194514</td>\n",
              "      <td>0.034206</td>\n",
              "      <td>0.163428</td>\n",
              "      <td>...</td>\n",
              "      <td>0.296618</td>\n",
              "      <td>0.086221</td>\n",
              "      <td>-0.015049</td>\n",
              "      <td>0.077926</td>\n",
              "      <td>0.153705</td>\n",
              "      <td>0.071112</td>\n",
              "      <td>0.140786</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>-0.406044</td>\n",
              "      <td>0.459180</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>Technology</th>\n",
              "      <td>0.183019</td>\n",
              "      <td>-0.083228</td>\n",
              "      <td>0.192184</td>\n",
              "      <td>-0.390468</td>\n",
              "      <td>0.161592</td>\n",
              "      <td>-0.167283</td>\n",
              "      <td>-0.360320</td>\n",
              "      <td>0.229686</td>\n",
              "      <td>-0.153468</td>\n",
              "      <td>-0.516823</td>\n",
              "      <td>...</td>\n",
              "      <td>-0.558795</td>\n",
              "      <td>-0.276119</td>\n",
              "      <td>-0.178860</td>\n",
              "      <td>-0.059522</td>\n",
              "      <td>-0.399519</td>\n",
              "      <td>-0.503669</td>\n",
              "      <td>-0.205639</td>\n",
              "      <td>-0.406044</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>-0.181858</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>Utilities</th>\n",
              "      <td>-0.298784</td>\n",
              "      <td>0.079584</td>\n",
              "      <td>-0.128391</td>\n",
              "      <td>-0.265789</td>\n",
              "      <td>0.354843</td>\n",
              "      <td>-0.367235</td>\n",
              "      <td>-0.230939</td>\n",
              "      <td>-0.171734</td>\n",
              "      <td>-0.089277</td>\n",
              "      <td>0.193802</td>\n",
              "      <td>...</td>\n",
              "      <td>0.437409</td>\n",
              "      <td>0.222716</td>\n",
              "      <td>-0.220199</td>\n",
              "      <td>0.153245</td>\n",
              "      <td>0.049687</td>\n",
              "      <td>0.036674</td>\n",
              "      <td>0.302273</td>\n",
              "      <td>0.459180</td>\n",
              "      <td>-0.181858</td>\n",
              "      <td>1.000000</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "<p>5271 rows × 21 columns</p>\n",
              "</div>"
            ],
            "text/plain": [
              "                                                         Market  Dividend  \\\n",
              "date       factor                                                           \n",
              "2025-04-01 Market                                      1.000000 -1.000000   \n",
              "           Dividend                                   -1.000000  1.000000   \n",
              "           Growth                                      1.000000 -1.000000   \n",
              "           Leverage                                    1.000000 -1.000000   \n",
              "           Momentum                                    1.000000 -1.000000   \n",
              "...                                                         ...       ...   \n",
              "2026-03-31 Industrials                                 0.170070 -0.044535   \n",
              "           Institutions, Associations & Organizations -0.933890 -0.033733   \n",
              "           Real Estate                                -0.148931  0.074664   \n",
              "           Technology                                  0.183019 -0.083228   \n",
              "           Utilities                                  -0.298784  0.079584   \n",
              "\n",
              "                                                         Growth  Leverage  \\\n",
              "date       factor                                                           \n",
              "2025-04-01 Market                                      1.000000  1.000000   \n",
              "           Dividend                                   -1.000000 -1.000000   \n",
              "           Growth                                      1.000000  1.000000   \n",
              "           Leverage                                    1.000000  1.000000   \n",
              "           Momentum                                    1.000000  1.000000   \n",
              "...                                                         ...       ...   \n",
              "2026-03-31 Industrials                                -0.001732  0.282298   \n",
              "           Institutions, Associations & Organizations  0.117979  0.094535   \n",
              "           Real Estate                                -0.101923  0.084998   \n",
              "           Technology                                  0.192184 -0.390468   \n",
              "           Utilities                                  -0.128391 -0.265789   \n",
              "\n",
              "                                                       Momentum      Size  \\\n",
              "date       factor                                                           \n",
              "2025-04-01 Market                                      1.000000  1.000000   \n",
              "           Dividend                                   -1.000000 -1.000000   \n",
              "           Growth                                      1.000000  1.000000   \n",
              "           Leverage                                    1.000000  1.000000   \n",
              "           Momentum                                    1.000000  1.000000   \n",
              "...                                                         ...       ...   \n",
              "2026-03-31 Industrials                                 0.075855  0.135843   \n",
              "           Institutions, Associations & Organizations  0.157461 -0.159906   \n",
              "           Real Estate                                -0.011548 -0.144872   \n",
              "           Technology                                  0.161592 -0.167283   \n",
              "           Utilities                                   0.354843 -0.367235   \n",
              "\n",
              "                                                          Value  Volatility  \\\n",
              "date       factor                                                             \n",
              "2025-04-01 Market                                     -1.000000    1.000000   \n",
              "           Dividend                                    1.000000   -1.000000   \n",
              "           Growth                                     -1.000000    1.000000   \n",
              "           Leverage                                   -1.000000    1.000000   \n",
              "           Momentum                                   -1.000000    1.000000   \n",
              "...                                                         ...         ...   \n",
              "2026-03-31 Industrials                                 0.312093    0.112918   \n",
              "           Institutions, Associations & Organizations -0.118393   -0.750945   \n",
              "           Real Estate                                -0.086143   -0.194514   \n",
              "           Technology                                 -0.360320    0.229686   \n",
              "           Utilities                                  -0.230939   -0.171734   \n",
              "\n",
              "                                                       Academic & Educational Services  \\\n",
              "date       factor                                                                        \n",
              "2025-04-01 Market                                                             1.000000   \n",
              "           Dividend                                                          -1.000000   \n",
              "           Growth                                                             1.000000   \n",
              "           Leverage                                                           1.000000   \n",
              "           Momentum                                                           1.000000   \n",
              "...                                                                                ...   \n",
              "2026-03-31 Industrials                                                        0.055694   \n",
              "           Institutions, Associations & Organizations                         0.227706   \n",
              "           Real Estate                                                        0.034206   \n",
              "           Technology                                                        -0.153468   \n",
              "           Utilities                                                         -0.089277   \n",
              "\n",
              "                                                       Basic Materials  ...  \\\n",
              "date       factor                                                       ...   \n",
              "2025-04-01 Market                                            -1.000000  ...   \n",
              "           Dividend                                           1.000000  ...   \n",
              "           Growth                                            -1.000000  ...   \n",
              "           Leverage                                          -1.000000  ...   \n",
              "           Momentum                                          -1.000000  ...   \n",
              "...                                                                ...  ...   \n",
              "2026-03-31 Industrials                                        0.375272  ...   \n",
              "           Institutions, Associations & Organizations         0.111177  ...   \n",
              "           Real Estate                                        0.163428  ...   \n",
              "           Technology                                        -0.516823  ...   \n",
              "           Utilities                                          0.193802  ...   \n",
              "\n",
              "                                                       Consumer Non-Cyclicals  \\\n",
              "date       factor                                                               \n",
              "2025-04-01 Market                                                    1.000000   \n",
              "           Dividend                                                 -1.000000   \n",
              "           Growth                                                    1.000000   \n",
              "           Leverage                                                  1.000000   \n",
              "           Momentum                                                  1.000000   \n",
              "...                                                                       ...   \n",
              "2026-03-31 Industrials                                               0.137219   \n",
              "           Institutions, Associations & Organizations                0.461493   \n",
              "           Real Estate                                               0.296618   \n",
              "           Technology                                               -0.558795   \n",
              "           Utilities                                                 0.437409   \n",
              "\n",
              "                                                         Energy  Financials  \\\n",
              "date       factor                                                             \n",
              "2025-04-01 Market                                      1.000000    1.000000   \n",
              "           Dividend                                   -1.000000   -1.000000   \n",
              "           Growth                                      1.000000    1.000000   \n",
              "           Leverage                                    1.000000    1.000000   \n",
              "           Momentum                                    1.000000    1.000000   \n",
              "...                                                         ...         ...   \n",
              "2026-03-31 Industrials                                 0.076103    0.085461   \n",
              "           Institutions, Associations & Organizations  0.291784   -0.292655   \n",
              "           Real Estate                                 0.086221   -0.015049   \n",
              "           Technology                                 -0.276119   -0.178860   \n",
              "           Utilities                                   0.222716   -0.220199   \n",
              "\n",
              "                                                       Government Activity  \\\n",
              "date       factor                                                            \n",
              "2025-04-01 Market                                                -1.000000   \n",
              "           Dividend                                               1.000000   \n",
              "           Growth                                                -1.000000   \n",
              "           Leverage                                              -1.000000   \n",
              "           Momentum                                              -1.000000   \n",
              "...                                                                    ...   \n",
              "2026-03-31 Industrials                                           -0.026741   \n",
              "           Institutions, Associations & Organizations             0.420177   \n",
              "           Real Estate                                            0.077926   \n",
              "           Technology                                            -0.059522   \n",
              "           Utilities                                              0.153245   \n",
              "\n",
              "                                                       Healthcare  \\\n",
              "date       factor                                                   \n",
              "2025-04-01 Market                                       -1.000000   \n",
              "           Dividend                                      1.000000   \n",
              "           Growth                                       -1.000000   \n",
              "           Leverage                                     -1.000000   \n",
              "           Momentum                                     -1.000000   \n",
              "...                                                           ...   \n",
              "2026-03-31 Industrials                                  -0.059674   \n",
              "           Institutions, Associations & Organizations    0.212377   \n",
              "           Real Estate                                   0.153705   \n",
              "           Technology                                   -0.399519   \n",
              "           Utilities                                     0.049687   \n",
              "\n",
              "                                                       Industrials  \\\n",
              "date       factor                                                    \n",
              "2025-04-01 Market                                         1.000000   \n",
              "           Dividend                                      -1.000000   \n",
              "           Growth                                         1.000000   \n",
              "           Leverage                                       1.000000   \n",
              "           Momentum                                       1.000000   \n",
              "...                                                            ...   \n",
              "2026-03-31 Industrials                                    1.000000   \n",
              "           Institutions, Associations & Organizations    -0.077878   \n",
              "           Real Estate                                    0.071112   \n",
              "           Technology                                    -0.503669   \n",
              "           Utilities                                      0.036674   \n",
              "\n",
              "                                                       Institutions, Associations & Organizations  \\\n",
              "date       factor                                                                                   \n",
              "2025-04-01 Market                                                                        1.000000   \n",
              "           Dividend                                                                     -1.000000   \n",
              "           Growth                                                                        1.000000   \n",
              "           Leverage                                                                      1.000000   \n",
              "           Momentum                                                                      1.000000   \n",
              "...                                                                                           ...   \n",
              "2026-03-31 Industrials                                                                  -0.077878   \n",
              "           Institutions, Associations & Organizations                                    1.000000   \n",
              "           Real Estate                                                                   0.140786   \n",
              "           Technology                                                                   -0.205639   \n",
              "           Utilities                                                                     0.302273   \n",
              "\n",
              "                                                       Real Estate  \\\n",
              "date       factor                                                    \n",
              "2025-04-01 Market                                        -1.000000   \n",
              "           Dividend                                       1.000000   \n",
              "           Growth                                        -1.000000   \n",
              "           Leverage                                      -1.000000   \n",
              "           Momentum                                      -1.000000   \n",
              "...                                                            ...   \n",
              "2026-03-31 Industrials                                    0.071112   \n",
              "           Institutions, Associations & Organizations     0.140786   \n",
              "           Real Estate                                    1.000000   \n",
              "           Technology                                    -0.406044   \n",
              "           Utilities                                      0.459180   \n",
              "\n",
              "                                                       Technology  Utilities  \n",
              "date       factor                                                             \n",
              "2025-04-01 Market                                        1.000000   1.000000  \n",
              "           Dividend                                     -1.000000  -1.000000  \n",
              "           Growth                                        1.000000   1.000000  \n",
              "           Leverage                                      1.000000   1.000000  \n",
              "           Momentum                                      1.000000   1.000000  \n",
              "...                                                           ...        ...  \n",
              "2026-03-31 Industrials                                  -0.503669   0.036674  \n",
              "           Institutions, Associations & Organizations   -0.205639   0.302273  \n",
              "           Real Estate                                  -0.406044   0.459180  \n",
              "           Technology                                    1.000000  -0.181858  \n",
              "           Utilities                                    -0.181858   1.000000  \n",
              "\n",
              "[5271 rows x 21 columns]"
            ]
          },
          "execution_count": 13,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# calculate the ewma on the outer product (vcov with mean zero)\n",
        "df_factor_returns_outer = df_factor_returns.groupby(\"date\").apply(\n",
        "    lambda df: pd.DataFrame(np.outer(df, df), df.columns, df.columns)\n",
        ")\n",
        "df_factor_returns_outer.index.names = [\"date\", \"factor\"]\n",
        "df_cor_tieout = (\n",
        "    pd.DataFrame(df_factor_returns_outer)\n",
        "    .unstack()\n",
        "    .ewm(halflife=report_settings.factor_cov_settings.halflife_cor)\n",
        "    .mean()\n",
        "    .stack(future_stack=True)\n",
        "    .reindex(df_factor_returns_outer.columns, axis=1)\n",
        "    .groupby(\"date\")\n",
        "    .apply(\n",
        "        lambda df: df.droplevel(\"date\")\n",
        "        / np.outer(np.diag(df) ** 0.5, np.diag(df) ** 0.5)\n",
        "    )\n",
        "    .astype(np.float32)\n",
        ")\n",
        "\n",
        "df_cor_tieout"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 14,
      "metadata": {},
      "outputs": [],
      "source": [
        "pd.testing.assert_frame_equal(df_cor, df_cor_tieout, check_index_type=False, check_categorical=False, check_like=True, atol=1e-5)"
      ]
    }
  ],
  "metadata": {
    "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": 2
}