#11329·sktime

[BUG] Poor performance of global recursive reduction forecasters on panel data

Author: marrovCreated Sep 28, 2026Updated Sep 29, 2026
Labelsbug

(Partially) LLM generated content, by claude-opus-5.5

Describe the bug

On panel data, make_reduction(estimator, strategy="recursive", pooling="global") runs 60–400× slower than an equivalent pooled recursive model in mlforecast. Both libraries produce the same forecasts (max absolute difference ~1e-13). The regressor is not the bottleneck. With LinearRegression, and also with LightGBM in my real use case, more than 95% of the wall time is framework overhead. Most of that time is spent in predict.

Note: this is also true (and even worse) with the v2 reducers like RecursiveReductionForecaster. See below for details.

With 200 series × 60 periods, fit takes 0.5 s and predict takes 5.1 s. The time grows linearly with the number of series (see timings below). This makes rolling-origin backtesting and hyperparameter tuning of global reduction models impractical for panels of a few hundred series or more.

To Reproduce

python
import cProfile
import pstats
import time

import numpy as np
import pandas as pd
from mlforecast import MLForecast
from sklearn.linear_model import LinearRegression
from sktime.forecasting.compose import make_reduction

WINDOW, FH = 8, list(range(1, 9))


def fake_panel(n_series, n_periods=60, seed=0):
    rng = np.random.default_rng(seed)
    t = np.arange(n_periods)
    rows = []
    for i in range(n_series):
        y = 100 + 10 * np.sin(2 * np.pi * t / 4) + rng.normal(0, 5, n_periods).cumsum()
        rows.append(pd.DataFrame({"series": f"s{i}", "time": t, "y": y}))
    return pd.concat(rows, ignore_index=True)


def run_sktime(df):
    y = df.set_index(["series", "time"])[["y"]]
    f = make_reduction(LinearRegression(), strategy="recursive",
                       window_length=WINDOW, pooling="global")
    f.fit(y, fh=FH)
    return f.predict(fh=FH)


def run_mlforecast(df):
    long = df.rename(columns={"series": "unique_id", "time": "ds"})
    m = MLForecast(models={"lr": LinearRegression()}, freq=1,
                   lags=list(range(1, WINDOW + 1)))
    return m.fit(long).predict(len(FH))


for n_series in [50, 200, 800]:
    df = fake_panel(n_series)
    run_mlforecast(df)  # warm-up (numba compilation)
    t0 = time.perf_counter()
    p_sk = run_sktime(df)
    t_sk = time.perf_counter() - t0
    t0 = time.perf_counter()
    p_mlf = run_mlforecast(df)
    t_mlf = time.perf_counter() - t0
    # same model -> same forecasts
    a = p_sk["y"].to_numpy()
    b = p_mlf.sort_values(["unique_id", "ds"], key=lambda s: s.str[1:].astype(int)
                          if s.name == "unique_id" else s)["lr"].to_numpy()
    print(f"n_series={n_series:4d}  sktime={t_sk:6.2f}s  mlforecast={t_mlf:5.2f}s  "
          f"ratio={t_sk / t_mlf:5.0f}x  max|diff|={np.abs(a - b).max():.1e}")

# where does sktime spend its time? (200 series)
df = fake_panel(200)
prof = cProfile.Profile()
prof.enable()
run_sktime(df)
prof.disable()
stats = pstats.Stats(prof)
print(f"\nsktime, 200 series: {stats.total_calls:,} Python function calls")
stats.sort_stats("cumulative").print_stats(r"sktime|skbase", 15)

Output (Apple M-series, macOS, Python 3.12):

n_series=  50  sktime=  1.72s  mlforecast= 0.03s  ratio=   66x  max|diff|=0.0e+00
n_series= 200  sktime=  5.65s  mlforecast= 0.03s  ratio=  165x  max|diff|=2.3e-13
n_series= 800  sktime= 23.01s  mlforecast= 0.06s  ratio=  401x  max|diff|=1.7e-13

sktime, 200 series: 20,918,467 Python function calls
   ncalls  cumtime  filename:lineno(function)
        1   12.624  sktime/forecasting/base/_base.py:540(predict)
        1   12.592  sktime/forecasting/compose/_reduce.py:1008(_predict_last_window)
        8    8.903  sktime/forecasting/compose/_reduce.py:385(_get_shifted_window)
       17    6.839  sktime/forecasting/compose/_reduce.py:1844(_create_fcst_df)
       17    6.831  sktime/forecasting/base/_fh.py:764(get_expected_pred_idx)
        9    5.775  sktime/transformations/base/_base.py:537(transform)
     3400    5.416  sktime/forecasting/base/_fh.py:789(_make_y_pred)
       72    5.202  sktime/transformations/summarize/_window.py:435(_window_feature)
     3432    3.939  sktime/datatypes/_convert.py:195(convert_to)

(The profiled times are inflated by cProfile. Without profiling, fit = 0.54 s and predict = 5.08 s for 200 series.)

Where the time seems to go

Call counts during a single predict (200 series, fh=1..8):

function calls cumulative
sktime/forecasting/base/_fh.py::_make_y_pred 3,400 (= 200 series × 17) 6.1 s
sktime/datatypes/_utilities.py::get_cutoff 3,400 4.2 s
sktime/datatypes/_convert.py::convert_to 3,425 4.2 s
sktime/datatypes/_check.py::check_is_mtype 27,316 3.6 s
pandas.Series.__init__ 27,093 2.3 s
skbase ... get_params / get_tag ~27,400 each 0.7 s

Observations:

  1. Recursive predict loops over individual series in Python. For every recursion step, _predict_last_window calls _get_shifted_window and _create_fcst_df, and get_expected_pred_idx then calls _make_y_pred once per series. The number of calls therefore grows with n_series × horizon, rather than being one vectorised operation across the panel.
  2. Every per-series call goes through the full data-type machinery. get_cutoff → convert_to → check_is_mtype runs about 8 mtype checks per call, and each check does tag and parameter lookups via skbase (get_params, get_tag, and some deepcopy calls). These checks run on internal intermediate objects whose type is already known.
  3. The lag window is rebuilt with a transformer at every step. _get_shifted_window calls WindowSummarizer.fit_transform / transform on each of the 8 steps (_window_feature is called 72 times). That accounts for about 5 s of the time.
  4. The estimator is called only once per step. LinearRegression.predict is called 8 times on all series at once, so the model itself is already vectorised. All the extra cost is in the index and data handling around it.

Other things I noticed while benchmarking the reduction family on a real panel (~500 series, LightGBM). These may deserve separate issues:

  • RecursiveReductionForecaster(..., pooling="global") was about 7× slower than make_reduction(strategy="recursive", pooling="global"), with a similar profile dominated by deepcopy, pandas.Series.__init__ and skbase calls.
  • make_reduction(..., strategy="multioutput" | "dirrec", pooling="global") fitted one estimator per series (3,584 fits for 448 series × 8 horizons) instead of a pooled model.

Expected behavior

A pooled global recursive reduction on panel data should cost roughly the time of the regressor's fit plus H vectorised predict calls. That means runtime is dominated by the estimator and grows only mildly with the number of series, as it does in mlforecast (0.03 s → 0.06 s from 50 to 800 series). Internal per-series Python loops and repeated mtype checking/conversion of intermediate objects should not dominate runtime.

Additional context

My use case is rolling-origin backtesting and hyperparameter search over roughly 500 series. With LightGBM, one fold takes about 6.3 s in sktime, of which about 0.13 s is spent inside LightGBM. The same model in mlforecast takes about 0.15 s per fold. The forecasts are identical, so the whole difference is framework overhead.

Versions

System:
    python: 3.12.12 (main, Feb 12 2026, 00:42:34) [Clang 21.1.4 ]
   machine: macOS-26.6-arm64-arm-64bit
Python dependencies:
          pip: 26.0.1
       sktime: 1.2.0
 scikit-learn: 1.9.1
  scikit-base: 1.1.1
        numpy: 2.5.3
        scipy: 1.18.1
       pandas: 2.3.3
   matplotlib: None
       joblib: 1.6.0
huggingface-hub: None
        numba: None
     pmdarima: None
pytorch-forecasting: None
   skforecast: None
        skpro: None
statsforecast: None
  statsmodels: None
 transformers: None
      tsfresh: None
      tslearn: None
        torch: None
   tensorflow: None

(mlforecast 1.1.0 was used for the comparison.)