Time Series Forecasting and Facebook’s Prophet

Amay B
CoderHack.com
Published in
4 min readSep 14, 2023
Photo by Aron Visuals on Unsplash

Time series forecasting is the use of a model to predict future values based on previously observed values. It is useful for understanding the underlying dynamics of a time series and predicting future trends. Facebook’s Prophet is an open-source library for time series modeling. It is optimized for business metrics, short time series, and handling missing data, outliers, and seasonality.

Prophet uses an additive model with several components:

  • Trend: A curved trend line representing long term changes.
  • Yearly Seasonality: Seasonal changes that repeat yearly.
  • Weekly Seasonality: Seasonal changes that repeat weekly.
  • Holidays: User-defined holidays that can influence the time series.

Prophet works best with time series that have strong seasonal effects and several seasons of historical data. It can be installed with pip:

pip install fbprophet

We can import it into Python and instantiate a model:

from fbprophet import Prophet
model = Prophet()

Here’s an example of fitting a model and making forecasts:

df = pd.read_csv('example_wp_log_peyton_manning.csv')
df['y'] = np.log(df['wp'])
model.fit(df)  future = model.make_future_dataframe(periods=365, freq='D')
forecast = model.predict(future)

Model Components

The Prophet model has four main components:

Trend — The trend component models long-term changes using an order-2 polynomial:

growth ~ trend + trend * t + 0.5 * trend * t^2

Seasonality — Prophet models yearly, weekly, and daily seasonality by including Fourier series terms in the regression model. For example, the yearly seasonality is modeled with 12 months:

seasonality_year ~ sin(2*π*k/12 * t) + cos(2*π*k/12 * t)    for k in 0, ..., 11

You can control whether or not each type of seasonality is included:

# Add yearly seasonality 
model.add_seasonality(name='yearly', period=365.25, fourier_order=5)
# Add weekly seasonality
model.add_seasonality(name='weekly', period=7, fourier_order=3)
# Add daily seasonality
model.add_seasonality(name='daily', period=1, fourier_order=4)

Holidays — Holidays can be added to the model to capture day-specific effects (e.g. Super Bowl Sunday). They are modeled using dummy variables:

model.add_holidays(['super_bowl_sunday'])

Cross Validation

We can perform cross validation on our Prophet model to evaluate its accuracy on new data using .cross_validation():

cross_validation = model.cross_validation(initial='180 days', period='180 days', horizon ='180 days')

This will split the data into training and testing windows, fit the model on the training set and make predictions on the testing set. We can evaluate metrics like MAPE, MSE, MAE on the cross validation results:

from sklearn.metrics import mean_absolute_percentage_error
mapes = cross_validation['mape']
mean_mape = np.mean(mapes)
print(f'Mean MAPE: {mean_mape}')

Tuning and Making Forecasts

Some key hyperparameters we can tune in Prophet are:

  • changepoints: A list of dates where the trend changes. If not specified, Prophet will automatically detect changes.
  • n_changepoints: The number of potential trend changes to explore. A larger number will explore more possible changepoints.
  • interval_width: Width of the uncertainty intervals provided for the forecasts. Wider intervals will capture more uncertainty.

We can tune these and see how the forecasts and accuracy metrics change. Once we have tuned the model, we can make future forecasts with .predict():

future = model.make_future_dataframe(periods=365)  
forecast = model.predict(future)

The forecast will contain the forecast predictions as well as uncertainty intervals:

  • yhat: The predicted values.
  • yhat_lower and yhat_upper: The uncertainty interval around the forecast.

Advanced Usage

Prophet is capable of handling outliers, missing data, including covariates, multi-horizon forecasts and modeling multiple time series.

Missing data and outliers — Prophet is robust to missing data and outliers in the time series. It will ignore missing datapoints and outliers when fitting.

Covariates — We can include additional regressors, called “covariates”, to help with the predictions. For example, if a time series is closely related to price, we could add price as a covariate.

Multi-horizon Forecasts — To make predictions multiple steps ahead (e.g. predicting 10 days and 20 days ahead), we can call .predict_uncertainty() instead of .predict():

forecasts = model.predict_uncertainty(future, uncertainty_samples=100)
ten_day_forecast = forecasts['340':'350']  # 10 days ahead
twenty_day_forecast = forecasts['360':'380'] # 20 days ahead

Multiple Time Series — We can pass a DataFrame with multiple columns, each representing a separate time series, to model and forecast them jointly.

Conclusion

Prophet is an open-source time series forecasting tool optimized for business metrics and handling various components like trends, seasonality, and holidays. Some advantages of Prophet are:

  • It’s open source and developed by Facebook for their internal metrics forecasting.
  • Requires only a few lines of code to get started.
  • Handles missing data and outliers automatically.
  • Captures seasonal trends in the data.
  • Uncertainty intervals for forecasts.
  • Actively developed and maintained.

However, Prophet may not work as well for time series with multiple strong seasonalities or with very long historical data. It is limited to univariate and bivariate time series. Overall, Prophet offers a simple yet robust solution for time series modeling and can yield good results with minimal tuning required.

--

--