The longest run has no limit
Worth reading first: Two-thirds of a step past the line · Two patterns, one chance, different waits.
Two-thirds of a step past the line ended with a question about races between coin patterns: whether all sixteen patterns of length four, raced at once, finish in an order that can be read off their overlaps. That question turns out to have a trivial answer. Every window of four tosses is one of the sixteen patterns, so the race always ends at the fourth toss, and each pattern wins with chance exactly one in sixteen. The interesting races are the ones in which some outcome is not covered, and the most extreme of those is a single pattern that is very long: a run of heads.
This essay is about the longest run of heads in tosses of a fair coin — the largest such that heads appear in a row somewhere. Two patterns, one chance, different waits mentioned in passing that it is about . That is true, and the average is known to remarkable precision. What is surprising is that the distribution of the longest run has no limit at all. It does not converge to anything as grows; it cycles.
The three distributions in the figure are for 65,536, 82,273 and 104,408 tosses, and they are drawn against the run length minus , so that a common limiting shape would put them on top of one another. They do not coincide. The bars sit at different horizontal positions, and their heights differ. Double and each shape comes back exactly, shifted by one. That is the whole story in one picture, and the rest of the essay computes it.
A wait that has already ended
The longest run is a waiting time in disguise. The longest run in tosses is at least exactly when a run of heads has appeared by toss — that is, when the wait for the first run of heads is at most . So the distribution of the longest run is the distribution of every waiting time read off at a single moment.
The waits are the ones computed earlier for coin patterns. A run of heads is a pattern that overlaps itself at every shift, which is the worst case for waiting: the mean wait is , twice what a pattern with no self-overlap of the same length would take. The figure below checks that mean from the chain whose state is the current run length — each toss either extends the run by one or resets it to zero — and checks the whole distribution of the longest run against a simple approximation.
The approximation is easy to explain. A run of heads begins at a given toss when that toss is a head preceded by a tail — or by the start — and followed by more heads: probability . Such beginnings are rare and almost independent, so their number by toss is nearly Poisson with mean , and the chance of none is . The exact calculation uses a recurrence for the chance that tosses contain no run of :
which subtracts, from the sequences of length with no run, those that complete a first run at toss — and such a run must be a tail followed by heads, with no run in the tosses before. Every figure in this essay comes from running that recurrence for every up to 44 and every up to , a little over a million, and the recurrence was checked against a direct count of all sequences for up to 14.
The average settles
Write for the longest run in tosses. Its average can be computed from the distribution, and it settles beautifully.
The average approaches within a few doublings, and by a million tosses it is within three millionths of it. The constant has an exact form, , with Euler’s constant — the same constant by which the harmonic numbers exceed the logarithm in the collector’s problem, and for a related reason. The longest run is the largest of many nearly independent, geometrically distributed run lengths, and the average of a maximum of that kind is a logarithm plus a constant built from .
The route to the constant is short. The average of a quantity that takes the values is the sum of its tail probabilities, . By the previous figure each tail is close to , which is nearly 1 for well below and nearly 0 well above it. The sum is therefore about — the number of terms close to 1 — plus a correction from the terms in the transition, and that correction is a sum of the form against over the halvings and doublings around the transition. Sums over powers of two of that kind are evaluated by turning them into integrals of against , and is exactly where Euler’s constant lives. The splits as , from the exponent in the rate — a run needs a tail in front of it, which halves the rate and costs one doubling — and , the average price of replacing a sum over whole numbers by an integral — the same rounding to whole numbers that, further down, adds to the variance.
The average is not quite settled, though. Over the last doubling computed it still moves by about four millionths, and part of what moves is an oscillation of period one in that never dies out. Its amplitude is so small that no figure scaled to show the average can show it, and the computed averages, which move by a few millionths over the last doubling, bound it. That small oscillation is the trace, in the average, of something that in the distribution is not small at all.
The distribution cycles
The figure that shows the failure to converge plots the probability of individual values.
Each curve is a sawtooth with one tooth per doubling of , and the teeth do not shrink. The chance that the longest run equals swings between 0.172 and 0.238 at the beginning of the range and between exactly the same values at the end. At each power of two the floor of steps up by one, every curve jumps to a different value, and the cycle begins again. Even which value is the likeliest changes within each doubling: just after a power of two the likeliest value is , and just before the next it is about to become — which, as soon as the power of two is passed, is renamed .
The reason is arithmetic, not probability. A continuous quantity near with a fixed spread would have a limiting shape once is subtracted. The longest run is a whole number. As doubles, advances by one, and in between it passes smoothly through every fractional value, while the possible values of the run stay put at the integers. The same continuous shape is sliced at the integers in a different place for every fractional part of , and the slices come round again at every doubling. A distribution that depends on the fractional part of cannot converge as , because the fractional part does not.
The cycle itself
Plotting the probabilities against the fractional part of instead of itself folds the sawteeth into a single closed curve.
For large every value of with the same fractional part of gives the same probabilities, so the curves for beyond 131,072 lie on a single cycle, and the dots for between 512 and 1,024 already lie nearly on it. The cycle is predicted exactly by the waiting-time approximation: the chance that the longest run is below is close to , which depends on only through , and when is measured from that ratio depends only on the fractional part. The formula is the extreme-value law of Emil Gumbel, the limiting law for the maximum of many independent quantities with exponential tails, cut into whole numbers. The cut is what makes the law cycle instead of converge.
This is the opposite of what a central limit theorem leads one to expect. Sums of many independent quantities settle into a bell curve once they are centred and scaled, whatever the quantities. Maxima of many independent quantities settle into one of three extreme-value shapes — when the quantities are continuous. When they are whole numbers with geometric tails, as run lengths are, there is no limit at all, only this cycle. The tail is not a bell found that the extremes of a sum obey laws the bell does not describe; here the extreme does not obey a limit law of any kind.
A spread that does not grow
The variance tells the same story in its own terms.
However many tosses, the longest run is pinned to within a couple of units: its variance does not grow with but settles at about 3.507. The value has two parts. The variance of Gumbel’s law, measured in units where one unit is a doubling, is . Rounding a continuous quantity to the nearest whole number, at a position that is uniformly spread through the cycle, adds the variance of a uniform error on an interval of length one, . Their sum is , and the computed variance at a million tosses is 3.5070. Like the average, the variance carries an oscillation of period one in , too small to draw.
Why the average hardly moves
The cycle is large in the probabilities and tiny in the average, and the contrast has a precise cause. Any quantity that depends on only through the fractional part of can be written as a Fourier series in that fractional part, a sum of waves . For the average of the longest run, the coefficient of the -th wave turns out to be a value of Euler’s gamma function at the imaginary point , divided by . Far up the imaginary axis the gamma function is extraordinarily small: its size there falls like , and at that is about , below a millionth. So the oscillation in the average is real, has period one in , never dies out, and is about a millionth in size.
The probabilities have no such protection. Their Fourier coefficients are not values of a smooth function evaluated far out, but the coefficients of a sawtooth, with a jump at every power of two, and a jump’s Fourier coefficients fall only like . The same arithmetic produces a cycle of size a tenth in the probabilities and a millionth in the average. It is the same mechanism multiplying makes the digit one common used for leading digits: a quantity periodic in a logarithm, whose size depends on how smooth the periodic function is.
Why there is a cycle at all
The cycle needs one more ingredient besides a whole-number quantity tracking a logarithm: the spread must stay bounded. If the spread grew with , the integer slicing would become finer and finer relative to the shape, and the dependence on the fractional part would wash out. That is what happens to the longest climb of a shuffle, another longest-something in a random arrangement: it is a whole number too, near , but its fluctuations grow like , and centred and scaled it converges to a fixed limiting shape. The longest run’s spread is stuck at about for every , so the slicing never gets finer, and the cycle never fades.
The same holds for a biased coin. With heads coming up with probability , the longest run is about , its variance again settles to a constant, and the probabilities cycle with period one in — once every time is multiplied by rather than by 2. The fair coin is the case in which the cycle’s period is a doubling.
The averages hide the cycle because averaging over the slices smooths it: each moment of the distribution is a sum over all the slices, and the sums barely depend on where the slicing falls. Only the individual probabilities expose it. Small cases lie, and here large cases lie as well: no value of , however large, shows the limit, because there is none.
What runs in random data look like
The longest run is a practical statistic. Tests of randomness check whether a sequence contains runs as long as a random sequence would, and a person writing down a “random” sequence of a hundred coin tosses almost always makes the longest run too short — four or five, when the average for a hundred fair tosses is about six. Twenty-three people noted that fake sequences are recognisable for exactly this reason: people avoid the clusters that self-overlapping patterns produce, and a run of heads is the pattern that overlaps itself most.
The cycle matters for such tests only in a mild way. A test that compares the longest run in tosses with a fixed table of probabilities is using a distribution that depends on where falls between powers of two, and a table computed for one is not right for another a little larger. The exact recurrence costs almost nothing to run, and the safe practice is to compute the distribution for the in hand.
Still open: runs in sequences that are not independent
For independent fair tosses everything here is exact: the recurrence, the cycle, the constants. The open questions begin when the tosses are not independent. For the binary digits of numbers like or , whose normality is itself unproved, nobody can prove that the longest run of ones among the first digits grows like — though every computation agrees, and almost every orbit is fair explained why almost every number behaves this way. For sequences with dependence that decays, such as the output of a Markov chain, the longest run is known to be logarithmic with a constant set by the chain, and the oscillation persists; how large it is, and how it depends on the chain, has been worked out only for special cases. And for the longest run of a pattern other than all heads — the longest stretch of repeated HTH, say — the waiting-time picture generalises, but the constants depend on the pattern’s overlaps in ways that are known one pattern at a time.
Settled on average, cycling in detail
The longest run of heads in tosses is a very predictable quantity: within a couple of units of , with a variance of 3.507 that does not grow. Its average and variance settle to constants built from and . And its distribution never settles at all, because a whole number cannot follow a logarithm smoothly; the chance of each value goes round the same cycle once every time the number of tosses doubles. Computed exactly to a million tosses, the cycle at the end is the cycle at the beginning.
What links here
Computed from the collection, not written here: the essays that point at this one.
Shares its objects with
Essays that name at least two of the same things, and that neither author linked.
- Where the shares have nowhere to go — both name expectation, limit, markov chain, recurrence
- How far from the average a thing can be — both name convergence rate, expectation, variance
- How fast the bell arrives — both name convergence rate, expectation, variance
- No single input can move it far — both name convergence rate, expectation, variance
- Sampling where the answer lives — both name convergence rate, expectation, variance
- The average settles and the wobble does not — both name convergence rate, expectation, variance
Named objects
A dashed tag is an object no other essay names yet.
Convergence rateExpectationLimitMarkov chainRecurrenceVariance