Why the Fixed Counts Matter
A runs statistic counts consecutive groups of identical symbols. For example, the sequence A A B A B B has four runs: A A, B, A, and B B. In a runs test, the observed number of runs is compared with what would be expected if the same observed counts of the two symbols had been placed in random order.
The fixed-count condition is essential. If a sequence contains \(n_1\) symbols of type A and \(n_2\) symbols of type B, random ordering means that every distinct arrangement of those \(N=n_1+n_2\) symbols is equally likely. It does not mean that each position receives an independent A or B with a fixed probability. The formulas for the expectation and variance below are for this uniform random permutation model.
This model treats the sequence as a random permutation of fixed labels. For instance, it can represent a hypothetical process-monitoring record in which the numbers of observations above and below a specified reference value have already been fixed, and the question is whether their order is consistent with random ordering. No interest-rate or mortality assumption is involved: the model is a finite probability distribution over arrangements.
Expected Number of Runs
A useful way to count runs is to count changes between adjacent positions. A sequence always begins with one run. Each adjacent pair with different symbols starts one additional run. Write \(I_j=1\) if positions \(j\) and \(j+1\) have different symbols, and \(I_j=0\) otherwise, for \(j=1,\ldots,N-1\). Then
For any specified adjacent pair, there are \(N(N-1)\) ordered ways to fill its two positions from the fixed collection of symbols. There are \(n_1n_2\) ways to place A then B, and \(n_2n_1\) ways to place B then A. Thus the probability of a change at that pair is \(2n_1n_2/[N(N-1)]\). By linearity of expectation, the expected number of changes is the sum of this probability over the \(N-1\) adjacent pairs.
Worked Example: Expected Runs With Unequal Counts
A hypothetical quality-control record has five A labels and three B labels. Conditional on those counts, suppose all distinct orderings are equally likely. Find the expected number of runs. This is a fixed-count random-permutation model; no interest or mortality assumption applies.
Method. Use the adjacent-change indicator argument. Here \(n_1=5\), \(n_2=3\), and \(N=5+3=8\).
The probability that a specified adjacent pair differs is
There are \(N-1=7\) adjacent pairs, so the expected number of changes is
Adding the initial run gives
The common mistake is to use an independent-position probability for a change, even though the counts are fixed. The fixed-count adjacent-pair probability is \(15/28\), not \(2(5/8)(3/8)\). The computed expectation is between the smallest possible number of runs, 2, and the largest possible number, 7, as it should be.
Variance: Why Adjacent Changes Are Dependent
The indicators \(I_j\) are not independent. In particular, \(I_j\) and \(I_{j+1}\) share a position. Even indicators for disjoint pairs are dependent because the total counts of A and B are fixed. Therefore, adding the individual indicator variances alone does not give the variance of \(R\); the covariances must also be included.
Set \(a=n_1\) and \(b=n_2\), so \(N=a+b\), and write \(p=2ab/[N(N-1)]\). For two neighboring indicators to both equal 1, their three positions must have the pattern A-B-A or B-A-B. Counting these patterns gives
The last equality uses \((a-1)+(b-1)=N-2\). Thus the covariance for a neighboring pair of indicators is \(ab/[N(N-1)]-p^2\).
For two non-overlapping adjacent pairs, both pairs must contain one A and one B. There are four possible orientations across the two pairs. Counting assignments to their four positions gives
when \(N\geq 4\). The corresponding covariance is this joint probability minus \(p^2\). There are \(N-2\) neighboring pairs of indicators, and \((N-2)(N-3)/2\) non-overlapping pairs. Applying the variance-of-a-sum identity, which includes twice the covariance for each unordered pair, gives the following expression before simplification:
Substituting \(p=2ab/[N(N-1)]\) and \(a+b=N\), then collecting terms over a common denominator, simplifies this expression to
The derivation shows why dependence matters: the variance contains contributions from both overlapping and disjoint pairs. The final formula is nonnegative because \(n_1,n_2\geq1\) implies \(2n_1n_2\geq n_1+n_2=N\). Equality occurs when one count is 1 and \(N=2\), where the ordering and number of runs are fixed.
Worked Example: Variance for Balanced Counts
A hypothetical sequence contains four A labels and four B labels. Conditional on these counts, every ordering is equally likely. Find the mean and variance of the number of runs. This is a fixed-count permutation model, not independent sampling; no interest or mortality assumption applies.
Method. Apply the expectation and variance formulas with \(n_1=4\), \(n_2=4\), and \(N=8\). The expected number is
For the variance, first calculate the factors in the numerator:
The denominator is
Therefore,
The common mistake is to treat the seven adjacent-change indicators as independent Bernoulli variables and use only \((N-1)p(1-p)\). They are dependent, so that calculation omits covariance terms. The result is consistent with the symmetry of the model: exchanging A and B does not change the distribution of the number of runs.
Worked Example: A Single Label of One Type
A hypothetical sequence contains one A and five B labels, randomly ordered conditional on those counts. Find the mean and variance of the number of runs, and verify them by considering the position of A. No interest or mortality assumption applies.
Method. First use the fixed-count formulas with \(n_1=1\), \(n_2=5\), and \(N=6\):
For the variance,
To check, A is equally likely to occupy any of the six positions. If it is at either end, there are two runs; this occurs with probability \(2/6=1/3\). If it is in one of the four interior positions, it is separated from B labels on both sides, so there are three runs; this occurs with probability \(4/6=2/3\). The mean from these possibilities is
Using \(E[R^2]=2^2(1/3)+3^2(2/3)=22/3\), the variance check is
The common mistake is to say that one A always creates two runs. It creates two runs only when it is at an end; an interior A creates three. Both direct checks agree with the formulas.
Assumptions and Practical Checks
The results apply to a binary sequence under a specific null model, not to every sequence that happens to contain two kinds of observations. Before using them, check that the following conditions are appropriate:
- Two symbols: every observation has been assigned to exactly one of two categories, A or B.
- Fixed counts: the values of \(n_1\) and \(n_2\) are conditioned on, rather than modeled as random counts.
- Uniform ordering under the null: all distinct arrangements with those counts are equally likely. This is the meaning of random ordering here.
- Meaningful sequence order: the order of observations is the order relevant to the question, such as time or a prespecified sequence of positions.
If the observations are assigned to A or B independently with a fixed probability, the number of each symbol is generally random. That is a different probability model, and the fixed-count formulas need not apply. Likewise, a changing probability of observing A across positions, or a design that restricts which arrangements are possible, violates uniform random ordering. The formulas describe the null distribution’s mean and variance; they do not by themselves determine an exact probability for a particular observed number of runs.
Check Your Understanding
For each problem, assume uniform random ordering conditional on the stated symbol counts. Give the mean and variance unless the question asks for an interpretation.
- A sequence has three A labels and five B labels. Find the expected number of runs and its variance. (Answer: \(E[R]=4.75\), \(\operatorname{Var}(R)=165/112\approx1.4732\).)
- A sequence has four A labels and four B labels. Find the expected number of runs and its variance. (Answer: \(E[R]=5\), \(\operatorname{Var}(R)=12/7\approx1.7143\).)
- A sequence has two A labels and seven B labels. Find the expected number of runs and its variance. (Answer: \(E[R]=37/9\approx4.1111\), \(\operatorname{Var}(R)=133/162\approx0.8210\).)
- A sequence has one A label and seven B labels. Find the mean and variance, then explain why the number of runs can be either two or three. (Answer: \(E[R]=11/4=2.75\), \(\operatorname{Var}(R)=3/16=0.1875\); A at an end gives two runs, while A in an interior position gives three.)
- Compare a sequence with five A and five B labels to one with two A and eight B labels. Which has the larger expected number of runs and which has the larger variance? (Answer: the five-and-five sequence has the larger mean, \(6\) versus \(4.2\), and the larger variance, \(20/9\approx2.2222\) versus \(176/225\approx0.7822\).)