-
Notifications
You must be signed in to change notification settings - Fork 88
/
entropy.py
450 lines (351 loc) · 13 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
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
import math
from collections import Counter
import numpy as np
def time_delay_embedding(time_series, embedding_dimension, delay):
"""Calculate time-delayed embedding.
Parameters
----------
time_series : np.ndarray
The input time series, shape (n_times)
embedding_dimension : int
The embedding dimension (order).
delay : int
The delay between embedded points.
Returns
-------
embedded : ndarray
The embedded time series with shape (n_times - (order - 1) * delay, order).
"""
series_length = len(time_series)
embedded_series = np.empty((embedding_dimension, series_length - (embedding_dimension - 1) * delay))
for i in range(embedding_dimension):
embedded_series[i] = time_series[i * delay : i * delay + embedded_series.shape[1]]
return embedded_series.T
def util_pattern_space(time_series, lag, dim):
"""Create a set of sequences with a given lag and dimension.
Parameters
----------
time_series : np.ndarray
Vector or string of the sample data
lag : int
Lag between the beginning of sequences
dim : int
Dimension (number of patterns)
Returns
-------
pattern_space: np.ndarray
2D array of vectors
Raises
------
ValueError: If the lag is less than 1 or the result matrix exceeds the size limit.
"""
n = len(time_series)
if lag < 1:
raise ValueError("Lag should be greater than or equal to 1.")
if lag * dim > n:
raise ValueError("Result matrix size limit exceeded. Adjust the lag or dim value.")
pattern_space = np.zeros((n - lag * (dim - 1), dim))
for i in range(dim):
pattern_space[:, i] = time_series[i * lag : i * lag + n - lag * (dim - 1)]
return pattern_space
def util_granulate_time_series(time_series, scale):
"""Extract coarse-grained time series.
Parameters
----------
time_series : np.ndarray
Time series
scale : int
Scale factor
Returns
-------
cts : np.ndarray
Array of coarse-grained time series with a given scale factor
"""
if not isinstance(time_series, np.ndarray):
time_series = np.array(time_series)
n = time_series.shape[0]
b = n // scale
cts = np.mean(time_series[: b * scale].reshape(b, scale), axis=1)
return cts
def shannon_entropy(time_series):
"""Calculate Shannon Entropy of the sample data.
Parameters
----------
time_series: np.ndarray | list[str]
Vector or string of the sample data
Returns
-------
ent: float
The Shannon Entropy as float value
"""
if isinstance(time_series, str):
# Calculate frequency counts
counter = Counter(time_series)
total_count = len(time_series)
# Calculate frequencies and Shannon entropy
ent = 0.0
for count in counter.values():
freq = count / total_count
ent += freq * np.log2(freq)
ent = -ent
return ent
# Calculate frequency counts
_, counts = np.unique(time_series, return_counts=True)
total_count = len(time_series)
# Calculate frequencies and Shannon entropy
frequencies = counts / total_count
ent = -np.sum(frequencies * np.log2(frequencies))
return ent
def sample_entropy(time_series, sample_length, tolerance=None):
"""Calculate the sample entropy of degree m of a time_series.
This method uses Chebyshev norm.
It is quite fast for random data but can be slower is there is
structure in the input time series.
Parameters
----------
time_series : np.ndarray
Time series, 1-d vector
sample_length : int
length of longest template vector
tolerance : float
tolerance (defaults to 0.1 * std(time_series)))
Returns
-------
sampen: np.ndarray
Array of Sample Entropies SE.
SE[k] is the ratio `#templates of length k+1` / `#templates of length k`
where `#templates of length 0` = n*(n - 1) / 2, by definition
Notes
-----
The parameter 'sample_length' is equal to m + 1 in Ref[1].
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 not isinstance(time_series, np.ndarray):
time_series = np.array(time_series)
if tolerance is None:
tolerance = 0.1 * np.std(time_series)
# The code below follows the sample length convention of Ref [1] so:
sample_length = sample_length - 1
n = len(time_series)
# N_temp is a vector that holds the number of matches
# N_temp[k] holds matches templates of length k
N_temp = np.zeros(sample_length + 2)
# Templates of length 0 match by definition:
N_temp[0] = n * (n - 1) / 2
for i in range(n - sample_length - 1):
template = time_series[i : (i + sample_length + 1)] # We have `sample_length+1` elements in the template
rem_time_series = time_series[i + 1 :]
search_list = np.arange(len(rem_time_series) - sample_length, dtype=np.int32)
for length in range(1, len(template) + 1):
hit_list = np.abs(rem_time_series[search_list] - template[length - 1]) < tolerance
N_temp[length] += np.sum(hit_list)
search_list = search_list[hit_list] + 1
sampen = -np.log(N_temp[1:] / N_temp[:-1])
return sampen
def multiscale_entropy(time_series, sample_length, tolerance=None, maxscale=None):
"""Calculate Multiscale Entropy considering different time-scales of the time series.
Parameters
----------
time_series : np.ndarray
Input time series for analysis.
sample_length : int
Bandwidth or group of points
tolerance : float
Tolerance value (default is 0.1 times the standard deviation of the `time_series`)
maxscale : int
Maximum timescale (default is the length of the `time_series`)
Returns
-------
mse : np.ndarray
Array of Multiscale Entropies
References
----------
.. [1] http://en.pudn.com/downloads149/sourcecode/math/detail646216_en.html
Can be viewed at https://web.archive.org/web/20170207221539/http://en.pudn.com/downloads149/sourcecode/math/detail646216_en.html
"""
if tolerance is None:
tolerance = 0.1 * np.std(time_series)
if maxscale is None:
maxscale = len(time_series)
mse = np.zeros(maxscale)
for i in range(maxscale):
temp = util_granulate_time_series(time_series, i + 1)
mse[i] = sample_entropy(temp, sample_length, tolerance)[-1]
return mse
def permutation_entropy(time_series, order=3, delay=1, normalize=False):
"""Calculate Permutation Entropy.
Parameters
----------
time_series : list | np.ndarray
Time series
order : int
Order of permutation entropy
delay : int
Time delay
normalize : bool
If True, divide by log2(factorial(m)) to normalize the entropy
between 0 and 1. Otherwise, return the permutation entropy in bit.
Returns
-------
pe : float
Permutation Entropy
References
----------
.. [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
Notes
-----
Last updated (Oct 2018) by Raphael Vallat (raphaelvallat9@gmail.com):
- Major speed improvements
- Use of base 2 instead of base e
- Added normalization
Examples
--------
1. Permutation entropy with order 2
>>> x = [4, 7, 9, 10, 6, 11, 3]
>>> # Return a value between 0 and log2(factorial(order))
>>> print(permutation_entropy(x, order=2))
0.918
2. Normalized permutation entropy with order 3
>>> x = [4, 7, 9, 10, 6, 11, 3]
>>> # Return a value comprised between 0 and 1.
>>> print(permutation_entropy(x, order=3, normalize=True))
0.589
"""
x = np.array(time_series)
hashmult = np.power(order, np.arange(order))
# Embed x and sort the order of permutations
sorted_idx = time_delay_embedding(x, embedding_dimension=order, delay=delay).argsort(kind="quicksort")
# Associate unique integer to each permutations
hashval = (np.multiply(sorted_idx, hashmult)).sum(1)
# Return the counts
_, c = np.unique(hashval, return_counts=True)
# Use np.true_divide for Python 2 compatibility
p = np.true_divide(c, c.sum())
pe = -np.multiply(p, np.log2(p)).sum()
if normalize:
factorial = math.factorial(order)
pe /= np.log2(factorial)
return pe
def multiscale_permutation_entropy(time_series, m, delay, scale):
"""Calculate the Multiscale Permutation Entropy.
Parameters
----------
time_series : np.ndarray
Time series for analysis
m : int
Order of permutation entropy
delay : int
Time delay
scale : int
Scale factor
Returns
-------
mspe : np.ndarray
Array of Multiscale Permutation Entropies
References
----------
.. [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 = np.empty(scale)
for i in range(scale):
coarse_time_series = util_granulate_time_series(time_series, i + 1)
mspe[i] = permutation_entropy(coarse_time_series, order=m, delay=delay)
return mspe
def weighted_permutation_entropy(time_series, order=2, delay=1, normalize=False):
"""Calculate the weighted permutation entropy.
Weighted permutation entropy captures the information in the amplitude of a signal where
standard permutation entropy only measures the information in the ordinal pattern, "motif".
Parameters
----------
time_series : list | np.ndarray
Time series
order : int
Order of permutation entropy
delay : int
Time delay
normalize : bool
If True, divide by log2(factorial(m)) to normalize the entropy
between 0 and 1. Otherwise, return the permutation entropy in bit.
Returns
-------
wpe : float
Weighted Permutation Entropy
References
----------
.. [1] Bilal Fadlallah, Badong Chen, Andreas Keil, and José Príncipe
Phys. Rev. E 87, 022911 - Published 20 February 2013
Notes
-----
- Updated in Jun 2023 by Nikolay Donets
- Updated in March 2021 by Samuel Dotson (samgdotson@gmail.com)
Examples
--------
1. Weighted permutation entropy with order 2
>>> x = [4, 7, 9, 10, 6, 11, 3]
>>> # Return a value between 0 and log2(factorial(order))
>>> print(permutation_entropy(x, order=2))
0.912
2. Normalized weighted permutation entropy with order 3
>>> x = [4, 7, 9, 10, 6, 11, 3]
>>> # Return a value comprised between 0 and 1.
>>> print(permutation_entropy(x, order=3, normalize=True))
0.547
"""
x = time_delay_embedding(time_series, embedding_dimension=order, delay=delay)
weights = np.var(x, axis=1)
sorted_idx = x.argsort(kind="quicksort", axis=1)
motif_weights = {}
for weight, indices in zip(weights, sorted_idx): # noqa: B905
motif = tuple(indices)
if motif in motif_weights:
motif_weights[motif] += weight
else:
motif_weights[motif] = weight
pw = np.array(list(motif_weights.values()))
pw /= weights.sum()
b = np.log2(pw)
wpe = -np.dot(pw, b)
if normalize:
wpe /= np.log2(math.factorial(order))
return wpe
def composite_multiscale_entropy(time_series, sample_length, scale, tolerance=None):
"""Calculate Composite Multiscale Entropy.
Parameters
----------
time_series : np.ndarray
Time series for analysis
sample_length : int
Number of sequential points of the time series
scale : int
Scale factor
tolerance : float
Tolerance (default = 0.1 * std(time_series))
Returns
-------
cmse : np.ndarray
Array of Composite Multiscale Entropies
References
----------
.. [1] Wu, Shuen-De, et al. "Time series analysis using
composite multiscale entropy." Entropy 15.3 (2013): 1069-1084.
"""
if tolerance is None:
tolerance = 0.1 * np.std(time_series)
cmse = np.zeros(scale)
for i in range(scale):
for j in range(i + 1):
tmp = util_granulate_time_series(time_series[j:], i + 1)
se = sample_entropy(tmp, sample_length, tolerance)[-1]
cmse[i] += se / (i + 1)
return cmse