我想用mean=0和variance=1来测试一个数据集的分布与高斯分布的接近程度。
来自astropy.stats
的kuiper测试有一个来自文档的cdf
参数:“一个可调用来评估被测试分布的CDF的参数。将一次调用所有值的向量。默认值是均匀分布”,但我不知道如何使用这个参数来测试正态分布。如果我想要一个均值为0.2,方差为2的正态分布怎么办?
所以我使用了kuiper_two,同样来自astropy,并创建了一个随机正态分布。请参见下面的示例。
我看到的问题是,它取决于我生成的要比较的数据点的数量。如果我使用100点而不是10000个数据点,概率(fpp)将提高到43%。
我想问题是,我该如何正确地做到这一点?另外,我如何解释D数字?
# create data and its cdf
np.random.seed(0)
data = np.random.normal(loc=0.02, scale=1.15, size=50)
data_sort = np.sort(data)
data_cdf = [x/len(data) for x in range(0, len(data))]
# create the normal data with mean 0 and variance 1
xx = np.random.normal(loc=0, scale=1, size=10000)
xx_sort = np.sort(xx)
xx_cdf = [x/len(xx) for x in range(0, len(xx))]
# compute the pdf for a plot
x = np.linspace(-4, 4, 50)
x_pdf = stats.norm.pdf(x, 0, 1)
# we can see it all in a plot
fig, ax = plt.subplots(figsize=(8, 6))
plt.hist(xx, bins=20, density=True, stacked=True, histtype='stepfilled', alpha=0.6)
plt.hist(data, density=True, stacked=True, histtype='step', lw=3)
plt.plot(x, x_pdf, lw=3, label='G($\mu=0$, $\sigma^2=1$)')
ax2 = ax.twinx()
ax2.plot(xx_sort, xx_cdf, marker='o', ms=8, mec='green', mfc='green', ls='None')
ax2.plot(data_sort, data_cdf, marker='^', ms=8, mec='orange', mfc='orange', ls='None')
# Kuiper test
D, fpp = kuiper_two(data_sort, xx_sort)
print('# D number =', round(D, 5))
print('# fpp =', round(fpp, 5))
# Which resulted in:
# D number = 0.211
# fpp = 0.14802
发布于 2021-01-12 22:26:30
astropy.stats.kuiper
期望第一个参数是您想要测试的分布的样本,第二个参数是您要测试的分布的CDF。
此变量是一个Callable
,它期望自己(a)样本返回累积分布函数下的值。你可以使用scipy.stat
的CDF来实现,通过使用functools.partial
,我们可以设置任何参数。
from scipy import stats
from scipy.stats import norm
from astropy.stats import kuiper
from functools import partial
from random import shuffle
np.random.seed(0)
data = np.random.normal(loc=0.02, scale=1.15, size=50)
print(kuiper(data, partial(norm.cdf, loc=0.2, scale=2.0)))
# Output: (0.2252118027033838, 0.08776036566607946)
# The data does not have to be sorted, in case you wondered:
shuffle(data)
print(kuiper(data, partial(norm.cdf, loc=0.2, scale=2.0)))
# Output: (0.2252118027033838, 0.08776036566607946)
在这个来自the Wikipedia article about this test的图表中,您可以了解柯伊伯统计V度量的内容:
[
]3
如果您共享比较分布的参数,则距离较小,并且各个底层CDF相同的估计概率会上升:
print(kuiper(data, partial(norm.cdf, loc=0.02, scale=1.15)))
# Output: (0.14926352419821276, 0.68365004302431)
相比之下,函数astropy.stats.kuiper_two
期望两个经验数据样本相互比较。因此,如果您想要与具有易处理的CDF的发行版进行比较,最好直接使用CDF (使用kuiper
),而不是从比较发行版中采样(并使用kuiper_two
)。
还有一个挑剔的地方。除了在不是CDF的变量名中使用CDF之外,这个公式比上面的公式更具可读性:
data_cdf = np.linspace(0.0, 1.0, len(data), endpoint=False)
xx_cdf = np.linspace(0.0, 1.0, len(xx), endpoint=False)
https://stackoverflow.com/questions/63599689
复制相似问题