-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapen.py
More file actions
26 lines (18 loc) · 697 Bytes
/
apen.py
File metadata and controls
26 lines (18 loc) · 697 Bytes
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
# Source: https://en.wikipedia.org/wiki/Approximate_entropy#Python_implementation
import numpy as np
def ApEn(U, m, r):
def _maxdist(x_i, x_j):
return max([abs(ua - va) for ua, va in zip(x_i, x_j)])
def _phi(m):
x = [[U[j] for j in range(i, i + m - 1 + 1)] for i in range(N - m + 1)]
C = [len([1 for x_j in x if _maxdist(x_i, x_j) <= r]) / (N - m + 1.0) for x_i in x]
return (N - m + 1.0)**(-1) * sum(np.log(C))
N = len(U)
return abs(_phi(m+1) - _phi(m))
# Usage example
U = np.array([85, 80, 89] * 17)
print(ApEn(U, 2, 3))
1.0996541105257052e-05
randU = np.random.choice([85, 80, 89], size=17*3)
print(ApEn(randU, 2, 3))
0.8626664154888908