Skip to content

Commit

Permalink
Notebooks for module on intermittent time series forecasting
Browse files Browse the repository at this point in the history
  • Loading branch information
marcopeix committed Sep 30, 2023
1 parent a741680 commit 044bf28
Show file tree
Hide file tree
Showing 10 changed files with 2,922 additions and 0 deletions.
280 changes: 280 additions & 0 deletions 26_croston.ipynb

Large diffs are not rendered by default.

88 changes: 88 additions & 0 deletions 26_croston_starter.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "81a34b98",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"\n",
"import warnings\n",
"warnings.filterwarnings('ignore')\n",
"\n",
"%matplotlib inline"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e1cf585c",
"metadata": {},
"outputs": [],
"source": [
"plt.rcParams[\"figure.figsize\"] = (9,6)"
]
},
{
"cell_type": "markdown",
"id": "97dfdc88",
"metadata": {},
"source": [
"# Croston's method"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "67466e30",
"metadata": {},
"outputs": [],
"source": [
"df = pd.read_csv('data/intermittent_time_series.csv')\n",
"\n",
"df.head()"
]
},
{
"cell_type": "markdown",
"id": "2389581d",
"metadata": {},
"source": [
"## Optimized Croston's Method "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6c116ec6",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.10.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
332 changes: 332 additions & 0 deletions 27_adida_imapa.ipynb

Large diffs are not rendered by default.

196 changes: 196 additions & 0 deletions 27_adida_imapa_starter.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "6027e279",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"\n",
"from statsforecast import StatsForecast\n",
"from statsforecast.models import CrostonOptimized\n",
"\n",
"import warnings\n",
"warnings.filterwarnings('ignore')\n",
"\n",
"%matplotlib inline"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "89c57f05",
"metadata": {},
"outputs": [],
"source": [
"plt.rcParams[\"figure.figsize\"] = (9,6)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "224ffed1",
"metadata": {},
"outputs": [],
"source": [
"df = pd.read_csv('data/intermittent_time_series.csv')\n",
"\n",
"df.head()"
]
},
{
"cell_type": "markdown",
"id": "df4c6ea3",
"metadata": {},
"source": [
"## ADIDA "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d8be5a99",
"metadata": {},
"outputs": [],
"source": [
"# Model with ADIDA and Croston"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "768d18c8",
"metadata": {},
"outputs": [],
"source": [
"fig, ax = plt.subplots(figsize=(10,8))\n",
"\n",
"ax.bar(df.index, df['y'], color='lightgray')\n",
"ax.plot(cv_df.index, cv_df['CrostonOptimized'], ls='--', label='Croston (optimized)')\n",
"ax.plot(cv_df.index, cv_df['ADIDA'], ls=':', label='ADIDA')\n",
"ax.set_ylabel('Value')\n",
"ax.set_xlabel('Time steps')\n",
"ax.legend(loc='best')\n",
"plt.xlim(40, 100)\n",
"\n",
"plt.tight_layout()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "feed0476",
"metadata": {},
"outputs": [],
"source": [
"# Calculate the MAE\n",
"from sklearn.metrics import mean_absolute_error\n",
"\n",
"\n",
"print(f'MAE Croston: {mae_croston}')\n",
"print(f'MAE ADIDA: {mae_adida}')"
]
},
{
"cell_type": "markdown",
"id": "f28706aa",
"metadata": {},
"source": [
"## IMAPA "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8ed3259f",
"metadata": {},
"outputs": [],
"source": [
"# Model with IMAPA and ADIDA\n",
"\n",
"sf = StatsForecast(\n",
" df=df,\n",
" models=models,\n",
" freq='H',\n",
" n_jobs=-1\n",
")\n",
"\n",
"cv_df = sf.cross_validation(\n",
" df=df,\n",
" h=1,\n",
" step_size=1,\n",
" n_windows=50\n",
")\n",
"\n",
"cv_df.index = np.arange(50, 100, 1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0e7710e0",
"metadata": {},
"outputs": [],
"source": [
"fig, ax = plt.subplots(figsize=(10,8))\n",
"\n",
"ax.bar(df.index, df['y'], color='lightgray')\n",
"ax.plot(cv_df.index, cv_df['IMAPA'], ls='--', label='IMAPA')\n",
"ax.plot(cv_df.index, cv_df['ADIDA'], ls=':', label='ADIDA')\n",
"ax.set_ylabel('Value')\n",
"ax.set_xlabel('Time steps')\n",
"ax.legend(loc='best')\n",
"plt.xlim(40, 100)\n",
"\n",
"plt.tight_layout()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "87d6cbe5",
"metadata": {},
"outputs": [],
"source": [
"mae_imapa = mean_absolute_error(cv_df['y'], cv_df['IMAPA'])\n",
"mae_adida = mean_absolute_error(cv_df['y'], cv_df['ADIDA'])\n",
"\n",
"print(f'MAE Croston: {mae_imapa}')\n",
"print(f'MAE ADIDA: {mae_adida}')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a98074fa",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.10.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading

0 comments on commit 044bf28

Please sign in to comment.