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

Remove freq argument in SeasonalNaivePredictor #2932

Merged
merged 6 commits into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 6 additions & 16 deletions src/gluonts/model/seasonal_naive/_predictor.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
LastValueImputation,
MissingValueImputation,
)
from gluonts.time_feature import get_seasonality


class SeasonalNaivePredictor(RepresentablePredictor):
Expand All @@ -43,40 +42,31 @@ class SeasonalNaivePredictor(RepresentablePredictor):

Parameters
----------
freq
Frequency of the input data
prediction_length
Number of time points to predict
Number of time points to predict.
season_length
Length of the seasonality pattern of the input data
Seasonality used to make predictions.
imputation_method
The imputation method to use in case of missing values.
Defaults to `LastValueImputation` which replaces each missing
Defaults to :py:class:`LastValueImputation` which replaces each missing
value with the last value that was not missing.
"""

@validated()
def __init__(
self,
freq: str,
prediction_length: int,
season_length: Optional[int] = None,
season_length: int,
imputation_method: Optional[
MissingValueImputation
] = LastValueImputation(),
) -> None:
super().__init__(prediction_length=prediction_length)

assert (
season_length is None or season_length > 0
), "The value of `season_length` should be > 0"
assert season_length > 0, "The value of `season_length` should be > 0"

self.prediction_length = prediction_length
self.season_length = (
season_length
if season_length is not None
else get_seasonality(freq)
)
self.season_length = season_length
self.imputation_method = imputation_method

def predict_item(self, item: DataEntry) -> Forecast:
Expand Down
3 changes: 2 additions & 1 deletion test/ev/test_metrics_compared_to_previous_approach.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,8 @@ def test_against_former_evaluator():
)

predictor = SeasonalNaivePredictor(
prediction_length=prediction_length, freq=freq
prediction_length=prediction_length,
season_length=get_seasonality(freq),
)

quantile_levels = (0.1, 0.5, 0.9)
Expand Down
62 changes: 46 additions & 16 deletions test/ext/naive_2/test_predictors.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from gluonts.ext.naive_2 import Naive2Predictor
from gluonts.model.predictor import Predictor
from gluonts.model.seasonal_naive import SeasonalNaivePredictor
from gluonts.time_feature import get_seasonality


def generate_random_dataset(
Expand All @@ -49,17 +50,24 @@ def generate_random_dataset(


@pytest.mark.parametrize(
"predictor_cls", [SeasonalNaivePredictor, Naive2Predictor]
"make_predictor",
[
lambda freq: SeasonalNaivePredictor(
prediction_length=PREDICTION_LENGTH,
season_length=SEASON_LENGTH,
),
lambda freq: Naive2Predictor(
freq=freq,
prediction_length=PREDICTION_LENGTH,
season_length=SEASON_LENGTH,
),
],
)
@pytest.mark.parametrize(
"freq", ["1min", "15min", "30min", "1H", "2H", "12H", "7D", "1W", "1M"]
)
def test_predictor(predictor_cls, freq: str):
predictor = predictor_cls(
freq=freq,
prediction_length=PREDICTION_LENGTH,
season_length=SEASON_LENGTH,
)
def test_predictor(make_predictor, freq: str):
predictor = make_predictor(freq)
dataset = list(
generate_random_dataset(
num_ts=NUM_TS,
Expand Down Expand Up @@ -87,7 +95,7 @@ def test_predictor(predictor_cls, freq: str):
assert forecast.start_date == forecast_start(data)

# specifically for the seasonal naive we can test the supposed result directly
if predictor_cls == SeasonalNaivePredictor:
if isinstance(predictor, SeasonalNaivePredictor):
assert np.allclose(forecast.samples[0], ref)


Expand Down Expand Up @@ -115,11 +123,25 @@ def naive_2_predictor():

@flaky(max_runs=3, min_passes=1)
@pytest.mark.parametrize(
"predictor_cls, parameters, accuracy",
[seasonal_naive_predictor() + (0.0,), naive_2_predictor() + (0.0,)],
"predictor, accuracy",
[
(
SeasonalNaivePredictor(
prediction_length=CONSTANT_DATASET_PREDICTION_LENGTH,
season_length=get_seasonality(CONSTANT_DATASET_FREQ),
),
0.0,
),
(
Naive2Predictor(
freq=CONSTANT_DATASET_FREQ,
prediction_length=CONSTANT_DATASET_PREDICTION_LENGTH,
),
0.0,
),
],
)
def test_accuracy(predictor_cls, parameters, accuracy):
predictor = predictor_cls(freq=CONSTANT_DATASET_FREQ, **parameters)
def test_accuracy(predictor, accuracy):
agg_metrics, item_metrics = backtest_metrics(
test_dataset=constant_test_ds,
predictor=predictor,
Expand All @@ -133,11 +155,19 @@ def test_accuracy(predictor_cls, parameters, accuracy):


@pytest.mark.parametrize(
"predictor_cls, parameters",
[seasonal_naive_predictor(), naive_2_predictor()],
"predictor",
[
SeasonalNaivePredictor(
prediction_length=CONSTANT_DATASET_PREDICTION_LENGTH,
season_length=get_seasonality(CONSTANT_DATASET_FREQ),
),
Naive2Predictor(
freq=CONSTANT_DATASET_FREQ,
prediction_length=CONSTANT_DATASET_PREDICTION_LENGTH,
),
],
)
def test_seriali_predictors(predictor_cls, parameters):
predictor = predictor_cls(freq=CONSTANT_DATASET_FREQ, **parameters)
def test_seriali_predictors(predictor):
with tempfile.TemporaryDirectory() as temp_dir:
predictor.serialize(Path(temp_dir))
predictor_exp = Predictor.deserialize(Path(temp_dir))
Expand Down
10 changes: 3 additions & 7 deletions test/model/seasonal_naive/test_seasonal_naive.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ def get_prediction(
imputation_method=LastValueImputation(),
):
pred = SeasonalNaivePredictor(
freq=FREQ,
prediction_length=prediction_length,
season_length=season_length,
imputation_method=imputation_method,
Expand Down Expand Up @@ -68,12 +67,9 @@ def get_prediction(
([1, 2, 3], [1], 1, 3, LastValueImputation()),
([1, 2, 3], [1, 2], 2, 3, LastValueImputation()),
([1, 2, 3], [1, 2, 3], 3, 3, LastValueImputation()),
([1, 1, 1], [1], 1, None, LastValueImputation()),
([1, 1, 1], [1, 1], 2, None, LastValueImputation()),
([1, 1, 1], [1, 1, 1], 3, None, LastValueImputation()),
([1, 3, np.nan], [3], 1, None, LastValueImputation()),
([1, 3, np.nan], [3, 3], 2, None, LastValueImputation()),
([1, 3, np.nan], [3, 3, 3], 3, None, LastValueImputation()),
([1, 3, np.nan], [3], 1, 1, LastValueImputation()),
([1, 3, np.nan], [3, 3], 2, 1, LastValueImputation()),
([1, 3, np.nan], [3, 3, 3], 3, 1, LastValueImputation()),
([1, 3, np.nan], [np.nan], 1, 1, LeavesMissingValues()),
([1, 3, np.nan], [np.nan] * 2, 2, 1, LeavesMissingValues()),
([1, 3, np.nan], [np.nan] * 3, 3, 1, LeavesMissingValues()),
Expand Down
8 changes: 2 additions & 6 deletions test/model/test_evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,7 @@ def test_evaluate_model_vs_forecasts():
prediction_length=3, windows=4
)

model = SeasonalNaivePredictor(
freq="D", prediction_length=3, season_length=1
)
model = SeasonalNaivePredictor(prediction_length=3, season_length=1)

forecasts = list(model.predict(test_data.input))

Expand Down Expand Up @@ -246,9 +244,7 @@ def test_data_nan():
prediction_length=3, windows=4
)

model = SeasonalNaivePredictor(
freq="D", prediction_length=3, season_length=1
)
model = SeasonalNaivePredictor(prediction_length=3, season_length=1)

forecasts = list(model.predict(test_data.input))

Expand Down