Runs test: check whether a sequence of data is random

Updated

The runs test (also called the Wald–Wolfowitz runs test) checks whether a sequence of observations in time order looks random. It marks each value as above or below a reference line, usually the mean or the median, and counts the runs: unbroken stretches of values on the same side. Too few runs means the data cluster, as they do when a process drifts or cycles. Too many runs means the data alternate, as they do when two sources take turns or someone over-adjusts. Most tests assume independent observations; the runs test checks that assumption directly.

When to use it

  • Checking a measurement system's repeated readings of a check standard for drift or cycles.
  • Checking that model residuals, or data meant for a control chart, behave like independent draws.
  • Looking for a pattern that is too subtle for the special-cause rules on a control chart.

How it works

Hypotheses:

  • H₀: the sequence is random; every order of the values above and below the reference line is equally likely.
  • H₁: the sequence is not random (too few or too many runs). One-sided versions test only for clustering (too few) or only for alternation (too many).

Steps:

  1. Choose the reference line: the mean or the median. Values exactly on the line are dropped.
  2. Count n₁ values above the line, n₂ below, with N = n₁ + n₂.
  3. Count the runs, R. For example, + + − + + + − − has 4 runs.
  4. Under H₀, R has this mean and variance:
E(R) = 2·n₁·n₂ / N + 1 Var(R) = 2·n₁·n₂·(2·n₁·n₂ − N) / (N² · (N − 1))
  1. For larger samples (n₁ and n₂ both above about 10), use the normal approximation:
z = (R − E(R)) / √Var(R)

The two-sided p-value is 2 × P(Z > |z|). For small samples, the exact distribution of R (counting every possible arrangement of the n₁ plus signs and n₂ minus signs) gives the p-value. Some texts also subtract 0.5 from |R − E(R)| as a continuity correction.

The NIST/SEMATECH e-Handbook of Statistical Methods describes the test in its section on the runs test for detecting non-randomness.

Assumptions: the values are in the true time (or position) order, and the reference line is fixed before counting. The test uses only the signs, so it makes no assumption about the shape of the distribution.

Example

An electronics plant uses an automatic test station to measure 100 Ω precision resistors. Every 3 hours, the station measures a check-standard resistor with a certified value, and the technician records the deviation from that value in milliohms (mΩ). Here are 28 readings, three and a half days of checks:

1.2, 2.5, 0.9, 1.7, −0.6, 0.1, −1.4, 1.0, 2.2, 3.1, 1.5, −0.9, −2.0, 0.2, −1.1, −0.4, 1.9, 0.8, 2.6, −0.3, −1.7, 0.4, −0.8, 1.3, 2.0, −1.2, 0.0, 1.4

Check-standard deviation on the resistor test station-202Deviation from certified value (mΩ)CL=0.5114710131619222528Check (every 3 hours)
Check-standard deviation on the resistor test station

On an individuals chart, none of the eight tests for special causes fires: every point is well inside the limits and the longest run on one side is 5. The runs test looks at the sequence as a whole:

  • Mean = 0.51 mΩ. Signs about the mean: + + + + − − − + + + + − − − − − + + + − − − − + + − − +
  • n₁ = 14 above, n₂ = 14 below, N = 28.
  • Runs: R = 9.
  • E(R) = 2 × 14 × 14/28 + 1 = 15.
  • Var(R) = 2 × 14 × 14 × (392 − 28)/(28² × 27) = 6.74, so the standard deviation is 2.60.
  • z = (9 − 15)/2.60 = −2.31, two-sided p-value = 0.021 (normal approximation).

The exact distribution of R gives a two-sided p-value of 0.031. The median is 0.60 mΩ, and since no value falls between the mean and the median, the runs about the median are the same and the result is identical.

How to read the results

  • The p-value of 0.021 says the readings are not a random sequence. There are 6 fewer runs than expected: the readings cluster.
  • The direction tells the story. Too few runs means clustering. Most runs last 3 to 5 checks, or 9 to 15 hours, which matches a day and night temperature cycle in the test bay. The likely cause is the temperature coefficient of the check standard or the station's reference.
  • In engineering terms, the whole range of about 5 mΩ is small next to the ±100 mΩ tolerance (±0.1%) of the resistors the station tests, so the station is still fit for use. But consecutive readings are not independent, so a measurement study run within one shift would understate the station's variation. Logging the bay temperature with each check would confirm the cause.

Common mistakes

  • Sorting the data first. The runs test only means something on data in the order they were produced. A sorted column has exactly 2 runs.
  • Moving the reference line after looking. Report whether you used the mean or the median.
  • Relying on it alone. The count says the order is not random, not what the pattern is. A run chart or an autocorrelation plot shows whether it is a drift, a cycle or a shift.
  • Using the normal approximation on very short series. With fewer than about 10 values on either side, use the exact p-value.

Frequently asked questions

What does "too few runs" mean in practice?

The process stays on one side of the center for longer than chance allows: drift, cycles, shifts between batches or shifts between operators. It is the same kind of signal as Test 2 in the tests for special causes, but spread across the whole series instead of one long run.

What does "too many runs" mean?

The values flip sides more often than chance allows. Common causes are two alternating sources (two fixtures, two spindles, two suppliers taking turns) or an operator who adjusts the process after every reading.

Should I use the mean or the median?

Either works; pick one before counting. The median splits the data evenly and is not moved by outliers, so it is the usual choice for run charts. With skewed data the two can give different counts; with roughly symmetric data they agree, as in the example.

What is the difference between this test and runs up and down?

Runs about the mean or median count stretches on one side of a line. Runs up and down count stretches of rising or falling values, which makes them more sensitive to trends and oscillation. A run chart often reports both.

Try it with your own data

StatLab SPC is free, open-source statistics for engineers. It runs in your browser, works offline, and never uploads your data.

Open StatLab