Forecasting Time Series Data with Prophet - Greg Rafferty - E-Book

Forecasting Time Series Data with Prophet E-Book

Greg Rafferty

0,0
32,39 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

Create and improve fully automated forecasts for time series data with strong seasonal effects, holidays, and additional regressors using Python

Purchase of the print or Kindle book includes a free PDF eBook

Key Features

  • Explore Prophet, the open source forecasting tool developed at Meta, to improve your forecasts
  • Create a forecast and run diagnostics to understand forecast quality
  • Fine-tune models to achieve high performance and report this performance with concrete statistics

Book Description

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.

What you will learn

  • Understand the mathematics behind Prophet’s models
  • Build practical forecasting models from real datasets using Python
  • Understand the different modes of growth that time series often exhibit
  • Discover how to identify and deal with outliers in time series data
  • Find out how to control uncertainty intervals to provide percent confidence in your forecasts
  • Productionalize your Prophet models to scale your work faster and more efficiently

Who this book is for

This book is for business managers, data scientists, data analysts, machine learning engineers, and software engineers who want to build time-series forecasts in Python or R. To get the most out of this book, you should have a basic understanding of time series data and be able to differentiate it from other types of data. Basic knowledge of forecasting techniques is a plus.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB

Seitenzahl: 297

Veröffentlichungsjahr: 2023

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Forecasting Time Series Data with Prophet

Build, improve, and optimize time series forecasting models using Meta's advanced forecasting tool

Greg Rafferty

BIRMINGHAM—MUMBAI

Forecasting Time Series Data with Prophet

Copyright © 2023 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

Publishing Product Manager: Aditya Datar

Content Development Editor: Shreya Moharir

Technical Editor: Sweety Pagaria

Copy Editor: Safis Editing

Project Coordinator: Farheen Fathima

Proofreader: Safis Editing

Indexer: Tejal Daruwale Soni

Production Designer: Joshua Misquitta

Marketing Coordinators: Shifa Ansari, Vinishka Kalra

First published: March 2021

Second published: April 2023

Production reference: 1230323

Published by Packt Publishing Ltd.

Livery Place

35 Livery Street

Birmingham

B3 2PB, UK.

ISBN 978-1-83763-041-7

www.packtpub.com

Contributors

About the author

Greg Rafferty is a data scientist at Google in San Francisco, California. With over a decade of experience, he has worked with many of the top firms in tech, including Facebook (Meta) and IBM. Greg has been an instructor in business analytics on Coursera and has led face-to-face workshops with industry professionals in data science and analytics. With both an MBA and a degree in engineering, he is able to work across the spectrum of data science and communicate with both technical experts and non-technical consumers of data alike.

About the reviewers

Jose Angel Sanchez, born and raised in Oaxaca, Mexico, is a software developer at Pinterest. Previously, Jose worked at Bayer, Credijusto, and Connus International, and throughout his career has had the opportunity to work with different technologies and solve problems in different disciplines. A math lover and a crypto-believer, he knows that only through science and skeptical thinking will the human race achieve its true potential. Jose lives happily with his wife, Mariana, and their dog, Koly, in St Louis, Missouri.

Shuvojyoti Acharjee is a data engineer enthusiast holding a master’s degree in big data and technology from the University of Westminster, London. Shuvojyoti has worked in multiple industries including customer service, research analysis, and hospitality and currently works as a manager at Travelex. 

I would love to thank my friend, Rakhi Mondal, for helping me complete this book. She has been a great motivation for me and has always been there for me.

Table of Contents

Preface

Part 1: Getting Started with Prophet

1

The History and Development of Time Series Forecasting

Understanding time series forecasting

The problem with dependent data

Moving averages and exponential smoothing

ARIMA

ARCH/GARCH

Neural networks

Prophet

Recent developments

NeuralProphet

Google’s “robust time series forecasting at scale”

LinkedIn’s Silverkite/Greykite

Uber’s Orbit

Summary

2

Getting Started with Prophet

Technical requirements

Installing Prophet

Installation on macOS

Installation on Windows

Installation on Linux

Building a simple model in Prophet

Interpreting the forecast DataFrame

Understanding components plots

Summary

3

How Prophet Works

Technical requirements

Facebook’s motivation for building Prophet

Analyst-in-the-loop forecasting

The math behind Prophet

Linear growth

Logistic growth

Seasonality

Holidays

Summary

Part 2: Seasonality, Tuning, and Advanced Features

4

Handling Non-Daily Data

Technical requirements

Using monthly data

Using sub-daily data

Using data with regular gaps

Summary

5

Working with Seasonality

Technical requirements

Understanding additive versus multiplicative seasonality

Controlling seasonality with the Fourier order

Adding custom seasonalities

Adding conditional seasonalities

Regularizing seasonality

Global seasonality regularization

Local seasonality regularization

Summary

6

Forecasting Holiday Effects

Technical requirements

Adding default country holidays

Adding default state/province holidays

Creating custom holidays

Creating multi-day holidays

Regularizing holidays

Global holiday regularization

Individual holiday regularization

Summary

7

Controlling Growth Modes

Technical requirements

Applying linear growth

Understanding the logistic function

Saturating forecasts

Increasing logistic growth

Non-constant cap

Decreasing logistic growth

Applying flat growth

Creating a custom trend

Summary

8

Influencing Trend Changepoints

Technical requirements

Automatic trend changepoint detection

Default changepoint detection

Regularizing changepoints

Specifying custom changepoint locations

Summary

9

Including Additional Regressors

Technical requirements

Adding binary regressors

Adding continuous regressors

Interpreting the regressor coefficients

Summary

10

Accounting for Outliers and Special Events

Technical requirements

Correcting outliers that cause seasonality swings

Correcting outliers that cause wide uncertainty intervals

Detecting outliers automatically

Winsorizing

Standard deviation

The moving average

Error standard deviation

Modeling outliers as special events

Modeling shocks such as COVID-19 lockdowns

Summary

11

Managing Uncertainty Intervals

Technical requirements

Modeling uncertainty in trends

Modeling uncertainty in seasonality

Summary

Part 3: Diagnostics and Evaluation

12

Performing Cross-Validation

Technical requirements

Performing k-fold cross-validation

Performing forward-chaining cross-validation

Creating the Prophet cross-validation DataFrame

Parallelizing cross-validation

Summary

13

Evaluating Performance Metrics

Technical requirements

Understanding Prophet’s metrics

Mean squared error

Root mean squared error

Mean absolute error

Mean absolute percent error

Median absolute percent error

Symmetric mean absolute percent error

Coverage

Choosing the best metric

Creating a Prophet performance metrics DataFrame

Handling irregular cut-offs

Tuning hyperparameters with grid search

Summary

14

Productionalizing Prophet

Technical requirements

Saving a model

Updating a fitted model

Making interactive plots with Plotly

Plotly forecast plot

Plotly components plot

Plotly single component plot

Plotly seasonality plot

Summary

Index

Other Books You May Enjoy

Part 1: Getting Started with Prophet

The first part of this book will give you an understanding of the historical developments in time-series forecasting techniques that led to the inception of Prophet and will then guide you through the installation of the program. The section continues with a walk-through of a basic Prophet forecasting model and introduces the output that this kind of model produces. Part 1 closes with a description of the math Prophet uses to build its forecasts.

This section comprises the following chapters:

Chapter 1, The History and Development of Time Series ForecastingChapter 2, Getting Started with ProphetChapter 3, How Prophet Works

1

The History and Development of Time Series Forecasting

Prophet is a powerful tool for creating, visualizing, and optimizing your forecasts! With Prophet, you’ll be able to understand what factors will drive your future results, which will enable you to make more confident decisions. You accomplish these tasks and goals through an intuitive but very flexible programming interface that is designed for both the beginner and expert alike.

You don’t need a deep knowledge of the math or statistics behind time series forecasting techniques to leverage the power of Prophet, although if you do possess this knowledge, Prophet includes a rich feature set that allows you to deploy your experience to great effect. You’ll be working in a structured paradigm where each problem follows the same pattern, allowing you to spend less time figuring out how to optimize your forecasts and more time discovering key insights to supercharge your decisions.

This chapter introduces the foundational ideas behind time series forecasting and discusses some of the key model iterations that eventually led to the development of Prophet. In this chapter, you’ll learn what time series data is and why it must be handled differently than non-time series data, and then you’ll discover the most powerful innovations, of which Prophet is one of the latest. Specifically, we will cover the following topics:

Understanding time series forecastingMoving averages and exponential smoothingARIMAARCH/GARCHNeural networksProphetRecent developments

Understanding time series forecasting

A time series is a set of data collected sequentially over time. For example, think of any chart where the x axis is some measurement of time—anything from the number of stars in the universe since the Big Bang until today or the amount of energy released each nanosecond from a nuclear reaction. The data behind both is time series. The chart in the weather app on your phone showing the expected temperature for the next 7 days? That’s also the plot of a time series.

In this book, we are mostly concerned with events on the human scales of years, months, days, and hours, but all of this is time series data. Predicting future values is the act of forecasting.

Forecasting the weather has obviously been important to humans for millennia, particularly since the advent of agriculture. In fact, over 2,300 years ago, the Greek philosopher Aristotle wrote a treatise called Meteorology that contained a discussion of early weather forecasting. The very word forecast was coined by an English meteorologist in the 1850s, Robert FitzRoy, who achieved fame as the captain of the HMS Beagle during Charles Darwin’s pioneering voyage.

However, time series data is not unique to weather. The field of medicine adopted time series analysis techniques with the 1901 invention of the first practical electrocardiogram (ECG) by the Dutch physician Willem Einthoven. The ECG produces the familiar pattern of heartbeats we now see on the machine next to a patient’s bed in every medical drama.

Today, one of the most discussed fields of forecasting is economics. There are entire television channels dedicated to analyzing trends in the stock market. Governments use economic forecasting to advise central bank policy, politicians use economic forecasting to develop their platforms, and business leaders use economic forecasting to guide their decisions.

In this book, we will be forecasting topics as varied as carbon dioxide levels in the atmosphere, the number of riders on Chicago’s public bike share program, the growth of the wolf population in Yellowstone, the solar sunspot cycles, local rainfall, and even Instagram likes on some popular accounts.

The problem with dependent data

So, why does time series forecasting require its own unique approach? From a statistical perspective, you might see a scatter plot of time series with a relatively clear trend and attempt to fit a line using standard regression—the technique for fitting a straight line to data. The problem is that this violates the assumption of independence that linear regression demands.

To illustrate time series dependence with an example, let’s say that a gambler is rolling an unbiased die. I tell you that they just rolled a 2 and ask what the next value will be. This data is independent; previous rolls have no effect on future rolls, so knowing that the previous roll was a 2 does not provide any information about the next roll.

However, in a different situation, let’s say that I call you from an undisclosed location somewhere on Earth and ask you to guess the temperature at my location. Your best bet would be to guess some average global temperature for that day. Now, imagine that I tell you that yesterday’s temperature at my location was 90°F. That provides a great deal of information to you because you intuitively know that yesterday’s temperature and today’s temperature are linked in some way; they are not independent.

With time series data, you cannot randomly shuffle the order of data without disturbing the trends, within a reasonable margin of error. The order of the data matters; it is not independent. When data is dependent like this, a regression model can show statistical significance by random chance, even when there is no true correlation, much more often than your chosen confidence level would suggest.

Because high values tend to follow high values and low values tend to follow low values, a time series dataset is more likely to show more clusters of high or low values than would otherwise be present, and this, in turn, can lead to the appearance of more correlations than would otherwise be present.

The website Spurious Correlations by Tyler Vigen specializes in pointing out examples of seemingly significant, but utterly ridiculous, time series correlations. Here is one example:

Figure 1.1 – A spurious time series correlation (https://www.tylervigen.com/spurious-correlations)

Obviously, the number of people who drown in pools each year is completely independent of the number of films Nicolas Cage appears in. They simply have no effect on each other at all. However, by making the fallacy of treating time series data as if it were independent, Vigen has shown that by pure random chance, the two series of data do, in fact, correlate significantly. These types of random chances are much more likely to happen when ignoring dependence in time series data.

Now that you understand what exactly time series data is and what sets it apart from other datasets, let’s look at a few milestones in the development of models, from the earliest models up to Prophet.

Moving averages and exponential smoothing

Possibly the simplest form of forecasting is the moving average (MA). Often, an MA is used as a smoothing technique to find a straighter line through data with a lot of variation. Each data point is adjusted to the value of the average of n surrounding data points, with n being referred to as the window size. With a window size of 10, for example, we would adjust a data point to be the average of the 5 values before and the 5 values after. In a forecasting setting, the future values are calculated as the average of the n previous values, so again, with a window size of 10, this means the average of the 10 previous values.

The balancing act with an MA is that you want a large window size in order to smooth out the noise and capture the actual trend, but with a larger window size, your forecasts are going to lag the trend significantly as you reach back further and further to calculate the average. The idea behind exponential smoothing is to apply exponentially decreasing weights to the values being averaged over time, giving recent values more weight and older values less weight. This allows the forecast to be more reactive to changes while still ignoring a good deal of noise.

As you can see in the following plot of simulated data, the MA line exhibits much rougher behavior than the exponential smoothing line, but both lines still adjust to trend changes at the same time:

Figure 1.2 – MA versus exponential smoothing

Exponential smoothing originated in the 1950s with simple exponential smoothing, which does not allow for trends or seasonality. Charles Holt advanced the technique in 1957 to allow for a trend with what he called double exponential smoothing; and in collaboration with Peter Winters, Holt added seasonality support in 1960, in what is commonly called Holt-Winters exponential smoothing.

The downside to these methods of forecasting is that they can be slow to adjust to new trends and so forecasted values lag behind reality—they do not hold up well to longer forecasting timeframes, and there are many hyperparameters to tune, which can be a difficult and very time-consuming process.

ARIMA

In 1970, the mathematicians George Box and Gwilym Jenkins published Time Series: Forecasting and Control, which described what is now known as the Box-Jenkins model. This methodology took the idea of the MA further with the development of ARIMA. As a term, ARIMA is often used interchangeably with Box-Jenkins, although technically, Box-Jenkins refers to a method of parameter optimization for an ARIMA model.

ARIMA is an acronym that refers to three concepts: Autoregressive (AR), Integrated (I), and MA. We already understand the MA part. AR means that the model uses the dependent relationship between a data point and a certain number of lagged data points. That is, the model predicts upcoming values based on previous values. This is similar to predicting that it will be warm tomorrow because it’s been warm all week so far.

The integrated part means that instead of using any raw data point, the difference between that data point and a previous data point is used. Essentially, this means that we convert a series of values into a series of changes in values. Intuitively, this suggests that tomorrow will be more or less the same temperature as today because the temperature all week hasn’t varied too much.

Each of the AR, I, and MA components of an ARIMA model are explicitly specified as a parameter in the model. Traditionally, p is used for the number of lag observations to use, also known as the lag order. The number of times that a raw observation is differenced, or the degree of differencing, is known as d, and q represents the size of the MA window. Thus arises the standard notation for an ARIMA model of ARIMA(p, d, q), where p, d, and q are all non-negative integers.

A problem with ARIMA models is that they do not support seasonality, or data with repeating cycles, such as temperature rising in the day and falling at night or rising in summer and falling in winter. Seasonal ARIMA (SARIMA) was developed to overcome this drawback. Similar to the ARIMA notation, the notation for a SARIMA model is SARIMA(p, d, q)(P, D, Q)m, with P being the seasonal AR order, D the seasonal difference order, Q the seasonal MA order, and m the number of time steps for a single seasonal period.

You may also come across other variations of ARIMA models, including Vector ARIMA (VARIMA) for cases with multiple time series as vectors; Fractional ARIMA(FARIMA) or Autoregressive Fractionally Integrated Moving Average

PD: Style as P-Keyword (ARFIMA), both of which include a fractional differencing degree, allowing for long memory in the sense that observations far apart in time can have non-negligible dependencies; and SARIMAX, a seasonal ARIMA model where the X stands for exogenous or additional variables added to the model, such as adding a rain forecast to a temperature model.

ARIMA does typically exhibit very good results, but the downside is its complexity. Tuning and optimizing ARIMA models is often computationally expensive and successful results can depend upon the skill and experience of the forecaster. It is not a scalable process, but better suited to ad hoc analyses by skilled practitioners.

ARCH/GARCH

When the variance of a dataset is not constant over time, ARIMA models face problems with modeling it. In economics and finance, in particular, this is common. In a financial time series, large returns tend to be followed by large returns and small returns tend to be followed by small returns. The former is called high volatility, and the latter is low volatility.

Autoregressive Conditional Heteroscedasticity (ARCH) models were developed to solve this problem. Heteroscedasticity is a fancy way of saying that the variance or spread of the data is not constant throughout, with the opposite term being homoscedasticity. The difference is visualized here:

Figure 1.3 – Scedasticity

Robert Engle introduced the first ARCH model in 1982 by describing conditional variance as a function of previous values. For example, there is a lot more uncertainty about daytime electricity usage than there is about nighttime usage. In a model of electricity usage, then, we might assume that the daytime hours have a particular variance, and usage during the night would have a lower variance.

Tim Bollerslev and Stephen Taylor introduced a moving average component to the model in 1986 with their Generalized ARCH (GARCH) model. In the electricity example, the variance in usage was a function of the time of day, but perhaps the swings in volatility don’t necessarily occur at specific times of the day, and the swings themselves are random. This is when GARCH is useful.

Both ARCH and GARCH models can handle neither trend nor seasonality though, so often, in practice, an ARIMA model may be built first to extract out the seasonal variation and trend of a time series, and then an ARCH model may be used to model the expected variance.

Neural networks

A relatively recent development in time series forecasting is the use of Recurrent Neural Networks (RNNs). This was made possible with the development of the Long Short-Term Memory (LSTM) unit by Sepp Hochreiter and Jürgen Schmidhuber in 1997. Essentially, an LSTM unit allows a neural network to process a sequence of data, such as speech or video, instead of a single data point, such as an image.

A standard RNN is called recurrent because it has loops built into it, which is what gives it memory, that is, gives it access to previous information. A basic neural network can be trained to recognize an image of a pedestrian on a street by learning what a pedestrian looks like from previous images, but it cannot be trained to identify that a pedestrian in a video will soon be crossing the street based upon the pedestrian’s approach observed in previous frames of the video. It has no knowledge of the sequence of images that leads to the pedestrian stepping out into the road. Short-term memory is what the network needs temporarily to provide context, but that memory degrades quickly.

Early RNNs had a memory problem: it just wasn’t very long. In the sentence “airplanes fly in the …,” a simple RNN may be able to guess the next word will be sky, but with “I went to France for vacation last summer. That’s why I spent my spring learning to speak …,” it’s not so easy for the RNN to guess that French comes next; it understands that the word for a language should come next but has forgotten that the phrase started by mentioning France. An LSTM, though, has this necessary context. It gives the network’s short-term memory more longevity. In the case of time series data, where patterns can reoccur over long time scales, LSTMs can perform very well.

Time series forecasting with LSTMs is still in its infancy when compared to the other forecasting methods discussed here; however, it shows promise. One strong advantage over other forecasting techniques is the ability of neural networks to capture non-linear relationships, but as with any deep learning problem, LSTM forecasting requires a great deal of data and computing power and a long processing time.

Additionally, there are many decisions to be made regarding the architecture of the model and the hyperparameters to be used, which necessitate a very experienced forecaster. In most practical problems, where budget and deadlines must be considered, an ARIMA model is often thebetter choice.

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.