forked from tinkoff-ai/etna
-
Notifications
You must be signed in to change notification settings - Fork 6
/
fourier.py
392 lines (318 loc) · 14.4 KB
/
fourier.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
import math
from typing import Dict
from typing import List
from typing import Optional
from typing import Sequence
from typing import Union
import numpy as np
import pandas as pd
from etna.datasets import TSDataset
from etna.datasets.utils import determine_freq
from etna.datasets.utils import determine_num_steps
from etna.distributions import BaseDistribution
from etna.distributions import IntDistribution
from etna.transforms.base import IrreversibleTransform
_DEFAULT_FREQ = object()
class FourierTransform(IrreversibleTransform):
"""Adds fourier features to the dataset.
Transform can work with two types of timestamp data: numeric and datetime.
Transform can accept timestamp data in two forms:
- As index. In this case the dataset index is used to compute features.
The features will be the same for each segment.
- As external column. In this case for each segment its ``in_column`` will be used to compute features.
It is expected that for each segment we have the same type of timestamp data (datetime or numeric),
and for datetime type only one frequency is used for all the segments.
If we are working with external column, there is a difference in handling numeric and datetime data:
- Numeric data can have missing values at any place.
- Datetime data could have missing values only at the beginning of each segment.
Notes
-----
To understand how transform works we recommend reading:
`Fourier series <https://otexts.com/fpp3/useful-predictors.html#fourier-series>`_.
If we already have a numeric data then for a mode $m$ with a period $p$ we have:
.. math::
& k = \\left \\lfloor \\frac{m}{2} \\right \\rfloor
\\\\
& f_{m, i} = \\sin \\left( \\frac{2 \\pi k i}{p} + \\frac{\\pi}{2} (m \\mod 2) \\right)
If we have datetime data, then it first should be transformed into numeric.
During fitting the transform saves frequency and some datetime timestamp as a reference point.
During transformation it uses reference point to compute number of frequency units between reference point and each timestamp.
"""
def __init__(
self,
period: float,
order: Optional[int] = None,
mods: Optional[Sequence[int]] = None,
out_column: Optional[str] = None,
in_column: Optional[str] = None,
):
"""Create instance of FourierTransform.
Parameters
----------
period:
the period of the seasonality to capture in frequency units of time series;
``period`` should be >= 2
order:
upper order of Fourier components to include;
``order`` should be >= 1 and <= ceil(period/2))
mods:
alternative and precise way of defining which harmonics will be used,
for example, ``order=2`` can be represented as ``mods=[1, 2, 3, 4]`` if ``period`` > 4 and
as ``mods=[1, 2, 3]`` if 3 <= ``period`` <= 4.
``mods`` should be >= 1 and < period
out_column:
* if set, name of added column, the final name will be '{out_columnt}_{mod}';
* if don't set, name will be ``transform.__repr__()``,
repr will be made for transform that creates exactly this column
in_column:
name of column to work with:
* if ``in_column`` is ``None`` (default) both datetime and integer timestamps are supported;
* if ``in_column`` isn't ``None`` datetime and numeric columns are supported,
but for datetime values only regular timestamps with some frequency are supported
Raises
------
ValueError:
if period < 2
ValueError:
if both or none of order, mods is set
ValueError:
if order is < 1 or > ceil(period/2)
ValueError:
if at least one mod is < 1 or >= period
"""
if period < 2:
raise ValueError("Period should be at least 2")
self.period = period
self.order = order
self.mods = mods
self._mods: Sequence[int]
if order is not None and mods is None:
if order < 1 or order > math.ceil(period / 2):
raise ValueError("Order should be within [1, ceil(period/2)] range")
self._mods = [mod for mod in range(1, 2 * order + 1) if mod < period]
elif mods is not None and order is None:
if min(mods) < 1 or max(mods) >= period:
raise ValueError("Every mod should be within [1, int(period)) range")
self._mods = mods
else:
raise ValueError("There should be exactly one option set: order or mods")
self.out_column = out_column
self.in_column = in_column
self._reference_timestamp: Union[pd.Timestamp, int, None] = None
self._freq: Optional[str] = _DEFAULT_FREQ # type: ignore
if self.in_column is None:
self.in_column_regressor: Optional[bool] = True
else:
self.in_column_regressor = None
if in_column is None:
required_features = ["target"]
else:
required_features = [in_column]
super().__init__(required_features=required_features)
def _get_column_name(self, mod: int) -> str:
if self.out_column is None:
return f"{FourierTransform(period=self.period, mods=[mod]).__repr__()}"
else:
return f"{self.out_column}_{mod}"
def get_regressors_info(self) -> List[str]:
"""Return the list with regressors created by the transform."""
if self.in_column_regressor is None:
raise ValueError("Fit the transform to get the correct regressors info!")
if not self.in_column_regressor:
return []
output_columns = [self._get_column_name(mod=mod) for mod in self._mods]
return output_columns
def fit(self, ts: TSDataset) -> "FourierTransform":
"""Fit the transform.
Parameters
----------
ts:
Dataset to fit the transform on.
Returns
-------
:
The fitted transform instance.
Raises
------
ValueError
if external timestamp doesn't have frequency
ValueError
if external timestamp doesn't have the same frequency for all segments
"""
if self.in_column is None:
self._freq = ts.freq
self.in_column_regressor = True
else:
self.in_column_regressor = self.in_column in ts.regressors
super().fit(ts)
return self
def _validate_external_timestamps(self, df: pd.DataFrame):
df = df.droplevel("feature", axis=1)
# here we are assuming that every segment has the same timestamp dtype
timestamp_dtype = df.dtypes.iloc[0]
if not pd.api.types.is_datetime64_dtype(timestamp_dtype):
return
segments = df.columns.unique()
freq_values = set()
for segment in segments:
timestamps = df[segment]
timestamps = timestamps.loc[timestamps.first_valid_index() :]
if len(timestamps) >= 3:
cur_freq = pd.infer_freq(timestamps)
if cur_freq is None:
raise ValueError(
f"Invalid in_column values! Datetime values should be regular timestamps with some frequency. "
f"This doesn't hold for segment {segment}"
)
freq_values.add(cur_freq)
if len(freq_values) > 1:
raise ValueError(
f"Invalid in_column values! Datetime values should have the same frequency for every segment. "
f"Discovered frequencies: {freq_values}"
)
def _infer_external_freq(self, df: pd.DataFrame) -> Optional[str]:
df = df.droplevel("feature", axis=1)
# here we are assuming that every segment has the same timestamp dtype
timestamp_dtype = df.dtypes.iloc[0]
if not pd.api.types.is_datetime64_dtype(timestamp_dtype):
return None
sample_segment = df.columns[0]
sample_timestamps = df[sample_segment]
sample_timestamps = sample_timestamps.loc[sample_timestamps.first_valid_index() :]
result = determine_freq(sample_timestamps)
return result
def _infer_external_reference_timestamp(self, df: pd.DataFrame) -> Union[pd.Timestamp, int]:
# here we are assuming that every segment has the same timestamp dtype
timestamp_dtype = df.dtypes.iloc[0]
if not pd.api.types.is_datetime64_dtype(timestamp_dtype):
return 0
sample_segment = df.columns[0]
sample_timestamps = df[sample_segment]
reference_timestamp = sample_timestamps.loc[sample_timestamps.first_valid_index()]
return reference_timestamp
def _fit(self, df: pd.DataFrame) -> "FourierTransform":
"""Fit method does nothing and is kept for compatibility.
Parameters
----------
df:
dataframe with data.
Returns
-------
:
The fitted transform instance.
Raises
------
ValueError
if external timestamp doesn't have frequency
ValueError
if external timestamp doesn't have the same frequency for all segments
"""
if self.in_column is None:
self._reference_timestamp = df.index[0]
else:
self._validate_external_timestamps(df)
self._freq = self._infer_external_freq(df)
self._reference_timestamp = self._infer_external_reference_timestamp(df)
return self
@staticmethod
def _construct_answer_for_index(df: pd.DataFrame, features: pd.DataFrame) -> pd.DataFrame:
dataframes = []
for seg in df.columns.get_level_values("segment").unique():
tmp = df[seg].join(features)
_idx = tmp.columns.to_frame()
_idx.insert(0, "segment", seg)
tmp.columns = pd.MultiIndex.from_frame(_idx)
dataframes.append(tmp)
result = pd.concat(dataframes, axis=1).sort_index(axis=1)
result.columns.names = ["segment", "feature"]
return result
def _compute_features(self, timestamps: pd.Series) -> pd.DataFrame:
features = pd.DataFrame(index=timestamps.index)
elapsed = timestamps / self.period
for mod in self._mods:
order = (mod + 1) // 2
is_cos = mod % 2 == 0
features[self._get_column_name(mod)] = np.sin(2 * np.pi * order * elapsed + np.pi / 2 * is_cos)
return features
def _convert_regular_timestamps_datetime_to_numeric(
self, timestamps: pd.Series, reference_timestamp: pd.Timestamp, freq: Optional[str]
) -> pd.Series:
# we should always align timestamps to some fixed point
end_timestamp = timestamps.iloc[-1]
if end_timestamp >= reference_timestamp:
end_idx = determine_num_steps(start_timestamp=reference_timestamp, end_timestamp=end_timestamp, freq=freq)
else:
end_idx = -determine_num_steps(start_timestamp=end_timestamp, end_timestamp=reference_timestamp, freq=freq)
numeric_timestamp = pd.Series(np.arange(end_idx - len(timestamps) + 1, end_idx + 1))
return numeric_timestamp
def _transform(self, df: pd.DataFrame) -> pd.DataFrame:
"""Add harmonics to the dataset.
Parameters
----------
df:
dataframe with data to transform.
Returns
-------
:
transformed dataframe
Raises
------
ValueError:
if transform isn't fitted
ValueError
if external timestamp doesn't have frequency
ValueError
if external timestamp doesn't have the same frequency for all segments
"""
if self._freq is _DEFAULT_FREQ:
raise ValueError("The transform isn't fitted!")
if self.in_column is None:
if pd.api.types.is_integer_dtype(df.index.dtype):
timestamps = df.index.to_series()
else:
timestamps = self._convert_regular_timestamps_datetime_to_numeric(
timestamps=df.index.to_series(), reference_timestamp=self._reference_timestamp, freq=self._freq
)
features = self._compute_features(timestamps=timestamps)
features.index = df.index
result = self._construct_answer_for_index(df=df, features=features)
else:
# here we are assuming that every segment has the same timestamp dtype
timestamp_dtype = df.dtypes.iloc[0]
if pd.api.types.is_numeric_dtype(timestamp_dtype):
flat_df = TSDataset.to_flatten(df=df)
timestamps = flat_df[self.in_column]
else:
self._validate_external_timestamps(df=df)
segments = df.columns.get_level_values("segment").unique()
int_values = []
for segment in segments:
segment_timestamps = df[segment][self.in_column]
int_segment = self._convert_regular_timestamps_datetime_to_numeric(
timestamps=segment_timestamps,
reference_timestamp=self._reference_timestamp,
freq=self._freq,
)
int_values.append(int_segment)
df_int = pd.DataFrame(np.array(int_values).T, index=df.index, columns=df.columns)
flat_df = TSDataset.to_flatten(df=df_int)
timestamps = flat_df[self.in_column]
features = self._compute_features(timestamps=timestamps)
features["timestamp"] = flat_df["timestamp"]
features["segment"] = flat_df["segment"]
wide_df = TSDataset.to_dataset(features)
result = pd.concat([df, wide_df], axis=1).sort_index(axis=1)
return result
def params_to_tune(self) -> Dict[str, BaseDistribution]:
"""Get default grid for tuning hyperparameters.
If ``self.order`` is set then this grid tunes ``order`` parameter:
Other parameters are expected to be set by the user.
Returns
-------
:
Grid to tune.
"""
if self.mods is not None:
return {}
max_value = math.ceil(self.period / 2)
return {"order": IntDistribution(low=1, high=max_value, log=True)}