# How to Perform an Anderson-Darling Test in Python

### Introduction

Statistical tests are critical in determining the nature of your data, enabling you to make appropriate assumptions and select suitable models for analysis. One such test is the Anderson-Darling (A-D) Test, a statistical test used to check whether a given sample of data is drawn from a specific probability distribution.

Named after Theodore Anderson and Donald Darling, the A-D test is particularly suited to identifying whether data follow a particular distribution. Unlike many other goodness-of-fit tests, the Anderson-Darling test gives more weight to the tails of the distribution. This property makes the A-D test very effective for many practical applications since it has higher sensitivity for the distribution tails.

In Python, we can perform the Anderson-Darling test using the scipy library. This article will guide you on how to perform the Anderson-Darling test in Python.

### Performing the Anderson-Darling Test

Let’s illustrate the use of the Anderson-Darling test using a sample of data drawn from a normal distribution:

import numpy as np
from scipy import stats

# Generate a sample data from a standard normal distribution
np.random.seed(0)
data = np.random.normal(loc=0, scale=1, size=1000)

We generate 1000 data points from a standard normal distribution (mean = 0, standard deviation = 1). Now, we want to test if this data follows a normal distribution using the Anderson-Darling test.

The scipy library provides the anderson function, which we can use to perform the Anderson-Darling test:

# Perform the Anderson-Darling test
result = stats.anderson(data)

print(f'Statistic: {result.statistic:.2f}')

for i in range(len(result.critical_values)):
sl, cv = result.significance_level[i], result.critical_values[i]
if result.statistic < cv:
print(f'At the {sl}% significance level, the data looks normally distributed (CV: {cv:.2f}).')
else:
print(f'At the {sl}% significance level, the data does not look normally distributed (CV: {cv:.2f}).')

The anderson function returns an AndersonResult object which contains the following attributes:

• statistic: The Anderson-Darling test statistic.
• critical_values: The critical values for this distribution.
• significance_level: The significance levels for the corresponding critical values in a distribution.

The logic to interpret the Anderson-Darling test’s result is straightforward: if the calculated test statistic is less than the critical value at a chosen significance level, the data is likely drawn from the tested distribution.

### Other Distributions

The Anderson-Darling test isn’t just restricted to normal distributions. The anderson function in scipy also supports the exponential, logistic, and Gumbel distributions.

To test for these distributions, pass the name of the distribution as the second argument to the anderson function. Here is an example of testing whether data is drawn from an exponential distribution:

# Generate a sample data from an exponential distribution
np.random.seed(0)
data = np.random.exponential(scale=1, size=1000)

# Perform the Anderson-Darling test for the exponential distribution
result = stats.anderson(data, dist='expon')

print(f'Statistic: {result.statistic:.2f}')

for i in range(len(result.critical_values)):
sl, cv = result.significance_level[i], result.critical_values[i]
if result.statistic < cv:
print(f'At the {sl}% significance level, the data looks exponentially distributed (CV: {cv:.2f}).')
else:
print(f'At the {sl}% significance level, the data does not look exponentially distributed (CV: {cv:.2f}).')

### Conclusion

The Anderson-Darling test is an essential tool in your statistical toolkit. It’s a powerful test for checking if a sample of data is drawn from a particular distribution. It’s especially sensitive to deviations in the tails of the distribution, making it useful for many practical applications. In Python, performing the Anderson-Darling test is straightforward using the scipy.stats.anderson function.

Remember, while the Anderson-Darling test can provide evidence supporting a hypothesis that the data follow a particular distribution, it cannot prove it. Also, failing the Anderson-Darling test doesn’t necessarily mean you cannot use a particular statistical method. It means you need to account for the distribution properties when interpreting your data.