Skip to content

ADescribe · Guide test 096

Ansari–Bradley test

Compares the scale of two independent distributions, assuming suitable alignment of their centers.

Runs in CodeSplashPython example validatedR example validated

Do it in CodeSplash

In the app, set it up in Analyze with this method:

In Equal spread tests, choose Test = Ansari–Bradley.

Do it in CodeSplash Run this test on your own data, in Python or R.

Bench

Change the design, drag the data, move α. The statistic, its reference distribution and the verdict follow.

Bench · Ansari–Bradley test

20
20
0.00
1.0
2.0
0.050

Your data

Drag a point or use ↑/↓ to change it
Group 1 · n = 20 · mean 0.07Group 2 · n = 20 · mean −0.29

If H₀ were true

z = 2.38
  1. The reference: how z varies across samples when H₀ is true (normal approximation).
  2. Your statistic, from the data above.
  3. The p-value: the area at least as extreme as yours (p = .017).
  4. The hatched rejection region at α = .050: statistics landing here reject H₀.

The formula

AB=∑i∈xmin⁡(ri, N−ri+1)AB = \sum_{i\in x}\min(r_i,\,N-r_i+1)

With your numbers

AB=254,z=2.38AB = 254,\quad z = 2.38
rir_i
pooled rank
min⁡(ri,N−ri+1)\min(r_i, N-r_i+1)
scores largest in the middle, smallest at both ends

The verdict

AB = 254.00, z = 2.38, p = .017

p ≤ α = .050: reject H₀

A rank test of dispersion that assumes equal medians; a location difference masquerades as a scale difference.

Statistic
2.383
p-value
0.0172
Ansari–Bradley test on Group 1: n = 20; Group 2: n = 20: AB = 254.00, z = 2.38, p = .017. The p-value is below α = .050, so the data are hard to reconcile with H0.

Ansari–Bradley test on Group 1: n = 20; Group 2: n = 20: AB = 254.00, z = 2.38, p = .017. The p-value is below α = .050, so the data are hard to reconcile with H₀.

Question and hypotheses

H0: the two population scales are equal.

Assumptions

Independent samples from distributions with a common location and comparable shape under a scale model; continuous observations for simple rank calibration. Location shifts can confound scale tests.

Formula

A=∑i∈Xmin⁡(Ri,N+1−Ri)A=\sum_{i\in X}\min(R_i,N+1-R_i)

Parameters and notation

Pooled ranks receive scores 1,2,… inward from both ends. Larger A generally means the first sample is less dispersed.

Structure of the method

Pool the two samples, assign symmetric scores increasing from both extremes toward the center, and sum the first sample’s scores. An exact rank distribution or tie-adjusted normal reference assesses scale differences when the distributions share a suitable common center and shape.

Estimated and nuisance parameters

Observed ranks, symmetric scores, and group sizes define the statistic. The common location is a nuisance assumption, not automatically removed by the raw rank calculation. Larger first-sample score sums usually indicate greater concentration near the center, hence smaller rather than larger scale.

Settings and design choices

Prespecify scale direction, tie handling, exact versus asymptotic method, and any location-alignment strategy. Aligning sample medians by estimation changes finite-sample calibration and must not be portrayed as the original exact rank test without justification. Software alternative labels refer to scale, not raw statistic size.

Fitting and adaptation

New observations require re-pooling and re-ranking. Exact or conditional permutation calculations keep sample sizes and the permitted score allocation fixed. If centers are estimated, a valid resampling analysis must address that additional step. No neural fitting or fine-tuning is involved.

Sensitivity and robustness

A location shift can masquerade as a scale difference. Heavy ties, extreme imbalance, or very small samples reduce resolution and may disable an exact software route. Check group centers and shapes and interpret the estimated scale ratio where available rather than relying only on the p-value.

How to use and calibrate

Use exact rank enumeration or a tie corrected normal approximation.

Example application

Compare dispersion between two independent measurement processes.

Interpretation and pitfalls

Watch out

Do not interpret unequal locations as evidence of a scale difference. Estimated recentering changes finite sample calibration.

What changed in edition 2

  • New sections: structure of the method, estimated and nuisance parameters, settings and design choices, fitting and adaptation, sensitivity and robustness.

Python and R

Scale interpretation requires a common location; an unmodeled location difference can affect rank scale tests.

Python · native

Validated — Executed successfully in the validation environment · needs numpy, scipy

import numpy as np
from scipy import stats
rng = np.random.default_rng(2026)
x = np.array([4.2,5.1,5.4,4.8,6.0,5.8,4.9,5.6,6.2,5.0])
y = np.array([4.0,4.4,4.8,5.0,5.1,4.3,4.7,4.6,5.2,4.9])
print(stats.ansari(x,y))
Output
AnsariResult(statistic=np.float64(51.0), pvalue=np.float64(0.5413702095537719))
R

Validated — Executed successfully in the validation environment · needs Base/standard library

x <- c(4.2,5.1,5.4,4.8,6.0,5.8,4.9,5.6,6.2,5.0)
y <- c(4.0,4.4,4.8,5.0,5.1,4.3,4.7,4.6,5.2,4.9)
print(ansari.test(x,y,exact=FALSE))
Output
	Ansari-Bradley test

data:  x and y
AB = 51, p-value = 0.5414
alternative hypothesis: true ratio of scales is not equal to 1

Sources

Statistics guide v4 (historical):
R: Executed successfully in the validation environment
Reference's record:
Edition 2 review: Python: executed · R: not executed (Rscript unavailable). Python: Ran in the reference's review, on this exact code. R: Reviewed but not run: R wasn't available to the reference's authors.

From the CodeSplash statistics guide (v4), entry 096, updated from the reference's edition 2 (reviewed 14 November 2026) as STAT096.