File size: 602 Bytes
c68a03b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from __future__ import annotations

import math


def collision_probability(d: int, delta: int, k: int) -> float:
    denom = math.comb(d, k)
    total = 0.0
    for r in range(0, min(k, delta) + 1, 2):
        total += math.comb(delta, r) * math.comb(d - delta, k - r) / denom
    return total


def recommended_params(d: int, delta: int, lam: int = 40) -> tuple[int, float, int]:
    k = math.ceil(d / (delta + 1))
    p_delta = collision_probability(d, delta, k)
    p_delta = min(max(p_delta, 1e-9), 1.0 - 1e-9)
    l_min = math.ceil(lam / (-math.log2(1.0 - p_delta)))
    return k, p_delta, l_min