Reader small image

You're reading from  Deep Learning for Time Series Cookbook

Product typeBook
Published inMar 2024
PublisherPackt
ISBN-139781805129233
Edition1st Edition
Right arrow
Authors (2):
Vitor Cerqueira
Vitor Cerqueira
author image
Vitor Cerqueira

​Vitor Cerqueira is a time series researcher with an extensive background in machine learning. Vitor obtained his Ph.D. degree in Software Engineering from the University of Porto in 2019. He is currently a Post-Doctoral researcher in Dalhousie University, Halifax, developing machine learning methods for time series forecasting. Vitor has co-authored several scientific articles that have been published in multiple high-impact research venues.
Read more about Vitor Cerqueira

Luís Roque
Luís Roque
author image
Luís Roque

Luís Roque, is the Founder and Partner of ZAAI, a company focused on AI product development, consultancy, and investment in AI startups. He also serves as the Vice President of Data & AI at Marley Spoon, leading teams across data science, data analytics, data product, data engineering, machine learning operations, and platforms. In addition, he holds the position of AI Advisor at CableLabs, where he contributes to integrating the broadband industry with AI technologies. Luís is also a Ph.D. Researcher in AI at the University of Porto's AI&CS lab and oversees the Data Science Master's program at Nuclio Digital School in Barcelona. Previously, he co-founded HUUB, where he served as CEO until its acquisition by Maersk.
Read more about Luís Roque

View More author details
Right arrow

Probabilistic Time Series Forecasting

In the preceding chapters, we delved into time series problems from a point forecasting perspective. Point forecasting models predict a single value. However, forecasts are inherently uncertain, so it makes sense to quantify the uncertainty around a prediction. This is the goal of probabilistic forecasting, which can be a valuable approach for better-informed decision-making.

In this chapter, we’ll focus on three types of probabilistic forecasting settings. We’ll delve into exceedance probability forecasting, which helps us estimate the likelihood of a time series surpassing a predefined threshold. We will also deal with prediction intervals, which provide a range of possible values within which a future observation is likely to fall. Finally, we will explore predicted probability forecasting, which offers a probabilistic assessment of individual outcomes, providing a fine-grained perspective of future possibilities.

This chapter...

Technical requirements

We’ll focus on the PyTorch ecosystem in this chapter. Here’s the full list of libraries that will be used in this chapter:

  • NumPy (1.26.2)
  • pandas (2.1.3)
  • scikit-learn (1.3.2)
  • PyTorch Forecasting (1.0.0)
  • PyTorch Lightning (2.1.2)
  • torch (2.1.1)
  • statsforecast (1.6.0)
  • GluonTS (0.14.2)
  • gpytorch (1.11)
  • prophet (1.1.5)

You can install these libraries using pip, Python’s package manager. For example, to install scikit-learn, you can run the following command:

pip install -U scikit-learn

The code for this chapter can be found in this book’s GitHub repository: https://github.com/PacktPublishing/Deep-Learning-for-Time-Series-Data-Cookbook.

Introduction to exceedance probability forecasting

This recipe introduces exceedance probability forecasting problems. Exceedance events occur when a time series exceeds a predefined threshold in a predefined future period. This problem is relevant when the tails of the time series distribution can have a significant impact on the domain. For example, consider the case of the inflation rate in the economy. Central banks leverage this type of forecast to assess the possibility that the inflation rate will exceed some critical threshold, above which they might consider increasing interest rates.

From a data science perspective, exceedance events are binary problems. Thus, it is common to tackle them using binary probabilistic classification models. One of the challenges is that the class representing the exceedance events is rare, which makes the learning task more difficult.

Getting ready

We’ll use a multivariate time series as an example to describe what an exceedance...

Exceedance probability forecasting with an LSTM

This recipe describes creating a probabilistic deep learning model to tackle exceedance tasks with a multivariate time series.

Getting ready

We’ll continue our example with the solar radiation dataset. Here’s the data module that we defined in the previous recipe:

N_LAGS = 14
HORIZON = 7
mvtseries = pd.read_csv('assets/daily_multivariate_timeseries.csv',
        parse_dates=['datetime'],
        index_col='datetime')
datamodule = ExceedanceDataModule(data=mvtseries,
        batch_size=64, test_size=0.3)

Now, let’s see how to create a classifier using an LSTM neural network and PyTorch’s LightningModule.

How to do it…

We will set up a binary classification using PyTorch Lightning’s LightningModule. Here’s the constructor and...

Creating prediction intervals using conformal prediction

In this recipe, we’ll explore how to create prediction intervals. Prediction intervals describe the range of values within which future observations will likely fall with some confidence level. The greater the confidence required, the larger the intervals will be.

In practice, the model predicts not just a single point but a distribution for future observations. Various techniques exist to construct these intervals, including parametric methods that assume a specific distribution of errors and non-parametric methods that use empirical data to estimate intervals.

We’ll resort to a conformal prediction approach, which is increasingly popular among data science practitioners.

Getting ready

We’ll build prediction intervals for an ARIMA model, which is a popular forecasting approach. Yet, conformal prediction is agnostic to the underlying method and can be applied to other forecasting methods.

Let...

Probabilistic forecasting with an LSTM

This recipe will walk you through building an LSTM neural network for probabilistic forecasting using PyTorch Lightning.

Getting ready

In this recipe, we’ll introduce probabilistic forecasting with LSTM networks. This approach combines the strengths of LSTM models in capturing long-term dependencies within sequential data with the nuanced perspective of probabilistic forecasting. This method goes beyond traditional point estimates by predicting a range of possible future outcomes, each accompanied by a probability. This means that we are incorporating uncertainty into forecasts.

This recipe uses the same dataset that we used in Chapter 4, in the Feedforward neural networks for multivariate time series forecasting recipe. We’ll also use the same data module we created in that recipe, which is called MultivariateSeriesDataModule.

Let’s explore how to use this data module to build an LSTM model for probabilistic forecasting...

Probabilistic forecasting with DeepAR

This time, we’ll turn our attention to DeepAR, a state-of-the-art method for probabilistic forecasting. We’ll also leverage the neuralforecast framework to exemplify how to apply DeepAR for this task.

Getting ready

We’ll continue with the same dataset that we used in the previous recipe.

Since we are using a different Python package, we need to change our preprocessing steps to get the data into a suitable format. Now, each row corresponds to a single observation at a given time for a specific time series. This is similar to what we did in the Prediction intervals using conformal prediction recipe:

def load_and_prepare_data(file_path, time_column, series_column, 
    aggregation_freq):
    """Load the time series data and prepare it for modeling."""
    dataset = pd.read_csv(file_path, parse_dates=[time_column])
  ...

Introduction to Gaussian Processes

In this recipe, we’ll introduce Gaussian Processes (GP), a powerful algorithm for probabilistic machine learning.

Getting ready

GP offers a flexible, probabilistic approach to modeling in machine learning. This section introduces the concept of GP and prepares the necessary environment for forecasting using a GP model.

We need to import a new library to be able to fit GP, namely gpytorch:

import torch
import gpytorch
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from pytorch_lightning import LightningDataModule

Then, we must read the multivariate time series data and process it, scaling both the features and target variable, as scaled data typically improves GP modeling performance significantly.

How to do it…

We’ll use the gpytorch library to implement a GP model:

  1. The key components in a GP model are the...

Using Prophet for probabilistic forecasting

In this recipe, we’ll show how to use Prophet for probabilistic forecasting.

Getting ready

Prophet is a tool developed by Facebook for forecasting time series data. It’s particularly adept at handling data with strong seasonal patterns and irregular events such as holidays. To get started with Prophet, we need to prepare our data and environment.

The process begins with loading and preprocessing the time series data so that it fits the format Prophet requires. Each time series in Prophet must have two columns – ds (the timestamp) and y (the value we wish to predict):

import pandas as pd
from prophet import Prophet
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
mvtseries = pd.read_csv(
"assets/daily_multivariate_timeseries.csv",
parse_dates=["datetime"],
)
mvtseries['ds'] = mvtseries['datetime...
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Deep Learning for Time Series Cookbook
Published in: Mar 2024Publisher: PacktISBN-13: 9781805129233
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (2)

author image
Vitor Cerqueira

​Vitor Cerqueira is a time series researcher with an extensive background in machine learning. Vitor obtained his Ph.D. degree in Software Engineering from the University of Porto in 2019. He is currently a Post-Doctoral researcher in Dalhousie University, Halifax, developing machine learning methods for time series forecasting. Vitor has co-authored several scientific articles that have been published in multiple high-impact research venues.
Read more about Vitor Cerqueira

author image
Luís Roque

Luís Roque, is the Founder and Partner of ZAAI, a company focused on AI product development, consultancy, and investment in AI startups. He also serves as the Vice President of Data & AI at Marley Spoon, leading teams across data science, data analytics, data product, data engineering, machine learning operations, and platforms. In addition, he holds the position of AI Advisor at CableLabs, where he contributes to integrating the broadband industry with AI technologies. Luís is also a Ph.D. Researcher in AI at the University of Porto's AI&CS lab and oversees the Data Science Master's program at Nuclio Digital School in Barcelona. Previously, he co-founded HUUB, where he served as CEO until its acquisition by Maersk.
Read more about Luís Roque