Classifier.lean
Lean 4 source, lean/DataMiningAsObservation/Classifier.lean at commit bccca4f. Appendix C, Machine-checked statements, says what each file proves. All Lean files.
/-
Classifier.lean. Equation 6.1, the classifier as a consumer.
A linear classifier scores a row by a weight direction and a bias and passes the score
through a link. Its sensitivity at a row is the link's slope times the weight direction, so
its read operator is the workload mean of the squared slope times the outer product of the
weight direction. That operator sends every vector to a multiple of the weight direction,
which is what rank one means, and it reads nothing orthogonal to the weights. The score
itself is affine, so chapter 0's exact quotient applies to it before the link.
-/
import Mathlib
import DataMiningAsObservation.ReadOperator
namespace DMO.Classifier
open Finset Matrix DMO.ReadOperator
variable {Ω : Type*} [Fintype Ω] {n : ℕ}
/-- The score of equation 6.1. -/
def score (w : Fin n → ℝ) (b : ℝ) (x : Fin n → ℝ) : ℝ := w ⬝ᵥ x + b
/-- The sensitivity of the linked score at a row with link slope `σ'`. -/
def sensitivity (w : Fin n → ℝ) (slope : ℝ) : Fin n → ℝ := slope • w
/-- **Equation 6.1.** The read operator of a linear classifier is the workload mean of the
squared slope times the outer product of the weight direction. -/
theorem readOp_classifier (wt : Ω → ℝ) (w : Fin n → ℝ) (slope : Ω → ℝ) :
readOp wt (fun ω => sensitivity w (slope ω)) = (∑ ω, wt ω * slope ω ^ 2) • vecMulVec w w := by
unfold readOp sensitivity
rw [Finset.sum_smul]
refine Finset.sum_congr rfl fun ω _ => ?_
rw [smul_vecMulVec, vecMulVec_smul, smul_smul, smul_smul]
congr 1
ring
/-- **Rank one.** The operator sends every vector to a multiple of the weight direction. -/
theorem readOp_classifier_mulVec (wt : Ω → ℝ) (w : Fin n → ℝ) (slope : Ω → ℝ) (v : Fin n → ℝ) :
readOp wt (fun ω => sensitivity w (slope ω)) *ᵥ v
= ((∑ ω, wt ω * slope ω ^ 2) * (w ⬝ᵥ v)) • w := by
rw [readOp_classifier]
ext i
simp only [mulVec, dotProduct, Matrix.smul_apply, vecMulVec_apply, Pi.smul_apply, smul_eq_mul]
rw [Finset.mul_sum, Finset.sum_mul]
exact Finset.sum_congr rfl fun j _ => by ring
/-- **Nothing orthogonal to the weights is read.** -/
theorem readOp_classifier_orth (wt : Ω → ℝ) (w : Fin n → ℝ) (slope : Ω → ℝ) (v : Fin n → ℝ)
(hv : w ⬝ᵥ v = 0) : readOp wt (fun ω => sensitivity w (slope ω)) *ᵥ v = 0 := by
rw [readOp_classifier_mulVec, hv, mul_zero, zero_smul]
/-- **The score is affine**, so moving orthogonally to the weights leaves it unchanged. -/
theorem score_orth (w : Fin n → ℝ) (b : ℝ) (x v : Fin n → ℝ) (hv : w ⬝ᵥ v = 0) :
score w b (x + v) = score w b x := by
unfold score
rw [dotProduct_add, hv, add_zero]
end DMO.Classifier