Sub topic 5
Combining registers with a harmonic mean
This chapter assumes you know how a hash is split into a register index and a rank, as covered in the previous chapter. It also assumes basic Python and a rough idea of means and expected values.
The problem: many noisy registers
After add has processed a stream, each of the m registers holds the largest rank it has seen. A register with value M suggests that about 2^M distinct items landed in it. That suggestion is noisy. One unlucky hash with many leading zeros can push a register far above what its share of the items justifies.
We need one number for the whole set, so we have to combine the m register values.
Why not the arithmetic mean
The obvious choice is to average 2^M over all registers and multiply by m. The trouble is that 2^M grows exponentially. A single outlier register dominates the average.
registers = [3, 4, 3, 3, 4, 3, 3, 20]
m = len(registers)
arithmetic = sum(2 ** r for r in registers) / m
harmonic = m / sum(2.0 ** -r for r in registers)
print(f"arithmetic mean of 2^M: {arithmetic:.2f}")
print(f"harmonic mean of 2^M: {harmonic:.2f}")The output is 131081.00 for the arithmetic mean and 10.67 for the harmonic mean. Seven registers say "around 8 to 16", and one register says "about a million". The arithmetic mean follows the outlier. The harmonic mean mostly ignores it.
The harmonic mean
The harmonic mean of values x_1 ... x_m is m / (1/x_1 + ... + 1/x_m). It averages the reciprocals and then inverts the result. Large values have small reciprocals, so they add almost nothing to the sum. Small values dominate the sum.
For registers, x_j = 2^M_j, so the reciprocal is 2^-M_j. That is exactly the term in the code from the implementation chapter: sum(2.0 ** -r for r in self.registers).
The full formula
The raw estimate is:
E = alpha_m * m^2 / sum(2^-M_j)Read it in two steps.
m / sum(2^-M_j)is the harmonic mean of2^M_j. It estimates the distinct count of one register's share of the stream.- Multiplying by
mscales that share up to the whole stream. This works because the hash spreads items evenly across registers.
The constant alpha_m corrects a systematic bias. Even the harmonic mean of 2^M does not equal the true per-register count, and the gap depends on m. For m >= 128, the standard approximation is:
alpha_m = 0.7213 / (1 + 1.079 / m)For smaller m, the published constants are 0.673 for 16 registers, 0.697 for 32 and 0.709 for 64. For p = 12 (m = 4096), alpha is about 0.7211. As m grows, alpha approaches 0.7213.
Applying it to a real sketch
This code reuses the HyperLogLog class from the implementation chapter. It prints the raw estimate for 10,000 items.
sketch = HyperLogLog(p=12)
for i in range(10_000):
sketch.add(f"user-{i}")
s = sum(2.0 ** -r for r in sketch.registers)
raw = sketch.alpha * sketch.m ** 2 / s
print(round(raw))The result is close to 10000, within the expected error of about 1.6%.
Where the formula breaks down
The formula has a weak spot at low counts. An empty register has M = 0, so it contributes 2^0 = 1 to the sum. A few items spread over 4096 registers leave most registers empty, and the formula then reports a number that is too high. This is why estimate switches to linear counting when raw <= 2.5 * m and some registers are still zero. The next chapter covers that correction.
Common errors
- Averaging the registers directly, such as
sum(registers) / m. This averages the exponents. It is a geometric-style mean of the counts, and it is biased. - Leaving out
alpha. The estimate would be about 28% too high (1 / 0.7213 is about 1.39, so it is too high by roughly 39% relative to the corrected value, depending onm). - Forgetting the factor
monce, so the estimate is a single register's share. Check that the numerator containsm * m.
Exercise
A sketch has m = 16 registers, and every register holds the value 3. Using alpha = 0.673, what is the raw estimate?
Show answer
The sum is 16 2^-3 = 2. The estimate is 0.673 16 * 16 / 2 = 86.144, so about 86 distinct items.
Exercise
Why does one register with rank 20 barely change the harmonic mean, while it dominates the arithmetic mean of 2^M?
Show answer
Its reciprocal 2^-20 is about 0.000001, which adds almost nothing to the sum of reciprocals. Its value 2^20 is about a million, which is much larger than the other values and dominates their arithmetic average.