Confidence.lean

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

/-
Confidence.lean. Equations 5.1 and 5.2, confidence.

The confidence of a rule is the support of the rule's itemset over the support of its
antecedent. It lies in the unit interval, it is not symmetric, and it misleads when the
consequent is common. Under independence the confidence equals the consequent's support,
whatever the antecedent, so a common consequent gives a high confidence with no association
at all. Lift is confidence over the consequent's support, and is one in that case.
-/
import Mathlib

namespace DMO.Confidence

/-- **Equation 5.1.** The confidence of the rule from the supports. -/
noncomputable def confidence (sx sxy : ℝ) : ℝ := sxy / sx

/-- **In the unit interval**, since the rule's itemset is no more frequent than its antecedent. -/
theorem confidence_mem_unit (sx sxy : ℝ) (hx : 0 < sx) (h0 : 0 ≤ sxy) (h : sxy ≤ sx) :
    0 ≤ confidence sx sxy ∧ confidence sx sxy ≤ 1 := by
  unfold confidence
  refine ⟨div_nonneg h0 hx.le, ?_⟩
  rw [div_le_one hx]
  exact h

/-- **Confidence is not symmetric.** The rule and its reverse have confidences in the ratio of
the two supports. -/
theorem confidence_asymm (sx sy sxy : ℝ) (hx : 0 < sx) (hy : 0 < sy) :
    confidence sx sxy * sx = confidence sy sxy * sy := by
  unfold confidence
  field_simp

/-- **Under independence the confidence is the consequent's support**, whatever the antecedent. -/
theorem confidence_indep (sx sy : ℝ) (hx : 0 < sx) : confidence sx (sx * sy) = sy := by
  unfold confidence
  field_simp

/-- **A common consequent gives a high confidence with no association.** Under independence
with the consequent in nine of ten transactions, every rule into it has confidence 0.9. -/
theorem common_consequent (sx : ℝ) (hx : 0 < sx) : confidence sx (sx * 0.9) = 0.9 :=
  confidence_indep sx 0.9 hx

/-- **Equation 5.2 from confidence.** Lift is confidence over the consequent's support, one
under independence. -/
theorem lift_of_indep (sx sy : ℝ) (hx : 0 < sx) (hy : 0 < sy) :
    confidence sx (sx * sy) / sy = 1 := by
  rw [confidence_indep sx sy hx]
  exact div_self hy.ne'

end DMO.Confidence