Skip to content

[BUG] Seasonal periodicity seems to work incorrectly (at least for naive forecasts with mean strategy) #907

@Flix6x

Description

@Flix6x

Describe the bug

The interpretation of seasonal periodicity seems to be off by a number of steps in some cases, resulting in shifted forecasts.

To Reproduce

import pandas as pd
from sktime.forecasting.base import ForecastingHorizon
from sktime.forecasting.naive import NaiveForecaster

# given 4 hours of data with a seasonal periodicity of 3 hours
sp = 3
train_period = pd.date_range("2021-06-01 00:00", "2021-06-1 04:00", freq="1h", closed="left")
train_data = pd.Series(
    index=train_period,
    data=(list(range(1, sp + 1))*len(train_period))[:len(train_period)],
)
print(train_data)

# 2021-06-01 00:00:00    1
# 2021-06-01 01:00:00    2
# 2021-06-01 02:00:00    3
# 2021-06-01 03:00:00    1
# Freq: H, dtype: int64

# let's forecast the next 5 hours with a periodicity of 3 hours
test_period = pd.date_range("2021-06-01 04:00", "2021-06-1 09:00", freq="1h", closed="left")
fh = ForecastingHorizon(test_period, is_relative=False)
model = NaiveForecaster(strategy="mean", sp=sp)
model.fit(train_data)
test_data = model.predict(fh)
print(test_data)

# 2021-06-01 04:00:00    1.0
# 2021-06-01 05:00:00    2.0
# 2021-06-01 06:00:00    3.0
# 2021-06-01 07:00:00    1.0
# 2021-06-01 08:00:00    2.0
# dtype: float64

Expected behavior

# the result I expected is

# 2021-06-01 04:00:00    2.0  # (value of 3 hours earlier)
# 2021-06-01 05:00:00    3.0  # (value of 3 hours earlier)
# 2021-06-01 06:00:00    1.0  # (mean value of 3 and 6 hours earlier)
# 2021-06-01 07:00:00    2.0  # (value of 6 hours earlier)
# 2021-06-01 08:00:00    3.0  # (value of 6 hours earlier)
# dtype: float64

Additional context

In the above, sp=3 gives forecasts shifted by 1 step. Changing sp=3 to sp=1, sp=2 or sp=4 all yield results as expected. With a training dataset of 5 hours and a seasonality of 3 hours, the forecasts are shifted by 2 steps. It this somehow caused by the training set not being divisible by the periodicity?

Versions

Details

System:
python: 3.8.6 (default, Jan 27 2021, 15:42:20) [GCC 10.2.0]
executable: /home/felix/PycharmProjects/sktime/venv/bin/python
machine: Linux-5.11.0-7614-generic-x86_64-with-glibc2.32

Python dependencies:
pip: 21.1.2
setuptools: 57.0.0
sklearn: 0.24.2
sktime: 0.6.1
statsmodels: 0.12.2
numpy: 1.20.3
scipy: 1.6.3
Cython: 0.29.23
pandas: 1.2.4
matplotlib: None
joblib: 1.0.1
numba: 0.53.1
pmdarima: None
tsfresh: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingmodule:forecastingforecasting module: forecasting, incl probabilistic and hierarchical forecasting

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions