Home >Backend Development >Python Tutorial >Python tests whether Jarque-Bera conforms to normal distribution
The content of this article is to test whether Jarque-Bera conforms to the normal distribution in python. It has a certain reference value. Friends in need can refer to it
Normal distribution is a normality test of a population distribution. When the sequence obeys the normal distribution, the JB statistic:
##gradually obeys distributed. Where n is the sample size, S and K are the skewness and kurtosis of the random variable respectively. Calculated as follows:
#The functions called for skewness and kurtosis in python's sicipy.stats are
stats.skew(y)
, stats.kurtosis(y)
, where the formula of kurtosis is In excel, the calculation formulas for skewness and kurtosis are as follows :
Code
# Next, implement the formulas for calculating skewness and skew in Python's scipy library and establish a normal distribution test.import numpy as npimport scipy.stats as statsdef self_JBtest(y): # 样本规模n n = y.size y_ = y - y.mean() """ M2:二阶中心钜 skew 偏度 = 三阶中心矩 与 M2^1.5的比 krut 峰值 = 四阶中心钜 与 M2^2 的比 """ M2 = np.mean(y_**2) skew = np.mean(y_**3)/M2**1.5 krut = np.mean(y_**4)/M2**2 """ 计算JB统计量,以及建立假设检验 """ JB = n*(skew**2/6 + (krut-3 )**2/24) pvalue = 1 - stats.chi2.cdf(JB,df=2) print("偏度:",stats.skew(y),skew) print("峰值:",stats.kurtosis(y)+3,krut) print("JB检验:",stats.jarque_bera(y)) return np.array([JB,pvalue]) y1 = stats.norm.rvs(size=10) y2 = stats.t.rvs(size=1000,df=4) print(self_JBtest(y1)) print(self_JBtest(y2))Result=============== RESTART: C:\Users\tinysoft\Desktop\JB正态性检验.py =============== 偏度: 0.5383125387398069 0.53831253874 峰值: 2.9948926317585918 2.99489263176 JB检验: (0.48297818444514068, 0.78545737133644544) [ 0.48297818 0.78545737] 偏度: -1.0488825341925703 -1.04888253419 峰值: 13.40804986639119 13.4080498664 JB检验: (4697.0050126426095, 0.0) [ 4697.00501264 0. ]
The above is the detailed content of Python tests whether Jarque-Bera conforms to normal distribution. For more information, please follow other related articles on the PHP Chinese website!