How to Check Normality of Data Using Anderson-Darling Test in Python
Share
Condition for Checking Normality of Data Using Anderson-Darling Test in Python
Description:
The Anderson-Darling test is a statistical test used to assess whether a sample comes
from a specific distribution, typically a normal distribution.
It is an extension of the Kolmogorov-Smirnov test and is more sensitive to the tails
of the distribution.
It is used to evaluate the goodness of fit of a sample to a theoretical distribution,
and it provides a test statistic along with critical values for different significance
levels.
Step-by-Step Process
Import Libraries:
Import the necessary libraries (`numpy`, `scipy`).
Prepare the Dataset:
Provide a dataset (either real or generated data).
Perform the Anderson-Darling Test:
Use `scipy.stats.anderson()` to perform the test.
Interpret the Result:
Compare the test statistic with critical values for the chosen significance level.
Sample Source Code
# Code for Anderson Darling Test
import numpy as np
from scipy import stats
# Generate random data from a normal distribution (mean=50, std=10)
data = np.random.normal(loc=50, scale=10, size=1000)
# Perform the Anderson-Darling test for normality
result = stats.anderson(data, dist='norm')
print(f"Anderson-Darling Test Statistic: {result.statistic}")
alpha = 0.05
if result.statistic > result.critical_values[2]:
print("The data is likely not normally distributed (Reject H0).")
else:
print("The data is likely normally distributed (Fail to Reject H0).")