@Rui 提到了如何计算概率分布函数,但这对你没有多大帮助。
您想要使用的是柯尔莫哥洛夫-斯米尔诺夫检验 http://www.itl.nist.gov/div898/handbook/eda/section3/eda35g.htm or the 卡方检验 http://en.wikipedia.org/wiki/Pearson%27s_chi-squared_test。两者都用于将数据与已知的概率分布进行比较,以确定数据集是否可能/不可能具有该概率分布。
卡方适用于离散分布,K-S 适用于连续分布。
为了使用卡方与本福德定律,您只需创建一个直方图 H[N],例如有 9 个 bin N=1,2,... 9,迭代数据集以检查第一个数字,以计算 9 个非零数字(或 90 个 bin 的前两位数字)中每个数字的样本数。然后运行卡方检验,将直方图与预期计数 E[N] 进行比较。
例如,假设您有 100 条数据。 E[N] 可以根据本福德定律计算:
E[1] = 30.1030 (=100*log(1+1))
E[2] = 17.6091 (=100*log(1+1/2))
E[3] = 12.4939 (=100*log(1+1/3))
E[4] = 9.6910
E[5] = 7.9181
E[6] = 6.6946
E[7] = 5.7992
E[8] = 5.1152
E[9] = 4.5757
Then compute Χ2 = sum((H[k]-E[k])^2/E[k]), and compare to a threshold as specified in the test. (Here we have a fixed distribution with no parameters, so the number of parameters s=0 and p = s+1 = 1, and the # of bins n is 9, so the # of degrees of freedom = n-p = 8*. Then you go to your handy-dandy chi-squared table http://itl.nist.gov/div898/handbook/eda/section3/eda3674.htm and see if the numbers look ok. For 8 degrees of freedom the confidence levels look like this:
Χ2 > 13.362: 10% chance the dataset still matches Benford's Law
Χ2 > 15.507: 5% chance the dataset still matches Benford's Law
Χ2 > 17.535: 2.5% chance the dataset still matches Benford's Law
Χ2 > 20.090: 1% chance the dataset still matches Benford's Law
Χ2 > 26.125: 0.1% chance the dataset still matches Benford's Law
Suppose your histogram yielded H = [29,17,12,10,8,7,6,5,6], for a Χ2 = 0.5585. That's very close to the expected distribution. (maybe even too close!)
Now suppose your histogram yielded H = [27,16,10,9,5,11,6,5,11], for a Χ2 = 13.89. There is less than a 10% chance that this histogram is from a distribution that matches Benford's Law. So I'd call the dataset questionable but not overly so.
注意you必须选择显着性水平(例如 10%/5%/等)。如果您使用 10%,则预计大约每 10 个真正来自 Benford 分布的数据集就有 1 个会失败,即使它们没问题。这是一个判断。
看起来 Apache Commons Math 有一个卡方检验的 Java 实现:
ChiSquareTestImpl.chiSquare(double[] expected, long[] observed) http://commons.apache.org/math/api-2.2/org/apache/commons/math/stat/inference/ChiSquareTestImpl.html#chiSquare%28double%5B%5D,%20long%5B%5D%29
*关于自由度的注释 = 8:这是有道理的;你有 9 个数字,但它们有 1 个约束,即它们都必须加起来等于数据集的大小,所以一旦你知道直方图的前 8 个数字,你就可以算出第九个。
柯尔莫哥洛夫-斯米尔诺夫实际上更简单(直到我找到一个足够简单的说明它如何工作之前我才意识到这一点),但适用于连续分布。该方法的工作原理如下:
- 您计算概率分布的累积分布函数 (CDF)。
- 您可以计算经验累积分布函数 (ECDF),通过将数据集按排序顺序即可轻松获得该函数。
- 您发现 D =(大约)两条曲线之间的最大垂直距离。
让我们根据本福德定律更深入地处理这些问题。
CDF for Benford's Law: this is just C = log10 x, where x is in the interval [1,10), i.e. including 1 but excluding 10. This can be easily seen if you look at the generalized form of Benford's Law http://en.wikipedia.org/wiki/Benford%27s_law#Generalization_to_digits_beyond_the_first, and instead of writing it log(1+1/n), writing it as log(n+1)-log(n) -- in other words, to get the probability of each bin, they're subtracting successive differences of log(n), so log(n) must be the CDF
ECDF:获取你的数据集,对于每个数字,将符号设置为正,用科学记数法写出来,并将指数设置为 0。(不知道如果你有一个数字为 0 该怎么办;这似乎不适合自己)到本福德定律分析。)然后按升序对数字进行排序。 ECDF 是任何有效 x 的数据点数量
计算每个 d[k] = max(CDF(y[k]) - (k-1)/N, k/N - CDF(y[k]) 的最大差值 D = max(d[k])。
这是一个例子:假设我们的数据集 = [3.02, 1.99, 28.3, 47, 0.61]。然后 ECDF 由排序数组 [1.99, 2.83, 3.02, 4.7, 6.1] 表示,计算 D 如下:
D = max(
log10(1.99) - 0/5, 1/5 - log10(1.99),
log10(2.83) - 1/5, 2/5 - log10(2.83),
log10(3.02) - 2/5, 3/5 - log10(3.02),
log10(4.70) - 3/5, 4/5 - log10(4.70),
log10(6.10) - 4/5, 5/5 - log10(6.10)
)
其中 = 0.2988 (=log10(1.99) - 0)。
最后你必须useD 统计数据——我似乎无法在网上找到任何信誉良好的表格,但 Apache Commons Math 有一个柯尔莫哥洛夫斯米尔诺夫DistributionImpl.cdf() http://commons.apache.org/math/apidocs/org/apache/commons/math/distribution/KolmogorovSmirnovDistributionImpl.html#cdf%28double%29函数将计算出的 D 值作为输入,并告诉您 D 小于该值的概率。采用 1-cdf(D) 可能更容易,它告诉您 D 大于或等于您计算的值的概率:如果这是 1% 或 0.1%,则可能意味着数据不符合本福德定律,但如果是 25% 或 50%,则可能是一个很好的匹配。