{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#| hide\n",
    "!pip install -Uqq nixtla datasetsforecast"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#| hide \n",
    "from nixtla.utils import in_colab"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#| hide \n",
    "IN_COLAB = in_colab()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#| hide\n",
    "if not IN_COLAB:\n",
    "    from nixtla.utils import colab_badge\n",
    "    from dotenv import load_dotenv"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# What if? Forecasting price effects in retail\n",
    "\n",
    "You can use TimeGPT to forecast a set of timeseries, for example the demand of a product in retail. But what if you want to evaluate different pricing scenarios for that product? Performing such a scenario analysis can help you better understand how pricing affects product demand and can aid in decision making.\n",
    "\n",
    "In this example, we will show you:\n",
    "* How you can use TimeGPT to forecast product demand using price as an exogenous variable\n",
    "* How you can evaluate different pricing scenarios that affect product demand"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "[![](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/Nixtla/nixtla/blob/main/nbs/docs/use-cases/5_what_if_pricing_scenarios_in_retail.ipynb)"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#| echo: false\n",
    "if not IN_COLAB:\n",
    "    load_dotenv()    \n",
    "    colab_badge('docs/use-cases/5_what_if_pricing_scenarios_in_retail')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Import packages\n",
    "First, we import the required packages and initialize the Nixtla client."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import os\n",
    "\n",
    "from nixtla import NixtlaClient\n",
    "from datasetsforecast.m5 import M5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "nixtla_client = NixtlaClient(\n",
    "    # defaults to os.environ.get(\"NIXTLA_API_KEY\")\n",
    "    api_key = 'my_api_key_provided_by_nixtla'\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> 👍 Use an Azure AI endpoint\n",
    "> \n",
    "> To use an Azure AI endpoint, remember to set also the `base_url` argument:\n",
    "> \n",
    "> `nixtla_client = NixtlaClient(base_url=\"you azure ai endpoint\", api_key=\"your api_key\")`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#| hide\n",
    "if not IN_COLAB:\n",
    "    nixtla_client = NixtlaClient()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Load M5 data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's see an example on predicting sales of products of the [M5 dataset](https://nixtlaverse.nixtla.io/datasetsforecast/m5.html). The M5 dataset contains daily product demand (sales) for 10 retail stores in the US.\\\n",
    "\\\n",
    "First, we load the data using `datasetsforecast`. This returns:\n",
    "\n",
    "* `Y_df`, containing the sales (`y` column), for each unique product (`unique_id` column) at every timestamp (`ds` column). \n",
    "* `X_df`, containing additional relevant information for each unique product (`unique_id` column) at every timestamp (`ds` column). \n",
    "\n",
    "::: {.callout-tip}\n",
    "You can find a tutorial on including exogenous variables in your forecast with TimeGPT [here](/docs/tutorials-timegpt_quickstart).\n",
    "::: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 50.2M/50.2M [00:00<00:00, 58.1MiB/s]\n",
      "INFO:datasetsforecast.utils:Successfully downloaded m5.zip, 50219189, bytes.\n",
      "INFO:datasetsforecast.utils:Decompressing zip file...\n",
      "INFO:datasetsforecast.utils:Successfully decompressed c:\\Users\\ospra\\OneDrive\\Nixtla\\Repositories\\nixtla\\m5\\datasets\\m5.zip\n",
      "c:\\Users\\ospra\\miniconda3\\envs\\nixtla\\lib\\site-packages\\datasetsforecast\\m5.py:143: FutureWarning: The default of observed=False is deprecated and will be changed to True in a future version of pandas. Pass observed=False to retain current behavior or observed=True to adopt the future default and silence this warning.\n",
      "  keep_mask = long.groupby('id')['y'].transform(first_nz_mask, engine='numba')\n"
     ]
    },
    {
     "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>unique_id</th>\n",
       "      <th>ds</th>\n",
       "      <th>y</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>FOODS_1_001_CA_1</td>\n",
       "      <td>2011-01-29</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>FOODS_1_001_CA_1</td>\n",
       "      <td>2011-01-30</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>FOODS_1_001_CA_1</td>\n",
       "      <td>2011-01-31</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>FOODS_1_001_CA_1</td>\n",
       "      <td>2011-02-01</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>FOODS_1_001_CA_1</td>\n",
       "      <td>2011-02-02</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>FOODS_1_001_CA_1</td>\n",
       "      <td>2011-02-03</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>FOODS_1_001_CA_1</td>\n",
       "      <td>2011-02-04</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>FOODS_1_001_CA_1</td>\n",
       "      <td>2011-02-05</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>FOODS_1_001_CA_1</td>\n",
       "      <td>2011-02-06</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>FOODS_1_001_CA_1</td>\n",
       "      <td>2011-02-07</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          unique_id         ds    y\n",
       "0  FOODS_1_001_CA_1 2011-01-29  3.0\n",
       "1  FOODS_1_001_CA_1 2011-01-30  0.0\n",
       "2  FOODS_1_001_CA_1 2011-01-31  0.0\n",
       "3  FOODS_1_001_CA_1 2011-02-01  1.0\n",
       "4  FOODS_1_001_CA_1 2011-02-02  4.0\n",
       "5  FOODS_1_001_CA_1 2011-02-03  2.0\n",
       "6  FOODS_1_001_CA_1 2011-02-04  0.0\n",
       "7  FOODS_1_001_CA_1 2011-02-05  2.0\n",
       "8  FOODS_1_001_CA_1 2011-02-06  0.0\n",
       "9  FOODS_1_001_CA_1 2011-02-07  0.0"
      ]
     },
     "execution_count": null,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#| eval: false\n",
    "Y_df, X_df, S_df = M5.load(directory=os.getcwd())\n",
    "\n",
    "Y_df.head(10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For this example, we will only keep the additional relevant information from the column `sell_price`. This column shows the selling price of the product, and we expect demand to fluctuate given a different selling price."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "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>unique_id</th>\n",
       "      <th>ds</th>\n",
       "      <th>sell_price</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>FOODS_1_001_CA_1</td>\n",
       "      <td>2011-01-29</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>FOODS_1_001_CA_1</td>\n",
       "      <td>2011-01-30</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>FOODS_1_001_CA_1</td>\n",
       "      <td>2011-01-31</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>FOODS_1_001_CA_1</td>\n",
       "      <td>2011-02-01</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>FOODS_1_001_CA_1</td>\n",
       "      <td>2011-02-02</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>FOODS_1_001_CA_1</td>\n",
       "      <td>2011-02-03</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>FOODS_1_001_CA_1</td>\n",
       "      <td>2011-02-04</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>FOODS_1_001_CA_1</td>\n",
       "      <td>2011-02-05</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>FOODS_1_001_CA_1</td>\n",
       "      <td>2011-02-06</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>FOODS_1_001_CA_1</td>\n",
       "      <td>2011-02-07</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          unique_id         ds  sell_price\n",
       "0  FOODS_1_001_CA_1 2011-01-29         2.0\n",
       "1  FOODS_1_001_CA_1 2011-01-30         2.0\n",
       "2  FOODS_1_001_CA_1 2011-01-31         2.0\n",
       "3  FOODS_1_001_CA_1 2011-02-01         2.0\n",
       "4  FOODS_1_001_CA_1 2011-02-02         2.0\n",
       "5  FOODS_1_001_CA_1 2011-02-03         2.0\n",
       "6  FOODS_1_001_CA_1 2011-02-04         2.0\n",
       "7  FOODS_1_001_CA_1 2011-02-05         2.0\n",
       "8  FOODS_1_001_CA_1 2011-02-06         2.0\n",
       "9  FOODS_1_001_CA_1 2011-02-07         2.0"
      ]
     },
     "execution_count": null,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#| eval: false\n",
    "X_df = X_df[['unique_id', 'ds', 'sell_price']]\n",
    "\n",
    "X_df.head(10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. Forecasting demand using price as an exogenous variable"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We will forecast the demand for a single product only, for all 10 retail stores in the dataset. We choose a food product with many price changes identified by `FOODS_1_129_`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#| eval: false\n",
    "products = [\n",
    "            'FOODS_1_129_CA_1', \n",
    "            'FOODS_1_129_CA_2', \n",
    "            'FOODS_1_129_CA_3', \n",
    "            'FOODS_1_129_CA_4', \n",
    "            'FOODS_1_129_TX_1', \n",
    "            'FOODS_1_129_TX_2', \n",
    "            'FOODS_1_129_TX_3',\n",
    "            'FOODS_1_129_WI_1',\n",
    "            'FOODS_1_129_WI_2',\n",
    "            'FOODS_1_129_WI_3'\n",
    "            ]\n",
    "Y_df_product = Y_df.query('unique_id in @products')\n",
    "X_df_product = X_df.query('unique_id in @products')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We merge our two dataframes to create the dataset to be used in TimeGPT."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "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>unique_id</th>\n",
       "      <th>ds</th>\n",
       "      <th>y</th>\n",
       "      <th>sell_price</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>FOODS_1_129_CA_1</td>\n",
       "      <td>2011-02-01</td>\n",
       "      <td>1.0</td>\n",
       "      <td>6.22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>FOODS_1_129_CA_1</td>\n",
       "      <td>2011-02-02</td>\n",
       "      <td>0.0</td>\n",
       "      <td>6.22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>FOODS_1_129_CA_1</td>\n",
       "      <td>2011-02-03</td>\n",
       "      <td>0.0</td>\n",
       "      <td>6.22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>FOODS_1_129_CA_1</td>\n",
       "      <td>2011-02-04</td>\n",
       "      <td>0.0</td>\n",
       "      <td>6.22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>FOODS_1_129_CA_1</td>\n",
       "      <td>2011-02-05</td>\n",
       "      <td>1.0</td>\n",
       "      <td>6.22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>FOODS_1_129_CA_1</td>\n",
       "      <td>2011-02-06</td>\n",
       "      <td>0.0</td>\n",
       "      <td>6.22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>FOODS_1_129_CA_1</td>\n",
       "      <td>2011-02-07</td>\n",
       "      <td>0.0</td>\n",
       "      <td>6.22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>FOODS_1_129_CA_1</td>\n",
       "      <td>2011-02-08</td>\n",
       "      <td>0.0</td>\n",
       "      <td>6.22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>FOODS_1_129_CA_1</td>\n",
       "      <td>2011-02-09</td>\n",
       "      <td>0.0</td>\n",
       "      <td>6.22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>FOODS_1_129_CA_1</td>\n",
       "      <td>2011-02-10</td>\n",
       "      <td>3.0</td>\n",
       "      <td>6.22</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          unique_id         ds    y  sell_price\n",
       "0  FOODS_1_129_CA_1 2011-02-01  1.0        6.22\n",
       "1  FOODS_1_129_CA_1 2011-02-02  0.0        6.22\n",
       "2  FOODS_1_129_CA_1 2011-02-03  0.0        6.22\n",
       "3  FOODS_1_129_CA_1 2011-02-04  0.0        6.22\n",
       "4  FOODS_1_129_CA_1 2011-02-05  1.0        6.22\n",
       "5  FOODS_1_129_CA_1 2011-02-06  0.0        6.22\n",
       "6  FOODS_1_129_CA_1 2011-02-07  0.0        6.22\n",
       "7  FOODS_1_129_CA_1 2011-02-08  0.0        6.22\n",
       "8  FOODS_1_129_CA_1 2011-02-09  0.0        6.22\n",
       "9  FOODS_1_129_CA_1 2011-02-10  3.0        6.22"
      ]
     },
     "execution_count": null,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#| eval: false\n",
    "df = Y_df_product.merge(X_df_product)\n",
    "\n",
    "df.head(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#| hide\n",
    "if not IN_COLAB:\n",
    "    df = pd.read_parquet(\"../../assets/M5_what_if_pricing_example.parquet\")\n",
    "    products = list(df[\"unique_id\"].unique())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's investigate how the demand - our target `y` - of these products has evolved in the last year of data. We see that in the California stores (with a `CA_` suffix), the product has sold intermittently, whereas in the other regions (`TX` and `WY`) sales where less intermittent. Note that the plot only shows 8 (out of 10) stores."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 692800 characters]",
      "text/plain": [
       "<Figure size 2400x1400 with 8 Axes>"
      ]
     },
     "execution_count": null,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nixtla_client.plot(df, \n",
    "                   unique_ids=products,\n",
    "                   max_insample_length=365)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Next, we look at the `sell_price` of these products across the entire data available. We find that there have been relatively few price changes - about 20 in total - over the period 2011 - 2016. Note that the plot only shows 8 (out of 10) stores."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 177084 characters]",
      "text/plain": [
       "<Figure size 2400x1400 with 8 Axes>"
      ]
     },
     "execution_count": null,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nixtla_client.plot(df, \n",
    "                   unique_ids=products,\n",
    "                   target_col='sell_price')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's turn to our forecasting task. We will forecast the last 28 days in the dataset. \n",
    "\n",
    "To use the `sell_price` exogenous variable in TimeGPT, we have to add it as future values. Therefore, we create a future values dataframe, that contains the `unique_id`, the timestamp `ds`, and `sell_price`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "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>unique_id</th>\n",
       "      <th>ds</th>\n",
       "      <th>sell_price</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1938</th>\n",
       "      <td>FOODS_1_129_CA_1</td>\n",
       "      <td>2016-05-23</td>\n",
       "      <td>5.74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1939</th>\n",
       "      <td>FOODS_1_129_CA_1</td>\n",
       "      <td>2016-05-24</td>\n",
       "      <td>5.74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1940</th>\n",
       "      <td>FOODS_1_129_CA_1</td>\n",
       "      <td>2016-05-25</td>\n",
       "      <td>5.74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1941</th>\n",
       "      <td>FOODS_1_129_CA_1</td>\n",
       "      <td>2016-05-26</td>\n",
       "      <td>5.74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1942</th>\n",
       "      <td>FOODS_1_129_CA_1</td>\n",
       "      <td>2016-05-27</td>\n",
       "      <td>5.74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1943</th>\n",
       "      <td>FOODS_1_129_CA_1</td>\n",
       "      <td>2016-05-28</td>\n",
       "      <td>5.74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1944</th>\n",
       "      <td>FOODS_1_129_CA_1</td>\n",
       "      <td>2016-05-29</td>\n",
       "      <td>5.74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1945</th>\n",
       "      <td>FOODS_1_129_CA_1</td>\n",
       "      <td>2016-05-30</td>\n",
       "      <td>5.74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1946</th>\n",
       "      <td>FOODS_1_129_CA_1</td>\n",
       "      <td>2016-05-31</td>\n",
       "      <td>5.74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1947</th>\n",
       "      <td>FOODS_1_129_CA_1</td>\n",
       "      <td>2016-06-01</td>\n",
       "      <td>5.74</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             unique_id         ds  sell_price\n",
       "1938  FOODS_1_129_CA_1 2016-05-23        5.74\n",
       "1939  FOODS_1_129_CA_1 2016-05-24        5.74\n",
       "1940  FOODS_1_129_CA_1 2016-05-25        5.74\n",
       "1941  FOODS_1_129_CA_1 2016-05-26        5.74\n",
       "1942  FOODS_1_129_CA_1 2016-05-27        5.74\n",
       "1943  FOODS_1_129_CA_1 2016-05-28        5.74\n",
       "1944  FOODS_1_129_CA_1 2016-05-29        5.74\n",
       "1945  FOODS_1_129_CA_1 2016-05-30        5.74\n",
       "1946  FOODS_1_129_CA_1 2016-05-31        5.74\n",
       "1947  FOODS_1_129_CA_1 2016-06-01        5.74"
      ]
     },
     "execution_count": null,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "future_ex_vars_df = df.drop(columns = ['y'])\n",
    "future_ex_vars_df = future_ex_vars_df.query(\"ds >= '2016-05-23'\")\n",
    "\n",
    "future_ex_vars_df.head(10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Next, we limit our input dataframe to all but the 28 forecast days:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "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>unique_id</th>\n",
       "      <th>ds</th>\n",
       "      <th>y</th>\n",
       "      <th>sell_price</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>19640</th>\n",
       "      <td>FOODS_1_129_WI_3</td>\n",
       "      <td>2016-05-13</td>\n",
       "      <td>3.0</td>\n",
       "      <td>7.23</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19641</th>\n",
       "      <td>FOODS_1_129_WI_3</td>\n",
       "      <td>2016-05-14</td>\n",
       "      <td>1.0</td>\n",
       "      <td>7.23</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19642</th>\n",
       "      <td>FOODS_1_129_WI_3</td>\n",
       "      <td>2016-05-15</td>\n",
       "      <td>2.0</td>\n",
       "      <td>7.23</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19643</th>\n",
       "      <td>FOODS_1_129_WI_3</td>\n",
       "      <td>2016-05-16</td>\n",
       "      <td>3.0</td>\n",
       "      <td>7.23</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19644</th>\n",
       "      <td>FOODS_1_129_WI_3</td>\n",
       "      <td>2016-05-17</td>\n",
       "      <td>1.0</td>\n",
       "      <td>7.23</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19645</th>\n",
       "      <td>FOODS_1_129_WI_3</td>\n",
       "      <td>2016-05-18</td>\n",
       "      <td>2.0</td>\n",
       "      <td>7.23</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19646</th>\n",
       "      <td>FOODS_1_129_WI_3</td>\n",
       "      <td>2016-05-19</td>\n",
       "      <td>3.0</td>\n",
       "      <td>7.23</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19647</th>\n",
       "      <td>FOODS_1_129_WI_3</td>\n",
       "      <td>2016-05-20</td>\n",
       "      <td>1.0</td>\n",
       "      <td>7.23</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19648</th>\n",
       "      <td>FOODS_1_129_WI_3</td>\n",
       "      <td>2016-05-21</td>\n",
       "      <td>0.0</td>\n",
       "      <td>7.23</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19649</th>\n",
       "      <td>FOODS_1_129_WI_3</td>\n",
       "      <td>2016-05-22</td>\n",
       "      <td>0.0</td>\n",
       "      <td>7.23</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              unique_id         ds    y  sell_price\n",
       "19640  FOODS_1_129_WI_3 2016-05-13  3.0        7.23\n",
       "19641  FOODS_1_129_WI_3 2016-05-14  1.0        7.23\n",
       "19642  FOODS_1_129_WI_3 2016-05-15  2.0        7.23\n",
       "19643  FOODS_1_129_WI_3 2016-05-16  3.0        7.23\n",
       "19644  FOODS_1_129_WI_3 2016-05-17  1.0        7.23\n",
       "19645  FOODS_1_129_WI_3 2016-05-18  2.0        7.23\n",
       "19646  FOODS_1_129_WI_3 2016-05-19  3.0        7.23\n",
       "19647  FOODS_1_129_WI_3 2016-05-20  1.0        7.23\n",
       "19648  FOODS_1_129_WI_3 2016-05-21  0.0        7.23\n",
       "19649  FOODS_1_129_WI_3 2016-05-22  0.0        7.23"
      ]
     },
     "execution_count": null,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_train = df.query(\"ds < '2016-05-23'\")\n",
    "\n",
    "df_train.tail(10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's call the `forecast` method of TimeGPT:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:nixtla.nixtla_client:Validating inputs...\n",
      "INFO:nixtla.nixtla_client:Preprocessing dataframes...\n",
      "INFO:nixtla.nixtla_client:Inferred freq: D\n",
      "WARNING:nixtla.nixtla_client:The specified horizon \"h\" exceeds the model horizon. This may lead to less accurate forecasts. Please consider using a smaller horizon.\n",
      "INFO:nixtla.nixtla_client:Using the following exogenous variables: sell_price\n",
      "INFO:nixtla.nixtla_client:Calling Forecast Endpoint...\n"
     ]
    },
    {
     "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>unique_id</th>\n",
       "      <th>ds</th>\n",
       "      <th>TimeGPT</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>FOODS_1_129_CA_1</td>\n",
       "      <td>2016-05-23</td>\n",
       "      <td>0.875594</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>FOODS_1_129_CA_1</td>\n",
       "      <td>2016-05-24</td>\n",
       "      <td>0.777731</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>FOODS_1_129_CA_1</td>\n",
       "      <td>2016-05-25</td>\n",
       "      <td>0.786871</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>FOODS_1_129_CA_1</td>\n",
       "      <td>2016-05-26</td>\n",
       "      <td>0.828223</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>FOODS_1_129_CA_1</td>\n",
       "      <td>2016-05-27</td>\n",
       "      <td>0.791228</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          unique_id          ds   TimeGPT\n",
       "0  FOODS_1_129_CA_1  2016-05-23  0.875594\n",
       "1  FOODS_1_129_CA_1  2016-05-24  0.777731\n",
       "2  FOODS_1_129_CA_1  2016-05-25  0.786871\n",
       "3  FOODS_1_129_CA_1  2016-05-26  0.828223\n",
       "4  FOODS_1_129_CA_1  2016-05-27  0.791228"
      ]
     },
     "execution_count": null,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "timegpt_fcst_df = nixtla_client.forecast(df=df_train, X_df=future_ex_vars_df, h=28)\n",
    "timegpt_fcst_df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> 📘 Available models in Azure AI\n",
    ">\n",
    "> If you are using an Azure AI endpoint, please be sure to set `model=\"azureai\"`:\n",
    ">\n",
    "> `nixtla_client.forecast(..., model=\"azureai\")`\n",
    "> \n",
    "> For the public API, we support two models: `timegpt-1` and `timegpt-1-long-horizon`. \n",
    "> \n",
    "> By default, `timegpt-1` is used. Please see [this tutorial](https://docs.nixtla.io/docs/tutorials-long_horizon_forecasting) on how and when to use `timegpt-1-long-horizon`."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We plot the forecast, the actuals and the last 28 days before the forecast period:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 478752 characters]",
      "text/plain": [
       "<Figure size 2400x1400 with 8 Axes>"
      ]
     },
     "execution_count": null,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nixtla_client.plot(\n",
    "    df[['unique_id', 'ds', 'y']], \n",
    "    timegpt_fcst_df, \n",
    "    max_insample_length=56, \n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4. What if? Varying price when forecasting demand"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "What happens when we change the price of the products in our forecast period? Let's see how our forecast changes when we increase and decrease the `sell_price` by 5%."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "price_change = 0.05\n",
    "\n",
    "# Plus \n",
    "future_ex_vars_df_plus= future_ex_vars_df.copy()\n",
    "future_ex_vars_df_plus[\"sell_price\"] = future_ex_vars_df_plus[\"sell_price\"] * (1 + price_change)\n",
    "# Minus\n",
    "future_ex_vars_df_minus = future_ex_vars_df.copy()\n",
    "future_ex_vars_df_minus[\"sell_price\"] = future_ex_vars_df_minus[\"sell_price\"] * (1 - price_change)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's create a new set of forecasts with TimeGPT."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:nixtla.nixtla_client:Validating inputs...\n",
      "INFO:nixtla.nixtla_client:Preprocessing dataframes...\n",
      "INFO:nixtla.nixtla_client:Inferred freq: D\n",
      "WARNING:nixtla.nixtla_client:The specified horizon \"h\" exceeds the model horizon. This may lead to less accurate forecasts. Please consider using a smaller horizon.\n",
      "INFO:nixtla.nixtla_client:Using the following exogenous variables: sell_price\n",
      "INFO:nixtla.nixtla_client:Calling Forecast Endpoint...\n",
      "INFO:nixtla.nixtla_client:Validating inputs...\n",
      "INFO:nixtla.nixtla_client:Preprocessing dataframes...\n",
      "INFO:nixtla.nixtla_client:Inferred freq: D\n",
      "WARNING:nixtla.nixtla_client:The specified horizon \"h\" exceeds the model horizon. This may lead to less accurate forecasts. Please consider using a smaller horizon.\n",
      "INFO:nixtla.nixtla_client:Using the following exogenous variables: sell_price\n",
      "INFO:nixtla.nixtla_client:Calling Forecast Endpoint...\n"
     ]
    }
   ],
   "source": [
    "timegpt_fcst_df_plus = nixtla_client.forecast(df=df_train, X_df=future_ex_vars_df_plus, h=28)\n",
    "timegpt_fcst_df_minus = nixtla_client.forecast(df=df_train, X_df=future_ex_vars_df_minus, h=28)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> 📘 Available models in Azure AI\n",
    ">\n",
    "> If you are using an Azure AI endpoint, please be sure to set `model=\"azureai\"`:\n",
    ">\n",
    "> `nixtla_client.forecast(..., model=\"azureai\")`\n",
    "> \n",
    "> For the public API, we support two models: `timegpt-1` and `timegpt-1-long-horizon`. \n",
    "> \n",
    "> By default, `timegpt-1` is used. Please see [this tutorial](https://docs.nixtla.io/docs/tutorials-long_horizon_forecasting) on how and when to use `timegpt-1-long-horizon`."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's combine our three forecasts. We see that - as we expect - demand is expected to slightly increase (decrease) if we reduce (increase) the price. In other words, a cheaper product leads to higher sales and vice versa. \n",
    "\n",
    "::: {.callout-note}\n",
    "_Price elasticity_ is a measure of how sensitive the (product) demand is to a change in price. Read more about it [here](https://en.wikipedia.org/wiki/Price_elasticity_of_demand).\n",
    "::: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "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>unique_id</th>\n",
       "      <th>ds</th>\n",
       "      <th>TimeGPT</th>\n",
       "      <th>TimeGPT-sell_price_plus_5%</th>\n",
       "      <th>TimeGPT-sell_price_minus_5%</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>FOODS_1_129_CA_1</td>\n",
       "      <td>2016-05-23</td>\n",
       "      <td>0.875594</td>\n",
       "      <td>0.847006</td>\n",
       "      <td>1.370029</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>FOODS_1_129_CA_1</td>\n",
       "      <td>2016-05-24</td>\n",
       "      <td>0.777731</td>\n",
       "      <td>0.749142</td>\n",
       "      <td>1.272166</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>FOODS_1_129_CA_1</td>\n",
       "      <td>2016-05-25</td>\n",
       "      <td>0.786871</td>\n",
       "      <td>0.758283</td>\n",
       "      <td>1.281306</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>FOODS_1_129_CA_1</td>\n",
       "      <td>2016-05-26</td>\n",
       "      <td>0.828223</td>\n",
       "      <td>0.799635</td>\n",
       "      <td>1.322658</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>FOODS_1_129_CA_1</td>\n",
       "      <td>2016-05-27</td>\n",
       "      <td>0.791228</td>\n",
       "      <td>0.762640</td>\n",
       "      <td>1.285663</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>FOODS_1_129_CA_1</td>\n",
       "      <td>2016-05-28</td>\n",
       "      <td>0.819133</td>\n",
       "      <td>0.790545</td>\n",
       "      <td>1.313568</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>FOODS_1_129_CA_1</td>\n",
       "      <td>2016-05-29</td>\n",
       "      <td>0.839992</td>\n",
       "      <td>0.811404</td>\n",
       "      <td>1.334427</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>FOODS_1_129_CA_1</td>\n",
       "      <td>2016-05-30</td>\n",
       "      <td>0.843070</td>\n",
       "      <td>0.814481</td>\n",
       "      <td>1.337505</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>FOODS_1_129_CA_1</td>\n",
       "      <td>2016-05-31</td>\n",
       "      <td>0.833089</td>\n",
       "      <td>0.804500</td>\n",
       "      <td>1.327524</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>FOODS_1_129_CA_1</td>\n",
       "      <td>2016-06-01</td>\n",
       "      <td>0.855032</td>\n",
       "      <td>0.826443</td>\n",
       "      <td>1.349467</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          unique_id          ds   TimeGPT  TimeGPT-sell_price_plus_5%  \\\n",
       "0  FOODS_1_129_CA_1  2016-05-23  0.875594                    0.847006   \n",
       "1  FOODS_1_129_CA_1  2016-05-24  0.777731                    0.749142   \n",
       "2  FOODS_1_129_CA_1  2016-05-25  0.786871                    0.758283   \n",
       "3  FOODS_1_129_CA_1  2016-05-26  0.828223                    0.799635   \n",
       "4  FOODS_1_129_CA_1  2016-05-27  0.791228                    0.762640   \n",
       "5  FOODS_1_129_CA_1  2016-05-28  0.819133                    0.790545   \n",
       "6  FOODS_1_129_CA_1  2016-05-29  0.839992                    0.811404   \n",
       "7  FOODS_1_129_CA_1  2016-05-30  0.843070                    0.814481   \n",
       "8  FOODS_1_129_CA_1  2016-05-31  0.833089                    0.804500   \n",
       "9  FOODS_1_129_CA_1  2016-06-01  0.855032                    0.826443   \n",
       "\n",
       "   TimeGPT-sell_price_minus_5%  \n",
       "0                     1.370029  \n",
       "1                     1.272166  \n",
       "2                     1.281306  \n",
       "3                     1.322658  \n",
       "4                     1.285663  \n",
       "5                     1.313568  \n",
       "6                     1.334427  \n",
       "7                     1.337505  \n",
       "8                     1.327524  \n",
       "9                     1.349467  "
      ]
     },
     "execution_count": null,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "timegpt_fcst_df_plus = timegpt_fcst_df_plus.rename(columns={'TimeGPT':f'TimeGPT-sell_price_plus_{price_change * 100:.0f}%'})\n",
    "timegpt_fcst_df_minus = timegpt_fcst_df_minus.rename(columns={'TimeGPT':f'TimeGPT-sell_price_minus_{price_change * 100:.0f}%'})\n",
    "\n",
    "timegpt_fcst_df = pd.concat([timegpt_fcst_df, \n",
    "                             timegpt_fcst_df_plus[f'TimeGPT-sell_price_plus_{price_change * 100:.0f}%'], \n",
    "                             timegpt_fcst_df_minus[f'TimeGPT-sell_price_minus_{price_change * 100:.0f}%']], axis=1)\n",
    "\n",
    "timegpt_fcst_df.head(10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Finally, let's plot the forecasts for our different pricing scenarios, showing how TimeGPT forecasts a different demand when the price of a set of products is changed. In the graphs we can see that for specific products for certain periods the discount increases expected demand, while during other periods and for other products, price change has a smaller effect on total demand. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 552316 characters]",
      "text/plain": [
       "<Figure size 2400x1400 with 8 Axes>"
      ]
     },
     "execution_count": null,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nixtla_client.plot(\n",
    "    df[['unique_id', 'ds', 'y']], \n",
    "    timegpt_fcst_df, \n",
    "    max_insample_length=56, \n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this example, we have shown you:\n",
    "* How you can use TimeGPT to forecast product demand using price as an exogenous variable\n",
    "* How you can evaluate different pricing scenarios that affect product demand\n",
    "\n",
    "::: {.callout-important}\n",
    "* This method assumes that historical demand and price behaviour is predictive of future demand, and omits other factors affecting demand. To include these other factors, use additional exogenous variables that provide the model with more context about the factors influencing demand.\n",
    "* This method is sensitive to unmodelled events that affect the demand, such as sudden market shifts. To include those, use additional exogenous variables indicating such sudden shifts if they have been observed in the past too.\n",
    ":::"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "python3",
   "language": "python",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
