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 #
Cslib.Streaming.amsSketch: the AMS sketch with given seeds.Cslib.Streaming.ams: the randomized AMS sketch with uniform seeds.
Main results #
Cslib.Streaming.run_amsSketch: the answer is the median of means of the squared tug-of-war sums.Cslib.Streaming.init_amsSketch,Cslib.Streaming.update_amsSketch,Cslib.Streaming.output_amsSketch: only the initial memory depends on the seeds.Cslib.Streaming.dens_amsSketch_error_le,Cslib.Streaming.solves_ams: the AMS sketch estimatesF₂withinη F₂except with probability(8 / 9) ^ r.Cslib.Streaming.spaceLE_ams: its memory under the public-coin convention, seeds not charged.Cslib.Streaming.exists_injOn_iUnion_reachable_amsSketch: its memory with the seeds stored.
References #
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
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
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.
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.
The error of the randomized AMS sketch is the fraction of bad seeds.
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.
On streams of at most m items, every counter of the AMS sketch lies in [-m, m].
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.
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.