Test whether a sample differs from a normal distribution.
This function tests the null hypothesis that a sample comes from a normal distribution. It is based on D’Agostino and Pearson’s [1], [2] test that combines skew and kurtosis to produce an omnibus test of normality.
Parametersaarray_likeThe array containing the sample to be tested.
axisint or None, optionalAxis along which to compute test. Default is 0. If None, compute over the whole array a.
nan_policy{‘propagate’, ‘raise’, ‘omit’}, optionalDefines how to handle when input contains nan. The following options are available [default is ‘propagate’]:
Returnsstatisticfloat or array
‘propagate’: returns nan
‘raise’: throws an error
‘omit’: performs the calculations ignoring nan values
s^2 + k^2
, where s
is the z-score returned by
skewtest
and k
is the z-score returned by kurtosistest
.
A 2-sided chi squared probability for the hypothesis test.
References
1D’Agostino, R. B. [1971], “An omnibus test of normality for moderate and large sample size”, Biometrika, 58, 341-348
2D’Agostino, R. and Pearson, E. S. [1973], “Tests for departure from normality”, Biometrika, 60, 613-622
Examples
>>> from scipy import stats >>> rng = np.random.default_rng[] >>> pts = 1000 >>> a = rng.normal[0, 1, size=pts] >>> b = rng.normal[2, 1, size=pts] >>> x = np.concatenate[[a, b]] >>> k2, p = stats.normaltest[x] >>> alpha = 1e-3 >>> print["p = {:g}".format[p]] p = 8.4713e-19 >>> if p >> from scipy.stats import chisquare >>> chisquare[[16, 18, 16, 14, 12, 12]] [2.0, 0.84914503608460956]
With f_exp the expected frequencies can be given.
>>> chisquare[[16, 18, 16, 14, 12, 12], f_exp=[16, 16, 16, 16, 16, 8]] [3.5, 0.62338762774958223]
When f_obs is 2-D, by default the test is applied to each column.
>>> obs = np.array[[[16, 18, 16, 14, 12, 12], [32, 24, 16, 28, 20, 24]]].T >>> obs.shape [6, 2] >>> chisquare[obs] [array[[ 2. , 6.66666667]], array[[ 0.84914504, 0.24663415]]]
By setting axis=None
, the test is applied to all data in the array, which is equivalent to applying the test to the flattened array.
>>> chisquare[obs, axis=None] [23.31034482758621, 0.015975692534127565] >>> chisquare[obs.ravel[]] [23.31034482758621, 0.015975692534127565]
ddof is the change to make to the default degrees of freedom.
>>> chisquare[[16, 18, 16, 14, 12, 12], ddof=1] [2.0, 0.73575888234288467]
The calculation of the p-values is done by broadcasting the chi-squared statistic with ddof.
>>> chisquare[[16, 18, 16, 14, 12, 12], ddof=[0,1,2]] [2.0, array[[ 0.84914504, 0.73575888, 0.5724067 ]]]
f_obs and f_exp are also broadcast. In the following, f_obs has shape [6,] and f_exp has shape [2, 6], so the result of broadcasting f_obs and f_exp has shape [2, 6]. To compute the desired chi-squared statistics, we use axis=1
:
>>> chisquare[[16, 18, 16, 14, 12, 12], ... f_exp=[[16, 16, 16, 16, 16, 8], [8, 20, 20, 16, 12, 12]], ... axis=1] [array[[ 3.5 , 9.25]], array[[ 0.62338763, 0.09949846]]]