Skip to content

ADescribe · Guide test 121

Cramér–von Mises two sample test

Compares two independent distributions using integrated squared differences between empirical cumulative functions.

Runs in CodeSplashPython example validatedR example validated

Do it in CodeSplash

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

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

25
25
0.50
1.0
1.6
0.050

Your data

Drag a point or use ↑/↓ to change it
Group 1 · n = 25 · mean 0.70Group 2 · n = 25 · mean 0.02

If H₀ were true

T = 0.44
  1. The reference: how T varies across samples when H₀ is true, Anderson's limit law (moment-matched) (asymptotic).
  2. Your statistic, from the data above.
  3. The p-value: the area at least as extreme as yours (p = .058).
  4. The hatched rejection region at α = .050: statistics landing here reject H₀.

The formula

T=m∑i(ri−i)2+n∑j(sj−j)2mnN−4mn−16NT=\dfrac{m\sum_i(r_i-i)^2+n\sum_j(s_j-j)^2}{mnN}-\dfrac{4mn-1}{6N}

With your numbers

T=0.44,m=25, n=25T = 0.44,\qquad m = 25,\ n = 25
ri, sjr_i,\ s_j
pooled ranks of each sample, sorted
NN
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 H0.

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

T=mnm+n∫[Fm(x)−Gn(x)]2 dHm+n(x)T=\frac{mn}{m+n}\int[F_m(x)-G_n(x)]^2\,dH_{m+n}(x)

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.

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.cramervonmises_2samp(x,y,method="auto"))
Output
CramerVonMisesResult(statistic=0.5349999999999997, pvalue=0.03446708090670939)
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)
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.041

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 121, updated from the reference's edition 2 (reviewed 14 November 2026) as STAT121.