{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Advanced Sarimax Usage\n", "If you are not a magician who can easily infer correct SARIMAX orders from looking on PACF (partial autocorrelation function) and ACF (autocorrelation function), you want to rather leverage AutoSarima which finds them for you - set `init_with_autoarima` to True. \n", "\n", "If you want to further configure the search space of AutoARIMA, then you can provide all parameters of pmdarima.arima.AutoARIMA as `autoarima_dict` arguments.\n", "\n", "When you run cross-validation with enabled AutoARIMA (`init_with_autoarima`), it's often advisable to find the correct order only during the first fit call and reuse this model on all other splits in order to simulate the out-of-sample performance.\n", "\n", "The signature of SarimaxWrapper contains parameters of pmdarima.arima.ARIMA, not AutoARIMA class.\n", "\n", "For more parameters check [pmdarima docs](http://alkaline-ml.com/pmdarima/)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "plt.style.use('seaborn')\n", "plt.rcParams['figure.figsize'] = [12, 6]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from hcrystalball.utils import get_sales_data\n", "\n", "df = get_sales_data(n_dates=100, \n", " n_assortments=1, \n", " n_states=1, \n", " n_stores=1)\n", "X, y = df[[\"Open\"]], df['Sales']" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "X" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from hcrystalball.wrappers import SarimaxWrapper" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "SarimaxWrapper?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "model = SarimaxWrapper(\n", " autoarima_dict={'d':1, 'm':7, 'max_p':2, 'max_q':2}, \n", " init_with_autoarima=True\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "preds = (model.fit(X[:-10], y[:-10])\n", " .predict(X[-10:])\n", " .merge(y, left_index=True, right_index=True, how='outer')\n", " .tail(50) \n", ")\n", "preds.plot(title=f\"MAE:{(preds['Sales']-preds['sarimax']).abs().mean().round(3)}\");" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And now access the models parameters" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "model" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You might also directly pass the orders if you know, what are you doing" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "model = SarimaxWrapper(order=(1, 1, 2), seasonal_order=(1, 0, 2, 7))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "preds = (model.fit(X[:-10], y[:-10])\n", " .predict(X[-10:])\n", " .merge(y, left_index=True, right_index=True, how='outer')\n", " .tail(50) \n", ")\n", "preds.plot(title=f\"MAE:{(preds['Sales']-preds['sarimax']).abs().mean().round(3)}\");" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "model" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "model" ] } ], "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.8.10" } }, "nbformat": 4, "nbformat_minor": 4 }