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 .
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 |
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.
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 )
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
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)
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.
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.
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.
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.
ds | predicted | actual |
1990-01-01 | 15.423458 | 14.8 |
1990-01-02 | 14.917351 | 13.3 |
1990-01-03 | 15.005665 | 15.6 |
1990-01-04 | 15.426364 | 14.5 |
1990-01-05 | 15.100560 | 14.3 |
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.
count | mean | std | min | 25% | 50% | 75% | max | |
predicted | 365 | 11.748838 | 3.244034 | 5.815357 | 8.927736 | 11.398311 | 14.793049 | 17.803684 |
actual | 365 | 11.669589 | 3.8616 | 2.1 | 8.7 | 11.4 | 14.6 | 22.1 |
residual | 365 | -0.079249 | 2.601818 | -6.945164 | -1.560951 | -0.207595 | 1.48082 | 7.463925 |
The maximum residual is 7.4 and minimum is -6.9.
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’
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.
horizon | mse | rmse | mae | mape | coverage |
36 days | 5.732601 | 2.394285 | 1.719826 | 0.117167 | 0.861111 |
37 days | 6.108647 | 2.471568 | 1.806564 | 0.124058 | 0.833333 |
38 days | 6.468656 | 2.543355 | 1.87154 | 0.128882 | 0.833333 |
39 days | 6.45858 | 2.541374 | 1.857532 | 0.127981 | 0.833333 |
40 days | 6.443046 | 2.538316 | 1.846613 | 0.12713 | 0.833333 |
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.
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/
Subscribe To Our Newsletter
Join our mailing list to receive the latest news and updates from our team.
You have Successfully Subscribed!