Download data/lean/OAI/Combinatorics/BooleanFunctions/Rounding.lean from introvoyz045/math: direct link, hf CLI and curl.
- Browser
- Download file 9.81 kB
-
https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Combinatorics/BooleanFunctions/Rounding.lean
- Command line
-
hf download hf://datasets/introvoyz045/math/data/lean/OAI/Combinatorics/BooleanFunctions/Rounding.lean
-
curl -L -o Rounding.lean https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Combinatorics/BooleanFunctions/Rounding.lean
9.81 kB
| import OAI.Combinatorics.BooleanFunctions.SoftEvent | |
| namespace OAI | |
| open scoped BigOperators Topology NNReal ENNReal | |
| open MeasureTheory ProbabilityTheory Filter Real | |
| noncomputable section | |
| attribute [local instance] Classical.propDecidable | |
| namespace SquareRootDegree | |
| namespace FiniteRounding | |
| /-- Real-valued finite probabilities; used only to select a deterministic finite table. -/ | |
| def productWeight {ΞΉ Ξ± : Type*} [Fintype ΞΉ] (p : ΞΉ β Ξ± β β) (x : ΞΉ β Ξ±) : β := | |
| β i, p i (x i) | |
| theorem sum_productWeight {ΞΉ Ξ± : Type*} [Fintype ΞΉ] [Fintype Ξ±] | |
| (p : ΞΉ β Ξ± β β) (hp : β i, β a, p i a = 1) : | |
| β x, productWeight p x = 1 := by | |
| classical | |
| simp only [productWeight] | |
| rw [β Fintype.prod_sum] | |
| simp [hp] | |
| theorem productWeight_nonneg {ΞΉ Ξ± : Type*} [Fintype ΞΉ] | |
| (p : ΞΉ β Ξ± β β) (hp : β i a, 0 β€ p i a) (x : ΞΉ β Ξ±) : | |
| 0 β€ productWeight p x := Finset.prod_nonneg (fun i _ => hp i (x i)) | |
| theorem weighted_product {ΞΉ Ξ± : Type*} [Fintype ΞΉ] [Fintype Ξ±] | |
| (p f : ΞΉ β Ξ± β β) : | |
| β x, productWeight p x * β i, f i (x i) = β i, β a, p i a * f i a := by | |
| classical | |
| simp only [productWeight, β Finset.prod_mul_distrib] | |
| exact (Fintype.prod_sum (fun i a => p i a * f i a)).symm | |
| theorem weighted_evaluation {ΞΉ Ξ± : Type*} [Fintype ΞΉ] [Fintype Ξ±] | |
| (p : ΞΉ β Ξ± β β) (hp : β i, β a, p i a = 1) (j : ΞΉ) (g : Ξ± β β) : | |
| β x, productWeight p x * g (x j) = β a, p j a * g a := by | |
| classical | |
| have h := weighted_product p (fun i a => if i = j then g a else 1) | |
| simpa [Finset.prod_ite_eq, hp] using h | |
| def bernoulliWeight (p : β) (b : Bool) : β := if b then p else 1-p | |
| @[simp] theorem bernoulli_sum (p : β) : β b, bernoulliWeight p b = 1 := by | |
| simp [bernoulliWeight] | |
| theorem bernoulli_nonneg {p : β} (hp : 0 β€ p) (hp' : p β€ 1) (b : Bool) : | |
| 0 β€ bernoulliWeight p b := by cases b <;> simp [bernoulliWeight] <;> linarith | |
| @[simp] theorem bernoulli_expect (p : β) : | |
| β b, bernoulliWeight p b * (if b then (1:β) else 0) = p := by | |
| simp [bernoulliWeight] | |
| /-- A distribution over entire leaf-test tables, not random bits added to the cube. -/ | |
| def leafWeight {Ξ± : Type*} [Fintype Ξ±] (p : Ξ± β β) (t : Ξ± β Bool) : β := | |
| productWeight (fun a => bernoulliWeight (p a)) t | |
| @[simp] theorem leafWeight_sum {Ξ± : Type*} [Fintype Ξ±] (p : Ξ± β β) : | |
| β t, leafWeight p t = 1 := sum_productWeight _ (fun _ => bernoulli_sum _) | |
| theorem leafWeight_nonneg {Ξ± : Type*} [Fintype Ξ±] (p : Ξ± β β) | |
| (hp : β a, 0 β€ p a) (hp' : β a, p a β€ 1) (t : Ξ± β Bool) : | |
| 0 β€ leafWeight p t := productWeight_nonneg _ (fun a => bernoulli_nonneg (hp a) (hp' a)) t | |
| @[simp] theorem leafWeight_eval {Ξ± : Type*} [Fintype Ξ±] (p : Ξ± β β) (a : Ξ±) : | |
| β t, leafWeight p t * (if t a then (1:β) else 0) = p a := by | |
| simp only [leafWeight] | |
| simpa [bernoulliWeight] using weighted_evaluation (fun x => bernoulliWeight (p x)) | |
| (fun _ => bernoulli_sum _) a (fun b => if b then (1 : β) else 0) | |
| theorem exists_ge_weighted_sum {Ξ± : Type*} [Fintype Ξ±] (p f : Ξ± β β) | |
| (hp : β a, 0 β€ p a) (hs : β a, p a = 1) (c : β) | |
| (hc : c < β a, p a * f a) : β a, c < f a := by | |
| by_contra h | |
| push Not at h | |
| have he : β a, p a * f a β€ c := by | |
| calc β a, p a * f a β€ β a, p a * c := by | |
| apply Finset.sum_le_sum; intro a _; exact mul_le_mul_of_nonneg_left (h a) (hp a) | |
| _ = c := by rw [β Finset.sum_mul, hs, one_mul] | |
| exact (not_lt_of_ge he) hc | |
| end FiniteRounding | |
| end SquareRootDegree | |
| namespace SquareRootDegree | |
| namespace TableRounding | |
| open FiniteRounding SoftEvent | |
| attribute [local instance] Classical.propDecidable | |
| variable {ΞΉ Ξ± Ξ² : Type*} [Fintype ΞΉ] [Fintype Ξ±] [Fintype Ξ²] | |
| def tableWeight (s w : ΞΉ β Ξ² β β) (t : (Ξ² β ΞΉ) Γ (ΞΉ β Ξ² β Bool)) : β := | |
| productWeight (fun b i => s i b) t.1 * productWeight (fun i => leafWeight (w i)) t.2 | |
| def bit (b : Bool) : β := if b then 1 else 0 | |
| def tableEvent (S : Ξ² β ΞΉ) (L : ΞΉ β Ξ² β Bool) (x : Ξ² Γ (ΞΉ β Ξ²)) : Prop := | |
| L (S x.1) (x.2 (S x.1)) = false β§ β i, i β S x.1 β L i (x.2 i) = true | |
| omit [Fintype Ξ²] in | |
| theorem event_indicator (S : Ξ² β ΞΉ) (L : ΞΉ β Ξ² β Bool) (x : Ξ² Γ (ΞΉ β Ξ²)) : | |
| (if tableEvent S L x then (1:β) else 0) = | |
| softWeight (fun i b => if S b = i then 1 else 0) (fun i b => bit (L i b)) x := by | |
| have hs : (β i, (if S x.1 = i then (1:β) else 0) * | |
| β j β Finset.univ.erase i, bit (L j (x.2 j))) = | |
| β j β Finset.univ.erase (S x.1), bit (L j (x.2 j)) := by simp | |
| unfold softWeight | |
| rw [hs] | |
| have hp : (β j, bit (L j (x.2 j))) = bit (L (S x.1) (x.2 (S x.1))) * | |
| β j β Finset.univ.erase (S x.1), bit (L j (x.2 j)) := | |
| (Finset.mul_prod_erase _ _ (Finset.mem_univ _)).symm | |
| rw [hp] | |
| by_cases h : β i, i β S x.1 β L i (x.2 i) = true | |
| Β· have he : (β j β Finset.univ.erase (S x.1), bit (L j (x.2 j))) = 1 := by | |
| apply Finset.prod_eq_one | |
| intro j hj | |
| simp [bit,h j (Finset.mem_erase.mp hj).1] | |
| rw [he] | |
| cases hb : L (S x.1) (x.2 (S x.1)) | |
| Β· simp only [tableEvent,hb,true_and,bit,Bool.false_eq_true,ite_false,zero_mul,sub_zero] | |
| rw [ite_eq_left h] | |
| Β· simp [tableEvent,hb,bit] | |
| Β· obtain β¨j,hj,hbβ© := by simpa only [not_forall,Classical.not_imp] using h | |
| have he : (β j β Finset.univ.erase (S x.1), bit (L j (x.2 j))) = 0 := by | |
| apply Finset.prod_eq_zero (Finset.mem_erase.mpr β¨hj,Finset.mem_univ _β©) | |
| cases ht : L j (x.2 j) <;> simp_all [bit] | |
| rw [he] | |
| simp [tableEvent,h] | |
| theorem tableWeight_nonneg (s w : ΞΉ β Ξ² β β) (hs : β i b, 0 β€ s i b) | |
| (hw : β i b, 0 β€ w i b) (hw' : β i b, w i b β€ 1) (t) : 0 β€ tableWeight s w t := by | |
| exact mul_nonneg (productWeight_nonneg _ (fun b i => hs i b) _) | |
| (productWeight_nonneg _ (fun i => leafWeight_nonneg _ (hw i) (hw' i)) _) | |
| theorem tableWeight_sum (s w : ΞΉ β Ξ² β β) (hs : β b, β i, s i b = 1) : | |
| β t, tableWeight s w t = 1 := by | |
| simp only [tableWeight,Fintype.sum_prod_type,β Finset.mul_sum, | |
| sum_productWeight _ (fun i => leafWeight_sum (w i)),mul_one] | |
| exact sum_productWeight _ hs | |
| theorem weighted_leaf_prod (w : ΞΉ β Ξ² β β) (x : ΞΉ β Ξ²) (I : Finset ΞΉ) : | |
| β L : ΞΉ β Ξ² β Bool, productWeight (fun i => leafWeight (w i)) L * | |
| β i β I, bit (L i (x i)) = β i β I, w i (x i) := by | |
| have h := weighted_product (fun i => leafWeight (w i)) | |
| (fun i (t : Ξ² β Bool) => if i β I then bit (t (x i)) else 1) | |
| have hi (i : ΞΉ) : (β t : Ξ² β Bool, leafWeight (w i) t * | |
| (if i β I then bit (t (x i)) else 1)) = if i β I then w i (x i) else 1 := by | |
| by_cases hi : i β I | |
| Β· simp only [hi,ite_true,bit] | |
| exact leafWeight_eval (w i) (x i) | |
| Β· simp [hi] | |
| simpa only [Finset.prod_ite_mem,Finset.inter_univ,Finset.univ_inter,hi] using h | |
| theorem weighted_indicator (s w : ΞΉ β Ξ² β β) (hs : β b, β i, s i b = 1) | |
| (x : Ξ² Γ (ΞΉ β Ξ²)) : | |
| β t, tableWeight s w t * (if tableEvent t.1 t.2 x then (1:β) else 0) = | |
| softWeight s w x := by | |
| -- First average the independent leaf-test tables. | |
| have hL (S : Ξ² β ΞΉ) : | |
| (β L : ΞΉ β Ξ² β Bool, productWeight (fun i => leafWeight (w i)) L * | |
| ((β i, (if S x.1 = i then (1:β) else 0) * | |
| β j β Finset.univ.erase i, bit (L j (x.2 j))) - β j, bit (L j (x.2 j)))) = | |
| (β i, (if S x.1 = i then (1:β) else 0) * β j β Finset.univ.erase i, w j (x.2 j)) - | |
| β j, w j (x.2 j) := by | |
| simp only [mul_sub,Finset.sum_sub_distrib,Finset.mul_sum] | |
| rw [Finset.sum_comm] | |
| simp_rw [mul_left_comm _ (if _ then _ else _),β Finset.mul_sum,weighted_leaf_prod] | |
| have hS (i : ΞΉ) : (β S : Ξ² β ΞΉ, productWeight (fun b i => s i b) S * | |
| (if S x.1 = i then (1:β) else 0)) = s i x.1 := by | |
| simpa using weighted_evaluation (fun b i => s i b) hs x.1 (fun j => if j = i then 1 else 0) | |
| simp only [tableWeight,Fintype.sum_prod_type] | |
| simp_rw [event_indicator,mul_assoc,β Finset.mul_sum] | |
| change (β S : Ξ² β ΞΉ, productWeight (fun b i => s i b) S * | |
| (β L : ΞΉ β Ξ² β Bool, productWeight (fun i => leafWeight (w i)) L * | |
| ((β i, (if S x.1 = i then (1:β) else 0) * | |
| β j β Finset.univ.erase i, bit (L j (x.2 j))) - β j, bit (L j (x.2 j))))) = _ | |
| simp_rw [hL,mul_sub,Finset.mul_sum,Finset.sum_sub_distrib] | |
| rw [Finset.sum_comm] | |
| simp_rw [β mul_assoc,β Finset.sum_mul,hS] | |
| rw [sum_productWeight _ hs,one_mul] | |
| rfl | |
| theorem weighted_deficit (s w : ΞΉ β Ξ² β β) (hs : β b, β i, s i b = 1) | |
| (F : Ξ± β Ξ²) (T : Ξ± β β) (b : β) : | |
| (β t, tableWeight s w t * deficit T b | |
| (fun x => if tableEvent t.1 t.2 (F x.1,fun i => F (x.2 i)) then 1 else 0)) = | |
| deficit T b (softWeight (fun i a => s i (F a)) (fun i a => w i (F a))) := by | |
| simp only [deficit,β average_const_mul,β average_sum] | |
| congr 1 | |
| funext x | |
| simp_rw [β mul_assoc,β Finset.sum_mul,weighted_indicator s w hs] | |
| rfl | |
| theorem exists_event_deficit (s w : ΞΉ β Ξ² β β) | |
| (hs : β b, β i, s i b = 1) (hs0 : β i b, 0 β€ s i b) | |
| (hw0 : β i b, 0 β€ w i b) (hw1 : β i b, w i b β€ 1) | |
| (F : Ξ± β Ξ²) (T : Ξ± β β) (b c : β) | |
| (hc : c < deficit T b (softWeight (fun i a => s i (F a)) (fun i a => w i (F a)))) : | |
| β S : Ξ² β ΞΉ, β L : ΞΉ β Ξ² β Bool, | |
| c < deficit T b (fun x => if tableEvent S L (F x.1,fun i => F (x.2 i)) then 1 else 0) := by | |
| rw [β weighted_deficit s w hs F T b] at hc | |
| obtain β¨t,htβ© := exists_ge_weighted_sum (tableWeight s w) _ | |
| (tableWeight_nonneg s w hs0 hw0 hw1) (tableWeight_sum s w hs) c hc | |
| exact β¨t.1,t.2,htβ© | |
| end TableRounding | |
| end SquareRootDegree | |
| end | |
| end OAI | |