scipy.special.kolmogorov

scipy.special.kolmogorov(y) = <ufunc 'kolmogorov'>

Kolmogorov分布的互补累积分布(生存函数)函数。

返回Kolmogorov极限分布的互补累积分布函数 (D_n*\sqrt(n) 当n趋于无穷大时)对经验分布和理论分布之间的相等进行双边检验。它等于(限制为n->无穷大的)概率 sqrt(n) * max absolute deviation > y

参数
y浮点数组_LIKE

经验CDF(ECDF)和目标CDF之间的绝对偏差,乘以SQRT(N)。

退货
浮动

Kolmogorov(Y)的值

参见

kolmogi

分布的逆生存函数

scipy.stats.kstwobign

以连续分发的形式提供功能

smirnov, smirnovi

单面分布的函数

注意事项

kolmogorov 由以下用户使用 stats.kstest 在Kolmogorov-Smirnov拟合优度检验中的应用。由于历史原因,此函数在 scpy.special ,但要实现最准确的CDF/SF/PDF/PPF/ISF计算,推荐的方法是使用 stats.kstwobign 分配。

示例

显示至少大到0、0.5和1.0的间隙的概率。

>>> from scipy.special import kolmogorov
>>> from scipy.stats import kstwobign
>>> kolmogorov([0, 0.5, 1.0])
array([ 1.        ,  0.96394524,  0.26999967])

将从拉普拉斯(0,1)分布中抽取的大小为1000的样本与目标分布(正态(0,1)分布)进行比较。

>>> from scipy.stats import norm, laplace
>>> rng = np.random.default_rng()
>>> n = 1000
>>> lap01 = laplace(0, 1)
>>> x = np.sort(lap01.rvs(n, random_state=rng))
>>> np.mean(x), np.std(x)
(-0.05841730131499543, 1.3968109101997568)

构造经验CDF和K-S统计量Dn。

>>> target = norm(0,1)  # Normal mean 0, stddev 1
>>> cdfs = target.cdf(x)
>>> ecdfs = np.arange(n+1, dtype=float)/n
>>> gaps = np.column_stack([cdfs - ecdfs[:n], ecdfs[1:] - cdfs])
>>> Dn = np.max(gaps)
>>> Kn = np.sqrt(n) * Dn
>>> print('Dn=%f, sqrt(n)*Dn=%f' % (Dn, Kn))
Dn=0.043363, sqrt(n)*Dn=1.371265
>>> print(chr(10).join(['For a sample of size n drawn from a N(0, 1) distribution:',
...   ' the approximate Kolmogorov probability that sqrt(n)*Dn>=%f is %f' %  (Kn, kolmogorov(Kn)),
...   ' the approximate Kolmogorov probability that sqrt(n)*Dn<=%f is %f' %  (Kn, kstwobign.cdf(Kn))]))
For a sample of size n drawn from a N(0, 1) distribution:
 the approximate Kolmogorov probability that sqrt(n)*Dn>=1.371265 is 0.046533
 the approximate Kolmogorov probability that sqrt(n)*Dn<=1.371265 is 0.953467

根据目标N(0,1)CDF绘制经验CDF。

>>> import matplotlib.pyplot as plt
>>> plt.step(np.concatenate([[-3], x]), ecdfs, where='post', label='Empirical CDF')
>>> x3 = np.linspace(-3, 3, 100)
>>> plt.plot(x3, target.cdf(x3), label='CDF for N(0, 1)')
>>> plt.ylim([0, 1]); plt.grid(True); plt.legend();
>>> # Add vertical lines marking Dn+ and Dn-
>>> iminus, iplus = np.argmax(gaps, axis=0)
>>> plt.vlines([x[iminus]], ecdfs[iminus], cdfs[iminus], color='r', linestyle='dashed', lw=4)
>>> plt.vlines([x[iplus]], cdfs[iplus], ecdfs[iplus+1], color='r', linestyle='dashed', lw=4)
>>> plt.show()
../../_images/scipy-special-kolmogorov-1.png