Documentation

CslibFrontier.Computability.Streaming.Frequency

Frequency statistics of streams #

The frequency of an item a in a stream s : List α is s.count a. The k-th frequency moment is F_k = ∑ₐ (count a s) ^ k (Cslib.Streaming.freqMoment), and the largest frequency is F∞ = maxₐ count a s (Cslib.Streaming.maxFreq), the statistics studied by Alon, Matias and Szegedy [AMS99]. F₀ is the number of distinct items, F₁ the length of the stream, and the items of a stream are distinct exactly when F∞ ≤ 1 (Cslib.Streaming.maxFreq_le_one_iff).

The join size of two streams u and v is the inner product of their frequency vectors, Cslib.Streaming.joinSize u v = ∑ₐ count a u · count a v. Read as lists of the join attribute values of two relations, it is the number of tuples in their equijoin: for key functions k and l, the pairs (a, b) of u ×ˢ v with k a = l b number joinSize (u.map k) (v.map l) (Cslib.Streaming.length_filter_product_eq_joinSize). When a key may be absent (k a = none), as SQL NULL keys are, a pair matches only on a common present key, and the pairs number joinSize (u.filterMap k) (v.filterMap l) (Cslib.Streaming.length_filter_product_eq_joinSize_filterMap). The self-join is F₂, and joinSize u v ^ 2 ≤ F₂ u F₂ v. The join-size sketch of Alon, Gibbons, Matias and Szegedy is in CslibFrontier.Computability.Streaming.JoinSize.

Main definitions #

Main results #

References #

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

The k-th frequency moment F_k = ∑ₐ (count a s) ^ k of a stream.

Instances For

    F₀ is the number of distinct items of the stream.

    theorem Cslib.Streaming.freqMoment_map {α : Type u_1} [DecidableEq α] {α' : Type u_2} [DecidableEq α'] {f : α → α'} (hf : Function.Injective f) (k : ℕ) (s : List α) :

    Renaming the items of a stream injectively keeps its frequency moments.

    theorem Cslib.Streaming.freqMoment_eq_sum_of_subset {α : Type u_1} [DecidableEq α] {k : ℕ} (hk : k ≠ 0) {s : List α} {T : Finset α} (hT : s.toFinset ⊆ T) :
    freqMoment k s = ∑ a ∈ T, List.count a s ^ k

    For k ≠ 0, F_k is the sum of (count a s) ^ k over any set of items containing those of s.

    theorem Cslib.Streaming.freqMoment_cons_add {α : Type u_1} [DecidableEq α] {k : ℕ} (hk : k ≠ 0) (a : α) (s : List α) :
    freqMoment k (a :: s) + List.count a s ^ k = freqMoment k s + (List.count a s + 1) ^ k

    Prepending an item a to the stream replaces the term (count a s) ^ k of F_k by (count a s + 1) ^ k, for k ≠ 0.

    theorem Cslib.Streaming.freqMoment_pos {α : Type u_1} [DecidableEq α] (k : ℕ) {s : List α} (hs : s ≠ []) :

    The frequency moments of a nonempty stream are positive.

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

    The largest number of occurrences of an item in the stream s (F∞; zero for the empty stream).

    Instances For
      theorem Cslib.Streaming.maxFreq_le_one_of_nodup {α : Type u_1} [DecidableEq α] {s : List α} (hs : s.Nodup) :
      theorem Cslib.Streaming.one_le_maxFreq {α : Type u_1} [DecidableEq α] {s : List α} (hs : s ≠ []) :

      The items of a stream are distinct exactly when no item occurs twice.

      Join sizes #

      def Cslib.Streaming.joinSize {α : Type u_1} [DecidableEq α] (u v : List α) :

      The join size |F ⋈ G| = ∑ₐ count a u · count a v of the relations whose join attribute values are listed by the streams u and v: the inner product of their frequency vectors.

      Instances For
        theorem Cslib.Streaming.joinSize_eq_sum_of_subset {α : Type u_1} [DecidableEq α] {u v : List α} {T : Finset α} (hT : u.toFinset ⊆ T) :
        joinSize u v = ∑ a ∈ T, List.count a u * List.count a v

        The join size is the sum of count a u · count a v over any set of values containing those of u.

        theorem Cslib.Streaming.joinSize_comm {α : Type u_1} [DecidableEq α] (u v : List α) :

        The join size is symmetric: |F ⋈ G| = |G ⋈ F|.

        theorem Cslib.Streaming.joinSize_self {α : Type u_1} [DecidableEq α] (u : List α) :

        The self-join size of a relation is its second frequency moment.

        theorem Cslib.Streaming.joinSize_sq_le {α : Type u_1} [DecidableEq α] (u v : List α) :

        Join sizes are bounded by self-join sizes: |F ⋈ G|² ≤ F₂(F) F₂(G), by the Cauchy–Schwarz inequality.

        theorem Cslib.Streaming.joinSize_eq_sum_map_count {α : Type u_1} [DecidableEq α] (u v : List α) :
        joinSize u v = (List.map (fun (x : α) => List.count x v) u).sum

        The join size of u and v sums, over the items a of u with multiplicity, the number of occurrences of a in v.

        theorem Cslib.Streaming.length_filter_product_eq_joinSize_filterMap {α : Type u_1} [DecidableEq α] {β : Type u_2} {γ : Type u_3} (k : β → Option α) (l : γ → Option α) (u : List β) (v : List γ) :
        (List.filter (fun (x : β × γ) => match x with | (a, b) => decide (∃ (c : α), k a = some c ∧ l b = some c)) (u ×ˢ v)).length = joinSize (List.filterMap k u) (List.filterMap l v)

        Equijoin cardinality with absent keys. If the tuples a of u and b of v have optional join keys k a and l b, the pairs (a, b) of u ×ˢ v with a common present key number joinSize (u.filterMap k) (v.filterMap l): a tuple without a key (none, such as an SQL NULL key) matches nothing, not even another tuple without a key.

        theorem Cslib.Streaming.length_filter_product_eq_joinSize {α : Type u_1} [DecidableEq α] {β : Type u_2} {γ : Type u_3} (k : β → α) (l : γ → α) (u : List β) (v : List γ) :
        (List.filter (fun (x : β × γ) => match x with | (a, b) => decide (k a = l b)) (u ×ˢ v)).length = joinSize (List.map k u) (List.map l v)

        Equijoin cardinality. If the tuples a of u and b of v have join keys k a and l b, the pairs (a, b) of u ×ˢ v with k a = l b number joinSize (u.map k) (v.map l).