Open In App

How to Perform a Brown – Forsythe Test in Python

Last Updated : 28 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisites: Parametric and Non-Parametric Methods, Hypothesis Testing 

In this article, we will be looking at the approach to perform a brown-Forsythe test in the Python programming language. Brown–Forsythe test is a statistical test for the equality of group variances based on performing an Analysis of Variance (ANOVA) on a transformation of the response variable.

A one-way ANOVA is employed to see whether or not or not there’s a big distinction between the means of 3 or additional independent teams. And the assumption of a one-way ANOVA is that the variances of the populations that the samples come from are equal. Ways to test by using a Brown-Forsythe test is by following hypotheses:

  • Ho: The variances among the populations are equal.
  • Ha: The variances among the populations are not equal.

Note: If the p-value of the test is less than some significance level (e.g.α = .05) then we reject the null hypothesis and finish that the variances aren’t equal to a few of the exclusive populations.

Get the variance of the Data Used

Before performing the Brown-Forsythe test, we need to calculate the variance of each group so that it can be seen that the variances between the groups differ, but to determine if these differences are statistically large we are able to perform the Brown-Forsythe test take a look at.

Python




import numpy as np
 
# Create data
group1 = [456, 564, 54, 554, 54, 51, 1, 12, 45, 5]
group2 = [65, 87, 456, 564, 456, 564, 564, 6, 4, 564]
 
# calculate variance of each group
print(np.var(group1), np.var(group2))


Output:

52101.43999999999 59121.2

Perform brown Forsythe test using Levene() function

In this approach to perform the brown Forsythe test, the user needs to first call the Levene() function from the scipy.stats library and pass the required parameter to it and then this function will be returning back the test statistic value to the user.

Syntax: levene(sample1, sample2, …s, center=’median’, proportiontocut=0.05)

Parameters:

  • sample1, sample2, …: The sample data, possibly with different lengths. Only one-dimensional samples are accepted.
  • center: Which function of the data to use in the test. The default is ‘median’.
  • proportiontocutfloat, optional: When the center is ‘trimmed’, this gives the proportion of data points to cut from each end.

Example:

In this example, we have two groups with ten elements each and we are using the Levene() function to get its brown Forsythe test in the python programming language.

Python




# Create data
import scipy.stats as stats
group1 = [456, 564, 54, 554, 54, 51, 1, 12, 45, 5]
group2 = [65, 87, 456, 564, 456, 564, 564, 6, 4, 564]
 
# conduct the Wilcoxon-Signed Rank Test
stats.levene(group1, group2, center='median')


Output:

LeveneResult(statistic=0.33617324893734357, pvalue=0.5692334858602581)

Output Interpretation

So, here the test statistic value is 0.33617 and the P-value is 0.569233. The p-value of the test seems to be more than .05, so we fail to reject the null hypothesis of the test. However, in the above example, we passed the test but in case we failed we have the apply the following steps:

Proceed with a One-Way ANOVA anyway:

Here, the one-way ANOVA is unequal variances as long as the largest variance is no larger(59121.2) than 4 times the smallest variance(52101.43).

59121.2!=208405.72(52101.43*4)

Perform a Kruskal-Wallis Test:

If the ratio of the largest variance to the smallest variance is greater than 4, we may choose to perform a Kruskal-Wallis test. This is taken into consideration the non-parametric equivalent to the one-way ANOVA. The ratio of the largest to smallest variance is  59121.2/ 52101.43 = 1.13, which is extremely less than 4, so we can proceed with the one-way ANOVA even if the Brown-Forsythe test indicated that the variances had been no longer equal.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads