Statistical, Machine Learning, and Neural Forecasting Methods In this tutorial, we will explore the process of forecasting on the M5 dataset by utilizing the most suitable model for each time series. We’ll accomplish this through an essential technique known as cross-validation. This approach helps us in estimating the predictive performance of our models, and in selecting the model that yields the best performance for each time series.

The M5 dataset comprises of hierarchical sales data, spanning five years, from Walmart. The aim is to forecast daily sales for the next 28 days. The dataset is broken down into the 50 states of America, with 10 stores in each state.

In the realm of time series forecasting and analysis, one of the more complex tasks is identifying the model that is optimally suited for a specific group of series. Quite often, this selection process leans heavily on intuition, which may not necessarily align with the empirical reality of our dataset.

In this tutorial, we aim to provide a more structured, data-driven approach to model selection for different groups of series within the M5 benchmark dataset. This dataset, well-known in the field of forecasting, allows us to showcase the versatility and power of our methodology.

We will train an assortment of models from various forecasting paradigms:

StatsForecast

  • Baseline models: These models are simple yet often highly effective for providing an initial perspective on the forecasting problem. We will use SeasonalNaive and HistoricAverage models for this category.
  • Intermittent models: For series with sporadic, non-continuous demand, we will utilize models like CrostonOptimized, IMAPA, and ADIDA. These models are particularly suited for handling zero-inflated series.
  • State Space Models: These are statistical models that use mathematical descriptions of a system to make predictions. The AutoETS model from the statsforecast library falls under this category.

MLForecast

Machine Learning: Leveraging ML models like LightGBM, XGBoost, and LinearRegression can be advantageous due to their capacity to uncover intricate patterns in data. We’ll use the MLForecast library for this purpose.

NeuralForecast

Deep Learning: DL models, such as Transformers (AutoTFT) and Neural Networks (AutoNHITS), allow us to handle complex non-linear dependencies in time series data. We’ll utilize the NeuralForecast library for these models.

Using the Nixtla suite of libraries, we’ll be able to drive our model selection process with data, ensuring we utilize the most suitable models for specific groups of series in our dataset.

Outline:

  • Reading Data: In this initial step, we load our dataset into memory, making it available for our subsequent analysis and forecasting. It is important to understand the structure and nuances of the dataset at this stage.

  • Forecasting Using Statistical and Deep Learning Methods: We apply a wide range of forecasting methods from basic statistical techniques to advanced deep learning models. The aim is to generate predictions for the next 28 days based on our dataset.

  • Model Performance Evaluation on Different Windows: We assess the performance of our models on distinct windows.

  • Selecting the Best Model for a Group of Series: Using the performance evaluation, we identify the optimal model for each group of series. This step ensures that the chosen model is tailored to the unique characteristics of each group.

  • Filtering the Best Possible Forecast: Finally, we filter the forecasts generated by our chosen models to obtain the most promising predictions. This is our final output and represents the best possible forecast for each series according to our models.

Warning

This tutorial was originally executed using a c5d.24xlarge EC2 instance.

Installing Libraries

!pip install statsforecast mlforecast neuralforecast datasetforecast s3fs pyarrow

Download and prepare data

The example uses the M5 dataset. It consists of 30,490 bottom time series.

import pandas as pd
# Load the training target dataset from the provided URL
Y_df = pd.read_parquet('https://m5-benchmarks.s3.amazonaws.com/data/train/target.parquet')

# Rename columns to match the Nixtlaverse's expectations
# The 'item_id' becomes 'unique_id' representing the unique identifier of the time series
# The 'timestamp' becomes 'ds' representing the time stamp of the data points
# The 'demand' becomes 'y' representing the target variable we want to forecast
Y_df = Y_df.rename(columns={
    'item_id': 'unique_id', 
    'timestamp': 'ds', 
    'demand': 'y'
})

# Convert the 'ds' column to datetime format to ensure proper handling of date-related operations in subsequent steps
Y_df['ds'] = pd.to_datetime(Y_df['ds'])

For simplicity sake we will keep just one category

Y_df = Y_df.query('unique_id.str.startswith("FOODS_3")').reset_index(drop=True)

Y_df['unique_id'] = Y_df['unique_id'].astype(str)

Basic Plotting

Plot some series using the plot method from the StatsForecast class. This method prints 8 random series from the dataset and is useful for basic EDA.

from statsforecast import StatsForecast
/home/ubuntu/statsforecast/statsforecast/core.py:23: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html
  from tqdm.autonotebook import tqdm
# Feature: plot random series for EDA
StatsForecast.plot(Y_df)
# Feature: plot groups of series for EDA
StatsForecast.plot(Y_df, unique_ids=["FOODS_3_432_TX_2"])
# Feature: plot groups of series for EDA
StatsForecast.plot(Y_df, unique_ids=["FOODS_3_432_TX_2"], engine ='matplotlib')

Create forecasts with Stats, Ml and Neural methods.

StatsForecast

StatsForecast is a comprehensive library providing a suite of popular univariate time series forecasting models, all designed with a focus on high performance and scalability.

Here’s what makes StatsForecast a powerful tool for time series forecasting:

  • Collection of Local Models: StatsForecast provides a diverse collection of local models that can be applied to each time series individually, allowing us to capture unique patterns within each series.

  • Simplicity: With StatsForecast, training, forecasting, and backtesting multiple models become a straightforward process, requiring only a few lines of code. This simplicity makes it a convenient tool for both beginners and experienced practitioners.

  • Optimized for Speed: The implementation of the models in StatsForecast is optimized for speed, ensuring that large-scale computations are performed efficiently, thereby reducing the overall time for model training and prediction.

  • Horizontal Scalability: One of the distinguishing features of StatsForecast is its ability to scale horizontally. It is compatible with distributed computing frameworks such as Spark, Dask, and Ray. This feature allows it to handle large datasets by distributing the computations across multiple nodes in a cluster, making it a go-to solution for large-scale time series forecasting tasks.

StatsForecast receives a list of models to fit each time series. Since we are dealing with Daily data, it would be benefitial to use 7 as seasonality.

# Import necessary models from the statsforecast library
from statsforecast.models import (
    # SeasonalNaive: A model that uses the previous season's data as the forecast
    SeasonalNaive,
    # Naive: A simple model that uses the last observed value as the forecast
    Naive,
    # HistoricAverage: This model uses the average of all historical data as the forecast
    HistoricAverage,
    # CrostonOptimized: A model specifically designed for intermittent demand forecasting
    CrostonOptimized,
    # ADIDA: Adaptive combination of Intermittent Demand Approaches, a model designed for intermittent demand
    ADIDA,
    # IMAPA: Intermittent Multiplicative AutoRegressive Average, a model for intermittent series that incorporates autocorrelation
    IMAPA,
    # AutoETS: Automated Exponential Smoothing model that automatically selects the best Exponential Smoothing model based on AIC
    AutoETS
)

We fit the models by instantiating a new StatsForecast object with the following parameters:

  • models: a list of models. Select the models you want from models and import them.
  • freq: a string indicating the frequency of the data. (See panda’s available frequencies.)
  • n_jobs: int, number of jobs used in the parallel processing, use -1 for all cores.
  • fallback_model: a model to be used if a model fails. Any settings are passed into the constructor. Then you call its fit method and pass in the historical data frame.
horizon = 28
models = [
    SeasonalNaive(season_length=7),
    Naive(),
    HistoricAverage(),
    CrostonOptimized(),
    ADIDA(),
    IMAPA(),
    AutoETS(season_length=7)
]
# Instantiate the StatsForecast class
sf = StatsForecast(
    models=models,  # A list of models to be used for forecasting
    freq='D',  # The frequency of the time series data (in this case, 'D' stands for daily frequency)
    n_jobs=-1,  # The number of CPU cores to use for parallel execution (-1 means use all available cores)
)

The forecast method takes two arguments: forecasts next h (horizon) and level.

  • h (int): represents the forecast h steps into the future. In this case, 12 months ahead.
  • level (list of floats): this optional parameter is used for probabilistic forecasting. Set the level (or confidence percentile) of your prediction interval. For example, level=[90] means that the model expects the real value to be inside that interval 90% of the times.

The forecast object here is a new data frame that includes a column with the name of the model and the y hat values, as well as columns for the uncertainty intervals.

This block of code times how long it takes to run the forecasting function of the StatsForecast class, which predicts the next 28 days (h=28). The level is set to [90], meaning it will compute the 90% prediction interval. The time is calculated in minutes and printed out at the end.

from time import time

# Get the current time before forecasting starts, this will be used to measure the execution time
init = time()

# Call the forecast method of the StatsForecast instance to predict the next 28 days (h=28) 
# Level is set to [90], which means that it will compute the 90% prediction interval
fcst_df = sf.forecast(df=Y_df, h=28, level=[90])

# Get the current time after the forecasting ends
end = time()

# Calculate and print the total time taken for the forecasting in minutes
print(f'Forecast Minutes: {(end - init) / 60}')
Forecast Minutes: 2.270755163828532
fcst_df.head()
dsSeasonalNaiveSeasonalNaive-lo-90SeasonalNaive-hi-90NaiveNaive-lo-90Naive-hi-90HistoricAverageHistoricAverage-lo-90HistoricAverage-hi-90CrostonOptimizedADIDAIMAPAAutoETSAutoETS-lo-90AutoETS-hi-90
unique_id
FOODS_3_001_CA_12016-05-231.0-2.8471744.8471742.00.0983633.9016370.448738-1.0095791.9070550.3451920.3454770.3472490.381414-1.0281221.790950
FOODS_3_001_CA_12016-05-240.0-3.8471743.8471742.0-0.6893214.6893210.448738-1.0095791.9070550.3451920.3454770.3472490.286933-1.1241361.698003
FOODS_3_001_CA_12016-05-250.0-3.8471743.8471742.0-1.2937325.2937320.448738-1.0095791.9070550.3451920.3454770.3472490.334987-1.0776141.747588
FOODS_3_001_CA_12016-05-261.0-2.8471744.8471742.0-1.8032745.8032740.448738-1.0095791.9070550.3451920.3454770.3472490.186851-1.2272801.600982
FOODS_3_001_CA_12016-05-270.0-3.8471743.8471742.0-2.2521906.2521900.448738-1.0095791.9070550.3451920.3454770.3472490.308112-1.1075481.723771

MLForecast

MLForecast is a powerful library that provides automated feature creation for time series forecasting, facilitating the use of global machine learning models. It is designed for high performance and scalability.

Key features of MLForecast include:

  • Support for sklearn models: MLForecast is compatible with models that follow the scikit-learn API. This makes it highly flexible and allows it to seamlessly integrate with a wide variety of machine learning algorithms.

  • Simplicity: With MLForecast, the tasks of training, forecasting, and backtesting models can be accomplished in just a few lines of code. This streamlined simplicity makes it user-friendly for practitioners at all levels of expertise.

  • Optimized for speed: MLForecast is engineered to execute tasks rapidly, which is crucial when handling large datasets and complex models.

  • Horizontal Scalability: MLForecast is capable of horizontal scaling using distributed computing frameworks such as Spark, Dask, and Ray. This feature enables it to efficiently process massive datasets by distributing the computations across multiple nodes in a cluster, making it ideal for large-scale time series forecasting tasks.

from mlforecast import MLForecast
from mlforecast.target_transforms import Differences
from mlforecast.utils import PredictionIntervals
from window_ops.expanding import expanding_mean
!pip install lightgbm xgboost
# Import the necessary models from various libraries

# LGBMRegressor: A gradient boosting framework that uses tree-based learning algorithms from the LightGBM library
from lightgbm import LGBMRegressor

# XGBRegressor: A gradient boosting regressor model from the XGBoost library
from xgboost import XGBRegressor

# LinearRegression: A simple linear regression model from the scikit-learn library
from sklearn.linear_model import LinearRegression

To use MLForecast for time series forecasting, we instantiate a new MLForecast object and provide it with various parameters to tailor the modeling process to our specific needs:

  • models: This parameter accepts a list of machine learning models you wish to use for forecasting. You can import your preferred models from scikit-learn, lightgbm and xgboost.

  • freq: This is a string indicating the frequency of your data (hourly, daily, weekly, etc.). The specific format of this string should align with pandas’ recognized frequency strings.

  • target_transforms: These are transformations applied to the target variable before model training and after model prediction. This can be useful when working with data that may benefit from transformations, such as log-transforms for highly skewed data.

  • lags: This parameter accepts specific lag values to be used as regressors. Lags represent how many steps back in time you want to look when creating features for your model. For example, if you want to use the previous day’s data as a feature for predicting today’s value, you would specify a lag of 1.

  • lags_transforms: These are specific transformations for each lag. This allows you to apply transformations to your lagged features.

  • date_features: This parameter specifies date-related features to be used as regressors. For instance, you might want to include the day of the week or the month as a feature in your model.

  • num_threads: This parameter controls the number of threads to use for parallelizing feature creation, helping to speed up this process when working with large datasets.

All these settings are passed to the MLForecast constructor. Once the MLForecast object is initialized with these settings, we call its fit method and pass the historical data frame as the argument. The fit method trains the models on the provided historical data, readying them for future forecasting tasks.

# Instantiate the MLForecast object
mlf = MLForecast(
    models=[LGBMRegressor(), XGBRegressor(), LinearRegression()],  # List of models for forecasting: LightGBM, XGBoost and Linear Regression
    freq='D',  # Frequency of the data - 'D' for daily frequency
    lags=list(range(1, 7)),  # Specific lags to use as regressors: 1 to 6 days
    lag_transforms = {
        1:  [expanding_mean],  # Apply expanding mean transformation to the lag of 1 day
    },
    date_features=['year', 'month', 'day', 'dayofweek', 'quarter', 'week'],  # Date features to use as regressors
)

Just call the fit models to train the select models. In this case we are generating conformal prediction intervals.

# Start the timer to calculate the time taken for fitting the models
init = time()

# Fit the MLForecast models to the data, with prediction intervals set using a window size of 28 days
mlf.fit(Y_df, prediction_intervals=PredictionIntervals(window_size=28))

# Calculate the end time after fitting the models
end = time()

# Print the time taken to fit the MLForecast models, in minutes
print(f'MLForecast Minutes: {(end - init) / 60}')
MLForecast Minutes: 2.2809854547182717

After that, just call predict to generate forecasts.

fcst_mlf_df = mlf.predict(28, level=[90])
fcst_mlf_df.head()
unique_iddsLGBMRegressorXGBRegressorLinearRegressionLGBMRegressor-lo-90LGBMRegressor-hi-90XGBRegressor-lo-90XGBRegressor-hi-90LinearRegression-lo-90LinearRegression-hi-90
0FOODS_3_001_CA_12016-05-230.5495200.5984310.359638-0.2139151.312955-0.0200501.2169120.0300000.689277
1FOODS_3_001_CA_12016-05-240.5531960.3372680.100361-0.2513831.357775-0.2014490.875985-0.2161950.416917
2FOODS_3_001_CA_12016-05-250.5996680.3496040.175840-0.2039741.403309-0.2844160.983624-0.1505930.502273
3FOODS_3_001_CA_12016-05-260.6380970.3221440.1564600.1186881.157506-0.0858720.730160-0.2738510.586771
4FOODS_3_001_CA_12016-05-270.7633050.3003620.328194-0.3130911.839701-0.2966360.897360-0.6570891.313476

NeuralForecast

NeuralForecast is a robust collection of neural forecasting models that focuses on usability and performance. It includes a variety of model architectures, from classic networks such as Multilayer Perceptrons (MLP) and Recurrent Neural Networks (RNN) to novel contributions like N-BEATS, N-HITS, Temporal Fusion Transformers (TFT), and more.

Key features of NeuralForecast include:

  • A broad collection of global models. Out of the box implementation of MLP, LSTM, RNN, TCN, DilatedRNN, NBEATS, NHITS, ESRNN, TFT, Informer, PatchTST and HINT.
  • A simple and intuitive interface that allows training, forecasting, and backtesting of various models in a few lines of code.
  • Support for GPU acceleration to improve computational speed.

This machine doesn’t have GPU, but Google Colabs offers some for free.

Using Colab’s GPU to train NeuralForecast.

# Read the results from Colab
fcst_nf_df = pd.read_parquet('https://m5-benchmarks.s3.amazonaws.com/data/forecast-nf.parquet')
fcst_nf_df.head()
unique_iddsAutoNHITSAutoNHITS-lo-90AutoNHITS-hi-90AutoTFTAutoTFT-lo-90AutoTFT-hi-90
0FOODS_3_001_CA_12016-05-230.00.02.00.00.02.0
1FOODS_3_001_CA_12016-05-240.00.02.00.00.02.0
2FOODS_3_001_CA_12016-05-250.00.02.00.00.01.0
3FOODS_3_001_CA_12016-05-260.00.02.00.00.02.0
4FOODS_3_001_CA_12016-05-270.00.02.00.00.02.0
# Merge the forecasts from StatsForecast and NeuralForecast
fcst_df = fcst_df.merge(fcst_nf_df, how='left', on=['unique_id', 'ds'])

# Merge the forecasts from MLForecast into the combined forecast dataframe
fcst_df = fcst_df.merge(fcst_mlf_df, how='left', on=['unique_id', 'ds'])
fcst_df.head()
unique_iddsSeasonalNaiveSeasonalNaive-lo-90SeasonalNaive-hi-90NaiveNaive-lo-90Naive-hi-90HistoricAverageHistoricAverage-lo-90AutoTFT-hi-90LGBMRegressorXGBRegressorLinearRegressionLGBMRegressor-lo-90LGBMRegressor-hi-90XGBRegressor-lo-90XGBRegressor-hi-90LinearRegression-lo-90LinearRegression-hi-90
0FOODS_3_001_CA_12016-05-231.0-2.8471744.8471742.00.0983633.9016370.448738-1.0095792.00.5495200.5984310.359638-0.2139151.312955-0.0200501.2169120.0300000.689277
1FOODS_3_001_CA_12016-05-240.0-3.8471743.8471742.0-0.6893214.6893210.448738-1.0095792.00.5531960.3372680.100361-0.2513831.357775-0.2014490.875985-0.2161950.416917
2FOODS_3_001_CA_12016-05-250.0-3.8471743.8471742.0-1.2937325.2937320.448738-1.0095791.00.5996680.3496040.175840-0.2039741.403309-0.2844160.983624-0.1505930.502273
3FOODS_3_001_CA_12016-05-261.0-2.8471744.8471742.0-1.8032745.8032740.448738-1.0095792.00.6380970.3221440.1564600.1186881.157506-0.0858720.730160-0.2738510.586771
4FOODS_3_001_CA_12016-05-270.0-3.8471743.8471742.0-2.2521906.2521900.448738-1.0095792.00.7633050.3003620.328194-0.3130911.839701-0.2966360.897360-0.6570891.313476

Forecast plots

sf.plot(Y_df, fcst_df, max_insample_length=28 * 3)

Use the plot function to explore models and ID’s

sf.plot(Y_df, fcst_df, max_insample_length=28 * 3, 
        models=['CrostonOptimized', 'AutoNHITS', 'SeasonalNaive', 'LGBMRegressor'])

Validate Model’s Performance

The three libraries - StatsForecast, MLForecast, and NeuralForecast - offer out-of-the-box cross-validation capabilities specifically designed for time series. This allows us to evaluate the model’s performance using historical data to obtain an unbiased assessment of how well each model is likely to perform on unseen data.

Cross Validation in StatsForecast

The cross_validation method from the StatsForecast class accepts the following arguments:

  • df: A DataFrame representing the training data.
  • h (int): The forecast horizon, represented as the number of steps into the future that we wish to predict. For example, if we’re forecasting hourly data, h=24 would represent a 24-hour forecast.
  • step_size (int): The step size between each cross-validation window. This parameter determines how often we want to run the forecasting process.
  • n_windows (int): The number of windows used for cross validation. This parameter defines how many past forecasting processes we want to evaluate.

These parameters allow us to control the extent and granularity of our cross-validation process. By tuning these settings, we can balance between computational cost and the thoroughness of the cross-validation.

init = time()
cv_df = sf.cross_validation(df=Y_df, h=horizon, n_windows=3, step_size=horizon, level=[90])
end = time()
print(f'CV Minutes: {(end - init) / 60}')
/home/ubuntu/statsforecast/statsforecast/ets.py:1041: RuntimeWarning:

divide by zero encountered in double_scalars
CV Minutes: 5.206169327100118

The crossvaldation_df object is a new data frame that includes the following columns:

  • unique_id index: (If you dont like working with index just run forecasts_cv_df.resetindex())
  • ds: datestamp or temporal index
  • cutoff: the last datestamp or temporal index for the n_windows. If n_windows=1, then one unique cuttoff value, if n_windows=2 then two unique cutoff values.
  • y: true value
  • "model": columns with the model’s name and fitted value.
cv_df.head()
dscutoffySeasonalNaiveSeasonalNaive-lo-90SeasonalNaive-hi-90NaiveNaive-lo-90Naive-hi-90HistoricAverageHistoricAverage-lo-90HistoricAverage-hi-90CrostonOptimizedADIDAIMAPAAutoETSAutoETS-lo-90AutoETS-hi-90
unique_id
FOODS_3_001_CA_12016-02-292016-02-280.02.0-1.8788855.8788850.0-1.9170111.9170110.449111-1.0218131.9200360.6184720.6183750.6179980.655286-0.7657312.076302
FOODS_3_001_CA_12016-03-012016-02-281.00.0-3.8788853.8788850.0-2.7110642.7110640.449111-1.0218131.9200360.6184720.6183750.6179980.568595-0.8539661.991155
FOODS_3_001_CA_12016-03-022016-02-281.00.0-3.8788853.8788850.0-3.3203613.3203610.449111-1.0218131.9200360.6184720.6183750.6179980.618805-0.8052982.042908
FOODS_3_001_CA_12016-03-032016-02-280.01.0-2.8788854.8788850.0-3.8340233.8340230.449111-1.0218131.9200360.6184720.6183750.6179980.455891-0.9697531.881534
FOODS_3_001_CA_12016-03-042016-02-280.01.0-2.8788854.8788850.0-4.2865684.2865680.449111-1.0218131.9200360.6184720.6183750.6179980.591197-0.8359872.018380

MLForecast

The cross_validation method from the MLForecast class takes the following arguments.

  • data: training data frame
  • window_size (int): represents h steps into the future that are being forecasted. In this case, 24 hours ahead.
  • step_size (int): step size between each window. In other words: how often do you want to run the forecasting processes.
  • n_windows (int): number of windows used for cross-validation. In other words: what number of forecasting processes in the past do you want to evaluate.
  • prediction_intervals: class to compute conformal intervals.
init = time()
cv_mlf_df = mlf.cross_validation(
    data=Y_df, 
    window_size=horizon, 
    n_windows=3, 
    step_size=horizon, 
    level=[90],
)
end = time()
print(f'CV Minutes: {(end - init) / 60}')
/home/ubuntu/miniconda/envs/statsforecast/lib/python3.10/site-packages/mlforecast/forecast.py:576: UserWarning:

Excuting `cross_validation` after `fit` can produce unexpected errors

/home/ubuntu/miniconda/envs/statsforecast/lib/python3.10/site-packages/mlforecast/forecast.py:468: UserWarning:

Please rerun the `fit` method passing a proper value to prediction intervals to compute them.

/home/ubuntu/miniconda/envs/statsforecast/lib/python3.10/site-packages/mlforecast/forecast.py:468: UserWarning:

Please rerun the `fit` method passing a proper value to prediction intervals to compute them.

/home/ubuntu/miniconda/envs/statsforecast/lib/python3.10/site-packages/mlforecast/forecast.py:468: UserWarning:

Please rerun the `fit` method passing a proper value to prediction intervals to compute them.
CV Minutes: 2.961174162228902

The crossvaldation_df object is a new data frame that includes the following columns:

  • unique_id index: (If you dont like working with index just run forecasts_cv_df.resetindex())
  • ds: datestamp or temporal index
  • cutoff: the last datestamp or temporal index for the n_windows. If n_windows=1, then one unique cuttoff value, if n_windows=2 then two unique cutoff values.
  • y: true value
  • "model": columns with the model’s name and fitted value.
cv_mlf_df.head()
unique_iddscutoffyLGBMRegressorXGBRegressorLinearRegression
0FOODS_3_001_CA_12016-02-292016-02-280.00.4356740.556261-0.312492
1FOODS_3_001_CA_12016-03-012016-02-281.00.6396760.625806-0.041924
2FOODS_3_001_CA_12016-03-022016-02-281.00.7929890.6596500.263699
3FOODS_3_001_CA_12016-03-032016-02-280.00.8068680.5351210.482491
4FOODS_3_001_CA_12016-03-042016-02-280.00.8291060.3133530.677326

NeuralForecast

This machine doesn’t have GPU, but Google Colabs offers some for free.

Using Colab’s GPU to train NeuralForecast.

cv_nf_df = pd.read_parquet('https://m5-benchmarks.s3.amazonaws.com/data/cross-validation-nf.parquet')
cv_nf_df.head()
unique_iddscutoffAutoNHITSAutoNHITS-lo-90AutoNHITS-hi-90AutoTFTAutoTFT-lo-90AutoTFT-hi-90y
0FOODS_3_001_CA_12016-02-292016-02-280.00.02.01.00.02.00.0
1FOODS_3_001_CA_12016-03-012016-02-280.00.02.01.00.02.01.0
2FOODS_3_001_CA_12016-03-022016-02-280.00.02.01.00.02.01.0
3FOODS_3_001_CA_12016-03-032016-02-280.00.02.01.00.02.00.0
4FOODS_3_001_CA_12016-03-042016-02-280.00.02.01.00.02.00.0

Merge cross validation forecasts

cv_df = cv_df.merge(cv_nf_df.drop(columns=['y']), how='left', on=['unique_id', 'ds', 'cutoff'])
cv_df = cv_df.merge(cv_mlf_df.drop(columns=['y']), how='left', on=['unique_id', 'ds', 'cutoff'])

Plots CV

cutoffs = cv_df['cutoff'].unique()
for cutoff in cutoffs:
    img = sf.plot(
        Y_df, 
        cv_df.query('cutoff == @cutoff').drop(columns=['y', 'cutoff']), 
        max_insample_length=28 * 5, 
        unique_ids=['FOODS_3_001_CA_1'],
    )
    img.show()

Aggregate Demand

agg_cv_df = cv_df.loc[:,~cv_df.columns.str.contains('hi|lo')].groupby(['ds', 'cutoff']).sum(numeric_only=True).reset_index()
agg_cv_df.insert(0, 'unique_id', 'agg_demand')
agg_Y_df = Y_df.groupby(['ds']).sum(numeric_only=True).reset_index()
agg_Y_df.insert(0, 'unique_id', 'agg_demand')
for cutoff in cutoffs:
    img = sf.plot(
        agg_Y_df, 
        agg_cv_df.query('cutoff == @cutoff').drop(columns=['y', 'cutoff']),
        max_insample_length=28 * 5,
    )
    img.show()

Evaluation per series and CV window

In this section, we will evaluate the performance of each model for each time series and each cross validation window. Since we have many combinations, we will use dask to parallelize the evaluation. The parallelization will be done using fugue.

from typing import List, Callable

from distributed import Client
from fugue import transform
from fugue_dask import DaskExecutionEngine
from datasetsforecast.losses import mse, mae, smape

The evaluate function receives a unique combination of a time series and a window, and calculates different metrics for each model in df.

def evaluate(df: pd.DataFrame, metrics: List[Callable]) -> pd.DataFrame:
    eval_ = {}
    models = df.loc[:, ~df.columns.str.contains('unique_id|y|ds|cutoff|lo|hi')].columns
    for model in models:
        eval_[model] = {}
        for metric in metrics:
            eval_[model][metric.__name__] = metric(df['y'], df[model])
    eval_df = pd.DataFrame(eval_).rename_axis('metric').reset_index()
    eval_df.insert(0, 'cutoff', df['cutoff'].iloc[0])
    eval_df.insert(0, 'unique_id', df['unique_id'].iloc[0])
    return eval_df
str_models = cv_df.loc[:, ~cv_df.columns.str.contains('unique_id|y|ds|cutoff|lo|hi')].columns
str_models = ','.join([f"{model}:float" for model in str_models])
cv_df['cutoff'] = cv_df['cutoff'].astype(str)
cv_df['unique_id'] = cv_df['unique_id'].astype(str)

Let’s cleate a dask client.

client = Client() # without this, dask is not in distributed mode
# fugue.dask.dataframe.default.partitions determines the default partitions for a new DaskDataFrame
engine = DaskExecutionEngine({"fugue.dask.dataframe.default.partitions": 96})

The transform function takes the evaluate functions and applies it to each combination of time series (unique_id) and cross validation window (cutoff) using the dask client we created before.

evaluation_df = transform(
    cv_df.loc[:, ~cv_df.columns.str.contains('lo|hi')], 
    evaluate, 
    engine="dask",
    params={'metrics': [mse, mae, smape]}, 
    schema=f"unique_id:str,cutoff:str,metric:str, {str_models}", 
    as_local=True,
    partition={'by': ['unique_id', 'cutoff']}
)
/home/ubuntu/miniconda/envs/statsforecast/lib/python3.10/site-packages/distributed/client.py:3109: UserWarning:

Sending large graph of size 49.63 MiB.
This may cause some slowdown.
Consider scattering data ahead of time and using futures.
evaluation_df.head()
unique_idcutoffmetricSeasonalNaiveNaiveHistoricAverageCrostonOptimizedADIDAIMAPAAutoETSAutoNHITSAutoTFTLGBMRegressorXGBRegressorLinearRegression
0FOODS_3_003_WI_32016-02-28mse1.1428571.1428570.8166460.8164711.1428571.1428571.1428571.1428571.1428570.8320101.0203610.887121
1FOODS_3_003_WI_32016-02-28mae0.5714290.5714290.7295920.7312610.5714290.5714290.5714290.5714290.5714290.7727880.6199490.685413
2FOODS_3_003_WI_32016-02-28smape71.42857471.428574158.813507158.516235200.000000200.000000200.00000071.42857471.428574145.901947188.159164178.883743
3FOODS_3_013_CA_32016-04-24mse4.0000006.2142862.4067643.5612022.2678532.2676002.2686772.7500002.1250002.1605082.3702282.289606
4FOODS_3_013_CA_32016-04-24mae1.5000002.1428571.2142861.3404461.2142861.2142861.2142861.1071431.1428571.1400841.1575481.148813
# Calculate the mean metric for each cross validation window
evaluation_df.groupby(['cutoff', 'metric']).mean(numeric_only=True)
SeasonalNaiveNaiveHistoricAverageCrostonOptimizedADIDAIMAPAAutoETSAutoNHITSAutoTFTLGBMRegressorXGBRegressorLinearRegression
cutoffmetric
2016-02-28mae1.7442892.0404961.7307041.6330171.5279651.5287721.4975531.4349381.4854191.6884031.5141021.576320
mse14.51071019.08058512.85899411.78503211.11449711.10090910.34784710.01098210.96466410.43620610.96878810.792831
smape85.20204287.719086125.418488124.749908127.591858127.704102127.79067279.13261480.983368118.489983140.420578127.043137
2016-03-27mae1.7959732.1064491.7540291.6620871.5707011.5727411.5353011.4324121.5023931.7124931.6001931.601612
mse14.81025926.04447212.80410412.02062012.08386112.12003311.3150139.44586710.76287710.72358912.92431210.943772
smape87.40747189.453247123.587196123.460030123.428459123.538521123.61299179.92678182.013168116.089699138.885941127.304871
2016-04-24mae1.7859831.9907741.7625061.6092681.5276271.5297211.5018201.4474011.5051271.6929461.5418451.590985
mse13.47635016.23491713.15131110.64704810.07222510.0623959.3934399.36389110.43621410.34707310.77420210.608137
smape89.23881590.685867121.124947119.721245120.325401120.345284120.64958281.40274883.614029113.334198136.755234124.618622

Results showed in previous experiments.

modelMSE
MQCNN10.09
DeepAR-student_t10.11
DeepAR-lognormal30.20
DeepAR9.13
NPTS11.53

Top 3 models: DeepAR, AutoNHITS, AutoETS.

Distribution of errors

!pip install seaborn
import matplotlib.pyplot as plt
import seaborn as sns
evaluation_df_melted = pd.melt(evaluation_df, id_vars=['unique_id', 'cutoff', 'metric'], var_name='model', value_name='error')

SMAPE

sns.violinplot(evaluation_df_melted.query('metric=="smape"'), x='error', y='model')

Choose models for groups of series

Feature:

  • A unified dataframe with forecasts for all different models
  • Easy Ensamble
  • E.g. Average predictions
  • Or MinMax (Choosing is ensembling)
# Choose the best model for each time series, metric, and cross validation window
evaluation_df['best_model'] = evaluation_df.idxmin(axis=1, numeric_only=True)
# count how many times a model wins per metric and cross validation window
count_best_model = evaluation_df.groupby(['cutoff', 'metric', 'best_model']).size().rename('n').to_frame().reset_index()
# plot results
sns.barplot(count_best_model, x='n', y='best_model', hue='metric')

Et pluribus unum: an inclusive forecasting Pie.

# For the mse, calculate how many times a model wins
eval_series_df = evaluation_df.query('metric == "mse"').groupby(['unique_id']).mean(numeric_only=True)
eval_series_df['best_model'] = eval_series_df.idxmin(axis=1)
counts_series = eval_series_df.value_counts('best_model')
plt.pie(counts_series, labels=counts_series.index, autopct='%.0f%%')
plt.show()

sf.plot(Y_df, cv_df.drop(columns=['cutoff', 'y']), 
        max_insample_length=28 * 6, 
        models=['AutoNHITS'],
        unique_ids=eval_series_df.query('best_model == "AutoNHITS"').index[:8])

Choose Forecasting method for different groups of series

# Merge the best model per time series dataframe
# and filter the forecasts based on that dataframe
# for each time series
fcst_df = pd.melt(fcst_df.set_index('unique_id'), id_vars=['ds'], var_name='model', value_name='forecast', ignore_index=False)
fcst_df = fcst_df.join(eval_series_df[['best_model']])
fcst_df[['model', 'pred-interval']] = fcst_df['model'].str.split('-', expand=True, n=1)
fcst_df = fcst_df.query('model == best_model')
fcst_df['name'] = [f'forecast-{x}' if x is not None else 'forecast' for x in fcst_df['pred-interval']]
fcst_df = pd.pivot_table(fcst_df, index=['unique_id', 'ds'], values=['forecast'], columns=['name']).droplevel(0, axis=1).reset_index()
sf.plot(Y_df, fcst_df, max_insample_length=28 * 3)

Technical Debt

  • Train the statistical models in the full dataset.
  • Increase the number of num_samples in the neural auto models.
  • Include other models such as Theta, ARIMA, RNN, LSTM, …

Further materials