-
Notifications
You must be signed in to change notification settings - Fork 15
/
sst.py
201 lines (169 loc) · 7.33 KB
/
sst.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
# -*- coding: utf-8 -*-
"""Singluar Spectrum Transformation.
The MIT License (MIT)
Copyright (c) 2018 statefb.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
import numpy as np
from numba import jit
from sklearn.preprocessing import MinMaxScaler
from .util.linear_algebra import power_method, lanczos, eig_tridiag
class SingularSpectrumTransformation():
"""SingularSpectrumTransformation class."""
def __init__(self, win_length, n_components=5, order=None, lag=None,
is_scaled=False, use_lanczos=True, rank_lanczos=None, eps=1e-3):
"""Change point detection with Singular Spectrum Transformation.
Parameters
----------
win_length : int
window length of Hankel matrix.
n_components : int
specify how many rank of Hankel matrix will be taken.
order : int
number of columns of Hankel matrix.
lag : int
interval between history Hankel matrix and test Hankel matrix.
is_scaled : bool
if false, min-max scaling will be applied(recommended).
use_lanczos : boolean
if true, Lanczos method will be used, which makes faster.
rank_lanczos : int
the rank which will be used for lanczos method.
for the detail of lanczos method, see [1].
eps : float
specify how much noise will be added to initial vector for
power method.
(FELIX: FEedback impLIcit kernel approXimation method)
for the detail, see [2].
References
----------
[1]: Tsuyoshi Ide et al., Change-Point Detection using Krylov Subspace Learning
[2]: Tsuyoshi Ide, Speeding up Change-Point Detection using Matrix Compression (Japanse)
"""
self.win_length = win_length
self.n_components = n_components
self.order = order
self.lag = lag
self.is_scaled = is_scaled
self.use_lanczos = use_lanczos
self.rank_lanczos = rank_lanczos
self.eps = eps
def score_offline(self, x):
"""Calculate anomaly score (offline).
Parameters
----------
x : 1d numpy array
input time series data.
Returns
-------
score : 1d array
change point score.
"""
if self.order is None:
# rule of thumb
self.order = self.win_length
if self.lag is None:
# rule of thumb
self.lag = self.order // 2
if self.rank_lanczos is None:
# rule of thumb
if self.n_components % 2 == 0:
self.rank_lanczos = 2 * self.n_components
else:
self.rank_lanczos = 2 * self.n_components - 1
assert isinstance(x, np.ndarray), "input array must be numpy array."
assert x.ndim == 1, "input array dimension must be 1."
assert isinstance(self.win_length, int), "window length must be int."
assert isinstance(self.n_components, int), "number of components must be int."
assert isinstance(self.order, int), "order of partial time series must be int."
assert isinstance(self.lag, int), "lag between test series and history series must be int."
assert isinstance(self.rank_lanczos, int), "rank for lanczos must be int."
assert self.win_length + self.order + self.lag < x.size, "data length is too short."
# all values should be positive for numerical stabilization
if not self.is_scaled:
x_scaled = MinMaxScaler(feature_range=(1, 2))\
.fit_transform(x.reshape(-1, 1))[:, 0]
else:
x_scaled = x
score = _score_offline(x_scaled, self.order,
self.win_length, self.lag, self.n_components, self.rank_lanczos,
self.eps, use_lanczos=self.use_lanczos)
return score
@jit(nopython=True)
def _score_offline(x, order, win_length, lag, n_components, rank, eps, use_lanczos):
"""Core implementation of offline score calculation."""
start_idx = win_length + order + lag + 1
end_idx = x.size + 1
# initialize vector for power method
x0 = np.empty(order, dtype=np.float64)
x0 = np.random.rand(order)
x0 /= np.linalg.norm(x0)
score = np.zeros_like(x)
for t in range(start_idx, end_idx):
# compute score at each index
# get Hankel matrix
X_history = _create_hankel(x, order,
start=t - win_length - lag,
end=t - lag)
X_test = _create_hankel(x, order,
start=t - win_length,
end=t)
if use_lanczos:
score[t-1], x1 = _sst_lanczos(X_test, X_history, n_components,
rank, x0)
# update initial vector for power method
x0 = x1 + eps * np.random.rand(x0.size)
x0 /= np.linalg.norm(x0)
else:
score[t-1] = _sst_svd(X_test, X_history, n_components)
return score
@jit(nopython=True)
def _create_hankel(x, order, start, end):
"""Create Hankel matrix.
Parameters
----------
x : full time series
order : order of Hankel matrix
start : start index
end : end index
Returns
-------
2d array shape (window length, order)
"""
win_length = end - start
X = np.empty((win_length, order))
for i in range(order):
X[:, i] = x[(start - i):(end - i)]
return X
@jit(nopython=True)
def _sst_lanczos(X_test, X_history, n_components, rank, x0):
"""Run sst algorithm with lanczos method (FELIX-SST algorithm)."""
P_history = X_history.T @ X_history
P_test = X_test.T @ X_test
# calculate the first singular vec of test matrix
u, _, _ = power_method(P_test, x0, n_iter=1)
T = lanczos(P_history, u, rank)
vec, val = eig_tridiag(T)
return 1 - (vec[0, :n_components] ** 2).sum(), u
@jit("f8(f8[:,:],f8[:,:],u1)", nopython=True)
def _sst_svd(X_test, X_history, n_components):
"""Run sst algorithm with svd."""
U_test, _, _ = np.linalg.svd(X_test, full_matrices=False)
U_history, _, _ = np.linalg.svd(X_history, full_matrices=False)
_, s, _ = np.linalg.svd(U_test[:, :n_components].T @
U_history[:, :n_components], full_matrices=False)
return 1 - s[0]