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 #
Cslib.Streaming.suffixCount: occurrences of the item at a position from that position on.Cslib.Streaming.fkBasicEstimate: the basic estimatorm (r ^ k - (r - 1) ^ k).Cslib.Streaming.FkState,Cslib.Streaming.fkSketch: the memory record andF_ksketch with given sampled positions.Cslib.Streaming.fkEstimator: the randomizedF_ksketch with uniform positions.
Main results #
Cslib.Streaming.expect_fkBasicEstimate,Cslib.Streaming.expect_fkBasicEstimate_sq_le: the basic estimator has meanF_kand second moment at mostk F₁ F_{2k-1}.Cslib.Streaming.length_mul_freqMoment_pow_le: the power-sum inequality for streams,(F₁ F_{2k+1}) ^ (k + 1) ≤ n ^ k F_{k+1} ^ (2 (k + 1)).Cslib.Streaming.dens_fkSketch_error_le,Cslib.Streaming.solves_fkEstimator,Cslib.Streaming.solves_fkEstimator_of_rpow: the sketch estimatesF_kwithinλ F_kexcept with probability(8 / 9) ^ r[AlonMatiasSzegedy1999, Theorem 2.1].Cslib.Streaming.spaceLE_fkEstimator,Cslib.Streaming.exists_injOn_iUnion_reachable_fkSketch: its memory without and with the sampled positions stored.
References #
The basic estimator #
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.
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.
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.
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.
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].
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.
The second moment of the basic estimator [AlonMatiasSzegedy1999, proof of Theorem 2.1]:
𝔼 X² ≤ k F₁ F_{2k-1}, for k ≠ 0.
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.
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 #
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.
Sampled stream positions for each of the
2 rgroups andKcopies.- step : ℕ
Number of stream items processed so far.
Per-copy sampled item (
nonebefore the sampled position arrives) and suffix count.
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).
The randomized AMS F_k sketch: the sketch with independent uniform positions.
The F_k sketch starts with its positions, step zero and empty copies.
The update of the F_k sketch reads the positions from the memory: it does not depend on
ω.
The output of the F_k sketch does not depend on its positions.
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.
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.
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.
The error of the randomized F_k sketch is the fraction of bad sampled positions.
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.
Cslib.Streaming.solves_fkEstimator with the group size condition
3 k n ^ (1 - 1/k) ≤ K η² of [AlonMatiasSzegedy1999, proof of Theorem 2.1].
Memory #
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.
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.