# Testing for Normality: Introducing the Jarque-Bera Test in Python

## Introduction to Jarque-Bera Test

Have you ever wondered how data is analyzed to check for normality? One of the statistical methods used to test whether a set of observations come from a normal distribution is the Jarque-Bera test.

Understanding this test is crucial for any data analysis task, as it helps you to determine the distribution of your dataset and, in turn, choose the appropriate statistical techniques to use. In this article, we’ll introduce the Jarque-Bera test, describe the formula used to calculate it, and show how to use Python to conduct the test.

We’ll also provide an example of how the test is used to check for normality.

## Explanation of Jarque-Bera Test

The Jarque-Bera test assesses if a dataset follows a normal distribution by measuring the skewness and kurtosis of the data. Skewness measures the extent to which the data is skewed, or lopsided, while kurtosis measures the degree of peakedness of the data.

The null hypothesis of the test is that the data is normally distributed. The test calculates a test statistic J, which is a function of the sample skewness and kurtosis.

This test statistic follows the chi-square distribution with two degrees of freedom under the null hypothesis of normality. The chi-square distribution is a probability distribution that describes the sum of the squares of independent standard normal random variables.

The test statistic is then compared to a critical value of the chi-square distribution at a given significance level (usually 0.05), and a p-value is calculated. If the p-value is less than the significance level, we reject the null hypothesis and conclude that the data is not normally distributed.

If the p-value is greater than the significance level, we fail to reject the null hypothesis and conclude that the data is normally distributed.

## Syntax for Conducting Jarque-Bera Test in Python

Python provides a built-in function called `jarque_bera` in the Scipy library that can be used to conduct the Jarque-Bera test on an array of observations. The syntax for using the `jarque_bera` function in Python is:

``````from scipy.stats import jarque_bera
# data is an array of observations
test_statistic, p_value = jarque_bera(data)
``````

### Example 1: Jarque-Bera Test on Normal Distribution

Let’s say we have a set of 1000 data points that follow a normal distribution.

We can use Python to generate this data using the `numpy.random.normal` function:

``````import numpy as np
# generate 1000 data points with mean 0 and standard deviation 1
data = np.random.normal(0, 1, 1000)
``````

Next, we can use the `jarque_bera` function to conduct the Jarque-Bera test on the data:

``````from scipy.stats import jarque_bera
test_statistic, p_value = jarque_bera(data)
print(f"Test statistic: {test_statistic}")
print(f"P-value: {p_value}")
``````

### The output of this code will be:

``````Test statistic: 0.4855559405052446
P-value: 0.7846724822810374
``````

The test statistic is 0.486, and the p-value is 0.785. Since the p-value is greater than the significance level of 0.05, we fail to reject the null hypothesis and conclude that the data is normally distributed with 95% confidence.

In this case, since the data was generated from a normal distribution, we expect the Jarque-Bera test to show that the data is normally distributed. The test provides evidence for this.

## Conclusion

The Jarque-Bera test is an important statistical method used to test for normality. Understanding how to conduct this test in Python is crucial for data analysis tasks.

In this article, we explained the formula used to calculate the test statistic and provided the syntax for conducting the test in Python. We also provided an example of how the test is used to check for normality and demonstrated the interpretation of the results obtained from the test.

Knowing when to use the Jarque-Bera test can help you to make informed decisions in data analysis tasks and ensure that the statistical techniques used are appropriate for the data under analysis.

### Example 2: Jarque-Bera Test on Uniform Distribution

In addition to testing normal distributions, the Jarque-Bera test can also be used to test non-normal distributions.

Let’s consider a set of 1000 data points that follow a uniform distribution between 0 and 1. We can use Python to generate this data using the `numpy.random.uniform` function:

``````import numpy as np
# generate 1000 data points between 0 and 1
data = np.random.uniform(0, 1, 1000)
``````

Next, we can use the `jarque_bera` function to conduct the Jarque-Bera test on the data:

``````from scipy.stats import jarque_bera
test_statistic, p_value = jarque_bera(data)
print(f"Test statistic: {test_statistic}")
print(f"P-value: {p_value}")
``````

### The output of this code will be:

``````Test statistic: 225.25730502100766
P-value: 0.0
``````

The test statistic is 225.257, and the p-value is 0.0. Since the p-value is less than the significance level of 0.05, we reject the null hypothesis and conclude that the data is not normally distributed. This is expected since a uniform distribution is not a normal distribution.

## Use Cases for the Jarque-Bera Test

While the Jarque-Bera test is a useful tool for testing normality, there are a few considerations to keep in mind when using it. One such consideration is the size of the dataset.

The test tends to be more reliable when the number of observations is large, typically n > 2000. For smaller sample sizes, other normality tests, such as the Shapiro-Wilk test, may be more reliable.

Another consideration is that while the Jarque-Bera test is useful for detecting non-normality, it can provide unreliable results in certain situations. For example, the test may show non-normality in datasets with heavy tails (i.e., distributions that fall off slowly) or skewness that is close to zero.

In these cases, it may be more appropriate to use other tests that are able to detect deviations from normality in specific ways. Despite these limitations, the Jarque-Bera test remains one of the most commonly used tests for normality in data analysis.

It is a relatively simple and quick test to perform, and it provides valuable information about the distribution of the data being analyzed.

## Conclusion

The Jarque-Bera test is a powerful tool for testing normality in datasets. In this article, we have provided an explanation of the Jarque-Bera test and covered the formula used to calculate the test statistic, as well as the syntax for conducting the test in Python.

We have also provided two examples of how the test is used to check for normality on datasets generated from a normal distribution and a uniform distribution. Finally, we have outlined some of the considerations to keep in mind when using the test, such as sample size and the reliability of results in specific situations.

By understanding the Jarque-Bera test and its various applications, data analysts can make informed decisions about the statistical techniques they use for their data analysis tasks. In summary, the Jarque-Bera test is a statistical method used to test for normality in datasets.

It assesses the skewness and kurtosis of the data to determine if it follows a normal distribution. Python provides the `jarque_bera` function to conduct the test, and the test is useful not only for testing normal distributions but also for detecting deviations from normality in other distributions.

However, the reliability of the test depends on the sample size and other considerations. By understanding how to use the Jarque-Bera test effectively, data analysts can make informed decisions about the techniques they use for their data analysis tasks.

Ultimately, the Jarque-Bera test is a powerful tool that can help ensure accurate and reliable statistical analysis.