2012-08-27 32 views
2

我在scipy(scipy.stats.kstest)中使用Kolmogorov-Smirnov测试时遇到了麻烦。在线文档(http://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.kstest.html)表示它需要样本,cdf与只命名其中一个scipy的选项进行比较.stats分布的CDF参数(和任选的几个值)scipy.stats.kstest与非标准分发

只要所选择的CDF不需要任何额外的参数,所有出现细

teststat,pval=stats.kstest(sample,'norm') 

(其中样品是值的列表)。 但是,对于其他需要额外修正的发行版,如t,chisquared等,它不适用于我。它正确地抗议,如果没有进一步给出参数

teststat,pval=stats.kstest(sample,'t') 

TypeError: _cdf() takes exactly 3 arguments (2 given) 

如果给定参数,

teststat,pval=stats.kstest(sample,'t',24) 

它抱怨

TypeError: cdf() argument after * must be a sequence, not int 

现在我不能完全肯定这意味着什么,但它似乎它不想int,24,而是一个int,(24)的序列。但是:

teststat,pval=stats.kstest(sample,'t',24) 

TypeError: cdf() argument after * must be a sequence, not int 

手动定义分布不产生更好的结果要么是因为它不觉得这是调用的:

numargs = stats.t.numargs 
[ df ] = [0.9,] * numargs 
rv = stats.t(df) 
teststat,pval=stats.kstest(sample,stats.t.cdf(numpy.linspace(0, numpy.minimum(rv.dist.b, 3)),df)) 

TypeError: 'numpy.ndarray' object is not callable 

我该怎么办,使其工作? (谷歌搜索无论是kstest功能或各种错误消息,不要把任何东西来回答这个问题非常有用。)

感谢

回答

2

在这个error寻找:

TypeError: cdf() argument after * must be a sequence, not int 

让我觉得,你是对的,它需要一个序列,而不是一个整数。该文件说

args : tuple, sequence 
    distribution parameters, used if rvs or cdf are strings 

这似乎工作:

>>> import scipy.stats 
>>> sample = scipy.stats.t(1).rvs(size=10**6) 
>>> scipy.stats.kstest(sample, 't', (1,)) 
(0.0006249662221899932, 0.82960203415652445) 

或更明确:

>>> scipy.stats.kstest(sample, 't', args=(1,)) 
(0.0006249662221899932, 0.82960203415652445) 
+0

感谢DSM, 你,当然,正确的。 (x,)是声明序列的正确方法;我不熟悉那种变量类型。谢谢。 – 0range