-
Notifications
You must be signed in to change notification settings - Fork 89
/
acf.py
266 lines (217 loc) · 9.36 KB
/
acf.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
#!/usr/bin/env python3 -u
# -*- coding: utf-8 -*-
# copyright: aeon developers, BSD-3-Clause License (see LICENSE file)
"""Auto-correlation transformations.
Module :mod:`aeon.transformations.series` implements auto-correlation
transformers.
"""
__author__ = ["afzal442"]
__all__ = ["AutoCorrelationTransformer", "PartialAutoCorrelationTransformer"]
import pandas as pd
from aeon.transformations.base import BaseTransformer
class AutoCorrelationTransformer(BaseTransformer):
"""Auto-correlation transformer.
The autocorrelation function measures how correlated a timeseries is
with itself at different lags. The AutocorrelationTransformer returns
these values as a series for each lag up to the `n_lags` specified.
Parameters
----------
adjusted : bool, default=False
If True, then denominators for autocovariance are n-k, otherwise n.
n_lags : int, default=None
Number of lags to return autocorrelation for. If None,
statsmodels acf function uses min(10 * np.log10(nobs), nobs - 1).
fft : bool, default=False
If True, computes the ACF via FFT.
missing : {"none", "raise", "conservative", "drop"}, default="none"
How missing values are to be treated in autocorrelation function
calculations.
- "none" performs no checks or handling of missing values
- “raise” raises an exception if NaN values are found.
- “drop” removes the missing observations and then estimates the
autocovariances treating the non-missing as contiguous.
- “conservative” computes the autocovariance using nan-ops so that nans
are removed when computing the mean and cross-products that are used to
estimate the autocovariance. "n" in calculation is set to the number of
non-missing observations.
See Also
--------
PartialAutoCorrelationTransformer
Notes
-----
Provides wrapper around statsmodels
`acf <https://www.statsmodels.org/devel/generated/
statsmodels.tsa.stattools.acf.html>`_ function.
Examples
--------
>>> from aeon.transformations.series.acf import AutoCorrelationTransformer
>>> from aeon.datasets import load_airline
>>> y = load_airline() # doctest: +SKIP
>>> transformer = AutoCorrelationTransformer(n_lags=12) # doctest: +SKIP
>>> y_hat = transformer.fit_transform(y) # doctest: +SKIP
"""
_tags = {
"scitype:transform-input": "Series",
# what is the scitype of X: Series, or Panel
"scitype:transform-output": "Series",
# what scitype is returned: Primitives, Series, Panel
"scitype:instancewise": True, # is this an instance-wise transform?
"X_inner_mtype": "pd.Series", # which mtypes do _fit/_predict support for X?
"y_inner_mtype": "None", # which mtypes do _fit/_predict support for y?
"univariate-only": True,
"fit_is_empty": True,
"python_dependencies": "statsmodels",
}
def __init__(
self,
adjusted=False,
n_lags=None,
fft=False,
missing="none",
):
self.adjusted = adjusted
self.n_lags = n_lags
self.fft = fft
self.missing = missing
super(AutoCorrelationTransformer, self).__init__()
def _transform(self, X, y=None):
"""Transform X and return a transformed version.
private _transform containing the core logic, called from transform
Parameters
----------
X : pd.Series
Data to be transformed
y : ignored argument for interface compatibility
Additional data, e.g., labels for transformation
Returns
-------
transformed version of X
"""
from statsmodels.tsa.stattools import acf
# Passing an alpha values other than None would return confidence intervals
# and break the signature of the series-to-series transformer
zt = acf(
X,
adjusted=self.adjusted,
nlags=self.n_lags,
qstat=False,
fft=self.fft,
alpha=None,
missing=self.missing,
)
return pd.Series(zt)
@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`
"""
return [{}, {"n_lags": 1}]
class PartialAutoCorrelationTransformer(BaseTransformer):
"""Partial auto-correlation transformer.
The partial autocorrelation function measures the conditional correlation
between a timeseries and its self at different lags. In particular,
the correlation between a time period and a lag, is calculated conditional
on all the points between the time period and the lag.
The PartialAutoCorrelationTransformer returns
these values as a series for each lag up to the `n_lags` specified.
Parameters
----------
n_lags : int, default=None
Number of lags to return partial autocorrelation for. If None,
statsmodels acf function uses min(10 * np.log10(nobs), nobs // 2 - 1).
method : str, default="ywadjusted"
Specifies which method for the calculations to use.
- "yw" or "ywadjusted" : Yule-Walker with sample-size adjustment in
denominator for acovf. Default.
- "ywm" or "ywmle" : Yule-Walker without adjustment.
- "ols" : regression of time series on lags of it and on constant.
- "ols-inefficient" : regression of time series on lags using a single
common sample to estimate all pacf coefficients.
- "ols-adjusted" : regression of time series on lags with a bias
adjustment.
- "ld" or "ldadjusted" : Levinson-Durbin recursion with bias
correction.
- "ldb" or "ldbiased" : Levinson-Durbin recursion without bias
correction.
See Also
--------
AutoCorrelationTransformer
Notes
-----
Provides wrapper around statsmodels
`pacf <https://www.statsmodels.org/devel/generated/
statsmodels.tsa.stattools.pacf.html>`_ function.
Examples
--------
>>> from aeon.transformations.series.acf import PartialAutoCorrelationTransformer
>>> from aeon.datasets import load_airline
>>> y = load_airline() # doctest: +SKIP
>>> transformer = PartialAutoCorrelationTransformer(n_lags=12) # doctest: +SKIP
>>> y_hat = transformer.fit_transform(y) # doctest: +SKIP
"""
_tags = {
"scitype:transform-input": "Series",
# what is the scitype of X: Series, or Panel
"scitype:transform-output": "Series",
# what scitype is returned: Primitives, Series, Panel
"scitype:instancewise": True, # is this an instance-wise transform?
"X_inner_mtype": "pd.Series", # which mtypes do _fit/_predict support for X?
"y_inner_mtype": "None", # which mtypes do _fit/_predict support for y?
"univariate-only": True,
"fit_is_empty": True,
"python_dependencies": "statsmodels",
}
def __init__(
self,
n_lags=None,
method="ywadjusted",
):
self.n_lags = n_lags
self.method = method
super(PartialAutoCorrelationTransformer, self).__init__()
def _transform(self, X, y=None):
"""Transform X and return a transformed version.
private _transform containing the core logic, called from transform
Parameters
----------
X : pd.Series
Data to be transformed
y : ignored argument for interface compatibility
Additional data, e.g., labels for transformation
Returns
-------
transformed version of X
"""
from statsmodels.tsa.stattools import pacf
# Passing an alpha values other than None would return confidence intervals
# and break the signature of the series-to-series transformer
zt = pacf(X, nlags=self.n_lags, method=self.method, alpha=None)
return pd.Series(zt)
@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`
"""
return [{}, {"n_lags": 1}]