{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/ourownstory/neural_prophet/blob/master/tutorials/feature-use/sub_daily_data_yosemite_temps.ipynb)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Sub-daily data\n",
    "NeuralProphet can make forecasts for time series with sub-daily observations by passing in a dataframe with timestamps in the ds column. The format of the timestamps should be `YYYY-MM-DD HH:MM:SS` - see the example csv [here](https://github.com/ourownstory/neuralprophet-data/blob/main/datasets/yosemite_temps.csv). When sub-daily data are used, daily seasonality will automatically be fit. \n",
    "\n",
    "Here we fit NeuralProphet to data with 5-minute resolution (daily temperatures at Yosemite). "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "if 'google.colab' in str(get_ipython()):\n",
    "    !pip install git+https://github.com/ourownstory/neural_prophet.git # may take a while\n",
    "    #!pip install neuralprophet # much faster, but may not have the latest upgrades/bugfixes\n",
    "\n",
    "import pandas as pd\n",
    "from neuralprophet import NeuralProphet, set_log_level\n",
    "set_log_level(\"ERROR\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_location = \"https://raw.githubusercontent.com/ourownstory/neuralprophet-data/main/datasets/\"\n",
    "\n",
    "df = pd.read_csv(data_location + \"yosemite_temps.csv\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we will attempt to forecast the next 7 days. The `5min` data resulution means that we have `60/5*24=288` daily values. Thus, we want to forecast `7*288` periods ahead.\n",
    "\n",
    "Using some common sense, we set:\n",
    "* First, we disable weekly seasonality, as nature does not follow the human week's calendar.\n",
    "* Second, we disable changepoints, as the dataset only contains two months of data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "aa42f2a23a434d0682b2077e0b042bf8",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/313 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "                                                                                                                                                  \r"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 220780 characters]",
      "text/plain": [
       "<Figure size 720x432 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 42496 characters]",
      "text/plain": [
       "<Figure size 720x432 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "m = NeuralProphet(\n",
    "    n_changepoints=0,\n",
    "    weekly_seasonality=False,\n",
    ")\n",
    "metrics = m.fit(df, freq='5min')\n",
    "future = m.make_future_dataframe(df, periods=7*288, n_historic_predictions=True)\n",
    "forecast = m.predict(future)\n",
    "fig = m.plot(forecast)\n",
    "# fig_comp = m.plot_components(forecast)\n",
    "fig_param = m.plot_parameters()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The daily seasonality seems to make sense, when we account for the time being recorded in GMT, while Yosemite local time is GMT-8.\n",
    "\n",
    "## Improving trend and seasonality \n",
    "As we have `288` daily values recorded, we can increase the flexibility of `daily_seasonality`, without danger of overfitting. \n",
    "\n",
    "Further, we may want to re-visit our decision to disable changepoints, as the data clearly shows changes in trend, as is typical with the weather. We make the following changes:\n",
    "* increase the `changepoints_range`, as the we are doing a short-term prediction\n",
    "* inrease the `n_changepoints` to allow to fit to the sudden changes in trend\n",
    "* carefully regularize the trend changepoints by setting `trend_reg` in order to avoid overfitting"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "7c6b9217267349a1b79bcbc9079e7216",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/313 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "                                                                                                                                                  \r"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 220812 characters]",
      "text/plain": [
       "<Figure size 720x432 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 59564 characters]",
      "text/plain": [
       "<Figure size 720x648 with 3 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "m = NeuralProphet(\n",
    "    changepoints_range=0.95,\n",
    "    n_changepoints=50,\n",
    "    trend_reg=1,\n",
    "    weekly_seasonality=False,\n",
    "    daily_seasonality=10,\n",
    ")\n",
    "metrics = m.fit(df, freq='5min')\n",
    "future = m.make_future_dataframe(df, periods=60//5*24*7, n_historic_predictions=True)\n",
    "forecast = m.predict(future)\n",
    "fig = m.plot(forecast)\n",
    "# fig_comp = m.plot_components(forecast)\n",
    "fig_param = m.plot_parameters()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "np-dev",
   "language": "python",
   "name": "np-dev"
  },
  "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.9.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
