Techno Blender
Digitally Yours.

Time Series Forecasting with Facebook’s Prophet in 10 Minutes

0 51


Part 1: Build a working model with 6 lines of code

Prophet’s output — Image by Author

The added value a time series forecasting model can bring to the decision making process in a business is undeniable. I recently built one which had such an impact that I thought I’d share the approach here.

When trying to make predictions about the future values of a target variable, there are 2 main categories of forecasting models:
Traditional Time Series Models and Machine Learning Models.

Time Series Models — Image by Author
  • With univariate time series models, the idea is to make predictions of future values based solely on trends and seasonality of past data of the target variable (the one we try to forecast) and nothing else. Multivariate models are an extension of that, where the input can be multiple time series.
  • With (supervised) machine learning models, the idea is to use other explanatory variables and model their relationship with the target variable to make predictions.

In this article, you guessed it, we will focus on the traditional approach, and we will try to answer a simple question: can we use the past values of a series to forecast the future ones QUICKLY? This question arises in countless areas: the stock market movements, the sales of a company, the emission of CO2 into the atmosphere, …

The thing is, quite often, to answer business questions, you are not after the very best model with an accuracy which would win Machine Learning challenges, but just after a good enough estimate which allows for a data driven decision.

This is where Facebook’s Prophet comes in. It is an off the shelf algorithm, easy to set up and giving decent results with very little effort.

Let’s see how to use it out of the box and get results in a matter of minutes.

For this quick tutorial, we will use a generated dataset representing the sales of a made-up company — The ABC Company (as you can probably tell, originality is not my strongest suit).

Quick disclaimer: this dataset is fictitious and is only used to illustrate the use of Prophet through an example that is well suited for Time Series Forecasting.

First, let’s import the necessary libraries to load and plot the data.

import pandas as pd
import datetime
import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator
from matplotlib.dates import MonthLocator, YearLocator, DateFormatter

df = pd.read_csv('sales_data.csv')
df['date'] = pd.to_datetime(df['date'])
df.head()

This is what our data frame looks like: the daily sales of the fictitious ABC Company:

Made up dataset — Image by Author

We can plot it to highlight two things of interest:

  • a clear uptrend year over year — the sales are growing over time
  • a seasonality within the year — the company sells more in the first half of the year than in the second
Sales over time of the well known ABC Company — Image by Author

Now, the goal of this entire little operation is to make predictions about future sales — from January 2023 onwards.

Because we are using Prophet, a univariate time series model, we only use the past sales figures (seasonality and trend) to predict the future sales figures.
Note that you could add additional time series as inputs to the model, essentially making Prophet a multivariate model.

Let’s see how this works.

If you’ve never used Prophet before you will need to install it using pip or conda and import it:

#pip install prophet
#OR
#conda install -c conda-forge prophet

from prophet import Prophet

Now let’s actually build the model and make predictions.
If you played around with Machine Learning models before, the next steps should look familiar:

  • Rename the columns to be called ‘ds’ and ‘y’. This is essential for Prophet to work
  • Create a Prophet instance and train / fit the model on past data
  • Use the model to make predictions on future data
# Rename the columns
df.columns = ['ds', 'y']

# Creating an instance of the Prophet class and training the model
m = Prophet() #instantiating a new Prophet object
model = m.fit(df) #build the model with the historical data

future = model.make_future_dataframe(periods=60, freq='D') #build the dataframe containing the predictions (and the historical data with the model fit)
forecast = model.predict(future) #The forecast object here is a new dataframe that includes a column yhat with the forecast

forecast[['ds', 'trend', 'weekly', 'yearly', 'yhat']].tail()

And this is what the forecast looks like:

Result of this first Prophet model out of the box — yhat is the prediction — Image by Author

Facebook’s Prophet is an additive regression model, where the final prediction (yhat) is the sum of all the components (trend and seasonalities) modeled separately.
yhat(t) = g(t) + s(t) + h(t) + e(t), where

  • g(t) = growth over time (trend)
  • s(t) = seasonalities (weekly, yearly, …)
  • h(t) = holidays (not yet modeled in our use case)
  • e(t) = white noise error term (not yet present in our use case)

It took a grand total of 6 lines of code to build a first working model.

There are two graphs which are interesting to look at to get a clearer picture of the results:

The first plot is the actual forecast over time:

plot1 = model.plot(forecast, uncertainty=True)
Prophet’s output — Image by Author

You can probably tell that there are 3 different elements on this graph:

  1. the black dots represent our actual past data points
  2. the blue line is the model which was fit through the data points and extrapolated into the future (on the last 60 days of the graph)
  3. the light blue space is the uncertainty interval

The second plot which is interesting to look at is the breakdown of the components of the model. This illustrates the trend and seasonalities models we mentioned just before.

plot2 = model.plot_components(forecast)
Prophet’s components — Image by Author

For now, in this very basic model, we only have 3 components:

  • The trend which is growing over time
  • The weekly seasonality which shows fewer sales on weekends
  • The yearly seasonality which clearly shows two periods of different sales volumes

We’ll see in part 2 that we can add more features to improve the performance of the model.

Note that a forecast, by nature, is uncertain but building this model can help identifying trends or planning for potential scenarios which unquestionably provide added value in the decision-making process.

Of course this is not a satisfying outcome (yet) as the quality of the forecast is unknown and it would be hard to make any sort of decisions based solely on what this basic “black box” model spat out.

However this is a great starting point. The next steps will consist of:

  • Assessing the model’s performance
  • Understanding the black box and the relevant hyper-parameters
  • Fine tune the model’s hyper-parameters to increase its performance

Stay tuned for part 2!

[1] Official Prophet documentation: https://facebook.github.io/prophet/

[2] Advanced Forecasting with Python: With State-of-the-Art-Models Including LSTMs, Facebook’s Prophet, and Amazon’s DeepAR by Joos Korstanje

[3] Practical Time Series Analysis: Prediction with Statistics and Machine Learning by Aileen Nielsen

[4] A useful website explaining Time Series Forecasting and give a quick look at Prophet’s equation: https://otexts.com/fpp3/prophet.html

Thanks for reading all the way to the end of the article.
Follow for more!
Feel free to leave a message below, or reach out to me through
LinkedIn if you have any questions / remarks!


Part 1: Build a working model with 6 lines of code

Prophet’s output — Image by Author

The added value a time series forecasting model can bring to the decision making process in a business is undeniable. I recently built one which had such an impact that I thought I’d share the approach here.

When trying to make predictions about the future values of a target variable, there are 2 main categories of forecasting models:
Traditional Time Series Models and Machine Learning Models.

Time Series Models — Image by Author
  • With univariate time series models, the idea is to make predictions of future values based solely on trends and seasonality of past data of the target variable (the one we try to forecast) and nothing else. Multivariate models are an extension of that, where the input can be multiple time series.
  • With (supervised) machine learning models, the idea is to use other explanatory variables and model their relationship with the target variable to make predictions.

In this article, you guessed it, we will focus on the traditional approach, and we will try to answer a simple question: can we use the past values of a series to forecast the future ones QUICKLY? This question arises in countless areas: the stock market movements, the sales of a company, the emission of CO2 into the atmosphere, …

The thing is, quite often, to answer business questions, you are not after the very best model with an accuracy which would win Machine Learning challenges, but just after a good enough estimate which allows for a data driven decision.

This is where Facebook’s Prophet comes in. It is an off the shelf algorithm, easy to set up and giving decent results with very little effort.

Let’s see how to use it out of the box and get results in a matter of minutes.

For this quick tutorial, we will use a generated dataset representing the sales of a made-up company — The ABC Company (as you can probably tell, originality is not my strongest suit).

Quick disclaimer: this dataset is fictitious and is only used to illustrate the use of Prophet through an example that is well suited for Time Series Forecasting.

First, let’s import the necessary libraries to load and plot the data.

import pandas as pd
import datetime
import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator
from matplotlib.dates import MonthLocator, YearLocator, DateFormatter

df = pd.read_csv('sales_data.csv')
df['date'] = pd.to_datetime(df['date'])
df.head()

This is what our data frame looks like: the daily sales of the fictitious ABC Company:

Made up dataset — Image by Author

We can plot it to highlight two things of interest:

  • a clear uptrend year over year — the sales are growing over time
  • a seasonality within the year — the company sells more in the first half of the year than in the second
Sales over time of the well known ABC Company — Image by Author

Now, the goal of this entire little operation is to make predictions about future sales — from January 2023 onwards.

Because we are using Prophet, a univariate time series model, we only use the past sales figures (seasonality and trend) to predict the future sales figures.
Note that you could add additional time series as inputs to the model, essentially making Prophet a multivariate model.

Let’s see how this works.

If you’ve never used Prophet before you will need to install it using pip or conda and import it:

#pip install prophet
#OR
#conda install -c conda-forge prophet

from prophet import Prophet

Now let’s actually build the model and make predictions.
If you played around with Machine Learning models before, the next steps should look familiar:

  • Rename the columns to be called ‘ds’ and ‘y’. This is essential for Prophet to work
  • Create a Prophet instance and train / fit the model on past data
  • Use the model to make predictions on future data
# Rename the columns
df.columns = ['ds', 'y']

# Creating an instance of the Prophet class and training the model
m = Prophet() #instantiating a new Prophet object
model = m.fit(df) #build the model with the historical data

future = model.make_future_dataframe(periods=60, freq='D') #build the dataframe containing the predictions (and the historical data with the model fit)
forecast = model.predict(future) #The forecast object here is a new dataframe that includes a column yhat with the forecast

forecast[['ds', 'trend', 'weekly', 'yearly', 'yhat']].tail()

And this is what the forecast looks like:

Result of this first Prophet model out of the box — yhat is the prediction — Image by Author

Facebook’s Prophet is an additive regression model, where the final prediction (yhat) is the sum of all the components (trend and seasonalities) modeled separately.
yhat(t) = g(t) + s(t) + h(t) + e(t), where

  • g(t) = growth over time (trend)
  • s(t) = seasonalities (weekly, yearly, …)
  • h(t) = holidays (not yet modeled in our use case)
  • e(t) = white noise error term (not yet present in our use case)

It took a grand total of 6 lines of code to build a first working model.

There are two graphs which are interesting to look at to get a clearer picture of the results:

The first plot is the actual forecast over time:

plot1 = model.plot(forecast, uncertainty=True)
Prophet’s output — Image by Author

You can probably tell that there are 3 different elements on this graph:

  1. the black dots represent our actual past data points
  2. the blue line is the model which was fit through the data points and extrapolated into the future (on the last 60 days of the graph)
  3. the light blue space is the uncertainty interval

The second plot which is interesting to look at is the breakdown of the components of the model. This illustrates the trend and seasonalities models we mentioned just before.

plot2 = model.plot_components(forecast)
Prophet’s components — Image by Author

For now, in this very basic model, we only have 3 components:

  • The trend which is growing over time
  • The weekly seasonality which shows fewer sales on weekends
  • The yearly seasonality which clearly shows two periods of different sales volumes

We’ll see in part 2 that we can add more features to improve the performance of the model.

Note that a forecast, by nature, is uncertain but building this model can help identifying trends or planning for potential scenarios which unquestionably provide added value in the decision-making process.

Of course this is not a satisfying outcome (yet) as the quality of the forecast is unknown and it would be hard to make any sort of decisions based solely on what this basic “black box” model spat out.

However this is a great starting point. The next steps will consist of:

  • Assessing the model’s performance
  • Understanding the black box and the relevant hyper-parameters
  • Fine tune the model’s hyper-parameters to increase its performance

Stay tuned for part 2!

[1] Official Prophet documentation: https://facebook.github.io/prophet/

[2] Advanced Forecasting with Python: With State-of-the-Art-Models Including LSTMs, Facebook’s Prophet, and Amazon’s DeepAR by Joos Korstanje

[3] Practical Time Series Analysis: Prediction with Statistics and Machine Learning by Aileen Nielsen

[4] A useful website explaining Time Series Forecasting and give a quick look at Prophet’s equation: https://otexts.com/fpp3/prophet.html

Thanks for reading all the way to the end of the article.
Follow for more!
Feel free to leave a message below, or reach out to me through
LinkedIn if you have any questions / remarks!

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Techno Blender is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.

Leave a comment