ADescribe · Guide test 121
Cramér–von Mises two sample test
Compares two independent distributions using integrated squared differences between empirical cumulative functions.
Do it in CodeSplash
In the app, set it up in Analyze with this method:
- Compare distributions (two or more groups) — Do the groups come from the same distribution — anywhere, not only in their centers?
In Compare distributions (two or more groups), choose Test = Cramér–von Mises.
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 · Cramér–von Mises two sample test
Your data
Drag a point or use ↑/↓ to change itIf H₀ were true
- The reference: how T varies across samples when H₀ is true, Anderson's limit law (moment-matched) (asymptotic).
- Your statistic, from the data above.
- The p-value: the area at least as extreme as yours (p = .058).
- The hatched rejection region at α = .050: statistics landing here reject H₀.
The formula
With your numbers
- pooled ranks of each sample, sorted
- m + n
The verdict
T = 0.44, p = .058
p > α = .050: compatible with H₀
Anderson's two-sample CvM integrates the squared gap between the two ECDFs, so it is more sensitive than KS to differences spread across the range.
- Statistic
- 0.437
- p-value
- 0.0579
Cramér–von Mises two sample test on Group 1: n = 25; Group 2: n = 25: T = 0.44, p = .058. The p-value is above α = .050, so the data are compatible with H₀.
Question and hypotheses
H0: two independent samples share a distribution.
Assumptions
Independent continuous samples, or a tie-aware permutation procedure.
Formula
Parameters and notation
H is the pooled empirical CDF; discrete rank implementations include finite-sample conventions.
Structure of the method
Compare the two empirical CDFs throughout the pooled sample and aggregate squared discrepancies. A pooled-rank expression provides an efficient implementation; exact finite-sample calculations and asymptotics depend on both sample sizes and tie conventions.
Estimated and nuisance parameters
Each empirical distribution and the pooled rank structure are computed from data. There are no parametric nuisance means or scales to fit, and the tested null concerns entire distributions rather than one moment.
Settings and design choices
Prespecify exact, asymptotic or permutation calibration and the ranking convention. Keep group sizes fixed in randomization. Select the resampling count and any follow-up testing plan before inspecting which method gives the smallest p-value.
Fitting and adaptation
For independent exchangeable groups, calibrate by pooled-label permutations using the same statistic and tie handling. A new sample pair is analyzed afresh; there is no transfer-learning stage. Clustered or paired designs need restricted permutations or another model. Document whether reported results come from the analytic SciPy reference or the R permutation illustration.
Sensitivity and robustness
Rounded values can make continuous-reference p-values conservative or otherwise differ from conditional permutation results. Very small samples yield coarse p-values. Rejection may arise from spread or shape changes even when means agree; inspect ECDFs and effect summaries to explain the distributional difference.
How to use and calibrate
Use the two sample CvM distribution or label permutation; large integrated discrepancy rejects.
Example application
Compare two distributions when differences may be spread across the whole range.
Interpretation and pitfalls
Watch out
Known-CDF one sample tables do not apply. State tie handling and exact/asymptotic method.
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
R demonstrates permutation calibration of the rank statistic; SciPy selects exact or asymptotic calibration. Statistics should agree; p-values need not be identical.
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.cramervonmises_2samp(x,y,method="auto"))
Output
CramerVonMisesResult(statistic=0.5349999999999997, pvalue=0.03446708090670939)
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)
cvm<-function(a,b) {n<-length(a);m<-length(b);r<-rank(c(a,b));U<-n*sum((sort(r[seq_len(n)])-seq_len(n))^2)+m*sum((sort(r[n+seq_len(m)])-seq_len(m))^2);U/(n*m*(n+m))-(4*n*m-1)/(6*(n+m))}
T<-cvm(x,y);pool<-c(x,y);n<-length(x);set.seed(2026);B<-999
null<-replicate(B,{z<-sample(pool);cvm(z[seq_len(n)],z[-seq_len(n)])})
print(c(statistic=T,p=(1+sum(null>=T))/(B+1)))
Output
statistic p
0.535 0.041Sources
- SciPy — SciPy · cramervonmises_2sampdocs.scipy.org
- scipy.stats.cramervonmises_2sampdocs.scipy.org
- 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 121, updated from the reference's edition 2 (reviewed 14 November 2026) as STAT121.