scipy.stats.anderson_ksamp#
- scipy.stats.anderson_ksamp(samples, midrank=True, *, n_resamples=0, random_state=None)[source]#
The Anderson-Darling test for k-samples.
The k-sample Anderson-Darling test is a modification of the one-sample Anderson-Darling test. It tests the null hypothesis that k-samples are drawn from the same population without having to specify the distribution function of that population. The critical values depend on the number of samples.
- Parameters:
- samplessequence of 1-D array_like
Array of sample data in arrays.
- midrankbool, optional
Type of Anderson-Darling test which is computed. Default (True) is the midrank test applicable to continuous and discrete populations. If False, the right side empirical distribution is used.
- n_resamplesint, default: 0
If positive, perform a permutation test to determine the p-value rather than interpolating between tabulated values. Typically 9999 is sufficient for at least two digits of accuracy.
- random_state{None, int,
numpy.random.Generator
,numpy.random.RandomState
}, optional Pseudorandom number generator state used to generate resamples.
If random_state is
None
(or np.random), thenumpy.random.RandomState
singleton is used. If random_state is an int, a newRandomState
instance is used, seeded with random_state. If random_state is already aGenerator
orRandomState
instance then that instance is used.
- Returns:
- resAnderson_ksampResult
An object containing attributes:
- statisticfloat
Normalized k-sample Anderson-Darling test statistic.
- critical_valuesarray
The critical values for significance levels 25%, 10%, 5%, 2.5%, 1%, 0.5%, 0.1%.
- pvaluefloat
The approximate p-value of the test. If n_resamples is not provided, the value is floored / capped at 0.1% / 25%.
- Raises:
- ValueError
If less than 2 samples are provided, a sample is empty, or no distinct observations are in the samples.
Notes
[1] defines three versions of the k-sample Anderson-Darling test: one for continuous distributions and two for discrete distributions, in which ties between samples may occur. The default of this routine is to compute the version based on the midrank empirical distribution function. This test is applicable to continuous and discrete data. If midrank is set to False, the right side empirical distribution is used for a test for discrete data. According to [1], the two discrete test statistics differ only slightly if a few collisions due to round-off errors occur in the test not adjusted for ties between samples.
The critical values corresponding to the significance levels from 0.01 to 0.25 are taken from [1]. p-values are floored / capped at 0.1% / 25%. Since the range of critical values might be extended in future releases, it is recommended not to test
p == 0.25
, but ratherp >= 0.25
(analogously for the lower bound).New in version 0.14.0.
References
Examples
>>> import numpy as np >>> from scipy import stats >>> rng = np.random.default_rng() >>> res = stats.anderson_ksamp([rng.normal(size=50), ... rng.normal(loc=0.5, size=30)]) >>> res.statistic, res.pvalue (1.974403288713695, 0.04991293614572478) >>> res.critical_values array([0.325, 1.226, 1.961, 2.718, 3.752, 4.592, 6.546])
The null hypothesis that the two random samples come from the same distribution can be rejected at the 5% level because the returned test value is greater than the critical value for 5% (1.961) but not at the 2.5% level. The interpolation gives an approximate p-value of 4.99%.
>>> samples = [rng.normal(size=50), rng.normal(size=30), ... rng.normal(size=20)] >>> res = stats.anderson_ksamp(samples) >>> res.statistic, res.pvalue (-0.29103725200789504, 0.25) >>> res.critical_values array([ 0.44925884, 1.3052767 , 1.9434184 , 2.57696569, 3.41634856, 4.07210043, 5.56419101])
The null hypothesis cannot be rejected for three samples from an identical distribution. The reported p-value (25%) has been capped and may not be very accurate (since it corresponds to the value 0.449 whereas the statistic is -0.291).
In such cases where the p-value is capped or when sample sizes are small, a permutation test may be more accurate.
>>> res = stats.anderson_ksamp(samples, n_resamples=9999, random_state=rng) >>> res.pvalue 0.5254