Skip to main content

optimal_bit_vector_size

Function optimal_bit_vector_size 

Source
pub fn optimal_bit_vector_size(n: usize, fpr: f64) -> usize
Expand 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:

  1. The optimal false positive rate for a Bloom filter is (1 - e^(-kn/m))^k
  2. When k = (m/n)*ln(2), this expression is minimized
  3. At this optimal k, the false positive rate is approximately (0.6185)^(m/n)
  4. Solving for m gives us the formula implemented here

Parameters:

  • n: The expected number of elements to be inserted
  • fpr: The target false positive rate (e.g., 0.01 for 1%)

Returns: The optimal size of the bit vector as a number of bits