Confound.lean
Lean 4 source, lean/DataMiningAsObservation/Confound.lean at commit bccca4f. Appendix C, Machine-checked statements, says what each file proves. All Lean files.
/-
Confound.lean. Chapter 8 section 8.6, the confound.
A confound moves with both the treatment and the outcome. In the linear case the outcome is
the treatment effect times the treatment plus the confound's effect times the confound, and
the naive difference of group means equals the treatment effect plus the confound's effect
times the difference of the confound's means between the groups. The naive difference is
the treatment effect exactly when the confound is balanced or has no effect, which is what
a control establishes and what randomization buys.
-/
import Mathlib
namespace DMO.Confound
open Finset
variable {ι : Type*}
/-- The mean of `f` over a nonempty group. -/
noncomputable def mean (G : Finset ι) (f : ι → ℝ) : ℝ := (∑ i ∈ G, f i) / G.card
/-- The linear outcome with treatment effect `a` and confound effect `b`. -/
def outcome (a b : ℝ) (t z : ι → ℝ) (i : ι) : ℝ := a * t i + b * z i
/-- The mean of a linear outcome is the linear combination of the means. -/
theorem mean_outcome (G : Finset ι) (a b : ℝ) (t z : ι → ℝ) :
mean G (outcome a b t z) = a * mean G t + b * mean G z := by
unfold mean outcome
rw [Finset.sum_add_distrib, ← Finset.mul_sum, ← Finset.mul_sum]
ring
/-- **The naive difference is the effect plus the confound's imbalance.** With every treated
row at treatment one and every control row at treatment zero, the difference of outcome
means is `a + b (z̄₁ − z̄₀)`. -/
theorem naive_diff (T C : Finset ι) (hT : T.Nonempty) (a b : ℝ) (t z : ι → ℝ)
(ht1 : ∀ i ∈ T, t i = 1) (ht0 : ∀ i ∈ C, t i = 0) :
mean T (outcome a b t z) - mean C (outcome a b t z) = a + b * (mean T z - mean C z) := by
rw [mean_outcome, mean_outcome]
have h1 : mean T t = 1 := by
unfold mean
rw [Finset.sum_congr rfl ht1, Finset.sum_const, nsmul_eq_mul, mul_one]
exact div_self (by exact_mod_cast hT.card_pos.ne')
have h0 : mean C t = 0 := by
unfold mean
rw [Finset.sum_congr rfl ht0, Finset.sum_const_zero, zero_div]
rw [h1, h0]
ring
/-- **A balanced confound cannot bias the difference.** -/
theorem naive_diff_of_balanced (T C : Finset ι) (hT : T.Nonempty) (a b : ℝ)
(t z : ι → ℝ) (ht1 : ∀ i ∈ T, t i = 1) (ht0 : ∀ i ∈ C, t i = 0)
(hbal : mean T z = mean C z) :
mean T (outcome a b t z) - mean C (outcome a b t z) = a := by
rw [naive_diff T C hT a b t z ht1 ht0, hbal]
ring
/-- **A confound with no effect cannot bias the difference**, however unbalanced. -/
theorem naive_diff_of_no_effect (T C : Finset ι) (hT : T.Nonempty) (a : ℝ)
(t z : ι → ℝ) (ht1 : ∀ i ∈ T, t i = 1) (ht0 : ∀ i ∈ C, t i = 0) :
mean T (outcome a 0 t z) - mean C (outcome a 0 t z) = a := by
rw [naive_diff T C hT a 0 t z ht1 ht0]
ring
/-- **The bias can have either sign and any size.** For any target difference there is an
imbalance that produces it. -/
theorem bias_unbounded (a b : ℝ) (hb : b ≠ 0) (target : ℝ) :
∃ imbalance : ℝ, a + b * imbalance = target :=
⟨(target - a) / b, by field_simp; ring⟩
end DMO.Confound