top of page
Search

Quick & Dirty time series forecasting using Facebook Prophet

  • Writer: Marco De Libero
    Marco De Libero
  • Apr 17
  • 3 min read

Updated: Apr 18

I’ve lost count of how many times I’ve been asked to make predictions on a wide range of exotic KPIs.

Before I was introduced to Facebook Prophet by two coworkers, I used to drown in indecipherable Excel files—full of unreadable nested IFs, broken formulas, #REF! errors, and more.

Prophet is an open-source library developed by Facebook for both Python and R, designed to forecast univariate time series data. Sounds fun, right?

It’s incredibly easy to use, and with just a bit of coding knowledge, you can build a prediction model in just a few lines—once the data is cleaned and in the right format.

No more reverse-engineering countless Excel sheets, fixing broken formulas, or redoing parts of the process from scratch. The days of dealing with that kind of operational complexity are long gone!


Where to chief?
Where to chief?

Once I imported the most common libraries—fbprophet, pandas, numpy, seaborn, matplotlib, and datetime—I created a time series filled with randomly generated numbers to play around with.


There are plenty of ways to create a normally distributed range of data with low variance. But I kept it simple—a range of random numbers between 100 and 125 does the trick.

After all, it’s the concept we’re focusing on. We’re not putting people on the moon here... yet. 😉

Some practical side notes I’ve learned (and wish I knew earlier):

  • The more historical data you have, the better.The model can capture more patterns and trends, making its predictions more reliable.

  • The further into the future you predict, the higher the margin of error you’ll need to accept.That’s just the nature of forecasting.

  • If C = A + B, then C_predicted ≠ A_predicted + B_predicted.Instead, the smartest approach is to predict A and B separately, then sum those predictions to get C_predicted.

Let’s roll!

First up: some good old-fashioned data cleaning. Outliers are replaced with the mean of the distribution (see the code block below).

There are plenty of sophisticated methods for detecting and correcting outliers, but the one I used here is based on the Interquartile Range (IQR) method.

Basically, any number greater than 1.5 times the IQR (the distance between the 1st and 3rd quartile) is considered an outlier.


Next step: setting up the model.

One of the powerful features of Prophet is its ability to capture seasonality (if you tell it to), factor in country-specific holidays, and make predictions within a defined minimum and maximum range, among other advanced capabilities.

The output of the model is an out-of-sample forecast—a list of estimated values labeled as yhat in the results.



Let’s plot the results and dive in:

  • Check the in-sample results to see how well the model fits the historical data. This step is crucial—it'll help us later when we want to quantify the prediction error.

  • Look at the out-of-sample forecast to see what the model thinks will happen in the future.

A quick visual inspection can already tell us a lot about how the model is performing.


I really like how the model projected the trendline—even though it didn’t perform particularly well around the spikes. That’s pretty common when you're working with unpredictable peaks.

Now, let’s assess the accuracy of our forecasting model so we can actually say how well it performed.

There are plenty of metrics out there—some more sophisticated than others—but my personal favorite is MAPE (Mean Absolute Percentage Error).

It tells us the average percentage error we make each time we generate a prediction. Simple, intuitive, and super helpful.


So, every time we make a prediction, we might be off by around 4.77%—which is actually pretty good, especially considering we’re working with just a couple of months of random data with unquantified variance.

You can find the full code on GitHub, and if you want to dig deeper, check out the official Facebook Prophet documentation here.


Bye for now!Marco

 
 
 

Comentarios


bottom of page