More about forecasting in cienciadedatos.net


No description has been provided for this image

Introduction

In Single-Series Modeling (Local Forecasting Model), each time series is analyzed individually, modeled as a combination of its own lags and, optionally, exogenous variables. This approach provides detailed insights specific to each series but can become impractical for scaling when dealing with a large number of time series. In contrast, Multi-Series Modeling (Global Forecasting Model) involves building a unified predictive model that learns multiple time series simultaneously. It attempts to capture common dynamics that influence the series as a whole, thereby reducing noise from individual series. This approach requires a single model regardless of the number of series, is easy to maintain, and can yield more robust generalizations across time series, albeit potentially at the cost of sacrificing some individual insights. Two strategies of global forecasting models can be distinguished: Independent multi-series and dependent multi-series.

Advantages of multi-series:

  • It is easier to maintain and monitor a single model than several.

  • Since all time series are combined during training, the model has a higher learning capacity even if the series are short.

  • By combining multiple time series, the model can learn more generalizable patterns.

Limitations of multi-series:

  • If the series do not follow the same internal dynamics, the model may learn a pattern that does not represent any of them.

  • The series may mask each other, so the model may not predict all of them with the same performance.

  • It is more computationally demanding (time and resources) to train and backtest a big model than several small ones.

Independent multi-series forecasting

In this scenario, each time series is independent of the others or, in other words, the past values of one series are not used as predictors of the other series. Why is it useful then to model everything together? Although the series do not depend on each other, they may follow the same intrinsic pattern regarding their past and future values. For example, in the same store, the sales of products A and B may not be related, but they follow the same dynamics, those of the store.

No description has been provided for this image

To predict the next n steps, a recursive strategy is followed, known as recursive multi-step forecasting.

No description has been provided for this image

Dependent multi-series forecasting (Multivariate forecasting)

In dependent multi-series forecasting (multivariate time series), all series are modeled together in a single model, considering that each time series depends not only on its past values but also on the past values of the other series. The forecaster is expected not only to learn the information of each series separately but also to relate them. An example is the measurements made by all the sensors (flow, temperature, pressure...) installed on an industrial machine such as a compressor. See the dependent multi-series (multivariate) forecasting user guide for more details.

No description has been provided for this image

💡 Tip

This is part of a series of documents on global forecasting models:

Multiple time series with equal length

In this first example, multiple time series with the same length are used. The goal is to compare the forecasting results of a global model with those of an individual model for each series when forecasting the next 7 days of sales for 50 different items in a store using the 5 years of available history. The data has been obtained from the Store Item Demand Forecasting Challenge. This dataset contains 913,000 sales transactions from 01/01/2013 to 31/12/2017 for 50 products (SKU) in 10 stores.

Libraries

# Data manipulation
# ==============================================================================
import numpy as np
import pandas as pd

# Plots
# ==============================================================================
import matplotlib.pyplot as plt
from skforecast.plot import set_dark_theme
from tqdm.notebook import tqdm

# Modeling and forecasting
# ==============================================================================
import sklearn
import skforecast
from sklearn.ensemble import HistGradientBoostingRegressor
from sklearn.preprocessing import StandardScaler
from skforecast.recursive import ForecasterRecursive, ForecasterRecursiveMultiSeries
from skforecast.model_selection import (
    TimeSeriesFold,
    OneStepAheadFold,
    backtesting_forecaster,
    bayesian_search_forecaster,
    backtesting_forecaster_multiseries,
    bayesian_search_forecaster_multiseries
)
from skforecast.preprocessing import (
    RollingFeatures,
    reshape_series_long_to_dict,
    reshape_exog_long_to_dict
)
from skforecast.exceptions import OneStepAheadValidationWarning

# Warnings configuration
# ==============================================================================
import warnings

color = '\033[1m\033[38;5;208m'
print(f'{color}Version skforecast: {skforecast.__version__}')
print(f'{color}Version scikit-learn: {sklearn.__version__}')
print(f'{color}Version pandas: {pd.__version__}')
print(f'{color}Version numpy: {np.__version__}')
Version skforecast: 0.27.0
Version scikit-learn: 1.9.1
Version pandas: 2.3.3
Version numpy: 2.5.4

Data

# Data loading
# ======================================================================================
data = pd.read_csv('./train_stores_kaggle.csv')
display(data.head())
print(f'Shape: {data.shape}')
date store item sales
0 2013-01-01 1 1 13
1 2013-01-02 1 1 11
2 2013-01-03 1 1 14
3 2013-01-04 1 1 13
4 2013-01-05 1 1 10
Shape: (913000, 4)
# Data preprocessing
# ======================================================================================
selected_store = 2
# All items are selected, modify `selected_items` to use a subset
selected_items = data.item.unique()
data = data[
    (data['store'] == selected_store) & (data['item'].isin(selected_items))
].copy()
data['date'] = pd.to_datetime(data['date'], format='%Y-%m-%d')
data = pd.pivot_table(
           data    = data,
           values  = 'sales',
           index   = 'date',
           columns = 'item'
       )
data.columns.name = None
data.columns = [f'item_{col}' for col in data.columns]
data = data.asfreq('1D')
data.head(4)
item_1 item_2 item_3 item_4 item_5 item_6 item_7 item_8 item_9 item_10 ... item_41 item_42 item_43 item_44 item_45 item_46 item_47 item_48 item_49 item_50
date
2013-01-01 12.0 41.0 19.0 21.0 4.0 34.0 39.0 49.0 28.0 51.0 ... 11.0 25.0 36.0 12.0 45.0 43.0 12.0 45.0 29.0 43.0
2013-01-02 16.0 33.0 32.0 14.0 6.0 40.0 47.0 42.0 21.0 56.0 ... 19.0 21.0 35.0 25.0 50.0 52.0 13.0 37.0 25.0 57.0
2013-01-03 16.0 46.0 26.0 12.0 12.0 41.0 43.0 46.0 29.0 46.0 ... 23.0 20.0 52.0 18.0 56.0 30.0 5.0 45.0 30.0 45.0
2013-01-04 20.0 50.0 34.0 17.0 16.0 41.0 44.0 55.0 32.0 56.0 ... 15.0 28.0 50.0 24.0 57.0 46.0 19.0 32.0 20.0 45.0

4 rows × 50 columns

The dataset is divided into 3 partitions: one for training, one for validation, and one for testing.

# Split data into train-validation-test
# ======================================================================================
end_train = '2016-05-31 23:59:00'
end_val = '2017-05-31 23:59:00'
data_train = data.loc[:end_train, :].copy()
data_val   = data.loc[end_train:end_val, :].copy()
data_test  = data.loc[end_val:, :].copy()
print(
    f'Train dates      : {data_train.index.min()} --- '
    f'{data_train.index.max()}  (n={len(data_train)})'
)
print(
    f'Validation dates : {data_val.index.min()} --- '
    f'{data_val.index.max()}  (n={len(data_val)})'
)
print(
    f'Test dates       : {data_test.index.min()} --- '
    f'{data_test.index.max()}  (n={len(data_test)})'
)
Train dates      : 2013-01-01 00:00:00 --- 2016-05-31 00:00:00  (n=1247)
Validation dates : 2016-06-01 00:00:00 --- 2017-05-31 00:00:00  (n=365)
Test dates       : 2017-06-01 00:00:00 --- 2017-12-31 00:00:00  (n=214)

Four of the series are plotted to understand their trends and patterns. The reader is strongly encouraged to plot several more to gain an in-depth understanding of the series.

# Plot time series
# ======================================================================================
set_dark_theme()
fig, axs = plt.subplots(4, 1, figsize=(7, 5), sharex=True)
data.iloc[:, :4].plot(
    legend    = True,
    subplots  = True,
    title     = f'Sales of store {selected_store}',
    ax        = axs,
    linewidth = 1
)
for ax in axs:
    ax.axvline(pd.to_datetime(end_train), color='white', linestyle='--', linewidth=1.5)
    ax.axvline(pd.to_datetime(end_val), color='white', linestyle='--', linewidth=1.5)
fig.tight_layout()
plt.show()
No description has been provided for this image

Individual forecaster for each product

A separate Gradient Boosting Machine (GBM) model is trained for each item, using sales over the last 14 days, as well as the average, maximum and minimum sales over the last 7 days (RollingFeatures) as predictive features. The performance of the model over the next 7 days is evaluated using backtesting with the Mean Absolute Error (MAE) as the evaluation metric. The folds are defined with TimeSeriesFold and the evaluation is run with backtesting_forecaster. Finally, the performance of these individual models is compared with that of a global model trained on all series.

# Train and backtest a model for each item: ForecasterRecursive
# ======================================================================================
items = []
mae_values = []
cv = TimeSeriesFold(
        steps              = 7,
        initial_train_size = len(data_train) + len(data_val),
        refit              = False,
     )

for item in tqdm(data.columns):
    # Define forecaster
    window_features = RollingFeatures(stats=['mean', 'min', 'max'], window_sizes=7)
    forecaster = ForecasterRecursive(
                     estimator       = HistGradientBoostingRegressor(random_state=8523),
                     lags            = 14,
                     window_features = window_features
                 )
    # Backtesting forecaster
    metric, _ = backtesting_forecaster(
                        forecaster    = forecaster,
                        y             = data[item],
                        cv            = cv,
                        metric        = 'mean_absolute_error',
                        show_progress = False
                    )
    items.append(item)
    mae_values.append(metric.at[0, 'mean_absolute_error'])

# Results
uni_series_mae = pd.Series(
                     data  = mae_values,
                     index = items,
                     name  = 'uni_series_mae'
                 )
uni_series_mae.head()
item_1     6.004406
item_2    10.104921
item_3     8.678198
item_4     5.528955
item_5     5.096925
Name: uni_series_mae, dtype: float64

Global model

A global model (ForecasterRecursiveMultiSeries) is trained on all series simultaneously and evaluated using the same backtesting process as the individual models, this time with backtesting_forecaster_multiseries. Since the series are passed as a DataFrame, skforecast shows an InputTypeWarning indicating that a dictionary of pandas.Series is a more efficient input format; this format is covered in the section Series with different lengths and different exogenous variables.

# Train and backtest a model for all items: ForecasterRecursiveMultiSeries
# ======================================================================================
# Define forecaster
estimator = HistGradientBoostingRegressor(random_state=8523)
window_features = RollingFeatures(stats=['mean', 'min', 'max'], window_sizes=7)
forecaster_ms = ForecasterRecursiveMultiSeries(
                    estimator          = estimator,
                    lags               = 14,
                    encoding           = 'ordinal',
                    transformer_series = StandardScaler(),
                    window_features    = window_features,
                )
# Backtesting forecaster for all items
cv = TimeSeriesFold(
        steps              = 7,
        initial_train_size = len(data_train) + len(data_val),
        refit              = False,
     )
multi_series_mae, predictions_ms = backtesting_forecaster_multiseries(
                                       forecaster            = forecaster_ms,
                                       series                = data,
                                       cv                    = cv,
                                       metric                = 'mean_absolute_error',
                                       add_aggregated_metric = False,
                                   )
# Results
display(multi_series_mae.head(3))
print('')
display(predictions_ms.head(3))
╭────────────────────────────────── InputTypeWarning ──────────────────────────────────╮
│ Passing a DataFrame (either wide or long format) as `series` requires additional     │
│ internal transformations, which can increase computational time. It is recommended   │
│ to use a dictionary of pandas Series instead. For more details, see:                 │
│ https://skforecast.org/latest/user_guides/independent-multi-time-series-forecasting. │
│ html#input-data                                                                      │
│                                                                                      │
│ Category : skforecast.exceptions.InputTypeWarning                                    │
│ Location :                                                                           │
│ C:\Users\Joaquin\miniconda3\envs\skforecast_27_py13\Lib\site-packages\skforecast\uti │
│ ls\utils.py:4425                                                                     │
│ Suppress : warnings.simplefilter('ignore', category=InputTypeWarning)                │
╰──────────────────────────────────────────────────────────────────────────────────────╯
levels mean_absolute_error n_evaluated
0 item_1 5.512613 214
1 item_2 9.218515 214
2 item_3 7.310630 214

level fold step y_true pred
2017-06-01 item_1 0 1 41.0 35.781356
2017-06-01 item_2 0 1 116.0 93.461083
2017-06-01 item_3 0 1 70.0 62.066491

Comparison

The mean absolute error (MAE) for each item is calculated using the individual and global models. The results are compared to determine which model performs better.

# Difference in backtesting metric for each item
# ======================================================================================
multi_series_mae = (
    multi_series_mae.set_index('levels')['mean_absolute_error']
    .rename('multi_series_mae')
)
results = pd.concat((uni_series_mae, multi_series_mae), axis=1)
results['improvement'] = results['uni_series_mae'] - results['multi_series_mae']
results['improvement_(%)'] = (
    100 * results['improvement'] / results['uni_series_mae']
)
results = results.round(2)
results.style.bar(subset=['improvement_(%)'], align='mid', color=['#d65f5f', '#5fba7d'])
  uni_series_mae multi_series_mae improvement improvement_(%)
item_1 6.000000 5.510000 0.490000 8.190000
item_2 10.100000 9.220000 0.890000 8.770000
item_3 8.680000 7.310000 1.370000 15.760000
item_4 5.530000 5.010000 0.520000 9.390000
item_5 5.100000 4.660000 0.440000 8.620000
item_6 10.930000 9.760000 1.170000 10.730000
item_7 10.240000 9.860000 0.380000 3.740000
item_8 11.930000 10.540000 1.390000 11.620000
item_9 9.410000 8.730000 0.680000 7.270000
item_10 11.680000 10.450000 1.230000 10.520000
item_11 11.140000 10.390000 0.750000 6.740000
item_12 11.750000 10.790000 0.960000 8.170000
item_13 11.970000 11.320000 0.650000 5.420000
item_14 10.200000 9.490000 0.710000 6.960000
item_15 12.620000 11.510000 1.100000 8.750000
item_16 6.000000 5.870000 0.140000 2.260000
item_17 7.610000 7.160000 0.460000 5.980000
item_18 12.620000 12.000000 0.620000 4.880000
item_19 7.810000 7.320000 0.490000 6.290000
item_20 8.510000 7.740000 0.770000 9.030000
item_21 8.530000 8.170000 0.370000 4.300000
item_22 11.550000 10.700000 0.860000 7.430000
item_23 7.490000 6.810000 0.670000 9.010000
item_24 10.240000 9.820000 0.430000 4.150000
item_25 12.550000 11.650000 0.900000 7.160000
item_26 9.350000 8.590000 0.760000 8.160000
item_27 5.480000 5.250000 0.230000 4.140000
item_28 12.850000 11.710000 1.140000 8.900000
item_29 11.150000 10.240000 0.910000 8.140000
item_30 8.400000 7.850000 0.550000 6.560000
item_31 10.630000 10.230000 0.400000 3.790000
item_32 9.900000 9.130000 0.770000 7.800000
item_33 9.820000 9.130000 0.690000 7.050000
item_34 6.340000 5.830000 0.510000 7.990000
item_35 11.040000 10.160000 0.880000 8.000000
item_36 11.920000 10.480000 1.440000 12.080000
item_37 6.510000 6.070000 0.430000 6.680000
item_38 11.750000 11.190000 0.550000 4.720000
item_39 8.240000 7.260000 0.980000 11.860000
item_40 7.100000 6.620000 0.480000 6.740000
item_41 5.670000 5.250000 0.420000 7.330000
item_42 7.470000 7.040000 0.430000 5.770000
item_43 8.470000 8.590000 -0.120000 -1.430000
item_44 6.980000 6.420000 0.560000 7.960000
item_45 12.490000 11.720000 0.770000 6.200000
item_46 10.390000 9.770000 0.620000 5.950000
item_47 5.500000 4.990000 0.510000 9.320000
item_48 9.440000 8.320000 1.110000 11.780000
item_49 6.300000 5.910000 0.390000 6.200000
item_50 11.920000 10.690000 1.230000 10.300000
# Average improvement for all items
# ======================================================================================
results[['improvement', 'improvement_(%)']].agg(['mean', 'min', 'max'])
improvement improvement_(%)
mean 0.7016 7.4626
min -0.1200 -1.4300
max 1.4400 15.7600
# Number of series with positive and negative improvement
# ======================================================================================
pd.Series(
    np.where(results['improvement_(%)'] < 0, 'negative', 'positive')
).value_counts()
positive    49
negative     1
Name: count, dtype: int64

The global model achieves an average improvement of approximately 7.5% compared to using an individual model for each series. For 49 of the 50 series, the prediction error evaluated by backtesting is lower when the global model is used, and in the remaining one it is only 1.4% higher. This use case demonstrates that a multi-series model can have advantages over multiple individual models when forecasting time series that follow similar dynamics. In addition to the potential improvements in forecasting, it is also important to consider the benefit of having only one model to maintain.

⚠️ Warning

This comparison was made without optimizing the model hyperparameters. See the Hyperparameter tuning and lags selection section to verify that the conclusions hold when the models are tuned with the best combination of hyperparameters and lags.

Series with different lengths and different exogenous variables

When faced with a multi-series forecasting problem, it is common for the series to have varying lengths due to differences in the starting times of data recording. To address this scenario, the ForecasterRecursiveMultiSeries allows the simultaneous modeling of time series of different lengths and using different exogenous variables. In these cases, the data must be structured in one of the following ways:

  • In a Python dictionary of pandas.Series: the keys of the dictionary are the names of the series and the values are the series themselves. All series must be of type pandas.Series, have a DatetimeIndex index, and have the same frequency.

  • In a pandas.DataFrame with MultiIndex: the first level of the index contains the series name and the second level is the time index. The second level must be a DatetimeIndex and all series must have the same frequency.

Series values Allowed
[NaN, NaN, NaN, NaN, 4, 5, 6, 7, 8, 9] ✔️
[0, 1, 2, 3, 4, 5, 6, 7, 8, NaN] ✔️
[0, 1, 2, 3, 4, NaN, 6, 7, 8, 9] ✔️
[NaN, NaN, 2, 3, 4, NaN, 6, 7, 8, 9] ✔️



When different exogenous variables are used for each series, or if the exogenous variables are the same but have different values for each series, they must be stored in one of the following ways:

  • In a dictionary: The keys of the dictionary are the names of the series and the values are the exogenous variables themselves. All exogenous variables must be of type pandas.DataFrame or pandas.Series.

  • In a pandas.DataFrame with MultiIndex: where the first level of the index contains the series name and the second level is the time index. All exogenous variables must be of type pandas.DataFrame or pandas.Series.

If the data does not follow these structures, skforecast provides two functions to transform the data: reshape_series_long_to_dict and reshape_exog_long_to_dict.

💡 Tip

In terms of performance, using a dictionary is more efficient than a pandas DataFrame, either wide or long format, especially for larger datasets. This is because dictionaries enable faster access and manipulation of individual time series, without the structural overhead associated with DataFrames.

Data

The data for this example consists of five daily demo time series (id_1000 to id_1004) with different lengths, together with calendar (day of the week) and weather (air temperature and wind speed) exogenous variables. The data is stored in "long format" in a single DataFrame. The series_id column identifies the series to which each observation belongs. The timestamp column contains the date of the observation, and the value column contains the value of the series at that date. Each time series is of a different length. The exogenous variables are stored in a separate DataFrame, also in "long format". The column series_id identifies the series to which each observation belongs. The column timestamp contains the date of the observation, and the remaining columns contain the values of the exogenous variables at that date.

# Load time series of multiple lengths and exogenous variables
# ==============================================================================
series = pd.read_csv(
    'https://raw.githubusercontent.com/skforecast/skforecast-datasets/main/data/demo_multi_series.csv'
)
exog = pd.read_csv(
    'https://raw.githubusercontent.com/skforecast/skforecast-datasets/main/data/demo_multi_series_exog.csv'
)
series['timestamp'] = pd.to_datetime(series['timestamp'])
exog['timestamp'] = pd.to_datetime(exog['timestamp'])
display(series.head())
print('')
display(exog.head())
series_id timestamp value
0 id_1000 2016-01-01 1012.500694
1 id_1000 2016-01-02 1158.500099
2 id_1000 2016-01-03 983.000099
3 id_1000 2016-01-04 1675.750496
4 id_1000 2016-01-05 1586.250694

series_id timestamp sin_day_of_week cos_day_of_week air_temperature wind_speed
0 id_1000 2016-01-01 -0.433884 -0.900969 6.416639 4.040115
1 id_1000 2016-01-02 -0.974928 -0.222521 6.366474 4.530395
2 id_1000 2016-01-03 -0.781831 0.623490 6.555272 3.273064
3 id_1000 2016-01-04 0.000000 1.000000 6.704778 4.865404
4 id_1000 2016-01-05 0.781831 0.623490 2.392998 5.228913

When series have different lengths, it is recommended to transform the data into a dictionary (a long DataFrame with a MultiIndex is also accepted). The keys of the dictionary are the names of the series and the values are the series themselves. To do this, the reshape_series_long_to_dict function is used, which takes the DataFrame in "long format" and returns a dict of series.

Similarly, when the exogenous variables are different (values or variables) for each series, it is recommended to store them in a dictionary. The keys of the dictionary are the names of the series and the values are the exogenous variables themselves. The reshape_exog_long_to_dict function is used, which takes the DataFrame in "long format" and returns a dict of exogenous variables.

# Transform series and exog to dictionaries
# ==============================================================================
series_dict = reshape_series_long_to_dict(
    data      = series,
    series_id = 'series_id',
    index     = 'timestamp',
    values    = 'value',
    freq      = 'D'
)

exog_dict = reshape_exog_long_to_dict(
    data      = exog,
    series_id = 'series_id',
    index     = 'timestamp',
    freq      = 'D'
)
╭──────────────────────────────── MissingValuesWarning ────────────────────────────────╮
│ Series 'id_1003' is incomplete. NaNs have been introduced after setting the          │
│ frequency.                                                                           │
│                                                                                      │
│ Category : skforecast.exceptions.MissingValuesWarning                                │
│ Location :                                                                           │
│ C:\Users\Joaquin\miniconda3\envs\skforecast_27_py13\Lib\site-packages\skforecast\pre │
│ processing\_preprocessing.py:592                                                     │
│ Suppress : warnings.simplefilter('ignore', category=MissingValuesWarning)            │
╰──────────────────────────────────────────────────────────────────────────────────────╯

The MissingValuesWarning indicates that series 'id_1003' has gaps: the missing dates are filled with NaN when the daily frequency is set (73 values in the training set and 73 in the test set, see the description of each series below). Rows whose target is NaN are always removed from the training matrices. The argument dropna_from_series controls the rest: with True, every row with at least one NaN among the predictors is also removed; with False (used in this example), those rows are kept. In this example, this matters because series 'id_1000' and 'id_1003' lack some exogenous variables, so all of their rows have at least one NaN and would be discarded with True. Keeping them is possible because HistGradientBoostingRegressor supports missing values.

Some exogenous variables are omitted for series 'id_1000' and 'id_1003' to illustrate that different exogenous variables can be used for each series.

# Drop some exogenous variables for series 'id_1000' and 'id_1003'
# ==============================================================================
exog_dict['id_1000'] = exog_dict['id_1000'].drop(
    columns=['air_temperature', 'wind_speed']
)
exog_dict['id_1003'] = exog_dict['id_1003'].drop(columns=['cos_day_of_week'])
# Partition data in train and test
# ==============================================================================
end_train = '2016-07-31 23:59:00'
series_dict_train = {k: v.loc[:end_train] for k, v in series_dict.items()}
exog_dict_train   = {k: v.loc[:end_train] for k, v in exog_dict.items()}
series_dict_test  = {k: v.loc[end_train:] for k, v in series_dict.items()}
exog_dict_test    = {k: v.loc[end_train:] for k, v in exog_dict.items()}
# Plot series
# ==============================================================================
colors = plt.rcParams['axes.prop_cycle'].by_key()['color']
fig, axs = plt.subplots(5, 1, figsize=(8, 4), sharex=True)
for i, s in enumerate(series_dict.values()):
    axs[i].plot(s, label=s.name, color=colors[i])
    axs[i].legend(loc='upper right', fontsize=8)
    axs[i].tick_params(axis='both', labelsize=8)
    axs[i].axvline(
        pd.to_datetime(end_train), color='white', linestyle='--', linewidth=1
    )
fig.tight_layout()
plt.show()
No description has been provided for this image
# Description of each series
# ==============================================================================
for k in series_dict.keys():
    print(f'{k}:')
    try:
        print(
            f'\tTrain: len={len(series_dict_train[k])}, {series_dict_train[k].index[0]}'
            f' --- {series_dict_train[k].index[-1]} '
            f' (missing={series_dict_train[k].isnull().sum()})'
        )
    except IndexError:
        print('\tTrain: len=0')
    try:
        print(
            f'\tTest : len={len(series_dict_test[k])}, {series_dict_test[k].index[0]}'
            f' --- {series_dict_test[k].index[-1]} '
            f' (missing={series_dict_test[k].isnull().sum()})'
        )
    except IndexError:
        print('\tTest : len=0')
id_1000:
	Train: len=213, 2016-01-01 00:00:00 --- 2016-07-31 00:00:00  (missing=0)
	Test : len=153, 2016-08-01 00:00:00 --- 2016-12-31 00:00:00  (missing=0)
id_1001:
	Train: len=30, 2016-07-02 00:00:00 --- 2016-07-31 00:00:00  (missing=0)
	Test : len=153, 2016-08-01 00:00:00 --- 2016-12-31 00:00:00  (missing=0)
id_1002:
	Train: len=183, 2016-01-01 00:00:00 --- 2016-07-01 00:00:00  (missing=0)
	Test : len=0
id_1003:
	Train: len=213, 2016-01-01 00:00:00 --- 2016-07-31 00:00:00  (missing=73)
	Test : len=153, 2016-08-01 00:00:00 --- 2016-12-31 00:00:00  (missing=73)
id_1004:
	Train: len=91, 2016-05-02 00:00:00 --- 2016-07-31 00:00:00  (missing=0)
	Test : len=31, 2016-08-01 00:00:00 --- 2016-08-31 00:00:00  (missing=0)
# Exogenous variables for each series
# ==============================================================================
for k in series_dict.keys():
    print(f'{k}:')
    print(f'\t{exog_dict[k].columns.to_list()}')
id_1000:
	['sin_day_of_week', 'cos_day_of_week']
id_1001:
	['sin_day_of_week', 'cos_day_of_week', 'air_temperature', 'wind_speed']
id_1002:
	['sin_day_of_week', 'cos_day_of_week', 'air_temperature', 'wind_speed']
id_1003:
	['sin_day_of_week', 'air_temperature', 'wind_speed']
id_1004:
	['sin_day_of_week', 'cos_day_of_week', 'air_temperature', 'wind_speed']

Global model

# Fit forecaster
# ==============================================================================
estimator = HistGradientBoostingRegressor(random_state=123, max_depth=5)
window_features = RollingFeatures(stats=['mean', 'min', 'max'], window_sizes=7)
forecaster = ForecasterRecursiveMultiSeries(
                estimator          = estimator,
                lags               = 14,
                window_features    = window_features,
                encoding           = 'ordinal',
                dropna_from_series = False
            )

forecaster.fit(series=series_dict_train, exog=exog_dict_train, suppress_warnings=True)
forecaster

ForecasterRecursiveMultiSeries

General Information
  • Estimator: HistGradientBoostingRegressor
  • Lags: [ 1 2 3 4 5 6 7 8 9 10 11 12 13 14]
  • Window features: ['roll_mean_7', 'roll_min_7', 'roll_max_7']
  • Calendar features: None
  • Window size: 14
  • Series encoding: ordinal
  • Exogenous included: True
  • Categorical features: auto
  • Weight function included: False
  • Series weights: None
  • Differentiation order: None
  • Drop NaN from series: False
  • Creation date: 2026-11-11 21:37:35
  • Last fit date: 2026-11-11 21:37:35
  • Skforecast version: 0.27.0
  • Python version: 3.13.18
  • Forecaster id: None
Exogenous Variables

sin_day_of_week, cos_day_of_week, air_temperature, wind_speed

Data Transformations
  • Transformer for series: None
  • Transformer for exog: None
Training Information
  • Series names (levels): id_1000, id_1001, id_1002, id_1003, id_1004
  • Training range: 'id_1000': ['2016-01-01', '2016-07-31'], 'id_1001': ['2016-07-02', '2016-07-31'], 'id_1002': ['2016-01-01', '2016-07-01'], 'id_1003': ['2016-01-01', '2016-07-31'], 'id_1004': ['2016-05-02', '2016-07-31']
  • Training index type: DatetimeIndex
  • Training index frequency: D
Estimator Parameters
    {'categorical_features': 'from_dtype', 'early_stopping': 'auto', 'interaction_cst': None, 'l2_regularization': 0.0, 'learning_rate': 0.1, 'loss': 'squared_error', 'max_bins': 255, 'max_depth': 5, 'max_features': 1.0, 'max_iter': 100, 'max_leaf_nodes': 31, 'min_samples_leaf': 20, 'monotonic_cst': None, 'n_iter_no_change': 10, 'quantile': None, 'random_state': 123, 'scoring': 'loss', 'tol': 1e-07, 'validation_fraction': 0.1, 'verbose': 0, 'warm_start': False}
Fit Kwargs
    {}

📖 API Reference    📝 User Guide

Only series whose last window of data ends at the same datetime index can be predicted together. If levels = None, series that do not reach the maximum index are excluded from prediction. In this example, series 'id_1002' is excluded.

# Predict
# ==============================================================================
predictions = forecaster.predict(steps=7, exog=exog_dict_test, suppress_warnings=True)
predictions
level pred
2016-08-01 id_1000 1434.796453
2016-08-01 id_1001 3087.267824
2016-08-01 id_1003 2713.304570
2016-08-01 id_1004 7047.329003
2016-08-02 id_1000 1460.038838
2016-08-02 id_1001 3590.816304
2016-08-02 id_1003 2282.892693
2016-08-02 id_1004 7244.170674
2016-08-03 id_1000 1397.298376
2016-08-03 id_1001 3342.230060
2016-08-03 id_1003 2040.577134
2016-08-03 id_1004 8238.670449
2016-08-04 id_1000 1364.350567
2016-08-04 id_1001 3295.089642
2016-08-04 id_1003 2047.429891
2016-08-04 id_1004 9004.924781
2016-08-05 id_1000 1315.912259
2016-08-05 id_1001 3187.266550
2016-08-05 id_1003 2125.307896
2016-08-05 id_1004 8829.729109
2016-08-06 id_1000 958.355479
2016-08-06 id_1001 2675.879194
2016-08-06 id_1003 2119.344835
2016-08-06 id_1004 6614.262906
2016-08-07 id_1000 958.530378
2016-08-07 id_1001 2000.102709
2016-08-07 id_1003 2730.302981
2016-08-07 id_1004 5631.517923

Backtesting

When series have different lengths, a series is only predicted in the folds in which its last window is available. Series 'id_1002' ends before the test period, so it has no predictions and its metric is NaN. Series 'id_1004' ends on 2016-08-31, in the middle of a fold, so its predictions continue until the end of that fold. Dates without observed values (including the gaps in 'id_1003') are ignored when computing the metric.

# Backtesting
# ==============================================================================
cv = TimeSeriesFold(
        steps              = 7,
        initial_train_size = end_train,
        refit              = False,
     )

metrics_levels, backtest_predictions = backtesting_forecaster_multiseries(
    forecaster            = forecaster,
    series                = series_dict,
    exog                  = exog_dict,
    cv                    = cv,
    metric                = 'mean_absolute_error',
    add_aggregated_metric = False,
    verbose               = True,
    suppress_warnings     = True
)

display(metrics_levels)
print('')
display(backtest_predictions)
Information of folds
--------------------
Number of observations used for initial training: 213
Number of observations used for backtesting: 153
    Number of folds: 22
    Number skipped folds: 0 
    Number of steps per fold: 7
    Number of steps to exclude between last observed data (last window) and predictions (gap): 0
    Last fold only includes 6 observations.

Fold: 0
    Training:   2016-01-01 00:00:00 -- 2016-07-31 00:00:00  (n=213)
    Validation: 2016-08-01 00:00:00 -- 2016-08-07 00:00:00  (n=7)
Fold: 1
    Training:   No training in this fold
    Validation: 2016-08-08 00:00:00 -- 2016-08-14 00:00:00  (n=7)
Fold: 2
    Training:   No training in this fold
    Validation: 2016-08-15 00:00:00 -- 2016-08-21 00:00:00  (n=7)
Fold: 3
    Training:   No training in this fold
    Validation: 2016-08-22 00:00:00 -- 2016-08-28 00:00:00  (n=7)
Fold: 4
    Training:   No training in this fold
    Validation: 2016-08-29 00:00:00 -- 2016-09-04 00:00:00  (n=7)
Fold: 5
    Training:   No training in this fold
    Validation: 2016-09-05 00:00:00 -- 2016-09-11 00:00:00  (n=7)
Fold: 6
    Training:   No training in this fold
    Validation: 2016-09-12 00:00:00 -- 2016-09-18 00:00:00  (n=7)
Fold: 7
    Training:   No training in this fold
    Validation: 2016-09-19 00:00:00 -- 2016-09-25 00:00:00  (n=7)
Fold: 8
    Training:   No training in this fold
    Validation: 2016-09-26 00:00:00 -- 2016-10-02 00:00:00  (n=7)
Fold: 9
    Training:   No training in this fold
    Validation: 2016-10-03 00:00:00 -- 2016-10-09 00:00:00  (n=7)
Fold: 10
    Training:   No training in this fold
    Validation: 2016-10-10 00:00:00 -- 2016-10-16 00:00:00  (n=7)
Fold: 11
    Training:   No training in this fold
    Validation: 2016-10-17 00:00:00 -- 2016-10-23 00:00:00  (n=7)
Fold: 12
    Training:   No training in this fold
    Validation: 2016-10-24 00:00:00 -- 2016-10-30 00:00:00  (n=7)
Fold: 13
    Training:   No training in this fold
    Validation: 2016-10-31 00:00:00 -- 2016-11-06 00:00:00  (n=7)
Fold: 14
    Training:   No training in this fold
    Validation: 2016-11-07 00:00:00 -- 2016-11-13 00:00:00  (n=7)
Fold: 15
    Training:   No training in this fold
    Validation: 2016-11-14 00:00:00 -- 2016-11-20 00:00:00  (n=7)
Fold: 16
    Training:   No training in this fold
    Validation: 2016-11-21 00:00:00 -- 2016-11-27 00:00:00  (n=7)
Fold: 17
    Training:   No training in this fold
    Validation: 2016-11-28 00:00:00 -- 2016-12-04 00:00:00  (n=7)
Fold: 18
    Training:   No training in this fold
    Validation: 2016-12-05 00:00:00 -- 2016-12-11 00:00:00  (n=7)
Fold: 19
    Training:   No training in this fold
    Validation: 2016-12-12 00:00:00 -- 2016-12-18 00:00:00  (n=7)
Fold: 20
    Training:   No training in this fold
    Validation: 2016-12-19 00:00:00 -- 2016-12-25 00:00:00  (n=7)
Fold: 21
    Training:   No training in this fold
    Validation: 2016-12-26 00:00:00 -- 2016-12-31 00:00:00  (n=6)

levels mean_absolute_error n_evaluated
0 id_1000 150.894697 153
1 id_1001 863.158413 153
2 id_1002 NaN 0
3 id_1003 265.134384 80
4 id_1004 921.148294 31

level fold step y_true pred
2016-08-01 id_1000 0 1 1555.500793 1434.796453
2016-08-01 id_1001 0 1 2844.000000 3087.267824
2016-08-01 id_1003 0 1 2618.750099 2713.304570
2016-08-01 id_1004 0 1 8768.003021 7047.329003
2016-08-02 id_1000 0 2 1477.750496 1460.038838
... ... ... ... ... ...
2016-12-30 id_1001 21 5 108.000000 1082.426104
2016-12-30 id_1003 21 5 1939.250000 1924.893045
2016-12-31 id_1000 21 6 1134.000000 1334.693872
2016-12-31 id_1001 21 6 125.000000 1057.469518
2016-12-31 id_1003 21 6 1964.500000 1908.610089

494 rows × 5 columns

# Plot backtesting predictions
# ==============================================================================
colors = plt.rcParams['axes.prop_cycle'].by_key()['color']
fig, axs = plt.subplots(5, 1, figsize=(8, 4), sharex=True)
for i, s in enumerate(series_dict.keys()):
    axs[i].plot(series_dict[s], label=series_dict[s].name, color=colors[i])
    axs[i].axvline(
        pd.to_datetime(end_train), color='white', linestyle='--', linewidth=1
    )
    axs[i].plot(
        backtest_predictions.loc[backtest_predictions['level'] == s, 'pred'],
        label='prediction',
        color='white',
    )
    axs[i].legend(loc='upper left', fontsize=8)
    axs[i].tick_params(axis='both', labelsize=8)
fig.tight_layout()
plt.show()
No description has been provided for this image

⚠️ Warning

The calendar variables (sin_day_of_week, cos_day_of_week) are known in advance, but air_temperature and wind_speed are observed values. In a real forecasting scenario, their future values are not known at prediction time and must be replaced by weather forecasts, which have their own error. The predictions and backtesting metrics shown above are therefore optimistic.

By allowing the modeling of time series of different lengths and with different exogenous variables, the ForecasterRecursiveMultiSeries class provides a flexible and powerful tool for using all available information to train the forecasting models.

Hyperparameter tuning and lags selection

In the first example of this document, the comparison between forecasters was done without optimizing the hyperparameters of the estimators. To make a fair comparison, a Bayesian search strategy (bayesian_search_forecaster and bayesian_search_forecaster_multiseries) is used in order to select the best configuration for each forecaster. See more information on hyperparameter tuning and lags selection.

✏️ Note

Hyperparameter tuning for several models can be computationally expensive. In order to speed up the process, the evaluation of each candidate configuration is done using one-step-ahead (OneStepAheadFold) instead of backtesting. For more details of the advantages and limitations of this approach, see the One-step-ahead validation.

# Hyperparameter search for each single series model
# ======================================================================================
items = []
mae_values = []

def search_space(trial):
    search_space = {
        'lags'          : trial.suggest_categorical('lags', [7, 14]),
        'max_iter'      : trial.suggest_int('max_iter', 100, 500),
        'max_depth'     : trial.suggest_categorical('max_depth', [None, 5, 6, 7, 8, 9, 10]),
        'learning_rate' : trial.suggest_float('learning_rate', 0.01, 0.1, log=True)
    }

    return search_space

cv_search = OneStepAheadFold(initial_train_size=len(data_train))
cv_backtesting = TimeSeriesFold(
                    steps              = 7,
                    initial_train_size = len(data_train) + len(data_val),
                    refit              = False,
                 )
warnings.simplefilter('ignore', category=OneStepAheadValidationWarning)

for item in tqdm(data.columns):

    window_features = RollingFeatures(stats=['mean', 'min', 'max'], window_sizes=7)
    forecaster = ForecasterRecursive(
                    estimator       = HistGradientBoostingRegressor(random_state=123),
                    lags            = 14,
                    window_features = window_features
                )

    # return_best=True (default): after the search, the forecaster is set and
    # retrained with the best configuration found
    bayesian_search_forecaster(
        forecaster    = forecaster,
        y             = data.loc[:end_val, item],
        cv            = cv_search,
        search_space  = search_space,
        n_trials      = 10,
        metric        = 'mean_absolute_error',
        show_progress = False
    )
    metric, _ = backtesting_forecaster(
                        forecaster    = forecaster,
                        y             = data[item],
                        cv            = cv_backtesting,
                        metric        = 'mean_absolute_error',
                        show_progress = False
                    )

    items.append(item)
    mae_values.append(metric.at[0, 'mean_absolute_error'])

uni_series_mae = pd.Series(
                    data  = mae_values,
                    index = items,
                    name  = 'uni_series_mae'
                )
# Hyperparameter search for the multi-series model and backtesting for each item
# ======================================================================================
# search_space, cv_search, cv_backtesting and window_features are the same as
# in the single series search
estimator = HistGradientBoostingRegressor(random_state=123)
forecaster_ms = ForecasterRecursiveMultiSeries(
                    estimator          = estimator,
                    lags               = 14,
                    window_features    = window_features,
                    transformer_series = StandardScaler(),
                    encoding           = 'ordinal'
                )

# return_best=True (default): after the search, the forecaster is set and
# retrained with the best configuration found
bayesian_search_forecaster_multiseries(
    forecaster    = forecaster_ms,
    series        = data.loc[:end_val, :],
    cv            = cv_search,
    search_space  = search_space,
    n_trials      = 20,
    metric        = 'mean_absolute_error',
    show_progress = False
)

multi_series_mae, _ = backtesting_forecaster_multiseries(
                                       forecaster            = forecaster_ms,
                                       series                = data,
                                       cv                    = cv_backtesting,
                                       metric                = 'mean_absolute_error',
                                       add_aggregated_metric = False,
                                   )
╭────────────────────────────────── InputTypeWarning ──────────────────────────────────╮
│ Passing a DataFrame (either wide or long format) as `series` requires additional     │
│ internal transformations, which can increase computational time. It is recommended   │
│ to use a dictionary of pandas Series instead. For more details, see:                 │
│ https://skforecast.org/latest/user_guides/independent-multi-time-series-forecasting. │
│ html#input-data                                                                      │
│                                                                                      │
│ Category : skforecast.exceptions.InputTypeWarning                                    │
│ Location :                                                                           │
│ C:\Users\Joaquin\miniconda3\envs\skforecast_27_py13\Lib\site-packages\skforecast\uti │
│ ls\utils.py:4425                                                                     │
│ Suppress : warnings.simplefilter('ignore', category=InputTypeWarning)                │
╰──────────────────────────────────────────────────────────────────────────────────────╯
╭────────────────────────────────── InputTypeWarning ──────────────────────────────────╮
│ Passing a DataFrame (either wide or long format) as `series` requires additional     │
│ internal transformations, which can increase computational time. It is recommended   │
│ to use a dictionary of pandas Series instead. For more details, see:                 │
│ https://skforecast.org/latest/user_guides/independent-multi-time-series-forecasting. │
│ html#input-data                                                                      │
│                                                                                      │
│ Category : skforecast.exceptions.InputTypeWarning                                    │
│ Location :                                                                           │
│ C:\Users\Joaquin\miniconda3\envs\skforecast_27_py13\Lib\site-packages\skforecast\uti │
│ ls\utils.py:4425                                                                     │
│ Suppress : warnings.simplefilter('ignore', category=InputTypeWarning)                │
╰──────────────────────────────────────────────────────────────────────────────────────╯
# Difference in backtesting metric for each item
# ======================================================================================
multi_series_mae = (
    multi_series_mae.set_index('levels')['mean_absolute_error']
    .rename('multi_series_mae')
)
results = pd.concat((uni_series_mae, multi_series_mae), axis=1)
results['improvement'] = results['uni_series_mae'] - results['multi_series_mae']
results['improvement_(%)'] = (
    100 * results['improvement'] / results['uni_series_mae']
)
results = results.round(2)

# Average improvement for all items
# ======================================================================================
results[['improvement', 'improvement_(%)']].agg(['mean', 'min', 'max'])
improvement improvement_(%)
mean 0.4862 5.2346
min -0.0200 -0.2800
max 1.2800 13.6400
# Number of series with positive and negative improvement
# ======================================================================================
pd.Series(
    np.where(results['improvement_(%)'] < 0, 'negative', 'positive')
).value_counts()
positive    48
negative     2
Name: count, dtype: int64

After tuning the lags and hyperparameters of both approaches, the average improvement of the multi-series model decreases from 7.5% to 5.2%. Even so, the multi-series model still provides better results for 48 of the 50 items, and in the other two the difference is negligible.

Feature selection

Feature selection is the process of selecting a subset of relevant features (variables, predictors) for use in model construction. Feature selection techniques are used for several reasons: to simplify models to make them easier to interpret, to reduce training time, to avoid the curse of dimensionality, to improve generalization by reducing overfitting (formally, variance reduction), and others.

Skforecast is compatible with the feature selection methods implemented in the scikit-learn library. There are several methods for feature selection, but the most common are:

  • Recursive feature elimination (RFE)

  • Sequential Feature Selection (SFS)

  • Feature selection based on threshold (SelectFromModel)

See the feature selection user guide for examples of how to apply these methods to skforecast forecasters with the select_features and select_features_multiseries functions.

💡 Tip

Feature selection is a powerful tool for improving the performance of machine learning models. However, it is computationally expensive and can be time-consuming. Since the goal is to find the best subset of features, not the best model, it is not necessary to use the entire data set or a highly complex model. Instead, it is recommended to use a small subset of the data and a simple model. Once the best subset of features has been identified, the model can then be trained using the entire dataset and a more complex configuration.

Weights in multi-series

The weights are used to control the influence that each observation has on the training of the model. ForecasterRecursiveMultiSeries accepts two types of weights:

  • series_weights controls the relative importance of each series. If a series has twice as much weight as the others, the observations of that series influence the training twice as much. The higher the weight of a series relative to the others, the more the model will focus on trying to learn that series.

  • weight_func controls the relative importance of each observation according to its index value. For example, a function that assigns a lower weight to certain dates.

If the two types of weights are indicated, they are multiplied to create the final weights as shown in the figure. The resulting sample_weight cannot have negative values.

No description has been provided for this image

Learn more about weights in multi-series forecasting and weighted time series forecasting with skforecast.

In this example, item_1 has higher relative importance among series (it weighs 3 times more than the rest of the series), and observations between '2013-12-01' and '2014-01-31' are considered non-representative and a weight of 0 is applied to them. Since only item_1 is included in series_weights, skforecast raises an IgnoredArgumentWarning to inform that the remaining series receive a weight of 1; this is the expected behavior.

# Weights in ForecasterRecursiveMultiSeries
# ======================================================================================
# Weights for each series
series_weights = {'item_1': 3.0}  # Series not present in the dict have weight 1

# Weights for each index
def custom_weights(index):
    """
    Return 0 if index is between '2013-12-01' and '2014-01-31', 1 otherwise.
    """
    weights = np.where(
                  (index >= '2013-12-01') & (index <= '2014-01-31'),
                   0,
                   1
              )

    return weights

forecaster = ForecasterRecursiveMultiSeries(
                 estimator          = HistGradientBoostingRegressor(random_state=123),
                 lags               = 14,
                 transformer_series = StandardScaler(),
                 encoding           = 'ordinal',
                 weight_func        = custom_weights,
                 series_weights     = series_weights
             )

forecaster.fit(series=data)
forecaster.predict(steps=7).head(3)
╭────────────────────────────────── InputTypeWarning ──────────────────────────────────╮
│ Passing a DataFrame (either wide or long format) as `series` requires additional     │
│ internal transformations, which can increase computational time. It is recommended   │
│ to use a dictionary of pandas Series instead. For more details, see:                 │
│ https://skforecast.org/latest/user_guides/independent-multi-time-series-forecasting. │
│ html#input-data                                                                      │
│                                                                                      │
│ Category : skforecast.exceptions.InputTypeWarning                                    │
│ Location :                                                                           │
│ C:\Users\Joaquin\miniconda3\envs\skforecast_27_py13\Lib\site-packages\skforecast\uti │
│ ls\utils.py:4425                                                                     │
│ Suppress : warnings.simplefilter('ignore', category=InputTypeWarning)                │
╰──────────────────────────────────────────────────────────────────────────────────────╯
╭─────────────────────────────── IgnoredArgumentWarning ───────────────────────────────╮
│ {'item_43', 'item_2', 'item_41', 'item_19', 'item_12', 'item_17', 'item_45',         │
│ 'item_35', 'item_49', 'item_44', 'item_16', 'item_23', 'item_3', 'item_50',          │
│ 'item_20', 'item_8', 'item_42', 'item_24', 'item_15', 'item_36', 'item_13',          │
│ 'item_34', 'item_14', 'item_31', 'item_11', 'item_37', 'item_30', 'item_4',          │
│ 'item_47', 'item_18', 'item_27', 'item_6', 'item_48', 'item_29', 'item_25',          │
│ 'item_10', 'item_5', 'item_22', 'item_40', 'item_38', 'item_26', 'item_21',          │
│ 'item_46', 'item_32', 'item_39', 'item_9', 'item_28', 'item_33', 'item_7'} not       │
│ present in `series_weights`. A weight of 1 is given to all their samples.            │
│                                                                                      │
│ Category : skforecast.exceptions.IgnoredArgumentWarning                              │
│ Location :                                                                           │
│ C:\Users\Joaquin\miniconda3\envs\skforecast_27_py13\Lib\site-packages\skforecast\rec │
│ ursive\_forecaster_recursive_multiseries.py:2227                                     │
│ Suppress : warnings.simplefilter('ignore', category=IgnoredArgumentWarning)          │
╰──────────────────────────────────────────────────────────────────────────────────────╯
level pred
2018-01-01 item_1 20.663257
2018-01-01 item_2 55.695596
2018-01-01 item_3 35.783077

✏️ Note

A dictionary can be passed to weight_func to apply different functions for each series. If a series is not present in the dictionary, it will have weight 1.

Predict new series (unknown series)

ForecasterRecursiveMultiSeries allows predicting unknown series (series not seen during the training process). Two scenarios may occur:

  • There is historical data for the new series: The user must provide a DataFrame with the historical data required to create the features (lags and window features) used during the prediction process. This DataFrame must contain one column per series to be predicted. It is possible to include both series seen during training and new (unseen) series.

  • There is no historical data for the new series: This scenario is similar, but the last_window for the new series is composed entirely of NaN values. This approach is only possible when using estimators that support missing values, since all lagged features will be NaN.

In both cases, exogenous variables may be available or not. If they are not available, they will be automatically set to NaN. Since the new series were not seen during training, their identifier cannot be encoded either: it is also set to NaN, and skforecast raises an UnknownLevelWarning to inform about it. The estimator must therefore accept missing values (HistGradientBoostingRegressor does).

✏️ Note

Predictions for unknown series are built from the values of their last window. Tree-based estimators cannot extrapolate beyond the range of values seen during training, so the new series should be on a scale similar to that of the training data; otherwise, all of them saturate the model and receive nearly identical predictions. When no historical data is available, all unknown series share the same features (all NaN values), so they receive identical predictions.

# Predict new series with available historical data
# ==============================================================================
# Create the last window with the last 14 observations of the new series
last_window_new = pd.DataFrame(
    {
        'series_new_1': [25.0, 31.0, 28.0, 35.0, 41.0, 38.0, 26.0,
                         27.0, 33.0, 29.0, 36.0, 43.0, 40.0, 28.0],
        'series_new_2': [65.0, 78.0, 72.0, 83.0, 95.0, 91.0, 68.0,
                         67.0, 80.0, 74.0, 86.0, 98.0, 92.0, 70.0]
    },
    index=pd.date_range(end=data.index[-1], periods=14, freq='D'),
)

forecaster.predict(steps=5, last_window=last_window_new)
╭──────────────────────────────── UnknownLevelWarning ─────────────────────────────────╮
│ `levels` {'series_new_1', 'series_new_2'} were not included in training. Unknown     │
│ levels are encoded as NaN, which may cause the prediction to fail if the estimator   │
│ does not accept NaN values.                                                          │
│                                                                                      │
│ Category : skforecast.exceptions.UnknownLevelWarning                                 │
│ Location :                                                                           │
│ C:\Users\Joaquin\miniconda3\envs\skforecast_27_py13\Lib\site-packages\skforecast\uti │
│ ls\utils.py:1502                                                                     │
│ Suppress : warnings.simplefilter('ignore', category=UnknownLevelWarning)             │
╰──────────────────────────────────────────────────────────────────────────────────────╯
level pred
2018-01-01 series_new_1 33.090028
2018-01-01 series_new_2 67.291398
2018-01-02 series_new_1 37.125181
2018-01-02 series_new_2 76.880932
2018-01-03 series_new_1 36.017066
2018-01-03 series_new_2 76.429767
2018-01-04 series_new_1 42.044894
2018-01-04 series_new_2 87.338898
2018-01-05 series_new_1 45.824647
2018-01-05 series_new_2 97.292583
# Predict new series without available historical data
# ==============================================================================
# Create the last window of the new series with NaN values
last_window_new = pd.DataFrame(
    {
        'series_new_1': np.nan,
        'series_new_2': np.nan
    },
    index=pd.date_range(end=data.index[-1], periods=14, freq='D'),
)

forecaster.predict(steps=5, last_window=last_window_new)
╭──────────────────────────────── UnknownLevelWarning ─────────────────────────────────╮
│ `levels` {'series_new_1', 'series_new_2'} were not included in training. Unknown     │
│ levels are encoded as NaN, which may cause the prediction to fail if the estimator   │
│ does not accept NaN values.                                                          │
│                                                                                      │
│ Category : skforecast.exceptions.UnknownLevelWarning                                 │
│ Location :                                                                           │
│ C:\Users\Joaquin\miniconda3\envs\skforecast_27_py13\Lib\site-packages\skforecast\uti │
│ ls\utils.py:1502                                                                     │
│ Suppress : warnings.simplefilter('ignore', category=UnknownLevelWarning)             │
╰──────────────────────────────────────────────────────────────────────────────────────╯
╭──────────────────────────────── MissingValuesWarning ────────────────────────────────╮
│ `last_window` has missing values. Most of machine learning models do not allow       │
│ missing values. Prediction method may either raise an error or return NaN            │
│ predictions.                                                                         │
│                                                                                      │
│ Category : skforecast.exceptions.MissingValuesWarning                                │
│ Location :                                                                           │
│ C:\Users\Joaquin\miniconda3\envs\skforecast_27_py13\Lib\site-packages\skforecast\uti │
│ ls\utils.py:1602                                                                     │
│ Suppress : warnings.simplefilter('ignore', category=MissingValuesWarning)            │
╰──────────────────────────────────────────────────────────────────────────────────────╯
level pred
2018-01-01 series_new_1 61.763950
2018-01-01 series_new_2 61.763950
2018-01-02 series_new_1 59.735558
2018-01-02 series_new_2 59.735558
2018-01-03 series_new_1 58.899508
2018-01-03 series_new_2 58.899508
2018-01-04 series_new_1 58.901234
2018-01-04 series_new_2 58.901234
2018-01-05 series_new_1 58.901234
2018-01-05 series_new_2 58.901234

Conclusions

This document has shown how global forecasting models can be used to model multiple time series with skforecast:

  • A single global model trained on the 50 sales series outperformed the individual models in 49 of the 50 series, with an average improvement of approximately 7.5% (5.2% after tuning the hyperparameters of both approaches, when it is better for 48 of the 50 series). This confirms that a multi-series model can have advantages over multiple individual models when forecasting time series that follow similar dynamics.

  • ForecasterRecursiveMultiSeries can combine series of different lengths, with missing values, and with different exogenous variables for each series.

  • The influence of each series and each observation on the model can be controlled with series_weights and weight_func.

  • Once trained, a global model can predict series not seen during training, provided that their recent history is available or the estimator supports missing values.

Beyond the potential improvements in forecasting accuracy, it is also important to take into consideration the benefit of having only one model to maintain.

Session information

import session_info
session_info.show(html=False)
-----
matplotlib          3.11.2
numpy               2.5.4
pandas              2.3.3
session_info        v1.0.1
skforecast          0.27.0
sklearn             1.9.1
tqdm                4.70.1
-----
IPython             9.17.1
jupyter_client      8.10.0
jupyter_core        5.9.1
-----
Python 3.13.18 | packaged by conda-forge | (main, Oct 26 2026, 11:30:40) [MSC v.1944 64 bit (AMD64)]
Windows-11-10.0.26200-SP0
-----
Session information updated at 2026-11-11 21:44

Citation

How to cite this document

If you use this document or any part of it, please acknowledge the source, thank you!

Global Forecasting Models: Modeling Multiple Time Series with Machine Learning by Joaquín Amat Rodrigo and Javier Escobar Ortiz, available under a CC BY-NC-SA 4.0 at https://www.cienciadedatos.net/documentos/py44-multi-series-forecasting-skforecast.html

How to cite skforecast

If you use skforecast for a publication, we would appreciate it if you cite the published software.

Zenodo:

Amat Rodrigo, Joaquin, & Escobar Ortiz, Javier. (2026). skforecast (v0.27.0). Zenodo. https://doi.org/10.5281/zenodo.8382787

APA:

Amat Rodrigo, J., & Escobar Ortiz, J. (2026). skforecast (Version 0.27.0) [Computer software]. https://doi.org/10.5281/zenodo.8382787

BibTeX:

@software{skforecast, author = {Amat Rodrigo, Joaquin and Escobar Ortiz, Javier}, title = {skforecast}, version = {0.27.0}, month = {11}, year = {2026}, license = {BSD-3-Clause}, url = {https://skforecast.org/}, doi = {10.5281/zenodo.8382787} }


Did you like the article? Your support is important

Your contribution will help me to continue generating free educational content. Many thanks! 😊

No description has been provided for this image

No description has been provided for this image

Become a GitHub Sponsor Become a GitHub Sponsor

Creative Commons Licence

This work by Joaquín Amat Rodrigo and Javier Escobar Ortiz is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International.

Allowed:

  • Share: copy and redistribute the material in any medium or format.

  • Adapt: remix, transform, and build upon the material.

Under the following terms:

  • Attribution: You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.

  • NonCommercial: You may not use the material for commercial purposes.

  • ShareAlike: If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original.