-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Notebooks for module on intermittent time series forecasting
- Loading branch information
Showing
10 changed files
with
2,922 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.