Skip to content

DPredict · Guide test 207

Wald–Wolfowitz one sample runs test

Tests randomness in the ordering of two types of observations.

Runs in CodeSplashPython example validatedR example validated

Do it in CodeSplash

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

  • Randomness and serial-dependence tests — Is the series (or a model's residuals) just random noise, or does it carry runs, trends, autocorrelation, volatility clustering or nonlinear structure?

In Randomness and serial-dependence tests, choose Test = Runs above / below the median.

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 one sample runs test

40
0.040
0.40

Each error carries φ × the previous one; 1 is a random walk.

0.0
0.050

Alternative

Your data

Simulated with a known ρ

If H₀ were true

z = −1.60
  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 = .109).
  4. The hatched rejection region at α = .050: statistics landing here reject H₀.

The formula

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

With your numbers

R=16,n1=20, n2=20,z=(−1.60)R = 16,\quad n_1 = 20,\ n_2 = 20,\quad z = (-1.60)
RR
number of runs above/below the median
n1, n2n_1,\ n_2
values above and below

The verdict

R = 16.00, z = −1.60, p = .109

p > α = .050: compatible with H₀

Too few runs: trend or positive autocorrelation; too many: alternation. It only uses signs, so it is weak but assumption-free.

Statistic
−1.602
p-value
0.1092
Wald–Wolfowitz one sample runs test on 40 (x, y) pairs: R = 16.00, z = −1.60, p = .109. The p-value is above α = .050, so the data are compatible with H0.

Wald–Wolfowitz one sample runs test on 40 (x, y) pairs: R = 16.00, z = −1.60, p = .109. The p-value is above α = .050, so the data are compatible with H₀.

Question and hypotheses

H0: the order of two symbols/signs is random conditional on their totals.

Assumptions

Independent/exchangeable binary sequence under H0; a specified dichotomization threshold if continuous values are used.

Formula

Z=R−[1+2n1n2/N]2n1n2(2n1n2−N)/[N2(N−1)]Z=\frac{R-[1+2n_1n_2/N]}{\sqrt{2n_1n_2(2n_1n_2-N)/[N^2(N-1)]}}

Parameters and notation

R is the number of consecutive runs, n1,n2 symbol totals,N=n1+n2.

Structure of the method

Convert an ordered sequence into two symbols, count consecutive runs, and compare the observed count with its distribution conditional on the two symbol totals. Exact enumeration or a normal approximation can be used. The observations retain their original temporal/order information; sorting the measurements would destroy the intended test.

Estimated and nuisance parameters

Symbol totals and run count are observed summaries; the conditional test need not estimate an unconditional success probability. If a median or mean threshold is estimated from the data, that threshold becomes a preprocessing quantity and its tie behavior must match the selected procedure.

Settings and design choices

Specify threshold, treatment of equality/ties, one- versus two-sided alternative, exact versus asymptotic calculation, and continuity correction. The displayed fixed cutoff zero avoids estimating a threshold, but software defaults may instead use a mean or median. Degenerate all-one-symbol sequences have no ordinary variance-based test.

Fitting and adaptation

There is no regression or neural training step. Recompute symbols and runs when the sample or threshold rule changes. For a permutation calibration, shuffle exchangeable observations/symbols while preserving counts; do not use unrestricted permutations when the null permits design dependence. Repeatedly choosing thresholds based on significance requires adjustment.

Sensitivity and robustness

Short sequences or highly unequal symbol counts make normal approximations poor. Many threshold ties and missing intervals alter the effective sequence. Too few runs indicate clustering and too many alternation, but other dependencies can escape detection. A passing runs test does not establish independence, stationarity, or randomness against every alternative.

How to use and calibrate

Use exact conditional run probabilities for small counts or a normal approximation with stated continuity correction.

Example application

Check whether above/below-target measurements appear in an unexpectedly clustered sequence.

Interpretation and pitfalls

Watch out

A runs test detects particular ordering departures, not every form of dependence; median ties need explicit treatment.

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

Binary runs above/below the prespecified threshold zero; observations must be in their meaningful sequence order.

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)
from statsmodels.sandbox.stats.runs import runstest_1samp
x=rng.normal(size=60)
print(runstest_1samp(x,cutoff=0,correction=True))
Output
(np.float64(1.0515299382299736), np.float64(0.2930152667646486))
R

Validated — Executed successfully in the validation environment · needs randtests

set.seed(2026)
x<-rnorm(60)
print(randtests::runs.test(x,threshold=0))
Output
	Runs Test

data:  x
statistic = -0.22672, runs = 30, n1 = 28, n2 = 32, n = 60, p-value =
0.8206
alternative hypothesis: nonrandomness

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