How do I do a F-test in python

python, statistics

Solution

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 = 1 - scipy.stats.f.cdf(F, df1, df2)

Alternatively, the p-value can be calculated using the sf-method of the f-test:

p_value = scipy.stats.f.sf(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

Problem

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,1,5,-1,6,-1,2] ``` is there something similar to ``` scipy.stats.ttest_ind(a, b) ``` I found ``` sp.stats.f(a, b) ``` But it appears to be something different to an F-test

Original source