Sample entropy

Sample entropy (SampEn) is a modification of approximate entropy (ApEn), used for assessing the complexity of physiological time-series signals, diagnosing diseased states. SampEn has two advantages over ApEn: data length independence and a relatively trouble-free implementation. Also, there is a small computational difference: In ApEn, the comparison between the template vector (see below) and the rest of the vectors also includes comparison with itself. This guarantees that probabilities formula_1 are never zero. Consequently, it is always possible to take a logarithm of probabilities. Because template comparisons with itself lower ApEn values, the signals are interpreted to be more regular than they actually are. These self-matches are not included in SampEn. However, since SampEn makes direct use of the correlation integrals, it is not a real measure of information but an approximation. The foundations and differences with ApEn, as well as a step-by-step tutorial for its application is available at.
There is a multiscale version of SampEn as well, suggested by Costa and others.
Definition.
Like approximate entropy (ApEn), Sample entropy (SampEn) is a measure of complexity. But it does not include self-similar patterns as ApEn does. For a given embedding dimension formula_2, tolerance formula_3 and number of data points formula_4, SampEn is the negative natural logarithm of the probability that if two sets of simultaneous data points of length formula_5 have distance formula_6 then two sets of simultaneous data points of length formula_7 also have distance formula_6. And we represent it by formula_9 (or by formula_10 including sampling time formula_11).
Now assume we have a time-series data set of length formula_12 with a constant time interval formula_11. We define a template vector of length formula_5, such that formula_15 and the distance function formula_16 (i≠j) is to be the Chebyshev distance (but it could be any distance function, including Euclidean distance). We define the sample entropy to be
Where
formula_18 = number of template vector pairs having formula_19
formula_20 = number of template vector pairs having formula_21
It is clear from the definition that formula_22 will always have a value smaller or equal to formula_23. Therefore, formula_24 will be always either be zero or positive value. A smaller value of formula_25 also indicates more self-similarity in data set or less noise.
Generally we take the value of formula_26 to be formula_27 and the value of formula_28 to be formula_29.
Where std stands for standard deviation which should be taken over a very large dataset. For instance, the r value of 6 ms is appropriate for sample entropy calculations of heart rate intervals, since this corresponds to formula_29 for a very large population.
Multiscale SampEn.
The definition mentioned above is a special case of multi scale sampEn with formula_31,where formula_32 is called skipping parameter. In multiscale SampEn template vectors are defined with a certain interval between its elements, specified by the value of formula_32. And modified template vector is defined as
formula_34
and sampEn can be written as
formula_35
And we calculate formula_36 and formula_37 like before.
Implementation.
Sample entropy can be implemented easily in many different programming languages. Below lies a vectorized example written in Python. 
import numpy as np
def sampen(L, m, r):
 N = len(L)
 B = 0.0
 A = 0.0
 
 # Split time series and save all templates of length m
 xmi = np.array([L[i : i + m] for i in range(N - m)])
 xmj = np.array([L[i : i + m] for i in range(N - m + 1)])
 # Save all matches minus the self-match, compute B
 B = np.sum([np.sum(np.abs(xmii - xmj).max(axis=1) <= r) - 1 for xmii in xmi])
 # Similar for computing A
 m += 1
 xm = np.array([L[i : i + m] for i in range(N - m + 1)])
 A = np.sum([np.sum(np.abs(xmi - xm).max(axis=1) <= r) - 1 for xmi in xm])
 # Return SampEn
 return -np.log(A / B)
An example written in other languages can be found: