pub fn optimal_bit_vector_size(n: usize, fpr: f64) -> usizeExpand description
Calculates the optimal bit vector size for a Bloom filter.
This function determines the ideal size of the bit array to achieve the target false positive rate (FPR) for a given number of elements.
The formula used is: m = -n * ln(fpr) / (ln(2)^2) Where:
- m is the optimal bit vector size
- n is the expected number of elements
- fpr is the target false positive rate (between 0 and 1)
- ln(2) is the natural logarithm of 2
Mathematical derivation:
- The optimal false positive rate for a Bloom filter is (1 - e^(-kn/m))^k
- When k = (m/n)*ln(2), this expression is minimized
- At this optimal k, the false positive rate is approximately (0.6185)^(m/n)
- Solving for m gives us the formula implemented here
Parameters:
n: The expected number of elements to be insertedfpr: The target false positive rate (e.g., 0.01 for 1%)
Returns: The optimal size of the bit vector as a number of bits