-
Notifications
You must be signed in to change notification settings - Fork 19
/
MOMspot.py
393 lines (329 loc) · 13.3 KB
/
MOMspot.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
# -*- coding: utf-8 -*-
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from math import log,floor
import tqdm
from scipy.optimize import minimize
# colors for plot
deep_saffron = '#FF9933'
air_force_blue = '#5D8AA8'
def backMean(X,d):
M = []
w = X[:d].sum()
M.append(w/d)
for i in range(d,len(X)):
w = w - X[i-d] + X[i]
M.append(w/d)
return np.array(M)
class momSPOT:
"""
This class allows to run biSPOT algorithm on univariate dataset (upper and lower bounds)
Attributes
----------
proba : float
Detection level (risk), chosen by the user
extreme_quantile : float
current threshold (bound between normal and abnormal events)
data : numpy.array
stream
init_data : numpy.array
initial batch of observations (for the calibration/initialization step)
init_threshold : float
initial threshold computed during the calibration step
peaks : numpy.array
array of peaks (excesses above the initial threshold)
n : int
number of observed values
Nt : int
number of observed peaks
"""
def __init__(self, q = 1e-4):
"""
Constructor
Parameters
----------
q
Detection level (risk)
Returns
----------
biSPOT object
"""
self.proba = q
self.data = None
self.init_data = None
self.n = 0
nonedict = {'up':None,'down':None}
self.extreme_quantile = dict.copy(nonedict)
self.init_threshold = dict.copy(nonedict)
self.peaks = dict.copy(nonedict)
self.gamma = dict.copy(nonedict)
self.sigma = dict.copy(nonedict)
self.Nt = {'up':0,'down':0}
def __str__(self):
s = ''
s += 'Streaming Peaks-Over-Threshold Object\n'
s += 'Detection level q = %s\n' % self.proba
if self.data is not None:
s += 'Data imported : Yes\n'
s += '\t initialization : %s values\n' % self.init_data.size
s += '\t stream : %s values\n' % self.data.size
else:
s += 'Data imported : No\n'
return s
if self.n == 0:
s += 'Algorithm initialized : No\n'
else:
s += 'Algorithm initialized : Yes\n'
s += '\t initial threshold : %s\n' % self.init_threshold
r = self.n-self.init_data.size
if r > 0:
s += 'Algorithm run : Yes\n'
s += '\t number of observations : %s (%.2f %%)\n' % (r,100*r/self.n)
s += '\t triggered alarms : %s (%.2f %%)\n' % (len(self.alarm),100*len(self.alarm)/self.n)
else:
s += '\t number of peaks : %s\n' % self.Nt
s += '\t upper extreme quantile : %s\n' % self.extreme_quantile['up']
s += '\t lower extreme quantile : %s\n' % self.extreme_quantile['down']
s += 'Algorithm run : No\n'
return s
def fit(self,init_data,data):
"""
Import data to biSPOT object
Parameters
----------
init_data : list, numpy.array or pandas.Series
initial batch to calibrate the algorithm ()
data : numpy.array
data for the run (list, np.array or pd.series)
"""
if isinstance(data,list):
self.data = np.array(data)
elif isinstance(data,np.ndarray):
self.data = data
elif isinstance(data,pd.Series):
self.data = data.values
else:
print('This data format (%s) is not supported' % type(data))
return
if isinstance(init_data,list):
self.init_data = np.array(init_data)
elif isinstance(init_data,np.ndarray):
self.init_data = init_data
elif isinstance(init_data,pd.Series):
self.init_data = init_data.values
elif isinstance(init_data,int):
self.init_data = self.data[:init_data]
self.data = self.data[init_data:]
elif isinstance(init_data,float) & (init_data<1) & (init_data>0):
r = int(init_data*data.size)
self.init_data = self.data[:r]
self.data = self.data[r:]
else:
print('The initial data cannot be set')
return
def add(self,data):
"""
This function allows to append data to the already fitted data
Parameters
----------
data : list, numpy.array, pandas.Series
data to append
"""
if isinstance(data,list):
data = np.array(data)
elif isinstance(data,np.ndarray):
data = data
elif isinstance(data,pd.Series):
data = data.values
else:
print('This data format (%s) is not supported' % type(data))
return
self.data = np.append(self.data,data)
return
def initialize(self, verbose = True):
"""
Run the calibration (initialization) step
Parameters
----------
verbose : bool
(default = True) If True, gives details about the batch initialization
"""
n_init = self.init_data.size
S = np.sort(self.init_data) # we sort X to get the empirical quantile
self.init_threshold['up'] = S[int(0.98*n_init)] # t is fixed for the whole algorithm
self.init_threshold['down'] = S[int(0.02*n_init)] # t is fixed for the whole algorithm
# initial peaks
self.peaks['up'] = self.init_data[self.init_data>self.init_threshold['up']]-self.init_threshold['up']
self.peaks['down'] = -(self.init_data[self.init_data<self.init_threshold['down']]-self.init_threshold['down'])
self.Nt['up'] = self.peaks['up'].size
self.Nt['down'] = self.peaks['down'].size
self.n = n_init
if verbose:
print('Initial threshold : %s' % self.init_threshold)
print('Number of peaks : %s' % self.Nt)
#print('Grimshaw maximum log-likelihood estimation ... ', end = '')
l = {'up':None,'down':None}
for side in ['up','down']:
g,s,l[side] = self._MOM(side)
self.extreme_quantile[side] = self._quantile(side,g,s)
self.gamma[side] = g
self.sigma[side] = s
ltab = 20
form = ('\t'+'%20s' + '%20.2f' + '%20.2f')
'''
if verbose:
print('[done]')
print('\t' + 'Parameters'.rjust(ltab) + 'Upper'.rjust(ltab) + 'Lower'.rjust(ltab))
print('\t' + '-'*ltab*3)
print(form % (chr(0x03B3),self.gamma['up'],self.gamma['down']))
print(form % (chr(0x03C3),self.sigma['up'],self.sigma['down']))
print(form % ('likelihood',l['up'],l['down']))
print(form % ('Extreme quantile',self.extreme_quantile['up'],self.extreme_quantile['down']))
print('\t' + '-'*ltab*3)
'''
return
def _MOM(self,side,epsilon = 1e-8, n_points = 10):
Yi = self.peaks[side]
avg = np.mean(Yi)
var = np.var(Yi)
sigma = 0.5*avg*(avg**2/var + 1)
gamma = 0.5*(avg**2/var - 1)
print gamma, sigma
return gamma,sigma,100
def _quantile(self,side,gamma,sigma):
"""
Compute the quantile at level 1-q for a given side
Parameters
----------
side : str
'up' or 'down'
gamma : float
GPD parameter
sigma : float
GPD parameter
Returns
----------
float
quantile at level 1-q for the GPD(γ,σ,μ=0)
"""
if side == 'up':
r = self.n * self.proba / self.Nt[side]
if gamma != 0:
return self.init_threshold['up'] + (sigma/gamma)*(pow(r,-gamma)-1)
else:
return self.init_threshold['up'] - sigma*log(r)
elif side == 'down':
r = self.n * self.proba / self.Nt[side]
if gamma != 0:
return self.init_threshold['down'] - (sigma/gamma)*(pow(r,-gamma)-1)
else:
return self.init_threshold['down'] + sigma*log(r)
else:
print('error : the side is not right')
def run(self, with_alarm = True):
"""
Run biSPOT on the stream
Parameters
----------
with_alarm : bool
(default = True) If False, SPOT will adapt the threshold assuming \
there is no abnormal values
Returns
----------
dict
keys : 'upper_thresholds', 'lower_thresholds' and 'alarms'
'***-thresholds' contains the extreme quantiles and 'alarms' contains \
the indexes of the values which have triggered alarms
"""
if (self.n>self.init_data.size):
print('Warning : the algorithm seems to have already been run, you \
should initialize before running again')
return {}
# list of the thresholds
thup = []
thdown = []
alarm = []
# Loop over the stream
for i in tqdm.tqdm(range(self.data.size)):
# If the observed value exceeds the current threshold (alarm case)
if self.data[i]>self.extreme_quantile['up'] :
# if we want to alarm, we put it in the alarm list
if with_alarm:
alarm.append(i)
# otherwise we add it in the peaks
else:
self.peaks['up'] = np.append(self.peaks['up'],self.data[i]-self.init_threshold['up'])
self.Nt['up'] += 1
self.n += 1
# and we update the thresholds
g,s,l = self._MOM('up')
self.extreme_quantile['up'] = self._quantile('up',g,s)
# case where the value exceeds the initial threshold but not the alarm ones
elif self.data[i]>self.init_threshold['up']:
# we add it in the peaks
self.peaks['up'] = np.append(self.peaks['up'],self.data[i]-self.init_threshold['up'])
self.Nt['up'] += 1
self.n += 1
# and we update the thresholds
g,s,l = self._MOM('up')
self.extreme_quantile['up'] = self._quantile('up',g,s)
elif self.data[i]<self.extreme_quantile['down'] :
# if we want to alarm, we put it in the alarm list
if with_alarm:
alarm.append(i)
# otherwise we add it in the peaks
else:
self.peaks['down'] = np.append(self.peaks['down'],-(self.data[i]-self.init_threshold['down']))
self.Nt['down'] += 1
self.n += 1
# and we update the thresholds
g,s,l = self._MOM('down')
self.extreme_quantile['down'] = self._quantile('down',g,s)
# case where the value exceeds the initial threshold but not the alarm ones
elif self.data[i]<self.init_threshold['down']:
# we add it in the peaks
self.peaks['down'] = np.append(self.peaks['down'],-(self.data[i]-self.init_threshold['down']))
self.Nt['down'] += 1
self.n += 1
# and we update the thresholds
g,s,l = self._MOM('down')
self.extreme_quantile['down'] = self._quantile('down',g,s)
else:
self.n += 1
thup.append(self.extreme_quantile['up']) # thresholds record
thdown.append(self.extreme_quantile['down']) # thresholds record
return {'upper_thresholds' : thup,'lower_thresholds' : thdown, 'alarms': alarm}
def plot(self,run_results,with_alarm = True):
"""
Plot the results of given by the run
Parameters
----------
run_results : dict
results given by the 'run' method
with_alarm : bool
(default = True) If True, alarms are plotted.
Returns
----------
list
list of the plots
"""
x = range(self.data.size)
K = run_results.keys()
ts_fig, = plt.plot(x,self.data,color=air_force_blue)
fig = [ts_fig]
if 'upper_thresholds' in K:
thup = run_results['upper_thresholds']
uth_fig, = plt.plot(x,thup,color=deep_saffron,lw=2,ls='dashed')
fig.append(uth_fig)
if 'lower_thresholds' in K:
thdown = run_results['lower_thresholds']
lth_fig, = plt.plot(x,thdown,color=deep_saffron,lw=2,ls='dashed')
fig.append(lth_fig)
if with_alarm and ('alarms' in K):
alarm = run_results['alarms']
al_fig = plt.scatter(alarm,self.data[alarm],color='red')
fig.append(al_fig)
plt.xlim((0,self.data.size))
plt.show()
return fig