How do I do a F-test in python

后端 未结 4 1514
借酒劲吻你
借酒劲吻你 2020-12-23 17:17

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

For example if I have

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


        
4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-23 17:33

    The test statistic F test for equal variances is simply:

    F = Var(X) / Var(Y)
    

    Where F is distributed as df1 = len(X) - 1, df2 = len(Y) - 1

    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.

    Thus:

    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)
    

    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:

    • Bartlett's test
    • Levene's test

提交回复
热议问题