{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Advanced Prophet Usage\n", "You can scan through [fbprophet docs](https://facebook.github.io/prophet/docs/quick_start.html) and find many options how to tweak your model. \n", "Some of that functionality is moved to initialization stage to be compatible with Sklearn API. We will showcase the parts that were moved to initialization, but you can also look for other model parameters that could help fine-tuning your model" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from hcrystalball.wrappers import ProphetWrapper" ] }, { "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 generate_tsdata\n", "X, y = generate_tsdata(n_dates=365*2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ProphetWrapper?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Advanced Holidays\n", "For holidays, we are able to define instead of single boolean attribute distribution around given day. We define `lower_window`, `upper_window` and `prior_scales`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "extra_holidays = {\n", " 'Whit Monday':{'lower_window': 2, 'upper_window':2, 'prior_scale': 10},\n", " 'Good Friday':{'lower_window': 1, 'upper_window':1, 'prior_scale': 30}\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Unusual Seasonalities" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "extra_seasonalities = [\n", " {\n", " 'name':'bi-weekly',\n", " 'period': 14.,\n", " 'fourier_order': 5,\n", " 'prior_scale': 15.0,\n", " 'mode': None\n", " },\n", " {\n", " 'name':'bi-yearly',\n", " 'period': 365*2.,\n", " 'fourier_order': 5, \n", " 'prior_scale': 5.0,\n", " 'mode': None\n", " },\n", "]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exogenous Variables" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "extra_regressors = ['trend_line']\n", "X['trend_line'] = np.arange(len(X))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "prophet = ProphetWrapper(\n", " extra_holidays=extra_holidays,\n", " extra_seasonalities=extra_seasonalities,\n", " extra_regressors=extra_regressors,\n", " name='prophet_extra',\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "(prophet.fit(X[:-10], y[:-10])\n", " .predict(X[-10:])\n", " .merge(y, left_index=True, right_index=True, how='outer')\n", " .tail(50)\n", " .plot()\n", ");" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Compared to non-tweaked model, this shows our fine-tuning helped to increase performance significantly" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "(ProphetWrapper().fit(X[:-10], y[:-10])\n", " .predict(X[-10:])\n", " .merge(y, left_index=True, right_index=True, how='outer')\n", " .tail(50)\n", " .plot()\n", ");" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Full Prophet Output\n", "If you need, you can also pass `full_prophet_output` and get rich predict output" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "(ProphetWrapper(full_prophet_output=True, conf_int=True)\n", " .fit(X[:-10], y[:-10])\n", " .predict(X[-10:])\n", ")" ] } ], "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.7.6" } }, "nbformat": 4, "nbformat_minor": 4 }