Skip to main content
`Ctrl`+`K`

# scipy.stats.ansari

scipy.stats.ansari(x, y, alternative='two-sided', *, axis=0, nan_policy='propagate', keepdims=False)

  Perform the Ansari-Bradley test for equal scale parameters.

  The Ansari-Bradley test ([1], [2]) is a non-parametric test for the equality of the scale parameter of the distributions from which two samples were drawn. The null hypothesis states that the ratio of the scale of the distribution underlying x to the scale of the distribution underlying y is 1.

  Parameters:
    x, y array_like

      Arrays of sample data.

    alternative{‘two-sided’, ‘less’, ‘greater’}, optional

      Defines the alternative hypothesis. Default is ‘two-sided’. The following options are available:

      - ‘two-sided’: the ratio of scales is not equal to 1.

      - ‘less’: the ratio of scales is less than 1.

      - ‘greater’: the ratio of scales is greater than 1.

      New in version 1.7.0.

    axis int or None, default: 0

      If an int, the axis of the input along which to compute the statistic. The statistic of each axis-slice (e.g. row) of the input will appear in a corresponding element of the output. If `None`, the input will be raveled before computing the statistic.

    nan_policy{‘propagate’, ‘omit’, ‘raise’}

      Defines how to handle input NaNs.

      - `propagate`: if a NaN is present in the axis slice (e.g. row) along which the statistic is computed, the corresponding entry of the output will be NaN.

      - `omit`: NaNs will be omitted when performing the calculation. If insufficient data remains in the axis slice along which the statistic is computed, the corresponding entry of the output will be NaN.

      - `raise`: if a NaN is present, a `ValueError` will be raised.

    keepdims bool, default: False

      If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.

  Returns:
    statistic float

      The Ansari-Bradley test statistic.

    pvalue float

      The p-value of the hypothesis test.

  See also

  `fligner`

    A non-parametric test for the equality of k variances

  `mood`

    A non-parametric test for the equality of two scale parameters

  Notes

  The p-value given is exact when the sample sizes are both less than 55 and there are no ties, otherwise a normal approximation for the p-value is used.

  Beginning in SciPy 1.9, `np.matrix` inputs (not recommended for new code) are converted to `np.ndarray` before the calculation is performed. In this case, the output will be a scalar or `np.ndarray` of appropriate shape rather than a 2D `np.matrix`. Similarly, while masked elements of masked arrays are ignored, the output will be a scalar or `np.ndarray` rather than a masked array with `mask=False`.

  References

  [1]

  Ansari, A. R. and Bradley, R. A. (1960) Rank-sum tests for dispersions, Annals of Mathematical Statistics, 31, 1174-1189.

  [2]

  Sprent, Peter and N.C. Smeeton. Applied nonparametric statistical methods. 3rd ed. Chapman and Hall/CRC. 2001. Section 5.8.2.

  [3]

  Nathaniel E. Helwig “Nonparametric Dispersion and Equality Tests” at http://users.stat.umn.edu/~helwig/notes/npde-Notes.pdf

  Examples

      >>> import numpy as np
      >>> from scipy.stats import ansari
      >>> rng = np.random.default_rng()

  For these examples, we’ll create three random data sets. The first two, with sizes 35 and 25, are drawn from a normal distribution with mean 0 and standard deviation 2. The third data set has size 25 and is drawn from a normal distribution with standard deviation 1.25.

      >>> x1 = rng.normal(loc=0, scale=2, size=35)
      >>> x2 = rng.normal(loc=0, scale=2, size=25)
      >>> x3 = rng.normal(loc=0, scale=1.25, size=25)

  First we apply `ansari` to x1 and x2. These samples are drawn from the same distribution, so we expect the Ansari-Bradley test should not lead us to conclude that the scales of the distributions are different.

      >>> ansari(x1, x2)
      AnsariResult(statistic=541.0, pvalue=0.9762532927399098)

  With a p-value close to 1, we cannot conclude that there is a significant difference in the scales (as expected).

  Now apply the test to x1 and x3:

      >>> ansari(x1, x3)
      AnsariResult(statistic=425.0, pvalue=0.0003087020407974518)

  The probability of observing such an extreme value of the statistic under the null hypothesis of equal scales is only 0.03087%. We take this as evidence against the null hypothesis in favor of the alternative: the scales of the distributions from which the samples were drawn are not equal.

  We can use the alternative parameter to perform a one-tailed test. In the above example, the scale of x1 is greater than x3 and so the ratio of scales of x1 and x3 is greater than 1. This means that the p-value when `alternative='greater'` should be near 0 and hence we should be able to reject the null hypothesis:

      >>> ansari(x1, x3, alternative='greater')
      AnsariResult(statistic=425.0, pvalue=0.0001543510203987259)

  As we can see, the p-value is indeed quite low. Use of `alternative='less'` should thus yield a large p-value:

      >>> ansari(x1, x3, alternative='less')
      AnsariResult(statistic=425.0, pvalue=0.9998643258449039)