From Numeric Observations to a Runs Test
A runs test can be applied to a sequence of numeric observations when the question is whether their order appears random. The values themselves may have different scales or units; the test reduces each observation to one of two symbols according to its position relative to the sample median. It then counts the runs: consecutive groups of the same symbol.
The order of observations must be meaningful and preserved—for example, measurements recorded over time or in a specified sequence. Sorting the observations before counting runs would destroy the ordering being tested. The runs test assesses whether that order is consistent with random ordering, conditional on the numbers of observations on each side of the median. A small run count suggests clustering; a large run count suggests unusually frequent alternation.
For example, the sequence \(--++-+\) has four runs: \(--\), \(++\), \(-\), and \(+\). The original numeric order matters, but the distances from the median do not enter the run count. Once the symbols have been formed, the test is the runs test for a binary sequence.
Median Ties and the Test’s Conditions
An observation equal to the median has neither a strict above-median nor a strict below-median symbol. This tutorial uses the common convention of omitting such observations before counting runs. Do not silently assign a tie to either symbol: doing so changes the sequence and can change \(R\). State that ties were omitted and report how many observations remain.
If the sample is odd, at least one observation equals the sample median and is therefore omitted under this convention. Additional equal observations may also be omitted. If the sample is even, the median is the average of the two middle ordered observations; it need not equal any observed value. When no observation equals it, the number of observations strictly above and below the median is equal.
After omission, let \(n_+\) be the number of \(+\) symbols, \(n_-\) the number of \(-\) symbols, and \(N=n_++n_-\). The standard random-ordering null model treats all arrangements of these fixed symbols as equally likely. It presumes that the original order is appropriate to test and that observations can be regarded as exchangeable under the null. If the data have a design or dependence structure that rules out random ordering, the test’s reference probabilities do not apply as stated.
Why the Expected Number of Runs Has This Form
The first symbol always starts a run. Every later run begins exactly where two adjacent symbols differ. Therefore, \(R\) is one plus the number of adjacent changes. Under the random-ordering null model, any two specified positions are occupied by different symbols with probability \(2n_+n_-/[N(N-1)]\). There are \(N-1\) adjacent pairs, so linearity of expectation gives the expected run count.
For \(n_+>1\) and \(n_->1\), the variance under this same null model is
For small samples, use the exact distribution of \(R\), obtained by counting arrangements with the fixed symbol totals. For sufficiently large samples, a normal approximation may be used with mean \(E[R]\) and variance \(\operatorname{Var}(R)\); the continuity correction is important because \(R\) is an integer. The exact probability calculation or the approximation must match the alternative being tested.
Exact Tests Using Numeric Data
Worked Example: A Clustered Numeric Sequence
A hypothetical sensor records eight readings in sequence: \(10,12,14,16,18,20,22,24\). Test at the 5% level whether the sequence shows unusually few runs, using an exact lower-tail test.
Method and coding. Sort only to find the median: the ordered readings are already \(10,12,14,16,18,20,22,24\). There are eight observations, so the median is the average of the fourth and fifth values: \(M=(16+18)/2=17\). There are no observations equal to 17. The first four readings are below 17 and the last four are above 17, giving the sequence \(----++++\). Thus \(n_-=4\), \(n_+=4\), \(N=8\), and \(R=2\).
Exact probability. Given four symbols of each kind, the total number of possible arrangements is \(\binom{8}{4}=70\). Exactly two arrangements have two runs: all four minus symbols followed by all four plus symbols, or the reverse. Therefore, \(P(R\leq2)=2/70=0.0286\), to four decimal places.
At significance level 0.05, \(0.0286<0.05\), so reject the random-ordering null in favor of unusually few runs. The sequence is clustered relative to this test. The result does not establish that the readings came from any particular cause.
Common mistake avoided. The sequence must not be sorted into below- and above-median groups before counting runs. That would produce four minus symbols followed by four plus symbols regardless of the original order and would not test the recorded sequence.
Worked Example: Ties at the Median and a Two-Sided Exact Test
A hypothetical process produces the ordered readings \(6,1,7,2,8,3,5,4,5\). Use the stated convention of omitting readings equal to the median, then conduct a two-sided exact test using distance from the expected number of runs.
Find the median and code the data. In sorted order the readings are \(1,2,3,4,5,5,6,7,8\), so the median is the fifth value, \(M=5\). Omit both observations equal to 5. The remaining sequence is \(6,1,7,2,8,3,4\), which codes as \(+-+-+--\). Hence \(n_+=3\), \(n_-=4\), \(N=7\), and \(R=6\).
Find the null mean. \(E[R]=1+2(3)(4)/7=31/7\), or approximately \(4.429\). The observed run count is above the mean by \(6-31/7=42/7-31/7=11/7\), or approximately \(1.571\) runs.
Count outcomes at least this far from the mean. The corresponding lower cutoff is \(31/7-11/7=20/7\), approximately \(2.857\). Since run counts are integers, the lower-tail outcomes are \(R\leq2\); the upper-tail outcomes are \(R\geq6\). There are \(\binom{7}{3}=35\) arrangements. For \(R=2\), the two symbols form two blocks, so there are two arrangements. For \(R=6\), there are six arrangements: the six runs alternate in sign, with three runs of each sign; the three plus symbols must occupy their three runs singly, while the four minus symbols can be divided among their three runs in \(\binom{3}{2}=3\) ways, for either starting sign. Thus the two-sided probability is \(P(R\leq2\text{ or }R\geq6)=(2+6)/35=0.2286\), to four decimal places.
At the 5% level, do not reject the null hypothesis. The observed run count is not sufficiently unusual under this two-sided exact rule. Common mistake avoided: writing \(31/7-6=-11/7\) reverses the subtraction and gives the wrong signed difference. The observed count is \(11/7\) above the mean; the distance used for the two-sided calculation is the positive value \(11/7\).
Normal Approximation for a Larger Sequence
When both symbol counts are sufficiently large for the normal approximation to be reasonable, calculate the null mean and variance from the formulas above. For an upper-tail test with observed count \(R\), the continuity-corrected standardized value is \((R-0.5-E[R])/\sqrt{\operatorname{Var}(R)}\). For a lower-tail test, use \((R+0.5-E[R])/\sqrt{\operatorname{Var}(R)}\). For a two-sided test based on distance from the mean, move the observed count 0.5 toward the mean before standardizing. These corrections approximate the probability of the discrete tail, rather than the area beyond the observed integer alone.
Worked Example: Two-Sided Approximation With Balanced Symbols
A hypothetical monitoring system records 18 readings. Their median is 9.5, and none equals the median. In their original order, the readings are \(10,11,12,1,2,13,3,4,14,5,15,6,16,7,17,8,18,9\). Use a two-sided normal approximation with continuity correction at the 5% level.
Code and count runs. Values \(1\) through \(9\) are below 9.5; values \(10\) through \(18\) are above it. The symbol sequence is \(+++--+--+-+-+-+-+-\). It has seven plus runs and seven minus runs, for \(R=14\); there are nine symbols of each kind, so \(n_+=n_-=9\) and \(N=18\).
Calculate the null mean and variance. \(E[R]=1+2(9)(9)/18=10\). The variance is \(2(9)(9)(2(9)(9)-9-9)/(18^2(17)) =162(144)/5508 =4.2338\), to four decimal places. The standard deviation is \(\sqrt{4.2338}\approx2.0576\).
Standardize with the continuity correction. The observed distance above the mean is \(14-10=4\) runs. For the two-sided approximation, use \(14-0.5=13.5\) in the upper tail: \(z=(13.5-10)/2.0576\approx1.701\). The approximate two-sided p-value is \(2[1-\Phi(1.701)]\approx0.0889\), to four decimal places.
Since \(0.0889>0.05\), do not reject the random-ordering null at the 5% level. This result is not enough evidence of an unusually high run count. Common mistake avoided. Omitting the continuity correction would use \(14\), not \(13.5\), and overstate the distance used for this discrete upper tail.
Interpreting the Result Carefully
A low run count indicates that observations on the same side of the median occur in longer groups than expected under random ordering. A high count indicates more switching between sides than expected. Neither outcome identifies a mechanism, and a failure to reject does not prove randomness. The test only checks one feature of the ordering: the number of adjacent changes after the median coding.
Before reporting a result, verify the median, the original order, the treatment of observations equal to the median, and the resulting symbol totals. Then state whether the alternative is lower-tail, upper-tail, or two-sided. For an exact test, describe the tail rule used; for an approximation, report the continuity correction and approximate p-value. These steps prevent a correct run count from being paired with an inappropriate probability calculation.
Check Your Understanding
For each problem, retain the given order and apply the stated convention for median ties.
- The readings are \(3,8,2,9,4,10,1,11\). Find the median, the coded sequence, the symbol counts, and the number of runs. (Answer: median \(6\); sequence \(-+-+-+-+\); four of each symbol; \(R=8\).)
- The readings are \(12,10,15,9,14,8,16,11,18,13\). Find the median and number of runs. (Answer: median \(12.5\); coded sequence \(--+-+-+-++\); \(R=8\).)
- A coded sequence has three plus symbols followed by three minus symbols. Under the exact null distribution, find the lower-tail probability and the two-sided probability using distance from the mean. (Answer: \(E[R]=4\); lower-tail probability \(P(R\leq2)=0.1000\); two-sided probability \(P(R\leq2\text{ or }R\geq6)=0.2000\).)
- The readings are \(2,6,4,7,5,9,6,10,11\). Omit observations equal to the sample median. Give the remaining symbol counts and runs. (Answer: median \(6\); omit both 6s; three below and four above; coded sequence \(--+-+++\); \(R=4\).)
- For \(n_+=n_-=8\), an observed sequence has \(R=12\). Use the normal approximation with a two-sided continuity correction. (Answer: \(E[R]=9\), \(\operatorname{Var}(R)=3.7333\), \(z\approx1.294\), and approximate two-sided p-value \(0.1958\).)