-
Notifications
You must be signed in to change notification settings - Fork 3
/
anom_detect.py
293 lines (254 loc) · 11.2 KB
/
anom_detect.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
from __future__ import division
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy import stats
class anom_detect():
"""Anomaly detection for time series data
The method can be used to computed a moving average based on a certain
window size, using a discrete linear convolution method. Anomalous Points
can then be found based on a defined singifcance level using an Extreme Studentized
deviate (ESD) test.
Parameters
----------
method : str
Method used in linear convolution method for dealing with boundaries
window : int
Window size to average data points over for moving average calculation
max_outliers : int
Maximum number of outliers to search for, if set to default
then it will be set to the length of data set. It is recommended
to limit this value to speed up computation.
alpha : float
Significance level for ESD test
mode : {full, valid, same}, default same
Method used in linear convolution method for dealing with boundaries
refer to numpy.convolve for more details regarding methods
Notes
-----
The ESD test can only be used if the residuals are aproximately normally
distributed this condition can be checked using the normality method.
The 'same' option is used for convolution method by default, this means
that the window of averaging must intersect with data points with a
length of >len(lag)/2. This improves dealing with boundary issues.
References
----------
[1] http://www.itl.nist.gov/div898/handbook/eda/section3/eda35h3.htm
[2] https://docs.scipy.org/doc/numpy/reference/generated/numpy.convolve.html
"""
def __init__(self,method='average',window=5,max_outliers=None,alpha=0.05,mode='same'):
self.method = method
self.window = window
self.max_outliers = max_outliers
self.alpha = alpha
self.mode = mode
def moving_average(self,f_t):
'''A moving average calculation (low pass filter) based on discrete
linear convolution
A moving average is calculated using discrete linear convolution.
Parameters
----------
f_t : numpy.array
Data to calculate moving average
Attributes
----------
rolling_mean : numpy.ndarray
The rolling average (filtered) data calcualated based on the window
size set and the inputted raw data.
Notes
-----
For the moment the implementation does not handle sparse time series data
ensure that the data does not have gaps > window or else the averaging will
be greatly impacted. Fill in missing data if possible.
'''
if type(f_t) is not np.ndarray:
raise TypeError\
('Expected one dimensional numpy array.')
if f_t.shape[1] != 1:
raise IndexError\
('Expected one dimensional numpy array, %d dimensions given.' % (f_t.shape[1]))
f_t = f_t.flatten()
window = self.window
mode = self.mode
g_t = np.ones(int(window))/float(window)
# Deal with boundaries with atleast lag/2 day window
#mode = 'same'
rolling_mean = np.convolve(f_t,g_t,mode)
self.rolling_mean = rolling_mean
return rolling_mean
def deviation_stats(self,df):
'''Calculates standard deviation statistics for data
This function calculates the standard deviation of the dataset
and adds the stationary standard deviation (1 and 2 sigma) to the
moving average. For displaying the standard deviation on the Anomaly
plot.
Parameters
----------
df : pandas.DataFrame
DataFrame containing timeseries data points
Attributes
----------
df : pandas.DataFrame
DataFrame containing columns with original data, rolling average
and standard deviation for 1 and 2 sigma above and below the
rolling average.
Notes
-----
For the moment no rolling standard deviation is implemented. The
stationary standard deviation calculated is before removal of Anomalous
points, so will be higher than the actual std. deviation if these where
removed.
'''
df['mean_count'] = self.rolling_mean
df['residual'] = df.iloc[:,0] - self.rolling_mean
std_resid = np.std(df.residual)
df['pos_std'] = df.mean_count + std_resid
df['neg_std'] = df.mean_count - std_resid
df['pos_std_2'] = df.mean_count + 2*std_resid
df['neg_std_2'] = df.mean_count - 2*std_resid
return df
def normality(self):
"""
Plots the distribution and probability plot for the Residuals
These two plots are used for a sanity check to confirm that the
residual between the actual data and the moving average are
approximately normally distributed.
This is important as the ESD test can only be used if the data is
approximately normally distributed. Refer to notes and References
for more details.
"""
if self.results is not None:
df = self.results
fig, (ax1, ax2) = plt.subplots(1, 2,figsize=(10, 6))
x = df.residual.values
re = stats.probplot(x, plot=ax2)
ax1.hist(df.residual,bins=100);
ax1.set_title('Distribution of Residuals');
else:
raise NameError\
('The moving average for the data has not yet been computed. Run moving_averge or evaluate prior to normality.')
def esd_test(self,df_in):
'''Implementation of Generalized ESD test for Outliers
An extension to Grubbs test to k unknown outliers, all that requires
specified is the maximum number of outliers and the confidence
interval. The data must be approximately normal to apply the test.
From Rosner, 1983 [1].
Parameters
----------
df_in : list
Data to be tested for outliers, must be approximately normal
Attributes
----------
ESD_stats : Pandas.DataFrame
Dataframe containing the ESD test statistic and Critical value.
outliers : list
List containing tuple of dataframe index of outlier & the
x value found to be anomolous.
References
----------
[1] http://www.itl.nist.gov/div898/handbook/eda/section3/eda35h3.htm
'''
ind = list(df_in.index)
x = list(df_in.values)
outliers = []
res_lst = [] # ESD Test Statistic for each k anomaly
lam_lst = [] # Critical Value for each k anomaly
n = len(x)
if self.max_outliers is None:
self.max_outliers = len(x)
for i in range(1,self.max_outliers+1):
x_mean = np.mean(x)
x_std = np.std(x,ddof=1)
res = abs((x - x_mean) / x_std)
max_res = np.max(res)
max_ind = np.argmax(res)
p = 1 - self.alpha / (2*(n-i+1))
t_v = stats.t.ppf(p,(n-i-1)) # Get critical values from t-distribution based on p and n
lam_i = ((n-i)*t_v)/ np.sqrt((n-i-1+t_v**2)*(n-i+1)) # Calculate critical region (lambdas)
res_lst.append(max_res)
lam_lst.append(lam_i)
if max_res > lam_i:
outliers.append((ind.pop(max_ind),x.pop(max_ind)))
# Record outlier Points
outliers_index = [x[0] for x in outliers]
ESD_stats = pd.DataFrame()
ESD_stats['ESD Test Statistic'] = res_lst
ESD_stats['Critical Value'] = lam_lst
self.ESD_stats = ESD_stats
return outliers_index
def ESD_plot(self):
"""
esd_plot : bool
This shows the plot of the critical value and test statistic
against number of anomalies removed, showing based on an alpha
the number of anomalies to be removed to be confident of the data.
"""
# Plot will show the point of intersection between critical value
# and ESD test statistic
self.ESD_stats.plot()
def plot(self,data_label=None,left=None,right=None,bottom=None,top=None):
'''Anomalous datapoint plotting method
This can be used to plot a visualisation of the data with the moving
average, standard deviations and the anomalous data points marked.
Parameters
----------
data_label : str
Raw data series name, to be displayed on y-axis and legend.
left : int
xlimit for left limit of plot x-axis
right : int
xlimit for right limit of plot x-axis
bottom : int
ylimit for bottom limit of plot y-axis
top : int
ylimit for top limit of plot y-axis
'''
df = self.results
anoma_points = self.anoma_points
fig, ax1 = plt.subplots(1, 1,figsize=(15, 8))
ax1.plot(list(df.index),df.iloc[:,0],'b.',label=data_label)
ax1.plot(list(df.index),df.mean_count,'r',label='Moving Average')
ax1.fill_between(df.index,df.pos_std,df.neg_std,color='red',alpha=0.3,label='1Sigma')
ax1.fill_between(df.index,df.pos_std_2,df.neg_std_2,color='red',alpha=0.1,label='2Sigma')
ax1.plot(list(anoma_points.index),anoma_points.iloc[:,0],'r*',label='Anomalous Points')
ax1.set_xlabel('time')
ax1.set_ylabel(data_label)
ax1.set_title('Data with Anomalies starred')
ax1.set_xlim(left=left,right=right)
ax1.set_ylim(bottom=bottom,top=top)
ax1.legend();
def evaluate(self,data,anom_detect=True):
'''Anomalous datapoint evaluation method
This method takes a timeseries data set in and calculates the moving
average and if desired identifies the anomalous points in the data set
within the significance level set.
Parameters
----------
data : pandas.DataFrame
Raw data with index as the time component and one dimensional
array for values containing the raw data points.
anom_detect : bool
Identify anomalous data points, default is true, if set to false
only the moving average for the data will be calculated.
Attributes
----------
anoma_points : pandas.DataFrame
DataFrame containing the anomalous datapoints identified.
'''
# Check data is in the right format and right order and dimension
# Check for example if there are large gaps in the data.
df = pd.DataFrame(data)
df.sort_index()
if df.shape[1] != 1:
raise IndexError\
('Insufficient dimensions provided, input data needs time and value columns.')
if self.method == 'average':
data_points = df.values
self.moving_average(data_points)
df = self.deviation_stats(df)
self.results = df
if anom_detect:
outliers_index = self.esd_test(df[['residual']])
anoma_points = pd.DataFrame(df[['sunspots']].iloc[outliers_index,0].sort_index())
self.anoma_points = anoma_points
return anoma_points