File tree Expand file tree Collapse file tree 1 file changed +11
-12
lines changed Expand file tree Collapse file tree 1 file changed +11
-12
lines changed Original file line number Diff line number Diff line change @@ -73,30 +73,29 @@ def random_string(size):
73
73
def test_probability (m = 64 , n = 20 ):
74
74
b = Bloom (size = m )
75
75
76
+ k = len (b .HASH_FUNCTIONS )
77
+ estimated_error_rate_beforehand = (1 - (1 - 1 / m ) ** (k * n )) ** k
78
+
76
79
added = {random_string (10 ) for i in range (n )}
77
80
for a in added :
78
81
b .add (a )
79
82
80
- k = len (b .HASH_FUNCTIONS )
81
-
82
83
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
86
85
87
86
not_added = {random_string (10 ) for i in range (1000 )}
88
- fails = 0
87
+ errors = 0
89
88
for string in not_added :
90
89
if b .exists (string ):
91
- fails += 1
92
- fail_rate = fails / len (not_added )
90
+ errors += 1
91
+ error_rate = errors / len (not_added )
93
92
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 = } " )
97
96
98
97
assert (
99
- abs (expected_probability - fail_rate ) <= 0.05
98
+ abs (estimated_error_rate - error_rate ) <= 0.05
100
99
) # 5% absolute margin calculated experiementally
101
100
102
101
You can’t perform that action at this time.
0 commit comments