This blog is a hands-on practical guide about Time Series Forecasting using Facebook’s Prophet in Python. This is a continuation of our last blog Overview of Facebook Prophet, where you can find a detailed walkthrough of Facebook’s Prophet. Please give it a read for a better understanding. Let’s get started.

The prophet is a dynamic time series forecasting tool with many features that allows forecasts to be accurate. It is available both in Python and R! The parameters in Prophet can be tuned to improve the quality of forecasts.

Prophet’s Installation

The first step is to install fbprophet library which can be installed using pip/conda command in Python .

Python

Once the Installation and Importing of Prophet is done , we can start building Prophet model for Time Series Forecasting.

Daily Minimum Temperature

The data used for Time Series Forecasting is Daily minimum temperature which describes the minimum temperature of over 10 years ( 1981 – 1990 ) consisting of about 3650 observations with two attributes ( date and temperature ) in a daily period (Daily Minimum Temperatures in Melbourne) and the unit of measurement is Celsius. 

The source of the data is credited as the Australian Bureau of Meteorology. 

Prophet model takes only two columns as input. The column name should be ‘ds‘ for date-stamp and ‘y‘ representing numeric values.

  Column ds in general can be

  • date format ( YYYY-MM-DD )
  • timestamp format ( YYYY-MM-DD HH:MM:SS )

Here is the sample of the dataset with columns renamed to ‘ds’ and ‘y’. 

ds y
1981-01-01 20.7
1981-01-02 17.9
1981-01-03 18.8
1981-01-04 14.6
1981-01-05 15.8
Daily Minimum Temperature
Python

Exploratory Data Analysis

         Exploratory data analysis ( E.D.A ) is a data understanding approach, an important phase in Machine Learning. This phase allows us to gain ideas regarding patterns in the data, noise and helps us detect outliers. We have our dataset imported, so lets quickly check if our data is suited for time series analysis.

Python
   Figure 1.1 : Visualization of temperature with respect to datestamp 

Here, we have defined our x-axis to be the ‘ds’ variable and y-axis to be ‘y’ variable.

The graph shown above is a line plot representing the temperature values from 1981 – 1990 on a daily basis. 

We can clearly visualize a seasonal component present in the data for the past 10 years. There was a rise in early months ( 1st Quarter ) and fall during mid – months ( 3rd Quarter ).

Building Prophet Model

We have explored the daily minimum temperature and got to know about the trends present with seasonal components. We will learn to how to build a time series forecasting model using Prophet.

Train Test Split

It is very important to have more data points in a training set which makes the model learn from different scenarios.  So, the period chosen for Training set is nine years data of minimum temperature which would be sufficient for a model to learn from the past. The test set would contain one year data which we our Prophet model will forecast.

Here, 

  • Train set range from January 1981 to December 1989 ( Nine years ). 
  • Test set covers one year of data points ( January 1990 – December 1990 )
Python

The number of observations present in training set is 3285 days and that of test set is 365  days.

Training Forecasting model using Prophet

Parameters in Prophet can be chosen based on the dataset and the patterns available in it. Tweaking these parameters will help us train our model for better results.

The data consists of a non-linear trend and a rise at the beginning and end of the month. Also a fall in the mid-months (July , August , September).

Having these analysis, we can proceed in building our Prophet Model.

  • growth = ‘logistic’ 
  • seasonality_mode = ‘multiplicative’ 
  • daily_seasonality = True

Custom seasonality 

We can specify the additional seasonality components with the required period and fourier order. 

  • add_seasonality(name = ‘quarterly, period = 90.5, fourier_order = 48) 
  • add_seasonality(name = ‘yearly’, period = 365, fourier_order = 48)

Since the growth parameter to logistic, we have to specify the maximum capacity ‘cap‘ and minimum capacity ‘floor‘. Two columns named ‘cap’ and ‘floor’ assigned to the 

  • training set before fitting the model 
  • future set before forecasting 
Python

Defining Future set

Future set is what we want our model to forecast with the trained Prophet model. One year time period , the test set (January 1990 – December 1990) is taken as a future set. 

  • m.make_data_frame(period=365)
Python

Forecasting the Future

Forecasting on the specified future dataframe can be done using the ‘predict’ function. This will make the forecasts with period and frequency mentioned. 

      Forecasting using Prophet will provide a wide range of results with upper limit and lower limit as end result. ‘yhat’ column consists of the predicted value.

Python

Visualizing Forecasts

Forecasts can be visualized using Prophet ‘plot’ method. 

    This will plot the actual data points and the performance of the model which captures the data trend.

Python
Figure 1.2 : Visualization of Forecasts

The above figure clearly helps us understand how Prophet captured the trends in the dataset and forecasts the given future set in few lines of code.

Component of Forecasts

The components of the forecasts such as ‘trend’ , ‘seasonality’ , ‘Impact of additional regressor’ can be visualized. 

    Prophet’s ‘plot_components’ method will provide the results.

Python
Figure 1.3 : Components of Forecasts

The above plot shows the seasonality components captured by Prophet model. Seasonality such as ‘daily’ and ‘weekly’ are default components in Prophet.

Additionally, we also have custom seasonal components ‘quarterly’ and ‘yearly’.

Finally, we have built time series forecasting model using Facebook’s Prophet. Now, we shall move to evaluate the forecasted results.

Model Evaluation

Observed values ( Actuals ) and Predicted values should be merged with respect to date for model performance and evaluation. 
This helps us to know if there are any possible tuning can be done to get improvised better  results.

Python
dspredictedactual
1990-01-0115.42345814.8
1990-01-0214.91735113.3
1990-01-0315.00566515.6
1990-01-0415.42636414.5
1990-01-0515.10056014.3
Dataset of Actual and Forecasted values
Python

  Figure 1.3 : Visualization of observed and forecasted values

The graph shown in the above figure clearly shows the plots of actual ( data points in red color ) and forecasted ( data points highlighted in blue). 

We are able to see the results closer to the observed values. Let us evaluate the performance of the model using metrics.

Residuals represent the difference between actual and predicted values.

Python
countmeanstdmin25%50%75% max
predicted36511.7488383.2440345.8153578.92773611.39831114.79304917.803684
actual36511.6695893.86162.18.711.414.622.1
residual365-0.0792492.601818-6.945164-1.560951-0.2075951.480827.463925
Descriptive Statistics of Actual, Predicted and its Residuals

 The maximum residual is 7.4 and minimum is -6.9.

Python

Mean squared error : 6.751129497118877

Mean absolute error : 2.0285695025255333

The error rate is lesser and this can be even reduced with adjusting and adding additional parameters.

Diagnostics – Cross Validation

Diagnostics functionality is also performed with this dataset to have a better understanding of overall Prophet’s functions. There is no need to specify the training set and testing set manually.

Features required for implementing the model are : 

  • Length of dataset = 3650 ( 1981 – 1990 ) 
  • The latest date ( ds )  in the dataframe ‘1990-12-31’ .

Suppose , we want one year to be predicted using Prophet. 

i.e., The first nine years will be a training sample and the last one year will be the forecast set. 

Therefore, Parameter values will be 

  • Initial value is set to ‘3285 days’ 
  • Period is set to be ‘365 days’ 
  • Horizon value is ‘365 days’ ( As we want prediction of one year)

Cutoff = latest date – horizon

Cutoff  = ‘1990-12-31’ - 365 days = '1989-12-31’

Prediction date ranges from cutoff and cutoff + horizon.

‘1989-12-31’ + 1 day to ( ‘1989-12-31’ + 1 + 365 days )
i.e.,  Forecasts set = ‘1990-01-01’ to ‘1990-12-31’
Python

    Units is the measurement of time based on the dataset. It can be ‘days’ or ‘hours’.

Performance Metrics

Performance of the model using cross validation can be evaluated using performance_metrics function. 

This will produce the error metrics for the number of days /  hours specified in horizon. The metrics include M.S.E , R.M.S.E , M.A.E and M.A.P.E.

Python
horizonmsermsemaemapecoverage
36 days5.7326012.3942851.7198260.1171670.861111
37 days6.1086472.4715681.8065640.1240580.833333
38 days6.4686562.5433551.871540.1288820.833333
39 days6.458582.5413741.8575320.1279810.833333
40 days6.4430462.5383161.8466130.127130.833333
Performance Metrics of specified period

The Performance metric will provide variables of forecasting metrics for all specified number of units in the period.

The same can be visualized through a plot using ‘plot_cross_validation_metric’  function with the metric specified.

Python

The plot consists of horizon ( number of days ) in the x-axis and the metric ( say metric = ‘mse’ ) in y-axis.

Diagnostics in Prophet can be implemented in an easier way. The model allows us to specify initial training samples, future days to be forecasted and evaluated with performance metrics.

Conclusion 

That’s it! we have implemented a Prophet Model to forecast minimum temperature. Time series forecasting is made simpler with Facebook’s Prophet. Without diving deep into the mathematics behind time series and no workaround experience, we were able to build forecasting models using Prophet.

With tunable parameters, our model results show a very less error rate and can be further improved with additional parameters. Plotting of forecasts and its components will guide us in improvising the model by adjusting and adding additional required parameters. Diagnostics functionality brought everything into a simpler mode of execution which provides performance metrics of the model.

A big thanks to all who read this blog. Let us know your thoughts in the comments section. Do Like, Share this blog if you find this post useful.

Download the code from the below link:

Reference Articles

Official page of Prophet – https://facebook.github.io/prophet/

An Introductory Study on Time Series ModelingDifferent dataset suited for Time Series Forecasting – https://arxiv.org/ftp/arxiv/papers/1302/1302.6613.pdf

Evaluation Metrics for Time Series – https://machinelearningmastery.com/time-series-forecasting-performance-measures-with-python/