mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-03-03 13:28:40 +00:00
from fail to erro
This commit is contained in:
parent
e4d39db77e
commit
7629686cc7
@ -73,30 +73,29 @@ def random_string(size):
|
|||||||
def test_probability(m=64, n=20):
|
def test_probability(m=64, n=20):
|
||||||
b = Bloom(size=m)
|
b = Bloom(size=m)
|
||||||
|
|
||||||
|
k = len(b.HASH_FUNCTIONS)
|
||||||
|
estimated_error_rate_beforehand = (1 - (1 - 1 / m) ** (k * n)) ** k
|
||||||
|
|
||||||
added = {random_string(10) for i in range(n)}
|
added = {random_string(10) for i in range(n)}
|
||||||
for a in added:
|
for a in added:
|
||||||
b.add(a)
|
b.add(a)
|
||||||
|
|
||||||
k = len(b.HASH_FUNCTIONS)
|
|
||||||
|
|
||||||
n_ones = bin(b.bitstring).count("1")
|
n_ones = bin(b.bitstring).count("1")
|
||||||
expected_probability = (n_ones / m) ** k
|
estimated_error_rate = (n_ones / m) ** k
|
||||||
|
|
||||||
expected_probability_wikipedia = (1 - (1 - 1 / m) ** (k * n)) ** k
|
|
||||||
|
|
||||||
not_added = {random_string(10) for i in range(1000)}
|
not_added = {random_string(10) for i in range(1000)}
|
||||||
fails = 0
|
errors = 0
|
||||||
for string in not_added:
|
for string in not_added:
|
||||||
if b.exists(string):
|
if b.exists(string):
|
||||||
fails += 1
|
errors += 1
|
||||||
fail_rate = fails / len(not_added)
|
error_rate = errors / len(not_added)
|
||||||
|
|
||||||
print(f"total = {len(not_added)}, fails = {fails}, fail_rate = {fail_rate}")
|
print(f"total = {len(not_added)}, errors = {errors}, error_rate = {error_rate}")
|
||||||
print(f"{expected_probability=}")
|
print(f"{estimated_error_rate=}")
|
||||||
print(f"{expected_probability_wikipedia=}")
|
print(f"{estimated_error_rate_beforehand=}")
|
||||||
|
|
||||||
assert (
|
assert (
|
||||||
abs(expected_probability - fail_rate) <= 0.05
|
abs(estimated_error_rate - error_rate) <= 0.05
|
||||||
) # 5% absolute margin calculated experiementally
|
) # 5% absolute margin calculated experiementally
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user