# 10 Most Popular Statistical Hypothesis Testing Methods Using Python

Decision making and storytelling are two important facets of a data scientist’s job description. Models can be tweaked and computational powers can be pumped up. But to choose a certain test or a method, will have great implications on the product lifecycle. From cost-cutting to life-saving, hypothesis testing is prevalent in the world of statistics and with the conception of statistical machine learning, the tests have been imbibed and are made more accessible with the Python’s ever-increasing and improving, task-specific libraries.

Statistical tests are commonly classified as parametric and non-parametric tests. Parametric tests are conducted, with an assumption that the data follows a Gaussian distribution. If this assumption fails, then non-parametric tests are considered for hypothesis testing.

Here we list few widely used statistical tests(parametric and non-parametric) available in Python:

### Chi-Squared Test

Chi-squared test is a well-known test even for those who are starting with statistical machine learning. Here, this test is used to check whether two categorical variables are related or independent. And, it is assumed that the observations used in the calculation of the contingency table are independent.

Python Code

from scipy.stats import chi2_contingency
table = ...
stat, p, dof, expected = chi2_contingency(table)

### Student’s t-test

Tests whether the means of two independent samples are significantly different.

Observations in each sample are independent and identically distributed (iid). Observations in each sample are normally distributed. Observations in each sample have the same variance.

Python Code

from scipy.stats import ttest_ind
data1, data2 = ...
stat, p = ttest_ind(data1, data2)

### Analysis of Variance Test (ANOVA)

ANOVA is another widely popular test which is used to test how independent two samples are of each other. Here the observations are assumed to follow a normal distribution without any change in the variance.

Python Code

from scipy.stats import f_oneway
data1, data2, ... = ...
stat, p = f_oneway(data1, data2, ...)

### Shapiro-Wilk Test

This test is used to check whether the sample data has a Gaussian distribution.

Python Code

from scipy.stats import shapiro
data1 = ....
stat, p = shapiro(data)

### D’Agostino’s K^2 Test

Similar to Shapiro-Wilk test, this too is used to check for Gaussian distribution in data samples.

Python Code

from scipy.stats import normaltest
data1 = ....
stat, p = normaltest(data)

### Pearson’s Correlation Coefficient

A statistical test for checking correlation between two samples and whether they have a linear relationship.

Python Code

from scipy.stats import pearsonr
data1, data2 = ...
corr, p = pearsonr(data1, data2)

### Spearman’s Rank Correlation

Observations in each sample are assumed that they can be ranked, for checking whether the relationship is monotonic or not.

Python Code

from scipy.stats import spearmanr
data1, data2 = ...
corr, p = spearmanr(data1, data2)

Mann-Whitney U Test

A non-parametric statistical hypothesis test to check for independent samples and to find whether the distributions are equal or not.

Python Code

from scipy.stats import mannwhitneyu
data1, data2 = ...
stat, p = mannwhitneyu(data1, data2)

### Kruskal-Wallis H Test

Like previous tests, Kruskal-Wallis hypothesis test also makes the same assumptions regarding the distribution and ranking of the observations in each sample. And, the test is carried to check for the independence of the observations from each other.

Python Code

from scipy.stats import kruskal
data1, data2, ... = ...
stat, p = kruskal(data1, data2, ...)

### Friedman Test

Friedman test checks whether the distributions of two or more paired samples are equal or not.

Python Code

from scipy.stats import friedmanchisquare
data1, data2, ... = ...
stat, p = friedmanchisquare(data1, data2, ...)

### Conclusion

The probability of rejecting the null hypothesis is a function of five factors: whether the test is one- or two-tailed, the level of significance, the standard deviation, the amount of deviation from the null hypothesis, and the number of observations. Having said that, statistical tests are also subject to criticism. For instance, while interpreting the p-value, the way multiple comparisons are done is tricky because p-values depend on both data observed and data that might have been observed but wasn’t.  Therefore, a statistician or an analyst or a data scientist should be aware of the fact that statistical significance does not imply practical significance and correlation doesn’t imply causation. Every test is only a means to an end which is, often vague.

## Download our Mobile App

I have a master's degree in Robotics and I write about machine learning advancements.

## Oct 11-13, 2023 | Bangalore

### Telegram group

Discover special offers, top stories, upcoming events, and more.

### Discord Server

Stay Connected with a larger ecosystem of data science and ML Professionals

### Will Meteor Lake Be Intel’s Saving Grace?

After Sapphire Rapids left a bad taste in customer’s mouths, Intel might bring victory back with Meteor Lake.

### How Whatfix is Revolutionising SaaS with GenAI Integration

With the help of a 12-member team split into two groups, Whatfix is working towards generative AI implementation across its products and functions with a focus on responsible AI.

### Council Post: AI’s Evolutionary Journey – Model-Centric to Data-Centric to Decision-Centric

The emerging trend of decision-centric AI holds great promise in transforming the way we make decisions by integrating data analysis, machine learning, and optimization techniques.

### Beware of the AI Bubble Burst

AI is this year’s investment craze but experts pinpoint the warning signs of an AI bubble forming

### Try Out These 5 New Crazy Features on Adobe Firefly

Currently, in beta testing, Adobe Firefly allows users to access its generative AI-based editing models through a waitlist.

### AWS Fires Up Indian Govt’s Digital Ambitions

AWS has helped the Telangana government migrate their mission-critical core application for e-governance across 93 districts

### 5 Things That Killed The Metaverse

In less than two years since genesis, the metaverse has already met its demise. Here are five reasons why we’ll never see the metaverse again.

### ChatGPT Craves Human Expertise

Looks like RLHF was not enough human interaction for OpenAI’s ChatGPT, which now craves more

### India’s Semiconductor Dreams Plunge into Chaos and Uncertainty

The delays and inefficiencies in communication and processing have resulted in the loss of one-and-a-half years

### Spark vs Presto: Unleashing the Power of Data

Although both Spark and Presto are preferred by companies because of similar similar offerings, they come with their own share of differences.