Sub topic 5
One-sided error: no false negatives
This chapter assumes you know how a Bloom filter inserts and queries items. That means k hash positions per item in an m-bit array, as in the earlier chapters. It also assumes you can read Python bitwise operators.
The guarantee
A Bloom filter can be wrong in only one direction:
- If
might_contain(x)returnsFalse, thenxwas never added. This answer is always correct. - If
might_contain(x)returnsTrue, thenxwas probably added. This answer can be wrong. The next chapter covers why.
A wrong False for an item that was added is a false negative. A correct Bloom filter never produces one.
Why the guarantee holds
The proof rests on two facts about the structure.
- Insertion sets bits to 1. It never sets a bit to 0.
- Hashing is deterministic. The same item always produces the same k positions.
Suppose you add x. Insertion sets the k positions for x to 1. Later insertions can set more bits to 1, but nothing ever clears one. When you query x, the filter computes the same k positions and finds all of them set. So the query returns True.
Querying works as a one-way test. A single 0 bit proves that x was never inserted, because insertion would have set that bit. All 1s proves nothing, because other items may have set those bits.
Checking it in code
This uses the BloomFilter class from the spine. It inserts 10,000 items, then queries every one of them.
import hashlib
import math
class BloomFilter:
def __init__(self, expected_items, false_positive_rate):
self.m = math.ceil(-expected_items * math.log(false_positive_rate) / (math.log(2) ** 2))
self.k = max(1, round((self.m / expected_items) * math.log(2)))
self.bits = bytearray((self.m + 7) // 8)
def _positions(self, item):
digest = hashlib.sha256(item.encode("utf-8")).digest()
h1 = int.from_bytes(digest[:8], "big")
h2 = int.from_bytes(digest[8:16], "big") | 1
return [(h1 + i * h2) % self.m for i in range(self.k)]
def add(self, item):
for pos in self._positions(item):
self.bits[pos // 8] |= 1 << (pos % 8)
def might_contain(self, item):
return all(self.bits[pos // 8] & (1 << (pos % 8)) for pos in self._positions(item))
bf = BloomFilter(10_000, 0.01)
items = [f"user-{i}" for i in range(10_000)]
for item in items:
bf.add(item)
false_negatives = [item for item in items if not bf.might_contain(item)]
print(len(false_negatives)) # always 0The count is 0 on every run, for any item set and any filter size. Try BloomFilter(100, 0.01) with the same 10,000 items. The filter is badly overfilled, but the count is still 0. Overfilling raises the false positive rate. It does not break the guarantee.
How the guarantee breaks
The guarantee holds only if the implementation keeps the two facts above. These mistakes produce false negatives:
- Clearing bits. Any attempt to delete an item by zeroing its bits can erase bits that other items share. The deletion chapter returns to this.
- Non-deterministic hashing. Python's built-in
hash()for strings is randomized per process. A filter saved to disk and loaded in a new process computes different positions for the same item. Use a stable hash such as SHA-256, as the class above does. - Mismatched parameters. Querying with a different m, k, or hash scheme than the one used for insertion gives different positions.
- Corrupted storage. A truncated file or flipped bits can turn a 1 back into a 0.
Why this matters for design
The one-sided error decides where you can safely use a filter. A "no" can be acted on without checking anything else. A "yes" must be confirmed by a slower, authoritative lookup. A typical pattern is to ask the filter first and consult the database only on "maybe". The filter then removes most lookups for absent items and never hides a present one.
Exercise
A Bloom filter is loaded from a file written by a previous run of your program. It reports False for an item you are certain was added before the file was saved. Name two likely causes.
Show answer
Likely causes are a hash that is not stable across processes (for example Python's randomized built-in hash()), different m, k, or hashing parameters at load time than at insertion time, and corrupted or truncated stored bits. A correct Bloom filter with matching parameters cannot give a false negative, so the cause is a bug or data damage.