Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 7629686

Browse files
committed
from fail to erro
1 parent e4d39db commit 7629686

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

data_structures/hashing/bloom_filter.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,30 +73,29 @@ def random_string(size):
7373
def test_probability(m=64, n=20):
7474
b = Bloom(size=m)
7575

76+
k = len(b.HASH_FUNCTIONS)
77+
estimated_error_rate_beforehand = (1 - (1 - 1 / m) ** (k * n)) ** k
78+
7679
added = {random_string(10) for i in range(n)}
7780
for a in added:
7881
b.add(a)
7982

80-
k = len(b.HASH_FUNCTIONS)
81-
8283
n_ones = bin(b.bitstring).count("1")
83-
expected_probability = (n_ones / m) ** k
84-
85-
expected_probability_wikipedia = (1 - (1 - 1 / m) ** (k * n)) ** k
84+
estimated_error_rate = (n_ones / m) ** k
8685

8786
not_added = {random_string(10) for i in range(1000)}
88-
fails = 0
87+
errors = 0
8988
for string in not_added:
9089
if b.exists(string):
91-
fails += 1
92-
fail_rate = fails / len(not_added)
90+
errors += 1
91+
error_rate = errors / len(not_added)
9392

94-
print(f"total = {len(not_added)}, fails = {fails}, fail_rate = {fail_rate}")
95-
print(f"{expected_probability=}")
96-
print(f"{expected_probability_wikipedia=}")
93+
print(f"total = {len(not_added)}, errors = {errors}, error_rate = {error_rate}")
94+
print(f"{estimated_error_rate=}")
95+
print(f"{estimated_error_rate_beforehand=}")
9796

9897
assert (
99-
abs(expected_probability - fail_rate) <= 0.05
98+
abs(estimated_error_rate - error_rate) <= 0.05
10099
) # 5% absolute margin calculated experiementally
101100

102101

0 commit comments

Comments
 (0)