首页 文章

如何解释`scipy.stats.kstest`和`ks_2samp`来评估数据的“拟合”?

提问于
浏览
3

I'm trying to evaluate/test how well my data fits a particular distribution.

有几个问题,我被告知使用 scipy.stats.kstestscipy.stats.ks_2samp . 这似乎很简单,给它:(A)数据; (2)分配; (3)拟合参数 . 唯一的问题是我的结果不适合不同的发行版但是从 kstest 的输出中,我不知道我是否可以这样做?

Goodness of fit tests in SciPy

“[SciPy]包含K-S”

Using Scipy's stats.kstest module for goodness-of-fit testing

“第一个值是测试统计数据,第二个值是p值 . 如果p值小于95(显着性水平为5%),这意味着你不能拒绝Null-Hypothese那个两个样本分布完全相同 . “

这只是展示如何适应:Fitting distributions, goodness of fit, p-value. Is it possible to do this with Scipy (Python)?

np.random.seed(2)
# Sample from a normal distribution w/ mu: -50 and sigma=1
x = np.random.normal(loc=-50, scale=1, size=100)
x
#array([-50.41675785, -50.05626683, -52.1361961 , -48.35972919,
#        -51.79343559, -50.84174737, -49.49711858, -51.24528809,
#        -51.05795222, -50.90900761, -49.44854596, -47.70779199,

# ...
#        -50.46200535, -49.64911151, -49.61813377, -49.43372456,
#        -49.79579202, -48.59330376, -51.7379595 , -48.95917605,
#        -49.61952803, -50.21713527, -48.8264685 , -52.34360319])

# Try against a Gamma Distribution
distribution = "gamma"
distr = getattr(stats, distribution)
params = distr.fit(x)

stats.kstest(x,distribution,args=params)
KstestResult(statistic=0.078494356486987549, pvalue=0.55408436218441004)

A p_value of pvalue=0.55408436218441004 is saying that the normal and gamma sampling are from the same distirbutions?

我认为伽玛分布必须包含正值? https://en.wikipedia.org/wiki/Gamma_distribution

现在反对正常分布:

# Try against a Normal Distribution
distribution = "norm"
distr = getattr(stats, distribution)
params = distr.fit(x)

stats.kstest(x,distribution,args=params)
KstestResult(statistic=0.070447707170256002, pvalue=0.70801104133244541)

根据这个,如果我采用最低的p_值,那么 I would conclude my data came from a gamma distribution even though they are all negative values?

np.random.seed(0)

distr = getattr(stats, "norm")
x = distr.rvs(loc=0, scale=1, size=50)
params = distr.fit(x)
stats.kstest(x,"norm",args=params, N=1000)

KstestResult(statistic=0.058435890774587329, pvalue=0.99558592119926814)

This means at a 5% level of significance, I can reject the null hypothesis that distributions are identical. So I conclude they are different but they clearly aren't? 我是否错误地解释了这个?如果我把它设为单尾,是否会使它越大,它们来自同一分布的可能性越大?

1 回答

  • 9

    因此,KT检验的零假设是分布是相同的 . 因此,你的p值越低,你必须拒绝零假设的统计证据越大,并得出结论不同的分布 . 该测试只能让您确信您的分布是不同的,而不是相同的,因为测试旨在找到类型I错误的概率 alpha .

    此外,我非常确定KT测试只有在事先考虑到完全指定的分布时才有效 . 在这里,您只需在一些数据上拟合伽马分布,当然,测试产生高p值也就不足为奇了(即您不能拒绝分布相同的零假设) .

    真的很快,这里是你适合的Gamma的pdf(蓝色)与你采样的正态分布的pdf(绿色):

    In [13]: paramsd = dict(zip(('shape','loc','scale'),params))
    
    In [14]: a = paramsd['shape']
    
    In [15]: del paramsd['shape']
    
    In [16]: paramsd
    Out[16]: {'loc': -71.588039241913037, 'scale': 0.051114096301755507}
    
    In [17]: X = np.linspace(-55, -45, 100)
    
    In [18]: plt.plot(X, stats.gamma.pdf(X,a,**paramsd))
    Out[18]: [<matplotlib.lines.Line2D at 0x7ff820f21d68>]
    

    enter image description here

    很明显,这些并没有太大的不同 . 实际上,该测试将经验CDF(ECDF)与您候选分布的CDF进行比较(同样,您从将数据拟合到该分布中得出),并且测试统计量是最大差异 . 借用ECDF from here的实现,我们可以看到任何这样的最大差异都很小,测试显然不会拒绝零假设:

    In [32]: def ecdf(x):
       .....:         xs = np.sort(x)
       .....:         ys = np.arange(1, len(xs)+1)/float(len(xs))
       .....:         return xs, ys
       .....: 
    
    In [33]: plt.plot(X, stats.gamma.cdf(X,a,**paramsd))
    Out[33]: [<matplotlib.lines.Line2D at 0x7ff805223a20>]
    
    In [34]: plt.plot(*ecdf(x))
    Out[34]: [<matplotlib.lines.Line2D at 0x7ff80524c208>]
    

    enter image description here

相关问题