|
| 1 | +"""Test Naive Forecaster.""" |
| 2 | + |
| 3 | +import numpy as np |
| 4 | + |
| 5 | +from aeon.forecasting import NaiveForecaster |
| 6 | + |
| 7 | + |
| 8 | +def test_naive_forecaster_last_strategy(): |
| 9 | + """Test NaiveForecaster with 'last' strategy.""" |
| 10 | + sample_data = np.array([10, 20, 30, 40, 50]) |
| 11 | + forecaster = NaiveForecaster(strategy="last", horizon=3) |
| 12 | + forecaster.fit(sample_data) |
| 13 | + predictions = forecaster.predict() |
| 14 | + expected = 50 |
| 15 | + np.testing.assert_array_equal(predictions, expected) |
| 16 | + |
| 17 | + |
| 18 | +def test_naive_forecaster_mean_strategy(): |
| 19 | + """Test NaiveForecaster with 'mean' strategy.""" |
| 20 | + sample_data = np.array([10, 20, 30, 40, 50]) |
| 21 | + forecaster = NaiveForecaster(strategy="mean", horizon=2) |
| 22 | + forecaster.fit(sample_data) |
| 23 | + predictions = forecaster.predict() |
| 24 | + expected = 30 # Mean of [10, 20, 30, 40, 50] is 30 |
| 25 | + np.testing.assert_array_equal(predictions, expected) |
| 26 | + |
| 27 | + |
| 28 | +def test_naive_forecaster_seasonal_last_strategy(): |
| 29 | + """Test NaiveForecaster with 'seasonal_last' strategy.""" |
| 30 | + data = np.array([1, 2, 3, 4, 5, 6, 7, 8]) |
| 31 | + |
| 32 | + # Last season is [6, 7, 8] for seasonal_period = 3 |
| 33 | + forecaster = NaiveForecaster(strategy="seasonal_last", seasonal_period=3, horizon=4) |
| 34 | + forecaster.fit(data) |
| 35 | + pred = forecaster.predict() |
| 36 | + pred2 = forecaster.predict(y=data) |
| 37 | + expected = 6 # predicts the 1-st element of the last season. |
| 38 | + np.testing.assert_array_equal(pred, expected) |
| 39 | + np.testing.assert_array_equal(pred2, expected) |
| 40 | + |
| 41 | + # Test horizon within the season length |
| 42 | + forecaster = NaiveForecaster(strategy="seasonal_last", seasonal_period=3, horizon=2) |
| 43 | + forecaster.fit(data) |
| 44 | + pred = forecaster.predict() |
| 45 | + pred2 = forecaster.predict(y=data) |
| 46 | + expected = 7 # predicts the 2-nd element of the last season. |
| 47 | + np.testing.assert_array_equal(pred, expected) |
| 48 | + np.testing.assert_array_equal(pred2, expected) |
| 49 | + |
| 50 | + # Test horizon wrapping around to a new season |
| 51 | + forecaster = NaiveForecaster(strategy="seasonal_last", seasonal_period=3, horizon=7) |
| 52 | + forecaster.fit(data) |
| 53 | + pred = forecaster.predict() |
| 54 | + pred2 = forecaster.predict(y=data) |
| 55 | + expected = 6 # predicts the 1-st element of the last season. |
| 56 | + np.testing.assert_array_equal(pred, expected) |
| 57 | + np.testing.assert_array_equal(pred2, expected) |
| 58 | + |
| 59 | + # Last season is now [5, 6, 7, 8] with seasonal_period = 4 |
| 60 | + forecaster = NaiveForecaster(strategy="seasonal_last", seasonal_period=4, horizon=6) |
| 61 | + forecaster.fit(data) |
| 62 | + pred = forecaster.predict() |
| 63 | + pred2 = forecaster.predict(y=data) |
| 64 | + expected = 6 # predicts the 2nd element of the new last season. |
| 65 | + np.testing.assert_array_equal(pred, expected) |
| 66 | + np.testing.assert_array_equal(pred2, expected) |
0 commit comments