Sub topic 7
Estimating the false positive rate
This chapter assumes you know how a Bloom filter inserts and queries items, and that you are comfortable with independent events and with the approximation (1 - x/N)^N ≈ e^(-x). It builds the formula that later chapters use to choose m and k.
What we want to estimate
A false positive happens when you query an item that was never added, and all k of its bit positions are already 1 because other items set them. The false positive rate (FPR) is the probability of that event for a random absent item. It depends on three numbers:
- m, the number of bits
- k, the number of hash functions
- n, the number of items inserted so far
Deriving the estimate
Assume each hash function picks every position with equal probability, independently of the others. The derivation then has three steps.
- One hash sets one specific bit with probability 1/m. So that bit stays 0 after one hash with probability 1 - 1/m.
- Inserting n items makes kn hash calls. A given bit is still 0 with probability (1 - 1/m)^(kn), which is approximately e^(-kn/m).
- A given bit is therefore 1 with probability 1 - e^(-kn/m). A query for an absent item needs all k of its positions to be 1, so the FPR is:
The result is p ≈ (1 - e^(-kn/m))^k.
The quantity 1 - e^(-kn/m) is the expected fraction of bits that are set, also called the fill ratio. The FPR is the fill ratio raised to the power k.
Checking the formula in code
This script computes the estimate. It also measures the real rate by simulating random bit positions, so the result does not depend on a particular hash function.
import math
import random
def estimated_fpr(m, k, n):
return (1 - math.exp(-k * n / m)) ** k
def simulate_fpr(m, k, n, trials=100_000, seed=1):
rng = random.Random(seed)
bits = bytearray(m)
for _ in range(n * k):
bits[rng.randrange(m)] = 1
hits = 0
for _ in range(trials):
if all(bits[rng.randrange(m)] for _ in range(k)):
hits += 1
return hits / trials
m, k = 10_000, 7
for n in (500, 1000, 1500):
print(n, round(estimated_fpr(m, k, n), 4), simulate_fpr(m, k, n))The estimates are about 0.0002 for n = 500, 0.0082 for n = 1000, and 0.049 for n = 1500. The simulated values land close to these, with some noise at the smallest rate because few trials produce a hit.
What the numbers tell you
The FPR grows quickly as n grows. In the example, the filter holds 3 times as many items at n = 1500 as it does at n = 500, but the FPR is about 250 times higher. A filter sized for 1000 items at about 1% error is no longer a 1% filter at 1500 items.
The two ways to lower the FPR work through different terms:
- Increasing m lowers the fill ratio, which helps every query.
- Increasing k adds more bits that must all match, but it also sets more bits per insertion. Past a certain point, the extra bits raise the fill ratio faster than the extra matches help. The next chapter finds the best k.
Common errors
- Treating the formula as exact. It assumes independent, uniform hashes. Bit positions are not truly independent, so the real FPR is slightly higher than the estimate, and the gap is noticeable for small m. For large filters it is negligible.
- Using the planned n instead of the actual n. The FPR depends on how many items are in the filter now. Track the insert count and recompute.
- Ignoring duplicates. Adding the same item twice sets the same bits, so the real fill ratio can be lower than the formula predicts if you count duplicate inserts toward n.
- Using a weak hash. If hash outputs cluster, the observed rate can be much worse than the estimate.
Exercise
A Bloom filter has m = 8000 bits and k = 5 hash functions, and it holds n = 1000 items. Estimate the false positive rate.
Show answer
kn/m = 5 × 1000 / 8000 = 0.625. The fill ratio is 1 - e^(-0.625) ≈ 1 - 0.5353 = 0.4647. Raising it to the 5th power gives about 0.022, so the false positive rate is roughly 2.2%.