FaceBook Prophet for Time Series

Sailaja Karra
4 min readSep 29, 2020

Time series forecasting is a very common problem that can be easily solve with increasing accuracy using various python packages. In this blog I will run Facebook Prophet model on Airline timeseries to show the predictive power of this model.

What is FaceBook Prophet?

Facebook Prophet uses an elegant yet simple method for analyzing and predicting periodic data known as the additive modeling. The idea is straightforward: represent a time series as a combination of patterns at different scales such as daily, weekly, seasonally, and yearly, along with an overall trend. Your energy use might rise in the summer and decrease in the winter, but have an overall decreasing trend as you increase the energy efficiency of your home. An additive model can show us both patterns/trends and make predictions based on these observations.

“Prophet has been a key piece to improving Facebook’s ability to create a large number of trustworthy forecasts used for decision-making and even in product features.”

In order to compute its forecasts, the fbprophet library relies on the STAN programming language. Before installing fbprophet, you need to make sure that the pystan Python wrapper to STAN is installed. We shall first install pystan and fbprophet using pip install.

Installation of Prophet

Installation of Prophet in python can be easily done using the following.

!pip install pystan
!pip install fbprophet

Dataset: We use the Airlines dataset. This dataset is from kaggle and is available here. This is a simple time series data showing total number of airline passengers by month.

We then divide the dataset into test and training parts. We have a total of 144 ie 12 years worth of data, so i used 11 years ie 132 observations for training and the last 12 for testing.

Here is how we use the model to run the predictions.

Imports

Next we need to import some modules that we need to initialize our environment.

from fbprophet import Prophet
from fbprophet.diagnostics import cross_validation,
from fbprophet.diagnostics import performance_metrics
from fbprophet.plot import plot_cross_validation_metric

This how our data set looks like

fb=pd.read_csv('/content/drive/My Drive/airline_passengers.csv')

In order to run our FB model we have to rename ‘date’ column as ‘ds’ and ‘passenger’ column as ‘y’.This renaming is mandatory.

fb.columns=['ds','y']

We have to clean the data and make sure we have no missing data.I am dropping them.

fb.dropna(inplace=True)

Make sure the ‘ds’ column is in date time format

fb['ds']=pd.to_datetime(fb.ds)
fb.plot(x='ds',y='y')

We can see there is trend and seasonality in our data.

let's split the data into train and test to start running our model.

len(fb)
#144
train=fb[:132]
test=fb[132:]

Let's start our FB model….

m=Prophet(seasonality_mode='multiplicative')
m.fit(train)
future=m.make_future_dataframe(periods=12,freq='MS')
forecast=m.predict(future)
forecast.head()
  • yhat: the forecasted value of our metric (in Statistics, yhat is a notation traditionally used to represent the predicted values of a value y)
  • yhat_lower: the lower bound of our forecasts
  • yhat_upper: the upper bound of our forecasts

Prophet also provides a convenient method to quickly plot the results of our forecasts.

m.plot(forecast, uncertainty=True)
plt.show()

Prophet plots the observed values of time series(black dots),the forecasted values(blue lines) and the uncertainty intervals of our forecasts(blue shaded region)

m.plot(forecast)ax=forecast.plot(x='ds',y='yhat',legend=True,label='predictions',figsize=(12,8))test.plot(x='ds',y='y',legend=True,label='True Test Data',ax=ax,xlim=('1960-01-01','1961-01-01'))

As you can see from the above plot predictions and test values are almost going together.

One other feature of Prophet is its ability to return components of our forecasts.This can help reveal how daily, weekly and yearly patterns of the time series contribute to the overall forecasted values.

fig=m.plot_components(forecast)

Since we are working with monthly data,Prophet will plot the trend and the yearly seasonality.But if you are working with daily data, you would see a weekly seasonality plot included.

Performance Metrics:

Prophet includes its own diagnostics functionality in order to perform cross validation and run and evaluate the model on several sections of the actual data set.


# Initial training period.
initial= 2*365
initial= str(initial)+' days'
#Period length that we perform the cross validation for.
period= 2*365
period=str(period)+' days'
#Horizon of prediction essentially for each fold.
horizon = 365
horizon=str(horizon)+' days'
fb_cv=cross_validation(m,initial=initial,period=period,
horizon=horizon)
# Performance Metrics of fb_cv
performance_metrics(fb_cv)
plot_cross_validation_metric(fb_cv,'rmse');

now let’s see how we can use Prophet to track changes in the trend line.

# changing trend pointsfrom fbprophet.plot import add_changepoints_to_plot
fig=m.plot(forecast)
a=add_changepoints_to_plot(fig.gca(),m,forecast)

These red dotted line show the major points where trendline happens to change.

Happy reading!!!

--

--