One pass, and no room

Counting past what the register holds

Morris's counter counts ten million events in five bits by incrementing with probability 2 to the minus c. The estimate is exactly unbiased at every n, its relative error is 71%, and the base is a dial that trades one against the other at a rate of the square root of half of a minus one.

A counter that can reach nn needs log2(n+1)\lceil \log_2(n+1) \rceil bits, and there is no argument about it: the register has to hold a different value for each count, and n+1n+1 values need that many bits. For ten million events that is twenty-four bits.

Robert Morris had eight, on a machine in 1977 that had eight to spare, and needed to count events that would run into the millions. What he wrote instead is three lines long, holds five bits, and is the oldest structure in this field by two decades.

Keep a register cc, starting at zero. On each event, increment cc with probability 2c2^{-c} and otherwise leave it alone. At the end, report 2c12^c - 1.

Morris's counter: register bits against relative error, n = 20,000Each point is 200 runs of an approximate counter at one base, counting the same 20,000 events. The horizontal position is how many bits the register actually needed; the vertical is the measured relative standard deviation of the estimate. The line behind them is √((a−1)/2), predicted rather than fitted. At base 2 the counter fits in 5 bits and is out by 72.6%; at base 1.02 it needs 9 and is out by 10.3%. An exact counter for the same stream needs 15 bits and is never wrong, which is the trade stated in full.0.1bits the register neededrelative errora = 2a = 1.5a = 1.2a = 1.1a = 1.05a = 1.02√((a−1)/2), predicted200 runs per base · n = 20,000 · exact counter needs 15 bits72.6% at 5 bits
Fig. 1 Six versions of the counter, each run two hundred times over the same twenty thousand events. The horizontal position is how many bits the register actually needed; the vertical is the measured relative standard deviation of the estimate over those two hundred runs. The dashed line is (a1)/2\sqrt{(a-1)/2}, predicted rather than fitted. The exact counter for the same stream needs fifteen bits and is never wrong, which is the right-hand end of a trade this figure shows the rest of.

Why the estimate is unbiased, and it is one line

The claim is that E[2c1]=n\mathbb{E}[2^c - 1] = n exactly, at every nn, for every run length — not asymptotically and not approximately. It is worth doing because the argument is three symbols long and because it explains the shape of the algorithm.

Consider E[2c]\mathbb{E}[2^{c}] after each event. Given the current cc, the register goes to c+1c+1 with probability 2c2^{-c} and stays at cc otherwise, so

E[2cnewc]=2c(12c)+2c+12c=2c1+2=2c+1.\mathbb{E}[2^{c_{new}} \mid c] = 2^{c}\left(1 - 2^{-c}\right) + 2^{c+1}\cdot 2^{-c} = 2^{c} - 1 + 2 = 2^{c} + 1.

The probability of the step and the size of the step cancel exactly, and the expectation of 2c2^c rises by one per event whatever cc happens to be. Starting from 20=12^0 = 1, after nn events E[2c]=1+n\mathbb{E}[2^c] = 1 + n, so E[2c1]=n\mathbb{E}[2^c - 1] = n.

That cancellation is the design. The register is not counting events, it is counting doublings, and the increment probability is chosen so that each doubling is worth exactly as many events as it represents. Any other probability breaks the identity, and the general form makes the dial visible: increment with probability aca^{-c} and report (ac1)/(a1)(a^c - 1)/(a-1), and the same one-line argument gives E[ac]\mathbb{E}[a^{c}] rising by a1a-1 per event and the estimate unbiased again.

What it costs, and the number is small

At base 2 the register reaches roughly log2n\log_2 n, so it needs log2log2n\log_2 \log_2 n bits. For ten million events cc ends up near twenty-three, and holding twenty-three needs five bits against the exact counter’s twenty-four.

On the twenty-thousand-event stream measured here the largest cc observed over three hundred runs at base 2 was seventeen, which is five bits, against fifteen for the exact counter. That is a factor of three, and it is the smallest saving this field offers — the interesting property is not the ratio at one nn but that the ratio grows without limit. Doubling the stream adds one bit to the exact counter and, on average, nothing at all to Morris’s.

Morris's counter: register bits against relative error, n = 1,000,000Each point is 80 runs of an approximate counter at one base, counting the same 1,000,000 events. The horizontal position is how many bits the register actually needed; the vertical is the measured relative standard deviation of the estimate. The line behind them is √((a−1)/2), predicted rather than fitted. At base 2 the counter fits in 6 bits and is out by 36.4%; at base 1.05 it needs 8 and is out by 15.0%. An exact counter for the same stream needs 20 bits and is never wrong, which is the trade stated in full.bits the register neededrelative errora = 1.5a = 1.2a = 1.1a = 1.05√((a−1)/2), predicted80 runs per base · n = 1,000,000 · exact counter needs 20 bits36.4% at 6 bits
Fig. 2 A million events, with the base-2 version left out so the finer designs can be read. Every horizontal position here has moved by two or three bits against the twenty-thousand-event plate, which is what a fiftyfold increase in the stream costs a register that grows like logan\log_a n. Every vertical position is unchanged. That asymmetry is the structure: the accuracy is a property of the base, and the size is a property of the base and the logarithm of the stream.
6 runs of Morris counter, base 1.08 against the truth, 40,000 itemsEach thin line is one complete run with its own seed; the dashed line is the exact answer, recomputed at every point by machinery the estimator cannot reach. Both axes are logarithmic. The estimate is valid at every prefix rather than only at the end — nothing here knows how long the stream is — and the spread between the runs at any vertical slice is what 16 bits of state bought. At the end the runs land between -6.51% and 37.39% of the true 40,000.10,00010⁴items seencountexactMorris counter, base 1.08 · 16 bits · 6 runs · one passfinal spread -6.51% to 37.39%
Fig. 3 Six runs of the counter with the base set to 1.08, against the exact count, as forty thousand events go past. Every line is a complete run with its own seed. The estimate tracks the truth continuously rather than only at the end — the counter never knows how many events are coming — and the spread between the runs at any vertical slice is what the register bought. The steps are visible at the left-hand end, where the register is small and each increment is a large fraction of the estimate.

The 71% nobody quotes

Unbiased is a weak property, and Morris’s counter is the standing example of how weak. The estimate is right on average over runs and any individual run is nowhere near.

The variance of 2c12^c - 1 after nn events is n(n1)/2n(n-1)/2, so the relative standard deviation is about 1/2=70.7%1/\sqrt{2} = 70.7\%, independently of nn. Over three hundred runs at n=20,000n = 20{,}000 the measured figure is 70.3%, which is the prediction to within the noise of three hundred draws.

Seventy per cent is not an accuracy anybody would design for. A single run of the base-2 counter reporting eight thousand is entirely consistent with a true count of twenty thousand, and a caption quoting one such run would be quoting something close to noise. This is exactly the shape expected is not average is about, arriving in a place where the distinction decides whether the structure is usable: the expectation is perfect and the structure is not.

Which is why the textbook version is usually presented as a curiosity, and why the curiosity is only half the algorithm.

What the dial reaches, at both ends

The predicted relative standard deviation is (a1)/2\sqrt{(a-1)/2}, so pushing the base towards one should keep buying accuracy for bits without limit — and the check is whether the measured points stay on the line where the increments become almost continuous.

Morris's counter: register bits against relative error, n = 20,000Each point is 200 runs of an approximate counter at one base, counting the same 20,000 events. The horizontal position is how many bits the register actually needed; the vertical is the measured relative standard deviation of the estimate. The line behind them is √((a−1)/2), predicted rather than fitted. At base 2 the counter fits in 8 bits and is out by 15.9%; at base 1.01 it needs 10 and is out by 7.2%. An exact counter for the same stream needs 15 bits and is never wrong, which is the trade stated in full.100.1bits the register neededrelative errora = 1.05a = 1.02a = 1.01√((a−1)/2), predicted200 runs per base · n = 20,000 · exact counter needs 15 bits15.9% at 8 bits
Fig. 4 The three finest bases alone over the same twenty thousand events, two hundred runs each. At base 1.01 the register needs ten bits and the estimate is out by 7.2%, against fifteen bits for an exact count that is never wrong. The line behind the points is (a1)/2\sqrt{(a-1)/2}, predicted rather than fitted, and the points are still on it where the counter increments on nearly every event.

And the other end of the same dial is where the design stops being worth anything, which the arithmetic says before the plate does: the saving is in bits of register, and a short stream has few to save.

Morris's counter: register bits against relative error, n = 1,000Each point is 200 runs of an approximate counter at one base, counting the same 1,000 events. The horizontal position is how many bits the register actually needed; the vertical is the measured relative standard deviation of the estimate. The line behind them is √((a−1)/2), predicted rather than fitted. At base 2 the counter fits in 4 bits and is out by 67.6%; at base 1.02 it needs 8 and is out by 10.1%. An exact counter for the same stream needs 10 bits and is never wrong, which is the trade stated in full.0.1bits the register neededrelative errora = 2a = 1.5a = 1.2a = 1.1a = 1.05a = 1.02√((a−1)/2), predicted200 runs per base · n = 1,000 · exact counter needs 10 bits67.6% at 4 bits
Fig. 5 The same six bases over a thousand events rather than twenty thousand. Base 2 fits in four bits and is out by 67.6%; base 1.02 needs eight and is out by 10.1%; an exact counter needs ten and is never wrong. Two bits saved for a tenth of the answer is not a trade anybody wants, and it is the same trade that is worth taking at twenty thousand.

The base is the dial

Replacing 2 with a base aa closer to one makes the counter increment more often, so cc grows faster, so the register needs more bits — and each increment represents fewer events, so the estimate is finer. The variance works out to (a1)n(n1)/2(a-1)\,n(n-1)/2, giving a relative standard deviation of (a1)/2\sqrt{(a-1)/2}: at a=2a = 2 that is 70.7%, at a=1.1a = 1.1 it is 22.4%, at a=1.02a = 1.02 it is 10%.

The measurements agree, and the point of taking them is that the agreement is not automatic. A counter implemented with the increment probability at one base and the estimate arithmetic at another still runs, still fits its register, and reports a number with no relationship to the stream — which is the realistic form of the bug, because the base appears in two places and changing one of them is a one-line edit. The gate has that sabotage in it and requires the unbiasedness check to refuse it.

base measured relative error predicted (a1)/2\sqrt{(a-1)/2} register bits
2 70.3% 70.7% 5
1.5 48.6% 50.0% 5
1.1 21.8% 22.4% 7
1.02 10.1% 10.0% 9

Read down the last two columns and the exchange rate is there: 10% error costs nine bits where 70% costs five, and the exact answer costs fifteen. Halving the error costs about two bits each time, which is the same ε2\varepsilon^{-2} law every structure in this field obeys, arriving here in its cheapest possible setting — a single register, no hashing, and no assumption about the keys because the counter never looks at them.

Morris's counter: register bits against relative error, n = 200,000Each point is 150 runs of an approximate counter at one base, counting the same 200,000 events. The horizontal position is how many bits the register actually needed; the vertical is the measured relative standard deviation of the estimate. The line behind them is √((a−1)/2), predicted rather than fitted. At base 2 the counter fits in 5 bits and is out by 71.5%; at base 1.1 it needs 7 and is out by 23.7%. An exact counter for the same stream needs 18 bits and is never wrong, which is the trade stated in full.bits the register neededrelative errora = 2a = 1.5a = 1.1√((a−1)/2), predicted150 runs per base · n = 200,000 · exact counter needs 18 bits71.5% at 5 bits
Fig. 6 The same measurement at ten times the stream length. The errors are unchanged — the relative standard deviation of Morris’s counter does not depend on nn, which is the property that makes it a summary rather than a compromise — and the register requirement has moved by the two or three bits that a tenfold increase in nn costs. Everything horizontal in this figure grows like loglogn\log\log n; everything vertical does not grow at all.

What a register value means

It is tempting to read cc as a compressed version of the count, and the reading that actually explains the behaviour is different: cc is the number of times the counter has been surprised.

Each increment is a rarer and rarer event by construction. Going from cc to c+1c+1 takes, in expectation, aca^{c} events, so the register spends most of its life not moving. Over twenty thousand events at base 2 the register moves about fifteen times and stays put nineteen thousand nine hundred and eighty-five times. Almost every event costs a draw and produces nothing.

That is where the variance comes from, and it is worth locating precisely because it is not the usual place. The counter is not noisy because each increment is imprecise; each increment is exactly worth what the estimate says it is. It is noisy because the last increment is worth as much as everything before it put together. At base 2 the estimate is 2c12^c - 1, so a single extra step doubles the answer and a single missing step halves it — and whether that last step happened is one coin flip. A structure whose output is decided by the most recent doubling has a relative error of order one, and no amount of running it longer changes that.

Lowering the base fixes exactly this. At a=1.02a = 1.02 a single step moves the estimate by 2%, so the last step is worth 2% rather than 100%, and the register has to record fifty times as many steps to reach the same count. The bits and the error are the same quantity read two ways.

Concentration, and why the mean is the wrong summary

The variance gives a standard deviation; what a caller needs is a probability that a particular run is badly wrong. Chebyshev’s inequality turns the first into the second, weakly: with relative standard deviation σ\sigma, the chance of missing by more than kσk\sigma is at most 1/k21/k^2. At base 2 that is a guarantee of “within a factor of about three, nine times in ten”, which is not usable for anything.

There are two escapes and this field uses both. The first is to lower the base, which drives σ\sigma down at a cost in bits — the trade this essay measures. The second is to run several counters and combine them, which drives the failure probability down at a cost in bits without touching σ\sigma at all, and which turns out to be the general construction behind every guarantee in the field. The measured version of the second is in the estimate that is a median of means, and the two are worth keeping apart in the mind: one buys accuracy and the other buys confidence, and a bound quoting both is quoting two separate purchases.

The off-by-one that no measurement here can see

The estimate is 2c12^c - 1 and not 2c2^c. Dropping the minus one is a plausible slip: it makes the estimate high by exactly one at every nn, always.

At n=100,000n = 100{,}000 that is a bias of one part in a hundred thousand sitting inside a spread of 70.7%. Separating it from zero at four standard errors of the mean would need about 8×10108 \times 10^{10} runs. It is a real defect, it is provably present, and no measurement this site could afford will ever detect it.

That is worth stating plainly on a site whose method is measurement. The rule here is not that everything must be measured — it is that nothing may be asserted without a test it could fail, and the test can be an argument as well as a run. The minus one is earned by the derivation above, which is checkable in a way a hundred billion runs are not, and the gate asserts the thing that is measurable: that the trials needed to see the bias are astronomical, so that if someone later tightens the unbiasedness tolerance to a level the bias would trip, the check says so rather than failing mysteriously.

Where the randomness comes from, and what it costs

Each event costs one draw. On this site a random bit is a counted resource — counting the coin flips introduced the fourth counter for exactly this reason — and Morris’s counter is an unusual consumer of it: the randomness budget is Θ(n)\Theta(n) draws for a structure whose entire output is five bits.

Random bits consumed against n, k = 16Coin flips charged at the point they are spent, on logarithmic axes, with the class each consumer declares fitted by the same ratio test the comparison counts get. Rejections are charged: a draw below a range that is not a power of two costs whatever the rejection loop actually spent, not the ⌈log₂ k⌉ it would cost if the arithmetic were free. At n = 16,384 the consumers drawn here span 7,068 to 293,682 bits.10³10⁴10³10⁴10⁵nrandom bitsskip list, one build — nreservoir, Algorithm R — n log nreservoir, Algorithm L — log nn from 256 to 16,384bits charged including rejections
Fig. 7 The randomness field’s own accounting, for comparison. A skip list spends bits proportional to the number of keys it holds; reservoir sampling’s two versions spend Θ(nlogn)\Theta(n \log n) and Θ(klog2n)\Theta(k \log^2 n) for identical output. Morris’s counter sits with the first group and against the second: it draws once per event, forever, and no version of it draws less, because the decision it is making is per-event by construction.

There is a cheaper implementation for the base-2 case that is worth naming, because it shows what the draw is actually for. Incrementing with probability 2c2^{-c} is the same as flipping cc coins and requiring all heads, so an implementation can draw cc bits and test them, or — better — draw a geometric variate and skip ahead. Neither changes the distribution and both change the bit budget, which is the same shape as the two reservoir algorithms: identical output, different appetite for entropy.

The skip-ahead version is the more interesting of the two, because it makes the counter’s cost structure match its state. While cc is at fifteen the counter is going to sit still for about thirty-two thousand events, and drawing a geometric variate says so in one step instead of thirty-two thousand. A counter implemented that way spends Θ(logn)\Theta(\log n) draws in total rather than Θ(n)\Theta(n) — it does not even have to look at most of the events, only to know how many went by — and the estimate it produces is drawn from exactly the same distribution.

That is worth holding onto because it is the first appearance of a pattern the field repeats. A structure’s state, its accuracy, its randomness budget and its per-item work are four separate quantities, they are not tied together, and an algorithm can be improved along one of them without moving the others at all. The measurements in this field keep them apart deliberately: the plates report bits and error, and the per-item hash and draw counts are recorded beside them rather than folded in.

Almost mergeable, and the obstruction is the register

Every other structure in this field is asked whether two of them combine, and the counter’s answer is the most interesting of the set: the algebra works and the register cannot hold the result.

Two counters over two streams hold c1c_1 and c2c_2, with E[aci]=1+(a1)ni\mathbb{E}[a^{c_i}] = 1 + (a-1)n_i each. For a merged counter to be unbiased about n1+n2n_1 + n_2 it needs E[ac]=1+(a1)(n1+n2)\mathbb{E}[a^{c}] = 1 + (a-1)(n_1+n_2), which is exactly

ac  =  ac1+ac21a^{c} \;=\; a^{c_1} + a^{c_2} - 1

That is a closed form, it needs nothing but the two register values, and it is right. The trouble is that loga(ac1+ac21)\log_a(a^{c_1} + a^{c_2} - 1) is not an integer, and a register holds integers. Two base-2 counters both at fifteen would need a merged register at log2(2161)=15.99997\log_2(2^{16}-1) = 15.99997, and rounding it to sixteen or to fifteen breaks the identity that the whole structure rests on.

So the counter is mergeable in expectation and not representable, which is a failure mode none of the other structures in this field has — a maximum of two registers is a register, a sum of two cells is a cell, and only here does the correct answer fall between two states.

The repair is the algorithm’s own trick applied a second time. Round randomly: take the floor with the probability that makes the expectation come out right, and the ceiling otherwise. Since acac1+ac21aca^{\lfloor c \rfloor} \le a^{c_1} + a^{c_2} - 1 \le a^{\lceil c \rceil}, such a probability always exists and is one division to compute. The merged counter is then exactly unbiased again, and it has paid for it in variance — the rounding is one more coin flip whose outcome moves the estimate by a whole step, which at base 2 is a factor of two.

Two things follow that are worth having.

The variance cost is the base again. A randomised rounding at base 2 adds a step worth 100% of the estimate; at base 1.02 it adds one worth 2%. So the same dial that makes the counter accurate makes it mergeable cheaply, and the base-2 counter — the one everybody quotes — is the setting at which merging is most expensive.

And it explains why the descendants merge cleanly. A HyperLogLog register merges by maximum, which is closed on integers and needs no rounding at all; that is what exact mergeability is bought with. The counter’s estimator is a sum in the transformed domain and the register is a logarithm of it, and a sum of logarithms is not a logarithm of an integer — which is one sentence explaining why the structure that came first is the one that does not combine.

Two things it is not

It is not a compressed counter. Nothing here is reversible and there is no encoding involved. A compressed counter with five bits would still have to distinguish only thirty-two counts; this one distinguishes thirty-two scales, and gives up on distinguishing anything inside a scale. The distinction is the same one the coding field draws between a lossless coder and a summary, arriving in a setting where the object being summarised is a single integer.

It is not a sample. The counter does not look at a subset of the events and extrapolate; it looks at every one of them and makes a decision about each. Which is why it has no notion of a sampling rate and no dependence on the order the events arrive in, and why its error does not shrink when the stream gets longer — the thing being estimated grows at exactly the rate the estimator’s precision does not.

What it is actually for

Approximate counting in five bits is not a problem many programs have. The reason the structure is here is that everything later in this field is a variation on the identity above.

A HyperLogLog register is a Morris counter with the coin replaced by the hash of the key, which is what makes it count distinct keys rather than events — the same key hashes the same way, so it cannot advance the register twice. A Count-Min cell is a counter with the randomness moved from the increment to the address. The median-of-means construction in the estimate that is a median of means exists precisely to fix the 71%, and it fixes it for every estimator in the field at once, using an argument that has nothing to do with counting.

And the register is the first place the field’s central sentence can be checked rather than believed: the state is a fixed number of bits, chosen before the stream starts, and the answer is whatever those bits can support. Five bits support 71%. Nine bits support 10%. Fifteen bits support the truth, and the stream is allowed to be as long as it likes only in the first two cases.

What this makes readable

Essays that name this one as a prerequisite.

Named alongside this one

Essays reaching for the same objects. Nobody chose these; they are what the concept index makes visible.

What links here

Every essay whose body links to this one.

The objects this essay names

Each one links to every other essay that touches it.

EstimatorEstimator biasMorris's counterRandom bitsRandomised algorithmRelative errorSketchState bitsStreaming algorithmTrade offUnbiased estimatorVariance