Tip

Automatic forecasts of large numbers of univariate time series are often needed. It is common to have multiple product lines or skus that need forecasting. In these circumstances, an automatic forecasting algorithm is an essential tool. Automatic forecasting algorithms must determine an appropriate time series model, estimate the parameters and compute the forecasts. They must be robust to unusual time series patterns, and applicable to large numbers of series without user intervention.

1. Install statsforecast and load data

Use pip to install statsforecast and load Air Passangers dataset as an example

# uncomment the following line to install the library
# %pip install statsforecast
from statsforecast.utils import AirPassengersDF
Y_df = AirPassengersDF

2. Import StatsForecast and models

Import the core StatsForecast class and the models you want to use

import pandas as pd

from statsforecast import StatsForecast
from statsforecast.models import AutoARIMA, AutoETS, AutoTheta, AutoCES

3. Instatiate the class

Instantiate the StatsForecast class with the appropriate parameters

season_length = 12 # Define season length as 12 months for monthly data
horizon = 1 # Forecast horizon is set to 1 month

# Define a list of models for forecasting
models = [
    AutoARIMA(season_length=season_length), # ARIMA model with automatic order selection and seasonal component
    AutoETS(season_length=season_length), # ETS model with automatic error, trend, and seasonal component
    AutoTheta(season_length=season_length), # Theta model with automatic seasonality detection
    AutoCES(season_length=season_length), # CES model with automatic seasonality detection
]

# Instantiate StatsForecast class with models, data frequency ('M' for monthly),
# and parallel computation on all CPU cores (n_jobs=-1)
sf = StatsForecast(
    models=models, # models for forecasting
    freq=pd.offsets.MonthEnd(),  # frequency of the timestamps
    n_jobs=1  # number of jobs to run in parallel, -1 means using all processors
)

4. a) Forecast with forecast method

The .forecast method is faster for distributed computing and does not save the fittted models

# Generate forecasts for the specified horizon using the sf object
Y_hat_df = sf.forecast(df=Y_df, h=horizon) # forecast data
# Display the first few rows of the forecast DataFrame
Y_hat_df.head() # preview of forecasted data
unique_iddsAutoARIMAAutoETSAutoThetaCES
01.01961-01-31444.309575442.357169442.940797453.03418

4. b) Forecast with fit and predict

The .fit method saves the fitted models

sf.fit(df=Y_df) # Fit the models to the data using the fit method of the StatsForecast object

sf.fitted_ # Access fitted models from the StatsForecast object

Y_hat_df = sf.predict(h=horizon) # Predict or forecast 'horizon' steps ahead using the predict method

Y_hat_df.head() # Preview the first few rows of the forecasted data
unique_iddsAutoARIMAAutoETSAutoThetaCES
01.01961-01-31444.309575442.357169442.940797453.03418

References

Hyndman, RJ and Khandakar, Y (2008) “Automatic time series forecasting: The forecast package for R”, Journal of Statistical Software, 26(3).