Book Image

Forecasting Time Series Data with Prophet - Second Edition

By : Greg Rafferty
5 (1)
Book Image

Forecasting Time Series Data with Prophet - Second Edition

5 (1)
By: Greg Rafferty

Overview of this book

Forecasting Time Series Data with Prophet will help you to implement Prophet's cutting-edge forecasting techniques to model future data with high accuracy using only a few lines of code. This second edition has been fully revised with every update to the Prophet package since the first edition was published two years ago. An entirely new chapter is also included, diving into the mathematical equations behind Prophet's models. Additionally, the book contains new sections on forecasting during shocks such as COVID, creating custom trend modes from scratch, and a discussion of recent developments in the open-source forecasting community. You'll cover advanced features such as visualizing forecasts, adding holidays and trend changepoints, and handling outliers. You'll use the Fourier series to model seasonality, learn how to choose between an additive and multiplicative model, and understand when to modify each model parameter. Later, you'll see how to optimize more complicated models with hyperparameter tuning and by adding additional regressors to the model. Finally, you'll learn how to run diagnostics to evaluate the performance of your models in production. By the end of this book, you'll be able to take a raw time series dataset and build advanced and accurate forecasting models with concise, understandable, and repeatable code.
Table of Contents (20 chapters)
1
Part 1: Getting Started with Prophet
5
Part 2: Seasonality, Tuning, and Advanced Features
14
Part 3: Diagnostics and Evaluation

Prophet

Prophet was developed internally at Facebook (now known as Meta) by Sean J. Taylor and Ben Letham in order to overcome two issues often encountered with other forecasting methodologies: the more automatic forecasting tools available tended to be too inflexible and unable to accommodate additional assumptions, and the more robust forecasting tools required an experienced analyst with specialized data science skills. Facebook experienced too much demand for high-quality business forecasts than their analysts were able to provide. In 2017, Facebook released Prophet to the public as open source software.

Prophet was designed to optimally handle business forecasting tasks, which typically feature any of these attributes:

  • Time series data captured at the hourly, daily, or weekly level with ideally at least a full year of historical data
  • Strong seasonality effects occurring daily, weekly, and/or yearly
  • Holidays and other special one-time events that don’t necessarily follow the seasonality patterns but occur irregularly
  • Missing data and outliers
  • Significant trend changes that may occur with the launch of new features or products, for example
  • Trends that asymptotically approach an upper or lower bound

Out of the box, Prophet typically produces very high-quality forecasts, but it is also very customizable and approachable for data analysts with no prior expertise in time series data. As you’ll see in later chapters, tuning a Prophet model is very intuitive.

Essentially, Prophet is an additive regression model. This means that the model is simply the sum of several (optional) components, such as the following:

  • A linear or logistic growth trend curve
  • An annual seasonality curve
  • A weekly seasonality curve
  • A daily seasonality curve
  • Holidays and other special events
  • Additional user-specified seasonality curves, such as hourly or quarterly, for example

To take a concrete example, let’s say we are modeling the sales of a small online retail store over 4 years, from January 1, 2000, to the end of 2003. We observe that the overall trend is constantly increasing over time from 1,000 sales per day to around 1,800 at the end of the time period. We also see that sales in spring are about 50 units above average and sales in autumn are about 50 units below average. Weekly, sales tend to be lowest on Tuesday and increase throughout the week, peaking on Saturday. Finally, throughout the hours of the day, sales peak at noon and smoothly fall to their lowest at midnight. This is what those individual curves would look like (note the different x-axis scales on each chart):

Figure 1.4 – Model components

Figure 1.4 – Model components

An additive model would take those four curves and simply add them to each other to arrive at the final model for sales throughout the years. The final curve gets more and more complex as the sub-components are added up:

Figure 1.5 – Additive model

Figure 1.5 – Additive model

This preceding plot displays just the first year to see the weekly and daily variations better, but the full curve extends for 4 years.

Under the hood, Prophet is written in Stan, a probabilistic programming language (see the home page at https://mc-stan.org/ for more information about Stan). This has several advantages. It allows Prophet to optimize the fit process so that it typically completes in under a second. Stan is also compatible with both Python and R, so the Prophet team is able to share the same core fitting procedure between both language implementations. Also, by using Bayesian statistics, Stan allows Prophet to create uncertainty intervals for future predictions to add a data-driven estimate of forecasting risk.

Prophet manages to achieve typical results just as well as more complicated forecasting techniques but with just a fraction of the effort. It has something for everyone. A beginner can build a highly accurate model in just a few lines of code without necessarily understanding the details of how everything works, while an expert can dig deep into the model, adding more features and tweaking hyperparameters to eke out incrementally better performance.