Getting the best out of Facebook Prophet

Akilendra Jayasumana
6 min readJan 6, 2021

When it comes to building time series models Facebook Prophet is a leading contender mostly due to its simplicity, ease of use and speed.

However these come at a cost, by selecting the default parameters all the time we may not get the best forecast results. There are numerous ways to improve forecasts generated by Prophet through means of adding holiday variables, custom seasonality, parameter tuning, adding additional regressors and using cross validation. In this post we will discuss on how to automate all these methods and techniques and use it in Prophet.

Facebook prophet in brief

Prophet is an open source procedure provided by Facebook for forecasting time series data, the library was made in such away that analysts would be able to give good insights even without much statistical expertise. Prophet is based on a decomposable time series model (generalized additive model) while taking trend g(t), seasonality s(t) and holidays h(t) and error term ϵₜ as its model components where t represents time. They can be represented in the following way

y(t) = g(t) + s(t) + h(t) + ϵₜ

Adding Holidays

Taking holidays and special days in to consideration are very important as they could have a prominent impact on the predictions. An example would be during a holiday season, where people tend to go more shopping, Prophet would be able to capture this trend better if holidays were taken into consideration.

When we use datasets coming in from multiple countries, it is important that we include holidays that are relevant only to that specific country. Using the built-in holiday feature Prophet is able to automatically include the holiday dates using the holiday package once the country code is provided.

The effect of holidays on the predictions can be adjusted using the holiday_prior_scale parameter.

Adding Custom Seasonality

A time series dataset could contain multiple seasonality. For example a daily school dataset which span across multiple years could have a 5 day school work week with effects that could repeat each week. The dataset could also contain certain school vacation holiday months which could create effects that will repeat each year. Prophet uses Fourier series to create a flexible model of periodic effects which can capture the multiple seasonality in the data.

Prophet uses a partial Fourier sum to estimate the daily, weekly and yearly seasonality and Prophet has given the option to create custom seasonality by changing the number of terms in the Fourier sum (Fourier Order) for each type of seasonality or even turning off a given seasonality. In short using a high Fourier order allows to fit in to more quick changing seasonality patterns. We should also keep in mind using a very high Fourier order value would lead to overfitting.

In the above example we were talking only about daily timeseries datasets but what happens when there are monthly and weekly datasets. The seasonality period will not be the same as daily for these datasets therefore it is important to adjust the custom seasonality with relevant period and Fourier_order.

Adding Regressors

When we need to fit and predict from Multivariate datasets in Prophet we have to specifically define the additional Predictor variable names that will need to be included in Prophet. We used Prophet’s add_regressor method to add our regressors.

If we take an example, say a Monthly Biscuits sales where we will have a Timeline, the Response(Sales) that we are trying to predict and additional Predictor variables like “Price” and “Advertisements”. In general we can say as price lowers and Advertisements increase there will be an increase in sales.

Dataset with Predictor variables

But we need to understand that things like Advertisements they take time to make an effect, say if we advertise on the month of April its effects will take place in May.

We can therefore create new predictor variables which will contain values of existing predictors that were lagged by n number of times, we call these lagged variables. We can thereafter remove correlated predictor variables and add the rest to the Prophet model as regressors.

Dataset with Predictor variables and Lagged variables

Parameter Tuning

Prophet has given the option of tuning multiple parameters to get the best out of Prophet.

In Prophet the effects of seasonality are assumed to be constant and its added to the trend by default, however there may be datasets where the seasonality could increase over time and change with trend(Multiplicative). In this cases we can try both ‘additive’ and ‘multiplicative’ for seasonality_mode parameter.

seasonality_prior_scale allows to control the flexibility of the seasonality that needs to fit, this may differ from dataset to dataset and it would be important to tune this parameter accordingly.

Prophets captures frequent and sudden changes in the direction of the data points known as changepoints in its algorithm. The strength of detecting theses changepoints(sparse prior) can be adjusted, incase these trend changes lead to overfit or underfit. The default value is set to 0.05 and increasing the value increases the flexibility of the trend and vice versa when decreasing the value. Therefore multiple values of changepoint_prior_scale can be used.

The growth trend varies according to different datasets. Prophet offers either a logistic growth or a linear growth, by default prophet uses the latter. Therefore, it would be better to try both ‘linear’ and ‘logistic’ when tuning the growth parameter.

After deciding which parameters needed to be tuned, a combination of all the parameters were generated. Each combination was iterated and a Prophet model was created using the parameter combination in each iteration. There after we calculated the RMSE for each model and selected the model with least RMSE.

Using Cross Validation

To further improve model selection we decided to see how Prophet Models performed on different dataset samples through Cross Validation and try to take them into consideration during model selection.

Since there is autocorrelation in a Timeseries dataset, the temporal order of data is crucial and we can’t carry out simple cross validation and mess it up by randomly splitting time series data at different points. Instead Time Series based Cross validation methods were examined. Some of them include:

  1. Time Series Split Cross-Validation
  2. Blocked Cross-Validation

For a more detailed explanation about these Time Series based Cross validation methods checkout cross-validation-in-time-series

Below you will find the steps we used to achieve Time Series Split Cross-Validation in our algorithm.

Hyperparameter tuning with cross validation

If you are going to use Prophet side by side with other time series based algorithms (either Classical or Machine learning) and planning to select the best model based on the RMSE which will be generated through Cross Validation. Make sure that all the other Time series algorithms use the same Cross validation method.

Prophet uses the Blocked Cross-Validation method as its default Cross Validation and if the other Time Series Models in the algorithm uses Time series Split Cross Validation, a custom Time series Split based cross validation method will be needed for Prophet.

In the next post we will try to compare how well Facebook Prophet performs against Classical Time series models.

References:

URL: https://facebook.github.io/prophet/docs/

URL: https://medium.com/@soumyachess1496/cross-validation-in-time-series-566ae4981ce4

--

--