Lean / Reader
Lean reader

WallisTransfer.lean

55 lines2 declarationsno sorryimports Mathlib.Tactic
  1. import Mathlib.Tactic
  2. namespace WallisTransfer
  3. /-- On a subset, removing a summand valued in [0,1] loses at most one
  4. unit of mass per removed element. -/
  5. private theorem sum_bounds {α : Type*} [DecidableEq α] (s t : Finset α)
  6. (hsub : t ⊆ s) (h : α → ℝ) (hn : ∀ x, 0 ≤ h x) (hu : ∀ x, h x ≤ 1) :
  7. (∑ x ∈ t, h x) ≤ ∑ x ∈ s, h x ∧
  8. (∑ x ∈ s, h x) ≤ (∑ x ∈ t, h x) + ((s.card : ℝ) - t.card) := by
  9. have decomp := Finset.sum_sdiff hsub (f := h)
  10. have lower : 0 ≤ ∑ x ∈ s \ t, h x := Finset.sum_nonneg (fun x _ => hn x)
  11. have upper : (∑ x ∈ s \ t, h x) ≤ (s \ t).card := by
  12. simpa using Finset.sum_le_sum (s:=s \ t) (fun x _ => hu x)
  13. rw [Finset.card_sdiff_of_subset hsub, Nat.cast_sub (Finset.card_le_card hsub)] at upper
  14. constructor <;> linarith
  15. /-- Changing from uniform encodings to uniform targets changes any
  16. [0,1]-valued mean by at most the fraction of exceptional encodings. -/
  17. theorem mean_transfer {α β : Type*} [DecidableEq α] [DecidableEq β]
  18. (e g : Finset α) (p : Finset β) (f : α → β) (h : β → ℝ)
  19. (hg : g ⊆ e) (hp : p.Nonempty) (into : g.image f ⊆ p)
  20. (inj : Set.InjOn f (↑g : Set α)) (onto : p ⊆ e.image f)
  21. (hn : ∀ x, 0 ≤ h x) (hu : ∀ x, h x ≤ 1) :
  22. |(∑ x ∈ p, h x) / p.card - (∑ x ∈ e, h (f x)) / e.card| ≤
  23. ((e.card : ℝ) - g.card) / e.card := by
  24. have hsize : p.card ≤ e.card := (Finset.card_le_card onto).trans Finset.card_image_le
  25. have posp : 0 < (p.card : ℝ) := by exact_mod_cast hp.card_pos
  26. have pose : 0 < (e.card : ℝ) := by exact_mod_cast hp.card_pos.trans_le hsize
  27. have lepe : (p.card : ℝ) ≤ e.card := by exact_mod_cast hsize
  28. have hsum := sum_bounds p (g.image f) into h hn hu
  29. rw [Finset.sum_image (fun _ hx _ hy hxy => inj hx hy hxy), Finset.card_image_of_injOn inj] at hsum
  30. have esum := sum_bounds e g hg (h ∘ f) (fun x => hn (f x)) (fun x => hu (f x))
  31. simp only [Function.comp_apply] at esum
  32. let q := (∑ x ∈ p, h x) / p.card
  33. have qmass : q*p.card = ∑ x ∈ p, h x := div_mul_cancel₀ _ (ne_of_gt posp)
  34. have q0 : 0 ≤ q := div_nonneg (Finset.sum_nonneg fun x _ => hn x) posp.le
  35. have q1 : q ≤ 1 := by
  36. apply (div_le_iff₀ posp).2
  37. simpa using Finset.sum_le_sum (s:=p) (fun x _ => hu x)
  38. have ld := mul_nonneg q0 (sub_nonneg.mpr lepe)
  39. have ud := mul_nonneg (sub_nonneg.mpr q1) (sub_nonneg.mpr lepe)
  40. change |q - _| ≤ _
  41. rw [abs_le]
  42. constructor
  43. · apply (le_sub_iff_add_le).2
  44. rw [← neg_div, ← add_div, div_le_iff₀ pose]
  45. linarith [hsum.1, esum.2, ld]
  46. · apply (sub_le_iff_le_add).2
  47. rw [← add_div, le_div_iff₀ pose]
  48. linarith [hsum.2, esum.1, ud]
  49. #print axioms mean_transfer
  50. end WallisTransfer