Sub topic 8
Sizing a filter: choosing m and k
This chapter assumes you know how a Bloom filter inserts and queries items, and that you can read the approximate false positive formula. Here you will turn a requirement ("1 million items, 1% false positives") into concrete values for the bit count m and the hash count k.
The two inputs and the two outputs
You choose the inputs. The formulas produce the outputs.
nis the number of items you expect to insert.pis the false positive rate you can tolerate.mis the number of bits in the array.kis the number of bit positions set per item.
Recall the approximation for the false positive rate after inserting n items:
p ≈ (1 − e^(−kn/m))^k
Sizing means choosing m and k so that this value meets your target as cheaply as possible.
The standard formulas
For a given m and n, the false positive rate is lowest when about half of the bits are set. That happens at:
k = (m / n) · ln 2
Substituting this k into the approximation and solving for m gives:
m = −n · ln(p) / (ln 2)²
So the memory you need grows linearly with n. It grows only with the logarithm of 1/p.
Computing the parameters
import math
def bloom_parameters(n, p):
"""Return (m, k) for n expected items and target false positive rate p."""
m = math.ceil(-n * math.log(p) / (math.log(2) ** 2))
k = max(1, round((m / n) * math.log(2)))
return m, k
m, k = bloom_parameters(1_000_000, 0.01)
print(m, k) # about 9.6 bits per item, so m is near 9.6 million, and k is 7The code rounds m up and k to the nearest whole number, because both must be integers. max(1, ...) keeps k at least 1 for very loose targets.
Because of rounding, check the result with the approximation:
import math
def false_positive_rate(m, k, n):
return (1 - math.exp(-k * n / m)) ** k
print(false_positive_rate(9_585_059, 7, 1_000_000)) # about 0.0100For 1 million items at 1%, the filter needs about 9.6 million bits, which is roughly 1.2 MB.
How the numbers scale
| Target rate p | Bits per item (m/n) | Hash count k |
|---|---|---|
| 10% | 4.8 | 3 |
| 1% | 9.6 | 7 |
| 0.1% | 14.4 | 10 |
| 0.01% | 19.2 | 13 |
Each tenfold drop in p costs about 4.8 more bits per item. Going from 10% to 0.01% is a thousandfold improvement for about four times the memory.
Common errors
Underestimating n is the most frequent mistake. A filter sized for 1 million items and given 2 million has far more bits set than planned, so its false positive rate rises well above the target. The filter does not warn you when this happens. Size for the largest count you expect, or rebuild the filter when the count grows.
A second mistake is picking k by habit. A k that is too large fills the array with set bits too fast. A k that is too small gives each item too little evidence. Both raise p for a fixed m.
Exercise
You need a Bloom filter for 500,000 items with a false positive rate of about 0.1%. Roughly how many bits and how many hash functions do you need? Use the table above.
Show answer
At 0.1%, the table gives about 14.4 bits per item and k = 10. That is 500,000 × 14.4 ≈ 7.2 million bits, or about 0.9 MB, with 10 hash functions.
Exercise
A filter was sized for n = 1,000,000 and p = 1%, but 2,000,000 items were inserted. Is the false positive rate still 1%? Which formula would you use to estimate the new rate?
Show answer
No, it is much higher. Use p ≈ (1 − e^(−kn/m))^k with the original m and k (9,585,059 and 7) and the actual n = 2,000,000. This gives roughly 0.18, or about 18%.