In [1]:
import numpy as np
x = np.random.rand(10,1)
In [2]:
x
Out[2]:
In [3]:
from entropy import *
In [4]:
sampen(x, 3, .2*np.nanstd(x), scale='False')
Out[4]:
In [5]:
x
Out[5]:
In [6]:
%matplotlib inline
In [12]:
x = np.random.uniform(0, 1, 100)
print x
In [13]:
"""
===============================================
FuzzyEn of Uniformly Distrubed Random Sequences
===============================================
Computes FuzzyEn of uniformly distributed random number sequences for different
values of fuzzy function width `r`. The result should look roughly linear.
"""
print(__doc__)
import numpy as np
import matplotlib.pyplot as plt
try:
import entropy
except:
sys.path.insert(0, '..')
import entropy
def main():
N = 100
rs = np.logspace(-3, 0, 10)
fig, ax = plt.subplots()
es = []
for r in rs:
runs = []
for i in range(50):
runs.append(entropy.fuzzyen(x, 2, r, 2))
es.append(np.mean(runs))
ax.semilogx(rs, es, 'o')
ax.set_ylim(0, 6)
plt.show()
if __name__ == '__main__':
main()
In [14]:
x
Out[14]:
In [16]:
def cross_samp_entropy(X1, X2, M, R):
"""Computer sample entropy (SampEn) of series X, specified by M and R.
SampEn is very close to ApEn.
Suppose given time series is X = [x(1), x(2), ... , x(N)]. We first build
embedding matrix Em, of dimension (N-M+1)-by-M, such that the i-th row of Em
is x(i),x(i+1), ... , x(i+M-1). Hence, the embedding lag and dimension are
1 and M-1 respectively. Such a matrix can be built by calling pyeeg function
as Em = embed_seq(X, 1, M). Then we build matrix Emp, whose only
difference with Em is that the length of each embedding sequence is M + 1
Denote the i-th and j-th row of Em as Em[i] and Em[j]. Their k-th elments
are Em[i][k] and Em[j][k] respectively. The distance between Em[i] and Em[j]
is defined as 1) the maximum difference of their corresponding scalar
components, thus, max(Em[i]-Em[j]), or 2) Euclidean distance. We say two 1-D
vectors Em[i] and Em[j] *match* in *tolerance* R, if the distance between them
is no greater than R, thus, max(Em[i]-Em[j]) <= R. Mostly, the value of R is
defined as 20% - 30% of standard deviation of X.
Pick Em[i] as a template, for all j such that 0 < j < N - M , we can
check whether Em[j] matches with Em[i]. Denote the number of Em[j],
which is in the range of Em[i], as k[i], which is the i-th element of the
vector k.
We repeat the same process on Emp and obtained Cmp[i], 0 < i < N - M.
The SampEn is defined as log(sum(Cm)/sum(Cmp))
References
----------
Costa M, Goldberger AL, Peng C-K, Multiscale entropy analysis of biolgical
signals, Physical Review E, 71:021906, 2005
See also
--------
ap_entropy: approximate entropy of a time series
Notes
-----
Extremely slow computation. Do NOT use if your dataset is not small and you
are not patient enough.
"""
if len(X1) != len(X2):
raise 'NameError'
N = len(X1)
Em = embed_seq(X, 1, M)
Emp = embed_seq(X, 1, M + 1)
Cm, Cmp = zeros(N - M - 1) + 1e-100, zeros(N - M - 1) + 1e-100
# in case there is 0 after counting. Log(0) is undefined.
for i in xrange(0, N - M):
for j in xrange(i + 1, N - M): # no self-match
# if max(abs(Em[i]-Em[j])) <= R: # v 0.01_b_r1
if in_range(Em[i], Em[j], R):
Cm[i] += 1
# if max(abs(Emp[i] - Emp[j])) <= R: # v 0.01_b_r1
if abs(Emp[i][-1] - Emp[j][-1]) <= R: # check last one
Cmp[i] += 1
Samp_En = log(sum(Cm)/sum(Cmp))
return Samp_En
In [ ]: