Documentation

CslibFrontier.Computability.Streaming.AMS

The AMS sketch for the second frequency moment #

The k-th frequency moment of a stream is F_k = ∑ₐ (count a s) ^ k (Cslib.Streaming.freqMoment). Alon, Matias and Szegedy estimate F₂ in small space [AlonMatiasSzegedy1999, Theorem 2.2]: a tug-of-war counter with seed θ adds a sign sgn (h θ a) = ±1 for each item a, so after the stream s it holds Z = ∑ₐ count a s · sgn (h θ a). For a four-wise independent family of signs of mean zero, 𝔼 Z² = F₂ and 𝔼 (Z² - F₂)² ≤ 2 F₂² (Cslib.Probability.IsKWiseIndependentHash, tug-of-war moments). The AMS sketch (Cslib.Streaming.amsSketch) keeps 2 r groups of k counters with independent seeds and answers the median over the groups of the average of Z² in the group, the median of means Cslib.Probability.medianOfMeans of Z².

With 6 ≤ k η² the answer is within η F₂ of F₂ except for an (8 / 9) ^ r fraction of the seeds (Cslib.Streaming.dens_amsSketch_error_le); so the randomized sketch with uniform seeds (Cslib.Streaming.ams) solves F₂ estimation with error δ ≥ (8 / 9) ^ r on every stream (Cslib.Streaming.solves_ams). Taking k = ⌈6 / η²⌉ and r = ⌈log (1 / δ) / log (9 / 8)⌉ uses O(η⁻² log δ⁻¹) counters.

Memory. On streams of at most m items each counter lies in [-m, m]. Under the public-coin convention of Cslib.Streaming.Randomized the seeds are drawn with the algorithm and are not charged, so every sketch drawn uses at most b bits as soon as (2 m + 1) ^ (2 r k) ≤ 2 ^ b (Cslib.Streaming.spaceLE_ams). Storing the seeds as well costs 2 r k log₂ |ι| more bits. Only the initial memory of the sketch depends on the seeds (Cslib.Streaming.update_amsSketch, Cslib.Streaming.output_amsSketch), so the sketch is one algorithm with a random initial memory, and one b-bit encoding of the states reachable from all seeds exists as soon as (|ι| (2 m + 1)) ^ (2 r k) ≤ 2 ^ b (Cslib.Streaming.exists_injOn_iUnion_reachable_amsSketch), that is b = ⌈2 r k (log₂ |ι| + log₂ (2 m + 1))⌉ bits. With four-wise independent seeds of O(log n) bits for a universe of size n, for instance from Cslib.Probability.trinomialPolyHash, this seed-inclusive count is the O(η⁻² log δ⁻¹ (log n + log m)) bound of [AlonMatiasSzegedy1999, Theorem 2.2].

Main definitions #

Main results #

References #

@[reducible, inline]
abbrev Cslib.Streaming.AMSState (ι : Type u_4) (r k : ℕ) :
Type u_4

The memory of the AMS sketch with 2 r groups of k tug-of-war counters: a seed and a counter for each of them.

Instances For
    noncomputable def Cslib.Streaming.amsSketch {ι : Type u_1} {α : Type u_2} {Y : Type u_3} (h : ι → α → Y) (sgn : Y → ℤ) (r k : ℕ) (ω : Fin (2 * r) → Fin k → ι) :
    Algorithm (AMSState ι r k) α ℝ

    The AMS sketch for F₂ [AlonMatiasSzegedy1999, Theorem 2.2] with seeds ω: the counter with seed θ adds the sign sgn (h θ a) of each item a, and the answer is the median over the 2 r groups of the average square of the k counters in the group. The seeds are written in the initial memory and read back by the update, which does not otherwise depend on ω (Cslib.Streaming.update_amsSketch).

    Instances For
      noncomputable def Cslib.Streaming.ams {ι : Type u_1} {α : Type u_2} {Y : Type u_3} [Fintype ι] [Nonempty ι] (h : ι → α → Y) (sgn : Y → ℤ) (r k : ℕ) :
      Randomized (AMSState ι r k) α ℝ

      The randomized AMS sketch: the sketch with independent uniform seeds.

      Instances For
        theorem Cslib.Streaming.init_amsSketch {ι : Type u_1} {α : Type u_2} {Y : Type u_3} {h : ι → α → Y} {sgn : Y → ℤ} {r k : ℕ} (ω : Fin (2 * r) → Fin k → ι) :
        (amsSketch h sgn r k ω).init = (ω, 0)

        The AMS sketch starts with its seeds and zero counters.

        theorem Cslib.Streaming.update_amsSketch {ι : Type u_1} {α : Type u_2} {Y : Type u_3} {h : ι → α → Y} {sgn : Y → ℤ} {r k : ℕ} (ω : Fin (2 * r) → Fin k → ι) (q : AMSState ι r k) (a : α) :
        (amsSketch h sgn r k ω).update q a = (q.1, fun (i : Fin (2 * r)) (l : Fin k) => q.2 i l + sgn (h (q.1 i l) a))

        The update of the AMS sketch reads the seeds from the memory: it does not depend on ω.

        theorem Cslib.Streaming.output_amsSketch {ι : Type u_1} {α : Type u_2} {Y : Type u_3} {h : ι → α → Y} {sgn : Y → ℤ} {r k : ℕ} (ω : Fin (2 * r) → Fin k → ι) (q : AMSState ι r k) :
        (amsSketch h sgn r k ω).output q = median fun (i : Fin (2 * r)) => Finset.univ.expect fun (l : Fin k) => ↑(q.2 i l) ^ 2

        The output of the AMS sketch does not depend on its seeds.

        theorem Cslib.Streaming.stateFrom_amsSketch {ι : Type u_1} {α : Type u_2} {Y : Type u_3} {h : ι → α → Y} {sgn : Y → ℤ} {r k : ℕ} (ω : Fin (2 * r) → Fin k → ι) (q : AMSState ι r k) (s : List α) :
        (amsSketch h sgn r k ω).stateFrom q s = (q.1, fun (i : Fin (2 * r)) (l : Fin k) => q.2 i l + (List.map (fun (a : α) => sgn (h (q.1 i l) a)) s).sum)
        theorem Cslib.Streaming.state_amsSketch {ι : Type u_1} {α : Type u_2} {Y : Type u_3} {h : ι → α → Y} {sgn : Y → ℤ} {r k : ℕ} (ω : Fin (2 * r) → Fin k → ι) (s : List α) :
        (amsSketch h sgn r k ω).state s = (ω, fun (i : Fin (2 * r)) (l : Fin k) => (List.map (fun (a : α) => sgn (h (ω i l) a)) s).sum)

        After the stream s, the counter with seed θ holds ∑_{a ∈ s} sgn (h θ a).

        theorem Cslib.Streaming.run_amsSketch {ι : Type u_1} {α : Type u_2} {Y : Type u_3} {h : ι → α → Y} {sgn : Y → ℤ} {r k : ℕ} [DecidableEq α] (ω : Fin (2 * r) → Fin k → ι) (s : List α) :
        (amsSketch h sgn r k ω).run s = Probability.medianOfMeans (fun (θ : ι) => (∑ a ∈ s.toFinset, ↑(List.count a s) * ↑(sgn (h θ a))) ^ 2) ω

        The answer of the AMS sketch is the median of means of the squared tug-of-war sums (∑ₐ count a s · sgn (h θ a)) ^ 2.

        theorem Cslib.Streaming.dens_amsSketch_error_le {ι : Type u_1} {α : Type u_2} {Y : Type u_3} {h : ι → α → Y} {sgn : Y → ℤ} {r k : ℕ} [Fintype ι] [DecidableEq α] [Fintype Y] [DecidableEq Y] (hh : Probability.IsKWiseIndependentHash 4 h) (hsq : ∀ (y : Y), sgn y ^ 2 = 1) (hsum : ∑ y : Y, sgn y = 0) {η : ℝ} (hη : 0 < η) (hk : 6 ≤ ↑k * η ^ 2) (s : List α) :
        ↑{ω : Fin (2 * r) → Fin k → ι | η * ↑(freqMoment 2 s) < |(amsSketch h sgn r k ω).run s - ↑(freqMoment 2 s)|}.dens ≤ (8 / 9) ^ r

        The AMS sketch estimates F₂ [AlonMatiasSzegedy1999, Theorem 2.2]: with a four-wise independent family of signs of mean zero, and 6 ≤ k η², the AMS sketch with 2 r groups of k counters misses F₂ by more than η F₂ for at most an (8 / 9) ^ r fraction of the seeds.

        theorem Cslib.Streaming.errorProb_ams {ι : Type u_1} {α : Type u_2} {Y : Type u_3} {h : ι → α → Y} {sgn : Y → ℤ} {r k : ℕ} [Fintype ι] [Nonempty ι] (R : List α → ℝ → Prop) (s : List α) [DecidablePred fun (ω : Fin (2 * r) → Fin k → ι) => ¬R s ((amsSketch h sgn r k ω).run s)] :
        (ams h sgn r k).errorProb R s = ↑{ω : Fin (2 * r) → Fin k → ι | ¬R s ((amsSketch h sgn r k ω).run s)}.dens

        The error of the randomized AMS sketch is the fraction of bad seeds.

        theorem Cslib.Streaming.solves_ams {ι : Type u_1} {α : Type u_2} {Y : Type u_3} {h : ι → α → Y} {sgn : Y → ℤ} {r k : ℕ} [Fintype ι] [Nonempty ι] [DecidableEq α] [Fintype Y] [DecidableEq Y] (hh : Probability.IsKWiseIndependentHash 4 h) (hsq : ∀ (y : Y), sgn y ^ 2 = 1) (hsum : ∑ y : Y, sgn y = 0) {η δ : ℝ} (hη : 0 < η) (hk : 6 ≤ ↑k * η ^ 2) (hδ : (8 / 9) ^ r ≤ δ) (m : ℕ) :
        (ams h sgn r k).Solves δ (fun (s : List α) (Y : ℝ) => |Y - ↑(freqMoment 2 s)| ≤ η * ↑(freqMoment 2 s)) m

        The AMS sketch solves F₂ estimation [AlonMatiasSzegedy1999, Theorem 2.2]: with a four-wise independent family of signs of mean zero, 6 ≤ k η² and (8 / 9) ^ r ≤ δ, the randomized AMS sketch estimates F₂ within η F₂ with error at most δ on every stream.

        theorem Cslib.Streaming.mem_Icc_state_amsSketch_snd {ι : Type u_1} {α : Type u_2} {Y : Type u_3} {h : ι → α → Y} {sgn : Y → ℤ} {r k : ℕ} (hsgn : ∀ (y : Y), |sgn y| ≤ 1) (ω : Fin (2 * r) → Fin k → ι) {m : ℕ} {s : List α} (hs : s.length ≤ m) (i : Fin (2 * r)) (l : Fin k) :
        ((amsSketch h sgn r k ω).state s).2 i l ∈ Finset.Icc (-↑m) ↑m

        On streams of at most m items, every counter of the AMS sketch lies in [-m, m].

        theorem Cslib.Streaming.spaceLE_ams {ι : Type u_1} {α : Type u_2} {Y : Type u_3} {h : ι → α → Y} {sgn : Y → ℤ} {r k : ℕ} [Fintype ι] [Nonempty ι] (hsgn : ∀ (y : Y), |sgn y| ≤ 1) {m b : ℕ} (hb : (2 * m + 1) ^ (2 * r * k) ≤ 2 ^ b) :
        (ams h sgn r k).SpaceLE m b

        The memory of the randomized AMS sketch [AlonMatiasSzegedy1999, Theorem 2.2]: every sketch it draws uses at most b bits of memory on streams of at most m items when (2 m + 1) ^ (2 r k) ≤ 2 ^ b. Under the public-coin convention of Cslib.Streaming.Randomized only the 2 r k counters are charged; the seeds are not. For the count with the seeds stored see Cslib.Streaming.exists_injOn_iUnion_reachable_amsSketch.

        theorem Cslib.Streaming.exists_injOn_iUnion_reachable_amsSketch {ι : Type u_1} {α : Type u_2} {Y : Type u_3} {h : ι → α → Y} {sgn : Y → ℤ} {r k : ℕ} [Fintype ι] (hsgn : ∀ (y : Y), |sgn y| ≤ 1) {m b : ℕ} (hb : (Fintype.card ι * (2 * m + 1)) ^ (2 * r * k) ≤ 2 ^ b) :
        ∃ (enc : AMSState ι r k → BitString b), Set.InjOn enc (⋃ (ω : Fin (2 * r) → Fin k → ι), (amsSketch h sgn r k ω).reachable m)

        The memory of the AMS sketch with its seeds stored [AlonMatiasSzegedy1999, Theorem 2.2]: on streams of at most m items, the states reachable from all seeds together have a single injective encoding by b bits as soon as (|ι| (2 m + 1)) ^ (2 r k) ≤ 2 ^ b. Since only the initial memory depends on the seeds (Cslib.Streaming.update_amsSketch, Cslib.Streaming.output_amsSketch), this is the memory of one algorithm whose initial memory is random, with the seeds charged.