我有分发
这个看起来非常高斯,我们也不能拒绝KS测试中具有如此高p值的想法。
但是,测试分布实际上也是一个具有有限样本大小而不是CDF本身的生成分布,正如您将在代码中注意到的那样。与使用CDF获得平滑的高斯函数相比,这就是作弊。
from scipy import stats
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(1)
d1 = np.random.normal(loc = 3, scale = 2, size = 1000)
d2 = np.random.normal(loc = 3, scale = 0.5, size = 250) # Vary this to test
data = np.concatenate((d1,d2))
xmin, xmax = min(data), max(data)
lnspc = np.linspace(xmin, xmax, len(data))
# lets try the normal distribution first
m, s = stats.norm.fit(data) # get mean and standard deviation from fit
pdf_g = stats.norm.pdf(lnspc, m, s) # now get theoretical values in our interval
plt.hist(data, color = "lightgrey", normed = True, bins = 50)
plt.plot(lnspc, pdf_g, color = "black", label="Gaussian") # plot it
# Test how not-gaussian our distribution is by generating a distribution from the fit
test_dist = np.random.normal(m, s, len(data))
KS_D, KS_p = stats.ks_2samp(data, test_dist)
plt.title("D = {0:.2f}, p = {1:.2f}".format(KS_D, KS_p))
plt.show()
但我无法弄清楚如何使用默认的KS测试,即
KS_D, KS_p = stats.kstest(data, "norm")
,
因为它总是返回p值0,即我的高斯数据必须是错误的格式。
如何规范化数据以正确使用KS测试?并且是模拟比较分布的有效用法,或者比针对分布的连续CDF测试更不正确?