ItemSet.lean
Lean 4 source, lean/DataMiningAsObservation/ItemSet.lean at commit bccca4f. Appendix C, Machine-checked statements, says what each file proves. All Lean files.
/-
ItemSet.lean. Equations 5.1 and 5.3, itemsets and support.
The support of an itemset is the fraction of transactions that contain it. It lies in the
unit interval, the empty itemset has support one, a superset has support at most that of a
subset, and the support of a union is at most the smaller of the two supports. A closed
itemset has no superset with the same support and a maximal frequent itemset has no
frequent superset, and every maximal frequent itemset is closed, since a superset with the
same support would be frequent too.
-/
import Mathlib
import DataMiningAsObservation.SafePruning
namespace DMO.ItemSet
open Finset DMO.SafePruning
variable {α ι : Type*} [DecidableEq α]
/-- **Equation 5.1.** The support as a fraction of the transactions. -/
noncomputable def supportFrac (T : Finset ι) (tx : ι → Finset α) (X : Finset α) : ℝ :=
(support T tx X : ℝ) / T.card
/-- **Support lies in the unit interval.** -/
theorem supportFrac_mem_unit (T : Finset ι) (hT : T.Nonempty) (tx : ι → Finset α) (X : Finset α) :
0 ≤ supportFrac T tx X ∧ supportFrac T tx X ≤ 1 := by
unfold supportFrac support
have hpos : (0 : ℝ) < T.card := by exact_mod_cast hT.card_pos
refine ⟨by positivity, ?_⟩
rw [div_le_one hpos]
exact_mod_cast Finset.card_filter_le _ _
/-- **The empty itemset has support one.** -/
theorem supportFrac_empty (T : Finset ι) (hT : T.Nonempty) (tx : ι → Finset α) :
supportFrac T tx ∅ = 1 := by
unfold supportFrac support
rw [Finset.filter_true_of_mem fun i _ => Finset.empty_subset _]
exact div_self (by exact_mod_cast hT.card_pos.ne')
/-- **Equation 5.3 as a fraction.** A superset has support at most that of a subset. -/
theorem supportFrac_anti (T : Finset ι) (tx : ι → Finset α) {X Y : Finset α} (h : X ⊆ Y) :
supportFrac T tx Y ≤ supportFrac T tx X := by
unfold supportFrac
apply div_le_div_of_nonneg_right _ (by positivity)
exact_mod_cast support_anti T tx h
/-- **The support of a union is at most the smaller support.** -/
theorem supportFrac_union_le (T : Finset ι) (tx : ι → Finset α) (X Y : Finset α) :
supportFrac T tx (X ∪ Y) ≤ min (supportFrac T tx X) (supportFrac T tx Y) :=
le_min (supportFrac_anti T tx Finset.subset_union_left)
(supportFrac_anti T tx Finset.subset_union_right)
/-- A closed itemset has no strict superset with the same support. -/
def Closed (T : Finset ι) (tx : ι → Finset α) (X : Finset α) : Prop :=
∀ Y, X ⊂ Y → support T tx Y < support T tx X
/-- A maximal frequent itemset is frequent and has no frequent strict superset. -/
def MaximalFrequent (T : Finset ι) (tx : ι → Finset α) (minsup : ℕ) (X : Finset α) : Prop :=
minsup ≤ support T tx X ∧ ∀ Y, X ⊂ Y → support T tx Y < minsup
/-- **Every maximal frequent itemset is closed.** -/
theorem closed_of_maximal (T : Finset ι) (tx : ι → Finset α) (minsup : ℕ) (X : Finset α)
(h : MaximalFrequent T tx minsup X) : Closed T tx X := by
intro Y hXY
exact lt_of_lt_of_le (h.2 Y hXY) h.1
end DMO.ItemSet