Skip to content

DPredict · Guide test 208

Wald–Wolfowitz two sample runs test

Tests equality of two distributions through runs of sample labels in their pooled ordering.

Runs in CodeSplashPython example validatedR example validated Corrected in edition 2

Do it in CodeSplash

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

In Compare distributions (two or more groups), choose Test = Wald–Wolfowitz runs.

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 · Wald–Wolfowitz two sample runs test

20
20
0.90
1.0
1.0
0.050

Alternative

Your data

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

If H₀ were true

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

The formula

z=R−(1+2mnN)2mn(2mn−N)N2(N−1)z = \dfrac{R-\left(1+\frac{2mn}{N}\right)}{\sqrt{\frac{2mn(2mn-N)}{N^2(N-1)}}}

With your numbers

R=20,z=(−0.32)R = 20,\quad z = (-0.32)
RR
number of runs of same-sample labels
m, nm,\ n
the two sample sizes

The verdict

R = 20.00, z = −0.32, p = .374

p > α = .050: compatible with H₀

Few runs mean the samples separate somewhere. It detects any difference but has low power; the KS test is usually better. The CodeSplash method always tests the lower tail (too few runs), with a continuity correction in its normal approximation; inside a tie block that mixes the samples it counts the most runs possible, the conservative choice.

Statistic
−0.320
p-value
0.3743
Wald–Wolfowitz two sample runs test on Group 1: n = 20; Group 2: n = 20: R = 20.00, z = −0.32, p = .374. The p-value is above α = .050, so the data are compatible with H0.

Wald–Wolfowitz two sample runs test on Group 1: n = 20; Group 2: n = 20: R = 20.00, z = −0.32, p = .374. The p-value is above α = .050, so the data are compatible with H₀.

Question and hypotheses

H0: two independent samples have identical continuous distributions.

Assumptions

Independent groups; pooled observations sortable, with a specified tie policy.

Formula

R=1+∑i=2NI(g(i)≠g(i−1))R=1+\sum_{i=2}^{N}I(g_{(i)}\ne g_{(i-1)})

Parameters and notation

Sort pooled values; g(i) is the sample label. Run count moments are those in entry 207 with the two group sizes.

Structure of the method

Pool two independent samples, sort their observed values, and count runs of sample labels along the sorted axis. Under equal continuous distributions, labels are exchangeable conditional on group sizes. This is a two-sample intermixing diagnostic, distinct from a time-order runs test and from similarly named serial-product statistics.

Estimated and nuisance parameters

Group sizes and the pooled label-run count determine the classical conditional moments. No location/scale parameters are fitted. Tied pooled values introduce ambiguous label orders, requiring a declared convention or a tie-aware randomization procedure rather than arbitrary sorting behavior.

Settings and design choices

Specify the classical lower-tail alternative or a two-sided randomness version, exact versus normal reference, continuity correction, and tie policy. Both original displayed examples use a two-sided approximation; their p-values should not be described as the classical lower-tail p-value without conversion and a justified convention.

Fitting and adaptation

No model training is required. Recompute the pooled ordering for new data and calibrate by exchangeable group-label assignments if using permutation. If features or thresholds were selected to separate groups, perform that selection independently or repeat it within testing; an optimized projection invalidates a naive fixed-variable p-value.

Sensitivity and robustness

Small or highly unequal samples reduce approximation accuracy. Ties can change the run count substantially, and different tie rules need not agree across software. The statsmodels documentation describes its two-sided implementation and tie caveats; this should be distinguished from a universally validated exact test. Rejection need not be a pure mean shift.

How to use and calibrate

Use the conditional runs distribution. Unusually few runs indicate separation/clustering of labels and reject equality.

Example application

Compare two distributions using intermixing of their ordered observations.

Interpretation and pitfalls

Watch out

This two sample Wald–Wolfowitz runs test differs from the serial-product Wald–Wolfowitz statistic also found in software.

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.
  • Rewritten in the review: how to use and calibrate.
  • How to use and calibratestatsmodels.org

    The prose emphasized a lower-tail two-sample test, but the supplied Python function and explicit R formula return two-sided p-values. This clarification preserves the existing working examples while accurately identifying their variant.

    Corrected text

    Use the conditional runs distribution or a justified normal approximation with the chosen tail and tie policy stated. The classical equality-of-distributions formulation rejects for unusually few runs. Both displayed code examples instead report a two-sided normal-approximation runs p-value; statsmodels documents its current implementation as two-sided only. Those p-values must not be labeled as the classical lower-tail p-value.

Python and R

Runs of group labels after sorting pooled observations; ties require a specified convention. R uses stable pooled ordering; tied samples can therefore differ from other implementations.

Python · native

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

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])
x=rng.normal(size=20);y=rng.normal(.3,1.2,25)
from statsmodels.sandbox.stats.runs import runstest_2samp
print(runstest_2samp(x,y,correction=False))
Output
(np.float64(-0.37332523052787486), np.float64(0.7089064003969938))
R

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

set.seed(2026)
x<-rnorm(20);y<-rnorm(25,.3,1.2)
o<-order(c(x,y));g<-c(rep(0,length(x)),rep(1,length(y)))[o];runs<-1+sum(diff(g)!=0);n<-length(x);m<-length(y);N<-n+m;mu<-1+2*n*m/N;v<-2*n*m*(2*n*m-N)/(N^2*(N-1));z<-(runs-mu)/sqrt(v);print(c(runs=runs,z=z,p=2*pnorm(-abs(z))))
Output
      runs          z          p 
20.0000000 -0.9842211  0.3250068

Sources

Statistics guide v4 (historical):
Python: Executed successfully in the validation environment · R: Executed successfully in the validation environment
Reference's record:
Edition 2 review: Python: skip · R: not executed (Rscript unavailable); why not run: Python: Unavailable local dependencies: statsmodels. Python: Not run in the reference's review: Unavailable local dependencies: statsmodels. No installation attempted. R: Reviewed but not run: R wasn't available to the reference's authors.

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