math / data /lean /OAI /Combinatorics /KServer /Rounding.lean
introvoyz045's picture
Migrated from GitHub
4a56cd6 verified
Raw History Blame Contribute Delete
89.3 kB
import Mathlib namespace OAI /- Prescribed correlated-total rounding and minimization over finite joint laws, without an independence requirement. -/ noncomputable section open Finset open scoped BigOperators namespace KServer.Rounding variable {J : Type*} [Fintype J] [DecidableEq J] def move (p : J β†’ ℝ) (x y : J) (Ξ΅ : ℝ) : J β†’ ℝ := fun z => p z + Ξ΅ * ((if z = y then 1 else 0) - (if z = x then 1 else 0)) omit [Fintype J] in lemma move_nonneg {p : J β†’ ℝ} (hp : βˆ€ z, 0 ≀ p z) {x y : J} {Ξ΅ : ℝ} (hΞ΅ : 0 ≀ Ξ΅) (hx : Ξ΅ ≀ p x) : βˆ€ z, 0 ≀ move p x y Ξ΅ z := by intro z by_cases hz : z = x Β· subst z by_cases hy : x = y Β· simp [move, hy, hp] Β· simp only [move, ite_eq_right hy, ite_true] linarith Β· by_cases hy : z = y Β· simp only [move, ite_eq_left hy, ite_eq_right hz] linarith [hp z] Β· simpa only [move, ite_eq_right hy, ite_eq_right hz, sub_self, mul_zero, add_zero] using hp z lemma move_sum (p : J β†’ ℝ) (x y : J) (Ξ΅ : ℝ) (f : J β†’ ℝ) : (βˆ‘ z, move p x y Ξ΅ z * f z) = (βˆ‘ z, p z * f z) + Ξ΅ * (f y - f x) := by calc (βˆ‘ z, move p x y Ξ΅ z * f z) = βˆ‘ z, (p z * f z + (if z = y then Ξ΅ * f z else 0) - (if z = x then Ξ΅ * f z else 0)) := by apply sum_congr rfl intro z _ simp only [move] split_ifs <;> ring _ = _ := by rw [sum_sub_distrib, sum_add_distrib]; simp; ring omit [Fintype J] in lemma move_coordinate (p : J β†’ ℝ) {x y z : J} {Ξ΅ : ℝ} (hΞ΅ : 0 ≀ Ξ΅) (hz : z β‰  x) : p z ≀ move p x y Ξ΅ z := by by_cases hy : z = y Β· simp only [move, ite_eq_left hy, ite_eq_right hz]; linarith Β· simp [move, hy, hz] variable {S I : Type*} [Fintype S] [Fintype I] [DecidableEq S] [DecidableEq I] /-- Each fiber records the actual observed total, not a conditional mean. -/ abbrev Outcome (r : S β†’ β„•) := {x : S Γ— Finset I // x.2.card = r x.1} def subset {r : S β†’ β„•} (x : Outcome (I := I) r) : Finset I := x.val.2 def state {r : S β†’ β„•} (x : Outcome (I := I) r) : S := x.val.1 def Marginal {r : S β†’ β„•} (p : Outcome (I := I) r β†’ ℝ) (i : I) : ℝ := βˆ‘ x, p x * (if i ∈ subset x then 1 else 0) def Mass {r : S β†’ β„•} (p : Outcome (I := I) r β†’ ℝ) (Ο‰ : S) : ℝ := βˆ‘ x, p x * (if state x = Ο‰ then 1 else 0) def Feasible {r : S β†’ β„•} (pβ‚€ p : Outcome (I := I) r β†’ ℝ) : Prop := (βˆ€ x, 0 ≀ p x) ∧ βˆ€ Ο‰, Mass p Ο‰ = Mass pβ‚€ Ο‰ omit [DecidableEq I] in lemma mass_total {r : S β†’ β„•} (p : Outcome (I := I) r β†’ ℝ) : (βˆ‘ Ο‰, Mass p Ο‰) = βˆ‘ x, p x := by simp only [Mass] rw [sum_comm] apply sum_congr rfl intro x _ simp lemma marginal_total {r : S β†’ β„•} (p : Outcome (I := I) r β†’ ℝ) : (βˆ‘ i, Marginal p i) = βˆ‘ Ο‰, Mass p Ο‰ * r Ο‰ := by simp only [Marginal, Mass] rw [sum_comm] simp_rw [sum_mul] conv_rhs => rw [sum_comm] apply sum_congr rfl intro x _ have hx : (subset x).card = r (state x) := x.property simp only [← mul_sum] have h : (βˆ‘ i : I, if i ∈ subset x then (1 : ℝ) else 0) = (subset x).card := by simp rw [h, hx] simp omit [DecidableEq I] in lemma feasible_mass {r : S β†’ β„•} {pβ‚€ p : Outcome (I := I) r β†’ ℝ} (hp : Feasible pβ‚€ p) : (βˆ‘ x, p x) = βˆ‘ x, pβ‚€ x := by rw [← mass_total p, ← mass_total pβ‚€] exact sum_congr rfl fun Ο‰ _ => hp.2 Ο‰ lemma move_feasible {r : S β†’ β„•} {pβ‚€ p : Outcome (I := I) r β†’ ℝ} (hp : Feasible pβ‚€ p) {x y : Outcome (I := I) r} (hxy : state x = state y) {Ξ΅ : ℝ} (hΞ΅ : 0 ≀ Ξ΅) (hx : Ξ΅ ≀ p x) : Feasible pβ‚€ (move p x y Ξ΅) := by refine ⟨move_nonneg hp.1 hΞ΅ hx, ?_⟩ intro Ο‰ rw [Mass, move_sum, hxy] simpa [Mass] using hp.2 Ο‰ omit [DecidableEq I] in lemma continuous_mass {r : S β†’ β„•} (Ο‰ : S) : Continuous (fun p : Outcome (I := I) r β†’ ℝ => Mass p Ο‰) := by unfold Mass fun_prop omit [DecidableEq S] in lemma continuous_marginal {r : S β†’ β„•} (i : I) : Continuous (fun p : Outcome (I := I) r β†’ ℝ => Marginal p i) := by unfold Marginal fun_prop omit [DecidableEq I] in lemma feasible_compact {r : S β†’ β„•} (pβ‚€ : Outcome (I := I) r β†’ ℝ) (hβ‚€ : βˆ‘ x, pβ‚€ x = 1) : IsCompact {p | Feasible pβ‚€ p} := by have hc : IsClosed {p | Feasible pβ‚€ p} := by change IsClosed ({p : Outcome (I := I) r β†’ ℝ | βˆ€ x, 0 ≀ p x} ∩ {p | βˆ€ Ο‰, Mass p Ο‰ = Mass pβ‚€ Ο‰}) apply IsClosed.inter Β· simp only [Set.ofPred_forall] exact isClosed_iInter fun x => isClosed_le continuous_const (continuous_apply x) Β· simp only [Set.ofPred_forall] exact isClosed_iInter fun Ο‰ => isClosed_eq (continuous_mass Ο‰) continuous_const apply (isCompact_pi_infinite fun _ : Outcome (I := I) r => isCompact_Icc (a := (0 : ℝ)) (b := 1)).of_isClosed_subset hc intro p hp x refine ⟨hp.1 x, ?_⟩ have he : βˆ‘ z, p z = 1 := (feasible_mass hp).trans hβ‚€ rw [← he] exact single_le_sum (fun z _ => hp.1 z) (mem_univ x) def Objective {r : S β†’ β„•} (c : Outcome (I := I) r β†’ ℝ) (f : I β†’ ℝ) (p : Outcome (I := I) r β†’ ℝ) : ℝ := (βˆ‘ x, p x * c x) + 10 * βˆ‘ i, |Marginal p i - f i| omit [DecidableEq S] in lemma objective_continuous {r : S β†’ β„•} (c : Outcome (I := I) r β†’ ℝ) (f : I β†’ ℝ) : Continuous (Objective c f) := by apply Continuous.add Β· fun_prop Β· apply Continuous.const_mul apply continuous_finsetSum intro i _ exact ((continuous_marginal i).sub continuous_const).abs lemma exists_minimizer {r : S β†’ β„•} (pβ‚€ c : Outcome (I := I) r β†’ ℝ) (f : I β†’ ℝ) (hpβ‚€ : βˆ€ x, 0 ≀ pβ‚€ x) (hβ‚€ : βˆ‘ x, pβ‚€ x = 1) : βˆƒ p, Feasible pβ‚€ p ∧ IsMinOn (Objective c f) {p | Feasible pβ‚€ p} p := by exact (feasible_compact pβ‚€ hβ‚€).exists_isMinOn ⟨pβ‚€, hpβ‚€, fun _ => rfl⟩ (objective_continuous c f).continuousOn def exchange (A : Finset I) (i j : I) : Finset I := insert j (A.erase i) omit [Fintype I] in lemma exchange_card {A : Finset I} {i j : I} (hi : i ∈ A) (hj : j βˆ‰ A) : (exchange A i j).card = A.card := by rw [exchange, card_insert_of_notMem (fun h => hj (mem_of_mem_erase h)), card_erase_of_mem hi] have := card_pos.mpr ⟨i, hi⟩ omega omit [Fintype I] in lemma exchange_indicator {A : Finset I} {i j : I} (hi : i ∈ A) (hj : j βˆ‰ A) (a : I) : (if a ∈ exchange A i j then (1 : ℝ) else 0) = (if a ∈ A then 1 else 0) + (if a = j then 1 else 0) - (if a = i then 1 else 0) := by have hij : i β‰  j := by intro h; subst j; contradiction by_cases ha : a = i Β· subst a; simp [exchange, hij, hi] Β· by_cases hb : a = j Β· subst a; simp [exchange, Ne.symm hij, hj] Β· simp [exchange, ha, hb] def afterExchange {r : S β†’ β„•} (x : Outcome (I := I) r) (i j : I) (hi : i ∈ subset x) (hj : j βˆ‰ subset x) : Outcome (I := I) r := ⟨(state x, exchange (subset x) i j), (exchange_card hi hj).trans x.property⟩ omit [Fintype I] [Fintype S] [DecidableEq S] in lemma exchange_state {r : S β†’ β„•} (x : Outcome (I := I) r) (i j : I) (hi : i ∈ subset x) (hj : j βˆ‰ subset x) : state (afterExchange x i j hi hj) = state x := rfl lemma move_exchange_marginal {r : S β†’ β„•} (p : Outcome (I := I) r β†’ ℝ) (x : Outcome (I := I) r) (i j : I) (hi : i ∈ subset x) (hj : j βˆ‰ subset x) (Ξ΅ : ℝ) (a : I) : Marginal (move p x (afterExchange x i j hi hj) Ξ΅) a = Marginal p a + Ξ΅ * ((if a = j then 1 else 0) - (if a = i then 1 else 0)) := by rw [Marginal, move_sum] change _ + Ξ΅ * ((if a ∈ exchange (subset x) i j then 1 else 0) - _) = _ rw [exchange_indicator hi hj] unfold Marginal ring lemma deviation_descent (a f : I β†’ ℝ) {i j : I} (hij : i β‰  j) {Ξ΅ : ℝ} (hΞ΅ : 0 ≀ Ξ΅) (hi : Ξ΅ ≀ a i - f i) (hj : Ξ΅ ≀ f j - a j) : (βˆ‘ x, |a x + Ξ΅ * ((if x = j then 1 else 0) - (if x = i then 1 else 0)) - f x|) = (βˆ‘ x, |a x - f x|) - 2 * Ξ΅ := by have he (x : I) : |a x + Ξ΅ * ((if x = j then 1 else 0) - (if x = i then 1 else 0)) - f x| = |a x - f x| - (if x = i then Ξ΅ else 0) - (if x = j then Ξ΅ else 0) := by by_cases hx : x = i Β· subst x simp only [ite_eq_right hij, ite_true] rw [abs_of_nonneg (by linarith : 0 ≀ a i + Ξ΅ * (0 - 1) - f i), abs_of_nonneg (by linarith : 0 ≀ a i - f i)] ring Β· by_cases hy : x = j Β· subst x simp only [ite_true, ite_eq_right (Ne.symm hij)] rw [abs_of_nonpos (by linarith : a j + Ξ΅ * (1 - 0) - f j ≀ 0), abs_of_nonpos (by linarith : a j - f j ≀ 0)] ring Β· simp [hx, hy] simp_rw [he] rw [sum_sub_distrib, sum_sub_distrib] simp ring omit [DecidableEq I] in lemma discrepancy_pair (a f : I β†’ ℝ) (hs : βˆ‘ i, a i = βˆ‘ i, f i) (hne : Β¬βˆ€ i, a i = f i) : βˆƒ i j, a i > f i ∧ a j < f j := by have hi : βˆƒ i, f i < a i := by by_contra! h apply hne intro i exact (sum_eq_sum_iff_of_le (fun i _ => h i)).mp hs i (mem_univ _) have hj : βˆƒ j, a j < f j := by by_contra! h apply hne have he := (sum_eq_sum_iff_of_le (fun i _ => h i)).mp hs.symm intro i exact (he i (mem_univ i)).symm obtain ⟨i, hi⟩ := hi obtain ⟨j, hj⟩ := hj exact ⟨i, j, hi, hj⟩ omit [DecidableEq S] in lemma support_mem {r : S β†’ β„•} {p : Outcome (I := I) r β†’ ℝ} (hp : βˆ€x, 0 ≀ p x) {i : I} (hi : 0 < Marginal p i) : βˆƒ x, 0 < p x ∧ i ∈ subset x := by by_contra! h have he : Marginal p i = 0 := by apply sum_eq_zero intro x _ by_cases hh : 0 < p x Β· simp [h x hh] Β· have hz := le_antisymm (le_of_not_gt hh) (hp x) simp [hz] linarith omit [DecidableEq S] in lemma support_notMem {r : S β†’ β„•} {p : Outcome (I := I) r β†’ ℝ} (hp : βˆ€x, 0 ≀ p x) (h₁ : βˆ‘ x, p x = 1) {i : I} (hi : Marginal p i < 1) : βˆƒ x, 0 < p x ∧ i βˆ‰ subset x := by by_contra! h have he : Marginal p i = βˆ‘ x, p x := by apply sum_congr rfl intro x _ by_cases hh : 0 < p x Β· simp [h x hh] Β· have hz := le_antisymm (le_of_not_gt hh) (hp x) simp [hz] linarith omit [Fintype I] in lemma exists_bridge {A B : Finset I} {i j : I} (hij : i β‰  j) (hiA : i ∈ A) (hjA : j ∈ A) (hiB : i βˆ‰ B) (hjB : j βˆ‰ B) (hc : A.card ≀ B.card + 1) : βˆƒ h, h ∈ B ∧ h βˆ‰ A := by by_contra! hh have hBA : B βŠ† A := hh have hsub : insert i (insert j B) βŠ† A := by intro a ha rcases mem_insert.mp ha with rfl | ha Β· exact hiA rcases mem_insert.mp ha with rfl | ha Β· exact hjA exact hBA ha have hcard := card_le_card hsub have hn : i βˆ‰ insert j B := by simp [hij, hiB] rw [card_insert_of_notMem hn, card_insert_of_notMem hjB] at hcard omega /-- The local Lipschitz property needed by the repair objective. It will be instantiated by Hamming distance from the state's preliminary rounding. -/ def ExchangeCost {r : S β†’ β„•} (c : Outcome (I := I) r β†’ ℝ) : Prop := βˆ€ (x : Outcome (I := I) r) (i j : I) (hi : i ∈ subset x) (hj : j βˆ‰ subset x), c (afterExchange x i j hi hj) - c x ≀ 2 lemma one_exchange_improves {r : S β†’ β„•} {pβ‚€ p c : Outcome (I := I) r β†’ ℝ} {f : I β†’ ℝ} (hp : Feasible pβ‚€ p) (hc : ExchangeCost c) {i j : I} (hi : f i < Marginal p i) (hj : Marginal p j < f j) (x : Outcome (I := I) r) (hx : 0 < p x) (hxi : i ∈ subset x) (hxj : j βˆ‰ subset x) : βˆƒ q, Feasible pβ‚€ q ∧ Objective c f q < Objective c f p := by have hij : i β‰  j := by intro he; subst j; linarith let Ξ΅ := min (p x) (min (Marginal p i - f i) (f j - Marginal p j)) have hΞ΅ : 0 < Ξ΅ := lt_min hx (lt_min (sub_pos.mpr hi) (sub_pos.mpr hj)) have hΞ΅x : Ξ΅ ≀ p x := min_le_left _ _ have hΞ΅i : Ξ΅ ≀ Marginal p i - f i := (min_le_right _ _).trans (min_le_left _ _) have hΞ΅j : Ξ΅ ≀ f j - Marginal p j := (min_le_right _ _).trans (min_le_right _ _) let y := afterExchange x i j hxi hxj let q := move p x y Ξ΅ refine ⟨q, move_feasible (x := x) (y := y) hp rfl hΞ΅.le hΞ΅x, ?_⟩ have hd : (βˆ‘ a, |Marginal q a - f a|) = (βˆ‘ a, |Marginal p a - f a|) - 2 * Ξ΅ := by simp only [q, y, move_exchange_marginal] exact deviation_descent _ _ hij hΞ΅.le hΞ΅i hΞ΅j have hcost : (βˆ‘ z, q z * c z) ≀ (βˆ‘ z, p z * c z) + 2 * Ξ΅ := by dsimp only [q] rw [move_sum] have hh := mul_le_mul_of_nonneg_left (hc x i j hxi hxj) hΞ΅.le dsimp [y] linarith unfold Objective rw [hd] linarith lemma two_exchanges_improve {r : S β†’ β„•} {pβ‚€ p c : Outcome (I := I) r β†’ ℝ} {f : I β†’ ℝ} (hp : Feasible pβ‚€ p) (hc : ExchangeCost c) {i j : I} (hi : f i < Marginal p i) (hj : Marginal p j < f j) (x y : Outcome (I := I) r) (hx : 0 < p x) (hy : 0 < p y) (hxi : i ∈ subset x) (hxj : j ∈ subset x) (hyi : i βˆ‰ subset y) (hyj : j βˆ‰ subset y) (h : I) (hyh : h ∈ subset y) (hxh : h βˆ‰ subset x) : βˆƒ q, Feasible pβ‚€ q ∧ Objective c f q < Objective c f p := by have hij : i β‰  j := by intro he; subst j; linarith have hyx : y β‰  x := by intro he; subst y; contradiction let Ξ΅ := min (min (p x) (p y)) (min (Marginal p i - f i) (f j - Marginal p j)) have hΞ΅ : 0 < Ξ΅ := lt_min (lt_min hx hy) (lt_min (sub_pos.mpr hi) (sub_pos.mpr hj)) have hΞ΅x : Ξ΅ ≀ p x := (min_le_left _ _).trans (min_le_left _ _) have hΞ΅y : Ξ΅ ≀ p y := (min_le_left _ _).trans (min_le_right _ _) have hΞ΅i : Ξ΅ ≀ Marginal p i - f i := (min_le_right _ _).trans (min_le_left _ _) have hΞ΅j : Ξ΅ ≀ f j - Marginal p j := (min_le_right _ _).trans (min_le_right _ _) let x' := afterExchange x i h hxi hxh let y' := afterExchange y h j hyh hyj let mid := move p x x' Ξ΅ let q := move mid y y' Ξ΅ have hmid : Feasible pβ‚€ mid := move_feasible (x := x) (y := x') hp rfl hΞ΅.le hΞ΅x have hq : Feasible pβ‚€ q := move_feasible (x := y) (y := y') hmid rfl hΞ΅.le (hΞ΅y.trans (move_coordinate p hΞ΅.le hyx)) refine ⟨q, hq, ?_⟩ have hm (a : I) : Marginal q a = Marginal p a + Ξ΅ * ((if a = j then 1 else 0) - (if a = i then 1 else 0)) := by dsimp only [q, mid, x', y'] rw [move_exchange_marginal, move_exchange_marginal] ring have hd : (βˆ‘ a, |Marginal q a - f a|) = (βˆ‘ a, |Marginal p a - f a|) - 2 * Ξ΅ := by simp only [hm] exact deviation_descent _ _ hij hΞ΅.le hΞ΅i hΞ΅j have hcost : (βˆ‘ z, q z * c z) ≀ (βˆ‘ z, p z * c z) + 4 * Ξ΅ := by dsimp only [q, mid] rw [move_sum, move_sum] have hhx := mul_le_mul_of_nonneg_left (hc x i h hxi hxh) hΞ΅.le have hhy := mul_le_mul_of_nonneg_left (hc y h j hyh hyj) hΞ΅.le dsimp only [x', y'] linarith unfold Objective rw [hd] linarith /-- A minimizer has the prescribed unconditional marginals, even when different fibers have different adjacent totals. This is the source's essential correlation-tolerant two-state exchange argument. -/ lemma minimizer_marginals {r : S β†’ β„•} {pβ‚€ p c : Outcome (I := I) r β†’ ℝ} {f : I β†’ ℝ} (hr : βˆ€ Ο‰ Ξ½, r Ο‰ ≀ r Ξ½ + 1) (hf : βˆ€ i, 0 ≀ f i ∧ f i ≀ 1) (hfSum : βˆ‘ i, f i = βˆ‘ Ο‰, Mass pβ‚€ Ο‰ * r Ο‰) (hp : Feasible pβ‚€ p) (hβ‚€ : βˆ‘ x, pβ‚€ x = 1) (hc : ExchangeCost c) (hmin : IsMinOn (Objective c f) {p | Feasible pβ‚€ p} p) : βˆ€ i, Marginal p i = f i := by by_contra hne have hsum : βˆ‘ i, Marginal p i = βˆ‘ i, f i := by rw [marginal_total, hfSum] exact sum_congr rfl fun Ο‰ _ => by rw [hp.2 Ο‰] obtain ⟨i, j, hi, hj⟩ := discrepancy_pair (Marginal p) f hsum hne have hij : i β‰  j := by intro he; subst j; linarith by_cases hex : βˆƒ x, 0 < p x ∧ i ∈ subset x ∧ j βˆ‰ subset x Β· obtain ⟨x, hx, hxi, hxj⟩ := hex obtain ⟨q, hq, hqCost⟩ := one_exchange_improves hp hc hi hj x hx hxi hxj exact (not_lt_of_ge (hmin hq)) hqCost Β· obtain ⟨x, hx, hxi⟩ := support_mem hp.1 ((hf i).1.trans_lt hi) have hxj : j ∈ subset x := by by_contra hh exact hex ⟨x, hx, hxi, hh⟩ obtain ⟨y, hy, hyj⟩ := support_notMem hp.1 ((feasible_mass hp).trans hβ‚€) (hj.trans_le (hf j).2) have hyi : i βˆ‰ subset y := fun hh => hex ⟨y, hy, hh, hyj⟩ have hcard : (subset x).card ≀ (subset y).card + 1 := by rw [show (subset x).card = r (state x) from x.property, show (subset y).card = r (state y) from y.property] exact hr _ _ obtain ⟨h, hyh, hxh⟩ := exists_bridge hij hxi hxj hyi hyj hcard obtain ⟨q, hq, hqCost⟩ := two_exchanges_improve hp hc hi hj x y hx hy hxi hxj hyi hyj h hyh hxh exact (not_lt_of_ge (hmin hq)) hqCost /-- The compact finite-law repair step, including its quantitative cost bound. -/ theorem finite_repair {r : S β†’ β„•} (pβ‚€ c : Outcome (I := I) r β†’ ℝ) (f : I β†’ ℝ) (hr : βˆ€ Ο‰ Ξ½, r Ο‰ ≀ r Ξ½ + 1) (hf : βˆ€ i, 0 ≀ f i ∧ f i ≀ 1) (hfSum : βˆ‘ i, f i = βˆ‘ Ο‰, Mass pβ‚€ Ο‰ * r Ο‰) (hpβ‚€ : βˆ€ x, 0 ≀ pβ‚€ x) (hβ‚€ : βˆ‘ x, pβ‚€ x = 1) (hc : ExchangeCost c) : βˆƒ p, Feasible pβ‚€ p ∧ (βˆ€ i, Marginal p i = f i) ∧ (βˆ‘ x, p x * c x) ≀ (βˆ‘ x, pβ‚€ x * c x) + 10 * βˆ‘ i, |Marginal pβ‚€ i - f i| := by obtain ⟨p, hp, hm⟩ := exists_minimizer pβ‚€ c f hpβ‚€ hβ‚€ have hmar := minimizer_marginals hr hf hfSum hp hβ‚€ hc hm refine ⟨p, hp, hmar, ?_⟩ have hh := hm (show Feasible pβ‚€ pβ‚€ from ⟨hpβ‚€, fun _ => rfl⟩) simpa [Objective, hmar] using hh def hamming (A B : Finset I) : ℝ := βˆ‘ i, |(if i ∈ A then (1 : ℝ) else 0) - (if i ∈ B then 1 else 0)| lemma hamming_self (A : Finset I) : hamming A A = 0 := by simp [hamming] lemma hamming_triangle (A B C : Finset I) : hamming A C ≀ hamming A B + hamming B C := by rw [hamming, hamming, hamming, ← sum_add_distrib] exact sum_le_sum fun i _ => abs_sub_le _ _ _ lemma hamming_exchange {A : Finset I} {i j : I} (hi : i ∈ A) (hj : j βˆ‰ A) : hamming (exchange A i j) A = 2 := by have hij : i β‰  j := by intro he; subst j; contradiction have he (a : I) : |(if a ∈ exchange A i j then (1 : ℝ) else 0) - (if a ∈ A then 1 else 0)| = (if a = j then 1 else 0) + (if a = i then 1 else 0) := by rw [exchange_indicator hi hj] by_cases ha : a = i Β· subst a; simp [hij] Β· by_cases hb : a = j Β· subst a; simp [Ne.symm hij] Β· simp [ha, hb] simp only [hamming, he, sum_add_distrib] norm_num def HammingCost {r : S β†’ β„•} (Aβ‚€ : S β†’ Finset I) (x : Outcome (I := I) r) : ℝ := hamming (subset x) (Aβ‚€ (state x)) omit [Fintype S] [DecidableEq S] in lemma hamming_exchangeCost {r : S β†’ β„•} (Aβ‚€ : S β†’ Finset I) : ExchangeCost (HammingCost (r := r) Aβ‚€) := by intro x i j hi hj have hh := hamming_triangle (exchange (subset x) i j) (subset x) (Aβ‚€ (state x)) rw [hamming_exchange hi hj] at hh change hamming (exchange (subset x) i j) (Aβ‚€ (state x)) - _ ≀ 2 unfold HammingCost linarith def seed {r : S β†’ β„•} (w : S β†’ ℝ) (e : S β†’ Outcome (I := I) r) : Outcome (I := I) r β†’ ℝ := fun x => βˆ‘ Ο‰, if x = e Ο‰ then w Ο‰ else 0 omit [Fintype I] in lemma seed_nonneg {r : S β†’ β„•} {w : S β†’ ℝ} (hw : βˆ€ Ο‰, 0 ≀ w Ο‰) (e : S β†’ Outcome (I := I) r) : βˆ€ x, 0 ≀ seed w e x := by intro x exact sum_nonneg fun Ο‰ _ => by split_ifs; exact hw Ο‰; rfl lemma seed_sum {r : S β†’ β„•} (w : S β†’ ℝ) (e : S β†’ Outcome (I := I) r) (g : Outcome (I := I) r β†’ ℝ) : (βˆ‘ x, seed w e x * g x) = βˆ‘ Ο‰, w Ο‰ * g (e Ο‰) := by simp only [seed, sum_mul] rw [sum_comm] apply sum_congr rfl intro Ο‰ _ simp [ite_mul] lemma seed_total {r : S β†’ β„•} (w : S β†’ ℝ) (e : S β†’ Outcome (I := I) r) : (βˆ‘ x, seed w e x) = βˆ‘ Ο‰, w Ο‰ := by simpa using seed_sum w e (fun _ => 1) lemma seed_mass {r : S β†’ β„•} (w : S β†’ ℝ) (e : S β†’ Outcome (I := I) r) (he : βˆ€ Ο‰, state (e Ο‰) = Ο‰) (Ο‰ : S) : Mass (seed w e) Ο‰ = w Ο‰ := by rw [Mass, seed_sum] simp [he] lemma seed_marginal {r : S β†’ β„•} (w : S β†’ ℝ) (e : S β†’ Outcome (I := I) r) (i : I) : Marginal (seed w e) i = βˆ‘ Ο‰, w Ο‰ * (if i ∈ subset (e Ο‰) then 1 else 0) := by exact seed_sum _ _ _ /-- Joint-law repair in the exact Hamming metric of the manuscript. All correlation is retained by preserving every original state probability. -/ theorem hamming_repair (w : S β†’ ℝ) (hw : βˆ€ Ο‰, 0 ≀ w Ο‰) (h₁ : βˆ‘ Ο‰, w Ο‰ = 1) (r : S β†’ β„•) (hr : βˆ€ Ο‰ Ξ½, r Ο‰ ≀ r Ξ½ + 1) (Aβ‚€ : S β†’ Finset I) (hA : βˆ€ Ο‰, (Aβ‚€ Ο‰).card = r Ο‰) (f : I β†’ ℝ) (hf : βˆ€ i, 0 ≀ f i ∧ f i ≀ 1) (hs : βˆ‘ i, f i = βˆ‘ Ο‰, w Ο‰ * r Ο‰) : βˆƒ p : Outcome (I := I) r β†’ ℝ, (βˆ€ x, 0 ≀ p x) ∧ (βˆ€ Ο‰, Mass p Ο‰ = w Ο‰) ∧ (βˆ€ i, Marginal p i = f i) ∧ (βˆ‘ x, p x * HammingCost Aβ‚€ x) ≀ 10 * βˆ‘ i, |(βˆ‘ Ο‰, w Ο‰ * (if i ∈ Aβ‚€ Ο‰ then 1 else 0)) - f i| := by let e : S β†’ Outcome (I := I) r := fun Ο‰ => ⟨(Ο‰, Aβ‚€ Ο‰), hA Ο‰βŸ© have he (Ο‰ : S) : state (e Ο‰) = Ο‰ := rfl have hsubset (Ο‰ : S) : subset (e Ο‰) = Aβ‚€ Ο‰ := rfl have hmass := seed_mass w e he have hfs : βˆ‘ i, f i = βˆ‘ Ο‰, Mass (seed w e) Ο‰ * r Ο‰ := by simpa [hmass] using hs obtain ⟨p, hp, hmar, hcost⟩ := finite_repair (seed w e) (HammingCost Aβ‚€) f hr hf hfs (seed_nonneg hw e) ((seed_total w e).trans h₁) (hamming_exchangeCost Aβ‚€) refine ⟨p, hp.1, fun Ο‰ => (hp.2 Ο‰).trans (hmass Ο‰), hmar, ?_⟩ have hzero : (βˆ‘ x, seed w e x * HammingCost Aβ‚€ x) = 0 := by rw [seed_sum] simp [HammingCost, he, hsubset, hamming_self] simpa [hzero, seed_marginal, hsubset] using hcost lemma hamming_comm (A B : Finset I) : hamming A B = hamming B A := by apply sum_congr rfl intro i _ exact abs_sub_comm _ _ lemma hamming_subset {A B : Finset I} (h : B βŠ† A) : hamming A B = (A.card : ℝ) - B.card := by have hn (i : I) : (0 : ℝ) ≀ (if i ∈ A then 1 else 0) - (if i ∈ B then 1 else 0) := by by_cases hi : i ∈ B Β· simp [hi, h hi] Β· simp [hi]; split_ifs <;> norm_num simp only [hamming, abs_of_nonneg (hn _)] rw [sum_sub_distrib] simp /-- Deterministic adjustment to any attainable integer total, with optimal Hamming cost. The permitted coordinates H may omit all integral targets. -/ lemma adjust_subset (H A : Finset I) (hAH : A βŠ† H) {r : β„•} (hr : r ≀ H.card) : βˆƒ B : Finset I, B βŠ† H ∧ B.card = r ∧ hamming B A = |(r : ℝ) - A.card| := by by_cases hsmall : r ≀ A.card Β· obtain ⟨B, hBA, hBr⟩ := exists_subset_card_eq hsmall refine ⟨B, hBA.trans hAH, hBr, ?_⟩ rw [hamming_comm, hamming_subset hBA, hBr, abs_of_nonpos (sub_nonpos.mpr (by exact_mod_cast hsmall : (r : ℝ) ≀ A.card))] ring Β· have hbig : A.card ≀ r := le_of_not_ge hsmall have hc : r - A.card ≀ (H \ A).card := by rw [card_sdiff, inter_eq_left.mpr hAH] omega obtain ⟨C, hCH, hCr⟩ := exists_subset_card_eq hc have hdis : Disjoint A C := by apply disjoint_left.mpr intro a ha hca exact (mem_sdiff.mp (hCH hca)).2 ha let B := A βˆͺ C have hAB : A βŠ† B := subset_union_left have hBH : B βŠ† H := union_subset hAH (hCH.trans sdiff_subset) have hBr : B.card = r := by dsimp [B] rw [card_union_of_disjoint hdis, hCr] omega refine ⟨B, hBH, hBr, ?_⟩ rw [hamming_subset hAB, hBr, abs_of_nonneg (sub_nonneg.mpr (by exact_mod_cast hbig : (A.card : ℝ) ≀ r))] /-- The literal scalar interval used by the source's preliminary clipping. -/ def clip (b : ℝ) (x : β„€) : β„€ := max ⌊bβŒ‹ (min ⌈bβŒ‰ x) def Balanced (b : ℝ) (x : β„€) : Prop := x = ⌊bβŒ‹ ∨ x = ⌈bβŒ‰ lemma balanced_iff_bounds (b : ℝ) (x : β„€) : Balanced b x ↔ ⌊bβŒ‹ ≀ x ∧ x ≀ ⌈bβŒ‰ := by have hl := Int.floor_le_ceil b have hu := Int.ceil_le_floor_add_one b unfold Balanced omega lemma clip_balanced (b : ℝ) (x : β„€) : Balanced b (clip b x) := by rw [balanced_iff_bounds] exact ⟨le_max_left _ _, max_le (Int.floor_le_ceil b) (min_le_left _ _)⟩ lemma balanced_bounds {b : ℝ} {x : β„€} (h : Balanced b x) : ⌊bβŒ‹ ≀ x ∧ x ≀ ⌈bβŒ‰ := (balanced_iff_bounds b x).mp h lemma clip_up {a b : ℝ} (hab : a ≀ b) {x : β„€} (hx : Balanced a x) : clip b x = max ⌊bβŒ‹ x := by unfold clip rw [min_eq_right ((balanced_bounds hx).2.trans (Int.ceil_mono hab))] lemma clip_down {a b : ℝ} (hab : b ≀ a) {x : β„€} (hx : Balanced a x) : clip b x = min ⌈bβŒ‰ x := by unfold clip rw [max_eq_right (le_min (Int.floor_le_ceil b) ((Int.floor_mono hab).trans (balanced_bounds hx).1))] omit [DecidableEq S] in /-- Exact source clipping estimate, with arbitrary finite weights and no independence assumptions. -/ lemma clipping_bound (w : S β†’ ℝ) (_hw : βˆ€ Ο‰, 0 ≀ w Ο‰) (h₁ : βˆ‘ Ο‰, w Ο‰ = 1) {a b : ℝ} (X : S β†’ β„€) (hX : βˆ€ Ο‰, Balanced a (X Ο‰)) (hmean : βˆ‘ Ο‰, w Ο‰ * (X Ο‰ : ℝ) = a) : (βˆ‘ Ο‰, w Ο‰ * |(clip b (X Ο‰) : ℝ) - X Ο‰|) ≀ |a - b| := by rcases le_total a b with hab | hba Β· simp_rw [clip_up hab (hX _)] by_cases hf : ⌊bβŒ‹ ≀ ⌊aβŒ‹ Β· have hh (Ο‰ : S) : max ⌊bβŒ‹ (X Ο‰) = X Ο‰ := max_eq_right (hf.trans (balanced_bounds (hX Ο‰)).1) simp [hh] Β· have hl : ⌈aβŒ‰ ≀ ⌊bβŒ‹ := by have := Int.ceil_le_floor_add_one a; omega have hh (Ο‰ : S) : max ⌊bβŒ‹ (X Ο‰) = ⌊bβŒ‹ := max_eq_left ((balanced_bounds (hX Ο‰)).2.trans hl) have hn (Ο‰ : S) : (0 : ℝ) ≀ (⌊bβŒ‹ : ℝ) - X Ο‰ := by exact_mod_cast sub_nonneg.mpr ((balanced_bounds (hX Ο‰)).2.trans hl) simp_rw [hh, abs_of_nonneg (hn _), mul_sub] rw [sum_sub_distrib, ← sum_mul, h₁, one_mul, hmean, abs_of_nonpos (sub_nonpos.mpr hab)] linarith [Int.floor_le b] Β· simp_rw [clip_down hba (hX _)] by_cases hc : ⌈aβŒ‰ ≀ ⌈bβŒ‰ Β· have hh (Ο‰ : S) : min ⌈bβŒ‰ (X Ο‰) = X Ο‰ := min_eq_right ((balanced_bounds (hX Ο‰)).2.trans hc) simp [hh] Β· have hu : ⌈bβŒ‰ ≀ ⌊aβŒ‹ := by have := Int.ceil_le_floor_add_one a; omega have hh (Ο‰ : S) : min ⌈bβŒ‰ (X Ο‰) = ⌈bβŒ‰ := min_eq_left (hu.trans (balanced_bounds (hX Ο‰)).1) have hn (Ο‰ : S) : (⌈bβŒ‰ : ℝ) - X Ο‰ ≀ 0 := by exact_mod_cast sub_nonpos.mpr (hu.trans (balanced_bounds (hX Ο‰)).1) simp_rw [hh, abs_of_nonpos (hn _), mul_neg, mul_sub] rw [sum_neg_distrib, sum_sub_distrib, ← sum_mul, h₁, one_mul, hmean, abs_of_nonneg (sub_nonneg.mpr hba)] linarith [Int.le_ceil b] def fraction (b : ℝ) : ℝ := b - ⌊bβŒ‹ def fractionalSet (b : I β†’ ℝ) : Finset I := univ.filter fun i => ⌊b iβŒ‹ < ⌈b iβŒ‰ def liftSet (b : I β†’ ℝ) (A : Finset I) (i : I) : β„€ := ⌊b iβŒ‹ + if i ∈ A then 1 else 0 lemma fraction_range (b : ℝ) : 0 ≀ fraction b ∧ fraction b < 1 := by unfold fraction constructor <;> linarith [Int.floor_le b, Int.lt_floor_add_one b] lemma ceil_formula (b : ℝ) : ⌈bβŒ‰ = ⌊bβŒ‹ + if ⌊bβŒ‹ < ⌈bβŒ‰ then 1 else 0 := by have := Int.ceil_le_floor_add_one b have := Int.floor_le_ceil b split_ifs <;> omega lemma not_fractional {b : ℝ} (h : ¬⌊bβŒ‹ < ⌈bβŒ‰) : (⌊bβŒ‹ : ℝ) = b := by have he : ⌊bβŒ‹ = ⌈bβŒ‰ := by have := Int.floor_le_ceil b; omega apply le_antisymm (Int.floor_le b) rw [he] exact Int.le_ceil b lemma fraction_zero_iff (b : ℝ) : fraction b = 0 ↔ ¬⌊bβŒ‹ < ⌈bβŒ‰ := by constructor Β· intro h have he : b = (⌊bβŒ‹ : ℝ) := by unfold fraction at h; linarith have hc := Int.ceil_le.mpr he.le omega Β· intro h simp [fraction, not_fractional h] lemma liftSet_sum (b : I β†’ ℝ) (A : Finset I) : (βˆ‘ i, liftSet b A i) = (βˆ‘ i, ⌊b iβŒ‹) + A.card := by simp [liftSet, sum_add_distrib] lemma liftSet_balanced (b : I β†’ ℝ) {A : Finset I} (hA : A βŠ† fractionalSet b) (i : I) : Balanced (b i) (liftSet b A i) := by unfold liftSet by_cases hi : i ∈ A Β· simp only [ite_eq_left hi] right have hbi : ⌊b iβŒ‹ < ⌈b iβŒ‰ := (mem_filter.mp (hA hi)).2 rw [ceil_formula, ite_eq_left hbi] Β· left simp [hi] lemma balanced_liftSet (b : I β†’ ℝ) (Y : I β†’ β„€) (hY : βˆ€ i, Balanced (b i) (Y i)) : βˆƒ A : Finset I, A βŠ† fractionalSet b ∧ βˆ€ i, Y i = liftSet b A i := by let A := univ.filter fun i => ⌊b iβŒ‹ < Y i have hA : A βŠ† fractionalSet b := by intro i hi have hi' : ⌊b iβŒ‹ < Y i := (mem_filter.mp hi).2 exact mem_filter.mpr ⟨mem_univ _, hi'.trans_le (balanced_bounds (hY i)).2⟩ refine ⟨A, hA, ?_⟩ intro i have hu := Int.ceil_le_floor_add_one (b i) have := balanced_bounds (hY i) by_cases hi : i ∈ A Β· unfold liftSet rw [ite_eq_left hi] have hh : ⌊b iβŒ‹ < Y i := (mem_filter.mp hi).2 omega Β· unfold liftSet rw [ite_eq_right hi] have hh : ¬⌊b iβŒ‹ < Y i := by simpa [A] using hi omega lemma liftSet_distance (b : I β†’ ℝ) (A B : Finset I) : (βˆ‘ i, |(liftSet b A i : ℝ) - liftSet b B i|) = hamming A B := by apply sum_congr rfl intro i _ unfold liftSet push_cast congr 1 ring omit [DecidableEq I] in lemma total_endpoints (b : I β†’ ℝ) : (βˆ‘ i, ⌊b iβŒ‹) ≀ βŒŠβˆ‘ i, b iβŒ‹ ∧ βŒˆβˆ‘ i, b iβŒ‰ ≀ (βˆ‘ i, ⌊b iβŒ‹) + (fractionalSet b).card := by constructor Β· apply Int.le_floor.mpr push_cast exact sum_le_sum fun i _ => Int.floor_le _ Β· apply Int.ceil_le.mpr have he : (βˆ‘ i, ⌈b iβŒ‰) = (βˆ‘ i, ⌊b iβŒ‹) + (fractionalSet b).card := by calc (βˆ‘ i, ⌈b iβŒ‰) = βˆ‘ i, (⌊b iβŒ‹ + if ⌊b iβŒ‹ < ⌈b iβŒ‰ then (1 : β„€) else 0) := sum_congr rfl (fun i _ => ceil_formula (b i)) _ = _ := ?_ rw [sum_add_distrib] congr 1 simp [fractionalSet] rw [← he] push_cast exact sum_le_sum fun i _ => Int.le_ceil _ def residual (b : I β†’ ℝ) (N : β„€) : β„• := (N - βˆ‘ i, ⌊b iβŒ‹).toNat omit [DecidableEq I] in lemma residual_cast (b : I β†’ ℝ) {N : β„€} (hN : Balanced (βˆ‘ i, b i) N) : (residual b N : β„€) = N - βˆ‘ i, ⌊b iβŒ‹ := by unfold residual apply Int.toNat_of_nonneg exact sub_nonneg.mpr ((total_endpoints b).1.trans (balanced_bounds hN).1) omit [DecidableEq I] in lemma residual_bound (b : I β†’ ℝ) {N : β„€} (hN : Balanced (βˆ‘ i, b i) N) : residual b N ≀ (fractionalSet b).card := by have hcast := residual_cast b hN have hu := (balanced_bounds hN).2.trans (total_endpoints b).2 omega omit [DecidableEq I] in lemma residual_adjacent (b : I β†’ ℝ) {N M : β„€} (hN : Balanced (βˆ‘ i, b i) N) (hM : Balanced (βˆ‘ i, b i) M) : residual b N ≀ residual b M + 1 := by have hcastN := residual_cast b hN have hcastM := residual_cast b hM have hN' := balanced_bounds hN have hM' := balanced_bounds hM have := Int.ceil_le_floor_add_one (βˆ‘ i, b i) omega omit [DecidableEq I] in lemma residual_cast_real (b : I β†’ ℝ) {N : β„€} (hN : Balanced (βˆ‘ i, b i) N) : (residual b N : ℝ) = N - βˆ‘ i, (⌊b iβŒ‹ : ℝ) := by have he := congrArg (fun z : β„€ => (z : ℝ)) (residual_cast b hN) simpa using he omit [DecidableEq I] [DecidableEq S] in lemma residual_mean (w : S β†’ ℝ) (h₁ : βˆ‘ Ο‰, w Ο‰ = 1) (b : I β†’ ℝ) (N : S β†’ β„€) (hN : βˆ€ Ο‰, Balanced (βˆ‘ i, b i) (N Ο‰)) (hm : βˆ‘ Ο‰, w Ο‰ * (N Ο‰ : ℝ) = βˆ‘ i, b i) : (βˆ‘ i, fraction (b i)) = βˆ‘ Ο‰, w Ο‰ * residual b (N Ο‰) := by simp_rw [residual_cast_real b (hN _), mul_sub, fraction] rw [sum_sub_distrib, sum_sub_distrib, ← sum_mul, h₁, one_mul, hm] /-- The statewise preliminary adjustment has exactly the source's cost. -/ lemma preliminary_state (b : I β†’ ℝ) (X : I β†’ β„€) {N : β„€} (hN : Balanced (βˆ‘ i, b i) N) : βˆƒ A : Finset I, A βŠ† fractionalSet b ∧ A.card = residual b N ∧ (βˆ‘ i, |(liftSet b A i : ℝ) - clip (b i) (X i)|) = |(N : ℝ) - βˆ‘ i, (clip (b i) (X i) : ℝ)| := by obtain ⟨Y, hY, he⟩ := balanced_liftSet b (fun i => clip (b i) (X i)) (fun i => clip_balanced _ _) obtain ⟨A, hA, hcard, hcost⟩ := adjust_subset (fractionalSet b) Y hY (residual_bound b hN) refine ⟨A, hA, hcard, ?_⟩ simp_rw [he] rw [liftSet_distance, hcost, residual_cast_real b hN] congr 1 have hy := congrArg (fun z : β„€ => (z : ℝ)) (liftSet_sum b Y) push_cast at hy rw [hy] ring def mean (w : S β†’ ℝ) (X : S β†’ ℝ) : ℝ := βˆ‘ Ο‰, w Ο‰ * X Ο‰ def dist₁ (X Y : I β†’ ℝ) : ℝ := βˆ‘ i, |X i - Y i| omit [DecidableEq I] in lemma dist₁_triangle (X Y Z : I β†’ ℝ) : dist₁ X Z ≀ dist₁ X Y + dist₁ Y Z := by unfold dist₁ rw [← sum_add_distrib] exact sum_le_sum fun i _ => abs_sub_le _ _ _ omit [DecidableEq I] in lemma abs_total_le (X Y : I β†’ ℝ) : |(βˆ‘ i, X i) - βˆ‘ i, Y i| ≀ dist₁ X Y := by rw [← sum_sub_distrib] exact abs_sum_le_sum_abs _ _ omit [DecidableEq S] in lemma mean_mono (w : S β†’ ℝ) (hw : βˆ€ Ο‰, 0 ≀ w Ο‰) {X Y : S β†’ ℝ} (h : βˆ€ Ο‰, X Ο‰ ≀ Y Ο‰) : mean w X ≀ mean w Y := sum_le_sum fun Ο‰ _ => mul_le_mul_of_nonneg_left (h Ο‰) (hw Ο‰) omit [DecidableEq S] in lemma mean_add (w : S β†’ ℝ) (X Y : S β†’ ℝ) : mean w (fun Ο‰ => X Ο‰ + Y Ο‰) = mean w X + mean w Y := by simp [mean, mul_add, sum_add_distrib] omit [DecidableEq S] in lemma mean_abs_sub (w : S β†’ ℝ) (hw : βˆ€ Ο‰, 0 ≀ w Ο‰) (X Y : S β†’ ℝ) : |mean w X - mean w Y| ≀ mean w (fun Ο‰ => |X Ο‰ - Y Ο‰|) := by calc |mean w X - mean w Y| = |βˆ‘ Ο‰, w Ο‰ * (X Ο‰ - Y Ο‰)| := by simp [mean, mul_sub, sum_sub_distrib] _ ≀ βˆ‘ Ο‰, |w Ο‰ * (X Ο‰ - Y Ο‰)| := abs_sum_le_sum_abs _ _ _ = _ := by simp [mean, abs_mul, abs_of_nonneg (hw _)] omit [DecidableEq I] [DecidableEq S] in lemma dist₁_mean (w : S β†’ ℝ) (hw : βˆ€ Ο‰, 0 ≀ w Ο‰) (X Y : S β†’ I β†’ ℝ) : dist₁ (fun i => mean w (fun Ο‰ => X Ο‰ i)) (fun i => mean w (fun Ο‰ => Y Ο‰ i)) ≀ mean w (fun Ο‰ => dist₁ (X Ο‰) (Y Ο‰)) := by calc _ ≀ βˆ‘ i, mean w (fun Ο‰ => |X Ο‰ i - Y Ο‰ i|) := sum_le_sum fun i _ => mean_abs_sub w hw _ _ _ = _ := by unfold mean dist₁; rw [sum_comm]; simp_rw [mul_sum] omit [DecidableEq S] in lemma preliminary_bounds (w : S β†’ ℝ) (hw : βˆ€ Ο‰, 0 ≀ w Ο‰) (h₁ : βˆ‘ Ο‰, w Ο‰ = 1) (a b : I β†’ ℝ) (X : S β†’ I β†’ β„€) (N : S β†’ β„€) (hX : βˆ€ Ο‰ i, Balanced (a i) (X Ο‰ i)) (hm : βˆ€ i, mean w (fun Ο‰ => (X Ο‰ i : ℝ)) = a i) (A : S β†’ Finset I) (hadj : βˆ€ Ο‰, (βˆ‘ i, |(liftSet b (A Ο‰) i : ℝ) - clip (b i) (X Ο‰ i)|) = |(N Ο‰ : ℝ) - βˆ‘ i, (clip (b i) (X Ο‰ i) : ℝ)|) : let L := mean w (fun Ο‰ => dist₁ (fun i => (liftSet b (A Ο‰) i : ℝ)) (fun i => (X Ο‰ i : ℝ))) let T := dist₁ a b let U := mean w (fun Ο‰ => |(N Ο‰ : ℝ) - βˆ‘ i, (X Ο‰ i : ℝ)|) let D := dist₁ (fun i => mean w (fun Ο‰ => (liftSet b (A Ο‰) i : ℝ))) b L ≀ 2*T + U ∧ D ≀ 3*T + U := by dsimp only let Y : S β†’ I β†’ ℝ := fun Ο‰ i => clip (b i) (X Ο‰ i) let Z : S β†’ I β†’ ℝ := fun Ο‰ i => liftSet b (A Ο‰) i let V : S β†’ I β†’ ℝ := fun Ο‰ i => X Ο‰ i have hclip : mean w (fun Ο‰ => dist₁ (Y Ο‰) (V Ο‰)) ≀ dist₁ a b := by unfold mean dist₁ simp_rw [mul_sum] rw [sum_comm] exact sum_le_sum fun i _ => clipping_bound w hw h₁ (fun Ο‰ => X Ο‰ i) (fun Ο‰ => hX Ο‰ i) (hm i) have hadjust : mean w (fun Ο‰ => dist₁ (Z Ο‰) (Y Ο‰)) ≀ mean w (fun Ο‰ => |(N Ο‰ : ℝ) - βˆ‘ i, (X Ο‰ i : ℝ)|) + mean w (fun Ο‰ => dist₁ (Y Ο‰) (V Ο‰)) := by rw [← mean_add] apply mean_mono w hw intro Ο‰ change (βˆ‘ i, |(liftSet b (A Ο‰) i : ℝ) - clip (b i) (X Ο‰ i)|) ≀ _ rw [hadj Ο‰] have hsum : |(βˆ‘ i, (X Ο‰ i : ℝ)) - βˆ‘ i, (clip (b i) (X Ο‰ i) : ℝ)| ≀ dist₁ (Y Ο‰) (V Ο‰) := by change |(βˆ‘ i, V Ο‰ i) - βˆ‘ i, Y Ο‰ i| ≀ dist₁ (Y Ο‰) (V Ο‰) rw [abs_sub_comm (βˆ‘ i, V Ο‰ i) (βˆ‘ i, Y Ο‰ i)] exact abs_total_le (Y Ο‰) (V Ο‰) calc _ ≀ |(N Ο‰ : ℝ) - βˆ‘ i, (X Ο‰ i : ℝ)| + |(βˆ‘ i, (X Ο‰ i : ℝ)) - βˆ‘ i, (clip (b i) (X Ο‰ i) : ℝ)| := abs_sub_le _ _ _ _ ≀ _ := add_le_add le_rfl hsum have hL : mean w (fun Ο‰ => dist₁ (Z Ο‰) (V Ο‰)) ≀ 2 * dist₁ a b + mean w (fun Ο‰ => |(N Ο‰ : ℝ) - βˆ‘ i, (X Ο‰ i : ℝ)|) := by have htri := mean_mono w hw (fun Ο‰ => dist₁_triangle (Z Ο‰) (Y Ο‰) (V Ο‰)) rw [mean_add] at htri linarith refine ⟨hL, ?_⟩ have htri := dist₁_triangle (fun i => mean w (fun Ο‰ => Z Ο‰ i)) a b have hmean := dist₁_mean w hw Z V have hmeans : (fun i => mean w (fun Ο‰ => V Ο‰ i)) = a := funext hm rw [hmeans] at hmean linarith omit [DecidableEq I] in lemma weighted_state {r : S β†’ β„•} (p : Outcome (I := I) r β†’ ℝ) (f : S β†’ ℝ) : (βˆ‘ x, p x * f (state x)) = βˆ‘ Ο‰, Mass p Ο‰ * f Ο‰ := by simp only [Mass, sum_mul] rw [sum_comm] apply sum_congr rfl intro x _ simp only [mul_assoc, ← mul_sum] simp omit [Fintype I] [DecidableEq S] in lemma liftSet_mean (w : S β†’ ℝ) (h₁ : βˆ‘ Ο‰, w Ο‰ = 1) (b : I β†’ ℝ) (A : S β†’ Finset I) (i : I) : mean w (fun Ο‰ => (liftSet b (A Ο‰) i : ℝ)) = (⌊b iβŒ‹ : ℝ) + βˆ‘ Ο‰, w Ο‰ * (if i ∈ A Ο‰ then 1 else 0) := by unfold mean liftSet push_cast simp_rw [mul_add] rw [sum_add_distrib, ← sum_mul, h₁, one_mul] omit [DecidableEq S] in lemma marginal_support {r : S β†’ β„•} {p : Outcome (I := I) r β†’ ℝ} (hp : βˆ€ x, 0 ≀ p x) (b : I β†’ ℝ) (hm : βˆ€ i, Marginal p i = fraction (b i)) {x : Outcome (I := I) r} (hx : 0 < p x) : subset x βŠ† fractionalSet b := by intro i hi apply mem_filter.mpr refine ⟨mem_univ _, ?_⟩ by_contra h have he : Marginal p i = 0 := (hm i).trans ((fraction_zero_iff _).mpr h) have hpos : p x ≀ Marginal p i := by calc p x = p x * (if i ∈ subset x then 1 else 0) := by simp [hi] _ ≀ _ := single_le_sum (f := fun y => p y * (if i ∈ subset y then 1 else 0)) (fun y _ => mul_nonneg (hp y) (by split_ifs <;> norm_num)) (mem_univ x) linarith /-- Full finite joint-law formulation of Section 09's star lemma. The first coordinate retains the original state and hence all its correlations. A kernel is obtained by finite disintegration below. Nonnegativity of a,b is needed only to deduce nonnegative integer output, not for this stronger signed statement. -/ theorem rounding_star_joint (w : S β†’ ℝ) (hw : βˆ€ Ο‰, 0 ≀ w Ο‰) (h₁ : βˆ‘ Ο‰, w Ο‰ = 1) (a b : I β†’ ℝ) (X : S β†’ I β†’ β„€) (N : S β†’ β„€) (hX : βˆ€ Ο‰ i, Balanced (a i) (X Ο‰ i)) (hmX : βˆ€ i, mean w (fun Ο‰ => (X Ο‰ i : ℝ)) = a i) (hN : βˆ€ Ο‰, Balanced (βˆ‘ i, b i) (N Ο‰)) (hmN : mean w (fun Ο‰ => (N Ο‰ : ℝ)) = βˆ‘ i, b i) : βˆƒ (p : Outcome (I := I) (fun Ο‰ => residual b (N Ο‰)) β†’ ℝ) (X' : Outcome (I := I) (fun Ο‰ => residual b (N Ο‰)) β†’ I β†’ β„€), (βˆ€ x, 0 ≀ p x) ∧ (βˆ€ Ο‰, Mass p Ο‰ = w Ο‰) ∧ (βˆ€ x i, Balanced (b i) (X' x i)) ∧ (βˆ€ x, (βˆ‘ i, X' x i) = N (state x)) ∧ (βˆ€ i, (βˆ‘ x, p x * (X' x i : ℝ)) = b i) ∧ (βˆ‘ x, p x * dist₁ (fun i => (X' x i : ℝ)) (fun i => (X (state x) i : ℝ))) ≀ 32 * dist₁ a b + 11 * mean w (fun Ο‰ => |(N Ο‰ : ℝ) - βˆ‘ i, (X Ο‰ i : ℝ)|) := by choose A hAH hAr hcost using fun Ο‰ => preliminary_state b (X Ο‰) (hN Ο‰) let r : S β†’ β„• := fun Ο‰ => residual b (N Ο‰) have hr : βˆ€ Ο‰ Ξ½, r Ο‰ ≀ r Ξ½ + 1 := fun Ο‰ Ξ½ => residual_adjacent b (hN Ο‰) (hN Ξ½) have hf (i : I) : 0 ≀ fraction (b i) ∧ fraction (b i) ≀ 1 := ⟨(fraction_range _).1, (fraction_range _).2.le⟩ obtain ⟨p, hp, hmass, hmar, hrepair⟩ := hamming_repair w hw h₁ r hr A hAr (fun i => fraction (b i)) hf (residual_mean w h₁ b N hN hmN) let A' : Outcome (I := I) r β†’ Finset I := fun x => if subset x βŠ† fractionalSet b then subset x else A (state x) have hA'H (x : Outcome (I := I) r) : A' x βŠ† fractionalSet b := by dsimp [A']; split_ifs with h Β· exact h Β· exact hAH _ have hA'r (x : Outcome (I := I) r) : (A' x).card = r (state x) := by dsimp [A']; split_ifs Β· exact x.property Β· exact hAr _ let X' : Outcome (I := I) r β†’ I β†’ β„€ := fun x => liftSet b (A' x) have hpos (x : Outcome (I := I) r) (hx : 0 < p x) : A' x = subset x := ite_eq_left (marginal_support hp b hmar hx) have htotal : βˆ‘ x, p x = 1 := by rw [← mass_total, show (fun Ο‰ => Mass p Ο‰) = w from funext hmass]; exact h₁ have hmean (i : I) : (βˆ‘ x, p x * (X' x i : ℝ)) = b i := by have he : (βˆ‘ x, p x * (X' x i : ℝ)) = mean p (fun x => (liftSet b (subset x) i : ℝ)) := by apply sum_congr rfl intro x _ by_cases hx : 0 < p x Β· simp only [X', hpos x hx] Β· simp [le_antisymm (le_of_not_gt hx) (hp x)] rw [he, liftSet_mean p htotal b subset i] change (⌊b iβŒ‹ : ℝ) + Marginal p i = b i rw [hmar] unfold fraction ring refine ⟨p, X', hp, hmass, fun x i => liftSet_balanced b (hA'H x) i, ?_, hmean, ?_⟩ Β· intro x rw [show (βˆ‘ i, X' x i) = (βˆ‘ i, ⌊b iβŒ‹) + (A' x).card from liftSet_sum b (A' x), hA'r] rw [show r (state x) = residual b (N (state x)) from rfl, residual_cast b (hN _)] ring Β· let L := mean w (fun Ο‰ => dist₁ (fun i => (liftSet b (A Ο‰) i : ℝ)) (fun i => (X Ο‰ i : ℝ))) let D := dist₁ (fun i => mean w (fun Ο‰ => (liftSet b (A Ο‰) i : ℝ))) b have hD : (βˆ‘ i, |(βˆ‘ Ο‰, w Ο‰ * (if i ∈ A Ο‰ then 1 else 0)) - fraction (b i)|) = D := by apply sum_congr rfl intro i _ dsimp only rw [liftSet_mean w h₁ b A i] unfold fraction congr 1 ring rw [hD] at hrepair have hbound : (βˆ‘ x, p x * dist₁ (fun i => (X' x i : ℝ)) (fun i => (X (state x) i : ℝ))) ≀ (βˆ‘ x, p x * HammingCost A x) + L := by calc _ ≀ βˆ‘ x, p x * (HammingCost A x + dist₁ (fun i => (liftSet b (A (state x)) i : ℝ)) (fun i => (X (state x) i : ℝ))) := by apply sum_le_sum intro x _ by_cases hx : 0 < p x Β· apply mul_le_mul_of_nonneg_left _ (hp x) have ht := dist₁_triangle (fun i => (X' x i : ℝ)) (fun i => (liftSet b (A (state x)) i : ℝ)) (fun i => (X (state x) i : ℝ)) simpa only [X', hpos x hx, dist₁, liftSet_distance, HammingCost] using ht Β· simp [le_antisymm (le_of_not_gt hx) (hp x)] _ = _ := by simp_rw [mul_add] rw [sum_add_distrib, weighted_state p (fun Ο‰ => dist₁ (fun i => (liftSet b (A Ο‰) i : ℝ)) (fun i => (X Ο‰ i : ℝ)))] simp only [hmass] rfl have hb := preliminary_bounds w hw h₁ a b X N hX hmX A hcost change L ≀ _ ∧ D ≀ _ at hb linarith /-- Finite disintegration, including genuinely zero-probability input states. -/ def conditionalKernel (w : S β†’ ℝ) (p : J β†’ ℝ) (st : J β†’ S) (e : S β†’ J) (Ο‰ : S) (j : J) : ℝ := if w Ο‰ = 0 then (if j = e Ο‰ then 1 else 0) else p j * (if st j = Ο‰ then 1 else 0) / w Ο‰ omit [Fintype J] [Fintype S] in lemma conditionalKernel_nonneg (w : S β†’ ℝ) (p : J β†’ ℝ) (st : J β†’ S) (e : S β†’ J) (hw : βˆ€ Ο‰, 0 ≀ w Ο‰) (hp : βˆ€ j, 0 ≀ p j) (Ο‰ : S) (j : J) : 0 ≀ conditionalKernel w p st e Ο‰ j := by unfold conditionalKernel split_ifs Β· norm_num Β· norm_num Β· exact div_nonneg (mul_nonneg (hp j) zero_le_one) (hw Ο‰) Β· simp omit [Fintype S] in lemma conditionalKernel_sum (w : S β†’ ℝ) (p : J β†’ ℝ) (st : J β†’ S) (e : S β†’ J) (hm : βˆ€ Ο‰, (βˆ‘ j, p j * (if st j = Ο‰ then 1 else 0)) = w Ο‰) (Ο‰ : S) : (βˆ‘ j, conditionalKernel w p st e Ο‰ j) = 1 := by by_cases hw : w Ο‰ = 0 Β· simp [conditionalKernel, hw] Β· simp only [conditionalKernel, ite_eq_right hw, ← sum_div, hm Ο‰, div_self hw] omit [Fintype S] in lemma conditionalKernel_identity (w : S β†’ ℝ) (p : J β†’ ℝ) (st : J β†’ S) (e : S β†’ J) (hp : βˆ€ j, 0 ≀ p j) (hm : βˆ€ Ο‰, (βˆ‘ j, p j * (if st j = Ο‰ then 1 else 0)) = w Ο‰) (Ο‰ : S) (j : J) : w Ο‰ * conditionalKernel w p st e Ο‰ j = p j * (if st j = Ο‰ then 1 else 0) := by by_cases hw : w Ο‰ = 0 Β· have hsmall : p j * (if st j = Ο‰ then 1 else 0) ≀ 0 := by calc _ ≀ βˆ‘ j, p j * (if st j = Ο‰ then 1 else 0) := single_le_sum (fun j _ => mul_nonneg (hp j) (by split_ifs <;> norm_num)) (mem_univ j) _ = 0 := (hm Ο‰).trans hw have hz : p j * (if st j = Ο‰ then 1 else 0) = 0 := le_antisymm hsmall (mul_nonneg (hp j) (by split_ifs <;> norm_num)) simp [hw, hz] Β· simp only [conditionalKernel, ite_eq_right hw] field_simp lemma conditionalKernel_average (w : S β†’ ℝ) (p : J β†’ ℝ) (st : J β†’ S) (e : S β†’ J) (hp : βˆ€ j, 0 ≀ p j) (hm : βˆ€ Ο‰, (βˆ‘ j, p j * (if st j = Ο‰ then 1 else 0)) = w Ο‰) (F : S β†’ J β†’ ℝ) : (βˆ‘ Ο‰, w Ο‰ * βˆ‘ j, conditionalKernel w p st e Ο‰ j * F Ο‰ j) = βˆ‘ j, p j * F (st j) j := by simp_rw [mul_sum, ← mul_assoc, conditionalKernel_identity w p st e hp hm] rw [sum_comm] apply sum_congr rfl intro j _ simp only [mul_assoc, ← mul_sum] simp [eq_comm] /-- The source's exact finite randomized kernel, with the constants 32 and 11, all correlations preserved, and valid outputs even at null input states. -/ theorem rounding_star (w : S β†’ ℝ) (hw : βˆ€ Ο‰, 0 ≀ w Ο‰) (h₁ : βˆ‘ Ο‰, w Ο‰ = 1) (a b : I β†’ ℝ) (_ha : βˆ€ i, 0 ≀ a i) (hb : βˆ€ i, 0 ≀ b i) (X : S β†’ I β†’ β„€) (N : S β†’ β„€) (hX : βˆ€ Ο‰ i, Balanced (a i) (X Ο‰ i)) (hmX : βˆ€ i, mean w (fun Ο‰ => (X Ο‰ i : ℝ)) = a i) (hN : βˆ€ Ο‰, Balanced (βˆ‘ i, b i) (N Ο‰)) (hmN : mean w (fun Ο‰ => (N Ο‰ : ℝ)) = βˆ‘ i, b i) : βˆƒ (ΞΊ : S β†’ Outcome (I := I) (fun Ο‰ => residual b (N Ο‰)) β†’ ℝ) (X' : S β†’ Outcome (I := I) (fun Ο‰ => residual b (N Ο‰)) β†’ I β†’ β„€), (βˆ€ Ο‰ x, 0 ≀ ΞΊ Ο‰ x) ∧ (βˆ€ Ο‰, (βˆ‘ x, ΞΊ Ο‰ x) = 1) ∧ (βˆ€ Ο‰ x i, 0 ≀ X' Ο‰ x i ∧ Balanced (b i) (X' Ο‰ x i)) ∧ (βˆ€ Ο‰ x, (βˆ‘ i, X' Ο‰ x i) = N Ο‰) ∧ (βˆ€ i, (βˆ‘ Ο‰, w Ο‰ * βˆ‘ x, ΞΊ Ο‰ x * (X' Ο‰ x i : ℝ)) = b i) ∧ (βˆ‘ Ο‰, w Ο‰ * βˆ‘ x, ΞΊ Ο‰ x * dist₁ (fun i => (X' Ο‰ x i : ℝ)) (fun i => (X Ο‰ i : ℝ))) ≀ 32 * dist₁ a b + 11 * mean w (fun Ο‰ => |(N Ο‰ : ℝ) - βˆ‘ i, (X Ο‰ i : ℝ)|) := by obtain ⟨p, Z, hp, hmass, hZ, htotal, hmean, hcost⟩ := rounding_star_joint w hw h₁ a b X N hX hmX hN hmN choose A hAH hAr hAcost using fun Ο‰ => preliminary_state b (X Ο‰) (hN Ο‰) let r : S β†’ β„• := fun Ο‰ => residual b (N Ο‰) let e : S β†’ Outcome (I := I) r := fun Ο‰ => ⟨(Ο‰, A Ο‰), hAr Ο‰βŸ© let ΞΊ := conditionalKernel w p state e let Z' : S β†’ Outcome (I := I) r β†’ I β†’ β„€ := fun Ο‰ x => if state x = Ο‰ then Z x else liftSet b (A Ο‰) have hZ' (Ο‰ : S) (x : Outcome (I := I) r) (i : I) : Balanced (b i) (Z' Ο‰ x i) := by dsimp [Z']; split_ifs Β· exact hZ x i Β· exact liftSet_balanced b (hAH Ο‰) i refine ⟨κ, Z', conditionalKernel_nonneg w p state e hw hp, conditionalKernel_sum w p state e hmass, ?_, ?_, ?_, ?_⟩ Β· intro Ο‰ x i exact ⟨(Int.floor_nonneg.mpr (hb i)).trans (balanced_bounds (hZ' Ο‰ x i)).1, hZ' Ο‰ x i⟩ Β· intro Ο‰ x dsimp [Z'] split_ifs with hx Β· exact (htotal x).trans (congrArg N hx) Β· rw [liftSet_sum, hAr, residual_cast b (hN Ο‰)] ring Β· intro i rw [conditionalKernel_average w p state e hp hmass] simp only [Z', ite_true] exact hmean i Β· rw [conditionalKernel_average w p state e hp hmass] simpa only [Z', ite_true] using hcost end KServer.Rounding /-! Continuation of Section09: finite top-down tree coupling. The sampled ancestors are retained as part of the joint state; no conditional means given those ancestors are assumed. -/ namespace KServer.TreeRounding open KServer.Rounding structure Law (Ξ± : Type) where Ξ© : Type finite : Fintype Ξ© weight : Ξ© β†’ ℝ nonneg : βˆ€ Ο‰, 0 ≀ weight Ο‰ total : βˆ‘ Ο‰, weight Ο‰=1 point : Ξ© β†’ Ξ± attribute [instance] Law.finite def Law.avg {Ξ± : Type} (L : Law Ξ±) (f : Ξ± β†’ ℝ) : ℝ := βˆ‘ Ο‰, L.weight Ο‰*f (L.point Ο‰) lemma Law.avg_const {Ξ± : Type} (L : Law Ξ±) (c : ℝ) : L.avg (fun _ => c)=c := by simp [Law.avg, ← sum_mul, L.total] lemma Law.avg_add {Ξ± : Type} (L : Law Ξ±) (f g : Ξ± β†’ ℝ) : L.avg (fun x => f x+g x)=L.avg f+L.avg g := by simp [Law.avg, mul_add, sum_add_distrib] lemma Law.avg_sub {Ξ± : Type} (L : Law Ξ±) (f g : Ξ± β†’ ℝ) : L.avg (fun x => f x-g x)=L.avg f-L.avg g := by simp [Law.avg, mul_sub, sum_sub_distrib] lemma Law.avg_sum {Ξ± I : Type} [Fintype I] (L : Law Ξ±) (f : I β†’ Ξ± β†’ ℝ) : L.avg (fun x => βˆ‘ i, f i x)=βˆ‘ i, L.avg (f i) := by simp only [Law.avg, mul_sum]; rw [sum_comm] lemma Law.avg_mono {Ξ± : Type} (L : Law Ξ±) {f g : Ξ± β†’ ℝ} (h : βˆ€ x, f x ≀ g x) : L.avg f ≀ L.avg g := sum_le_sum fun Ο‰ _ => mul_le_mul_of_nonneg_left (h _) (L.nonneg Ο‰) lemma Law.avg_nonneg {Ξ± : Type} (L : Law Ξ±) {f : Ξ± β†’ ℝ} (h : βˆ€ x, 0 ≀ f x) : 0 ≀ L.avg f := sum_nonneg fun Ο‰ _ => mul_nonneg (L.nonneg Ο‰) (h _) lemma Law.avg_congr {Ξ± : Type} (L : Law Ξ±) {f g : Ξ± β†’ ℝ} (h : βˆ€ Ο‰, f (L.point Ο‰)=g (L.point Ο‰)) : L.avg f=L.avg g := sum_congr rfl fun Ο‰ _ => congrArg (L.weight Ο‰ * Β·) (h Ο‰) /-- A finite extension with a normalized kernel retains the entire old state. -/ def Law.extend {Ξ± Ξ² J : Type} [Fintype J] (L : Law Ξ±) (ΞΊ : L.Ξ© β†’ J β†’ ℝ) (hΞΊ : βˆ€ Ο‰ j, 0 ≀ ΞΊ Ο‰ j) (hΞΊ1 : βˆ€ Ο‰, βˆ‘ j, ΞΊ Ο‰ j=1) (z : L.Ξ© β†’ J β†’ Ξ²) : Law (Ξ± Γ— Ξ²) where Ξ© := L.Ξ© Γ— J finite := inferInstance weight p := L.weight p.1*ΞΊ p.1 p.2 nonneg p := mul_nonneg (L.nonneg p.1) (hΞΊ p.1 p.2) total := by simp [Fintype.sum_prod_type, ← mul_sum, hΞΊ1, L.total] point p := (L.point p.1, z p.1 p.2) lemma Law.extend_avg {Ξ± Ξ² J : Type} [Fintype J] (L : Law Ξ±) (ΞΊ : L.Ξ© β†’ J β†’ ℝ) (hΞΊ : βˆ€ Ο‰ j, 0 ≀ ΞΊ Ο‰ j) (hΞΊ1 : βˆ€ Ο‰, βˆ‘ j, ΞΊ Ο‰ j=1) (z : L.Ξ© β†’ J β†’ Ξ²) (f : Ξ± Γ— Ξ² β†’ ℝ) : (L.extend ΞΊ hΞΊ hΞΊ1 z).avg f = βˆ‘ Ο‰, L.weight Ο‰*βˆ‘ j, ΞΊ Ο‰ j*f (L.point Ο‰, z Ο‰ j) := by change (βˆ‘ p : L.Ξ© Γ— J, (L.weight p.1*ΞΊ p.1 p.2)*f (L.point p.1,z p.1 p.2)) = _ rw [Fintype.sum_prod_type] simp only [mul_sum, mul_assoc] lemma Law.extend_old {Ξ± Ξ² J : Type} [Fintype J] (L : Law Ξ±) (ΞΊ : L.Ξ© β†’ J β†’ ℝ) (hΞΊ : βˆ€ Ο‰ j, 0 ≀ ΞΊ Ο‰ j) (hΞΊ1 : βˆ€ Ο‰, βˆ‘ j, ΞΊ Ο‰ j=1) (z : L.Ξ© β†’ J β†’ Ξ²) (f : Ξ± β†’ ℝ) : (L.extend ΞΊ hΞΊ hΞΊ1 z).avg (fun p => f p.1) = L.avg f := by rw [Law.extend_avg] simp [← sum_mul, hΞΊ1, Law.avg] def Law.map {Ξ± Ξ² : Type} (L : Law Ξ±) (f : Ξ± β†’ Ξ²) : Law Ξ² := { L with point := f ∘ L.point } @[simp] lemma Law.map_avg {Ξ± Ξ² : Type} (L : Law Ξ±) (f : Ξ± β†’ Ξ²) (g : Ξ² β†’ ℝ) : (L.map f).avg g=L.avg (g ∘ f) := rfl structure Tree (n : β„•) [NeZero n] where parent : Fin n β†’ Fin n root : parent 0=0 lower : βˆ€ v : Fin n, vβ‰ 0 β†’ (parent v).val<v.val variable {n : β„•} [NeZero n] abbrev Tree.Child (T : Tree n) (v : Fin n) := {u : Fin n // uβ‰ 0 ∧ T.parent u=v} instance (T : Tree n) (v : Fin n) : Fintype (T.Child v) := by classical unfold Tree.Child infer_instance instance (T : Tree n) (v : Fin n) : DecidableEq (T.Child v) := Classical.decEq _ lemma Tree.child_gt (T : Tree n) (v : Fin n) (u : T.Child v) : v.val<u.val.val := by simpa [u.property.2] using T.lower u.val u.property.1 def Tree.park (T : Tree n) (q : Fin n β†’ ℝ) (v : Fin n) : ℝ := q v-βˆ‘ u : T.Child v, q u.val def Tree.star (T : Tree n) (q : Fin n β†’ ℝ) (v : Fin n) : Option (T.Child v) β†’ ℝ | none => T.park q v | some u => q u.val lemma Tree.star_sum (T : Tree n) (q : Fin n β†’ ℝ) (v : Fin n) : βˆ‘ i, T.star q v i=q v := by simp [Fintype.sum_option, Tree.star, Tree.park] structure Counts (n : β„•) where subtree : Fin n β†’ β„€ park : Fin n β†’ β„€ structure PairState (n : β„•) where old : Counts n new : Counts n def Tree.intStar (T : Tree n) (c : Counts n) (v : Fin n) : Option (T.Child v) β†’ β„€ | none => c.park v | some u => c.subtree u.val def Tree.Consistent (T : Tree n) (c : Counts n) : Prop := βˆ€ v, c.park v+βˆ‘ u : T.Child v, c.subtree u.val=c.subtree v lemma Tree.intStar_sum (T : Tree n) {c : Counts n} (hc : T.Consistent c) (v : Fin n) : βˆ‘ i, T.intStar c v i=c.subtree v := by simpa [Fintype.sum_option, Tree.intStar] using hc v def Tree.Valid (T : Tree n) (q : Fin n β†’ ℝ) (c : Counts n) : Prop := (βˆ€ v, 0 ≀ c.subtree v ∧ Balanced (q v) (c.subtree v)) ∧ (βˆ€ v, 0 ≀ c.park v ∧ Balanced (T.park q v) (c.park v)) ∧ T.Consistent c def Tree.Means (T : Tree n) (q : Fin n β†’ ℝ) (L : Law (Counts n)) : Prop := (βˆ€ v, L.avg (fun c => (c.subtree v:ℝ))=q v) ∧ (βˆ€ v, L.avg (fun c => (c.park v:ℝ))=T.park q v) /-- Set just the new children and the new park of one star. -/ def Tree.setStar (T : Tree n) (c : Counts n) (v : Fin n) (z : Option (T.Child v) β†’ β„€) : Counts n where subtree u := if h : uβ‰ 0 ∧ T.parent u=v then z (some ⟨u,h⟩) else c.subtree u park u := if u=v then z none else c.park u lemma Tree.setStar_old (T : Tree n) (c : Counts n) (v u : Fin n) (z : Option (T.Child v) β†’ β„€) (h : Β¬(uβ‰ 0 ∧ T.parent u=v)) : (T.setStar c v z).subtree u=c.subtree u := by simp [Tree.setStar, h] lemma Tree.setStar_child (T : Tree n) (c : Counts n) (v : Fin n) (z : Option (T.Child v) β†’ β„€) (u : T.Child v) : (T.setStar c v z).subtree u.val=z (some u) := by simp only [Tree.setStar, dite_eq_left u.property] lemma Tree.setStar_root (T : Tree n) (c : Counts n) (v : Fin n) (z : Option (T.Child v) β†’ β„€) : (T.setStar c v z).subtree 0=c.subtree 0 := by simp [Tree.setStar] lemma Tree.setStar_self (T : Tree n) (c : Counts n) (v : Fin n) (z : Option (T.Child v) β†’ β„€) : (T.setStar c v z).subtree v=c.subtree v := by apply T.setStar_old rintro ⟨hv,hp⟩ have h := T.lower v hv rw [hp] at h exact (lt_irrefl _) h lemma Tree.setStar_intStar (T : Tree n) (c : Counts n) (v : Fin n) (z : Option (T.Child v) β†’ β„€) : T.intStar (T.setStar c v z) v=z := by funext i cases i with | none => simp [Tree.intStar, Tree.setStar] | some u => exact T.setStar_child c v z u lemma Tree.setStar_other (T : Tree n) (c : Counts n) (v w : Fin n) (z : Option (T.Child v) β†’ β„€) (hw : wβ‰ v) : T.intStar (T.setStar c v z) w=T.intStar c w := by funext i cases i with | none => simp [Tree.intStar, Tree.setStar, hw] | some u => apply T.setStar_old rintro ⟨_,hp⟩ exact hw (u.property.2.symm.trans hp) lemma Tree.setStar_before (T : Tree n) (c : Counts n) (v w : Fin n) (z : Option (T.Child v) β†’ β„€) (hw : w.val ≀ v.val) : (T.setStar c v z).subtree w=c.subtree w := by apply T.setStar_old rintro ⟨h0,hp⟩ have h := T.lower w h0 rw [hp] at h omega def Tree.Assigned (T : Tree n) (m : β„•) (v : Fin n) : Prop := v=0 ∨ (T.parent v).val < m lemma Tree.assigned_self (T : Tree n) (v : Fin n) : T.Assigned v.val v := by by_cases h : v=0 Β· exact Or.inl h Β· exact Or.inr (T.lower v h) lemma Tree.assigned_next (T : Tree n) (v u : Fin n) (h : T.Assigned (v.val+1) u) : T.Assigned v.val u ∨ (uβ‰ 0 ∧ T.parent u=v) := by by_cases h0 : u=0 Β· exact Or.inl (Or.inl h0) rcases h with h | h Β· exact (h0 h).elim by_cases hp : (T.parent u).val<v.val Β· exact Or.inl (Or.inr hp) Β· right exact ⟨h0, Fin.ext (by omega)⟩ lemma Tree.assigned_not_child (T : Tree n) (v u : Fin n) (h : T.Assigned v.val u) : Β¬(uβ‰ 0 ∧ T.parent u=v) := by rintro ⟨h0,hp⟩ rcases h with h | h Β· exact h0 h Β· rw [hp] at h; exact lt_irrefl _ h /-- The induction stores the original joint law and every already rounded ancestor, so all conditional correlations are retained. -/ structure Stage (T : Tree n) (a b : Fin n β†’ ℝ) (k : β„€) (Lβ‚€ : Law (Counts n)) (m : β„•) where law : Law (PairState n) oldLaw : βˆ€ f : Counts n β†’ ℝ, law.avg (fun s => f s.old)=Lβ‚€.avg f oldValid : βˆ€ Ο‰, T.Valid a (law.point Ο‰).old root : βˆ€ Ο‰, (law.point Ο‰).new.subtree 0=k balanced : βˆ€ u, T.Assigned m u β†’ βˆ€ Ο‰, 0 ≀ (law.point Ο‰).new.subtree u ∧ Balanced (b u) ((law.point Ο‰).new.subtree u) means : βˆ€ u, T.Assigned m u β†’ law.avg (fun s => (s.new.subtree u:ℝ))=b u parkBalanced : βˆ€ u, u.val < m β†’ βˆ€ Ο‰, 0 ≀ (law.point Ο‰).new.park u ∧ Balanced (T.park b u) ((law.point Ο‰).new.park u) parkMeans : βˆ€ u, u.val < m β†’ law.avg (fun s => (s.new.park u:ℝ))=T.park b u consistent : βˆ€ u, u.val < m β†’ βˆ€ Ο‰, (law.point Ο‰).new.park u+βˆ‘ c : T.Child u, (law.point Ο‰).new.subtree c.val= (law.point Ο‰).new.subtree u cost : βˆ€ u, u.val < m β†’ law.avg (fun s => dist₁ (fun i => (T.intStar s.new u i:ℝ)) (fun i => (T.intStar s.old u i:ℝ))) ≀ 32*dist₁ (T.star a u) (T.star b u)+ 11*law.avg (fun s => |(s.new.subtree u:ℝ)-(s.old.subtree u:ℝ)|) /-- The saved old law's means are valid at every intermediate refinement. -/ lemma Stage.oldMean {T : Tree n} {a b : Fin n β†’ ℝ} {k : β„€} {Lβ‚€ : Law (Counts n)} {m : β„•} (S : Stage T a b k Lβ‚€ m) (hmean : T.Means a Lβ‚€) (v : Fin n) (i : Option (T.Child v)) : mean S.law.weight (fun Ο‰ => (T.intStar (S.law.point Ο‰).old v i:ℝ))=T.star a v i := by change S.law.avg (fun s => (T.intStar s.old v i:ℝ)) = _ rw [S.oldLaw (fun c => (T.intStar c v i:ℝ))] cases i with | none => exact hmean.2 v | some u => exact hmean.1 u.val /-- A finite kernel is extended and its output written into one star. -/ def stepLaw {T : Tree n} (L : Law (PairState n)) (v : Fin n) {J : Type} [Fintype J] (ΞΊ : L.Ξ© β†’ J β†’ ℝ) (hΞΊ : βˆ€ Ο‰ j, 0 ≀ ΞΊ Ο‰ j) (hΞΊ1 : βˆ€ Ο‰, βˆ‘ j, ΞΊ Ο‰ j=1) (z : L.Ξ© β†’ J β†’ Option (T.Child v) β†’ β„€) : Law (PairState n) := (L.extend ΞΊ hΞΊ hΞΊ1 z).map fun p => ⟨p.1.old,T.setStar p.1.new v p.2⟩ lemma stepLaw_avg {T : Tree n} (L : Law (PairState n)) (v : Fin n) {J : Type} [Fintype J] (ΞΊ : L.Ξ© β†’ J β†’ ℝ) (hΞΊ : βˆ€ Ο‰ j, 0 ≀ ΞΊ Ο‰ j) (hΞΊ1 : βˆ€ Ο‰, βˆ‘ j, ΞΊ Ο‰ j=1) (z : L.Ξ© β†’ J β†’ Option (T.Child v) β†’ β„€) (f : PairState n β†’ ℝ) : (stepLaw L v ΞΊ hΞΊ hΞΊ1 z).avg f= βˆ‘ Ο‰, L.weight Ο‰*βˆ‘ j, ΞΊ Ο‰ j*f ⟨(L.point Ο‰).old,T.setStar (L.point Ο‰).new v (z Ο‰ j)⟩ := by unfold stepLaw rw [Law.map_avg, Law.extend_avg] rfl lemma stepLaw_preserve {T : Tree n} (L : Law (PairState n)) (v : Fin n) {J : Type} [Fintype J] (ΞΊ : L.Ξ© β†’ J β†’ ℝ) (hΞΊ : βˆ€ Ο‰ j, 0 ≀ ΞΊ Ο‰ j) (hΞΊ1 : βˆ€ Ο‰, βˆ‘ j, ΞΊ Ο‰ j=1) (z : L.Ξ© β†’ J β†’ Option (T.Child v) β†’ β„€) (f : PairState n β†’ ℝ) (hf : βˆ€ s Z, f ⟨s.old,T.setStar s.new v Z⟩=f s) : (stepLaw L v ΞΊ hΞΊ hΞΊ1 z).avg f=L.avg f := by rw [stepLaw_avg] simp only [hf, ← sum_mul, hΞΊ1, one_mul] rfl /-- One complete top-down update. The only existence input is the proved finite star kernel, not a policy or a tree-rounding interface. -/ theorem Stage.next {T : Tree n} {a b : Fin n β†’ ℝ} {k : β„€} {Lβ‚€ : Law (Counts n)} (ha : βˆ€ v, 0 ≀ a v) (hpa : βˆ€ v, 0 ≀ T.park a v) (hb : βˆ€ v, 0 ≀ b v) (hpb : βˆ€ v, 0 ≀ T.park b v) (hmean : T.Means a Lβ‚€) (v : Fin n) (S : Stage T a b k Lβ‚€ v.val) : Nonempty (Stage T a b k Lβ‚€ (v.val+1)) := by classical let L := S.law let old : L.Ξ© β†’ Option (T.Child v) β†’ β„€ := fun Ο‰ => T.intStar (L.point Ο‰).old v let N : L.Ξ© β†’ β„€ := fun Ο‰ => (L.point Ο‰).new.subtree v have hsa : βˆ€ i, 0 ≀ T.star a v i := by intro i; cases i with | none => exact hpa v | some u => exact ha u.val have hsb : βˆ€ i, 0 ≀ T.star b v i := by intro i; cases i with | none => exact hpb v | some u => exact hb u.val have hX : βˆ€ Ο‰ i, Balanced (T.star a v i) (old Ο‰ i) := by intro Ο‰ i; cases i with | none => exact (S.oldValid Ο‰).2.1 v |>.2 | some u => exact (S.oldValid Ο‰).1 u.val |>.2 have hmX : βˆ€ i, mean L.weight (fun Ο‰ => (old Ο‰ i:ℝ))=T.star a v i := S.oldMean hmean v have hN : βˆ€ Ο‰, Balanced (βˆ‘ i, T.star b v i) (N Ο‰) := by rw [T.star_sum] exact fun Ο‰ => (S.balanced v (T.assigned_self v) Ο‰).2 have hmN : mean L.weight (fun Ο‰ => (N Ο‰:ℝ))=βˆ‘ i, T.star b v i := by rw [T.star_sum] exact S.means v (T.assigned_self v) obtain ⟨κ,z,hΞΊ,hΞΊ1,hz,hztot,hzmean,hcost⟩ := rounding_star L.weight L.nonneg L.total (T.star a v) (T.star b v) hsa hsb old N hX hmX hN hmN let R := stepLaw L v ΞΊ hΞΊ hΞΊ1 z have hpres (f : PairState n β†’ ℝ) (hf : βˆ€ s Z, f ⟨s.old,T.setStar s.new v Z⟩=f s) : R.avg f=L.avg f := stepLaw_preserve L v ΞΊ hΞΊ hΞΊ1 z f hf have hcount (u : Fin n) (hu : Β¬(uβ‰ 0 ∧ T.parent u=v)) : R.avg (fun s => (s.new.subtree u:ℝ))=L.avg (fun s => (s.new.subtree u:ℝ)) := by apply hpres intro s Z rw [T.setStar_old _ _ _ _ hu] have hchild (u : T.Child v) : R.avg (fun s => (s.new.subtree u.val:ℝ))=b u.val := by rw [stepLaw_avg] simpa only [Tree.setStar_child, Tree.star] using hzmean (some u) have hpark : R.avg (fun s => (s.new.park v:ℝ))=T.park b v := by rw [stepLaw_avg] simpa only [Tree.setStar, ite_true, Tree.star] using hzmean none refine ⟨{ law := R oldLaw := ?_ oldValid := ?_ root := ?_ balanced := ?_ means := ?_ parkBalanced := ?_ parkMeans := ?_ consistent := ?_ cost := ?_ }⟩ Β· intro f exact (hpres _ (fun _ _ => rfl)).trans (S.oldLaw f) Β· intro Ο‰ exact S.oldValid Ο‰.1 Β· intro Ο‰ change (T.setStar (L.point Ο‰.1).new v (z Ο‰.1 Ο‰.2)).subtree 0=k rw [T.setStar_root] exact S.root Ο‰.1 Β· intro u hu Ο‰ rcases T.assigned_next v u hu with hu | hu Β· change 0 ≀ (T.setStar (L.point Ο‰.1).new v (z Ο‰.1 Ο‰.2)).subtree u ∧ Balanced (b u) ((T.setStar (L.point Ο‰.1).new v (z Ο‰.1 Ο‰.2)).subtree u) rw [T.setStar_old _ _ _ _ (T.assigned_not_child v u hu)] exact S.balanced u hu Ο‰.1 Β· change 0 ≀ (T.setStar (L.point Ο‰.1).new v (z Ο‰.1 Ο‰.2)).subtree u ∧ Balanced (b u) ((T.setStar (L.point Ο‰.1).new v (z Ο‰.1 Ο‰.2)).subtree u) have he := T.setStar_child (L.point Ο‰.1).new v (z Ο‰.1 Ο‰.2) ⟨u,hu⟩ change (T.setStar (L.point Ο‰.1).new v (z Ο‰.1 Ο‰.2)).subtree u=z Ο‰.1 Ο‰.2 (some ⟨u,hu⟩) at he rw [he] exact hz Ο‰.1 Ο‰.2 (some ⟨u,hu⟩) Β· intro u hu rcases T.assigned_next v u hu with hu | hu Β· exact (hcount u (T.assigned_not_child v u hu)).trans (S.means u hu) Β· exact hchild ⟨u,hu⟩ Β· intro u hu Ο‰ change 0 ≀ (T.setStar (L.point Ο‰.1).new v (z Ο‰.1 Ο‰.2)).park u ∧ Balanced (T.park b u) ((T.setStar (L.point Ο‰.1).new v (z Ο‰.1 Ο‰.2)).park u) by_cases huv : u=v Β· subst u simpa only [Tree.setStar, ite_true, Tree.star] using hz Ο‰.1 Ο‰.2 none Β· have hul : u.val<v.val := by have hne := Fin.val_ne_of_ne huv; omega simpa only [Tree.setStar, ite_eq_right huv] using S.parkBalanced u hul Ο‰.1 Β· intro u hu by_cases huv : u=v Β· subst u; exact hpark Β· have hul : u.val<v.val := by have hne := Fin.val_ne_of_ne huv; omega have he : R.avg (fun s => (s.new.park u:ℝ))=L.avg (fun s => (s.new.park u:ℝ)) := by apply hpres intro s Z simp only [Tree.setStar, ite_eq_right huv] exact he.trans (S.parkMeans u hul) Β· intro u hu Ο‰ change (T.setStar (L.point Ο‰.1).new v (z Ο‰.1 Ο‰.2)).park u+ βˆ‘ c : T.Child u, (T.setStar (L.point Ο‰.1).new v (z Ο‰.1 Ο‰.2)).subtree c.val= (T.setStar (L.point Ο‰.1).new v (z Ο‰.1 Ο‰.2)).subtree u by_cases huv : u=v Β· subst u rw [T.setStar_self] have he := hztot Ο‰.1 Ο‰.2 rw [Fintype.sum_option] at he simp only [Tree.setStar_child] simpa only [Tree.setStar, ite_true] using he Β· have hul : u.val<v.val := by have hne := Fin.val_ne_of_ne huv; omega rw [T.setStar_before _ _ _ _ hul.le] have he := congrArg (fun f => βˆ‘ i, f i) (T.setStar_other (L.point Ο‰.1).new v u (z Ο‰.1 Ο‰.2) huv) simp only [Fintype.sum_option, Tree.intStar] at he rw [he] exact S.consistent u hul Ο‰.1 Β· intro u hu by_cases huv : u=v Β· subst u have hleft : R.avg (fun s => dist₁ (fun i => (T.intStar s.new v i:ℝ)) (fun i => (T.intStar s.old v i:ℝ)))= βˆ‘ Ο‰, L.weight Ο‰*βˆ‘ j, ΞΊ Ο‰ j*dist₁ (fun i => (z Ο‰ j i:ℝ)) (fun i => (old Ο‰ i:ℝ)) := by rw [stepLaw_avg] simp only [Tree.setStar_intStar] rfl rw [hleft] have hright : R.avg (fun s => |(s.new.subtree v:ℝ)-(s.old.subtree v:ℝ)|)= mean L.weight (fun Ο‰ => |(N Ο‰:ℝ)-βˆ‘ i, (old Ο‰ i:ℝ)|) := by rw [hpres _ (by intro s Z; rw [T.setStar_self])] unfold Law.avg mean apply sum_congr rfl intro Ο‰ _ congr 1 change |((L.point Ο‰).new.subtree v:ℝ)-((L.point Ο‰).old.subtree v:ℝ)| = |((L.point Ο‰).new.subtree v:ℝ)-βˆ‘ i, (T.intStar (L.point Ο‰).old v i:ℝ)| rw [← Int.cast_sum, T.intStar_sum (S.oldValid Ο‰).2.2] rw [hright] exact hcost Β· have hul : u.val<v.val := by have hne := Fin.val_ne_of_ne huv; omega rw [hpres _ (by intro s Z; rw [T.setStar_other _ _ _ _ huv])] rw [hpres _ (by intro s Z; rw [T.setStar_before _ _ _ _ hul.le])] exact S.cost u hul def initialCounts (k : β„€) : Counts n where subtree v := if v=0 then k else 0 park _ := 0 lemma stage_zero {T : Tree n} {a b : Fin n β†’ ℝ} {k : β„€} (hk : 0 ≀ k) (hb0 : b 0=(k:ℝ)) (Lβ‚€ : Law (Counts n)) (hvalid : βˆ€ Ο‰, T.Valid a (Lβ‚€.point Ο‰)) : Nonempty (Stage T a b k Lβ‚€ 0) := by classical let L := Lβ‚€.map fun c => (⟨c,initialCounts k⟩ : PairState n) have hroot : βˆ€ Ο‰, (L.point Ο‰).new.subtree 0=k := by intro Ο‰ change (if (0 : Fin n)=0 then k else 0)=k simp refine ⟨{ law := L oldLaw := fun _ => rfl oldValid := hvalid root := hroot balanced := ?_ means := ?_ parkBalanced := by intro u hu; omega parkMeans := by intro u hu; omega consistent := by intro u hu; omega cost := by intro u hu; omega }⟩ Β· intro u hu Ο‰ have hu0 : u=0 := hu.resolve_right (by omega) subst u rw [hroot Ο‰, hb0] exact ⟨hk, Or.inl (by simp)⟩ Β· intro u hu have hu0 : u=0 := hu.resolve_right (by omega) subst u rw [hb0] calc _ = L.avg (fun _ => (k:ℝ)) := L.avg_congr fun Ο‰ => by rw [hroot Ο‰] _ = _ := L.avg_const _ /-- All vertices are processed in increasing order; the parent order is the only tree-shape assumption used by the constructive correlated coupling. -/ theorem tree_coupling {T : Tree n} {a b : Fin n β†’ ℝ} {k : β„€} (ha : βˆ€ v, 0 ≀ a v) (hpa : βˆ€ v, 0 ≀ T.park a v) (hb : βˆ€ v, 0 ≀ b v) (hpb : βˆ€ v, 0 ≀ T.park b v) (hk : 0 ≀ k) (hb0 : b 0=(k:ℝ)) (Lβ‚€ : Law (Counts n)) (hvalid : βˆ€ Ο‰, T.Valid a (Lβ‚€.point Ο‰)) (hmean : T.Means a Lβ‚€) : βˆƒ L : Law (PairState n), (βˆ€ f : Counts n β†’ ℝ, L.avg (fun s => f s.old)=Lβ‚€.avg f) ∧ (βˆ€ Ο‰, T.Valid a (L.point Ο‰).old ∧ T.Valid b (L.point Ο‰).new ∧ (L.point Ο‰).new.subtree 0=k) ∧ (βˆ€ v, L.avg (fun s => (s.new.subtree v:ℝ))=b v) ∧ (βˆ€ v, L.avg (fun s => (s.new.park v:ℝ))=T.park b v) ∧ (βˆ€ v, L.avg (fun s => dist₁ (fun i => (T.intStar s.new v i:ℝ)) (fun i => (T.intStar s.old v i:ℝ))) ≀ 32*dist₁ (T.star a v) (T.star b v)+ 11*L.avg (fun s => |(s.new.subtree v:ℝ)-(s.old.subtree v:ℝ)|)) := by have hstage : βˆ€ m, m ≀ n β†’ Nonempty (Stage T a b k Lβ‚€ m) := by intro m induction m with | zero => intro _; exact stage_zero hk hb0 Lβ‚€ hvalid | succ m ih => intro hm obtain ⟨S⟩ := ih (by omega) exact S.next ha hpa hb hpb hmean ⟨m,by omega⟩ obtain ⟨S⟩ := hstage n le_rfl have hassigned (v : Fin n) : T.Assigned n v := Or.inr (T.parent v).isLt refine ⟨S.law,S.oldLaw,?_,?_,?_,?_⟩ Β· intro Ο‰ exact ⟨S.oldValid Ο‰, ⟨fun v => S.balanced v (hassigned v) Ο‰, fun v => S.parkBalanced v v.isLt Ο‰, fun v => S.consistent v v.isLt Ο‰βŸ©,S.root Ο‰βŸ© Β· intro v; exact S.means v (hassigned v) Β· intro v; exact S.parkMeans v v.isLt Β· intro v; exact S.cost v v.isLt lemma Law.avg_mul {Ξ± : Type} (L : Law Ξ±) (c : ℝ) (f : Ξ± β†’ ℝ) : L.avg (fun x => c*f x)=c*L.avg f := by simp only [Law.avg, mul_sum, mul_left_comm (L.weight _) c] lemma Law.avg_le_of_support {Ξ± : Type} (L : Law Ξ±) {f g : Ξ± β†’ ℝ} (h : βˆ€ Ο‰, f (L.point Ο‰) ≀ g (L.point Ο‰)) : L.avg f ≀ L.avg g := sum_le_sum fun Ο‰ _ => mul_le_mul_of_nonneg_left (h Ο‰) (L.nonneg Ο‰) /-- Every nonroot vertex occurs in exactly one child list. -/ def Tree.childEquiv (T : Tree n) : (Ξ£ v : Fin n, T.Child v) ≃ {v : Fin n // vβ‰ 0} where toFun p := ⟨p.2.val,p.2.property.1⟩ invFun u := ⟨T.parent u.val,⟨u.val,u.property,rfl⟩⟩ left_inv := by rintro ⟨v,u,hu,hp⟩ dsimp subst v rfl right_inv := by intro u; rfl lemma Tree.weighted_children (T : Tree n) (w f : Fin n β†’ ℝ) (h0 : f 0=0) : (βˆ‘ v, w v*βˆ‘ u : T.Child v, f u.val)=βˆ‘ u, w (T.parent u)*f u := by classical simp_rw [mul_sum] rw [← Fintype.sum_sigma (fun p : Ξ£ v : Fin n, T.Child v => w p.1*f p.2.val)] have he := Fintype.sum_equiv (T.childEquiv) (fun p : Ξ£ v : Fin n, T.Child v => w p.1*f p.2.val) (fun u : {v : Fin n // vβ‰ 0} => w (T.parent u.val)*f u.val) (by intro p; change w p.1*f p.2.val=w (T.parent p.2.val)*f p.2.val rw [p.2.property.2]) rw [he] have hsub := Fintype.sum_subtype_add_sum_subtype (fun v : Fin n => vβ‰ 0) (fun u => w (T.parent u)*f u) have hz : (βˆ‘ u : {v : Fin n // Β¬vβ‰ 0}, w (T.parent u.val)*f u.val)=0 := by apply sum_eq_zero intro u _ have hu : u.val=0 := not_ne_iff.mp u.property simp [hu,h0] simpa only [hz, add_zero] using hsub lemma Tree.star_distance (T : Tree n) (a b : Fin n β†’ ℝ) (v : Fin n) : dist₁ (T.star a v) (T.star b v) ≀ |a v-b v|+2*βˆ‘ u : T.Child v, |a u.val-b u.val| := by have hp : |T.park a v-T.park b v| ≀ |a v-b v|+βˆ‘ u : T.Child v, |a u.val-b u.val| := by calc _ = |(a v-b v)-βˆ‘ u : T.Child v, (a u.val-b u.val)| := by unfold Tree.park rw [sum_sub_distrib] congr 1; ring _ ≀ |a v-b v|+|βˆ‘ u : T.Child v, (a u.val-b u.val)| := abs_sub _ _ _ ≀ _ := add_le_add_right (abs_sum_le_sum_abs (fun u : T.Child v => a u.val-b u.val) Finset.univ) _ simpa only [dist₁, Fintype.sum_option, Tree.star] using (show |T.park a v-T.park b v|+(βˆ‘ u : T.Child v, |a u.val-b u.val|) ≀ |a v-b v|+2*βˆ‘ u : T.Child v, |a u.val-b u.val| by linarith) lemma Tree.children_distance_le (T : Tree n) (c d : Counts n) (v : Fin n) : (βˆ‘ u : T.Child v, |(c.subtree u.val:ℝ)-(d.subtree u.val:ℝ)|) ≀ dist₁ (fun i => (T.intStar c v i:ℝ)) (fun i => (T.intStar d v i:ℝ)) := by simp only [dist₁, Fintype.sum_option, Tree.intStar] linarith [abs_nonneg ((c.park v:ℝ)-(d.park v:ℝ))] /-- The radius at a nonroot node is one tau-th of its parent radius. -/ lemma Tree.parent_weight (T : Tree n) {Ο„ : ℝ} (hΟ„ : 0 < Ο„) (w f : Fin n β†’ ℝ) (hscale : βˆ€ u, uβ‰ 0 β†’ w (T.parent u)=Ο„*w u) (h0 : f 0=0) : (βˆ‘ u, w u*f u)=(1/Ο„)*(βˆ‘ u, w (T.parent u)*f u) := by rw [mul_sum] apply sum_congr rfl intro u _ by_cases hu : u=0 Β· simp [hu,h0] Β· rw [hscale u hu] field_simp lemma Tree.star_weight_bound (T : Tree n) {Ο„ : ℝ} (hΟ„ : 0 < Ο„) (w a b : Fin n β†’ ℝ) (hw : βˆ€ u, 0 ≀ w u) (hscale : βˆ€ u, uβ‰ 0 β†’ w (T.parent u)=Ο„*w u) (h0 : a 0=b 0) : (βˆ‘ v, w v*dist₁ (T.star a v) (T.star b v)) ≀ (2+1/Ο„)*(βˆ‘ u, w (T.parent u)*|a u-b u|) := by calc _ ≀ βˆ‘ v, w v*(|a v-b v|+2*βˆ‘ u : T.Child v, |a u.val-b u.val|) := sum_le_sum fun v _ => mul_le_mul_of_nonneg_left (T.star_distance a b v) (hw v) _ = (βˆ‘ v, w v*|a v-b v|)+2*(βˆ‘ v, w v*βˆ‘ u : T.Child v, |a u.val-b u.val|) := by simp only [mul_add,mul_left_comm (w _) (2:ℝ),sum_add_distrib,mul_sum] _ = _ := by rw [T.weighted_children w (fun u => |a u-b u|) (by simp [h0]), T.parent_weight hΟ„ w _ hscale (by simp [h0])] ring /-- Finite, exact absorption of the local 32/11 bounds. No tree depth or initialization cost enters the coefficient. -/ theorem Tree.rounding_absorption (T : Tree n) {Ο„ : ℝ} (hΟ„ : 22 ≀ Ο„) (w a b : Fin n β†’ ℝ) (hw : βˆ€ u, 0 ≀ w u) (hscale : βˆ€ u, uβ‰ 0 β†’ w (T.parent u)=Ο„*w u) (h0 : a 0=b 0) (L : Law (PairState n)) (hroot : βˆ€ Ο‰, (L.point Ο‰).new.subtree 0=(L.point Ο‰).old.subtree 0) (hcost : βˆ€ v, L.avg (fun s => dist₁ (fun i => (T.intStar s.new v i:ℝ)) (fun i => (T.intStar s.old v i:ℝ))) ≀ 32*dist₁ (T.star a v) (T.star b v)+ 11*L.avg (fun s => |(s.new.subtree v:ℝ)-(s.old.subtree v:ℝ)|)) : L.avg (fun s => βˆ‘ u, w (T.parent u)*|(s.new.subtree u:ℝ)-(s.old.subtree u:ℝ)|) ≀ 132*(βˆ‘ u, w (T.parent u)*|a u-b u|) := by have ht : 0 < Ο„ := by linarith let F : Fin n β†’ PairState n β†’ ℝ := fun u s => |(s.new.subtree u:ℝ)-(s.old.subtree u:ℝ)| let V := βˆ‘ u, w (T.parent u)*|a u-b u| let N := L.avg (fun s => βˆ‘ u, w (T.parent u)*F u s) have hV : 0 ≀ V := sum_nonneg fun u _ => mul_nonneg (hw _) (abs_nonneg _) have hN : 0 ≀ N := L.avg_nonneg fun s => sum_nonneg fun u _ => mul_nonneg (hw _) (abs_nonneg _) have hF0 : βˆ€ Ο‰, F 0 (L.point Ο‰)=0 := by intro Ο‰; simp [F,hroot Ο‰] have havg0 : L.avg (F 0)=0 := by calc _ = L.avg (fun _ => 0) := L.avg_congr hF0 _ = 0 := L.avg_const _ have hNavg : N=βˆ‘ u, w (T.parent u)*L.avg (F u) := by dsimp [N] rw [L.avg_sum] simp_rw [L.avg_mul] have hparent : (βˆ‘ v, w v*L.avg (F v))=(1/Ο„)*N := by rw [T.parent_weight ht w _ hscale havg0, hNavg] have hchildren : (βˆ‘ v, w v*L.avg (fun s => βˆ‘ u : T.Child v, F u.val s))=N := by simp_rw [L.avg_sum] rw [T.weighted_children w (fun u => L.avg (F u)) havg0, hNavg] have hrec : N ≀ (64+32/Ο„)*V+(11/Ο„)*N := by calc N = βˆ‘ v, w v*L.avg (fun s => βˆ‘ u : T.Child v, F u.val s) := hchildren.symm _ ≀ βˆ‘ v, w v*L.avg (fun s => dist₁ (fun i => (T.intStar s.new v i:ℝ)) (fun i => (T.intStar s.old v i:ℝ))) := by apply sum_le_sum intro v _ exact mul_le_mul_of_nonneg_left (L.avg_mono (fun s => T.children_distance_le s.new s.old v)) (hw v) _ ≀ βˆ‘ v, w v*(32*dist₁ (T.star a v) (T.star b v)+11*L.avg (F v)) := sum_le_sum fun v _ => mul_le_mul_of_nonneg_left (hcost v) (hw v) _ = 32*(βˆ‘ v, w v*dist₁ (T.star a v) (T.star b v))+11*(βˆ‘ v, w v*L.avg (F v)) := by simp only [mul_add,mul_left_comm (w _) (32:ℝ),mul_left_comm (w _) (11:ℝ),sum_add_distrib,mul_sum] _ ≀ 32*((2+1/Ο„)*V)+11*((1/Ο„)*N) := by rw [hparent] exact add_le_add_left (mul_le_mul_of_nonneg_left (T.star_weight_bound ht w a b hw hscale h0) (by norm_num : (0:ℝ) ≀32)) _ _ = _ := by ring have h11 : 11/Ο„ ≀ 1/2 := (div_le_iffβ‚€ ht).mpr (by linarith) have h32 : 32/Ο„ ≀ 2 := (div_le_iffβ‚€ ht).mpr (by linarith) have hrec' : N ≀ 66*V+(1/2)*N := hrec.trans (add_le_add (mul_le_mul_of_nonneg_right (by linarith : 64+32/Ο„ ≀66) hV) (mul_le_mul_of_nonneg_right h11 hN)) change N ≀ 132*V linarith def Law.pure {Ξ± : Type} (x : Ξ±) : Law Ξ± where Ξ© := Unit finite := inferInstance weight _ := 1 nonneg _ := by norm_num total := by simp point _ := x @[simp] lemma Law.avg_pure {Ξ± : Type} (x : Ξ±) (f : Ξ± β†’ ℝ) : (Law.pure x).avg f=f x := by change (βˆ‘ Ο‰ : Unit, (1:ℝ)*f x)=f x simp lemma balanced_integer {r : ℝ} {j k : β„€} (hr : r=(k:ℝ)) (hj : Balanced r j) : j=k := by simpa [hr,KServer.Rounding.Balanced] using hj /-- Deterministic park of all k units at the root, solely for initialization. -/ def stock (k : β„€) : Counts n where subtree v := if v=0 then k else 0 park v := if v=0 then k else 0 def stockMass (k : β„€) (v : Fin n) : ℝ := if v=0 then (k:ℝ) else 0 lemma stockMass_park (T : Tree n) (k : β„€) (v : Fin n) : T.park (stockMass k) v=stockMass k v := by have hs : (βˆ‘ u : T.Child v, stockMass k u.val)=0 := sum_eq_zero fun u _ => by simp [stockMass,u.property.1] simp [Tree.park,hs] lemma stock_valid (T : Tree n) {k : β„€} (hk : 0 ≀ k) : T.Valid (stockMass k) (stock k) := by have hb (v : Fin n) : 0 ≀ (stock k : Counts n).subtree v ∧ KServer.Rounding.Balanced (stockMass k v) ((stock k : Counts n).subtree v) := by by_cases hv : v=0 Β· simp [stock,stockMass,hv,KServer.Rounding.Balanced,hk] Β· simp [stock,stockMass,hv,KServer.Rounding.Balanced] refine ⟨hb,?_,?_⟩ Β· intro v rw [stockMass_park] exact hb v Β· intro v have hs : (βˆ‘ u : T.Child v, (stock k : Counts n).subtree u.val)=0 := sum_eq_zero fun u _ => by simp [stock,u.property.1] change (stock k : Counts n).subtree v+_=(stock k).subtree v rw [hs,add_zero] lemma stock_means (T : Tree n) (k : β„€) : T.Means (stockMass k) (Law.pure (stock k)) := by constructor <;> intro v <;> simp [Law.avg_pure,stock,stockMass,stockMass_park] /-- Actual balanced law at the initial fractional configuration. This is not an assumed rounding interface: it is obtained by the finite star construction. -/ theorem tree_initialization (T : Tree n) (b : Fin n β†’ ℝ) {k : β„€} (hk : 0 ≀ k) (hb : βˆ€ v, 0 ≀ b v) (hpb : βˆ€ v, 0 ≀ T.park b v) (hb0 : b 0=(k:ℝ)) : βˆƒ L : Law (Counts n), (βˆ€ Ο‰, T.Valid b (L.point Ο‰) ∧ (L.point Ο‰).subtree 0=k) ∧ T.Means b L := by have ha (v : Fin n) : 0 ≀ stockMass k v := by dsimp [stockMass]; split_ifs <;> positivity obtain ⟨R,hRold,hRv,hRs,hRp,hRc⟩ := tree_coupling ha (fun v => by rw [stockMass_park]; exact ha v) hb hpb hk hb0 (Law.pure (stock k)) (fun _ => stock_valid T hk) (stock_means T k) refine ⟨R.map PairState.new,?_,?_,?_⟩ Β· intro Ο‰; exact (hRv Ο‰).2 Β· intro v; exact hRs v Β· intro v; exact hRp v /-- Section09's one-step top-down rounding, including the uniform constant132. The old marginal is kept exactly, and the new root and all parks are balanced. -/ theorem rounded_step (T : Tree n) {Ο„ : ℝ} (hΟ„ : 22 ≀ Ο„) (w a b : Fin n β†’ ℝ) (hw : βˆ€ u, 0 ≀ w u) (hscale : βˆ€ u, uβ‰ 0 β†’ w (T.parent u)=Ο„*w u) {k : β„€} (hk : 0 ≀ k) (ha0 : a 0=(k:ℝ)) (hb0 : b 0=(k:ℝ)) (ha : βˆ€ v, 0 ≀ a v) (hpa : βˆ€ v, 0 ≀ T.park a v) (hb : βˆ€ v, 0 ≀ b v) (hpb : βˆ€ v, 0 ≀ T.park b v) (Lβ‚€ : Law (Counts n)) (hvalid : βˆ€ Ο‰, T.Valid a (Lβ‚€.point Ο‰)) (hmean : T.Means a Lβ‚€) : βˆƒ L : Law (PairState n), (βˆ€ f : Counts n β†’ ℝ, L.avg (fun s => f s.old)=Lβ‚€.avg f) ∧ (βˆ€ Ο‰, T.Valid a (L.point Ο‰).old ∧ T.Valid b (L.point Ο‰).new ∧ (L.point Ο‰).new.subtree 0=k) ∧ T.Means b (L.map PairState.new) ∧ L.avg (fun s => βˆ‘ u, w (T.parent u)*|(s.new.subtree u:ℝ)-(s.old.subtree u:ℝ)|) ≀ 132*(βˆ‘ u, w (T.parent u)*|a u-b u|) := by obtain ⟨L,hLold,hLv,hLs,hLp,hLc⟩ := tree_coupling ha hpa hb hpb hk hb0 Lβ‚€ hvalid hmean refine ⟨L,hLold,hLv,⟨hLs,hLp⟩,?_⟩ apply T.rounding_absorption hΟ„ w a b hw hscale (ha0.trans hb0.symm) L _ hLc intro Ο‰ rw [(hLv Ο‰).2.2, balanced_integer ha0 ((hLv Ο‰).1.1 0).2] end KServer.TreeRounding end /- Finite-tree park conservation and geometric subcapacity. -/ noncomputable section open scoped BigOperators open Finset namespace KServer.Rounding structure Tree (n : β„•) [NeZero n] where parent : Fin n β†’ Fin n root : parent 0=0 lower : βˆ€ v : Fin n, vβ‰ 0 β†’ (parent v).val<v.val variable {n : β„•} [NeZero n] abbrev Tree.Child (T : Tree n) (v : Fin n) := {u : Fin n // uβ‰ 0 ∧ T.parent u=v} instance (T : Tree n) (v : Fin n) : Fintype (T.Child v) := by classical unfold Tree.Child infer_instance instance (T : Tree n) (v : Fin n) : DecidableEq (T.Child v) := Classical.decEq _ def Tree.park (T : Tree n) (q : Fin n β†’ ℝ) (v : Fin n) : ℝ := q v-βˆ‘ u : T.Child v, q u.val def Tree.childEquiv (T : Tree n) : (Ξ£ v : Fin n, T.Child v) ≃ {v : Fin n // vβ‰ 0} where toFun p := ⟨p.2.val,p.2.property.1⟩ invFun u := ⟨T.parent u.val,⟨u.val,u.property,rfl⟩⟩ left_inv := by rintro ⟨v,u,hu,hp⟩ dsimp subst v rfl right_inv := by intro u; rfl lemma Tree.weighted_children (T : Tree n) (w f : Fin n β†’ ℝ) (h0 : f 0=0) : (βˆ‘ v, w v*βˆ‘ u : T.Child v, f u.val)=βˆ‘ u, w (T.parent u)*f u := by classical simp_rw [mul_sum] rw [← Fintype.sum_sigma (fun p : Ξ£ v : Fin n, T.Child v => w p.1*f p.2.val)] have he := Fintype.sum_equiv (T.childEquiv) (fun p : Ξ£ v : Fin n, T.Child v => w p.1*f p.2.val) (fun u : {v : Fin n // vβ‰ 0} => w (T.parent u.val)*f u.val) (by intro p; change w p.1*f p.2.val=w (T.parent p.2.val)*f p.2.val rw [p.2.property.2]) rw [he] have hsub := Fintype.sum_subtype_add_sum_subtype (fun v : Fin n => vβ‰ 0) (fun u => w (T.parent u)*f u) have hz : (βˆ‘ u : {v : Fin n // Β¬vβ‰ 0}, w (T.parent u.val)*f u.val)=0 := by apply sum_eq_zero intro u _ have hu : u.val=0 := not_ne_iff.mp u.property simp [hu,h0] simpa only [hz, add_zero] using hsub /-- True precisely on a vertex's descendants (including the vertex itself). -/ def Tree.under (T : Tree n) (v w : Fin n) : Bool := if w=v then true else if _hw : w=0 then false else T.under v (T.parent w) termination_by w.val decreasing_by exact T.lower w _hw lemma Tree.under_self (T : Tree n) (v : Fin n) : T.under v v=true := by rw [Tree.under,ite_eq_left rfl] lemma Tree.under_le (T : Tree n) (v w : Fin n) (h : T.under v w=true) : v.val ≀ w.val := by by_cases hwv : w=v Β· subst w; exact le_rfl Β· by_cases hw0 : w=0 Β· rw [Tree.under,ite_eq_right hwv,dite_eq_left hw0] at h contradiction Β· have hp : T.under v (T.parent w)=true := by rw [Tree.under,ite_eq_right hwv,dite_eq_right hw0] at h exact h exact (T.under_le v (T.parent w) hp).trans (T.lower w hw0).le termination_by w.val decreasing_by exact T.lower w hw0 lemma Tree.under_parent_self (T : Tree n) (v : Fin n) (hv : vβ‰ 0) : T.under v (T.parent v)=false := by cases hh : T.under v (T.parent v) Β· rfl Β· have h := T.under_le v (T.parent v) hh have := T.lower v hv omega lemma Tree.under_zero (T : Tree n) (v : Fin n) : T.under v 0=(v==0) := by rw [Tree.under] by_cases h : v=0 Β· subst v; simp Β· simp [h,Ne.symm h] def Tree.indicator (T : Tree n) (v w : Fin n) : ℝ := if T.under v w then 1 else 0 lemma Tree.indicator_parent (T : Tree n) (v w : Fin n) (hw : wβ‰ 0) : T.indicator v w-T.indicator v (T.parent w) = if w=v then 1 else 0 := by by_cases h : w=v Β· subst w simp [Tree.indicator,T.under_self,T.under_parent_self v hw] Β· have he : T.under v w=T.under v (T.parent w) := by rw [Tree.under,ite_eq_right h,dite_eq_right hw] simp [Tree.indicator,he,h] /-- Literal telescoping over one subtree of the concrete parent tree. -/ theorem Tree.park_subtree_total (T : Tree n) (q : Fin n β†’ ℝ) (v : Fin n) : (βˆ‘ w, T.indicator v w*T.park q w)=q v := by classical let q0 : Fin n β†’ ℝ := fun w => if w=0 then 0 else q w have hq0 : q0 0=0 := by simp [q0] have he : βˆ€ w, (βˆ‘ u : T.Child w, q u.val) = βˆ‘ u : T.Child w, q0 u.val := by intro w apply sum_congr rfl intro u _ simp [q0,u.property.1] have hs := T.weighted_children (T.indicator v) q0 hq0 simp_rw [Tree.park,mul_sub,he] rw [sum_sub_distrib,hs,←sum_sub_distrib] have hpoint : βˆ€ w : Fin n, T.indicator v w*q w-T.indicator v (T.parent w)*q0 w = if w=v then q w else 0 := by intro w by_cases hw : w=0 Β· subst w by_cases hv : v=0 Β· subst v; simp [q0,Tree.indicator,T.under_zero] Β· simp [q0,Tree.indicator,T.under_zero,hv,Ne.symm hv] Β· simp only [q0,ite_eq_right hw] rw [←sub_mul,T.indicator_parent v w hw] split_ifs <;> simp simp_rw [hpoint] simp lemma Tree.indicator_nonneg (T : Tree n) (v w : Fin n) : 0 ≀ T.indicator v w := by unfold Tree.indicator split <;> norm_num lemma Tree.park_nonneg (T : Tree n) {q : Fin n β†’ ℝ} (h : βˆ€ v, (βˆ‘ u : T.Child v, q u.val) ≀ q v) (v : Fin n) : 0 ≀ T.park q v := sub_nonneg.mpr (h v) /-- The mass of any finite collection of descendant parks is bounded by its root allocation; overlapping depths do not introduce a height factor. -/ theorem Tree.selected_park_capacity (T : Tree n) (q : Fin n β†’ ℝ) (v : Fin n) (hp : βˆ€ w, 0 ≀ T.park q w) (S : Finset (Fin n)) (hS : βˆ€ w ∈ S, T.under v w=true) : (βˆ‘ w ∈ S, T.park q w) ≀ q v := by have hbound : (βˆ‘ w ∈ S, T.park q w) ≀ βˆ‘ w, T.indicator v w*T.park q w := by calc _ = βˆ‘ w ∈ S, T.indicator v w*T.park q w := by apply sum_congr rfl intro w hw simp [Tree.indicator,hS w hw] _ ≀ _ := sum_le_sum_of_subset_of_nonneg (subset_univ _) (fun w _ _ => mul_nonneg (T.indicator_nonneg v w) (hp w)) simpa [T.park_subtree_total] using hbound /-- Several descendant collections may be charged simultaneously to a fixed cut. The hypotheses are explicit incidence data, not an assumed mass bound. -/ theorem Tree.cut_park_capacity (T : Tree n) (q : Fin n β†’ ℝ) (hp : βˆ€ w, 0 ≀ T.park q w) (S F : Finset (Fin n)) (hcover : βˆ€ w ∈ S, βˆƒ v ∈ F, T.under v w=true) : (βˆ‘ w ∈ S, T.park q w) ≀ βˆ‘ v ∈ F, q v := by classical have hpoint : βˆ€ w ∈ S, T.park q w ≀ βˆ‘ v ∈ F, T.indicator v w*T.park q w := by intro w hw obtain ⟨v,hv,hu⟩ := hcover w hw calc _ = T.indicator v w*T.park q w := by simp [Tree.indicator,hu] _ ≀ _ := single_le_sum (fun z _ => mul_nonneg (T.indicator_nonneg z w) (hp w)) hv calc _ ≀ βˆ‘ w ∈ S, βˆ‘ v ∈ F, T.indicator v w*T.park q w := sum_le_sum hpoint _ = βˆ‘ v ∈ F, βˆ‘ w ∈ S, T.indicator v w*T.park q w := sum_comm _ ≀ βˆ‘ v ∈ F, βˆ‘ w, T.indicator v w*T.park q w := by apply sum_le_sum intro v _ exact sum_le_sum_of_subset_of_nonneg (subset_univ _) (fun w _ _ => mul_nonneg (T.indicator_nonneg v w) (hp w)) _ = _ := by simp_rw [T.park_subtree_total] section Geometry variable {Y : Type*} [MetricSpace Y] [Fintype Y] [DecidableEq Y] /-- Finite posterior counting mass; repeated hidden locations contribute their full weight and do not require an injective posterior support. -/ def countingMass (ΞΌ : Y β†’ ℝ) (R : Finset Y) : ℝ := βˆ‘ y ∈ R, ΞΌ y omit [DecidableEq Y] in /-- A cell with a nearby validity witness lies in the source's 67r ball. -/ lemma witness_ball_inclusion {R : Finset Y} {y z : Y} {r : ℝ} (hz : z ∈ R) (hnear : dist y z ≀ 27*r) (hdiam : βˆ€ a ∈ R, βˆ€ b ∈ R, dist a b ≀ 40*r) : R βŠ† univ.filter (fun a => dist y a ≀ 67*r) := by intro a ha apply mem_filter.mpr refine ⟨mem_univ _,?_⟩ have ht := dist_triangle y z a have hd := hdiam z hz a ha linarith omit [NeZero n] [MetricSpace Y] [Fintype Y] in lemma disjoint_counting_mass {ΞΌ : Y β†’ ℝ} (hΞΌ : βˆ€ y, 0 ≀ ΞΌ y) {R : Fin n β†’ Finset Y} {F : Finset (Fin n)} {B : Finset Y} (hdisj : Set.PairwiseDisjoint (↑F) R) (hsub : βˆ€ v ∈ F, R v βŠ† B) : (βˆ‘ v ∈ F, countingMass ΞΌ (R v)) ≀ countingMass ΞΌ B := by unfold countingMass rw [←sum_biUnion hdisj] apply sum_le_sum_of_subset_of_nonneg Β· intro y hy obtain ⟨v,hv,hy⟩ := mem_biUnion.mp hy exact hsub v hv hy Β· intro y _ _ exact hΞΌ y /-- Cumulative park capacity with the exact geometric radius 67. Grouping is by actual tree ancestors; posterior regions are those fixed-prefix cells. The cut F may contain unoccupied roots too, and can be the full depth-j cut. -/ theorem Tree.geometric_park_capacity (T : Tree n) (q : Fin n β†’ ℝ) (hp : βˆ€ w, 0 ≀ T.park q w) (S F : Finset (Fin n)) (hcover : βˆ€ w ∈ S, βˆƒ v ∈ F, T.under v w=true) (R : Fin n β†’ Finset Y) (ΞΌ : Y β†’ ℝ) (hΞΌ : βˆ€ y, 0 ≀ ΞΌ y) {C r : ℝ} (hC : 0 ≀ C) (y : Y) (hq : βˆ€ v ∈ F, q v ≀ C*countingMass ΞΌ (R v)) (hdisj : Set.PairwiseDisjoint (↑F) R) (hwitness : βˆ€ v ∈ F, βˆƒ z ∈ R v, dist y z ≀ 27*r) (hdiam : βˆ€ v ∈ F, βˆ€ a ∈ R v, βˆ€ b ∈ R v, dist a b ≀ 40*r) : (βˆ‘ w ∈ S, T.park q w) ≀ C*countingMass ΞΌ (univ.filter (fun z => dist y z ≀ 67*r)) := by have hsub : βˆ€ v ∈ F, R v βŠ† univ.filter (fun z => dist y z ≀ 67*r) := by intro v hv obtain ⟨z,hz,hn⟩ := hwitness v hv exact witness_ball_inclusion hz hn (hdiam v hv) calc _ ≀ βˆ‘ v ∈ F, q v := T.cut_park_capacity q hp S F hcover _ ≀ βˆ‘ v ∈ F, C*countingMass ΞΌ (R v) := sum_le_sum hq _ = C*(βˆ‘ v ∈ F, countingMass ΞΌ (R v)) := (mul_sum _ _ _).symm _ ≀ _ := mul_le_mul_of_nonneg_left (disjoint_counting_mass hΞΌ hdisj hsub) hC end Geometry end KServer.Rounding end end OAI
import Mathlib
namespace OAI
/- Prescribed correlated-total rounding and minimization over finite joint laws, without an independence requirement. -/
noncomputable section
open Finset
open scoped BigOperators
namespace KServer.Rounding
variable {J : Type*} [Fintype J] [DecidableEq J]
def move (p : J β†’ ℝ) (x y : J) (Ξ΅ : ℝ) : J β†’ ℝ :=
fun z => p z + Ξ΅ * ((if z = y then 1 else 0) - (if z = x then 1 else 0))
omit [Fintype J] in
lemma move_nonneg {p : J β†’ ℝ} (hp : βˆ€ z, 0 ≀ p z) {x y : J} {Ξ΅ : ℝ}
(hΞ΅ : 0 ≀ Ξ΅) (hx : Ξ΅ ≀ p x) : βˆ€ z, 0 ≀ move p x y Ξ΅ z := by
intro z
by_cases hz : z = x
Β· subst z
by_cases hy : x = y
Β· simp [move, hy, hp]
Β· simp only [move, ite_eq_right hy, ite_true]
linarith
Β· by_cases hy : z = y
Β· simp only [move, ite_eq_left hy, ite_eq_right hz]
linarith [hp z]
Β· simpa only [move, ite_eq_right hy, ite_eq_right hz, sub_self, mul_zero, add_zero] using hp z
lemma move_sum (p : J β†’ ℝ) (x y : J) (Ξ΅ : ℝ) (f : J β†’ ℝ) :
(βˆ‘ z, move p x y Ξ΅ z * f z) = (βˆ‘ z, p z * f z) + Ξ΅ * (f y - f x) := by
calc (βˆ‘ z, move p x y Ξ΅ z * f z) =
βˆ‘ z, (p z * f z + (if z = y then Ξ΅ * f z else 0) -
(if z = x then Ξ΅ * f z else 0)) := by
apply sum_congr rfl
intro z _
simp only [move]
split_ifs <;> ring
_ = _ := by rw [sum_sub_distrib, sum_add_distrib]; simp; ring
omit [Fintype J] in
lemma move_coordinate (p : J β†’ ℝ) {x y z : J} {Ξ΅ : ℝ} (hΞ΅ : 0 ≀ Ξ΅)
(hz : z β‰  x) : p z ≀ move p x y Ξ΅ z := by
by_cases hy : z = y
Β· simp only [move, ite_eq_left hy, ite_eq_right hz]; linarith
Β· simp [move, hy, hz]
variable {S I : Type*} [Fintype S] [Fintype I] [DecidableEq S] [DecidableEq I]
/-- Each fiber records the actual observed total, not a conditional mean. -/
abbrev Outcome (r : S β†’ β„•) := {x : S Γ— Finset I // x.2.card = r x.1}
def subset {r : S β†’ β„•} (x : Outcome (I := I) r) : Finset I := x.val.2
def state {r : S β†’ β„•} (x : Outcome (I := I) r) : S := x.val.1
def Marginal {r : S β†’ β„•} (p : Outcome (I := I) r β†’ ℝ) (i : I) : ℝ :=
βˆ‘ x, p x * (if i ∈ subset x then 1 else 0)
def Mass {r : S β†’ β„•} (p : Outcome (I := I) r β†’ ℝ) (Ο‰ : S) : ℝ :=
βˆ‘ x, p x * (if state x = Ο‰ then 1 else 0)
def Feasible {r : S β†’ β„•} (pβ‚€ p : Outcome (I := I) r β†’ ℝ) : Prop :=
(βˆ€ x, 0 ≀ p x) ∧ βˆ€ Ο‰, Mass p Ο‰ = Mass pβ‚€ Ο‰
omit [DecidableEq I] in
lemma mass_total {r : S β†’ β„•} (p : Outcome (I := I) r β†’ ℝ) :
(βˆ‘ Ο‰, Mass p Ο‰) = βˆ‘ x, p x := by
simp only [Mass]
rw [sum_comm]
apply sum_congr rfl
intro x _
simp
lemma marginal_total {r : S β†’ β„•} (p : Outcome (I := I) r β†’ ℝ) :
(βˆ‘ i, Marginal p i) = βˆ‘ Ο‰, Mass p Ο‰ * r Ο‰ := by
simp only [Marginal, Mass]
rw [sum_comm]
simp_rw [sum_mul]
conv_rhs => rw [sum_comm]
apply sum_congr rfl
intro x _
have hx : (subset x).card = r (state x) := x.property
simp only [← mul_sum]
have h : (βˆ‘ i : I, if i ∈ subset x then (1 : ℝ) else 0) = (subset x).card := by simp
rw [h, hx]
simp
omit [DecidableEq I] in
lemma feasible_mass {r : S β†’ β„•} {pβ‚€ p : Outcome (I := I) r β†’ ℝ}
(hp : Feasible pβ‚€ p) : (βˆ‘ x, p x) = βˆ‘ x, pβ‚€ x := by
rw [← mass_total p, ← mass_total pβ‚€]
exact sum_congr rfl fun Ο‰ _ => hp.2 Ο‰
lemma move_feasible {r : S β†’ β„•} {pβ‚€ p : Outcome (I := I) r β†’ ℝ} (hp : Feasible pβ‚€ p)
{x y : Outcome (I := I) r} (hxy : state x = state y)
{Ξ΅ : ℝ} (hΞ΅ : 0 ≀ Ξ΅) (hx : Ξ΅ ≀ p x) : Feasible pβ‚€ (move p x y Ξ΅) := by
refine ⟨move_nonneg hp.1 hΡ hx, ?_⟩