SafePruning.lean

Lean 4 source, lean/DataMiningAsObservation/SafePruning.lean at commit bccca4f. Appendix C, Machine-checked statements, says what each file proves. All Lean files.

/-
SafePruning.lean. Chapter 5, the Apriori license.

Support cannot increase when an itemset grows, so once an itemset is infrequent every
superset is infrequent, and a search that prunes the supersets discards no frequent itemset.
Transactions are indexed by a finite type, each holding a finite set of items.
-/
import Mathlib

namespace DMO.SafePruning

variable {α ι : Type*} [DecidableEq α]

/-- The support count of an itemset `X` over the transactions `T`, the number of
transactions that contain every item of `X`. -/
def support (T : Finset ι) (tx : ι → Finset α) (X : Finset α) : ℕ :=
  (T.filter fun i => X ⊆ tx i).card

/-- **Anti-monotonicity.** A superset has support at most that of any subset. -/
theorem support_anti (T : Finset ι) (tx : ι → Finset α) {X Y : Finset α} (h : X ⊆ Y) :
    support T tx Y ≤ support T tx X := by
  unfold support
  apply Finset.card_le_card
  intro i hi
  rw [Finset.mem_filter] at hi ⊢
  exact ⟨hi.1, h.trans hi.2⟩

/-- **The Apriori principle.** If `X` falls below the minimum support, so does every
superset, which is the pruning rule and the reason it loses nothing. -/
theorem apriori (T : Finset ι) (tx : ι → Finset α) (minsup : ℕ) {X Y : Finset α}
    (h : X ⊆ Y) (hX : support T tx X < minsup) : support T tx Y < minsup :=
  lt_of_le_of_lt (support_anti T tx h) hX

/-- The contrapositive as the book states it. Every subset of a frequent itemset is frequent. -/
theorem subset_of_frequent (T : Finset ι) (tx : ι → Finset α) (minsup : ℕ) {X Y : Finset α}
    (h : X ⊆ Y) (hY : minsup ≤ support T tx Y) : minsup ≤ support T tx X :=
  le_trans hY (support_anti T tx h)

end DMO.SafePruning