10 Normality Tests in Python (Step-By-Step Guide 2020)
By: Sivasai Yadav Mudugandla
Tags: Statistics, Artificial Intelligence, Python, Data Science, Machine Learning

10 Normality Tests in Python (Step-By-Step Guide 2020)

Normality test is used to check if a variable or sample has a normal distribution.

Image of Author

Before going to talk about Normality test lets first discuss normal distribution and why is it so important?

Normal distribution

The normal distribution also known as the Gaussian distribution is a probability function that describes how the values of a variable are distributed. It is a symmetric distribution where most of the observations fall around the central peak and the probabilities for values further away from the mean taper off equally in both directions with fewer outliers on the high and low ends of the data range.

The term “Gaussian distribution” refers to the German mathematician Carl Friedrich Gauss.

A normal distribution has some important properties:

the mean, median, and mode all represent the center of the distribution. the distribution is a bell shape ≈68% of the data falls within 1 standard deviation of the mean, ≈95% of the data falls within 2 S.D of the mean and ≈99.7% of the data falls within 3 S.D of the mean

Image from Christian Hubicki.

Why is it so important

A normal distribution is the most important probability distribution in statistics because

Many processes in nature follow the Normal distribution, Some of the examples are age, height, weight and blood pressure of a person Linear regression assumes that errors or residuals follow a normal distribution. Some ML algorithms like Linear Discriminant Analysis and Quadratic Discriminant Analysis are derived under the assumption of normal distribution.

Normality tests

In statistics, normality tests are used to check if the data is drawn from a Gaussian distribution or in simple if a variable or in sample has a normal distribution.

There are two ways to test normality,

Graphs for Normality test Statistical Tests for Normality

1. Graphs for Normality test

Various graphs can be used to test the normality of a variable. Using graphs/plots we can visually see the normality but graphs are not very accurate as statistical methods.

1.Q Q or Quantile-Quantile Plot

It plots two sets of quantiles against one another i.e. theoretical quantiles against the actual quantiles of the variable.

Image from Author

If our data comes from a normal distribution, we should see all the points sitting on the straight line.

2. Box Plot

Box Plot also know as a box and whisker plot is another way to visualize the normality of a variable. It displays the distribution of data based on a five-number summary i.e. minimum, first quartile (Q1), median (Q2), third quartile (Q3) and maximum.

Image from Author

If your variable has a normal distribution, we should see the mean and median in the center.

3. Histogram

One of the popular and commonly used plot to visualize the distribution of the data is a histogram. It also allows us to inspect data for its underlying outliers, skewness, etc. It divides the data into bins of equal width. Each bin is plotted as a bar and height of the bar depends on the number of the data points are in that bin.

Image from Author

If your variable has a normal distribution, we should see a bell curve.

2. Statistical Tests for Normality

On the other hand, there are many Statistical Tests to check if the distribution of a variable is normal/gaussian. In this section, I am not gonna talk about the math behind but I will show you the python code for each test.

Shapiro-Wilk Test

We should start with the Shapiro-Wilk Test. It is the most powerful test to check the normality of a variable. It was proposed in 1965 by Samuel Sanford Shapiro and Martin Wilk.

Image from Author

If the p-value ≤ 0.05, then we reject the null hypothesis i.e. we assume the distribution of our variable is not normal/gaussian.

p-value ≤ 0.05, then we reject the null hypothesis i.e. we assume the distribution of our variable is not normal/gaussian. If the p-value > 0.05, then we fail to reject the null hypothesis i.e. we assume the distribution of our variable is normal/gaussian.

2. D’Agostino’s K-squared test

D’Agostino’s K-squared test check’s normality of a variable based on skewness and kurtosis. It was named by Ralph D’Agostino.

Skewness is a measure of symmetry. Kurtosis is a measure of whether the data are heavy-tailed or light-tailed relative to a normal distribution.

Image from Author

If the p-value ≤ 0.05, then we reject the null hypothesis i.e. we assume the distribution of our variable is not normal/gaussian.

p-value ≤ 0.05, then we reject the null hypothesis i.e. we assume the distribution of our variable is not normal/gaussian. If the p-value > 0.05, then we fail to reject the null hypothesis i.e. we assume the distribution of our variable is normal/gaussian.

3. Anderson-Darling Normality Test

Anderson-Darling Normality Test is another general normality tests designed to determine if the data comes from a specified distribution, in our case, the normal distribution. It was developed in 1952 by Theodore Anderson and Donald Darling.

Image from Author

It gives a range of critical values, at which the null hypothesis can be failed to rejected if the calculated statistic is less than the critical value. In our case, at each significance level, the data has a gaussian distribution.

4. Chi-Square Normality Test

Another way of checking the normality of a variable is with Chi-Square Normality Test. It is not as popular as other methods.

Image from Author

If the p-value ≤ 0.05, then we reject the null hypothesis i.e. we assume the distribution of our variable is not normal/gaussian.

p-value ≤ 0.05, then we reject the null hypothesis i.e. we assume the distribution of our variable is not normal/gaussian. If the p-value > 0.05, then we fail to reject the null hypothesis i.e. we assume the distribution of our variable is normal/gaussian.

5. Lilliefors Test for Normality

The Lilliefors test is a normality test based on the Kolmogorov–Smirnov test. As all the above methods, this test is used to check if the data come from a normal distribution. It is named after Hubert Lilliefors, professor of statistics at George Washington University.

Image from Author

If the p-value ≤ 0.05, then we reject the null hypothesis i.e. we assume the distribution of our variable is not normal/gaussian.

p-value ≤ 0.05, then we reject the null hypothesis i.e. we assume the distribution of our variable is not normal/gaussian. If the p-value > 0.05, then we fail to reject the null hypothesis i.e. we assume the distribution of our variable is normal/gaussian.

6. Jarque–Bera test for Normality

The Jarque-Bera test tests whether the sample data has the skewness and kurtosis matching a normal distribution.

NOTE: This test only works for a large enough number of data samples (>2000).

Image from Author

If the p-value ≤ 0.05, then we reject the null hypothesis i.e. we assume the distribution of our variable is not normal/gaussian.

p-value ≤ 0.05, then we reject the null hypothesis i.e. we assume the distribution of our variable is not normal/gaussian. If the p-value > 0.05, then we fail to reject the null hypothesis i.e. we assume the distribution of our variable is normal/gaussian.

7. Kolmogorov-Smirnov test for Normality

Performs the (one sample or two samples) Kolmogorov-Smirnov test for goodness of fit. The one-sample test performs a test of the distribution F(x) of an observed random variable against a given distribution G(x) (i.e. a normal distribution).

Image from Author