【发布时间】:2017-05-19 18:35:27
【问题描述】:
我正在尝试计算占用数据集可预测性的上限,如 Song 的“人类流动性的可预测性限制”论文中所述。基本上,home (=1) 和 not at home (=0) 代表 Song 论文中访问过的位置(塔)。
我在一个随机二进制序列上测试了我的代码(我从https://github.com/gavin-s-smith/MobilityPredictabilityUpperBounds 和https://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 不等式计算可预测性。
可能是什么问题?
谢谢!
【问题讨论】: