scipy.special.smirnov¶
- scipy.special.smirnov(n, d) = <ufunc 'smirnov'>¶
Kolmogorov-Smirnov互补累积分布函数
返回Dn+(或Dn-)的精确Kolmogorov-Smirnov互补累积分布函数(也称为生存函数),以进行经验分布与理论分布相等的单边测试。它等于理论分布与基于以下公式的经验分布之间的最大差异的概率 n 样本数大于%d。
- 参数
- n集成
样本数
- d浮点数组_LIKE
经验CDF(ECDF)与目标CDF之间的偏差。
- 退货
- 浮动
Smirnov(n,d)、prob(Dn+>=d)的值(也称为prob(Dn->=d))
参见
smirnovi
分布的逆生存函数
scipy.stats.ksone
以连续分发的形式提供功能
kolmogorov
,kolmogi
用于双边分布的函数
注意事项
smirnov
由以下用户使用 stats.kstest 在Kolmogorov-Smirnov拟合优度检验中的应用。由于历史原因,此函数在 scpy.special ,但要实现最准确的CDF/SF/PDF/PPF/ISF计算,推荐的方法是使用 stats.ksone 分配。示例
>>> from scipy.special import smirnov
对于大小为5的样本,显示间距至少为0、0.5和1.0的概率
>>> smirnov(5, [0, 0.5, 1.0]) array([ 1. , 0.056, 0. ])
将从源N(0.5,1)分布中提取的大小为5的样本与目标N(0,1)CDF进行比较。
>>> from scipy.stats import norm >>> rng = np.random.default_rng() >>> n = 5 >>> gendist = norm(0.5, 1) # Normal distribution, mean 0.5, stddev 1 >>> x = np.sort(gendist.rvs(size=n, random_state=rng)) >>> x array([-1.3922078 , -0.13526532, 0.1371477 , 0.18981686, 1.81948167]) >>> target = norm(0, 1) >>> cdfs = target.cdf(x) >>> cdfs array([0.08192974, 0.44620105, 0.55454297, 0.57527368, 0.96558101]) # Construct the Empirical CDF and the K-S statistics (Dn+, Dn-, Dn) >>> ecdfs = np.arange(n+1, dtype=float)/n >>> cols = np.column_stack([x, ecdfs[1:], cdfs, cdfs - ecdfs[:n], ecdfs[1:] - cdfs]) >>> np.set_printoptions(precision=3) >>> cols array([[-1.392, 0.2 , 0.082, 0.082, 0.118], [-0.135, 0.4 , 0.446, 0.246, -0.046], [ 0.137, 0.6 , 0.555, 0.155, 0.045], [ 0.19 , 0.8 , 0.575, -0.025, 0.225], [ 1.819, 1. , 0.966, 0.166, 0.034]]) >>> gaps = cols[:, -2:] >>> Dnpm = np.max(gaps, axis=0) >>> print('Dn-=%f, Dn+=%f' % (Dnpm[0], Dnpm[1])) Dn-=0.246201, Dn+=0.224726 >>> probs = smirnov(n, Dnpm) >>> print(chr(10).join(['For a sample of size %d drawn from a N(0, 1) distribution:' % n, ... ' Smirnov n=%d: Prob(Dn- >= %f) = %.4f' % (n, Dnpm[0], probs[0]), ... ' Smirnov n=%d: Prob(Dn+ >= %f) = %.4f' % (n, Dnpm[1], probs[1])])) For a sample of size 5 drawn from a N(0, 1) distribution: Smirnov n=5: Prob(Dn- >= 0.246201) = 0.4713 Smirnov n=5: Prob(Dn+ >= 0.224726) = 0.5243
根据目标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='m', linestyle='dashed', lw=4) >>> plt.show()