使用 Scipy 的 stats.kstest 模块进行拟合优度测试 [英] Using Scipy's stats.kstest module for goodness-of-fit testing

查看:72
本文介绍了使用 Scipy 的 stats.kstest 模块进行拟合优度测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了关于这个模块的现有帖子(和 Scipy 文档),但我仍然不清楚如何使用 Scipy 的 kstest 模块进行拟合优度测试,当你有一个数据集和一个可调用函数.

我想用来测试我的数据的 PDF 不是标准的 scipy.stats 分布之一,所以我不能只使用以下内容来调用它:

kstest(mydata,'norm')

其中 mydata 是一个 Numpy 数组.相反,我想做类似的事情:

kstest(mydata,myfunc)

其中 'myfunc' 是可调用函数.这不起作用——这并不奇怪,因为 kstest 无法知道 'mydata' 数组的横坐标是什么,以便使用 'myfunc' 生成相应的理论频率.假设 'mydata' 中的频率对应于随机变量的值是数组 'abscissa'.然后我想也许我可以使用 stats.ks_2samp:

ks_2samp(mydata,myfunc(abscissa))

但我不知道这在统计上是否有效.(旁注:kstest 和 ks_2samp 是否希望频率数组归一化为 1,或者他们想要绝对频率?)

在任何情况下,由于单样本 KS 测试应该用于拟合优度测试,我必须假设有某种方法可以直接使用 kstest 进行测试.你是怎么做到的?

解决方案

一些示例可能会阐明如何使用 scipy.stats.kstest.让我们首先设置一些测试数据,例如正态分布,均值为 5,标准差为 10:

<预><代码>>>>数据 = scipy.stats.norm.rvs(loc=5, scale=10, size=(1000,))

为了对这些数据运行kstest,我们需要一个函数f(x),它接受一个分位数数组,并返回累积分布函数的相应值.如果我们重用 scipy.stats.normcdf 函数,我们可以这样做:

<预><代码>>>>scipy.stats.kstest(数据, lambda x: scipy.stats.norm.cdf(x, loc=5, scale=10))(0.019340993719575206, 0.84853828416694665)

以上通常会以更方便的形式运行:

<预><代码>>>>scipy.stats.kstest(data, 'norm', args=(5, 10))(0.019340993719575206, 0.84853828416694665)

如果我们有均匀分布的数据,手工构建cdf很容易:

<预><代码>>>>数据 = np.random.rand(1000)>>>scipy.stats.kstest(数据,lambda x:x)(0.019145675289412523, 0.85699937276355065)

I've read through existing posts about this module (and the Scipy docs), but it's still not clear to me how to use Scipy's kstest module to do a goodness-of-fit test when you have a data set and a callable function.

The PDF I want to test my data against isn't one of the standard scipy.stats distributions, so I can't just call it using something like:

kstest(mydata,'norm')

where mydata is a Numpy array. Instead, I want to do something like:

kstest(mydata,myfunc)

where 'myfunc' is the callable function. This doesn't work—which is unsurprising, since there's no way for kstest to know what the abscissa for the 'mydata' array is in order to generate the corresponding theoretical frequencies using 'myfunc'. Suppose the frequencies in 'mydata' correspond to the values of the random variable is the array 'abscissa'. Then I thought maybe I could use stats.ks_2samp:

ks_2samp(mydata,myfunc(abscissa))

but I don't know if that's statistically valid. (Sidenote: do kstest and ks_2samp expect frequency arrays to be normalized to one, or do they want the absolute frequencies?)

In any case, since the one-sample KS test is supposed to be used for goodness-of-fit testing, I have to assume there's some way to do it with kstest directly. How do you do this?

解决方案

Some examples may shed some light on how to use scipy.stats.kstest. Lets first set up some test data, e.g. normally distributed with mean 5 and standard deviation 10:

>>> data = scipy.stats.norm.rvs(loc=5, scale=10, size=(1000,))

To run kstest on these data we need a function f(x) that takes an array of quantiles, and returns the corresponding value of the cumulative distribution function. If we reuse the cdf function of scipy.stats.norm we could do:

>>> scipy.stats.kstest(data, lambda x: scipy.stats.norm.cdf(x, loc=5, scale=10))
(0.019340993719575206, 0.84853828416694665)

The above would normally be run with the more convenient form:

>>> scipy.stats.kstest(data, 'norm', args=(5, 10))
(0.019340993719575206, 0.84853828416694665)

If we have uniformly distributed data, it is easy to build the cdf by hand:

>>> data = np.random.rand(1000)
>>> scipy.stats.kstest(data, lambda x: x)
(0.019145675289412523, 0.85699937276355065)

这篇关于使用 Scipy 的 stats.kstest 模块进行拟合优度测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆