在python中编码排列测试的错误在哪里?

时间:2014-10-08 15:02:59

标签: python python-3.x statistics permutation itertools

我正在使用Python 3.4.1并尝试使用 itertools 重建置换测试。

here上找到的热门代码是:

from itertools import combinations as comb

def permutationTest(a, b):
    ab = a + b
    Tobs = sum(a)
    under = 0
    for count, perm in enumerate(comb(ab, len(a)), 1):
        if sum(perm) <= Tobs:
            under += 1
    return under * 100. / count

treatmentGroup = [9.1, 10.3, 11.0, 11.5, 11.9]
controlGroup   = [8.1, 8.4, 9.2, 9.4]
under = permutationTest(treatmentGroup, controlGroup)

averageTreatment = sum(treatmentGroup) / float(len(treatmentGroup))
averageControl = sum(controlGroup) / float(len(controlGroup))
print("under=%.2f%%, over=%.2f%%" % (under, 100. - under))
print ('The average of threatment group is', averageTreatment)
print ('The average of control group is', averageControl)

如您所见,有两个(治疗和控制)组像往常一样定义。我还在here找到了一所大学的例子。 我现在的问题:如您所见,结果不同。

有谁知道,错误位于何处? (我想这是在Python脚本中......)

感谢您的建议!

1 个答案:

答案 0 :(得分:0)

你遇到的问题在于<=。在您链接的文档中,如果t&gt; = 1.98,则将它们添加到结尾,因此只应将值t&lt; 1.98,或sum(perm) < Tobs