HYPERLOGLOG
Count distinct items in kilobytes, whatever the cardinality
HyperLogLog keeps m = 64 registers, each holding the largest rank (position of the first 1 bit) it has seen in item hashes. All start at 0. You add 10 distinct items and apply the raw formula α·m²/Σ2^(−reg) with α = 0.709. About what does it estimate?
HyperLogLog hashes every item. The first p bits pick one of m = 2^p registers. In the remaining bits it finds the first 1: seeing a long run of zeros first is rare, so a large rank means many distinct items. Each register keeps the largest rank it has seen.
The estimate is α·m² divided by Σ2^(−reg), a harmonic mean across registers. Its standard error is about 1.04/√m. When most registers are still 0, that formula is biased high, so for small counts HyperLogLog switches to linear counting: m·ln(m/zeros).
HYPERLOGLOG (m = 2^p registers)
ADD(x):
h = hash32(x)
idx = top p bits of h
rank = position of the first 1 in the other 32 − p bits (1-based)
reg[idx] = max(reg[idx], rank)
ESTIMATE():
Z = Σ 2^(−reg[j])
E = α_m · m² / Z α_16 = 0.673, α_32 = 0.697, α_64 = 0.709,
α_m = 0.7213 / (1 + 1.079/m) for m ≥ 128
if E ≤ 2.5m and V = (registers equal to 0) > 0:
E = m · ln(m / V) linear counting (small-range correction)
if E > 2^32 / 30: E = −2^32 · ln(1 − E / 2^32) (32-bit hash only)
Standard error ≈ 1.04 / √m. p = 14: m = 16384, 6-bit registers,
12 KB, about 0.81%. Repeats never change a register: it counts distinct.
Merge two sketches by taking the max of each register. Watch ESTIMATE and the invariant strip. RAW ESTIMATE ONLY reports 50 for 10 items and fails the 3 × 1.04/√m bound until enough registers fill. The fix uses the same registers and the same items and stays inside the bound from 10 to 5000. The repeat batch changes no register in either. Drag REGISTERS down to 4 or 8 and the bound breaks at 5000 items even with the fix: with so few registers the estimate has high variance, and the paper’s constants assume m ≥ 16.
Each register is 6 bits and your budget is 384 bytes (at most 512 registers). Find a size within budget whose worst error over the whole run, from 10 to 5000 distinct items, stays under 10%. The error shrinks like 1/√m, so halving it costs four times the memory.
GOAL: worst error < 10% with at most 512 registers
Implement add(item) and estimate() for m = 2^p registers. hash32(item) returns an unsigned 32-bit integer; alpha is α_m. The grader checks small and large cardinalities at p = 8 (allowed error 3 × 1.04/√256 ≈ 19.5%) and that repeats do not change the estimate.
Redis PFADD/PFCOUNT (16384 registers, 12 KB), approximate distinct-count functions in BigQuery, Presto/Trino, Spark and Druid, and unique-visitor or unique-IP dashboards. The symptom of a missing small-range correction is small counts that are off by multiples while large ones look fine.
“Count unique visitors per page per day for a billion pages.” Explain register index and rank, the harmonic-mean estimate and its 1.04/√m error, why repeats are free, the small-range switch to linear counting, and that sketches merge by register-wise max so you can roll days up into weeks.