Sub topic 7
Choosing precision: memory versus accuracy
This chapter assumes you know how a HyperLogLog sketch uses registers, rank, and a harmonic mean to estimate a distinct count. It also assumes basic Python and a rough sense of standard deviation. Here you will learn how the precision parameter p sets both memory use and error, and how to pick p for a budget or an accuracy target.
What p controls
A sketch has m = 2^p registers. The first p bits of each hash choose the register. Each register stores a rank, which is at most 64 - p + 1. That value is at most 61, so it fits in 6 bits.
Raising p by 1 doubles the number of registers. More registers means each one sees fewer items, and the average over registers is more stable.
The standard error of the estimate is:
standard error ≈ 1.04 / sqrt(m)Doubling m divides the error by only sqrt(2), about 1.41. To halve the error you must quadruple the memory. This is why accuracy gets expensive quickly.
Memory and error table
The memory figures below use 6 bits per register. The code in this tutorial stores each register in a Python integer, which takes far more space. The table describes a packed implementation such as Redis uses.
| p | Registers (m) | Memory at 6 bits each | Standard error |
|---|---|---|---|
| 4 | 16 | 12 bytes | 26.00% |
| 8 | 256 | 192 bytes | 6.50% |
| 10 | 1,024 | 768 bytes | 3.25% |
| 12 | 4,096 | 3 KB | 1.63% |
| 14 | 16,384 | 12 KB | 0.81% |
| 16 | 65,536 | 48 KB | 0.41% |
Redis uses p = 14, so a dense sketch takes about 12 KB and has an error of about 0.81%. That holds whether the sketch counts a thousand items or a billion.
Choosing p from a target error
Solve the error formula for m:
m ≥ (1.04 / target_error)^2
p = ceil(log2(m))For a 1% target, m ≥ (1.04 / 0.01)^2 = 10,816. The next power of two is 16,384, so p = 14.
The helper below applies this rule in both directions.
import math
def precision_for_error(target_error):
"""Smallest p in 4..16 whose standard error is at most target_error."""
m_needed = (1.04 / target_error) ** 2
p = math.ceil(math.log2(m_needed))
if p > 16:
raise ValueError("target error needs p above 16")
return max(p, 4)
def precision_for_memory(budget_bytes):
"""Largest p in 4..16 that fits in budget_bytes at 6 bits per register."""
p = int(math.floor(math.log2(budget_bytes * 8 / 6)))
return min(max(p, 4), 16)
def standard_error(p):
return 1.04 / math.sqrt(1 << p)
print(precision_for_error(0.01)) # 14
print(precision_for_memory(4096)) # 12
print(f"{standard_error(12):.2%}") # 1.63%A 4096-byte budget holds 5,461 registers at 6 bits each. The largest power of two that fits is 4,096, so p = 12.
What "standard error" means
The standard error is one standard deviation of the relative error. For a sketch with p = 14, about 68% of estimates fall within 0.81% of the true count. About 95% fall within roughly twice that, or 1.6%. If you need a hard guarantee, plan for two or three standard errors instead of one.
The error is relative to the true count. At 10 million distinct items, 0.81% is about 81,000 items. If you need absolute accuracy at small counts, the small-range correction from the previous chapter helps. For counts below a few thousand, an exact set may use less memory than a sketch.
Practical guidance
- Start from the error your use case tolerates. Dashboard visitor counts rarely need better than 2%, so
p = 12is often enough. - Multiply by the number of sketches. One sketch per user, per page, or per day multiplies the memory. A 12 KB sketch times a million keys is 12 GB.
- Match
pacross sketches you plan to merge. Merging requires equal precision, as themergemethod enforces. - Prefer a larger
ponly when the error matters. Each extra step doubles the memory for a gain of about 29% in error.
Exercise
You must count distinct visitors per page for 200,000 pages, with a total budget of 1 GB and the best accuracy that fits. Assume 6 bits per register and ignore overhead. Which p do you choose, and what is the standard error?
Show answer
The budget per page is 1,000,000,000 / 200,000 = 5,000 bytes, which is 40,000 bits and 6,666 registers at 6 bits each. The largest power of two not above 6,666 is 4,096, so p = 12 (3 KB per sketch, 600 MB in total). The standard error is 1.04 / sqrt(4096) = 1.04 / 64 ≈ 1.63%. The next step, p = 13, needs 8,192 registers and about 6 KB per page, or 1.2 GB in total, which exceeds the budget.
Exercise
A product owner wants distinct counts within 0.5% standard error. What is the smallest p that meets this, and how much memory does a packed sketch need?
Show answer
m ≥ (1.04 / 0.005)^2 = 208^2 = 43,264. The next power of two is 65,536, so p = 16. At 6 bits per register that is 65,536 × 6 / 8 = 49,152 bytes, or 48 KB. The actual standard error is about 0.41%.