feat: add plots for n m k trends

This commit is contained in:
2026-04-27 20:43:23 +02:00
parent 84c0b2b797
commit f6521aeb1c
12 changed files with 226 additions and 71 deletions
-68
View File
@@ -1,68 +0,0 @@
#include "bloom_filter/bloom_filter.hpp"
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <iostream>
#include <random>
#include <set>
#include <string>
int main(int argc, char *argv[]) {
if (argc != 4) { // REM: 0th ars it the program name
std::cerr << "need args n(number of keys inserted) m(filter size in "
"num bits) k(num hash funcs)";
return 1;
}
uint n = std::stoul(argv[1]);
uint m = std::stoul(argv[2]);
uint k = std::stoul(argv[3]);
// std::random_device rd; you can uncomment and pass it in here to randomise
std::mt19937_64 rng{}; // no seed so this is deterministic
const uint TOTAL_TRIALS = 20;
double sum_theorital_approx_fp_rate = 0;
double sum_filter_approx_fp_rate = 0;
double sum_actual_fp_rate = 0;
for (int _t = 0; _t < TOTAL_TRIALS; _t++) {
uint64_t seed1 = rng(), seed2 = rng();
BloomFilter filter{m, k, seed1, seed2};
std::set<uint64_t> actual_inserts;
for (int i = 0; i < n; i++) {
uint64_t num = rng();
actual_inserts.insert(num);
filter.put(&num, sizeof(num));
}
const uint ELEMS_CHECK = 10000;
uint fp_count = 0;
for (int i = 0; i < ELEMS_CHECK; i++) {
uint64_t test = rng();
bool actually_present = actual_inserts.count(test);
bool filter_contains = filter.may_contain(&test, sizeof(test));
fp_count += !actually_present && filter_contains;
}
double theoretical_approx_fp_rate =
std::pow(1 - std::exp(-static_cast<double>(k) * n / m), k);
double filter_approx_fp_rate = filter.false_positive_probability();
double actual_fp_rate = static_cast<double>(fp_count) / ELEMS_CHECK;
sum_theorital_approx_fp_rate += theoretical_approx_fp_rate;
sum_filter_approx_fp_rate += filter_approx_fp_rate;
sum_actual_fp_rate += actual_fp_rate;
}
std::printf("simple bloom filter with n = %d, m = %d, k = %d\n", n, m, k);
std::printf("theoretical fp probs : %.8f\n",
sum_theorital_approx_fp_rate / TOTAL_TRIALS);
std::printf("filter reported fp probs : %.8f\n",
sum_filter_approx_fp_rate / TOTAL_TRIALS);
std::printf("actual fp rate : %.8f\n", sum_actual_fp_rate / TOTAL_TRIALS);
return 0;
}