Sub topic 6
Correcting small counts with linear counting
This chapter assumes you have read the earlier chapters on ranks, registers, and the harmonic mean estimator. It also assumes you know basic Python and that a hash maps each item to a uniform bit string.
Why the raw estimate fails for small sets
The raw HyperLogLog estimate is alpha * m * m / sum(2 ** -r). It is built for sets much larger than the number of registers m. When few items have been added, most registers are still 0, and each empty register adds 2 ** 0 = 1 to the sum.
Take an empty sketch with m = 4096. The sum is 4096 and alpha is about 0.7211, so the raw estimate is about 2953. The true count is 0. The estimate is badly wrong, and it stays biased upward until the set holds a few times m distinct items.
Linear counting
When many registers are empty, there is a better signal: how many are empty. Each item lands in one of m registers with equal probability, as if you were throwing balls into bins. After n distinct items, the expected fraction of empty registers is about e^(-n/m).
Let V be the number of registers that are still 0. Then V / m ≈ e^(-n/m). Solving for n gives the linear counting estimate:
n ≈ m * ln(m / V)Here is an example with m = 4096 and 1000 distinct items. The expected number of empty registers is 4096 * e^(-1000/4096), about 3209. Plugging that back in gives 4096 * ln(4096 / 3209), which is about 1000.
If V = m, the sketch is empty and the formula gives ln(1) = 0, which is correct.
When to switch
The standard rule from the original HyperLogLog paper compares the raw estimate with 2.5 * m:
- Compute the raw estimate.
- If it is at most
2.5 * mand at least one register is 0, returnm * ln(m / V). - Otherwise return the raw estimate.
The check for at least one empty register matters. If V = 0, then ln(m / V) divides by zero. In that case the sketch is already filling up and the raw estimate is the right one.
Seeing the correction work
This script is self-contained. It builds sketches of increasing size and prints both estimates.
import hashlib
import math
def hash64(item):
digest = hashlib.sha1(item.encode("utf-8")).digest()
return int.from_bytes(digest[:8], "big")
def build(items, p=12):
registers = [0] * (1 << p)
bits = 64 - p
for item in items:
x = hash64(item)
index = x >> bits
rest = x & ((1 << bits) - 1)
rank = bits - rest.bit_length() + 1
registers[index] = max(registers[index], rank)
return registers
def raw_estimate(registers):
m = len(registers)
alpha = 0.7213 / (1 + 1.079 / m)
return alpha * m * m / sum(2.0 ** -r for r in registers)
def corrected_estimate(registers):
m = len(registers)
raw = raw_estimate(registers)
zeros = registers.count(0)
if raw <= 2.5 * m and zeros:
return m * math.log(m / zeros)
return raw
for n in [0, 100, 1000, 5000, 20000]:
regs = build(f"user-{i}" for i in range(n))
print(n, round(raw_estimate(regs)), round(corrected_estimate(regs)))For n = 0 and n = 100, the raw column stays near 3000, while the corrected column gives 0 and a value close to 100. Around n = 1000 the raw estimate is still too high, and linear counting tracks the truth. At n = 20000 the two columns agree closely, because the raw estimate is above 2.5 * 4096 = 10240 and the code returns it unchanged.
Common mistakes
- Applying the correction to every estimate. Linear counting is poor when few registers are empty.
- Checking only
raw <= 2.5 * m. You also needzeros > 0to avoid a division by zero. - Using
math.logwith the wrong ratio. The argument ism / V, which is at least 1, so the result is not negative. - Expecting the switch to be seamless. There can be a small jump in the estimate where the two formulas hand over. Production variants smooth this out with bias correction, covered in a later chapter.
Exercise
A sketch has m = 1024 registers. Its raw estimate is 1500, and 600 registers are still 0. Which estimate should you return, and what is its value?
Show answer
The raw estimate 1500 is below 2.5 1024 = 2560, and zeros is not 0, so use linear counting. The result is 1024 ln(1024 / 600), which is about 547.