"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "notebooks/additional_topics.ipynb" between
prophet-0.7.tar.gz and prophet-1.0.tar.gz

About: Prophet is a tool for producing high quality forecasts for time series data that has multiple seasonality with linear or non-linear growth.

additional_topics.ipynb  (prophet-0.7):additional_topics.ipynb  (prophet-1.0)
{ {
"cells": [ "cells": [
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 1, "execution_count": 1,
"metadata": { "metadata": {
"block_hidden": true "block_hidden": true
}, },
"outputs": [],
"source": [
"%load_ext rpy2.ipython\n",
"%matplotlib inline\n",
"import pandas as pd\n",
"import numpy as np\n",
"from prophet import Prophet\n",
"import logging\n",
"logging.getLogger('prophet').setLevel(logging.ERROR)\n",
"import warnings\n",
"warnings.filterwarnings(\"ignore\")"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"block_hidden": true
},
"outputs": [ "outputs": [
{ {
"name": "stderr",
"output_type": "stream",
"text": [
"INFO:numexpr.utils:NumExpr defaulting to 8 threads.\n"
]
},
{
"data": { "data": {
"text/plain": [ "text/plain": [
"<fbprophet.forecaster.Prophet at 0x7f9eb843ca90>" "<prophet.forecaster.Prophet at 0x7f578ce95760>"
] ]
}, },
"execution_count": 1, "execution_count": 2,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
], ],
"source": [ "source": [
"%load_ext rpy2.ipython\n",
"%matplotlib inline\n",
"import pandas as pd\n",
"import numpy as np\n",
"from fbprophet import Prophet\n",
"import logging\n",
"logging.getLogger('fbprophet').setLevel(logging.ERROR)\n",
"import warnings\n",
"warnings.filterwarnings(\"ignore\")\n",
"\n",
"df = pd.DataFrame({\n", "df = pd.DataFrame({\n",
" 'ds': pd.date_range(start='2020-01-01', periods=20),\n", " 'ds': pd.date_range(start='2020-01-01', periods=20),\n",
" 'y': np.arange(20),\n", " 'y': np.arange(20),\n",
"})\n", "})\n",
"m = Prophet(weekly_seasonality=False)\n", "m = Prophet(weekly_seasonality=False)\n",
"m.fit(df)" "m.fit(df)"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 2, "execution_count": 2,
"metadata": { "metadata": {
"block_hidden": true "block_hidden": true
}, },
"outputs": [ "outputs": [
{ {
"name": "stderr", "name": "stderr",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"WARNING:rpy2.rinterface_lib.callbacks:R[write to console]: Loading requir ed package: Rcpp\n", "R[write to console]: Loading required package: Rcpp\n",
"\n", "\n",
"WARNING:rpy2.rinterface_lib.callbacks:R[write to console]: Loading requir ed package: rlang\n", "R[write to console]: Loading required package: rlang\n",
"\n", "\n",
"WARNING:rpy2.rinterface_lib.callbacks:R[write to console]: Disabling year ly seasonality. Run prophet with yearly.seasonality=TRUE to override this.\n", "R[write to console]: Disabling yearly seasonality. Run prophet with yearl y.seasonality=TRUE to override this.\n",
"\n", "\n",
"WARNING:rpy2.rinterface_lib.callbacks:R[write to console]: Disabling dail y seasonality. Run prophet with daily.seasonality=TRUE to override this.\n", "R[write to console]: Disabling daily seasonality. Run prophet with daily. seasonality=TRUE to override this.\n",
"\n", "\n",
"WARNING:rpy2.rinterface_lib.callbacks:R[write to console]: n.changepoints greater than number of observations. Using 15\n", "R[write to console]: n.changepoints greater than number of observations. Using 15\n",
"\n" "\n"
] ]
} }
], ],
"source": [ "source": [
"%%R\n", "%%R\n",
"library(prophet)\n", "library(prophet)\n",
"df <- data.frame(\n", "df <- data.frame(\n",
" ds=seq(as.Date(\"2020-01-01\"), by = \"day\", length.out = 20),\n", " ds=seq(as.Date(\"2020-01-01\"), by = \"day\", length.out = 20),\n",
" y=1:20\n", " y=1:20\n",
skipping to change at line 105 skipping to change at line 121
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"In Python, models should not be saved with pickle; the Stan backend attache d to the model object will not pickle well, and will produce issues under certai n versions of Python. Instead, you should use the built-in serialization functio ns to serialize the model to json:" "In Python, models should not be saved with pickle; the Stan backend attache d to the model object will not pickle well, and will produce issues under certai n versions of Python. Instead, you should use the built-in serialization functio ns to serialize the model to json:"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 4, "execution_count": 3,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"import json\n", "import json\n",
"from fbprophet.serialize import model_to_json, model_from_json\n", "from prophet.serialize import model_to_json, model_from_json\n",
"\n", "\n",
"with open('serialized_model.json', 'w') as fout:\n", "with open('serialized_model.json', 'w') as fout:\n",
" json.dump(model_to_json(m), fout) # Save model\n", " json.dump(model_to_json(m), fout) # Save model\n",
"\n", "\n",
"with open('serialized_model.json', 'r') as fin:\n", "with open('serialized_model.json', 'r') as fin:\n",
" m = model_from_json(json.load(fin)) # Load model" " m = model_from_json(json.load(fin)) # Load model"
] ]
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"The json file will be portable across systems, and deserialization is backw ards compatible with older versions of fbprophet." "The json file will be portable across systems, and deserialization is backw ards compatible with older versions of prophet."
] ]
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"### Flat trend and custom trends\n", "### Flat trend and custom trends\n",
"\n", "\n",
"For time series that exhibit strong seasonality patterns rather than trend changes, it may be useful to force the trend growth rate to be flat. This can be achieved simply by passing `growth=flat` when creating the model:" "For time series that exhibit strong seasonality patterns rather than trend changes, it may be useful to force the trend growth rate to be flat. This can be achieved simply by passing `growth=flat` when creating the model:"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 5, "execution_count": 4,
"metadata": {
"output_hidden": true
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"R[write to console]: Disabling yearly seasonality. Run prophet with yearl
y.seasonality=TRUE to override this.\n",
"\n",
"R[write to console]: Disabling daily seasonality. Run prophet with daily.
seasonality=TRUE to override this.\n",
"\n",
"R[write to console]: n.changepoints greater than number of observations.
Using 15\n",
"\n"
]
}
],
"source": [
"%%R\n",
"m <- prophet(df, growth='flat')"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"m = Prophet(growth='flat')" "m = Prophet(growth='flat')"
] ]
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"This is currently implemented only in the Python version of Prophet. Note t hat if this is used on a time series that doesn't have a constant trend, any tre nd will be fit with the noise term and so there will be high predictive uncertai nty in the forecast.\n", "Note that if this is used on a time series that doesn't have a constant tre nd, any trend will be fit with the noise term and so there will be high predicti ve uncertainty in the forecast.\n",
"\n", "\n",
"To use a trend besides these three built-in trend functions (piecewise line ar, piecewise logistic growth, and flat), you can download the source code from github, modify the trend function as desired in a local branch, and then install that local version. This PR provides a good illustration of what must be done t o implement a custom trend: https://github.com/facebook/prophet/pull/1466/files. " "To use a trend besides these three built-in trend functions (piecewise line ar, piecewise logistic growth, and flat), you can download the source code from github, modify the trend function as desired in a local branch, and then install that local version. This PR provides a good illustration of what must be done t o implement a custom trend (https://github.com/facebook/prophet/pull/1466/files) , as does this one that implements a step function trend (https://github.com/fac ebook/prophet/pull/1794) and this one for a new trend in R (https://github.com/f acebook/prophet/pull/1778)."
] ]
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"### Updating fitted models\n", "### Updating fitted models\n",
"\n", "\n",
"A common setting for forecasting is fitting models that need to be updated as additional data come in. Prophet models can only be fit once, and a new model must be re-fit when new data become available. In most settings, model fitting is fast enough that there isn't any issue with re-fitting from scratch. However, it is possible to speed things up a little by warm-starting the fit from the mo del parameters of the earlier model. This code example shows how this can be don e in Python:" "A common setting for forecasting is fitting models that need to be updated as additional data come in. Prophet models can only be fit once, and a new model must be re-fit when new data become available. In most settings, model fitting is fast enough that there isn't any issue with re-fitting from scratch. However, it is possible to speed things up a little by warm-starting the fit from the mo del parameters of the earlier model. This code example shows how this can be don e in Python:"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 6, "execution_count": 5,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"1.44 s ± 121 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n", "1.33 s ± 55.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
"860 ms ± 203 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n" "185 ms ± 4.46 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
] ]
} }
], ],
"source": [ "source": [
"def stan_init(m):\n", "def stan_init(m):\n",
" \"\"\"Retrieve parameters from a trained model.\n", " \"\"\"Retrieve parameters from a trained model.\n",
" \n", " \n",
" Retrieve parameters from a trained model in the format\n", " Retrieve parameters from a trained model in the format\n",
" used to initialize a new Stan model.\n", " used to initialize a new Stan model.\n",
" \n", " \n",
skipping to change at line 212 skipping to change at line 253
"\n", "\n",
"\n", "\n",
"%timeit m2 = Prophet().fit(df) # Adding the last day, fitting from scratch \n", "%timeit m2 = Prophet().fit(df) # Adding the last day, fitting from scratch \n",
"%timeit m2 = Prophet().fit(df, init=stan_init(m1)) # Adding the last day, warm-starting from m1" "%timeit m2 = Prophet().fit(df, init=stan_init(m1)) # Adding the last day, warm-starting from m1"
] ]
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"As can be seen, the parameters from the previous model are passed in to the fitting for the next with the kwarg `init`. In this case, model fitting was alm ost 2x faster when using warm starting. The speedup will generally depend on how much the optimal model parameters have changed with the addition of the new dat a.\n", "As can be seen, the parameters from the previous model are passed in to the fitting for the next with the kwarg `init`. In this case, model fitting was abo ut 5x faster when using warm starting. The speedup will generally depend on how much the optimal model parameters have changed with the addition of the new data .\n",
"\n", "\n",
"There are few caveats that should be kept in mind when considering warm-sta rting. First, warm-starting may work well for small updates to the data (like th e addition of one day in the example above) but can be worse than fitting from s cratch if there are large changes to the data (i.e., a lot of days have been add ed). This is because when a large amount of history is added, the location of th e changepoints will be very different between the two models, and so the paramet ers from the previous model may actually produce a bad trend initialization. Sec ond, as a detail, the number of changepoints need to be consistent from one mode l to the next or else an error will be raised because the changepoint prior para meter `delta` will be the wrong size." "There are few caveats that should be kept in mind when considering warm-sta rting. First, warm-starting may work well for small updates to the data (like th e addition of one day in the example above) but can be worse than fitting from s cratch if there are large changes to the data (i.e., a lot of days have been add ed). This is because when a large amount of history is added, the location of th e changepoints will be very different between the two models, and so the paramet ers from the previous model may actually produce a bad trend initialization. Sec ond, as a detail, the number of changepoints need to be consistent from one mode l to the next or else an error will be raised because the changepoint prior para meter `delta` will be the wrong size."
] ]
} }
], ],
"metadata": { "metadata": {
"celltoolbar": "Edit Metadata", "celltoolbar": "Edit Metadata",
"kernelspec": { "kernelspec": {
"display_name": "Python 3", "display_name": "Python 3",
"language": "python", "language": "python",
skipping to change at line 235 skipping to change at line 276
"language_info": { "language_info": {
"codemirror_mode": { "codemirror_mode": {
"name": "ipython", "name": "ipython",
"version": 3 "version": 3
}, },
"file_extension": ".py", "file_extension": ".py",
"mimetype": "text/x-python", "mimetype": "text/x-python",
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.7.8" "version": "3.8.3"
} }
}, },
"nbformat": 4, "nbformat": 4,
"nbformat_minor": 2 "nbformat_minor": 2
} }
 End of changes. 20 change blocks. 
28 lines changed or deleted 72 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)