Skip to main content

log_fft/paper/
params.rs

1//! Window parameters of the count predictor (repair.tex, binary case `ℓ = 2`).
2//!
3//! The paper sets
4//! * `r` = the least integer with `2^r ≥ 2m`
5//! * `μ = m/2`, `B = ⌈√(2 m r)⌉`
6//! * window `[u, v]` with `u = ⌊μ − B⌋`, `v = ⌈μ + B⌉`
7//! * predictor degree `D = v − u`
8//! * exception budget `s = ⌊W / (2 m³)⌋`
9//!
10//! Hoeffding's inequality gives `Pr_β[c ∉ [u,v]] ≤ 2·exp(−2B²/m) ≤ 1/(2m³)`.
11
12/// Window `[u, v]` for `m` binary queries.
13#[derive(Clone, Copy, Debug, PartialEq, Eq)]
14pub struct Params {
15    /// Number of commuting involutions `Q_1 … Q_m`.
16    pub m: usize,
17    /// Lower end of the window (may be negative).
18    pub u: i64,
19    /// Upper end of the window (may exceed `m`).
20    pub v: i64,
21}
22
23impl Params {
24    /// The parameters exactly as chosen in the paper.
25    pub fn paper(m: usize) -> Self {
26        assert!(m >= 1);
27        let b = paper_half_width(m);
28        Self::with_half_width(m, b as f64)
29    }
30
31    /// A window `[⌊m/2 − b⌋, ⌈m/2 + b⌉]` with a chosen half-width `b`.
32    ///
33    /// For small `m` the paper's window covers all of `[0, m]`, so there are
34    /// no exceptions and the repair step has nothing to do. A narrower window
35    /// makes exceptions appear, which tests the repair step.
36    pub fn with_half_width(m: usize, b: f64) -> Self {
37        let mu = m as f64 / 2.0;
38        Params {
39            m,
40            u: (mu - b).floor() as i64,
41            v: (mu + b).ceil() as i64,
42        }
43    }
44
45    /// An explicit window `[u, v]`.
46    pub fn with_window(m: usize, u: i64, v: i64) -> Self {
47        assert!(u <= v);
48        Params { m, u, v }
49    }
50
51    /// Predictor degree `D = v − u`.
52    pub fn degree(&self) -> usize {
53        (self.v - self.u) as usize
54    }
55
56    /// True when the window is strictly narrower than the full count range
57    /// `[0, m]`, that is, when the predictor really has degree below `m`.
58    pub fn window_bites(&self) -> bool {
59        self.degree() < self.m
60    }
61
62    /// `true` when count `c` lies inside the window.
63    pub fn in_window(&self, c: i64) -> bool {
64        self.u <= c && c <= self.v
65    }
66
67    /// Exact fraction of shifts `β ∈ F_2^m` whose count falls outside the
68    /// window. It is the same for every frequency.
69    pub fn exception_fraction(&self) -> f64 {
70        // Work in log space so large m neither overflows nor underflows.
71        let m = self.m as u32;
72        let ln2m = m as f64 * core::f64::consts::LN_2;
73        let mut out = 0f64;
74        let mut ln_binom = 0f64;
75        for c in 0..=m {
76            if !self.in_window(c as i64) {
77                out += (ln_binom - ln2m).exp();
78            }
79            if c < m {
80                ln_binom += ((m - c) as f64).ln() - ((c + 1) as f64).ln();
81            }
82        }
83        out
84    }
85
86    /// The paper's Hoeffding bound `2·exp(−2B²/m)` on the exception fraction,
87    /// with `B = paper_half_width(m)`.
88    pub fn paper_hoeffding_bound(m: usize) -> f64 {
89        let b = paper_half_width(m) as f64;
90        2.0 * (-2.0 * b * b / m as f64).exp()
91    }
92}
93
94/// `r` = least integer with `2^r ≥ 2m`.
95pub fn paper_r(m: usize) -> u32 {
96    let mut r = 0;
97    while (1usize << r) < 2 * m {
98        r += 1;
99    }
100    r
101}
102
103/// `B = ⌈√(2 m r)⌉` (binary case).
104pub fn paper_half_width(m: usize) -> u64 {
105    let r = paper_r(m) as f64;
106    (2.0 * m as f64 * r).sqrt().ceil() as u64
107}
108
109/// Smallest `m` for which the paper's window is narrower than `[0, m]`.
110pub fn paper_threshold_m() -> usize {
111    (1..).find(|&m| Params::paper(m).window_bites()).unwrap()
112}
113
114#[cfg(test)]
115mod tests {
116    use super::*;
117
118    #[test]
119    fn small_m_window_covers_everything() {
120        // m = 3 gives [u, v] = [-4, 7]: the report's sanity value.
121        let p = Params::paper(3);
122        assert_eq!((p.u, p.v), (-4, 7));
123        assert!(!p.window_bites());
124        assert_eq!(p.exception_fraction(), 0.0);
125    }
126
127    #[test]
128    fn hoeffding_bound_holds() {
129        for m in 1..400 {
130            let p = Params::paper(m);
131            assert!(p.exception_fraction() <= Params::paper_hoeffding_bound(m) + 1e-15);
132            assert!(Params::paper_hoeffding_bound(m) <= 1.0 / (2.0 * (m as f64).powi(3)) + 1e-15);
133        }
134    }
135
136    #[test]
137    fn threshold_exists() {
138        let t = paper_threshold_m();
139        assert!(Params::paper(t).window_bites());
140        assert!(!Params::paper(t - 1).window_bites());
141    }
142}