-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
_update.py
474 lines (389 loc) · 19.4 KB
/
_update.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
"""Compositors that control stream and refitting behaviour of update."""
# copyright: sktime developers, BSD-3-Clause License (see LICENSE file)
__author__ = ["fkiraly"]
import pandas as pd
from sktime.datatypes import ALL_TIME_SERIES_MTYPES
from sktime.datatypes._utilities import get_window
from sktime.forecasting.base._delegate import _DelegatedForecaster
class UpdateRefitsEvery(_DelegatedForecaster):
"""Refits periodically when update is called.
If update is called with update_params=True and refit_interval or more has
elapsed since the last fit, refits the forecaster instead (call to fit).
refitting is done on (potentially) all data seen so far.
refit_window controls the lookback window on which refitting is done
refit data is cutoff (inclusive) to cutoff minus refit_window (exclusive)
Parameters
----------
refit_interval : difference of sktime time indices (int or timedelta), optional
interval that needs to elapse after which the first update defaults to fit
default = 0, i.e., always refits, never updates
if index of y seen in fit is integer or y is index-free container type,
refit_interval must be int, and is interpreted as difference of int location
if index of y seen in fit is timestamp, must be int or pd.Timedelta
if pd.Timedelta, will be interpreted as time since last refit elapsed
if int, will be interpreted as number of time stamps seen since last refit
refit_window_size : difference of sktime time indices (int or timedelta), optional
length of the data window to refit to in case update calls fit
default = inf, i.e., refits to entire training data seen so far
refit_window_lag : difference of sktime indices (int or timedelta), optional
lag of the data window to refit to, w.r.t. cutoff, in case update calls fit
default = 0, i.e., refit window ends with and includes cutoff
"""
# attribute for _DelegatedForecaster, which then delegates
# all non-overridden methods are same as of getattr(self, _delegate_name)
# see further details in _DelegatedForecaster docstring
_delegate_name = "forecaster_"
_tags = {
"authors": "fkiraly",
"fit_is_empty": False,
"requires-fh-in-fit": False,
"y_inner_mtype": ALL_TIME_SERIES_MTYPES,
"X_inner_mtype": ALL_TIME_SERIES_MTYPES,
}
def __init__(
self, forecaster, refit_interval=0, refit_window_size=None, refit_window_lag=0
):
self.forecaster = forecaster
self.forecaster_ = forecaster.clone()
self.refit_interval = refit_interval
self.refit_window_size = refit_window_size
self.refit_window_lag = refit_window_lag
super().__init__()
self._set_delegated_tags(self.forecaster_)
self.set_tags(**{"fit_is_empty": False})
def _fit(self, y, X, fh):
"""Fit forecaster to training data.
private _fit containing the core logic, called from fit
Writes to self:
Sets fitted model attributes ending in "_".
Parameters
----------
y : guaranteed to be of a type in self.get_tag("y_inner_mtype")
Time series to which to fit the forecaster.
if self.get_tag("scitype:y")=="univariate":
guaranteed to have a single column/variable
if self.get_tag("scitype:y")=="multivariate":
guaranteed to have 2 or more columns
if self.get_tag("scitype:y")=="both": no restrictions apply
fh : guaranteed to be ForecastingHorizon or None, optional (default=None)
The forecasting horizon with the steps ahead to to predict.
Required (non-optional) here if self.get_tag("requires-fh-in-fit")==True
Otherwise, if not passed in _fit, guaranteed to be passed in _predict
X : optional (default=None)
guaranteed to be of a type in self.get_tag("X_inner_mtype")
Exogeneous time series to fit to.
Returns
-------
self : reference to self
"""
# we need to remember the time we last fit, to compare to it in _update
self.last_fit_cutoff_ = self.cutoff[0]
estimator = self._get_delegate()
estimator.fit(y=y, fh=fh, X=X)
return self
def _update(self, y, X=None, update_params=True):
"""Update time series to incremental training data.
private _update containing the core logic, called from update
State required:
Requires state to be "fitted".
Accesses in self:
Fitted model attributes ending in "_"
self.cutoff
Writes to self:
Sets fitted model attributes ending in "_", if update_params=True.
Does not write to self if update_params=False.
Parameters
----------
y : guaranteed to be of a type in self.get_tag("y_inner_mtype")
Time series with which to update the forecaster.
if self.get_tag("scitype:y")=="univariate":
guaranteed to have a single column/variable
if self.get_tag("scitype:y")=="multivariate":
guaranteed to have 2 or more columns
if self.get_tag("scitype:y")=="both": no restrictions apply
X : pd.DataFrame, optional (default=None)
Exogenous time series
update_params : bool, optional (default=True)
whether model parameters should be updated
Returns
-------
self : reference to self
"""
estimator = self._get_delegate()
time_since_last_fit = self.cutoff[0] - self.last_fit_cutoff_
refit_interval = self.refit_interval
refit_window_size = self.refit_window_size
refit_window_lag = self.refit_window_lag
_y = self._y
_X = self._X
# treat situation where indexing of y is in timedelta but differences are int
# in that case, interpret any integers as iloc index differences
# and replace integers with timedelta quantities before proceeding
if isinstance(time_since_last_fit, pd.Timedelta):
if isinstance(refit_window_lag, int):
lag = min(refit_window_lag, len(_y))
refit_window_lag = self.cutoff[0] - _y.index[-lag]
if isinstance(refit_window_size, int):
_y_lag = get_window(_y, lag=refit_window_lag)
window_size = min(refit_window_size, len(_y_lag))
refit_window_size = _y_lag.index[-window_size]
if isinstance(refit_interval, int):
index = min(refit_interval, len(_y))
refit_interval = self.cutoff[0] - _y.index[-index]
# case distinction based on whether the refit_interval period has elapsed
# if yes: call fit, on the specified window sub-set of all observed data
if time_since_last_fit >= refit_interval and update_params:
if refit_window_size is not None or refit_window_lag != 0:
y_win = get_window(
_y, window_length=refit_window_size, lag=refit_window_lag
)
X_win = get_window(
_X, window_length=refit_window_size, lag=refit_window_lag
)
else:
y_win = _y
X_win = _X
fh = self._fh
estimator.fit(y=y_win, X=X_win, fh=fh, update_params=update_params)
# remember that we just fitted the estimator
self.last_fit_cutoff_ = self.cutoff[0]
else:
# if no: call update as usual
estimator.update(y=y, X=X, update_params=update_params)
return self
@classmethod
def get_test_params(cls):
"""Return testing parameter settings for the estimator.
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 sktime.forecasting.trend import TrendForecaster
forecaster = TrendForecaster.create_test_instance()
param1 = {"forecaster": forecaster}
param2 = {"forecaster": forecaster, "refit_interval": 2, "refit_window_size": 3}
return [param1, param2]
class UpdateEvery(_DelegatedForecaster):
"""Update only periodically when update is called.
If update is called, behaves like update_params=False, unless update_interval has
elapsed since the last "true" update = inner update with update_params=False.
update_window controls the lookback window on which refitting is done
refit data is cutoff (inclusive) to cutoff minus refit_window (exclusive)
Caution: default value of update_interval means *no updates* after ``fit``.
Parameters
----------
update_interval : difference of sktime time indices (int or timedelta), optional
interval that needs to elapse until inner update call with update_params=True
default = None = infinity, i.e., never updates
if index of y seen in fit is integer or y is index-free container type,
update_interval must be int, is interpreted as difference of int location
if index of y seen in fit is timestamp, must be int or pd.Timedelta
if pd.Timedelta, will be interpreted as time since last true update elapsed
if int, will be interpreted as number of time stamps seen since last update
"""
# attribute for _DelegatedForecaster, which then delegates
# all non-overridden methods are same as of getattr(self, _delegate_name)
# see further details in _DelegatedForecaster docstring
_delegate_name = "forecaster_"
_tags = {
"authors": "fkiraly",
"fit_is_empty": False,
"requires-fh-in-fit": False,
"y_inner_mtype": ALL_TIME_SERIES_MTYPES,
"X_inner_mtype": ALL_TIME_SERIES_MTYPES,
}
def __init__(self, forecaster, update_interval=None):
self.forecaster = forecaster
self.forecaster_ = forecaster.clone()
self.update_interval = update_interval
super().__init__()
self._set_delegated_tags(self.forecaster_)
self.set_tags(**{"fit_is_empty": False})
def _fit(self, y, X, fh):
"""Fit forecaster to training data.
private _fit containing the core logic, called from fit
Writes to self:
Sets fitted model attributes ending in "_".
Parameters
----------
y : guaranteed to be of a type in self.get_tag("y_inner_mtype")
Time series to which to fit the forecaster.
if self.get_tag("scitype:y")=="univariate":
guaranteed to have a single column/variable
if self.get_tag("scitype:y")=="multivariate":
guaranteed to have 2 or more columns
if self.get_tag("scitype:y")=="both": no restrictions apply
fh : guaranteed to be ForecastingHorizon or None, optional (default=None)
The forecasting horizon with the steps ahead to to predict.
Required (non-optional) here if self.get_tag("requires-fh-in-fit")==True
Otherwise, if not passed in _fit, guaranteed to be passed in _predict
X : optional (default=None)
guaranteed to be of a type in self.get_tag("X_inner_mtype")
Exogeneous time series to fit to.
Returns
-------
self : reference to self
"""
# we need to remember the time we last fit, to compare to it in _update
self.last_update_cutoff_ = self.cutoff[0]
estimator = self._get_delegate()
estimator.fit(y=y, fh=fh, X=X)
return self
def _update(self, y, X=None, update_params=True):
"""Update time series to incremental training data.
private _update containing the core logic, called from update
State required:
Requires state to be "fitted".
Accesses in self:
Fitted model attributes ending in "_"
self.cutoff
Writes to self:
Sets fitted model attributes ending in "_", if update_params=True.
Does not write to self if update_params=False.
Parameters
----------
y : guaranteed to be of a type in self.get_tag("y_inner_mtype")
Time series with which to update the forecaster.
if self.get_tag("scitype:y")=="univariate":
guaranteed to have a single column/variable
if self.get_tag("scitype:y")=="multivariate":
guaranteed to have 2 or more columns
if self.get_tag("scitype:y")=="both": no restrictions apply
X : pd.DataFrame, optional (default=None)
Exogenous time series
update_params : bool, optional (default=True)
whether model parameters should be updated
Returns
-------
self : reference to self
"""
estimator = self._get_delegate()
time_since_last_update = self.cutoff[0] - self.last_update_cutoff_
update_interval = self.update_interval
_y = self._y
# treat situation where indexing of y is in timedelta but differences are int
# in that case, interpret any integers as iloc index differences
# and replace integers with timedelta quantities before proceeding
if isinstance(time_since_last_update, pd.Timedelta):
if isinstance(update_interval, int):
index = min(update_interval, len(_y))
update_interval = self.cutoff[0] - _y.index[-index]
# case distinction based on whether the update_interval period has elapsed
# (None update_interval means infinite update_interval)
# if yes: call inner update with update_params=True, aka "true" update
if update_interval is not None and time_since_last_update >= update_interval:
estimator.update(y=y, X=X, update_params=update_params)
# remember that we just updated the estimator
self.last_update_cutoff_ = self.cutoff[0]
else:
# if no: call update, but with update_params=False
estimator.update(y=y, X=X, update_params=False)
return self
@classmethod
def get_test_params(cls):
"""Return testing parameter settings for the estimator.
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 sktime.forecasting.trend import TrendForecaster
forecaster = TrendForecaster.create_test_instance()
param1 = {"forecaster": forecaster}
param2 = {"forecaster": forecaster, "update_interval": 2}
return [param1, param2]
class DontUpdate(_DelegatedForecaster):
"""Turns off updates, i.e., ensures that forecaster is only fit and never updated.
This is useful when comparing forecasters that update with forecasters that don't,
in a set-up where all forecasters' ``update`` has ``update_params=True`` set.
Shorthand for UpdateEvery with default values.
Parameters
----------
refit_interval : difference of sktime time indices (int or timedelta), optional
interval that needs to elapse after which the first update defaults to fit
default = 0, i.e., always refits, never updates
if index of y seen in fit is integer or y is index-free container type,
refit_interval must be int, and is interpreted as difference of int location
if index of y seen in fit is timestamp, must be int or pd.Timedelta
if pd.Timedelta, will be interpreted as time since last refit elapsed
if int, will be interpreted as number of time stamps seen since last refit
refit_window_size : difference of sktime time indices (int or timedelta), optional
length of the data window to refit to in case update calls fit
default = inf, i.e., refits to entire training data seen so far
refit_window_lag : difference of sktime indices (int or timedelta), optional
lag of the data window to refit to, w.r.t. cutoff, in case update calls fit
default = 0, i.e., refit window ends with and includes cutoff
"""
# attribute for _DelegatedForecaster, which then delegates
# all non-overridden methods are same as of getattr(self, _delegate_name)
# see further details in _DelegatedForecaster docstring
_delegate_name = "forecaster_"
_tags = {
"authors": "fkiraly",
"fit_is_empty": False,
"requires-fh-in-fit": False,
"y_inner_mtype": ALL_TIME_SERIES_MTYPES,
"X_inner_mtype": ALL_TIME_SERIES_MTYPES,
}
def __init__(self, forecaster):
self.forecaster = forecaster
self.forecaster_ = forecaster.clone()
super().__init__()
self._set_delegated_tags(self.forecaster_)
self.set_tags(**{"fit_is_empty": False})
def _update(self, y, X=None, update_params=True):
"""Update time series to incremental training data.
private _update containing the core logic, called from update
State required:
Requires state to be "fitted".
Accesses in self:
Fitted model attributes ending in "_"
self.cutoff
Writes to self:
Sets fitted model attributes ending in "_", if update_params=True.
Does not write to self if update_params=False.
Parameters
----------
y : guaranteed to be of a type in self.get_tag("y_inner_mtype")
Time series with which to update the forecaster.
if self.get_tag("scitype:y")=="univariate":
guaranteed to have a single column/variable
if self.get_tag("scitype:y")=="multivariate":
guaranteed to have 2 or more columns
if self.get_tag("scitype:y")=="both": no restrictions apply
X : pd.DataFrame, optional (default=None)
Exogenous time series
update_params : bool, optional (default=True)
whether model parameters should be updated
Returns
-------
self : reference to self
"""
estimator = self._get_delegate()
# we need to call this to ensure cutoff of estimator is updated
estimator.update(y=y, X=X, update_params=False)
return self
@classmethod
def get_test_params(cls):
"""Return testing parameter settings for the estimator.
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 sktime.forecasting.trend import TrendForecaster
forecaster = TrendForecaster.create_test_instance()
return {"forecaster": forecaster}