I will explain how to perform the Jarque-Bera test in Python.
The Jarque-Bera test is a technique for determining whether a sample follows a normal distribution.
The test statistic JB is as shown in the formula below. [by Wikipedia](https://ja.wikipedia.org/wiki/%E3%82%B8%E3%83%A3%E3%83%83%E3%82%AF%E2%80%93%E3% 83% 99% E3% 83% A9% E6% A4% 9C% E5% AE% 9A #: ~: text =% E3% 82% B8% E3% 83% A3% E3% 83% 83% E3% 82% AF% EF% BC% 9D% E3% 83% 99% E3% 83% A9% E6% A4% 9C% E5% AE% 9A% EF% BC% 88% E3% 82% B8% E3% 83% A3% E3% 83% 83% E3% 82% AF,% E9% 81% A9% E5% 90% 88% E5% BA% A6% E6% A4% 9C% E5% AE% 9A% E3% 81% A7% E3 % 81% 82% E3% 82% 8B% E3% 80% 82)
The test statistic JB follows a chi-square distribution. Perform the temporary test described below with the test statistic JB. (Significance level p%)
H0 (null hypothesis): The sample follows a normal distribution. H1 (opposite hypothesis): The sample does not follow a normal distribution.In Python, stats.jarque_bera (x) can be used to calculate the test statistic JB for sample x. The return value of stats.jarque_bera is [JB calculated value, p value].
If the significance level is set to p = 0.05 and the return value p-value of stats.jarque_bera is> 0.05, the null hypothesis is adopted and the sample can be judged to follow a normal distribution. Conversely, if the calculated p-value <= 0.05, the sample is judged not to follow a normal distribution.
A sample is prepared by generating 10000 random numbers that follow a normal distribution with an average of 0 and a variance of 1. Perform the Jarque-Bera test on the sample to test whether the sample follows a normal distribution.
sample.py
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
#Generate random numbers that follow a normal distribution.
x = np.random.normal(0, 1, 10000)
#Draw a histogram of the sample.
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.hist(x, bins=50)
ax.set_title('first histogram')
ax.set_xlabel('x')
ax.set_ylabel('freq')
fig.show()
#Perform the Jarque-Bera test.
jarque_bera_test = stats.jarque_bera(x)
print('Jack Bera:', '\t', jarque_bera_test)
(Execution result) As a result of the Jarque-Bera test, p-value> 0.05 is calculated, so the null hypothesis that the sample follows a normal distribution can be adopted.
test.txt
Jack Bera: (JB=0.3352314362400006, p-value=0.8456787512712962)
A sample is prepared by generating 10000 random numbers that follow a χ-square distribution with 1 degree of freedom. Perform the Jarque-Bera test on the sample to test whether the sample follows a normal distribution.
sample.py
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
#Generate random numbers that follow a normal distribution.
x = np.random.normal(0, 1, 10000)
kai=x*x #kai follows a chi-square distribution
#Draw a histogram of the sample.
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.hist(kai, bins=50)
ax.set_title('first histogram')
ax.set_xlabel('kai')
ax.set_ylabel('freq')
fig.show()
#Perform the Jarque-Bera test.
jarque_bera_test = stats.jarque_bera(kai)
print('Jack Bera:', '\t', jarque_bera_test)
(Execution result) As a result of the Jarque-Bera test, p-value <0.05 is calculated, so the null hypothesis is rejected and the alternative hypothesis that the sample does not follow the normal distribution is adopted.
test.txt
Jack Bera: (JB=89946.17232671285, p-value=0.0)