{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "7ffc94eb",
      "metadata": {},
      "source": [
        "# Uploading New Exposures to Existing Dataset and Updating the Risk Dataset\n",
        "\n",
        "Use this notebook to append new exposure data to an existing upload and update an existing risk dataset."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "id": "aacb4bc0",
      "metadata": {},
      "outputs": [],
      "source": [
        "import datetime as dt\n",
        "from pathlib import Path\n",
        "\n",
        "from tqdm import tqdm\n",
        "\n",
        "from bayesline.apiclient import BayeslineApiClient\n",
        "from bayesline.api.equity import (\n",
        "    ContinuousExposureGroupSettings,\n",
        "    ExposureSettings, \n",
        "    UniverseSettings,\n",
        ")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "a210bdb7",
      "metadata": {
        "tags": [
          "skip-execution"
        ]
      },
      "outputs": [],
      "source": [
        "bln = BayeslineApiClient.new_client(\n",
        "    endpoint=\"https://[ENDPOINT]\",\n",
        "    api_key=\"[API-KEY]\",\n",
        ")"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "4c70b88c",
      "metadata": {},
      "source": [
        "## Updating New Exposure Files"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "id": "cce47038",
      "metadata": {},
      "outputs": [],
      "source": [
        "exposure_dir = Path(\"PATH/TO/EXPOSURES\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 4,
      "id": "eddc08b3",
      "metadata": {},
      "outputs": [],
      "source": [
        "exposure_dataset_name = \"My-Exposures\""
      ]
    },
    {
      "cell_type": "markdown",
      "id": "f37bfa31",
      "metadata": {},
      "source": [
        "Below gets the exposure uploader for the chosen dataset name `My-Exposures`. This dataset is assummed to be already created since we demonstrate a catch up upload. See the [Uploaders Tutorial](https://docs.bayesline.com/0.9.2/notebooks/tutorial_uploaders.html) for a deep dive into the `Uploaders API`."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 6,
      "id": "83717d79",
      "metadata": {},
      "outputs": [],
      "source": [
        "exposure_uploader = bln.equity.uploaders.get_data_type(\"exposures\")\n",
        "uploader = exposure_uploader.get_dataset(dataset=exposure_dataset_name)"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "56753e2c",
      "metadata": {},
      "source": [
        "Below we list the existing files in the provided folder and filter out all dates for which we already processed files in a previous run."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 7,
      "id": "aaa246e1",
      "metadata": {},
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Got 30 new dates\n"
          ]
        }
      ],
      "source": [
        "# list all csv files and group them by year\n",
        "# expects file pattern \"*_YYYY-MM-DD.csv\"\n",
        "\n",
        "files = list(exposure_dir.glob(\"*.csv\"))\n",
        "file_date_strs = [file.name.split(\"_\")[-1].replace(\"-\", \"\")[:8] for file in files]\n",
        "\n",
        "available_dates = [\n",
        "    dt.date(int(d[:4]), int(d[4:6]), int(d[6:8])) for d in file_date_strs\n",
        "]\n",
        "\n",
        "existing_dates = (\n",
        "    uploader.get_data(columns=[\"date\"], unique=True).collect().to_series().to_list()\n",
        ")\n",
        "\n",
        "new_dates = sorted(set(available_dates) - set(existing_dates))\n",
        "\n",
        "print(f\"Got {len(new_dates)} new dates\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 8,
      "id": "d3d51e8e",
      "metadata": {},
      "outputs": [],
      "source": [
        "files_by_date = {\n",
        "    dt.date(int(d[:4]), int(d[4:6]), int(d[6:8])): f for d, f in zip(file_date_strs, files)\n",
        "}"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "ebee4c21",
      "metadata": {},
      "source": [
        "As a next step we iterate over each `csv` file and stage it. Note that for large amounts of files it's much more performant to upload `zip` files instead. See this [recipe](https://docs.bayesline.com/0.9.2/notebooks/recipe_daily_exposure_upload.html) for details.\n",
        "\n",
        "See the [Uploaders Tutorial](https://docs.bayesline.com/0.9.2/notebooks/tutorial_uploaders.html#staging-data) for more details on the *staging* and *commit* concepts."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 9,
      "id": "f7a87e8e",
      "metadata": {
        "tags": [
          "remove-output"
        ]
      },
      "outputs": [
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "\r",
            "  0%|          | 0/30 [00:00<?, ?it/s]"
          ]
        },
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "\r",
            "  3%|▎         | 1/30 [00:01<00:37,  1.29s/it]"
          ]
        },
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "\r",
            "  7%|▋         | 2/30 [00:02<00:34,  1.22s/it]"
          ]
        },
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "\r",
            " 10%|█         | 3/30 [00:03<00:31,  1.18s/it]"
          ]
        },
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "\r",
            " 13%|█▎        | 4/30 [00:04<00:30,  1.18s/it]"
          ]
        },
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "\r",
            " 17%|█▋        | 5/30 [00:05<00:29,  1.18s/it]"
          ]
        },
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "\r",
            " 20%|██        | 6/30 [00:06<00:27,  1.13s/it]"
          ]
        },
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "\r",
            " 23%|██▎       | 7/30 [00:08<00:26,  1.13s/it]"
          ]
        },
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "\r",
            " 27%|██▋       | 8/30 [00:09<00:24,  1.12s/it]"
          ]
        },
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "\r",
            " 30%|███       | 9/30 [00:10<00:22,  1.09s/it]"
          ]
        },
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "\r",
            " 33%|███▎      | 10/30 [00:11<00:21,  1.07s/it]"
          ]
        },
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "\r",
            " 37%|███▋      | 11/30 [00:12<00:21,  1.12s/it]"
          ]
        },
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "\r",
            " 40%|████      | 12/30 [00:13<00:19,  1.10s/it]"
          ]
        },
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "\r",
            " 43%|████▎     | 13/30 [00:14<00:18,  1.09s/it]"
          ]
        },
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "\r",
            " 47%|████▋     | 14/30 [00:15<00:17,  1.10s/it]"
          ]
        },
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "\r",
            " 50%|█████     | 15/30 [00:16<00:16,  1.08s/it]"
          ]
        },
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "\r",
            " 53%|█████▎    | 16/30 [00:17<00:15,  1.09s/it]"
          ]
        },
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "\r",
            " 57%|█████▋    | 17/30 [00:18<00:14,  1.09s/it]"
          ]
        },
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "\r",
            " 60%|██████    | 18/30 [00:20<00:12,  1.08s/it]"
          ]
        },
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "\r",
            " 63%|██████▎   | 19/30 [00:21<00:12,  1.10s/it]"
          ]
        },
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "\r",
            " 67%|██████▋   | 20/30 [00:22<00:10,  1.09s/it]"
          ]
        },
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "\r",
            " 70%|███████   | 21/30 [00:23<00:09,  1.07s/it]"
          ]
        },
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "\r",
            " 73%|███████▎  | 22/30 [00:24<00:08,  1.06s/it]"
          ]
        },
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "\r",
            " 77%|███████▋  | 23/30 [00:25<00:07,  1.05s/it]"
          ]
        },
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "\r",
            " 80%|████████  | 24/30 [00:26<00:06,  1.04s/it]"
          ]
        },
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "\r",
            " 83%|████████▎ | 25/30 [00:27<00:05,  1.04s/it]"
          ]
        },
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "\r",
            " 87%|████████▋ | 26/30 [00:28<00:04,  1.03s/it]"
          ]
        },
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "\r",
            " 90%|█████████ | 27/30 [00:29<00:03,  1.03s/it]"
          ]
        },
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "\r",
            " 93%|█████████▎| 28/30 [00:30<00:02,  1.03s/it]"
          ]
        },
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "\r",
            " 97%|█████████▋| 29/30 [00:31<00:01,  1.03s/it]"
          ]
        },
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "\r",
            "100%|██████████| 30/30 [00:32<00:00,  1.05s/it]"
          ]
        },
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "\r",
            "100%|██████████| 30/30 [00:32<00:00,  1.09s/it]"
          ]
        },
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "\n"
          ]
        }
      ],
      "source": [
        "for date in tqdm(new_dates):\n",
        "    file = files_by_date[date]\n",
        "    result = uploader.stage_file(file)\n",
        "    assert result.success"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 10,
      "id": "c47c0e38",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "UploadCommitResult(version=2, committed_names=['exposures_2025-06-01', 'exposures_2025-06-02', 'exposures_2025-06-03', 'exposures_2025-06-04', 'exposures_2025-06-05', 'exposures_2025-06-06', 'exposures_2025-06-07', 'exposures_2025-06-08', 'exposures_2025-06-09', 'exposures_2025-06-10', 'exposures_2025-06-11', 'exposures_2025-06-12', 'exposures_2025-06-13', 'exposures_2025-06-14', 'exposures_2025-06-15', 'exposures_2025-06-16', 'exposures_2025-06-17', 'exposures_2025-06-18', 'exposures_2025-06-19', 'exposures_2025-06-20', 'exposures_2025-06-21', 'exposures_2025-06-22', 'exposures_2025-06-23', 'exposures_2025-06-24', 'exposures_2025-06-25', 'exposures_2025-06-26', 'exposures_2025-06-27', 'exposures_2025-06-28', 'exposures_2025-06-29', 'exposures_2025-06-30'])"
            ]
          },
          "execution_count": 10,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "uploader.commit(mode=\"append\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 11,
      "id": "1b9a47be",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/html": [
              "<div><style>\n",
              ".dataframe > thead > tr,\n",
              ".dataframe > tbody > tr {\n",
              "  text-align: right;\n",
              "  white-space: pre-wrap;\n",
              "}\n",
              "</style>\n",
              "<small>shape: (61, 6)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>date</th><th>n_assets</th><th>min_exposure</th><th>max_exposure</th><th>mean_exposure</th><th>std_exposure</th></tr><tr><td>date</td><td>i64</td><td>f32</td><td>f32</td><td>f64</td><td>f64</td></tr></thead><tbody><tr><td>2025-05-01</td><td>46773</td><td>-4.171875</td><td>4.28125</td><td>0.23534</td><td>1.023091</td></tr><tr><td>2025-05-02</td><td>46766</td><td>-4.171875</td><td>4.277344</td><td>0.235877</td><td>1.022765</td></tr><tr><td>2025-05-03</td><td>46763</td><td>-4.171875</td><td>4.277344</td><td>0.235797</td><td>1.022699</td></tr><tr><td>2025-05-04</td><td>46763</td><td>-4.171875</td><td>4.277344</td><td>0.235795</td><td>1.022698</td></tr><tr><td>2025-05-05</td><td>46766</td><td>-4.171875</td><td>4.277344</td><td>0.236549</td><td>1.022354</td></tr><tr><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td><td>&hellip;</td></tr><tr><td>2025-06-26</td><td>46656</td><td>-4.140625</td><td>4.285156</td><td>0.236344</td><td>1.021307</td></tr><tr><td>2025-06-27</td><td>46645</td><td>-4.140625</td><td>4.2890625</td><td>0.236652</td><td>1.021136</td></tr><tr><td>2025-06-28</td><td>46637</td><td>-4.140625</td><td>4.2890625</td><td>0.236683</td><td>1.021083</td></tr><tr><td>2025-06-29</td><td>46637</td><td>-4.140625</td><td>4.2890625</td><td>0.236683</td><td>1.021083</td></tr><tr><td>2025-06-30</td><td>46646</td><td>-4.140625</td><td>4.296875</td><td>0.236964</td><td>1.021116</td></tr></tbody></table></div>"
            ],
            "text/plain": [
              "shape: (61, 6)\n",
              "┌────────────┬──────────┬──────────────┬──────────────┬───────────────┬──────────────┐\n",
              "│ date       ┆ n_assets ┆ min_exposure ┆ max_exposure ┆ mean_exposure ┆ std_exposure │\n",
              "│ ---        ┆ ---      ┆ ---          ┆ ---          ┆ ---           ┆ ---          │\n",
              "│ date       ┆ i64      ┆ f32          ┆ f32          ┆ f64           ┆ f64          │\n",
              "╞════════════╪══════════╪══════════════╪══════════════╪═══════════════╪══════════════╡\n",
              "│ 2025-05-01 ┆ 46773    ┆ -4.171875    ┆ 4.28125      ┆ 0.23534       ┆ 1.023091     │\n",
              "│ 2025-05-02 ┆ 46766    ┆ -4.171875    ┆ 4.277344     ┆ 0.235877      ┆ 1.022765     │\n",
              "│ 2025-05-03 ┆ 46763    ┆ -4.171875    ┆ 4.277344     ┆ 0.235797      ┆ 1.022699     │\n",
              "│ 2025-05-04 ┆ 46763    ┆ -4.171875    ┆ 4.277344     ┆ 0.235795      ┆ 1.022698     │\n",
              "│ 2025-05-05 ┆ 46766    ┆ -4.171875    ┆ 4.277344     ┆ 0.236549      ┆ 1.022354     │\n",
              "│ …          ┆ …        ┆ …            ┆ …            ┆ …             ┆ …            │\n",
              "│ 2025-06-26 ┆ 46656    ┆ -4.140625    ┆ 4.285156     ┆ 0.236344      ┆ 1.021307     │\n",
              "│ 2025-06-27 ┆ 46645    ┆ -4.140625    ┆ 4.2890625    ┆ 0.236652      ┆ 1.021136     │\n",
              "│ 2025-06-28 ┆ 46637    ┆ -4.140625    ┆ 4.2890625    ┆ 0.236683      ┆ 1.021083     │\n",
              "│ 2025-06-29 ┆ 46637    ┆ -4.140625    ┆ 4.2890625    ┆ 0.236683      ┆ 1.021083     │\n",
              "│ 2025-06-30 ┆ 46646    ┆ -4.140625    ┆ 4.296875     ┆ 0.236964      ┆ 1.021116     │\n",
              "└────────────┴──────────┴──────────────┴──────────────┴───────────────┴──────────────┘"
            ]
          },
          "execution_count": 11,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "uploader.get_data_detail_summary()"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "68a3116a",
      "metadata": {},
      "source": [
        "## Updating the Risk Dataset\n",
        "\n",
        "To bring the newly uploaded exposures into the pre-existing dataset we need to update it. This pulls the most recent version for all referenced datasets and re-creates the risk dataset."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 12,
      "id": "4d06d24e",
      "metadata": {},
      "outputs": [],
      "source": [
        "risk_dataset_name = \"My-Risk-Dataset\""
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 13,
      "id": "f38c34e7",
      "metadata": {},
      "outputs": [],
      "source": [
        "risk_datasets = bln.equity.riskdatasets\n",
        "risk_dataset = risk_datasets.load(risk_dataset_name)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 14,
      "id": "9571936e",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "RiskDatasetUpdateResult()"
            ]
          },
          "execution_count": 14,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "risk_dataset.update()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 15,
      "id": "5fae939c",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "RiskDatasetProperties(factor_risk_model_settings_menu=FactorRiskModelSettingsMenu(exposure_settings_menu=ExposureSettingsMenu(universe_settings_menu=UniverseSettingsMenu(calendar_settings_menu=CalendarSettingsMenu(exchanges=['ARCX', 'BVCA', 'BVMF', 'DIFX', 'DSMD', 'ETFP', 'FRAB', 'HSTC', 'JBUL', 'PFTS', 'ROCO', 'SHSC', 'SZSC', 'WBDM', 'XADS', 'XAMM', 'XAMS', 'XASE', 'XASX', 'XATH', 'XBAH', 'XBEL', 'XBEY', 'XBKF', 'XBKK', 'XBOG', 'XBOM', 'XBOS', 'XBRA', 'XBRU', 'XBRV', 'XBUD', 'XBUE', 'XCAI', 'XCAN', 'XCAS', 'XCSE', 'XCYS', 'XDUB', 'XEQY', 'XETB', 'XHEL', 'XHKG', 'XHNX', 'XICE', 'XIDX', 'XJAM', 'XJAS', 'XJSE', 'XKAR', 'XKLS', 'XKOS', 'XKRX', 'XKUW', 'XLIM', 'XLIS', 'XLIT', 'XLJU', 'XLON', 'XLUX', 'XMAD', 'XMAL', 'XMAU', 'XMEX', 'XMUS', 'XNAI', 'XNAM', 'XNAS', 'XNCM', 'XNSA', 'XNSE', 'XNYS', 'XNZE', 'XOSL', 'XPAE', 'XPAR', 'XPHS', 'XPRM', 'XPSX', 'XQUI', 'XRIS', 'XSAU', 'XSEC', 'XSES', 'XSGO', 'XSHE', 'XSHG', 'XSSC', 'XSTC', 'XSTO', 'XSWX', 'XTAE', 'XTAI', 'XTAL', 'XTKS', 'XTSE', 'XTSX', 'XTUN', 'XWAR', 'XZAG', 'XZIM']), id_types=['bayesid'], categorical_hierarchies={'industry': ['Academic & Educational Services', 'Basic Materials', 'Consumer Cyclicals', 'Consumer Non-Cyclicals', 'Energy', 'Financials', 'Government Activity', 'Healthcare', 'Industrials', 'Institutions, Associations & Organizations', 'Real Estate', 'Technology', 'Utilities'], 'region': ['Argentina', 'Australia', 'Austria', 'Bahrain', 'Belgium', 'Bermuda', 'Brazil', 'British Virgin Islands', 'Bulgaria', 'Cambodia', 'Canada', 'Cayman Islands', 'Chile', 'China', 'Colombia', \"Cote d'Ivoire\", 'Croatia', 'Cyprus', 'Czechia', 'Denmark', 'Ecuador', 'Egypt', 'Estonia', 'Finland', 'France', 'Germany', 'Greece', 'Guernsey', 'Hong Kong', 'Hungary', 'Iceland', 'India', 'Indonesia', 'Ireland', 'Israel', 'Italy', 'Jamaica', 'Japan', 'Jersey', 'Jordan', 'Kazakhstan', 'Kenya', 'Kuwait', 'Latvia', 'Lebanon', 'Lithuania', 'Luxembourg', 'Macau', 'Malaysia', 'Malta', 'Mauritius', 'Mexico', 'Monaco', 'Morocco', 'Namibia', 'Netherlands', 'New Zealand', 'Nigeria', 'Norway', 'Oman', 'Pakistan', 'Palestine', 'Papua New Guinea', 'Peru', 'Philippines', 'Poland', 'Portugal', 'Qatar', 'Romania', 'Saudi Arabia', 'Serbia', 'Singapore', 'Slovakia', 'Slovenia', 'South Africa', 'South Korea', 'Spain', 'Sweden', 'Switzerland', 'Taiwan', 'Thailand', 'Trinidad and Tobago', 'Tunisia', 'Türkiye', 'Ukraine', 'United Arab Emirates', 'United Kingdom', 'United States', 'Venezuela', 'Vietnam']}, categorical_hierarchies_labels={'industry': {'Academic & Educational Services': 'Academic & Educational Services', 'Basic Materials': 'Basic Materials', 'Consumer Cyclicals': 'Consumer Cyclicals', 'Consumer Non-Cyclicals': 'Consumer Non-Cyclicals', 'Energy': 'Energy', 'Financials': 'Financials', 'Government Activity': 'Government Activity', 'Healthcare': 'Healthcare', 'Industrials': 'Industrials', 'Institutions, Associations & Organizations': 'Institutions, Associations & Organizations', 'Real Estate': 'Real Estate', 'Technology': 'Technology', 'Utilities': 'Utilities'}, 'region': {'Argentina': 'Argentina', 'Australia': 'Australia', 'Austria': 'Austria', 'Bahrain': 'Bahrain', 'Belgium': 'Belgium', 'Bermuda': 'Bermuda', 'Brazil': 'Brazil', 'British Virgin Islands': 'British Virgin Islands', 'Bulgaria': 'Bulgaria', 'Cambodia': 'Cambodia', 'Canada': 'Canada', 'Cayman Islands': 'Cayman Islands', 'Chile': 'Chile', 'China': 'China', 'Colombia': 'Colombia', \"Cote d'Ivoire\": \"Cote d'Ivoire\", 'Croatia': 'Croatia', 'Cyprus': 'Cyprus', 'Czechia': 'Czechia', 'Denmark': 'Denmark', 'Ecuador': 'Ecuador', 'Egypt': 'Egypt', 'Estonia': 'Estonia', 'Finland': 'Finland', 'France': 'France', 'Germany': 'Germany', 'Greece': 'Greece', 'Guernsey': 'Guernsey', 'Hong Kong': 'Hong Kong', 'Hungary': 'Hungary', 'Iceland': 'Iceland', 'India': 'India', 'Indonesia': 'Indonesia', 'Ireland': 'Ireland', 'Israel': 'Israel', 'Italy': 'Italy', 'Jamaica': 'Jamaica', 'Japan': 'Japan', 'Jersey': 'Jersey', 'Jordan': 'Jordan', 'Kazakhstan': 'Kazakhstan', 'Kenya': 'Kenya', 'Kuwait': 'Kuwait', 'Latvia': 'Latvia', 'Lebanon': 'Lebanon', 'Lithuania': 'Lithuania', 'Luxembourg': 'Luxembourg', 'Macau': 'Macau', 'Malaysia': 'Malaysia', 'Malta': 'Malta', 'Mauritius': 'Mauritius', 'Mexico': 'Mexico', 'Monaco': 'Monaco', 'Morocco': 'Morocco', 'Namibia': 'Namibia', 'Netherlands': 'Netherlands', 'New Zealand': 'New Zealand', 'Nigeria': 'Nigeria', 'Norway': 'Norway', 'Oman': 'Oman', 'Pakistan': 'Pakistan', 'Palestine': 'Palestine', 'Papua New Guinea': 'Papua New Guinea', 'Peru': 'Peru', 'Philippines': 'Philippines', 'Poland': 'Poland', 'Portugal': 'Portugal', 'Qatar': 'Qatar', 'Romania': 'Romania', 'Saudi Arabia': 'Saudi Arabia', 'Serbia': 'Serbia', 'Singapore': 'Singapore', 'Slovakia': 'Slovakia', 'Slovenia': 'Slovenia', 'South Africa': 'South Africa', 'South Korea': 'South Korea', 'Spain': 'Spain', 'Sweden': 'Sweden', 'Switzerland': 'Switzerland', 'Taiwan': 'Taiwan', 'Thailand': 'Thailand', 'Trinidad and Tobago': 'Trinidad and Tobago', 'Tunisia': 'Tunisia', 'Türkiye': 'Türkiye', 'Ukraine': 'Ukraine', 'United Arab Emirates': 'United Arab Emirates', 'United Kingdom': 'United Kingdom', 'United States': 'United States', 'Venezuela': 'Venezuela', 'Vietnam': 'Vietnam'}}), continuous_hierarchies={'market': ['Market'], 'style': ['Dividend', 'Growth', 'Leverage', 'Momentum', 'Size', 'Value', 'Volatility']}, continuous_hierarchies_labels={'market': {'Market': 'Market'}, 'style': {'Dividend': 'Dividend', 'Growth': 'Growth', 'Leverage': 'Leverage', 'Momentum': 'Momentum', 'Size': 'Size', 'Value': 'Value', 'Volatility': 'Volatility'}}), modelconstruction_settings_menu=ModelConstructionSettingsMenu(weights=['SqrtCap', 'InvIdioVar'])))"
            ]
          },
          "execution_count": 15,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "risk_dataset.describe()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 16,
      "id": "99a9235c",
      "metadata": {},
      "outputs": [],
      "source": [
        "exposures_api = bln.equity.exposures.load(\n",
        "    ExposureSettings(\n",
        "        exposures=[\n",
        "            ContinuousExposureGroupSettings(hierarchy=\"market\"),\n",
        "            ContinuousExposureGroupSettings(hierarchy=\"style\"),\n",
        "        ]\n",
        "    ),\n",
        ")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 17,
      "id": "f1f4990e",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/html": [
              "<div><style>\n",
              ".dataframe > thead > tr,\n",
              ".dataframe > tbody > tr {\n",
              "  text-align: right;\n",
              "  white-space: pre-wrap;\n",
              "}\n",
              "</style>\n",
              "<small>shape: (5, 10)</small><table border=\"1\" class=\"dataframe\"><thead><tr><th>date</th><th>bayesid</th><th>market.Market</th><th>style.Dividend</th><th>style.Growth</th><th>style.Leverage</th><th>style.Momentum</th><th>style.Size</th><th>style.Value</th><th>style.Volatility</th></tr><tr><td>date</td><td>str</td><td>f32</td><td>f32</td><td>f32</td><td>f32</td><td>f32</td><td>f32</td><td>f32</td><td>f32</td></tr></thead><tbody><tr><td>2025-06-30</td><td>&quot;ICFFE54368&quot;</td><td>1.0</td><td>-0.67334</td><td>-0.948242</td><td>0.128784</td><td>-0.860352</td><td>0.436035</td><td>-2.851562</td><td>0.599121</td></tr><tr><td>2025-06-30</td><td>&quot;ICFFE60191&quot;</td><td>0.0</td><td>-0.018005</td><td>-0.406006</td><td>-0.300049</td><td>-0.220947</td><td>-0.95459</td><td>0.286621</td><td>0.308105</td></tr><tr><td>2025-06-30</td><td>&quot;ICFFE94AED&quot;</td><td>0.0</td><td>-0.013519</td><td>-0.395264</td><td>-0.291992</td><td>-0.215088</td><td>-0.928711</td><td>0.279053</td><td>0.299805</td></tr><tr><td>2025-06-30</td><td>&quot;ICFFEBBB38&quot;</td><td>1.0</td><td>0.693359</td><td>0.486084</td><td>0.186523</td><td>0.691406</td><td>0.083557</td><td>0.051819</td><td>-1.09375</td></tr><tr><td>2025-06-30</td><td>&quot;ICFFF2F5AD&quot;</td><td>0.0</td><td>-0.039978</td><td>-0.459473</td><td>-0.339355</td><td>-0.25</td><td>-1.080078</td><td>0.324219</td><td>0.348633</td></tr></tbody></table></div>"
            ],
            "text/plain": [
              "shape: (5, 10)\n",
              "┌───────────┬───────────┬───────────┬───────────┬───┬───────────┬───────────┬───────────┬──────────┐\n",
              "│ date      ┆ bayesid   ┆ market.Ma ┆ style.Div ┆ … ┆ style.Mom ┆ style.Siz ┆ style.Val ┆ style.Vo │\n",
              "│ ---       ┆ ---       ┆ rket      ┆ idend     ┆   ┆ entum     ┆ e         ┆ ue        ┆ latility │\n",
              "│ date      ┆ str       ┆ ---       ┆ ---       ┆   ┆ ---       ┆ ---       ┆ ---       ┆ ---      │\n",
              "│           ┆           ┆ f32       ┆ f32       ┆   ┆ f32       ┆ f32       ┆ f32       ┆ f32      │\n",
              "╞═══════════╪═══════════╪═══════════╪═══════════╪═══╪═══════════╪═══════════╪═══════════╪══════════╡\n",
              "│ 2025-06-3 ┆ ICFFE5436 ┆ 1.0       ┆ -0.67334  ┆ … ┆ -0.860352 ┆ 0.436035  ┆ -2.851562 ┆ 0.599121 │\n",
              "│ 0         ┆ 8         ┆           ┆           ┆   ┆           ┆           ┆           ┆          │\n",
              "│ 2025-06-3 ┆ ICFFE6019 ┆ 0.0       ┆ -0.018005 ┆ … ┆ -0.220947 ┆ -0.95459  ┆ 0.286621  ┆ 0.308105 │\n",
              "│ 0         ┆ 1         ┆           ┆           ┆   ┆           ┆           ┆           ┆          │\n",
              "│ 2025-06-3 ┆ ICFFE94AE ┆ 0.0       ┆ -0.013519 ┆ … ┆ -0.215088 ┆ -0.928711 ┆ 0.279053  ┆ 0.299805 │\n",
              "│ 0         ┆ D         ┆           ┆           ┆   ┆           ┆           ┆           ┆          │\n",
              "│ 2025-06-3 ┆ ICFFEBBB3 ┆ 1.0       ┆ 0.693359  ┆ … ┆ 0.691406  ┆ 0.083557  ┆ 0.051819  ┆ -1.09375 │\n",
              "│ 0         ┆ 8         ┆           ┆           ┆   ┆           ┆           ┆           ┆          │\n",
              "│ 2025-06-3 ┆ ICFFF2F5A ┆ 0.0       ┆ -0.039978 ┆ … ┆ -0.25     ┆ -1.080078 ┆ 0.324219  ┆ 0.348633 │\n",
              "│ 0         ┆ D         ┆           ┆           ┆   ┆           ┆           ┆           ┆          │\n",
              "└───────────┴───────────┴───────────┴───────────┴───┴───────────┴───────────┴───────────┴──────────┘"
            ]
          },
          "execution_count": 17,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# note that the industry and region hierarchy names tie out with the factor groups we specified above\n",
        "\n",
        "df = exposures_api.get(UniverseSettings(dataset=risk_dataset_name), standardize_universe=None)\n",
        "\n",
        "df.tail()"
      ]
    }
  ],
  "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.14"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}