Probability

The longest run has no limit

Toss a fair coin n times and the longest run of heads is about log₂ n. The average settles: it is log₂ n − 0.667 to within a few millionths by a million tosses. The distribution does not. Because the run is a whole number and log₂ n is not, the chance of each value swings round the same cycle once every doubling of n, for ever, and computing it exactly to a million tosses shows the swing as large at the end as at the start.
19 min read 6 figures Small cases lieOrder out of noise

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 nn tosses of a fair coin — the largest kk such that kk heads appear in a row somewhere. Two patterns, one chance, different waits mentioned in passing that it is about log⁡2n\log_2 n. 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 nn grows; it cycles.

The longest run of heads at three sizes, never the same shape. n=65536: 12:0.018 13:0.117 14:0.233 15:0.239 16:0.172 17:0.104 18:0.057 19:0.030 20:0.015; n=82273: 13:0.075 14:0.204 15:0.249 16:0.197 17:0.124 18:0.070 19:0.037 20:0.019; n=104408: 13:0.040 14:0.162 15:0.248 16:0.221 17:0.148 18:0.086 19:0.046 20:0.024 21:0.012.
Fig. 1 The exact distribution of the longest run of heads in nn fair tosses for three values of nn a third of a doubling apart, each drawn against run length minus log⁡2n\log_2 n. If the distribution had a limiting shape, the three would coincide.

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 log⁡2n\log_2 n, 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 nn 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 nn tosses is at least kk exactly when a run of kk heads has appeared by toss nn — that is, when the wait TkT_k for the first run of kk heads is at most nn. So the distribution of the longest run is the distribution of every waiting time T1,T2,T3,…T_1, T_2, T_3, \ldots read off at a single moment.

The waits are the ones computed earlier for coin patterns. A run of kk heads is a pattern that overlaps itself at every shift, which is the worst case for waiting: the mean wait is 2k+1−22^{k+1} - 2, 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.

A long run, as a wait that has already ended. 10: 1.0000/1.0000, 11: 1.0000/1.0000, 12: 0.9997/0.9997, 13: 0.9817/0.9817, 14: 0.8647/0.8647, 15: 0.6321/0.6321, 16: 0.3934/0.3935, 17: 0.2212/0.2212, 18: 0.1175/0.1175, 19: 0.0606/0.0606, 20: 0.0308/0.0308, 21: 0.0155/0.0155, 22: 0.0078/0.0078, 23: 0.0039/0.0039, 24: 0.0020/0.0020.
Fig. 2 For n=65,536n = 65{,}536, the exact chance that the longest run is at least kk (bars) against 1−exp⁡(−n/2k+1)1 - \exp(-n/2^{k+1}) (dots), the chance if runs of kk began independently at rate 1/2k+11/2^{k+1} per toss. They agree to within 0.0001.

The approximation is easy to explain. A run of kk heads begins at a given toss when that toss is a head preceded by a tail — or by the start — and followed by k−1k - 1 more heads: probability 2−(k+1)2^{-(k+1)}. Such beginnings are rare and almost independent, so their number by toss nn is nearly Poisson with mean n/2k+1n/2^{k+1}, and the chance of none is exp⁡(−n/2k+1)\exp(-n/2^{k+1}). The exact calculation uses a recurrence for the chance qk(n)q_k(n) that nn tosses contain no run of kk:

qk(n)=qk(n−1)−qk(n−k−1)2k+1,q_k(n) = q_k(n-1) - \frac{q_k(n-k-1)}{2^{k+1}},

which subtracts, from the sequences of length n−1n - 1 with no run, those that complete a first run at toss nn — and such a run must be a tail followed by kk heads, with no run in the n−k−1n - k - 1 tosses before. Every figure in this essay comes from running that recurrence for every kk up to 44 and every nn up to 2202^{20}, a little over a million, and the recurrence was checked against a direct count of all 2n2^n sequences for nn up to 14.

The average settles

Write LnL_n for the longest run in nn tosses. Its average can be computed from the distribution, and it settles beautifully.

The average longest run, settling on log₂ n − 0.667. 6.0: -0.6436154, 7.1: -0.6561575, 8.1: -0.6616722, 9.1: -0.6644594, 10.1: -0.6658564, 11.1: -0.6665541, 12.1: -0.6669031, 13.1: -0.6670776, 14.1: -0.6671649, 15.1: -0.6672086, 16.1: -0.6672304, 17.1: -0.6672413, 18.1: -0.6672468, 19.1: -0.6672495; limit -0.6672538.
Fig. 3 The exact average longest run in nn tosses minus log⁡2n\log_2 n, for nn from 64 to about a million. The dashed line is γ/ln⁡2−3/2=−0.66725\gamma/\ln 2 - 3/2 = -0.66725, where γ\gamma is Euler’s constant.

The average approaches log⁡2n−0.66725\log_2 n - 0.66725 within a few doublings, and by a million tosses it is within three millionths of it. The constant has an exact form, γ/ln⁡2−3/2\gamma/\ln 2 - 3/2, with γ=0.5772…\gamma = 0.5772\ldots 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 γ\gamma.

The route to the constant is short. The average of a quantity that takes the values 0,1,2,…0, 1, 2, \ldots is the sum of its tail probabilities, ∑k≥1P(Ln≥k)\sum_{k \ge 1} P(L_n \ge k). By the previous figure each tail is close to 1−exp⁡(−n/2k+1)1 - \exp(-n/2^{k+1}), which is nearly 1 for kk well below log⁡2n\log_2 n and nearly 0 well above it. The sum is therefore about log⁡2n\log_2 n — 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 ∑j(1−e−2j)\sum_j \left(1 - e^{-2^{j}}\right) against ∑je−2−j\sum_j e^{-2^{-j}} over the halvings and doublings around the transition. Sums over powers of two of that kind are evaluated by turning them into integrals of e−xe^{-x} against dx/xdx/x, and ∫(e−x−its step approximation) dx/x\int (e^{-x} - \text{its step approximation})\,dx/x is exactly where Euler’s constant lives. The −3/2-3/2 splits as −1-1, from the exponent k+1k + 1 in the rate 2−(k+1)2^{-(k+1)} — a run needs a tail in front of it, which halves the rate and costs one doubling — and −1/2-1/2, the average price of replacing a sum over whole numbers by an integral — the same rounding to whole numbers that, further down, adds 1/121/12 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 log⁡2n\log_2 n 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.

Probabilities that cycle once per doubling, for ever. P(L = floor(log2 n)) between 0.1723 and 0.2380 for n ≥ 2^16.
Fig. 4 The exact probability that the longest run equals ⌊log⁡2n⌋−2\lfloor \log_2 n \rfloor - 2, ⌊log⁡2n⌋−1\lfloor \log_2 n \rfloor - 1, ⌊log⁡2n⌋\lfloor \log_2 n \rfloor, ⌊log⁡2n⌋+1\lfloor \log_2 n \rfloor + 1 and ⌊log⁡2n⌋+2\lfloor \log_2 n \rfloor + 2, for nn from 64 to about a million. None of the five curves settles; each repeats the same sawtooth once per doubling.

Each curve is a sawtooth with one tooth per doubling of nn, and the teeth do not shrink. The chance that the longest run equals ⌊log⁡2n⌋\lfloor \log_2 n \rfloor 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 log⁡2n\log_2 n 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 ⌊log⁡2n⌋−1\lfloor \log_2 n \rfloor - 1, and just before the next it is about to become ⌊log⁡2n⌋\lfloor \log_2 n \rfloor — which, as soon as the power of two is passed, is renamed ⌊log⁡2n⌋−1\lfloor \log_2 n \rfloor - 1.

The reason is arithmetic, not probability. A continuous quantity near log⁡2n\log_2 n with a fixed spread would have a limiting shape once log⁡2n\log_2 n is subtracted. The longest run is a whole number. As nn doubles, log⁡2n\log_2 n 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 log⁡2n\log_2 n, and the slices come round again at every doubling. A distribution that depends on the fractional part of log⁡2n\log_2 n cannot converge as n→∞n \to \infty, because the fractional part does not.

The cycle itself

Plotting the probabilities against the fractional part of log⁡2n\log_2 n instead of log⁡2n\log_2 n itself folds the sawteeth into a single closed curve.

The cycle the longest run's distribution runs round. offset -1: 0.00:0.239 0.08:0.242 0.16:0.245 0.23:0.247 0.33:0.249 0.41:0.250 0.48:0.250 0.58:0.249 0.66:0.248 0.73:0.246 0.83:0.242 0.91:0.238 0.98:0.234; offset 0: 0.00:0.172 0.08:0.178 0.16:0.184 0.23:0.190 0.33:0.197 0.41:0.202 0.48:0.208 0.58:0.214 0.66:0.220 0.73:0.224 0.83:0.230 0.91:0.234 0.98:0.238; offset 1: 0.00:0.104 0.08:0.108 0.16:0.113 0.23:0.118 0.33:0.124 0.41:0.129 0.48:0.135 0.58:0.141 0.66:0.147 0.73:0.153 0.83:0.159 0.91:0.165 0.98:0.171; offset 2: 0.00:0.057 0.08:0.060 0.16:0.063 0.23:0.066 0.33:0.070 0.41:0.073 0.48:0.077 0.58:0.081 0.66:0.085 0.73:0.089 0.83:0.094 0.91:0.098 0.98:0.103.
Fig. 5 The probabilities of four values of the longest run against the fractional part of log⁡2n\log_2 n, for nn beyond 2172^{17} (lines) and between 292^9 and 2102^{10} (dots). The dashed curves come from the formula exp⁡(−n/2k+1)\exp(-n/2^{k+1}) for the chance that the longest run is below kk.

For large nn every value of nn with the same fractional part of log⁡2n\log_2 n gives the same probabilities, so the curves for nn beyond 131,072 lie on a single cycle, and the dots for nn 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 kk is close to exp⁡(−n/2k+1)\exp(-n/2^{k+1}), which depends on nn only through n/2k+1n/2^{k+1}, and when kk is measured from ⌊log⁡2n⌋\lfloor \log_2 n \rfloor 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.

A spread that does not grow with the number of tosses. 6.0: 3.09302, 7.1: 3.28067, 8.1: 3.37837, 9.1: 3.43511, 10.1: 3.46730, 11.1: 3.48527, 12.1: 3.49520, 13.1: 3.50064, 14.1: 3.50360, 15.1: 3.50520, 16.1: 3.50606, 17.1: 3.50652, 18.1: 3.50676, 19.1: 3.50689; limit 3.50705.
Fig. 6 The exact variance of the longest run against log⁡2n\log_2 n. The dashed line is π2/(6ln⁡22)+1/12=3.5070\pi^2/(6 \ln^2 2) + 1/12 = 3.5070: the variance of Gumbel’s law in units of a doubling, plus the 1/121/12 that rounding to whole numbers adds.

However many tosses, the longest run is pinned to log⁡2n\log_2 n within a couple of units: its variance does not grow with nn 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 π2/(6ln⁡22)≈3.4237\pi^2/(6 \ln^2 2) \approx 3.4237. 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, 1/12≈0.08331/12 \approx 0.0833. Their sum is 3.50703.5070, and the computed variance at a million tosses is 3.5070. Like the average, the variance carries an oscillation of period one in log⁡2n\log_2 n, 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 nn only through the fractional part of log⁡2n\log_2 n can be written as a Fourier series in that fractional part, a sum of waves e2πimlog⁡2ne^{2\pi i m \log_2 n}. For the average of the longest run, the coefficient of the mm-th wave turns out to be a value of Euler’s gamma function at the imaginary point 2πim/ln⁡22\pi i m/\ln 2, divided by ln⁡2\ln 2. Far up the imaginary axis the gamma function is extraordinarily small: its size there falls like e−π∣t∣/2e^{-\pi |t|/2}, and at t=2π/ln⁡2≈9.06t = 2\pi/\ln 2 \approx 9.06 that is about e−14.2e^{-14.2}, below a millionth. So the oscillation in the average is real, has period one in log⁡2n\log_2 n, 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 1/m1/m. 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 nn, 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 2n2\sqrt n, but its fluctuations grow like n1/6n^{1/6}, and centred and scaled it converges to a fixed limiting shape. The longest run’s spread is stuck at about 3.5≈1.9\sqrt{3.5} \approx 1.9 for every nn, so the slicing never gets finer, and the cycle never fades.

The same holds for a biased coin. With heads coming up with probability pp, the longest run is about log⁡1/pn\log_{1/p} n, its variance again settles to a constant, and the probabilities cycle with period one in log⁡1/pn\log_{1/p} n — once every time nn is multiplied by 1/p1/p 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 nn, 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 nn tosses with a fixed table of probabilities is using a distribution that depends on where nn falls between powers of two, and a table computed for one nn 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 nn 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 2\sqrt 2 or π\pi, whose normality is itself unproved, nobody can prove that the longest run of ones among the first nn digits grows like log⁡2n\log_2 n — 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 nn tosses is a very predictable quantity: within a couple of units of log⁡2n−0.667\log_2 n - 0.667, with a variance of 3.507 that does not grow. Its average and variance settle to constants built from γ\gamma and π2\pi^2. 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.

Named objects

A dashed tag is an object no other essay names yet.

Convergence rateExpectationLimitMarkov chainRecurrenceVariance