Working in a SME with limited resources, the kind of sophisticated forecasting tools used by the major multinationals can seem far out of reach. For people in smaller companies like mine, the abundance of free to use, open-sourced, state of the art software like Facebook Prophet offers access to game-changing functionality.
The last couple of years have seen several major internet names open sourcing powerful predictive analytic API’s, making them free to use for developers and professionals. Google’s TensorFlow deep-learning library is probably the most widely used and influential library, but there are many more libraries that provide valuable functionality for Demand Planners and Purchasers that don’t require the kind of GPU computing power that deep-learning requires.
What Is Facebook Prophet?
Facebook describe the software as “a procedure for forecasting time series data. It is based on an additive model where non-linear trends are fit with yearly and weekly seasonality, plus holidays. It works best with daily periodicity data with at least one year of historical data. Prophet is robust to missing data, shifts in the trend, and large outliers.”
Prophet was designed to tackle the problem that quality forecasts are required faster than analysts can produce them, while automated forecasting techniques are too inflexible to incorporate useful assumptions or rules gleaned from experience. In a business context we’ve all seen automatically generated forecasts that don’t factor in change points in demand such as market breakout for a booming trend of the slump of a major customer moving to a competitor. At the same time, with thousands or tens of thousands of SKU’s to monitor, we know that finding highly skilled analysts to complete the workload consistently and rapidly can be a major challenge.
Why Use Facebook Prophet For Forecasting?
Firstly, Prophet is stupidly easy to use and generates reasonable results without having to worry about choosing between models and tuning hyperparameters.
Secondly, Prophets parameters allow for customization in ways that make sense to a non-expert and in a business context, such as the ability to inject S&OP information about how the forecast will likely change, the ability to set caps on possible demand based on experience and market knowledge, and the ability to model irregular holidays like Chinese New Year or Easter.
As a keen Pythonista, one of the best things for me about Prophet is that it can be used in Python and is easily installed from either pip or conda. Generally, R has had the edge over Python for time series regression problems. The auto.arima function in R is hard to beat for ease of use and accuracy of results. R also has some recent additions for dealing with time series problems – CausalImpact from Google which identifies the causal effects of things like marketing campaigns on sales, and AnomalyDetection/BreakoutDetection from Twitter, that help identify anomalies and shifts in trends. Facebook Prophet is therefore a very welcome addition to the Python ecosystem.
What is Facebook Prophet Optimized To Solve?
Prophet was designed by Facebook, so it’s well suited to regularly spaced timeseries observations and works best with at least a year’s observations to catch seasonal trends. It has a very useful function for incorporating national holidays, which depending on your business might represent peaks (television ratings during holidays) or dips (stores closed or open half-days on national holidays). It also has a useful parameter called a ‘changepoint’ which enables you to specify a point after which demand is likely to change, such as the launch of a competitor’s product or a major television campaign.
One important note is that Prophet is an additive regression model built up from trend, annual seasonality, weekly seasonality, and a user-specified holiday list. If you’re concerned that your model might be inherently multiplicative in nature, then it might be worth log-transforming the data and then using the inverse log to normalize the predictions.
I should also make clear from the start that as impressive as Prophet is, you can get better results by stacking the model in an ensemble of various techniques if you have the computing power to do so. On my laptop (and my work desktop), it would take several days to fit a very sophisticated ensemble model, whereas Prophet is able to do a reasonable job on all 3000 SKU’s for my current company in a matter of minutes, so there is a trade-off of accuracy against computational/time cost.
Installing Facebook Prophet in Python
Prophet can be installed very easily in Python, either through pip or through conda install. I used the conda installation which also loads all the dependencies and is very convenient: https://anaconda.org/conda-forge/fbprophet
Preparing Facebook’s Prophet Datasets
Prophet accepts the primary dataset of time series data and an optional list of holidays. I read these with this into Python with Pandas read_csv function passing parse_dates=True. If you’re unfamiliar with the Pandas commands, you can gain a quick understanding with the excellent 10 minutes to Pandas guide here: https://pandas.pydata.org/pandas-docs/stable/10min.html
The Prophet documentation shows that the variables for the primary dataset should be labelled ‘ds’ for the time series and ‘y’ for the variable. The holiday list should also be labelled ‘ds’ for the time series and ‘holiday’ for the list of notable events. Time series should be sorted and formatted as datetime datatype. This is easily done inside the workflow. I’ve take a single line of crystal glass tableware as an example having already sliced the dataframe down to one SKU:
Setting Up The Process
I tend to import a range of packages to perform basic exploratory data analysis, but the only essential packages for this will be pandas and fbprophet.
Here’s a quick plot of the time series:
Eyeballing the data, you should notice an increase in both frequency and volume of orders in a regular annual cycle with perhaps a faint downward trend over the three years.
Anyone familiar with sci-kit learns fit_predict/fit_transform methods will find Prophet follows a very similar pattern.
Here I instantiate the model with an uncertainty window of 95% (Prophet defaults to 80% even though 95% is normally standard in many business fields). I feed it my holiday list as a parameter, and then fit the model to my filtered dataframe (Ndf). I then project a future dataframe of around 3 months using Prophets ‘make_future_dataframe’ function.
Once the model is fit. All that remains to do is to predict the model over the future dates and have a look at the dataframe to insanity check the results.
Then using the model.plot(forecast) we can have a look at the fit and projected values:
As you can see the model has done an excellent job in finding the seasonal pattern and correctly identified the downward trend over the last three years. One of the best features of Prophet is that it will return the model components. Here we can see the overall trend and holidays have been isolated:
And the weekly and annual seasonality analysis is a very good fit for my experience in the table top HORECA trade, with annual demand peaking slightly at the start of summer and rising to a peak in the lead up to Christmas when parties fill up the hotels, restaurants and bars.
As you can also see, we close for weekends, so not so many invoices raised then. The busiest weekday is a Wendesday.
Final Thoughts On Facebook Prophet
With very little coding and without setting any of the numerous other hyperparameters, Prophet did an excellent job on the time series, despite the large number of outliers in the data, achieving a coefficient of determination of 0.84.
There are certainly many advantages for an SME purchaser or demand planner considering forecasting with Prophet:
The software is free to use, open-source code and is ridiculously easy to deploy.
Prophet is extremely quick, taking only a few seconds even on my now badly outdated laptop – more advanced neural networks are notorious for requiring multiple GPU’s or burning out the CPU’s of machine’s after several days running.
The optional hyperparameters as intuitive even to the less technically minded demand planner.
The predictions are returned with a confidence interval around the forecast, which can often be more useful that the predicted value itself when making decisions about stock levels.
All in all, R still has the edge when it comes to comprehensively tackling time series regression tasks, but if you’re a Pythonista working in demand planning and you want to upgrade your forecasting accuracy, then I’d strongly recommend Prophet as a tool to consider.