Documentation

CslibFrontier.Computability.Streaming.DistinctElements

The k-minimum-values sketch for the number of distinct elements #

The number of distinct items of a stream is F₀ = #s.toFinset (Cslib.Streaming.freqMoment_zero). Bar-Yossef, Jayram, Kumar, Sivakumar and Trevisan estimate it in small space [BarYossefJayramKumarSivakumarTrevisan2002, Theorem 1]. Hash the items by a pairwise independent family h θ : α → Fin M and keep the t smallest hash values seen. If d distinct items have been read, the t-th smallest of their d hash values is near t M / d, so t M divided by it estimates d.

The sketch. Each copy (Cslib.Streaming.kmvSketch) keeps the t smallest keys (h θ a, a) (Cslib.Streaming.kmvKey) in lexicographic order, selected by Finset.lowest. Each item is inserted into the selection, and the selection is cut back to t keys. Since selection can be maintained online (Finset.lowest_insert_lowest), after the stream s the copy holds the t lowest keys of the distinct items of s (Cslib.Streaming.state_kmvSketch). Ties between hash values are broken by the item, so collisions never merge two distinct items. While it holds fewer than t keys, a copy reports their number, which is then exactly F₀. Otherwise it reports t M / (v + 1), where v is the largest hash value it keeps (Cslib.Streaming.kmvEstimate); hash values are counted from 0, so v + 1 is the source's t-th smallest value in [1, M]. The sketch answers the median of the estimates of its copies.

Accuracy. The t lowest hash values of d ≥ t items are all below K exactly when at least t items hash below K (Finset.forall_mem_lowest_iff). The number of items hashing below K has mean d K / M and variance at most its mean (Cslib.Probability.IsPairwiseIndependentHash.dens_le_abs_card_filter_apply_mem_sub_le), and Chebyshev's inequality bounds each side. For 0 < ε ≤ 1 and d ≤ M, one copy misses d by more than ε d for at most a 2 / (t ε²) fraction of seeds (Cslib.Streaming.dens_abs_kmvEstimate_sub_gt_le). The two tails contribute (1 + ε) / (t ε²) and (1 - ε) / (t ε²); the source bounds each by 1 / 6 with t = ⌈96 / ε²⌉. For 6 ≤ t ε² one copy thus fails with probability at most 1 / 3. The median trick (Cslib.Probability.dens_abs_median_sub_gt_le, [Chakrabarti2025, Section 2.4]) brings the failure probability of the median of 2 r copies down to (8 / 9) ^ r (Cslib.Streaming.dens_kmvSketch_error_le), so kmv h t (2 r) solves F₀ estimation with relative error ε and error probability δ ≥ (8 / 9) ^ r on streams of length at most M (Cslib.Streaming.solves_kmv).

Memory. For fixed seeds, the reachable states are determined by the at most t items behind each copy's keys. Their number is at most (n + 1) ^ t for a universe of n items. Under the public-coin convention of Cslib.Streaming.Randomized, every sketch with c copies uses at most b bits as soon as (n + 1) ^ (c t) ≤ 2 ^ b (Cslib.Streaming.spaceLE_kmv), on streams of any length. With the seeds stored as well, (|ι| (n + 1) ^ t) ^ c ≤ 2 ^ b suffices (Cslib.Streaming.exists_injOn_iUnion_reachable_kmvSketch). With t = ⌈6 / ε²⌉ and c of order log (1 / δ) this is O(ε⁻² log (1 / δ) log n) bits, the bound of [BarYossefJayramKumarSivakumarTrevisan2002, Theorem 1] (the source writes m for the size of the universe).

Differences from the source. The source keeps bare hash values in [1, m³] and pays a 1 / m probability that h is not injective on the stream. Keeping the item beside the hash removes that failure, and needs only d ≤ M rather than M = m³. The memory is still O(log n) bits per key, since the key is determined by the item and the seed. The seeds are drawn as public coins and are not charged by Cslib.Streaming.spaceLE_kmv.

This upper bound complements the Ω(n) lower bounds for computing F₀ exactly with bounded error and for approximating it deterministically (Cslib.Streaming.le_of_solves_freqMoment, Cslib.Streaming.Algorithm.le_of_approx_freqMoment with k = 0): randomness together with approximation brings the memory down to O(ε⁻² log n) bits for constant error. The matching Ω(ε⁻²) lower bound is not formalized.

Main definitions #

Main results #

References #

The sketch #

def Cslib.Streaming.kmvKey {ι : Type u_1} {α : Type u_2} {M : ℕ} (h : ι → α → Fin M) (θ : ι) (a : α) :
Lex (Fin M × α)

The sort key of the item a under the seed θ: its hash value, ties broken by the item.

def Cslib.Streaming.kmvMaxHash {α : Type u_2} {M : ℕ} (S : Finset (Lex (Fin M × α))) :

The largest hash value among a set of keys (zero for the empty set).

noncomputable def Cslib.Streaming.kmvEstimate {α : Type u_2} (M t : ℕ) (S : Finset (Lex (Fin M × α))) :

The estimate of F₀ from a set S of at most t keys: their number if there are fewer than t, and otherwise t M / (v + 1), where v is the largest hash value in S.

@[reducible, inline]
abbrev Cslib.Streaming.KMVState (ι : Type u_3) (α : Type u_4) (M c : ℕ) :
Type (max u_4 u_3)

The memory of the k-minimum-values sketch with c copies: a seed and a set of keys for each copy.

noncomputable def Cslib.Streaming.kmvSketch {ι : Type u_1} {α : Type u_2} [LinearOrder α] {M : ℕ} (h : ι → α → Fin M) (t c : ℕ) (ω : Fin c → ι) :
Algorithm (KMVState ι α M c) α ℝ

The k-minimum-values sketch [BarYossefJayramKumarSivakumarTrevisan2002, Theorem 1] with seeds ω: each copy i keeps the t lowest keys kmvKey h (ω i) a of the items read, and the answer is the median over the copies of their estimates Cslib.Streaming.kmvEstimate. The seeds are written in the initial memory and read back by the update, which does not otherwise depend on ω (Cslib.Streaming.update_kmvSketch).

noncomputable def Cslib.Streaming.kmv {ι : Type u_1} {α : Type u_2} [LinearOrder α] {M : ℕ} [Fintype ι] [Nonempty ι] (h : ι → α → Fin M) (t c : ℕ) :
Randomized (KMVState ι α M c) α ℝ

The randomized k-minimum-values sketch: the sketch with independent uniform seeds.

theorem Cslib.Streaming.kmvKey_injective {ι : Type u_1} {α : Type u_2} {M : ℕ} (h : ι → α → Fin M) (θ : ι) :
theorem Cslib.Streaming.kmvEstimate_of_card_lt {α : Type u_2} {M t : ℕ} {S : Finset (Lex (Fin M × α))} (hS : S.card < t) :
kmvEstimate M t S = ↑S.card

With fewer than t keys, the estimate is their number.

theorem Cslib.Streaming.kmvEstimate_of_le_card {α : Type u_2} {M t : ℕ} {S : Finset (Lex (Fin M × α))} (hS : t ≤ S.card) :
kmvEstimate M t S = ↑t * ↑M / (↑(kmvMaxHash S) + 1)

With at least t keys, the estimate is t M / (v + 1) for the largest hash value v.

theorem Cslib.Streaming.init_kmvSketch {ι : Type u_1} {α : Type u_2} [LinearOrder α] {M t c : ℕ} {h : ι → α → Fin M} (ω : Fin c → ι) :
(kmvSketch h t c ω).init = (ω, fun (x : Fin c) => ∅)

The sketch starts with its seeds and no keys.

theorem Cslib.Streaming.update_kmvSketch {ι : Type u_1} {α : Type u_2} [LinearOrder α] {M t c : ℕ} {h : ι → α → Fin M} (ω : Fin c → ι) (q : KMVState ι α M c) (a : α) :
(kmvSketch h t c ω).update q a = (q.1, fun (i : Fin c) => Finset.lowest t (insert (kmvKey h (q.1 i) a) (q.2 i)))

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

theorem Cslib.Streaming.output_kmvSketch {ι : Type u_1} {α : Type u_2} [LinearOrder α] {M t c : ℕ} {h : ι → α → Fin M} (ω : Fin c → ι) (q : KMVState ι α M c) :
(kmvSketch h t c ω).output q = median fun (i : Fin c) => kmvEstimate M t (q.2 i)

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

theorem Cslib.Streaming.stateFrom_kmvSketch {ι : Type u_1} {α : Type u_2} [LinearOrder α] {M t c : ℕ} {h : ι → α → Fin M} (ω : Fin c → ι) (T : Fin c → Finset (Lex (Fin M × α))) (s : List α) :
(kmvSketch h t c ω).stateFrom (ω, fun (i : Fin c) => Finset.lowest t (T i)) s = (ω, fun (i : Fin c) => Finset.lowest t (Finset.image (kmvKey h (ω i)) s.toFinset ∪ T i))

From a memory holding the t lowest keys of the sets T i, reading s leads to the t lowest keys of T i together with the keys of the items of s.

theorem Cslib.Streaming.state_kmvSketch {ι : Type u_1} {α : Type u_2} [LinearOrder α] {M t c : ℕ} {h : ι → α → Fin M} (ω : Fin c → ι) (s : List α) :
(kmvSketch h t c ω).state s = (ω, fun (i : Fin c) => Finset.lowest t (Finset.image (kmvKey h (ω i)) s.toFinset))

The state of the sketch: after the stream s, the copy with seed ω i holds the t lowest keys of the distinct items of s.

theorem Cslib.Streaming.run_kmvSketch {ι : Type u_1} {α : Type u_2} [LinearOrder α] {M t c : ℕ} {h : ι → α → Fin M} (ω : Fin c → ι) (s : List α) :
(kmvSketch h t c ω).run s = median fun (i : Fin c) => kmvEstimate M t (Finset.lowest t (Finset.image (kmvKey h (ω i)) s.toFinset))

The answer of the sketch is the median over the copies of the estimate from the t lowest keys of the distinct items.

theorem Cslib.Streaming.card_image_kmvKey {ι : Type u_1} {α : Type u_2} [LinearOrder α] {M : ℕ} {h : ι → α → Fin M} (θ : ι) (D : Finset α) :

Distinct items have distinct keys, so a set of d items has d keys.

theorem Cslib.Streaming.run_kmvSketch_of_card_lt {ι : Type u_1} {α : Type u_2} [LinearOrder α] {M t c : ℕ} {h : ι → α → Fin M} [NeZero c] (ω : Fin c → ι) {s : List α} (hs : s.toFinset.card < t) :
(kmvSketch h t c ω).run s = ↑s.toFinset.card

Exactness on few distinct items: on a stream with fewer than t distinct items, every copy holds all their keys, so the sketch answers F₀ exactly.

Accuracy of one copy #

theorem Cslib.Streaming.dens_abs_kmvEstimate_sub_gt_le {ι : Type u_1} {α : Type u_2} [LinearOrder α] {M t : ℕ} {h : ι → α → Fin M} [Fintype ι] {D : Finset α} (hh : Probability.IsPairwiseIndependentHash h) {ε : ℝ} (hε : 0 < ε) (hε1 : ε ≤ 1) (ht : 0 < t) (hDM : D.card ≤ M) :
↑{θ : ι | ε * ↑D.card < |kmvEstimate M t (Finset.lowest t (Finset.image (kmvKey h θ) D)) - ↑D.card|}.dens ≤ 2 / (↑t * ε ^ 2)

One copy estimates F₀ [BarYossefJayramKumarSivakumarTrevisan2002, proof of Theorem 1]: for a pairwise independent family h θ : α → Fin M, a set D of d ≤ M items, 0 < ε ≤ 1 and t > 0, the estimate from the t lowest keys of D misses d by more than ε d for at most a 2 / (t ε²) fraction of the seeds. The source proves 1 / 6 + 1 / 6 for t = ⌈96 / ε²⌉, plus a 1 / m term for hash collisions, which the tie-breaking keys avoid.

theorem Cslib.Streaming.errorProb_kmv {ι : Type u_1} {α : Type u_2} [LinearOrder α] {M t c : ℕ} {h : ι → α → Fin M} [Fintype ι] [Nonempty ι] (R : List α → ℝ → Prop) (s : List α) [DecidablePred fun (ω : Fin c → ι) => ¬R s ((kmvSketch h t c ω).run s)] :
(kmv h t c).errorProb R s = ↑{ω : Fin c → ι | ¬R s ((kmvSketch h t c ω).run s)}.dens

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

Amplification #

theorem Cslib.Streaming.dens_kmvSketch_error_le {ι : Type u_1} {α : Type u_2} [LinearOrder α] {M t : ℕ} {h : ι → α → Fin M} [Fintype ι] [Nonempty ι] (hh : Probability.IsPairwiseIndependentHash h) {ε : ℝ} (hε : 0 < ε) (hε1 : ε ≤ 1) (ht : 6 ≤ ↑t * ε ^ 2) {s : List α} (hsM : s.toFinset.card ≤ M) (r : ℕ) :
↑{ω : Fin (2 * r) → ι | ε * ↑s.toFinset.card < |(kmvSketch h t (2 * r) ω).run s - ↑s.toFinset.card|}.dens ≤ (8 / 9) ^ r

The median of 2 r copies misses F₀ by more than ε F₀ for at most an (8 / 9) ^ r fraction of seeds once 6 ≤ t ε² [BarYossefJayramKumarSivakumarTrevisan2002, Theorem 1]: one copy fails for at most a third of the seeds, and the median trick (Cslib.Probability.dens_abs_median_sub_gt_le) amplifies this.

theorem Cslib.Streaming.solves_kmv {ι : Type u_1} {α : Type u_2} [LinearOrder α] {M t : ℕ} {h : ι → α → Fin M} [Fintype ι] [Nonempty ι] (hh : Probability.IsPairwiseIndependentHash h) {ε δ : ℝ} (hε : 0 < ε) (hε1 : ε ≤ 1) (ht : 6 ≤ ↑t * ε ^ 2) {r : ℕ} (hδ : (8 / 9) ^ r ≤ δ) {m : ℕ} (hm : m ≤ M) :
(kmv h t (2 * r)).Solves δ (fun (s : List α) (Y : ℝ) => |Y - ↑(freqMoment 0 s)| ≤ ε * ↑(freqMoment 0 s)) m

The randomized sketch with 2 r copies solves F₀ estimation with relative error ε and failure probability δ ≥ (8 / 9) ^ r on streams of length at most M [BarYossefJayramKumarSivakumarTrevisan2002, Theorem 1].

Memory #

theorem Cslib.Streaming.spaceLE_kmv {ι : Type u_1} {α : Type u_2} [LinearOrder α] {M t c : ℕ} {h : ι → α → Fin M} [Fintype ι] [Nonempty ι] [Fintype α] {m b : ℕ} (hb : (Fintype.card α + 1) ^ (c * t) ≤ 2 ^ b) :
(kmv h t c).SpaceLE m b

The memory of the randomized sketch [BarYossefJayramKumarSivakumarTrevisan2002, Theorem 1]: over a universe of n items, every sketch it draws uses at most b bits of memory on streams of any length m as soon as (n + 1) ^ (c t) ≤ 2 ^ b. Under the public-coin convention of Cslib.Streaming.Randomized the seeds are not charged; each copy is charged for the at most t items behind its keys. For the count with the seeds stored see Cslib.Streaming.exists_injOn_iUnion_reachable_kmvSketch.

theorem Cslib.Streaming.exists_injOn_iUnion_reachable_kmvSketch {ι : Type u_1} {α : Type u_2} [LinearOrder α] {M t c : ℕ} {h : ι → α → Fin M} [Fintype ι] [Fintype α] {m b : ℕ} (hb : (Fintype.card ι * (Fintype.card α + 1) ^ t) ^ c ≤ 2 ^ b) :
∃ (enc : KMVState ι α M c → BitString b), Set.InjOn enc (⋃ (ω : Fin c → ι), (kmvSketch h t c ω).reachable m)

The memory of the sketch with its seeds stored [BarYossefJayramKumarSivakumarTrevisan2002, Theorem 1]: over a universe of n items, the states reachable from all seeds together have a single injective encoding by b bits as soon as (|ι| (n + 1) ^ t) ^ c ≤ 2 ^ b. Since only the initial memory depends on the seeds (Cslib.Streaming.update_kmvSketch, Cslib.Streaming.output_kmvSketch), this is the memory of one algorithm whose initial memory is random, with the seeds charged.