Documentation

CslibFrontier.Computability.Streaming.FkEstimator

The AMS estimator for the frequency moments F_k #

Alon, Matias and Szegedy estimate every frequency moment F_k = ∑ₐ (count a s) ^ k (Cslib.Streaming.freqMoment), k ≥ 1, of a stream of m items from a universe of size n in one pass with O(k λ⁻² log (1/ε) n ^ (1 - 1/k) (log n + log m)) bits of memory, to within λ F_k except with probability ε [AlonMatiasSzegedy1999, Theorem 2.1].

The basic estimator. Draw a position p of the stream uniformly, let r be the number of occurrences of the item a_p at positions q ≥ p (Cslib.Streaming.suffixCount), and set X = m (r ^ k - (r - 1) ^ k) (Cslib.Streaming.fkBasicEstimate). Summing over the occurrences of each item telescopes, so 𝔼 X = F_k (Cslib.Streaming.expect_fkBasicEstimate). By a ^ k - b ^ k ≤ (a - b) k a ^ (k - 1) (Nat.succ_pow_sub_pow_le), 𝔼 X² ≤ k F₁ F_{2k-1} (Cslib.Streaming.expect_fkBasicEstimate_sq_le), and the power-sum inequality F₁ F_{2k-1} ≤ n ^ (1 - 1/k) F_k² of the source (Finset.sum_mul_sum_pow_le, raised to the k-th power: Cslib.Streaming.length_mul_freqMoment_pow_le) bounds the variance by k n ^ (1 - 1/k) F_k².

The sketch. Cslib.Streaming.fkSketch k m r K ω runs 2 r groups of K copies of the basic estimator with sampled positions ω i l and reports the median over the groups of the average in the group (Cslib.Streaming.run_fkSketch_eq_medianOfMeans). Each copy keeps the item at its position, once that position has been read, and the number of its occurrences since; a step counter tells when a sampled position arrives. As in the source the stream length m is known in advance: the positions are drawn from Fin m and the answer scales by m, and the guarantees are for streams of exactly m items. When (3 k) ^ k n ^ (k - 1) ≤ (K λ²) ^ k, that is 3 k n ^ (1 - 1/k) ≤ K λ² (Cslib.Streaming.pow_mul_pow_sub_one_le_of_mul_rpow_le), the answer is within λ F_k of F_k except for an (8 / 9) ^ r fraction of the seeds (Cslib.Streaming.dens_fkSketch_error_le), by the median-of-means bound Cslib.Probability.dens_abs_medianOfMeans_sub_gt_le. So the randomized sketch with uniform positions (Cslib.Streaming.fkEstimator) solves F_k estimation with error δ ≥ (8 / 9) ^ r on streams of m items (Cslib.Streaming.solves_fkEstimator). The source takes 8 k n ^ (1 - 1/k) / λ² copies per group, a Chebyshev bound 1/8 per group and a Chernoff bound over 2 log (1/ε) groups; here the group size 3 k n ^ (1 - 1/k) / λ² gives 1/3 per group and r = ⌈log (1/ε) / log (9/8)⌉, the same order.

Memory. On streams of at most M items the counter and every count lie in [0, M], and a copy stores one of n + 1 item values (none or an item). Under the public-coin convention of Cslib.Streaming.Randomized the positions are not charged, so every sketch drawn uses b bits once (M + 1) ((n + 1) (M + 1)) ^ (2 r K) ≤ 2 ^ b (Cslib.Streaming.spaceLE_fkEstimator). As for the AMS F₂ sketch, only the initial memory depends on the positions (Cslib.Streaming.update_fkSketch, Cslib.Streaming.output_fkSketch), and the states reachable from all positions have one b-bit encoding once m ^ (2 r K) (M + 1) ((n + 1) (M + 1)) ^ (2 r K) ≤ 2 ^ b (Cslib.Streaming.exists_injOn_iUnion_reachable_fkSketch): O(r K (log n + log m)) bits for M = m, the bound of the theorem with K = ⌈3 k n ^ (1 - 1/k) / λ²⌉.

Main definitions #

Main results #

References #

The basic estimator #

def Cslib.Streaming.suffixCount {α : Type u_1} [DecidableEq α] (s : List α) (p : ℕ) :

The number of occurrences of the item at position p of s among the positions q ≥ p, the quantity r of [AlonMatiasSzegedy1999, proof of Theorem 2.1]; zero if p is not a position of s.

@[simp]
@[simp]
theorem Cslib.Streaming.suffixCount_cons_zero {α : Type u_1} [DecidableEq α] (a : α) (s : List α) :
suffixCount (a :: s) 0 = List.count a s + 1
@[simp]
theorem Cslib.Streaming.suffixCount_cons_succ {α : Type u_1} [DecidableEq α] (a : α) (s : List α) (p : ℕ) :
suffixCount (a :: s) (p + 1) = suffixCount s p
theorem Cslib.Streaming.suffixCount_eq_of_lt {α : Type u_1} [DecidableEq α] {s : List α} {p : ℕ} (hp : p < s.length) :
theorem Cslib.Streaming.suffixCount_eq_zero_of_le {α : Type u_1} [DecidableEq α] {s : List α} {p : ℕ} (hp : s.length ≤ p) :
theorem Cslib.Streaming.sum_range_pow_sub_pow_suffixCount {α : Type u_1} [DecidableEq α] {k : ℕ} (hk : k ≠ 0) (s : List α) :
∑ p ∈ Finset.range s.length, (suffixCount s p ^ k - (suffixCount s p - 1) ^ k) = freqMoment k s

Telescoping [AlonMatiasSzegedy1999, proof of Theorem 2.1]: summing r ^ k - (r - 1) ^ k over the positions of the stream gives F_k, since the positions of an item with c occurrences contribute 1 ^ k + (2 ^ k - 1 ^ k) + ⋯ + (c ^ k - (c - 1) ^ k) = c ^ k.

theorem Cslib.Streaming.sum_range_sq_pow_sub_pow_suffixCount_le {α : Type u_1} [DecidableEq α] (j : ℕ) (s : List α) :
∑ p ∈ Finset.range s.length, (suffixCount s p ^ (j + 1) - (suffixCount s p - 1) ^ (j + 1)) ^ 2 ≤ (j + 1) * freqMoment (2 * j + 1) s

The second-moment sum [AlonMatiasSzegedy1999, proof of Theorem 2.1, inequality (1)]: summing (r ^ k - (r - 1) ^ k) ^ 2 over the positions gives at most k F_{2k-1}, here with k = j + 1.

theorem Cslib.Streaming.length_mul_freqMoment_pow_le {α : Type u_1} [DecidableEq α] (j : ℕ) (s : List α) :
(s.length * freqMoment (2 * j + 1) s) ^ (j + 1) ≤ s.toFinset.card ^ j * freqMoment (j + 1) s ^ (2 * (j + 1))

The AMS power-sum inequality for streams [AlonMatiasSzegedy1999, Fact in the proof of Theorem 2.1]: F₁ F_{2k-1} ≤ n ^ (1 - 1/k) F_k² for a stream with n distinct items, raised to the k-th power, with k = j + 1.

noncomputable def Cslib.Streaming.fkBasicEstimate {α : Type u_1} [DecidableEq α] (k : ℕ) (s : List α) (p : ℕ) :

The basic AMS estimator of F_k at position p of the stream s: X = m (r ^ k - (r - 1) ^ k) with m the length of s and r = suffixCount s p [AlonMatiasSzegedy1999, proof of Theorem 2.1].

theorem Cslib.Streaming.expect_fkBasicEstimate {α : Type u_1} [DecidableEq α] {k : ℕ} (hk : k ≠ 0) (s : List α) :
(Finset.univ.expect fun (p : Fin s.length) => fkBasicEstimate k s ↑p) = ↑(freqMoment k s)

The basic estimator is unbiased [AlonMatiasSzegedy1999, proof of Theorem 2.1]: at a uniform position of the stream its mean is F_k, for k ≠ 0.

theorem Cslib.Streaming.expect_fkBasicEstimate_sq_le {α : Type u_1} [DecidableEq α] {k : ℕ} (hk : k ≠ 0) (s : List α) :
(Finset.univ.expect fun (p : Fin s.length) => fkBasicEstimate k s ↑p ^ 2) ≤ ↑k * ↑s.length * ↑(freqMoment (2 * k - 1) s)

The second moment of the basic estimator [AlonMatiasSzegedy1999, proof of Theorem 2.1]: 𝔼 X² ≤ k F₁ F_{2k-1}, for k ≠ 0.

theorem Cslib.Streaming.three_mul_length_mul_freqMoment_le {α : Type u_1} [DecidableEq α] {k K n : ℕ} (hk : k ≠ 0) {η : ℝ} (hK : (3 * ↑k) ^ k * ↑n ^ (k - 1) ≤ (↑K * η ^ 2) ^ k) {s : List α} (hn : s.toFinset.card ≤ n) :
3 * (↑k * ↑s.length * ↑(freqMoment (2 * k - 1) s)) ≤ ↑K * (η * ↑(freqMoment k s)) ^ 2

The group size condition (3 k) ^ k n ^ (k - 1) ≤ (K η²) ^ k bounds three times the second moment bound k F₁ F_{2k-1} by K (η F_k)² on streams with at most n distinct items, through the power-sum inequality Cslib.Streaming.length_mul_freqMoment_pow_le.

theorem Cslib.Streaming.pow_mul_pow_sub_one_le_of_mul_rpow_le {k K n : ℕ} {η : ℝ} (h : 3 * ↑k * ↑n ^ (1 - 1 / ↑k) ≤ ↑K * η ^ 2) :
(3 * ↑k) ^ k * ↑n ^ (k - 1) ≤ (↑K * η ^ 2) ^ k

The rpow form 3 k n ^ (1 - 1/k) ≤ K η² of the group size condition of [AlonMatiasSzegedy1999, proof of Theorem 2.1] implies the form (3 k) ^ k n ^ (k - 1) ≤ (K η²) ^ k used here.

The sketch #

structure Cslib.Streaming.FkState (α : Type u_2) (m r K : ℕ) :
Type u_2

The memory of the F_k sketch with 2 r groups of K copies on streams of m items: the sampled positions, a step counter, and for each copy an item (none before its position is read) and a suffix count.

  • positions : Fin (2 * r) → Fin K → Fin m

    Sampled stream positions for each of the 2 r groups and K copies.

  • step : ℕ

    Number of stream items processed so far.

  • cells : Fin (2 * r) → Fin K → Option α × ℕ

    Per-copy sampled item (none before the sampled position arrives) and suffix count.

theorem Cslib.Streaming.FkState.ext_iff {α : Type u_2} {m r K : ℕ} {x y : FkState α m r K} :
theorem Cslib.Streaming.FkState.ext {α : Type u_2} {m r K : ℕ} {x y : FkState α m r K} (positions : x.positions = y.positions) (step : x.step = y.step) (cells : x.cells = y.cells) :
x = y
def Cslib.Streaming.fkCell {α : Type u_1} [DecidableEq α] (t : ℕ) (a : α) (p : ℕ) (c : Option α × ℕ) :

The update of one copy with sampled position p on reading the item a at step t: at its position it stores a with count one, and afterwards it counts the occurrences of its item.

noncomputable def Cslib.Streaming.fkSketch {α : Type u_1} [DecidableEq α] (k m r K : ℕ) (ω : Fin (2 * r) → Fin K → Fin m) :
Algorithm (FkState α m r K) α ℝ

The AMS F_k sketch [AlonMatiasSzegedy1999, Theorem 2.1] for streams of m items with sampled positions ω: the copy with position p computes r = suffixCount s p, and the answer is the median over the 2 r groups of the average of m (r ^ k - (r - 1) ^ k) over the K copies in the group. The positions are written in the initial memory and read back by the update, which does not otherwise depend on ω (Cslib.Streaming.update_fkSketch).

noncomputable def Cslib.Streaming.fkEstimator (α : Type u_2) [DecidableEq α] (k m r K : ℕ) [NeZero m] :
Randomized (FkState α m r K) α ℝ

The randomized AMS F_k sketch: the sketch with independent uniform positions.

theorem Cslib.Streaming.init_fkSketch {α : Type u_1} [DecidableEq α] {k m r K : ℕ} (ω : Fin (2 * r) → Fin K → Fin m) :
(fkSketch k m r K ω).init = { positions := ω, step := 0, cells := fun (x : Fin (2 * r)) (x_1 : Fin K) => (none, 0) }

The F_k sketch starts with its positions, step zero and empty copies.

theorem Cslib.Streaming.update_fkSketch {α : Type u_1} [DecidableEq α] {k m r K : ℕ} (ω : Fin (2 * r) → Fin K → Fin m) (q : FkState α m r K) (a : α) :
(fkSketch k m r K ω).update q a = { positions := q.positions, step := q.step + 1, cells := fun (i : Fin (2 * r)) (l : Fin K) => fkCell q.step a (↑(q.positions i l)) (q.cells i l) }

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

theorem Cslib.Streaming.output_fkSketch {α : Type u_1} [DecidableEq α] {k m r K : ℕ} (ω : Fin (2 * r) → Fin K → Fin m) (q : FkState α m r K) :
(fkSketch k m r K ω).output q = median fun (i : Fin (2 * r)) => Finset.univ.expect fun (l : Fin K) => ↑m * ↑((q.cells i l).2 ^ k - ((q.cells i l).2 - 1) ^ k)

The output of the F_k sketch does not depend on its positions.

theorem Cslib.Streaming.state_fkSketch {α : Type u_1} [DecidableEq α] {k m r K : ℕ} (ω : Fin (2 * r) → Fin K → Fin m) (s : List α) :
(fkSketch k m r K ω).state s = { positions := ω, step := s.length, cells := fun (i : Fin (2 * r)) (l : Fin K) => (s[↑(ω i l)]?, suffixCount s ↑(ω i l)) }

After the stream s, the step counter is the length of s, and the copy with position p holds the item at position p (if any) and suffixCount s p.

theorem Cslib.Streaming.run_fkSketch {α : Type u_1} [DecidableEq α] {k m r K : ℕ} (ω : Fin (2 * r) → Fin K → Fin m) (s : List α) :
(fkSketch k m r K ω).run s = median fun (i : Fin (2 * r)) => Finset.univ.expect fun (l : Fin K) => ↑m * ↑(suffixCount s ↑(ω i l) ^ k - (suffixCount s ↑(ω i l) - 1) ^ k)
theorem Cslib.Streaming.run_fkSketch_eq_medianOfMeans {α : Type u_1} [DecidableEq α] {k r K : ℕ} (s : List α) (ω : Fin (2 * r) → Fin K → Fin s.length) :
(fkSketch k s.length r K ω).run s = Probability.medianOfMeans (fun (p : Fin s.length) => fkBasicEstimate k s ↑p) ω

On a stream of exactly m items, the answer of the F_k sketch is the median of means of the basic estimator at the sampled positions.

theorem Cslib.Streaming.dens_fkSketch_error_le {α : Type u_1} [DecidableEq α] {k r K n : ℕ} (hk : k ≠ 0) {η : ℝ} (hη : 0 < η) (hK : (3 * ↑k) ^ k * ↑n ^ (k - 1) ≤ (↑K * η ^ 2) ^ k) (s : List α) (hn : s.toFinset.card ≤ n) :
↑{ω : Fin (2 * r) → Fin K → Fin s.length | η * ↑(freqMoment k s) < |(fkSketch k s.length r K ω).run s - ↑(freqMoment k s)|}.dens ≤ (8 / 9) ^ r

The AMS sketch estimates F_k [AlonMatiasSzegedy1999, Theorem 2.1]: on a stream of m items with at most n distinct items, if k ≠ 0, η > 0 and (3 k) ^ k n ^ (k - 1) ≤ (K η²) ^ k, the F_k sketch with 2 r groups of K copies misses F_k by more than η F_k for at most an (8 / 9) ^ r fraction of the sampled positions.

theorem Cslib.Streaming.errorProb_fkEstimator {α : Type u_1} [DecidableEq α] {k m r K : ℕ} [NeZero m] (R : List α → ℝ → Prop) (s : List α) [DecidablePred fun (ω : Fin (2 * r) → Fin K → Fin m) => ¬R s ((fkSketch k m r K ω).run s)] :
(fkEstimator α k m r K).errorProb R s = ↑{ω : Fin (2 * r) → Fin K → Fin m | ¬R s ((fkSketch k m r K ω).run s)}.dens

The error of the randomized F_k sketch is the fraction of bad sampled positions.

theorem Cslib.Streaming.solves_fkEstimator {α : Type u_1} [DecidableEq α] {k m r K : ℕ} [Fintype α] [NeZero m] (hk : k ≠ 0) {η δ : ℝ} (hη : 0 < η) (hK : (3 * ↑k) ^ k * ↑(Fintype.card α) ^ (k - 1) ≤ (↑K * η ^ 2) ^ k) (hδ : (8 / 9) ^ r ≤ δ) :
(fkEstimator α k m r K).Solves δ (fun (s : List α) (Y : ℝ) => s.length = m → |Y - ↑(freqMoment k s)| ≤ η * ↑(freqMoment k s)) m

The AMS sketch solves F_k estimation [AlonMatiasSzegedy1999, Theorem 2.1]: for items from a universe of size n, if k ≠ 0, η > 0, (3 k) ^ k n ^ (k - 1) ≤ (K η²) ^ k and (8 / 9) ^ r ≤ δ, the randomized F_k sketch for streams of m items estimates F_k within η F_k with error at most δ on every stream of m items.

theorem Cslib.Streaming.solves_fkEstimator_of_rpow {α : Type u_1} [DecidableEq α] {k m r K : ℕ} [Fintype α] [NeZero m] (hk : k ≠ 0) {η δ : ℝ} (hη : 0 < η) (hK : 3 * ↑k * ↑(Fintype.card α) ^ (1 - 1 / ↑k) ≤ ↑K * η ^ 2) (hδ : (8 / 9) ^ r ≤ δ) :
(fkEstimator α k m r K).Solves δ (fun (s : List α) (Y : ℝ) => s.length = m → |Y - ↑(freqMoment k s)| ≤ η * ↑(freqMoment k s)) m

Cslib.Streaming.solves_fkEstimator with the group size condition 3 k n ^ (1 - 1/k) ≤ K η² of [AlonMatiasSzegedy1999, proof of Theorem 2.1].

Memory #

theorem Cslib.Streaming.spaceLE_fkEstimator {α : Type u_1} [DecidableEq α] {k m r K : ℕ} [Fintype α] [NeZero m] {M b : ℕ} (hb : (M + 1) * ((Fintype.card α + 1) * (M + 1)) ^ (2 * r * K) ≤ 2 ^ b) :
(fkEstimator α k m r K).SpaceLE M b

The memory of the randomized F_k sketch [AlonMatiasSzegedy1999, Theorem 2.1]: for items from a universe of size n, every sketch it draws uses at most b bits of memory on streams of at most M items when (M + 1) ((n + 1) (M + 1)) ^ (2 r K) ≤ 2 ^ b. Under the public-coin convention of Cslib.Streaming.Randomized the sampled positions are not charged; for the count with the positions stored see Cslib.Streaming.exists_injOn_iUnion_reachable_fkSketch.

theorem Cslib.Streaming.exists_injOn_iUnion_reachable_fkSketch {α : Type u_1} [DecidableEq α] {k m r K : ℕ} [Fintype α] {M b : ℕ} (hb : m ^ (2 * r * K) * ((M + 1) * ((Fintype.card α + 1) * (M + 1)) ^ (2 * r * K)) ≤ 2 ^ b) :
∃ (enc : FkState α m r K → BitString b), Set.InjOn enc (⋃ (ω : Fin (2 * r) → Fin K → Fin m), (fkSketch k m r K ω).reachable M)

The memory of the F_k sketch with its positions stored [AlonMatiasSzegedy1999, Theorem 2.1]: on streams of at most M items, the states reachable from all sampled positions together have a single injective encoding by b bits as soon as m ^ (2 r K) (M + 1) ((n + 1) (M + 1)) ^ (2 r K) ≤ 2 ^ b. Since only the initial memory depends on the positions (Cslib.Streaming.update_fkSketch, Cslib.Streaming.output_fkSketch), this is the memory of one algorithm whose initial memory is random, with the positions charged.