From c8ef06d360ed0189bab5f01a4d0c7cdbd7f2c1a8 Mon Sep 17 00:00:00 2001 From: fede Date: Fri, 18 Mar 2022 20:30:02 -0600 Subject: [PATCH] Revert "Feat: add AutoARIMA adapter for prophet pipelines" --- docs/adapters.prophet.html | 519 ----------------------------- environment.yml | 1 - nbs/adapters.prophet.ipynb | 468 -------------------------- statsforecast/_nbdev.py | 6 +- statsforecast/adapters/__init__.py | 0 statsforecast/adapters/prophet.py | 207 ------------ 6 files changed, 2 insertions(+), 1199 deletions(-) delete mode 100644 docs/adapters.prophet.html delete mode 100644 nbs/adapters.prophet.ipynb delete mode 100644 statsforecast/adapters/__init__.py delete mode 100644 statsforecast/adapters/prophet.py diff --git a/docs/adapters.prophet.html b/docs/adapters.prophet.html deleted file mode 100644 index d8f7ca684..000000000 --- a/docs/adapters.prophet.html +++ /dev/null @@ -1,519 +0,0 @@ ---- - -title: Adapters for Prophet - - -keywords: fastai -sidebar: home_sidebar - - - -nb_path: "nbs/adapters.prophet.ipynb" ---- - - -
- - {% raw %} - -
- -
-
- -
- -
-
2022-03-17 00:21:14 prophet.plot ERROR: Importing plotly failed. Interactive plots will not work.
-
-
-
- -
-
- -
- {% endraw %} - - {% raw %} - -
- -
- {% endraw %} - -
-
-

Arima

-
-
-
- {% raw %} - -
- -
-
- -
- - -
-

class AutoARIMAProphet[source]

AutoARIMAProphet(growth='linear', changepoints=None, n_changepoints=25, changepoint_range=0.8, yearly_seasonality='auto', weekly_seasonality='auto', daily_seasonality='auto', holidays=None, seasonality_mode='additive', seasonality_prior_scale=10.0, holidays_prior_scale=10.0, changepoint_prior_scale=0.05, mcmc_samples=0, interval_width=0.8, uncertainty_samples=1000, stan_backend=None, d=None, D=None, max_p=5, max_q=5, max_P=2, max_Q=2, max_order=5, max_d=2, max_D=1, start_p=2, start_q=2, start_P=1, start_Q=1, stationary=False, seasonal=True, ic='aicc', stepwise=True, nmodels=94, trace=False, approximation=False, method=None, truncate=None, test='kpss', test_kwargs=None, seasonal_test='seas', seasonal_test_kwargs=None, allowdrift=False, allowmean=False, blambda=None, biasadj=False, parallel=False, num_cores=2, period=1) :: Prophet

-
-

Returns best ARIMA model using external variables created -by the Prophet interface.

-

This class receives as parameters the same as prophet.Prophet -and statsforecast.arima.AutoARIMA.

-

If your pipeline uses Prophet you can simply replace Prophet -with AutoARIMAProphet and you'll be using AutoARIMA insted -of Prophet.

- -
- -
- -
-
- -
- {% endraw %} - - {% raw %} - -
- -
- {% endraw %} - -
-
-

Peyton Manning example

-
-
-
- {% raw %} - -
-
- -
-
-
import pandas as pd
-df = pd.read_csv('https://raw.githubusercontent.com/facebook/prophet/main/examples/example_wp_log_peyton_manning.csv')
-
- -
-
-
- -
- {% endraw %} - -
-
-

Without additional info

-
-
-
- {% raw %} - -
-
- -
-
-
m = AutoARIMAProphet()
-
- -
-
-
- -
- {% endraw %} - - {% raw %} - -
-
- -
-
-
%%capture
-m.fit(df)
-
- -
-
-
- -
-
- -
- -
-
2022-03-17 00:18:03 prophet INFO: Disabling daily seasonality. Run prophet with daily_seasonality=True to override this.
-
-
-
- -
-
- -
- {% endraw %} - - {% raw %} - -
-
- -
-
-
future = m.make_future_dataframe(365)
-
- -
-
-
- -
- {% endraw %} - - {% raw %} - -
-
- -
-
-
forecast = m.predict(future)
-
- -
-
-
- -
-
- -
- -
-
/home/ubuntu/fede/sktime/statsforecast/statsforecast/arima.py:2384: FutureWarning: In a future version of pandas all arguments of concat except for the argument 'objs' will be keyword-only
-  return pd.concat([lo, mean, hi], 1)
-
-
-
- -
-
- -
- {% endraw %} - - {% raw %} - -
-
- -
-
-
fig = m.plot(forecast)
-
- -
-
-
- -
-
- -
- - - -
- -
- -
- -
-
- -
- {% endraw %} - - {% raw %} - -
-
- -
-
-
in_sample = m.predict()
-
- -
-
-
- -
- {% endraw %} - - {% raw %} - -
-
- -
-
-
fig_in = m.plot(in_sample)
-
- -
-
-
- -
-
- -
- - - -
- -
- -
- -
-
- -
- {% endraw %} - -
-
-

With exogenous regressors provided by prophet

-
-
-
- {% raw %} - -
-
- -
-
-
playoffs = pd.DataFrame({
-  'holiday': 'playoff',
-  'ds': pd.to_datetime(['2008-01-13', '2009-01-03', '2010-01-16',
-                        '2010-01-24', '2010-02-07', '2011-01-08',
-                        '2013-01-12', '2014-01-12', '2014-01-19',
-                        '2014-02-02', '2015-01-11', '2016-01-17',
-                        '2016-01-24', '2016-02-07']),
-  'lower_window': 0,
-  'upper_window': 1,
-})
-superbowls = pd.DataFrame({
-  'holiday': 'superbowl',
-  'ds': pd.to_datetime(['2010-02-07', '2014-02-02', '2016-02-07']),
-  'lower_window': 0,
-  'upper_window': 1,
-})
-holidays = pd.concat((playoffs, superbowls))
-
- -
-
-
- -
- {% endraw %} - - {% raw %} - -
-
- -
-
-
m = AutoARIMAProphet(holidays=holidays)
-
- -
-
-
- -
- {% endraw %} - - {% raw %} - -
-
- -
-
-
m.add_country_holidays(country_name='US')
-
- -
-
-
- -
-
- -
- - - -
-
<__main__.AutoARIMAProphet at 0x7fd344b7f750>
-
- -
- -
-
- -
- {% endraw %} - - {% raw %} - -
-
- -
-
-
%%capture
-m.fit(df)
-
- -
-
-
- -
-
- -
- -
-
2022-03-17 00:11:58 prophet INFO: Disabling daily seasonality. Run prophet with daily_seasonality=True to override this.
-
-
-
- -
-
- -
- {% endraw %} - - {% raw %} - -
-
- -
-
-
future = m.make_future_dataframe(365)
-
- -
-
-
- -
- {% endraw %} - - {% raw %} - -
-
- -
-
-
forecast = m.predict(future)
-
- -
-
-
- -
-
- -
- -
-
/home/ubuntu/fede/sktime/statsforecast/statsforecast/arima.py:2384: FutureWarning: In a future version of pandas all arguments of concat except for the argument 'objs' will be keyword-only
-  return pd.concat([lo, mean, hi], 1)
-
-
-
- -
-
- -
- {% endraw %} - - {% raw %} - -
-
- -
-
-
fig = m.plot(forecast)
-
- -
-
-
- -
-
- -
- - - -
- -
- -
- -
-
- -
- {% endraw %} - -
- - diff --git a/environment.yml b/environment.yml index c5d4c7ef2..372e5bda9 100644 --- a/environment.yml +++ b/environment.yml @@ -7,7 +7,6 @@ dependencies: - numba - numpy - pandas - - prophet - pip - scipy - statsmodels diff --git a/nbs/adapters.prophet.ipynb b/nbs/adapters.prophet.ipynb deleted file mode 100644 index b6f3f83df..000000000 --- a/nbs/adapters.prophet.ipynb +++ /dev/null @@ -1,468 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": null, - "id": "fe274985-cc73-405c-a071-54ad64ff9b21", - "metadata": {}, - "outputs": [], - "source": [ - "#default_exp adapters.prophet" - ] - }, - { - "cell_type": "markdown", - "id": "bc2893ba-429b-4fd0-bc90-6c8dd321b243", - "metadata": {}, - "source": [ - "# Adapters for Prophet" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "838ffafe-46b7-432b-9066-a7a3f52af6e0", - "metadata": {}, - "outputs": [], - "source": [ - "#export\n", - "from copy import deepcopy\n", - "\n", - "from prophet import Prophet\n", - "from statsforecast.arima import AutoARIMA" - ] - }, - { - "cell_type": "markdown", - "id": "2e175b82-e452-48c2-859a-012da60f1259", - "metadata": {}, - "source": [ - "## Arima" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "00253a27-0cf8-4589-bea4-fe4fa2b2fcb4", - "metadata": {}, - "outputs": [], - "source": [ - "#export\n", - "class AutoARIMAProphet(Prophet):\n", - " \"\"\"Returns best ARIMA model using external variables created \n", - " by the Prophet interface.\n", - " \n", - " This class receives as parameters the same as prophet.Prophet\n", - " and statsforecast.arima.AutoARIMA.\n", - " \n", - " If your pipeline uses Prophet you can simply replace Prophet\n", - " with AutoARIMAProphet and you'll be using AutoARIMA insted\n", - " of Prophet.\n", - " \"\"\"\n", - " \n", - " def __init__(self, \n", - " growth='linear',\n", - " changepoints=None,\n", - " n_changepoints=25,\n", - " changepoint_range=0.8,\n", - " yearly_seasonality='auto',\n", - " weekly_seasonality='auto',\n", - " daily_seasonality='auto',\n", - " holidays=None,\n", - " seasonality_mode='additive',\n", - " seasonality_prior_scale=10.0,\n", - " holidays_prior_scale=10.0,\n", - " changepoint_prior_scale=0.05,\n", - " mcmc_samples=0,\n", - " interval_width=0.80,\n", - " uncertainty_samples=1000,\n", - " stan_backend=None,\n", - " d=None,\n", - " D=None,\n", - " max_p=5,\n", - " max_q=5,\n", - " max_P= 2,\n", - " max_Q= 2,\n", - " max_order= 5,\n", - " max_d=2,\n", - " max_D=1,\n", - " start_p=2,\n", - " start_q=2,\n", - " start_P=1,\n", - " start_Q=1,\n", - " stationary=False,\n", - " seasonal=True,\n", - " ic='aicc',\n", - " stepwise=True,\n", - " nmodels=94,\n", - " trace=False,\n", - " approximation=False,\n", - " method=None,\n", - " truncate=None,\n", - " test='kpss',\n", - " test_kwargs=None,\n", - " seasonal_test='seas',\n", - " seasonal_test_kwargs=None,\n", - " allowdrift=False,\n", - " allowmean=False,\n", - " blambda=None,\n", - " biasadj=False,\n", - " parallel=False,\n", - " num_cores=2,\n", - " period=1):\n", - " Prophet.__init__(self,\n", - " growth,\n", - " changepoints,\n", - " n_changepoints,\n", - " changepoint_range,\n", - " yearly_seasonality,\n", - " weekly_seasonality,\n", - " daily_seasonality,\n", - " holidays,\n", - " seasonality_mode,\n", - " seasonality_prior_scale,\n", - " holidays_prior_scale,\n", - " changepoint_prior_scale,\n", - " mcmc_samples,\n", - " interval_width,\n", - " uncertainty_samples,\n", - " stan_backend)\n", - " self.arima = AutoARIMA(d=d,\n", - " D=D,\n", - " max_p=max_p,\n", - " max_q=max_q,\n", - " max_P=max_P,\n", - " max_Q=max_Q,\n", - " max_order=max_order,\n", - " max_d=max_d,\n", - " max_D=max_D,\n", - " start_p=start_p,\n", - " start_q=start_q,\n", - " start_P=start_P,\n", - " start_Q=start_Q,\n", - " stationary=stationary,\n", - " seasonal=seasonal,\n", - " ic=ic,\n", - " stepwise=stepwise,\n", - " nmodels=nmodels,\n", - " trace=trace,\n", - " approximation=approximation,\n", - " method=method,\n", - " truncate=truncate,\n", - " test=test,\n", - " test_kwargs=test_kwargs,\n", - " seasonal_test=seasonal_test,\n", - " seasonal_test_kwargs=seasonal_test_kwargs,\n", - " allowdrift=allowdrift,\n", - " allowmean=allowmean,\n", - " blambda=blambda,\n", - " biasadj=biasadj,\n", - " parallel=parallel,\n", - " num_cores=num_cores,\n", - " period=period)\n", - " \n", - " def fit(self, df, **kwargs):\n", - " \"\"\"Fit the AutoARIMAProphet model.\n", - "\n", - " Parameters\n", - " ----------\n", - " df: pd.DataFrame containing the history. Must have columns ds (date\n", - " type) and y, the time series.\n", - " kwargs: Additional arguments.\n", - " \n", - " Returns\n", - " -------\n", - " The AutoARIMAProphet object.\n", - " \"\"\"\n", - " if self.history is not None:\n", - " raise Exception('Prophet object can only be fit once. '\n", - " 'Instantiate a new object.')\n", - " if ('ds' not in df) or ('y' not in df):\n", - " raise ValueError(\n", - " 'Dataframe must have columns \"ds\" and \"y\" with the dates and '\n", - " 'values respectively.'\n", - " )\n", - " history = df[df['y'].notnull()].copy()\n", - " if history.shape[0] < 2:\n", - " raise ValueError('Dataframe has less than 2 non-NaN rows.')\n", - " self.history_dates = pd.to_datetime(pd.Series(df['ds'].unique(), name='ds')).sort_values()\n", - "\n", - " history = self.setup_dataframe(history, initialize_scales=True)\n", - " self.history = history\n", - " self.set_auto_seasonalities()\n", - " seasonal_features, prior_scales, component_cols, modes = (\n", - " self.make_all_seasonality_features(history))\n", - " self.train_component_cols = component_cols\n", - " self.component_modes = modes\n", - " self.fit_kwargs = deepcopy(kwargs)\n", - " \n", - " \n", - " self.arima = self.arima.fit(y = history['y'].values, X = seasonal_features.values)\n", - "\n", - " return self\n", - "\n", - " def predict(self, df=None):\n", - " \"\"\"Predict using the AutoARIMAProphet model.\n", - " \n", - " Parameters\n", - " ----------\n", - " df: pd.DataFrame with dates for predictions (column ds.\n", - " If not provided, predictions are\n", - " made on the history.\n", - " \n", - " Returns\n", - " -------\n", - " A pd.DataFrame with the forecast components.\n", - " \"\"\"\n", - " if self.history is None:\n", - " raise Exception('Model has not been fit.')\n", - "\n", - " if df is None:\n", - " df = self.history.copy()\n", - " else:\n", - " if df.shape[0] == 0:\n", - " raise ValueError('Dataframe has no rows.')\n", - " df = self.setup_dataframe(df.copy())\n", - " \n", - " seasonal_components = self.make_all_seasonality_features(df)[0] \n", - " \n", - " ds_forecast = set(df['ds'])\n", - " h = len(ds_forecast - set(self.history['ds']))\n", - " if h > 0:\n", - " df2 = self.arima.predict(h=h, \n", - " X=seasonal_components.values[-h:],\n", - " level=int(100*self.interval_width))\n", - " else:\n", - " df2 = pd.DataFrame()\n", - " if len(ds_forecast) > h: \n", - " in_sample = self.arima.predict_in_sample(level=int(100*self.interval_width))\n", - " df2 = pd.concat([in_sample, df2]).reset_index(drop=True)\n", - " \n", - " yhat = df2.pop('mean')\n", - " df2.columns = ['yhat_lower', 'yhat_upper']\n", - " df2.insert(0, 'yhat', yhat)\n", - " df2.insert(0, 'ds', df['ds'])\n", - " \n", - " return df2" - ] - }, - { - "cell_type": "markdown", - "id": "6136672f-d34b-4833-8782-811935cd2593", - "metadata": {}, - "source": [ - "### Peyton Manning example" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "5e9ba8d7-4890-457e-956f-d6d0269fc1f6", - "metadata": {}, - "outputs": [], - "source": [ - "import pandas as pd\n", - "df = pd.read_csv('https://raw.githubusercontent.com/facebook/prophet/main/examples/example_wp_log_peyton_manning.csv')" - ] - }, - { - "cell_type": "markdown", - "id": "312bd903-a199-4412-926f-33d57a68d2b8", - "metadata": { - "tags": [] - }, - "source": [ - "#### Without additional info" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "fbf323bb-4b65-4473-a5d4-f96b7708e38b", - "metadata": {}, - "outputs": [], - "source": [ - "m = AutoARIMAProphet()" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "bbec23b9-c7ec-4393-ac66-5178d1fb65f0", - "metadata": {}, - "outputs": [], - "source": [ - "%%capture\n", - "m.fit(df)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "091240bd-05a0-4fcf-a489-038abecd14c1", - "metadata": {}, - "outputs": [], - "source": [ - "future = m.make_future_dataframe(365)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "70faf083-4e54-4160-8a86-2102b7612c84", - "metadata": {}, - "outputs": [], - "source": [ - "forecast = m.predict(future)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "b15cf687-9ade-4165-8e2c-54eec5198025", - "metadata": {}, - "outputs": [], - "source": [ - "fig = m.plot(forecast)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "bed55431-03e6-466c-b83b-cc494d6d444a", - "metadata": {}, - "outputs": [], - "source": [ - "in_sample = m.predict()" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "3c9ef99c-9aa1-4a88-8d88-d00bcf92a995", - "metadata": {}, - "outputs": [], - "source": [ - "fig_in = m.plot(in_sample)" - ] - }, - { - "cell_type": "markdown", - "id": "6c7034db-3440-43ce-98c0-54a598da04d6", - "metadata": {}, - "source": [ - "### With exogenous regressors provided by prophet" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "b912a786-200a-4bbc-a2ac-6ab483311758", - "metadata": {}, - "outputs": [], - "source": [ - "playoffs = pd.DataFrame({\n", - " 'holiday': 'playoff',\n", - " 'ds': pd.to_datetime(['2008-01-13', '2009-01-03', '2010-01-16',\n", - " '2010-01-24', '2010-02-07', '2011-01-08',\n", - " '2013-01-12', '2014-01-12', '2014-01-19',\n", - " '2014-02-02', '2015-01-11', '2016-01-17',\n", - " '2016-01-24', '2016-02-07']),\n", - " 'lower_window': 0,\n", - " 'upper_window': 1,\n", - "})\n", - "superbowls = pd.DataFrame({\n", - " 'holiday': 'superbowl',\n", - " 'ds': pd.to_datetime(['2010-02-07', '2014-02-02', '2016-02-07']),\n", - " 'lower_window': 0,\n", - " 'upper_window': 1,\n", - "})\n", - "holidays = pd.concat((playoffs, superbowls))" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "58863c15-bfed-448d-a38f-4e4a6f895cb2", - "metadata": {}, - "outputs": [], - "source": [ - "m = AutoARIMAProphet(holidays=holidays)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "bc2f2caa-9583-48d2-b7af-8fe526045065", - "metadata": {}, - "outputs": [], - "source": [ - "m.add_country_holidays(country_name='US')" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "76c5cc06-aa38-4920-bae9-757a0f615898", - "metadata": { - "tags": [] - }, - "outputs": [], - "source": [ - "%%capture\n", - "m.fit(df)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "7a966736-72d8-48c7-abc4-9da313916ca1", - "metadata": {}, - "outputs": [], - "source": [ - "future = m.make_future_dataframe(365)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "a47facdb-882a-4fd8-9896-e4d6d791a98a", - "metadata": {}, - "outputs": [], - "source": [ - "forecast = m.predict(future)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "3d54a652-8fa4-41fe-81f1-6008966e5711", - "metadata": {}, - "outputs": [], - "source": [ - "fig = m.plot(forecast)" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3 (ipykernel)", - "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.11" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -} diff --git a/statsforecast/_nbdev.py b/statsforecast/_nbdev.py index 8dafad811..3005878d1 100644 --- a/statsforecast/_nbdev.py +++ b/statsforecast/_nbdev.py @@ -2,8 +2,7 @@ __all__ = ["index", "modules", "custom_doc_links", "git_url"] -index = {"AutoARIMAProphet": "adapters.prophet.ipynb", - "OptimResult": "arima.ipynb", +index = {"OptimResult": "arima.ipynb", "partrans": "arima.ipynb", "arima_gradtrans": "arima.ipynb", "arima_undopars": "arima.ipynb", @@ -60,8 +59,7 @@ "generate_series": "utils.ipynb", "AirPassengers": "utils.ipynb"} -modules = ["adapters/prophet.py", - "arima.py", +modules = ["arima.py", "core.py", "models.py", "utils.py"] diff --git a/statsforecast/adapters/__init__.py b/statsforecast/adapters/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/statsforecast/adapters/prophet.py b/statsforecast/adapters/prophet.py deleted file mode 100644 index 128c9cdf3..000000000 --- a/statsforecast/adapters/prophet.py +++ /dev/null @@ -1,207 +0,0 @@ -# AUTOGENERATED! DO NOT EDIT! File to edit: nbs/adapters.prophet.ipynb (unless otherwise specified). - -__all__ = ['AutoARIMAProphet'] - -# Cell -from copy import deepcopy - -from prophet import Prophet -from ..arima import AutoARIMA - -# Cell -class AutoARIMAProphet(Prophet): - """Returns best ARIMA model using external variables created - by the Prophet interface. - - This class receives as parameters the same as prophet.Prophet - and statsforecast.arima.AutoARIMA. - - If your pipeline uses Prophet you can simply replace Prophet - with AutoARIMAProphet and you'll be using AutoARIMA insted - of Prophet. - """ - - def __init__(self, - growth='linear', - changepoints=None, - n_changepoints=25, - changepoint_range=0.8, - yearly_seasonality='auto', - weekly_seasonality='auto', - daily_seasonality='auto', - holidays=None, - seasonality_mode='additive', - seasonality_prior_scale=10.0, - holidays_prior_scale=10.0, - changepoint_prior_scale=0.05, - mcmc_samples=0, - interval_width=0.80, - uncertainty_samples=1000, - stan_backend=None, - d=None, - D=None, - max_p=5, - max_q=5, - max_P= 2, - max_Q= 2, - max_order= 5, - max_d=2, - max_D=1, - start_p=2, - start_q=2, - start_P=1, - start_Q=1, - stationary=False, - seasonal=True, - ic='aicc', - stepwise=True, - nmodels=94, - trace=False, - approximation=False, - method=None, - truncate=None, - test='kpss', - test_kwargs=None, - seasonal_test='seas', - seasonal_test_kwargs=None, - allowdrift=False, - allowmean=False, - blambda=None, - biasadj=False, - parallel=False, - num_cores=2, - period=1): - Prophet.__init__(self, - growth, - changepoints, - n_changepoints, - changepoint_range, - yearly_seasonality, - weekly_seasonality, - daily_seasonality, - holidays, - seasonality_mode, - seasonality_prior_scale, - holidays_prior_scale, - changepoint_prior_scale, - mcmc_samples, - interval_width, - uncertainty_samples, - stan_backend) - self.arima = AutoARIMA(d=d, - D=D, - max_p=max_p, - max_q=max_q, - max_P=max_P, - max_Q=max_Q, - max_order=max_order, - max_d=max_d, - max_D=max_D, - start_p=start_p, - start_q=start_q, - start_P=start_P, - start_Q=start_Q, - stationary=stationary, - seasonal=seasonal, - ic=ic, - stepwise=stepwise, - nmodels=nmodels, - trace=trace, - approximation=approximation, - method=method, - truncate=truncate, - test=test, - test_kwargs=test_kwargs, - seasonal_test=seasonal_test, - seasonal_test_kwargs=seasonal_test_kwargs, - allowdrift=allowdrift, - allowmean=allowmean, - blambda=blambda, - biasadj=biasadj, - parallel=parallel, - num_cores=num_cores, - period=period) - - def fit(self, df, **kwargs): - """Fit the AutoARIMAProphet model. - - Parameters - ---------- - df: pd.DataFrame containing the history. Must have columns ds (date - type) and y, the time series. - kwargs: Additional arguments. - - Returns - ------- - The AutoARIMAProphet object. - """ - if self.history is not None: - raise Exception('Prophet object can only be fit once. ' - 'Instantiate a new object.') - if ('ds' not in df) or ('y' not in df): - raise ValueError( - 'Dataframe must have columns "ds" and "y" with the dates and ' - 'values respectively.' - ) - history = df[df['y'].notnull()].copy() - if history.shape[0] < 2: - raise ValueError('Dataframe has less than 2 non-NaN rows.') - self.history_dates = pd.to_datetime(pd.Series(df['ds'].unique(), name='ds')).sort_values() - - history = self.setup_dataframe(history, initialize_scales=True) - self.history = history - self.set_auto_seasonalities() - seasonal_features, prior_scales, component_cols, modes = ( - self.make_all_seasonality_features(history)) - self.train_component_cols = component_cols - self.component_modes = modes - self.fit_kwargs = deepcopy(kwargs) - - - self.arima = self.arima.fit(y = history['y'].values, X = seasonal_features.values) - - return self - - def predict(self, df=None): - """Predict using the AutoARIMAProphet model. - - Parameters - ---------- - df: pd.DataFrame with dates for predictions (column ds. - If not provided, predictions are - made on the history. - - Returns - ------- - A pd.DataFrame with the forecast components. - """ - if self.history is None: - raise Exception('Model has not been fit.') - - if df is None: - df = self.history.copy() - else: - if df.shape[0] == 0: - raise ValueError('Dataframe has no rows.') - df = self.setup_dataframe(df.copy()) - - seasonal_components = self.make_all_seasonality_features(df)[0] - - ds_forecast = set(df['ds']) - h = len(ds_forecast - set(self.history['ds'])) - if h > 0: - df2 = self.arima.predict(h=h, - X=seasonal_components.values[-h:], - level=int(100*self.interval_width)) - else: - df2 = pd.DataFrame() - if len(ds_forecast) > h: - in_sample = self.arima.predict_in_sample(level=int(100*self.interval_width)) - df2 = pd.concat([in_sample, df2]).reset_index(drop=True) - - yhat = df2.pop('mean') - df2.columns = ['yhat_lower', 'yhat_upper'] - df2.insert(0, 'yhat', yhat) - df2.insert(0, 'ds', df['ds']) - - return df2 \ No newline at end of file