Sub topic 6
How false positives arise
This chapter assumes you know the insert and lookup steps from the previous chapters. You should also be comfortable with bitwise operations and with the idea of independent probabilities.
Bits are shared
A Bloom filter stores no items. It stores only a bit array of m bits. Each inserted item sets k bits, and each bit can be set by many different items. The filter does not record which item set which bit.
A lookup checks whether all k positions for the queried item are 1. If another item, or a combination of several items, has already set those positions, the lookup passes for an item that was never inserted. That is a false positive.
Bit 5 is set by both A and B. C needs bits 1, 5 and 7. B set bits 1 and 7, and A (or B) set bit 5, so the lookup for C finds all three set.
What controls the error
A false positive needs k bits to be set by coincidence. The chance of that grows with the fill ratio, which is the fraction of bits that are 1.
After n insertions, each of the kn bit-setting operations hits a given bit with probability 1/m. The probability that a particular bit is still 0 is therefore:
(1 - 1/m)^(k*n) ≈ e^(-k*n/m)So the fill ratio is about 1 - e^(-kn/m). A query for a non-member passes if all k of its positions are set. If the positions behave independently, the false positive rate is roughly the fill ratio raised to the power k. The next chapter turns this into the standard estimate.
Three things follow:
- More items (
nup) fill more bits and raise the error. - More bits (
mup) lower the fill ratio and the error. - More hashes (
kup) make each lookup stricter, but they also set more bits per insert. The bestkbalances these effects, which a later chapter covers.
Measuring it
This script builds a deliberately small filter, m = 64 and k = 3, and inserts 20 items. It then queries 10,000 strings that were never inserted.
import hashlib
M, K = 64, 3
def positions(item):
digest = hashlib.sha256(item.encode("utf-8")).digest()
return [int.from_bytes(digest[4 * i:4 * i + 4], "big") % M for i in range(K)]
bits = [0] * M
for i in range(20):
for p in positions(f"member-{i}"):
bits[p] = 1
def might_contain(item):
return all(bits[p] for p in positions(item))
fill = sum(bits) / M
trials = 10_000
false_hits = sum(might_contain(f"other-{i}") for i in range(trials))
print(f"fill ratio: {fill:.2f}")
print(f"false positive rate: {false_hits / trials:.2f}")The formula predicts a fill ratio near 0.6 (1 - e^(-60/64)). The false positive rate should be near 0.2 (0.6^3). Your exact numbers vary slightly, but they should be close to these. This filter is badly overloaded, which is why the error is so high.
Common mistakes
- Treating
Trueas proof. A positive means "possibly present." If a wrong positive is costly, confirm it against the real data store. - Inserting far more than the planned
n. A filter sized for 1,000 items and fed 100,000 ends up with almost every bit set. It then answersTruefor nearly everything. It does not fail with an error, so you must track the item count yourself. - Weak or correlated hashes. The estimate assumes positions are uniform and independent. If two different hash functions often return the same position, an item effectively uses fewer than
kbits and the error rises. - Expecting false positives to be random per query. The same non-member gives the same wrong answer every time, because its positions never change. Retrying the query does not help.
Exercise
A filter has m = 1,000 bits and k = 2. After 300 insertions, estimate the fill ratio and the false positive rate.
Show answer
Fill ratio ≈ 1 - e^(-kn/m) = 1 - e^(-0.6) ≈ 1 - 0.549 = 0.451. False positive rate ≈ 0.451^2 ≈ 0.20, so about 20%.