Introduction to Prophet Algorithm

Nadeem
CodeX
Published in
5 min readDec 18, 2021

Prophet is a popular local Bayesian structural time series model.

Prophet is a forecasting procedure implemented in R and Python, It is fast and provides completely automated forecasts that can be tuned by hand by data scientists and analysts.

Overview

Forecasting is a common data science task that helps organizations with capacity planning, goal setting, and anomaly detection.

Despite its importance, there are serious challenges associated with producing reliable and high-quality forecasts — especially when there are a variety of time series and analysts with expertise in time series modeling are relatively rare.

To address these challenges, we describe a practical approach to forecasting “at scale” that combines configurable models with analyst-in-loop performance analysis.

A modular regression model with interpretable parameters that can be intuitively adjusted by analysts with domain knowledge about the time series.

The performance analyses compare and evaluate forecasting procedures, and automatically flag forecasts for manual review and adjustment.

Prophet is a procedure for forecasting time series data based on an additive model where non-linear trends are fit with yearly, weekly, and daily seasonality, plus holiday effects.

It works best with time series that have strong seasonal effects and several seasons of historical data.

Prophet is robust to missing data and shifts in the trend, and typically handles outliers well.

Prophet is open-source software released by Facebook’s core Data Science team.

How Prophet Works

Prophet is especially useful for datasets that:

  • Contain an extended time period(month or years) of detailed historical observations(hourly, daily, or weekly)
  • Have multiple strong seasonalities
  • Include previously known important, but irregular, events
  • Have missing data points or large outliers
  • Have non-linear growth trends that are approaching a limit.

Prophet is an additive regression model with a piecewise linear or logistic growth curve trend. It includes a yearly seasonal component modeled using Fourier series and a weekly seasonal component modeled using dummy variables.

Accurate and fast.

Prophet is used in many applications across Facebook for producing reliable forecasts for planning and goal setting. the prophet is found to perform better than any other approach in the majority of cases. the model fits in Stan so that you get forecasts in just a few seconds.

Stan is a state-of-the-art platform for statistical modeling and high-performance statistical computation.

Fully automatic

Get a reasonable forecast on messy data with no manual effort. Prophet is robust to outliers, missing data, and dramatic changes in your time series.

Tunable forecasts

The prophet procedure includes many possibilities for users to tweak and adjust forecasts. You can use human-interpretable parameters to improve your forecast by adding your domain knowledge.

Available in R or Python

We can implement the prophet procedure in R and Python, but they share the same underlying Stan code for fitting. Use whatever language you’re comfortable with to get forecasts.

Python Implementation

# Python
import pandas as pd
from prophet import Prophet
df = pd.read_csv('../examples/example_wp_log_peyton_manning.csv')
df.head()

We fit the model by instantiating a new Prophet object. Any settings to the forecasting procedure are passed into the constructor. Then you call its fit method and pass in the historical data frame. Fitting should take 1-5 seconds.

m = Prophet()
m.fit(df)

Predictions are then made on a data frame with a column ds containing the dates for which a prediction is to be made. You can get a suitable data frame that extends into the future a specified number of days using the helper method Prophet.make_future_dataframe. By default it will also include the dates from the history, so we will see the model fit as well.

future = m.make_future_dataframe(periods=365)
future.tail()

The predict the method will assign each row in future a predicted value which it names yhat. If you pass in historical dates, it will provide an in-sample fit. The forecast the object here is a new data frame that includes a column yhat with the forecast, as well as columns for components and uncertainty intervals.

forecast = m.predict(future)
forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail()

You can plot the forecast by calling the Prophet.plot method and passing in your forecast data frame.

fig1 = m.plot(forecast)

If you want to see the forecast components, you can use the Prophet.plot_components method. By default, you’ll see the trend, yearly seasonality, and weekly seasonality of the time series. If you include holidays, you’ll see those here, too.

fig2 = m.plot_components(forecast)

An interactive figure of the forecast and components can be created with Plotly. You will need to install Plotly 4.0 or above separately, as it will not by default be installed with the prophet. You will also need to install the notebook and ipywidgets packages.

from prophet.plot import plot_plotly, plot_components_plotly

plot_plotly(m, forecast)
plot_components_plotly(m, forecast)

References

If you learned something new or enjoyed reading this article, please clap it up 👏 and share it so that others will see it. Feel free to leave a comment too.💬

--

--