Skip to content
Related Articles

Related Articles

How to Perform an F-Test in Python

View Discussion
Improve Article
Save Article
  • Last Updated : 20 Feb, 2022
View Discussion
Improve Article
Save Article

In this article, we will be looking at the approach to performing an F-Test in the python programming language. The scipy stats.f() function in Python with the certain parameters required to be passed to get the F- test of the given data.

scipy stats.f(): It is an F continuous random variable that is defined with a standard format and some shape parameters to complete its specification.

Syntax: scipy stats.f()

Parameters:

  • x :  quantiles
  • q :  lower or upper tail probability
  • dfn, dfd shape parameters
  • loc :location parameter
  • scale :  scale parameter (default=1)
  • size :shape of random variate
  • moments : composed of letters [‘mvsk’] specifying which moments to compute

In this example, we will be using the data from the normalized distributed sample and with the different variance values and we will be further using the scipy.stats.f.cdf() to get the data’s F-test in the python programming language.

Python




import numpy as np
import scipy.stats
  
# Create data
group1 = [0.28, 0.2, 0.26, 0.28, 0.5]
group2 = [0.2, 0.23, 0.26, 0.21, 0.23]
  
# converting the list to array
x = np.array(group1)
y = np.array(group2)
  
# calculate variance of each group
print(np.var(group1), np.var(group2))
  
def f_test(group1, group2):
    f = np.var(group1, ddof=1)/np.var(group2, ddof=1)
    nun = x.size-1
    dun = y.size-1
    p_value = 1-scipy.stats.f.cdf(f, nun, dun)
    return f, p_value
  
# perform F-test
f_test(x, y)


Output:

0.010464 0.00042400000000000017— Variances

(24.679245283018858, 0.004431318383760985) –F-test values

Interpretation from the test:

The F test statistic is 24.67 and the p-value is 0.0044 we would reject the null hypothesis asp-value<=0.05 so we can say simply by looking at the p-value of the data used that the two population variances are not equal.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!