Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ENH] Add test for global forecasting #3728

Merged
merged 3 commits into from Nov 17, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
62 changes: 61 additions & 1 deletion sktime/forecasting/compose/tests/test_reduce_global.py
Expand Up @@ -2,15 +2,24 @@
"""Test extraction of features across (shifted) windows."""
__author__ = ["danbartl"]

import random

import numpy as np
import pandas as pd
import pytest
from sklearn.ensemble import RandomForestRegressor
from sklearn.ensemble import (
GradientBoostingRegressor,
HistGradientBoostingRegressor,
RandomForestRegressor,
)
from sklearn.pipeline import make_pipeline

from sktime.datasets import load_airline
from sktime.datatypes import get_examples
from sktime.forecasting.base import ForecastingHorizon
from sktime.forecasting.compose import make_reduction
from sktime.forecasting.model_selection import temporal_train_test_split
from sktime.performance_metrics.forecasting import mean_absolute_percentage_error
from sktime.transformations.series.summarize import WindowSummarizer

# Load data that will be the basis of tests
Expand Down Expand Up @@ -175,3 +184,54 @@ def test_list_reduction(y, index_names):
forecaster2.fit(y, fh=[1, 2])
y_pred = forecaster2.predict(fh=[1, 2, 12])
check_eval(y_pred.index.names, index_names)


def test_equality_transfo_nontranso():
"""Test that recursive reducers return same results for global / local forecasts."""
ensemblealgos = [
danbartl marked this conversation as resolved.
Show resolved Hide resolved
RandomForestRegressor(),
GradientBoostingRegressor(),
HistGradientBoostingRegressor(),
]

y = load_airline()
y_train, y_test = temporal_train_test_split(y, test_size=30)
fh = ForecastingHorizon(y_test.index, is_relative=False)

lag_vec = [i for i in range(1, 13)]
lag_vec.reverse()
danbartl marked this conversation as resolved.
Show resolved Hide resolved
kwargs = {
"lag_feature": {
"lag": lag_vec,
# "mean": [[1, 52], [3, 52]],
danbartl marked this conversation as resolved.
Show resolved Hide resolved
# "std": [[1, 52]],
}
}

for algo in ensemblealgos:
for _i in range(1, 5):
random_int = random.randint(1, 1000)
regressor = algo
danbartl marked this conversation as resolved.
Show resolved Hide resolved
regressor.random_state = random_int
forecaster = make_reduction(
regressor, window_length=int(12), strategy="recursive"
)
forecaster.fit(y_train)
y_pred = forecaster.predict(fh)
recursive_without = mean_absolute_percentage_error(
y_test, y_pred, symmetric=False
)
forecaster = make_reduction(
regressor,
window_length=None,
strategy="recursive",
transformers=[WindowSummarizer(**kwargs, n_jobs=1)],
pooling="global",
)

forecaster.fit(y_train)
y_pred = forecaster.predict(fh)
recursive_global = mean_absolute_percentage_error(
y_test, y_pred, symmetric=False
)
np.testing.assert_almost_equal(recursive_without, recursive_global)