← Back to Model Library

Prophet Model

A Forecasting Procedure by Facebook

Overview

Prophet is an open-source forecasting procedure developed by Facebook. It is designed for forecasting time series data with strong seasonal effects and several seasons of historical data. Prophet is particularly robust to missing data, shifts in the trend, and large outliers, making it a popular choice for business forecasting where such irregularities are common.

Architecture & Components

Prophet uses an additive regression model with four main components:

$ y(t) = g(t) + s(t) + h(t) + \epsilon_t $

  • $g(t)$ - Trend Component: Models non-linear trends with changepoints. Prophet automatically detects these changepoints (points where the trend rate changes) from the data. It uses a piecewise linear or logistic growth model.
  • $s(t)$ - Seasonality Component: Models periodic changes. Prophet uses Fourier series to model yearly, weekly, and daily seasonality. You can specify the periods (e.g., 7 for weekly, 365.25 for yearly) and the order of the Fourier series.
  • $h(t)$ - Holiday Component: Models the impact of holidays and special events. You can provide a custom list of holidays with their start and end dates, and Prophet will estimate their impact.
  • $\epsilon_t$ - Error Term: Represents the irreducible noise or residual fluctuations not explained by the model. This is assumed to be normally distributed.

Prophet's strength lies in its modularity and interpretability, allowing users to inspect and adjust each component separately.

When to Use Prophet

Prophet is an excellent choice for:

  • Business forecasting scenarios with daily, weekly, or yearly seasonality.
  • Time series data that might have missing values or outliers.
  • Data where trend changes (changepoints) are expected or observed.
  • Users who prefer an intuitive and easily configurable model without deep statistical knowledge.
  • When you need to incorporate custom holidays or events into your forecast.

Pros and Cons

Pros

  • Handles Seasonality & Holidays: Excellent at capturing complex seasonal patterns and incorporating holiday effects.
  • Robust to Missing Data & Outliers: Can produce good forecasts even with gaps in the data or extreme values.
  • Fast & Easy to Use: Designed for automation and ease of use, requiring minimal feature engineering.
  • Interpretable Components: The trend, seasonality, and holiday components can be easily visualized and understood.
  • Automatic Changepoint Detection: Automatically identifies shifts in the trend, which is a significant advantage.

Cons

  • Less Flexible for Complex Non-Linearity: May not perform as well on time series with highly complex, non-linear patterns not related to time (e.g., chaotic systems).
  • "Black Box" for Non-Experts: While components are interpretable, the underlying statistical mechanics can still be a "black box" for users without a background in generalized additive models.
  • Limited for Short Series: Requires several seasons of historical data for accurate seasonal modeling.
  • No Uncertainty in Components: While it provides prediction intervals for the total forecast, it doesn't provide uncertainty estimates for individual components.

Example Implementation

Here's how to implement Prophet in Python. The library expects the time series data to be in a Pandas DataFrame with two columns: `ds` (datestamp) and `y` (value).


# Import necessary libraries
import pandas as pd
import numpy as np
from prophet import Prophet

# 1. Prepare sample data
# Create a date range for 2 years (730 days)
dates = pd.date_range(start='2020-01-01', periods=730, freq='D')
# Generate a series with trend, yearly seasonality, and some noise
values = (100 + np.arange(730) * 0.2 + # Trend
          50 * np.sin(np.arange(730) * 2 * np.pi / 365) + # Yearly seasonality
          np.random.normal(0, 5, 730)) # Noise

df = pd.DataFrame({'ds': dates, 'y': values})

# 2. Initialize and fit the Prophet model
# You can add parameters like yearly_seasonality=True, weekly_seasonality=True, etc.
m = Prophet(
    yearly_seasonality=True,
    weekly_seasonality=True,
    daily_seasonality=False # Set to True if daily patterns are expected
)

# Optional: Add custom holidays
# holidays = pd.DataFrame({
#     'holiday': 'christmas',
#     'ds': pd.to_datetime(['2020-12-25', '2021-12-25']),
#     'lower_window': 0,
#     'upper_window': 1,
# })
# m.add_country_holidays(country_name='US') # Example for US holidays
# m.add_seasonality(name='monthly', period=30.5, fourier_order=5) # Example for monthly seasonality

m.fit(df)

# 3. Create a future DataFrame for predictions
# Make future dataframe for the next 30 days
future = m.make_future_dataframe(periods=30)

# 4. Make predictions
forecast = m.predict(future)

# 5. Display the forecast (conceptual, actual plotting requires matplotlib setup)
print("Prophet Forecast (first 5 and last 5 rows):")
print(forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].head())
print(forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail())

# You can also plot the components
# fig = m.plot_components(forecast)
# plt.show()
                        

Dependencies & Resources

Dependencies: pandas, numpy, prophet, matplotlib (for plotting).