Attribution.lean

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

/-
Attribution.lean. Chapter 14 section 14.5 and chapter 0 section 0.18, the attribution.

A gradient-based attribution assigns coordinate `i` the share `g i * (x i − x' i)` of the
move from a baseline `x'` to the row `x`. For an affine consumer those shares sum exactly to
the change in output, a coordinate the consumer does not read gets share zero, and the
weighted mean of the squared sensitivity is the read operator's diagonal entry, so an
averaged gradient attribution estimates the read operator of equation 0.9.
-/
import Mathlib
import DataMiningAsObservation.ReadOperator

namespace DMO.Attribution

open Finset DMO.ReadOperator

variable {Ω : Type*} [Fintype Ω] {n : ℕ}

/-- The gradient attribution of coordinate `i` for the move from `x'` to `x`. -/
def attr (g x x' : Fin n → ℝ) (i : Fin n) : ℝ := g i * (x i - x' i)

/-- **For an affine consumer the attributions sum exactly to the change in output.** -/
theorem attr_sum_affine (a : Fin n → ℝ) (b : ℝ) (x x' : Fin n → ℝ) :
    ∑ i, attr a x x' i = affine a b x - affine a b x' := by
  simp only [attr, affine, dotProduct, mul_sub, Finset.sum_sub_distrib]
  ring

/-- **A coordinate the consumer does not read gets attribution zero.** -/
theorem attr_unread (g x x' : Fin n → ℝ) (i : Fin n) (h : g i = 0) : attr g x x' i = 0 := by
  simp [attr, h]

/-- **The averaged squared sensitivity is the read operator's diagonal.** -/
theorem sq_sensitivity_eq_readOp_diag (w : Ω → ℝ) (g : Ω → Fin n → ℝ) (i : Fin n) :
    ∑ ω, w ω * g ω i ^ 2 = readOp w g i i := by
  rw [readOp_diag]

end DMO.Attribution