How do I compute the approximate entropy of a bit string?

前端 未结 7 2062
梦如初夏
梦如初夏 2020-12-07 20:46

Is there a standard way to do this?

Googling -- \"approximate entropy\" bits -- uncovers multiple academic papers but I\'d like to just find a chunk of pseudocode de

相关标签:
7条回答
  • 2020-12-07 21:11

    Here's an implementation in Python (I also added it to the Wiki page):

    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 _phi(m) - _phi(m + 1)
    

    Example:

    >>> U = np.array([85, 80, 89] * 17)
    >>> ApEn(U, 2, 3)
    -1.0996541105257052e-05
    

    The above example is consistent with the example given on Wikipedia.

    0 讨论(0)
提交回复
热议问题