Sub topic 4
Splitting the stream across registers
This chapter assumes you know how a hash gives uniform bits, and that the rank of a hash is the position of its first 1-bit. You should also be comfortable with Python bitwise operators. Here you will see why one rank is a poor estimator and how splitting the stream across many registers fixes that.
The problem with a single register
The previous idea was simple. Hash every item and track the largest rank seen. If the maximum rank is r, the stream probably held about 2^r distinct items.
This works on average, but a single maximum is very noisy. One unlucky item with a rank of 20 makes you estimate about a million items, even if the stream held only a few hundred. A single rare hash can also be missing. The estimate can only be a power of two, so it jumps in large steps.
You need many independent measurements to average out this noise. Hashing each item many times would be costly. Stochastic averaging gets the same effect from one hash.
Using one hash twice
Stochastic averaging splits the hash into two parts:
- The first
pbits choose a register. There arem = 2^pregisters. - The remaining
64 - pbits give the rank.
Each register keeps the largest rank among the items routed to it. Because the hash is uniform, each register receives about n / m of the distinct items. So each register is a small, independent estimator of a stream that is m times smaller.
Duplicates cause no harm. The same item always hashes to the same register with the same rank, so adding it again cannot raise the maximum.
Seeing it in code
This script routes 10,000 distinct items into 16 registers.
import hashlib
def hash64(item):
digest = hashlib.sha1(item.encode("utf-8")).digest()
return int.from_bytes(digest[:8], "big")
def split_hash(x, p):
bits = 64 - p
index = x >> bits # first p bits
rest = x & ((1 << bits) - 1) # remaining bits
rank = bits - rest.bit_length() + 1 # position of the first 1-bit
return index, rank
p = 4
registers = [0] * (1 << p)
for i in range(10_000):
index, rank = split_hash(hash64(f"user-{i}"), p)
registers[index] = max(registers[index], rank)
print(registers)Each register sees roughly 10,000 / 16 = 625 items. Since log2(625) is about 9.3, most registers should hold values near 9 or 10. A few will be lower or higher. That spread is the noise, and combining the registers is what reduces it.
Why more registers means less error
Averaging m roughly independent measurements shrinks the relative error by a factor of about the square root of m. HyperLogLog's standard error is about 1.04 / sqrt(m).
| p | Registers (m) | Standard error |
|---|---|---|
| 4 | 16 | about 26% |
| 10 | 1,024 | about 3.25% |
| 12 | 4,096 | about 1.63% |
| 14 | 16,384 | about 0.81% |
Quadrupling the registers halves the error. Memory grows linearly with m, so accuracy gets more expensive as you demand more of it. A later chapter covers how to choose p.
What you can combine and what you can't
You can't simply take the arithmetic mean of the register values. One large rank would still dominate the result, because an estimate grows exponentially with rank. The next chapter explains how a harmonic mean handles this.
Two details matter in practice:
- Index bits are spent. The first
pbits are used for routing, so only64 - pbits remain for the rank. With 64-bit hashes this is plenty, since ranks rarely exceed about 50. - Registers start at 0. A register that has seen no items still holds 0. At small counts many registers are empty, which skews the estimate. This is the reason for the small-range correction later.
Exercise
A sketch uses p = 10 and a 64-bit hash. How many registers does it have, how many bits remain for computing the rank, and roughly how many distinct items does each register see when the stream has 512,000 distinct items?
Show answer
It has 2^10 = 1,024 registers. The index uses 10 bits, so 54 bits remain for the rank. Each register sees about 512,000 / 1,024 = 500 distinct items.
Exercise
Why does adding the same item a thousand times leave the sketch unchanged after the first time?
Show answer
The hash is deterministic, so the item always maps to the same register with the same rank. A register only changes when a new rank exceeds its current value, and the repeated rank never does.