Documentation

CslibFrontier.Computability.Streaming.CountSketch

Count-Sketch #

The Count-Sketch of Charikar, Chen and Farach-Colton [CCFC04] estimates the frequency f_a = count a s of a queried item a in a stream s. Each of its d = 2 r rows has w counters, a bucket hash h θ : α → Fin w and a sign hash sgn (g φ a) = ±1, with seed (θ, φ). On reading an item a, every row adds the sign of a to the counter of a; a point query a reports the median over the rows of the counter of a times the sign of a (Cslib.Streaming.countSketch, [Chakrabarti2025, Algorithm 8]).

For each fixed query a, with a two-universal bucket family and an independent pairwise independent family of signs of mean zero, each row errs by more than t with probability at most 1 / 3 once 3 ‖f_{-a}‖₂² ≤ w t², where ‖f_{-a}‖₂² = ∑_{b ≠ a} f_b² (Cslib.Probability.CountSketch.dens_abs_sum_ite_mul_sign_gt_le); so the median errs with probability at most (8 / 9) ^ r (Cslib.Streaming.dens_countSketch_error_gt_le). With 3 ≤ w ε² and (8 / 9) ^ r ≤ δ the randomized Count-Sketch with uniform seeds (Cslib.Streaming.countSketchRandom) answers every point query within ε ‖f_{-a}‖₂, hence within ε ‖f‖₂ = ε √F₂, except with probability δ (Cslib.Streaming.solves_countSketchRandom, Cslib.Streaming.solves_countSketchRandom_freqMoment) [Chakrabarti2025, equation (5.7)]. The query is fixed before the seeds are drawn: there is no claim about adaptively chosen queries.

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 not charged, so every sketch drawn uses at most b bits when (2 m + 1) ^ (2 r w) ≤ 2 ^ b (Cslib.Streaming.spaceLE_countSketchRandom). Only the initial memory depends on the seeds (Cslib.Streaming.update_countSketch, Cslib.Streaming.output_countSketch), and with the seeds stored one b-bit encoding of the states reachable from all seeds exists when (|ι| |κ|) ^ (2 r) (2 m + 1) ^ (2 r w) ≤ 2 ^ b (Cslib.Streaming.exists_injOn_iUnion_reachable_countSketch). With w = ⌈3 / ε²⌉, r = O(log δ⁻¹) and hash families with O(log n)-bit seeds, this is the O(ε⁻² log δ⁻¹ (log m + log n)) bound of [Chakrabarti2025, Section 5.3.2].

Main definitions #

Main results #

References #

@[reducible, inline]
abbrev Cslib.Streaming.CountSketchState (ι : Type u_5) (κ : Type u_6) (r w : ℕ) :
Type (max u_5 u_6)

The memory of a Count-Sketch with 2 r rows of w counters: the seeds of the hashes of each row, and the counters.

noncomputable def Cslib.Streaming.countSketch {ι : Type u_1} {κ : Type u_2} {α : Type u_3} {Y : Type u_4} {w : ℕ} (h : ι → α → Fin w) (g : κ → α → Y) (sgn : Y → ℤ) (r : ℕ) (ω : Fin (2 * r) → ι × κ) :
Algorithm (CountSketchState ι κ r w) α (α → ℝ)

The Count-Sketch [CCFC04] with 2 r rows of w counters and seeds ω: row i with seed (θ, φ) = ω i adds the sign sgn (g φ a) of each item a to its counter h θ a, and a point query a is answered with the median over the rows of the counter of a times the sign of a [Chakrabarti2025, Algorithm 8]. The seeds are written in the initial memory and read back by the update.

noncomputable def Cslib.Streaming.countSketchRandom {ι : Type u_1} {κ : Type u_2} {α : Type u_3} {Y : Type u_4} {w : ℕ} [Fintype ι] [Nonempty ι] [Fintype κ] [Nonempty κ] (h : ι → α → Fin w) (g : κ → α → Y) (sgn : Y → ℤ) (r : ℕ) :
Randomized (CountSketchState ι κ r w) α (α → ℝ)

The randomized Count-Sketch: the sketch with independent uniform seeds for its rows.

theorem Cslib.Streaming.init_countSketch {ι : Type u_1} {κ : Type u_2} {α : Type u_3} {Y : Type u_4} {w r : ℕ} {h : ι → α → Fin w} {g : κ → α → Y} {sgn : Y → ℤ} (ω : Fin (2 * r) → ι × κ) :
(countSketch h g sgn r ω).init = (ω, 0)

The Count-Sketch starts with its seeds and zero counters.

theorem Cslib.Streaming.update_countSketch {ι : Type u_1} {κ : Type u_2} {α : Type u_3} {Y : Type u_4} {w r : ℕ} {h : ι → α → Fin w} {g : κ → α → Y} {sgn : Y → ℤ} (ω ω' : Fin (2 * r) → ι × κ) (C : Fin (2 * r) → Fin w → ℤ) (a : α) :
(countSketch h g sgn r ω).update (ω', C) a = (ω', fun (i : Fin (2 * r)) (j : Fin w) => C i j + if h (ω' i).1 a = j then sgn (g (ω' i).2 a) else 0)

The update of the Count-Sketch reads the seeds from the memory: it does not depend on ω.

theorem Cslib.Streaming.output_countSketch {ι : Type u_1} {κ : Type u_2} {α : Type u_3} {Y : Type u_4} {w r : ℕ} {h : ι → α → Fin w} {g : κ → α → Y} {sgn : Y → ℤ} (ω ω' : Fin (2 * r) → ι × κ) (C : Fin (2 * r) → Fin w → ℤ) (a : α) :
(countSketch h g sgn r ω).output (ω', C) a = median fun (i : Fin (2 * r)) => ↑(C i (h (ω' i).1 a) * sgn (g (ω' i).2 a))

The output of the Count-Sketch does not depend on its seeds.

theorem Cslib.Streaming.stateFrom_countSketch {ι : Type u_1} {κ : Type u_2} {α : Type u_3} {Y : Type u_4} {w r : ℕ} {h : ι → α → Fin w} {g : κ → α → Y} {sgn : Y → ℤ} (ω ω' : Fin (2 * r) → ι × κ) (C : Fin (2 * r) → Fin w → ℤ) (s : List α) :
(countSketch h g sgn r ω).stateFrom (ω', C) s = (ω', fun (i : Fin (2 * r)) (j : Fin w) => C i j + (List.map (fun (a : α) => if h (ω' i).1 a = j then sgn (g (ω' i).2 a) else 0) s).sum)

Reading the stream s from the memory (ω', C) adds to the counter j of row i the sum of the signs of the items of s hashed to j.

theorem Cslib.Streaming.state_countSketch {ι : Type u_1} {κ : Type u_2} {α : Type u_3} {Y : Type u_4} {w r : ℕ} {h : ι → α → Fin w} {g : κ → α → Y} {sgn : Y → ℤ} (ω : Fin (2 * r) → ι × κ) (s : List α) :
(countSketch h g sgn r ω).state s = (ω, fun (i : Fin (2 * r)) (j : Fin w) => (List.map (fun (a : α) => if h (ω i).1 a = j then sgn (g (ω i).2 a) else 0) s).sum)

After the stream s, the counter j of row i holds the sum of the signs of the items of s hashed to j.

theorem Cslib.Streaming.run_countSketch {ι : Type u_1} {κ : Type u_2} {α : Type u_3} {Y : Type u_4} {w r : ℕ} {h : ι → α → Fin w} {g : κ → α → Y} {sgn : Y → ℤ} [DecidableEq α] (ω : Fin (2 * r) → ι × κ) (s : List α) (a : α) :
(countSketch h g sgn r ω).run s a = median fun (i : Fin (2 * r)) => (∑ b ∈ s.toFinset, if h (ω i).1 b = h (ω i).1 a then ↑(List.count b s) * ↑(sgn (g (ω i).2 b)) else 0) * ↑(sgn (g (ω i).2 a))

The answer of the Count-Sketch to the query a is the median over the rows of (∑ b ∈ s with h θ b = h θ a, count b s · sgn (g φ b)) · sgn (g φ a).

theorem Cslib.Streaming.dens_countSketch_error_gt_le {ι : Type u_1} {κ : Type u_2} {α : Type u_3} {Y : Type u_4} {w r : ℕ} {h : ι → α → Fin w} {g : κ → α → Y} {sgn : Y → ℤ} [Fintype ι] [Nonempty ι] [Fintype κ] [DecidableEq α] [Fintype Y] [DecidableEq Y] (hh : Probability.IsTwoUniversal (Probability.PMF.uniformOfFintype ι) h) (hg : Probability.IsKWiseIndependentHash 2 g) (hsq : ∀ (y : Y), sgn y ^ 2 = 1) (hsum : ∑ y : Y, sgn y = 0) (s : List α) (a : α) {t : ℝ} (ht : 0 ≤ t) (hw : 3 * ∑ b ∈ s.toFinset.erase a, ↑(List.count b s) ^ 2 ≤ ↑w * t ^ 2) :
↑{ω : Fin (2 * r) → ι × κ | t < |(countSketch h g sgn r ω).run s a - ↑(List.count a s)|}.dens ≤ (8 / 9) ^ r

The Count-Sketch estimates frequencies [Chakrabarti2025, equations (5.6)–(5.7)]: with a two-universal bucket family h and an independent pairwise independent family sgn ∘ g of signs of mean zero, if 3 ‖f_{-a}‖₂² ≤ w t² then the Count-Sketch with 2 r rows misses the frequency of a by more than t for at most an (8 / 9) ^ r fraction of the seeds.

theorem Cslib.Streaming.errorProb_countSketchRandom {ι : Type u_1} {κ : Type u_2} {α : Type u_3} {Y : Type u_4} {w r : ℕ} {h : ι → α → Fin w} {g : κ → α → Y} {sgn : Y → ℤ} [Fintype ι] [Nonempty ι] [Fintype κ] [Nonempty κ] (R : List α → (α → ℝ) → Prop) (s : List α) [DecidablePred fun (ω : Fin (2 * r) → ι × κ) => ¬R s ((countSketch h g sgn r ω).run s)] :
(countSketchRandom h g sgn r).errorProb R s = ↑{ω : Fin (2 * r) → ι × κ | ¬R s ((countSketch h g sgn r ω).run s)}.dens

The error of the randomized Count-Sketch is the fraction of bad seeds.

theorem Cslib.Streaming.solves_countSketchRandom {ι : Type u_1} {κ : Type u_2} {α : Type u_3} {Y : Type u_4} {w r : ℕ} {h : ι → α → Fin w} {g : κ → α → Y} {sgn : Y → ℤ} [Fintype ι] [Nonempty ι] [Fintype κ] [Nonempty κ] [DecidableEq α] [Fintype Y] [DecidableEq Y] (hh : Probability.IsTwoUniversal (Probability.PMF.uniformOfFintype ι) h) (hg : Probability.IsKWiseIndependentHash 2 g) (hsq : ∀ (y : Y), sgn y ^ 2 = 1) (hsum : ∑ y : Y, sgn y = 0) {ε δ : ℝ} (hε : 0 ≤ ε) (hw : 3 ≤ ↑w * ε ^ 2) (hδ : (8 / 9) ^ r ≤ δ) (a : α) (m : ℕ) :
(countSketchRandom h g sgn r).Solves δ (fun (s : List α) (est : α → ℝ) => |est a - ↑(List.count a s)| ≤ ε * √(∑ b ∈ s.toFinset.erase a, ↑(List.count b s) ^ 2)) m

The Count-Sketch answers point queries [Chakrabarti2025, equation (5.7)]: with a two-universal bucket family h, an independent pairwise independent family sgn ∘ g of signs of mean zero, 3 ≤ w ε² and (8 / 9) ^ r ≤ δ, for every fixed query a the randomized Count-Sketch estimates the frequency of a within ε ‖f_{-a}‖₂ = ε (∑_{b ≠ a} f_b²)^{1/2} with error at most δ on every stream.

theorem Cslib.Streaming.solves_countSketchRandom_freqMoment {ι : Type u_1} {κ : Type u_2} {α : Type u_3} {Y : Type u_4} {w r : ℕ} {h : ι → α → Fin w} {g : κ → α → Y} {sgn : Y → ℤ} [Fintype ι] [Nonempty ι] [Fintype κ] [Nonempty κ] [DecidableEq α] [Fintype Y] [DecidableEq Y] (hh : Probability.IsTwoUniversal (Probability.PMF.uniformOfFintype ι) h) (hg : Probability.IsKWiseIndependentHash 2 g) (hsq : ∀ (y : Y), sgn y ^ 2 = 1) (hsum : ∑ y : Y, sgn y = 0) {ε δ : ℝ} (hε : 0 ≤ ε) (hw : 3 ≤ ↑w * ε ^ 2) (hδ : (8 / 9) ^ r ≤ δ) (a : α) (m : ℕ) :
(countSketchRandom h g sgn r).Solves δ (fun (s : List α) (est : α → ℝ) => |est a - ↑(List.count a s)| ≤ ε * √↑(freqMoment 2 s)) m

The Count-Sketch answers point queries within ε √F₂ [Chakrabarti2025, Section 5.3.2]: under the hypotheses of Cslib.Streaming.solves_countSketchRandom, for every fixed query a the randomized Count-Sketch estimates the frequency of a within ε ‖f‖₂ = ε √F₂ with error at most δ on every stream.

theorem Cslib.Streaming.mem_Icc_state_countSketch_snd {ι : Type u_1} {κ : Type u_2} {α : Type u_3} {Y : Type u_4} {w r : ℕ} {h : ι → α → Fin w} {g : κ → α → Y} {sgn : Y → ℤ} (hsgn : ∀ (y : Y), |sgn y| ≤ 1) (ω : Fin (2 * r) → ι × κ) {m : ℕ} {s : List α} (hs : s.length ≤ m) (i : Fin (2 * r)) (j : Fin w) :
((countSketch h g sgn r ω).state s).2 i j ∈ Finset.Icc (-↑m) ↑m

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

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

The memory of the randomized Count-Sketch [Chakrabarti2025, Section 5.3.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 w) ≤ 2 ^ b. Under the public-coin convention of Cslib.Streaming.Randomized only the 2 r w counters are charged; the seeds are not. For the count with the seeds stored see Cslib.Streaming.exists_injOn_iUnion_reachable_countSketch.

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

The memory of the Count-Sketch with its seeds stored [Chakrabarti2025, Section 5.3.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 r) (2 m + 1) ^ (2 r w) ≤ 2 ^ b. Since only the initial memory depends on the seeds (Cslib.Streaming.update_countSketch, Cslib.Streaming.output_countSketch), this is the memory of one algorithm whose initial memory is random, with the seeds charged.