可预测性的上限

时间:2017-05-19 18:35:27

标签: python entropy

我正在尝试计算我的占用数据集的可预测性的上限,如宋的“人类流动可预测性的限制”一文中所述。基本上,家(= 1)而不是在家(= 0)则表示宋的论文中的访问地点(塔)。

我在一个随机二进制序列上测试了我的代码(我从https://github.com/gavin-s-smith/MobilityPredictabilityUpperBoundshttps://github.com/gavin-s-smith/EntropyRateEst派生),该二进制序列的熵应为1,可预测性为0.5。相反,返回的熵为0.87,可预测性为0.71。

这是我的代码:

import numpy as np
from scipy.optimize import fsolve
from cmath import log 
import math

def matchfinder(data):
    data_len = len(data)    
    output = np.zeros(len(data))
    output[0] = 1

    # Using L_{n} definition from
    #"Nonparametric Entropy Estimation for Stationary Process and Random Fields, with Applications to English Text"
    # by Kontoyiannis et. al.
    # $L_{n} = 1 + max \{l :0 \leq l \leq n, X^{l-1}_{0} = X^{-j+l-1}_{-j} \text{ for some } l \leq j \leq n \}$

    # for each position, i, in the sub-sequence that occurs before the current position, start_idx
    # check to see the maximum continuously equal string we can make by simultaneously extending from i and start_idx

    for start_idx in range(1,data_len):
        max_subsequence_matched = 0
        for i in range(0,start_idx):
            #    for( int i = 0; i < start_idx; i++ )
            #    {
            j = 0

            #increase the length of the substring starting at j and start_idx
            #while they are the same keeping track of the length
            while( (start_idx+j < data_len) and (i+j < start_idx) and (data[i+j] == data[start_idx+j]) ):
                j = j + 1

            if j > max_subsequence_matched:     
                max_subsequence_matched = j;

        #L_{n} is obtained by adding 1 to the longest match-length
        output[start_idx] = max_subsequence_matched + 1;    

    return output

if __name__ == '__main__':
    #Read dataset            
    data = np.random.randint(2,size=2000)

    #Number of distinct locations
    N = len(np.unique(data))

    #True entropy
    lambdai = matchfinder(data)
    Etrue = math.pow(sum( [ lambdai[i] / math.log(i+1,2) for i in range(1,len(data))] ) * (1.0/len(data)),-1)

    S = Etrue
    #use Fano's inequality to compute the predictability
    func = lambda x: (-(x*log(x,2).real+(1-x)*log(1-x,2).real)+(1-x)*log(N-1,2).real ) - S 
    ub = fsolve(func, 0.9)[0]
    print ub

matchfinder函数通过查找最长匹配找到熵并向其添加1(=之前未见过的最短子串)。然后使用Fano的不等式计算可预测性。

可能是什么问题?

谢谢!

1 个答案:

答案 0 :(得分:2)

熵函数似乎是错误的。 参考论文 Song,C.,Qu,Z.,Blumm,N。,&amp; Barabási,A。L.(2010)。人类流动性的可预测性限制。科学,327(5968),1018-1021。你提到过,实际熵是通过基于Lempel-Ziv数据压缩的算法估算的:

formula

在代码中它看起来像这样:

Etrue = math.pow((np.sum(lambdai)/ n),-1)*log(n,2).real

其中n是时间序列的长度。

请注意,我们使用不同的对数基数而不是给定的公式。然而,由于Fano不等式的对数基数为2,因此使用相同的基数进行熵计算似乎是合乎逻辑的。另外,我不确定你为什么从第一个而不是零索引开始求和。

所以现在把它包装成功能,例如:

def solve(locations, size):
    data = np.random.randint(locations,size=size)
    N = len(np.unique(data))
    n = float(len(data))
    print "Distinct locations: %i" % N
    print "Time series length: %i" % n

    #True entropy
    lambdai = matchfinder(data)
    #S = math.pow(sum([lambdai[i] / math.log(i + 1, 2) for i in range(1, len(data))]) * (1.0 / len(data)), -1)
    Etrue = math.pow((np.sum(lambdai)/ n),-1)*log(n,2).real
    S = Etrue
    print "Maximum entropy: %2.5f" % log(locations,2).real
    print "Real entropy: %2.5f" % S

    func = lambda x: (-(x * log(x, 2).real + (1 - x) * log(1 - x, 2).real) + (1 - x) * log(N - 1, 2).real) - S
    ub = fsolve(func, 0.9)[0]
    print "Upper bound of predictability: %2.5f" % ub
    return ub

2个地点的输出

Distinct locations: 2
Time series length: 10000
Maximum entropy: 1.00000
Real entropy: 1.01441
Upper bound of predictability: 0.50013

3个地点的输出

Distinct locations: 3
Time series length: 10000
Maximum entropy: 1.58496
Real entropy: 1.56567
Upper bound of predictability: 0.41172
n 接近无穷大时,Lempel-Ziv压缩收敛到实熵,这就是为什么对于2个位置情况,它略高于最大限制。

我不确定你是否正确解释了lambda的定义。它被定义为“从位置i开始的最短子串的长度,它从先前不会从位置1出现到i-1”,所以当我们到达某个点时,其他子串不是唯一的再过,你的匹配算法会给它长度总是高于子串的长度,而它应该等于0,因为不存在唯一的子串。

为了更清楚,让我们举一个简单的例子。如果位置数组看起来像:

[1 0 0 1 0 0]

然后我们可以看到,在前三个位置模式再次重复之后。这意味着从第四个位置最短的唯一子串不存在,因此它等于0.所以输出(lambda)应如下所示:

[1 1 2 0 0 0]

但是,您对该案例的函数将返回:

[1 1 2 4 3 2]

我重写了匹配功能来处理这个问题:

def matchfinder2(data):
    data_len = len(data)
    output = np.zeros(len(data))
    output[0] = 1
    for start_idx in range(1,data_len):
        max_subsequence_matched = 0
        for i in range(0,start_idx):
            j = 0
            end_distance = data_len - start_idx #length left to the end of sequence (including current index)
            while( (start_idx+j < data_len) and (i+j < start_idx) and (data[i+j] == data[start_idx+j]) ):
                j = j + 1
            if j == end_distance: #check if j has reached the end of sequence
                output[start_idx::] = np.zeros(end_distance) #if yes fill the rest of output with zeros
                return output #end function
            elif j > max_subsequence_matched:
                max_subsequence_matched = j;
        output[start_idx] = max_subsequence_matched + 1;
    return output

当然,差异很小,因为结果只会改变序列的一小部分。