introvoyz045's picture
Migrated from GitHub
4a56cd6 verified
Raw History Blame Contribute Delete
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