|
1 | 1 | import numpy as np
|
| 2 | +import pytest |
2 | 3 |
|
3 | 4 | from darts import TimeSeries
|
4 | 5 | from darts.models import ExponentialSmoothing
|
5 | 6 | from darts.utils import timeseries_generation as tg
|
6 | 7 |
|
7 | 8 |
|
8 | 9 | class TestExponentialSmoothing:
|
9 |
| - def helper_test_seasonality_inference(self, freq_string, expected_seasonal_periods): |
10 |
| - series = tg.sine_timeseries(length=200, freq=freq_string) |
11 |
| - model = ExponentialSmoothing() |
12 |
| - model.fit(series) |
13 |
| - assert model.seasonal_periods == expected_seasonal_periods |
| 10 | + series = tg.sine_timeseries(length=100, freq="H") |
14 | 11 |
|
15 |
| - def test_seasonality_inference(self): |
16 |
| - |
17 |
| - # test `seasonal_periods` inference for datetime indices |
18 |
| - freq_str_seasonality_periods_tuples = [ |
| 12 | + @pytest.mark.parametrize( |
| 13 | + "freq_string,expected_seasonal_periods", |
| 14 | + [ |
19 | 15 | ("D", 7),
|
20 | 16 | ("H", 24),
|
21 | 17 | ("M", 12),
|
22 | 18 | ("W", 52),
|
23 | 19 | ("Q", 4),
|
24 | 20 | ("B", 5),
|
25 |
| - ] |
26 |
| - for tuple in freq_str_seasonality_periods_tuples: |
27 |
| - self.helper_test_seasonality_inference(*tuple) |
| 21 | + ], |
| 22 | + ) |
| 23 | + def test_seasonality_inference( |
| 24 | + self, freq_string: str, expected_seasonal_periods: int |
| 25 | + ): |
| 26 | + series = tg.sine_timeseries(length=200, freq=freq_string) |
| 27 | + model = ExponentialSmoothing() |
| 28 | + model.fit(series) |
| 29 | + assert model.seasonal_periods == expected_seasonal_periods |
28 | 30 |
|
29 |
| - # test default selection for integer index |
| 31 | + def test_default_parameters(self): |
| 32 | + """Test default selection for integer index""" |
30 | 33 | series = TimeSeries.from_values(np.arange(1, 30, 1))
|
31 | 34 | model = ExponentialSmoothing()
|
32 | 35 | model.fit(series)
|
33 | 36 | assert model.seasonal_periods == 12
|
34 | 37 |
|
35 |
| - # test whether a model that inferred a seasonality period before will do it again for a new series |
| 38 | + def test_multiple_fit(self): |
| 39 | + """Test whether a model that inferred a seasonality period before will do it again for a new series""" |
36 | 40 | series1 = tg.sine_timeseries(length=100, freq="M")
|
37 | 41 | series2 = tg.sine_timeseries(length=100, freq="D")
|
38 | 42 | model = ExponentialSmoothing()
|
39 | 43 | model.fit(series1)
|
40 | 44 | model.fit(series2)
|
41 | 45 | assert model.seasonal_periods == 7
|
| 46 | + |
| 47 | + def test_constructor_kwargs(self): |
| 48 | + """Using kwargs to pass additional parameters to the constructor""" |
| 49 | + constructor_kwargs = { |
| 50 | + "initialization_method": "known", |
| 51 | + "initial_level": 0.5, |
| 52 | + "initial_trend": 0.2, |
| 53 | + "initial_seasonal": np.arange(1, 25), |
| 54 | + } |
| 55 | + model = ExponentialSmoothing(kwargs=constructor_kwargs) |
| 56 | + model.fit(self.series) |
| 57 | + # must be checked separately, name is not consistent |
| 58 | + np.testing.assert_array_almost_equal( |
| 59 | + model.model.model.params["initial_seasons"], |
| 60 | + constructor_kwargs["initial_seasonal"], |
| 61 | + ) |
| 62 | + for param_name in ["initial_level", "initial_trend"]: |
| 63 | + assert ( |
| 64 | + model.model.model.params[param_name] == constructor_kwargs[param_name] |
| 65 | + ) |
| 66 | + |
| 67 | + def test_fit_kwargs(self): |
| 68 | + """Using kwargs to pass additional parameters to the fit()""" |
| 69 | + # using default optimization method |
| 70 | + model = ExponentialSmoothing() |
| 71 | + model.fit(self.series) |
| 72 | + assert model.fit_kwargs == {} |
| 73 | + pred = model.predict(n=2) |
| 74 | + |
| 75 | + model_bis = ExponentialSmoothing() |
| 76 | + model_bis.fit(self.series) |
| 77 | + assert model_bis.fit_kwargs == {} |
| 78 | + pred_bis = model_bis.predict(n=2) |
| 79 | + |
| 80 | + # two methods with the same parameters should yield the same forecasts |
| 81 | + assert pred.time_index.equals(pred_bis.time_index) |
| 82 | + np.testing.assert_array_almost_equal(pred.values(), pred_bis.values()) |
| 83 | + |
| 84 | + # change optimization method |
| 85 | + model_ls = ExponentialSmoothing(method="least_squares") |
| 86 | + model_ls.fit(self.series) |
| 87 | + assert model_ls.fit_kwargs == {"method": "least_squares"} |
| 88 | + pred_ls = model_ls.predict(n=2) |
| 89 | + |
| 90 | + # forecasts should be slightly different |
| 91 | + assert pred.time_index.equals(pred_ls.time_index) |
| 92 | + assert all(np.not_equal(pred.values(), pred_ls.values())) |
0 commit comments