-
Notifications
You must be signed in to change notification settings - Fork 89
/
trend.py
632 lines (543 loc) · 24.1 KB
/
trend.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
"""Implements trend based forecasters."""
__author__ = ["tensorflow-as-tf", "mloning", "aiwalter", "fkiraly"]
__all__ = ["TrendForecaster", "PolynomialTrendForecaster", "STLForecaster"]
import pandas as pd
from sklearn.base import clone
from sklearn.linear_model import LinearRegression
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import PolynomialFeatures
from aeon.forecasting.base import BaseForecaster
def _get_X_numpy_int_from_pandas(x):
"""Convert pandas index to an sklearn compatible X, 2D np.ndarray, int type."""
if isinstance(x, (pd.DatetimeIndex)):
x = x.astype("int64") / 864e11
else:
x = x.astype("int64")
return x.to_numpy().reshape(-1, 1)
class TrendForecaster(BaseForecaster):
r"""Trend based forecasts of time series data, regressing values on index.
Uses a `sklearn` regressor `regressor` to regress values of time series on index:
In `fit`, for input time series :math:`(v_i, t_i), i = 1, \dots, T`,
where :math:`v_i` are values and :math:`t_i` are time stamps,
fits an `sklearn` model :math:`v_i = f(t_i) + \epsilon_i`, where `f` is
the model fitted when `regressor.fit` is passed `X` = vector of :math:`t_i`,
and `y` = vector of :math:`v_i`.
In `predict`, for a new time point :math:`t_*`, predicts :math:`f(t_*)`,
where :math:`f` is the function as fitted above in `fit`.
Default for `regressor` is linear regression = `sklearn` `LinearRegression` default.
If time stamps are `pd.DatetimeIndex`, fitted coefficients are in units
of days since start of 1970. If time stamps are `pd.PeriodIndex`,
coefficients are in units of (full) periods since start of 1970.
Parameters
----------
regressor : estimator object, default = None
Define the regression model type. If not set, will default to
sklearn.linear_model.LinearRegression
Examples
--------
>>> from aeon.datasets import load_airline
>>> from aeon.forecasting.trend import TrendForecaster
>>> y = load_airline()
>>> forecaster = TrendForecaster()
>>> forecaster.fit(y)
TrendForecaster(...)
>>> y_pred = forecaster.predict(fh=[1,2,3])
"""
_tags = {
"ignores-exogeneous-X": True,
"requires-fh-in-fit": False,
"capability:missing_values": False,
}
def __init__(self, regressor=None):
# for default regressor, set fit_intercept=True
self.regressor = regressor
super(TrendForecaster, self).__init__()
def _fit(self, y, X=None, fh=None):
"""Fit to training data.
Parameters
----------
y : pd.Series
Target time series with which to fit the forecaster.
X : pd.DataFrame, default=None
Exogenous variables are ignored
fh : int, list or np.array, optional (default=None)
The forecasters horizon with the steps ahead to to predict.
Returns
-------
self : returns an instance of self.
"""
if self.regressor is None:
self.regressor_ = LinearRegression(fit_intercept=True)
else:
self.regressor_ = clone(self.regressor)
# we regress index on series values
# the sklearn X is obtained from the index of y
# the sklearn y can be taken as the y seen here
X_sklearn = _get_X_numpy_int_from_pandas(y.index)
# fit regressor
self.regressor_.fit(X_sklearn, y)
return self
def _predict(self, fh=None, X=None):
"""Make forecasts for the given forecast horizon.
Parameters
----------
fh : int, list or np.array
The forecast horizon with the steps ahead to predict
X : pd.DataFrame, default=None
Exogenous variables (ignored)
Returns
-------
y_pred : pd.Series
Point predictions for the forecast
"""
# use relative fh as time index to predict
fh = self.fh.to_absolute(self.cutoff)
X_sklearn = _get_X_numpy_int_from_pandas(fh.to_pandas())
y_pred = self.regressor_.predict(X_sklearn)
return pd.Series(y_pred, index=self.fh.to_absolute(self.cutoff).to_pandas())
@classmethod
def get_test_params(cls, parameter_set="default"):
"""Return testing parameter settings for the estimator.
Parameters
----------
parameter_set : str, default="default"
Name of the set of test parameters to return, for use in tests. If no
special parameters are defined for a value, will return `"default"` set.
Returns
-------
params : dict or list of dict, default = {}
Parameters to create testing instances of the class
Each dict are parameters to construct an "interesting" test instance, i.e.,
`MyClass(**params)` or `MyClass(**params[i])` creates a valid test instance.
`create_test_instance` uses the first (or only) dictionary in `params`
"""
from sklearn.ensemble import RandomForestRegressor
params_list = [{}, {"regressor": RandomForestRegressor()}]
return params_list
class PolynomialTrendForecaster(BaseForecaster):
r"""Forecast time series data with a polynomial trend.
Uses a `sklearn` regressor `regressor` to regress values of time series on index,
after extraction of polynomial features.
Same `TrendForecaster` where `regressor` is pipelined with transformation step
`PolynomialFeatures(degree, with_intercept)` applied to time, at the start.
In `fit`, for input time series :math:`(v_i, p(t_i)), i = 1, \dots, T`,
where :math:`v_i` are values, :math:`t_i` are time stamps,
and :math:`p` is the polynomial feature transform with degree `degree`,
and with/without intercept depending on `with_intercept`,
fits an `sklearn` model :math:`v_i = f(p(t_i)) + \epsilon_i`, where `f` is
the model fitted when `regressor.fit` is passed `X` = vector of :math:`p(t_i)`,
and `y` = vector of :math:`v_i`.
In `predict`, for a new time point :math:`t_*`, predicts :math:`f(p(t_*))`,
where :math:`f` is the function as fitted above in `fit`,
and :math:`p` is the same polynomial feature transform as above.
Default for `regressor` is linear regression = `sklearn` `LinearRegression` default.
If time stamps are `pd.DatetimeIndex`, fitted coefficients are in units
of days since start of 1970. If time stamps are `pd.PeriodIndex`,
coefficients are in units of (full) periods since start of 1970.
Parameters
----------
regressor : sklearn regressor estimator object, default = None
Define the regression model type. If not set, will default to
sklearn.linear_model.LinearRegression
degree : int, default = 1
Degree of polynomial function
with_intercept : bool, default=True
If true, then include a feature in which all polynomial powers are
zero. (i.e. a column of ones, acts as an intercept term in a linear
model)
Examples
--------
>>> from aeon.datasets import load_airline
>>> from aeon.forecasting.trend import PolynomialTrendForecaster
>>> y = load_airline()
>>> forecaster = PolynomialTrendForecaster(degree=1)
>>> forecaster.fit(y)
PolynomialTrendForecaster(...)
>>> y_pred = forecaster.predict(fh=[1,2,3])
"""
_tags = {
"ignores-exogeneous-X": True,
"requires-fh-in-fit": False,
"capability:missing_values": False,
}
def __init__(self, regressor=None, degree=1, with_intercept=True):
self.regressor = regressor
self.degree = degree
self.with_intercept = with_intercept
self.regressor_ = self.regressor
super(PolynomialTrendForecaster, self).__init__()
def _fit(self, y, X=None, fh=None):
"""Fit to training data.
Parameters
----------
y : pd.Series
Target time series with which to fit the forecaster.
X : pd.DataFrame, default=None
Exogenous variables are ignored
fh : int, list or np.array, default=None
The forecasters horizon with the steps ahead to to predict.
Returns
-------
self : returns an instance of self.
"""
# for default regressor, set fit_intercept=False as we generate a
# dummy variable in polynomial features
if self.regressor is None:
regressor = LinearRegression(fit_intercept=False)
else:
regressor = clone(self.regressor)
# make pipeline with polynomial features
self.regressor_ = make_pipeline(
PolynomialFeatures(degree=self.degree, include_bias=self.with_intercept),
regressor,
)
# we regress index on series values
# the sklearn X is obtained from the index of y
# the sklearn y can be taken as the y seen here
X_sklearn = _get_X_numpy_int_from_pandas(y.index)
# fit regressor
self.regressor_.fit(X_sklearn, y)
return self
def _predict(self, fh=None, X=None):
"""Make forecasts for the given forecast horizon.
Parameters
----------
fh : int, list or np.array
The forecast horizon with the steps ahead to predict
X : pd.DataFrame, default=None
Exogenous variables (ignored)
Returns
-------
y_pred : pd.Series
Point predictions for the forecast
"""
# use relative fh as time index to predict
fh = self.fh.to_absolute(self.cutoff)
X_sklearn = _get_X_numpy_int_from_pandas(fh.to_pandas())
y_pred = self.regressor_.predict(X_sklearn)
return pd.Series(y_pred, index=self.fh.to_absolute(self.cutoff).to_pandas())
@classmethod
def get_test_params(cls, parameter_set="default"):
"""Return testing parameter settings for the estimator.
Parameters
----------
parameter_set : str, default="default"
Name of the set of test parameters to return, for use in tests. If no
special parameters are defined for a value, will return `"default"` set.
Returns
-------
params : dict or list of dict, default = {}
Parameters to create testing instances of the class
Each dict are parameters to construct an "interesting" test instance, i.e.,
`MyClass(**params)` or `MyClass(**params[i])` creates a valid test instance.
`create_test_instance` uses the first (or only) dictionary in `params`
"""
from sklearn.ensemble import RandomForestRegressor
params_list = [
{},
{
"regressor": RandomForestRegressor(),
"degree": 2,
"with_intercept": False,
},
]
return params_list
class STLForecaster(BaseForecaster):
"""Implements STLForecaster based on statsmodels.tsa.seasonal.STL implementation.
The STLForecaster applies the following algorithm, also see [1]_.
in `fit`:
1. use `statsmodels` `STL` [2]_ to decompose the given series `y` into
the three components: `trend`, `season` and `residuals`.
2. fit clones of `forecaster_trend` to `trend`, `forecaster_seasonal` to `season`,
and `forecaster_resid` to `residuals`, using `y`, `X`, `fh` from `fit`.
The forecasters are fitted as clones, stored in the attributes
`forecaster_trend_`, `forecaster_seasonal_`, `forecaster_resid_`.
In `predict`, forecasts as follows:
1. obtain forecasts `y_pred_trend` from `forecaster_trend_`,
`y_pred_seasonal` from `forecaster_seasonal_`, and
`y_pred_residual` from `forecaster_resid_`, using `X`, `fh`, from `predict`.
2. recompose `y_pred` as `y_pred = y_pred_trend + y_pred_seasonal + y_pred_residual`
3. return `y_pred`
`update` refits entirely, i.e., behaves as `fit` on all data seen so far.
Parameters
----------
sp : int, optional, default=2. Passed to `statsmodels` `STL`.
Length of the seasonal period passed to `statsmodels` `STL`.
(forecaster_seasonal, forecaster_resid) that are None. The
default forecaster_trend does not get sp as trend is independent
to seasonality.
seasonal : int, optional., default=7. Passed to `statsmodels` `STL`.
Length of the seasonal smoother. Must be an odd integer >=3, and should
normally be >= 7 (default).
trend : {int, None}, optional, default=None. Passed to `statsmodels` `STL`.
Length of the trend smoother. Must be an odd integer. If not provided
uses the smallest odd integer greater than
1.5 * period / (1 - 1.5 / seasonal), following the suggestion in
the original implementation.
low_pass : {int, None}, optional, default=None. Passed to `statsmodels` `STL`.
Length of the low-pass filter. Must be an odd integer >=3. If not
provided, uses the smallest odd integer > period.
seasonal_deg : int, optional, default=1. Passed to `statsmodels` `STL`.
Degree of seasonal LOESS. 0 (constant) or 1 (constant and trend).
trend_deg : int, optional, default=1. Passed to `statsmodels` `STL`.
Degree of trend LOESS. 0 (constant) or 1 (constant and trend).
low_pass_deg : int, optional, default=1. Passed to `statsmodels` `STL`.
Degree of low pass LOESS. 0 (constant) or 1 (constant and trend).
robust : bool, optional, default=False. Passed to `statsmodels` `STL`.
Flag indicating whether to use a weighted version that is robust to
some forms of outliers.
seasonal_jump : int, optional, default=1. Passed to `statsmodels` `STL`.
Positive integer determining the linear interpolation step. If larger
than 1, the LOESS is used every seasonal_jump points and linear
interpolation is between fitted points. Higher values reduce
estimation time.
trend_jump : int, optional, default=1. Passed to `statsmodels` `STL`.
Positive integer determining the linear interpolation step. If larger
than 1, the LOESS is used every trend_jump points and values between
the two are linearly interpolated. Higher values reduce estimation
time.
low_pass_jump : int, optional, default=1. Passed to `statsmodels` `STL`.
Positive integer determining the linear interpolation step. If larger
than 1, the LOESS is used every low_pass_jump points and values between
the two are linearly interpolated. Higher values reduce estimation
time.
inner_iter: int or None, optional, default=None. Passed to `statsmodels` `STL`.
Number of iterations to perform in the inner loop. If not provided uses 2 if
robust is True, or 5 if not. This param goes into STL.fit() from statsmodels.
outer_iter: int or None, optional, default=None. Passed to `statsmodels` `STL`.
Number of iterations to perform in the outer loop. If not provided uses 15 if
robust is True, or 0 if not. This param goes into STL.fit() from statsmodels.
forecaster_trend : aeon forecaster, optional
Forecaster to be fitted on trend_ component of the
STL, by default None. If None, then
a NaiveForecaster(strategy="drift") is used.
forecaster_seasonal : aeon forecaster, optional
Forecaster to be fitted on seasonal_ component of the
STL, by default None. If None, then
a NaiveForecaster(strategy="last") is used.
forecaster_resid : aeon forecaster, optional
Forecaster to be fitted on resid_ component of the
STL, by default None. If None, then
a NaiveForecaster(strategy="mean") is used.
Attributes
----------
trend_ : pd.Series
Trend component.
seasonal_ : pd.Series
Seasonal component.
resid_ : pd.Series
Residuals component.
forecaster_trend_ : aeon forecaster
Fitted trend forecaster.
forecaster_seasonal_ : aeon forecaster
Fitted seasonal forecaster.
forecaster_resid_ : aeon forecaster
Fitted residual forecaster.
Examples
--------
>>> from aeon.datasets import load_airline
>>> from aeon.forecasting.trend import STLForecaster
>>> y = load_airline()
>>> forecaster = STLForecaster(sp=12) # doctest: +SKIP
>>> forecaster.fit(y) # doctest: +SKIP
STLForecaster(...)
>>> y_pred = forecaster.predict(fh=[1,2,3]) # doctest: +SKIP
See Also
--------
Deseasonalizer
Detrender
References
----------
.. [1] R. B. Cleveland, W. S. Cleveland, J.E. McRae, and I. Terpenning (1990)
STL: A Seasonal-Trend Decomposition Procedure Based on LOESS.
Journal of Official Statistics, 6, 3-73.
.. [2] https://www.statsmodels.org/dev/generated/statsmodels.tsa.seasonal.STL.html
"""
_tags = {
"y_input_type": "univariate", # which y are fine? univariate/multivariate/both
"ignores-exogeneous-X": False, # does estimator ignore the exogeneous X?
"capability:missing_values": False, # can estimator handle missing data?
"y_inner_type": "pd.Series", # which types do _fit, _predict, assume for y?
"X_inner_type": "pd.DataFrame", # which types do _fit, _predict, assume for X?
"requires-fh-in-fit": False, # is forecasting horizon already required in fit?
"python_dependencies": "statsmodels",
}
def __init__(
self,
sp=2,
seasonal=7,
trend=None,
low_pass=None,
seasonal_deg=1,
trend_deg=1,
low_pass_deg=1,
robust=False,
seasonal_jump=1,
trend_jump=1,
low_pass_jump=1,
inner_iter=None,
outer_iter=None,
forecaster_trend=None,
forecaster_seasonal=None,
forecaster_resid=None,
):
self.sp = sp
self.seasonal = seasonal
self.trend = trend
self.low_pass = low_pass
self.seasonal_deg = seasonal_deg
self.trend_deg = trend_deg
self.low_pass_deg = low_pass_deg
self.robust = robust
self.seasonal_jump = seasonal_jump
self.trend_jump = trend_jump
self.low_pass_jump = low_pass_jump
self.inner_iter = inner_iter
self.outer_iter = outer_iter
self.forecaster_trend = forecaster_trend
self.forecaster_seasonal = forecaster_seasonal
self.forecaster_resid = forecaster_resid
super(STLForecaster, self).__init__()
def _fit(self, y, X=None, fh=None):
"""Fit forecaster to training data.
Parameters
----------
y : pd.Series
Target time series to which to fit the forecaster.
fh : int, list, np.array or ForecastingHorizon, optional (default=None)
The forecasters horizon with the steps ahead to to predict.
X : pd.DataFrame, optional (default=None)
Returns
-------
self : returns an instance of self.
"""
from statsmodels.tsa.seasonal import STL as _STL
from aeon.forecasting.naive import NaiveForecaster
self._stl = _STL(
y.values,
period=self.sp,
seasonal=self.seasonal,
trend=self.trend,
low_pass=self.low_pass,
seasonal_deg=self.seasonal_deg,
trend_deg=self.trend_deg,
low_pass_deg=self.low_pass_deg,
robust=self.robust,
seasonal_jump=self.seasonal_jump,
trend_jump=self.trend_jump,
low_pass_jump=self.low_pass_jump,
).fit(inner_iter=self.inner_iter, outer_iter=self.outer_iter)
self.seasonal_ = pd.Series(self._stl.seasonal, index=y.index)
self.resid_ = pd.Series(self._stl.resid, index=y.index)
self.trend_ = pd.Series(self._stl.trend, index=y.index)
self.forecaster_seasonal_ = (
NaiveForecaster(sp=self.sp, strategy="last")
if self.forecaster_seasonal is None
else self.forecaster_seasonal.clone()
)
# trend forecaster does not need sp
self.forecaster_trend_ = (
NaiveForecaster(strategy="drift")
if self.forecaster_trend is None
else self.forecaster_trend.clone()
)
self.forecaster_resid_ = (
NaiveForecaster(sp=self.sp, strategy="mean")
if self.forecaster_resid is None
else self.forecaster_resid.clone()
)
# fitting forecasters to different components
self.forecaster_seasonal_.fit(y=self.seasonal_, X=X, fh=fh)
self.forecaster_trend_.fit(y=self.trend_, X=X, fh=fh)
self.forecaster_resid_.fit(y=self.resid_, X=X, fh=fh)
def _predict(self, fh, X=None):
"""Forecast time series at future horizon.
Parameters
----------
fh : int, list, np.array or ForecastingHorizon
Forecasting horizon
X : pd.DataFrame, optional (default=None)
Exogenous time series
Returns
-------
y_pred : pd.Series
Point predictions
"""
y_pred_seasonal = self.forecaster_seasonal_.predict(fh=fh, X=X)
y_pred_trend = self.forecaster_trend_.predict(fh=fh, X=X)
y_pred_resid = self.forecaster_resid_.predict(fh=fh, X=X)
y_pred = y_pred_seasonal + y_pred_trend + y_pred_resid
return y_pred
def _update(self, y, X=None, update_params=True):
"""Update cutoff value and, optionally, fitted parameters.
Parameters
----------
y : pd.Series, pd.DataFrame, or np.array
Target time series to which to fit the forecaster.
X : pd.DataFrame, optional (default=None)
Exogeneous data
update_params : bool, optional (default=True)
whether model parameters should be updated
Returns
-------
self : reference to self
"""
from statsmodels.tsa.seasonal import STL as _STL
self._stl = _STL(
y.values,
period=self.sp,
seasonal=self.seasonal,
trend=self.trend,
low_pass=self.low_pass,
seasonal_deg=self.seasonal_deg,
trend_deg=self.trend_deg,
low_pass_deg=self.low_pass_deg,
robust=self.robust,
seasonal_jump=self.seasonal_jump,
trend_jump=self.trend_jump,
low_pass_jump=self.low_pass_jump,
).fit(inner_iter=self.inner_iter, outer_iter=self.outer_iter)
self.seasonal_ = pd.Series(self._stl.seasonal, index=y.index)
self.resid_ = pd.Series(self._stl.resid, index=y.index)
self.trend_ = pd.Series(self._stl.trend, index=y.index)
self.forecaster_seasonal_.update(
y=self.seasonal_, X=X, update_params=update_params
)
self.forecaster_trend_.update(y=self.trend_, X=X, update_params=update_params)
self.forecaster_resid_.update(y=self.resid_, X=X, update_params=update_params)
return self
@classmethod
def get_test_params(cls, parameter_set="default"):
"""Return testing parameter settings for the estimator.
Parameters
----------
parameter_set : str, default="default"
Name of the set of test parameters to return, for use in tests. If no
special parameters are defined for a value, will return `"default"` set.
Returns
-------
params : dict or list of dict, default = {}
Parameters to create testing instances of the class
Each dict are parameters to construct an "interesting" test instance, i.e.,
`MyClass(**params)` or `MyClass(**params[i])` creates a valid test instance.
`create_test_instance` uses the first (or only) dictionary in `params`
"""
from aeon.forecasting.naive import NaiveForecaster
params_list = [
{},
{
"sp": 3,
"seasonal": 7,
"trend": 5,
"seasonal_deg": 2,
"trend_deg": 2,
"robust": True,
"seasonal_jump": 2,
"trend_jump": 2,
"low_pass_jump": 2,
"forecaster_trend": NaiveForecaster(strategy="drift"),
"forecaster_seasonal": NaiveForecaster(sp=3),
"forecaster_resid": NaiveForecaster(strategy="mean"),
},
]
return params_list