-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentropy.py
259 lines (201 loc) · 7.96 KB
/
entropy.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
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import itertools
import numpy as np
def util_pattern_space(time_series, lag, dim):
"""Create a set of sequences with given lag and dimension
Args:
time_series: Vector or string of the sample data
lag: Lag between beginning of sequences
dim: Dimension (number of patterns)
Returns:
2D array of vectors
"""
n = len(time_series)
if lag * dim > n:
raise Exception('Result matrix exceeded size limit, try to change lag or dim.')
elif lag < 1:
raise Exception('Lag should be greater or equal to 1.')
pattern_space = np.empty((n - lag * (dim - 1), dim))
for i in xrange(n - lag * (dim - 1)):
for j in xrange(dim):
pattern_space[i][j] = time_series[i + j * lag]
return pattern_space
def util_standardize_signal(time_series):
return (time_series - np.mean(time_series)) / np.std(time_series)
def util_granulate_time_series(time_series, scale):
"""Extract coarse-grained time series
Args:
time_series: Time series
scale: Scale factor
Returns:
Vector of coarse-grained time series with given scale factor
"""
n = len(time_series)
b = int(np.fix(n / scale))
cts = [0] * b
for i in xrange(b):
cts[i] = np.mean(time_series[i * scale: (i + 1) * scale])
return cts
def shannon_entropy(time_series):
"""Return the Shannon Entropy of the sample data.
Args:
time_series: Vector or string of the sample data
Returns:
The Shannon Entropy as float value
"""
# Check if string
if not isinstance(time_series, str):
time_series = list(time_series)
# Create a frequency data
data_set = list(set(time_series))
freq_list = []
for entry in data_set:
counter = 0.
for i in time_series:
if i == entry:
counter += 1
freq_list.append(float(counter) / len(time_series))
# Shannon entropy
ent = 0.0
for freq in freq_list:
ent += freq * np.log2(freq)
ent = -ent
return ent
def sample_entropy(time_series, sample_length, tolerance=None):
"""Calculate and return Sample Entropy of the given time series.
Distance between two vectors defined as Euclidean distance and can
be changed in future releases
Args:
time_series: Vector or string of the sample data
sample_length: Number of sequential points of the time series
tolerance: Tolerance (default = 0.1...0.2 * std(time_series))
Returns:
Vector containing Sample Entropy (float)
References:
[1] http://en.wikipedia.org/wiki/Sample_Entropy
[2] http://physionet.incor.usp.br/physiotools/sampen/
[3] Madalena Costa, Ary Goldberger, CK Peng. Multiscale entropy analysis
of biological signals
"""
if tolerance is None:
tolerance = 0.1 * np.std(time_series)
n = len(time_series)
prev = np.zeros((1, n))
curr = np.zeros((1, n))
A = np.zeros((sample_length, 1)) # number of matches for m = [1,...,template_length - 1]
B = np.zeros((sample_length, 1)) # number of matches for m = [1,...,template_length]
for i in xrange(n - 1):
nj = n - i - 1
ts1 = time_series[i]
for jj in xrange(nj):
j = jj + i + 1
if abs(time_series[j] - ts1) < tolerance: # distance between two vectors
curr[0, jj] = prev[0, jj] + 1
temp_ts_length = min(sample_length, curr[0, jj])
for m in xrange(int(temp_ts_length)):
A[m] += 1
if j < n - 1:
B[m] += 1
else:
curr[0, jj] = 0
for j in xrange(nj):
prev[0, j] = curr[0, j]
N = n * (n - 1) / 2
B = np.vstack(([N], B[:sample_length - 1]))
similarity_ratio = A / B # np.divide(A, B)
se = - np.log(similarity_ratio)
se = np.reshape(se, -1)
return A,B
def multiscale_entropy(time_series, sample_length, tolerance):
"""Calculate the Multiscale Entropy of the given time series considering
different time-scales of the time series.
Args:
time_series: Time series for analysis
sample_length: Bandwidth or group of points
tolerance: Tolerance (default = 0.1...0.2 * std(time_series))
Returns:
Vector containing Multiscale Entropy
Reference:
[1] http://en.pudn.com/downloads149/sourcecode/math/detail646216_en.html
"""
n = len(time_series)
mse = np.zeros((1, sample_length))
for i in xrange(sample_length):
b = np.fix(n / (i + 1))
temp_ts = [0] * int(b)
for j in xrange(b):
num = sum(time_series[j * (i + 1): (j + 1) * (i + 1)])
den = i + 1
temp_ts[j] = float(num) / float(den)
se = sample_entropy(temp_ts, 1, tolerance)
mse[0, i] = se
return mse[0]
def permutation_entropy(time_series, m, delay):
"""Calculate the Permutation Entropy
Args:
time_series: Time series for analysis
m: Order of permutation entropy
delay: Time delay
Returns:
Vector containing Permutation Entropy
Reference:
[1] Massimiliano Zanin et al. Permutation Entropy and Its Main Biomedical and Econophysics Applications:
A Review. http://www.mdpi.com/1099-4300/14/8/1553/pdf
[2] Christoph Bandt and Bernd Pompe. Permutation entropy — a natural complexity
measure for time series. http://stubber.math-inf.uni-greifswald.de/pub/full/prep/2001/11.pdf
[3] http://www.mathworks.com/matlabcentral/fileexchange/37289-permutation-entropy/content/pec.m
"""
n = len(time_series)
permutations = np.array(list(itertools.permutations(range(m))))
c = [0] * len(permutations)
for i in xrange(n - delay * (m - 1)):
# sorted_time_series = np.sort(time_series[i:i+delay*m:delay], kind='quicksort')
sorted_index_array = np.array(np.argsort(time_series[i:i + delay * m:delay], kind='quicksort'))
for j in xrange(len(permutations)):
if abs(permutations[j] - sorted_index_array).any() == 0:
c[j] += 1
c = [element for element in c if element != 0]
p = np.divide(np.array(c), float(sum(c)))
pe = -sum(p * np.log(p))
return pe
def multiscale_permutation_entropy(time_series, m, delay, scale):
"""Calculate the Multiscale Permutation Entropy
Args:
time_series: Time series for analysis
m: Order of permutation entropy
delay: Time delay
scale: Scale factor
Returns:
Vector containing Multiscale Permutation Entropy
Reference:
[1] Francesco Carlo Morabito et al. Multivariate Multi-Scale Permutation Entropy for
Complexity Analysis of Alzheimer’s Disease EEG. www.mdpi.com/1099-4300/14/7/1186
[2] http://www.mathworks.com/matlabcentral/fileexchange/37288-multiscale-permutation-entropy-mpe/content/MPerm.m
"""
mspe = []
for i in xrange(scale):
coarse_time_series = util_granulate_time_series(time_series, i + 1)
pe = permutation_entropy(coarse_time_series, m, delay)
mspe.append(pe)
return mspe
# TODO add tests
def composite_multiscale_entropy(time_series, sample_length, scale):
"""Calculate the Composite Multiscale Entropy of the given time series.
Args:
time_series: Time series for analysis
sample_length: Number of sequential points of the time series
scale: Scale factor
Returns:
Vector containing Composite Multiscale Entropy
Reference:
[1] Wu, Shuen-De, et al. "Time series analysis using
composite multiscale entropy." Entropy 15.3 (2013): 1069-1084.
"""
cmse = np.zeros((1, scale))
r = np.std(time_series) * 0.15
for i in xrange(scale):
for j in xrange(i):
tmp = util_granulate_time_series(time_series[j:], i+1)
cmse[i] += sample_entropy(tmp, sample_length, r)/(i + 1)
return cmse