本文介绍了我如何在 python 中进行 F 测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何进行 F 检验以检查 Python 中两个向量的方差是否相等?

How do I do an F-test to check if the variance is equivalent in two vectors in Python?

例如如果我有

a = [1,2,1,2,1,2,1,2,1,2]
b = [1,3,-1,2,1,5,-1,6,-1,2]

有没有类似的东西

scipy.stats.ttest_ind(a, b)

我找到了

sp.stats.f(a, b)

但它似乎与 F-test 不同

But it appears to be something different to an F-test

推荐答案

等方差的检验统计量 F 检验很简单:

The test statistic F test for equal variances is simply:

F = Var(X) / Var(Y)

其中 F 分布为 df1 = len(X) - 1, df2 = len(Y) - 1

scipy.stats.f 您在问题中提到的具有 CDF 方法.这意味着您可以为给定的统计量生成 p 值并测试该 p 值是否大于您选择的 alpha 水平.

scipy.stats.f which you mentioned in your question has a CDF method. This means you can generate a p-value for the given statistic and test whether that p-value is greater than your chosen alpha level.

因此:

alpha = 0.05 #Or whatever you want your alpha to be.
p_value = scipy.stats.f.cdf(F, df1, df2)
if p_value > alpha:
    # Reject the null hypothesis that Var(X) == Var(Y)

请注意,F 检验对 X 和 Y 的非正态性极为敏感,因此您最好进行更可靠的测试,例如 Levene's testBartlett's test 除非您有理由确信 X和 Y 正态分布.这些测试可以在 scipy api 中找到:

Note that the F-test is extremely sensitive to non-normality of X and Y, so you're probably better off doing a more robust test such as Levene's test or Bartlett's test unless you're reasonably sure that X and Y are distributed normally. These tests can be found in the scipy api:

这篇关于我如何在 python 中进行 F 测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 07:08