Logistic.lean

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

/-
Logistic.lean. Chapter 6 section 6.1 and equation 6.1, logistic regression.

Logistic regression scores by the sigmoid of a weighted sum. The sigmoid lies strictly
between zero and one, is one half at zero, is symmetric about one half, and is strictly
increasing, so the decision at threshold one half is the sign of the weighted sum and the
decision boundary is the hyperplane.
-/
import Mathlib

namespace DMO.Logistic

/-- The sigmoid. -/
noncomputable def sigmoid (z : ℝ) : ℝ := 1 / (1 + Real.exp (-z))

/-- **The sigmoid is positive.** -/
theorem sigmoid_pos (z : ℝ) : 0 < sigmoid z := by
  unfold sigmoid
  positivity

/-- **The sigmoid is below one.** -/
theorem sigmoid_lt_one (z : ℝ) : sigmoid z < 1 := by
  unfold sigmoid
  rw [div_lt_one (by positivity)]
  linarith [Real.exp_pos (-z)]

/-- **The sigmoid is one half at zero.** -/
theorem sigmoid_zero : sigmoid 0 = 1 / 2 := by
  norm_num [sigmoid]

/-- **The sigmoid is symmetric about one half.** -/
theorem sigmoid_neg (z : ℝ) : sigmoid (-z) = 1 - sigmoid z := by
  unfold sigmoid
  rw [neg_neg, Real.exp_neg]
  have h := Real.exp_pos z
  field_simp
  ring

/-- **The sigmoid is strictly increasing.** -/
theorem sigmoid_strictMono : StrictMono sigmoid := by
  intro z w hzw
  unfold sigmoid
  apply one_div_lt_one_div_of_lt (by positivity)
  linarith [Real.exp_lt_exp.mpr (neg_lt_neg hzw)]

/-- **The decision at one half is the sign of the score.** -/
theorem decision_iff (z : ℝ) : 1 / 2 < sigmoid z ↔ 0 < z := by
  rw [← sigmoid_zero]
  exact sigmoid_strictMono.lt_iff_lt

/-- **The decision boundary of a linear classifier is the hyperplane.** -/
theorem decision_linear {n : ℕ} (w x : Fin n → ℝ) (b : ℝ) :
    1 / 2 < sigmoid (w ⬝ᵥ x + b) ↔ 0 < w ⬝ᵥ x + b :=
  decision_iff _

end DMO.Logistic