math / data /lean /OAI /Probability /Contingency /LawComposition.lean
introvoyz045's picture
Migrated from GitHub
4a56cd6 verified
Raw History Blame Contribute Delete
25 kB
import Mathlib
import OAI.Probability.Contingency.DyadicLaws
namespace OAI
/-! Law Composition. -/
noncomputable section
open scoped BigOperators
open scoped BigOperators
namespace Contingency.Law
noncomputable section
variable {Ξ± Ξ² Ξ³ : Type*} [Fintype Ξ±] [Fintype Ξ²] [Fintype Ξ³]
theorem pure_bind (x : Ξ±) (f : Ξ± β†’ Law Ξ²) : (pure x).bind f=f x := by
classical
apply Law.ext; funext y
simp [bind,pure]
theorem bind_assoc (p : Law Ξ±) (f : Ξ± β†’ Law Ξ²) (g : Ξ² β†’ Law Ξ³) :
(p.bind f).bind g=p.bind (fun x => (f x).bind g) := by
apply Law.ext; funext y
simp only [bind,Finset.sum_mul,Finset.mul_sum,mul_assoc]
exact Finset.sum_comm
theorem map_bind (p : Law Ξ±) (f : Ξ± β†’ Ξ²) (g : Ξ² β†’ Law Ξ³) :
(p.map f).bind g=p.bind (fun x => g (f x)) := by
simp only [map,bind_assoc,pure_bind]
theorem map_map (p : Law Ξ±) (f : Ξ± β†’ Ξ²) (g : Ξ² β†’ Ξ³) :
(p.map f).map g=p.map (fun x => g (f x)) := by
change (p.map f).bind _=_
rw [map_bind]
rfl
end
end Contingency.Law
open scoped BigOperators
open Filter
namespace Contingency
def Tables {m n : β„•} (r : Fin m β†’ β„•) (c : Fin n β†’ β„•) :=
{X : Fin m β†’ Fin n β†’ β„• //
(βˆ€ i, βˆ‘ j, X i j = r i) ∧ (βˆ€ j, βˆ‘ i, X i j = c j)}
instance tablesFinite {m n : β„•} (r : Fin m β†’ β„•) (c : Fin n β†’ β„•) :
Finite (Tables r c) := by
let f : Tables r c β†’ (i : Fin m) β†’ Fin n β†’ Fin (r i + 1) :=
fun X i j => ⟨X.val i j, by
have := Finset.single_le_sum (fun (j : Fin n) _ => Nat.zero_le (X.val i j))
(Finset.mem_univ j)
rw [X.property.1 i] at this
omega⟩
apply Finite.of_injective f
intro X Y h
apply Subtype.ext
funext i j
exact congrArg Fin.val (congrFun (congrFun h i) j)
def encodeNat (x : β„•) : List Bool :=
List.replicate x.bits.length true ++ false :: x.bits
def encodeList (xs : List β„•) : List Bool := xs.flatMap encodeNat
def inputBits {m n : β„•} (r : Fin m β†’ β„•) (c : Fin n β†’ β„•) (k : β„•) : List Bool :=
encodeList ([m, n, k] ++ List.ofFn r ++ List.ofFn c)
def outputBits {m n : β„•} {r : Fin m β†’ β„•} {c : Fin n β†’ β„•}
(X : Tables r c) : List Bool :=
encodeList ((List.ofFn fun i => List.ofFn (X.val i)).flatten)
namespace BitModel
inductive Move | left | stay | right
deriving DecidableEq
structure Tape where
left : List (Option Bool)
head : Option Bool
right : List (Option Bool)
deriving DecidableEq
def Tape.blank : Tape := ⟨[], none, []⟩
def Tape.ofInput (xs : List Bool) : Tape :=
match xs with
| [] => .blank
| x :: xs => ⟨[], some x, xs.map some⟩
def Tape.move (d : Move) (a : Option Bool) (t : Tape) : Tape :=
match d with
| .stay => ⟨t.left, a, t.right⟩
| .left => match t.left with
| [] => ⟨[], none, a :: t.right⟩
| x :: xs => ⟨xs, x, a :: t.right⟩
| .right => match t.right with
| [] => ⟨a :: t.left, none, []⟩
| x :: xs => ⟨a :: t.left, x, xs⟩
structure Instruction (states tapes : β„•) where
next : Option (Fin states)
write : Fin tapes β†’ Option Bool
move : Fin tapes β†’ Move
emit : Option Bool
structure Machine where
states : β„•
tapes : β„•
start : Fin states
inputTape : Fin tapes
transition : Fin states β†’ Bool β†’ (Fin tapes β†’ Option Bool) β†’ Instruction states tapes
structure Config (M : Machine) where
state : Option (Fin M.states)
tape : Fin M.tapes β†’ Tape
outputRev : List Bool
def Machine.initial (M : Machine) (xs : List Bool) : Config M :=
⟨some M.start, fun i => if i = M.inputTape then .ofInput xs else .blank, []⟩
def Machine.step (M : Machine) (coin : Bool) (c : Config M) : Config M :=
match c.state with
| none => c
| some s =>
let a := M.transition s coin (fun i => (c.tape i).head)
⟨a.next, fun i => (c.tape i).move (a.move i) (a.write i),
match a.emit with | none => c.outputRev | some b => b :: c.outputRev⟩
def Machine.run (M : Machine) (xs : List Bool) {t : β„•} (coins : Fin t β†’ Bool) : Config M :=
(List.ofFn coins).foldl (fun c b => M.step b c) (M.initial xs)
noncomputable def Machine.mass (M : Machine) (xs out : List Bool) (t : β„•) : ℝ :=
((Finset.univ.filter fun coins : Fin t β†’ Bool =>
(M.run xs coins).state = none ∧ (M.run xs coins).outputRev.reverse = out).card : ℝ) /
2 ^ t
noncomputable def Machine.tail (M : Machine) (xs : List Bool) (t : β„•) : ℝ :=
((Finset.univ.filter fun coins : Fin t β†’ Bool =>
(M.run xs coins).state β‰  none).card : ℝ) / 2 ^ t
end BitModel
def inputSize (m n N : β„•) : β„• := m + n + Nat.log2 (N + 1) + 1
noncomputable def variationBy {m n : β„•} (r : Fin m β†’ β„•) (c : Fin n β†’ β„•)
(M : BitModel.Machine) (k t : β„•) : ℝ := by
letI := Fintype.ofFinite (Tables r c)
exact (βˆ‘ X : Tables r c,
|M.mass (inputBits r c k) (outputBits X) t - 1 / Fintype.card (Tables r c)|) / 2
def BoundedMain : Prop :=
βˆƒ (M : BitModel.Machine) (A q : β„•),
βˆ€ (m n : β„•) (r : Fin m β†’ β„•) (c : Fin n β†’ β„•),
(βˆ‘ i, r i) = (βˆ‘ j, c j) β†’ βˆ€ k : β„•, 1 ≀ k β†’
βˆƒ t : β„•, t ≀ A * (inputSize m n (βˆ‘ i, r i) + k) ^ q ∧
(βˆ€ coins : Fin t β†’ Bool,
(M.run (inputBits r c k) coins).state = none ∧
βˆƒ X : Tables r c,
(M.run (inputBits r c k) coins).outputRev.reverse = outputBits X) ∧
variationBy r c M k t ≀ (2 : ℝ) ^ (-(k : β„€))
def ExactMain : Prop :=
βˆƒ (M : BitModel.Machine) (A q : β„•),
βˆ€ (m n : β„•) (r : Fin m β†’ β„•) (c : Fin n β†’ β„•),
(βˆ‘ i, r i) = (βˆ‘ j, c j) β†’
(βˆ€ (t : β„•) (coins : Fin t β†’ Bool),
(M.run (inputBits r c 0) coins).state = none β†’ βˆƒ X : Tables r c,
(M.run (inputBits r c 0) coins).outputRev.reverse = outputBits X) ∧
Tendsto (M.tail (inputBits r c 0)) atTop (nhds 0) ∧
(βˆ€ X : Tables r c, Tendsto (M.mass (inputBits r c 0) (outputBits X))
atTop (nhds (1 / (Nat.card (Tables r c) : ℝ)))) ∧
Summable (M.tail (inputBits r c 0)) ∧
(βˆ‘' t, M.tail (inputBits r c 0) t) ≀
(A * (inputSize m n (βˆ‘ i, r i)) ^ q : β„•)
def MainTheorem : Prop := BoundedMain ∧ ExactMain
end Contingency
open scoped BigOperators
namespace Contingency
theorem tables_nonempty {m n : β„•} (r : Fin m β†’ β„•) (c : Fin n β†’ β„•)
(hrc : (βˆ‘ i, r i) = βˆ‘ j, c j) : Nonempty (Tables r c) := by
classical
let e : (Ξ£ i, Fin (r i)) ≃ (Ξ£ j, Fin (c j)) :=
Fintype.equivOfCardEq (by simpa [Fintype.card_sigma] using hrc)
let X (i : Fin m) (j : Fin n) : β„• := βˆ‘ z : (Ξ£ i, Fin (r i)),
(if z.1=i then 1 else 0)*(if (e z).1=j then 1 else 0)
refine ⟨⟨X,?_,?_⟩⟩
Β· intro i
change (βˆ‘ j, βˆ‘ z : (Ξ£ i, Fin (r i)),
(if z.1=i then 1 else 0)*(if (e z).1=j then 1 else 0)) = r i
rw [Finset.sum_comm]
simp only [← Finset.mul_sum]
have hz (z : Ξ£ i, Fin (r i)) : (βˆ‘ j, if (e z).1=j then (1:β„•) else 0)=1 := by simp
simp only [hz,mul_one]
rw [Fintype.sum_sigma]
change (βˆ‘ a, βˆ‘ _t : Fin (r a), if a=i then (1:β„•) else 0)=r i
simp only [Finset.sum_const,Finset.card_univ,Fintype.card_fin,nsmul_eq_mul,mul_ite,mul_one,mul_zero]
simp
Β· intro j
change (βˆ‘ i, βˆ‘ z : (Ξ£ i, Fin (r i)),
(if z.1=i then 1 else 0)*(if (e z).1=j then 1 else 0)) = c j
rw [Finset.sum_comm]
simp only [← Finset.sum_mul]
have hz (z : Ξ£ i, Fin (r i)) : (βˆ‘ i, if z.1=i then (1:β„•) else 0)=1 := by simp
simp only [hz,one_mul]
calc
_ = βˆ‘ z : (Ξ£ j, Fin (c j)), if z.1=j then (1:β„•) else 0 :=
e.sum_comp (fun z : (Ξ£ j, Fin (c j)) => if z.1=j then (1:β„•) else 0)
_ = _ := by
rw [Fintype.sum_sigma]
change (βˆ‘ a, βˆ‘ _t : Fin (c a), if a=j then (1:β„•) else 0)=c j
simp only [Finset.sum_const,Finset.card_univ,Fintype.card_fin,nsmul_eq_mul,mul_ite,mul_one,mul_zero]
simp
end Contingency
open scoped BigOperators
namespace Contingency
noncomputable section
lemma sum_dite_subtype {Ξ± M : Type*} [Fintype Ξ±] [AddCommMonoid M]
(p : Ξ± β†’ Prop) [DecidablePred p] (f : {x // p x} β†’ M) :
(βˆ‘ x, if h : p x then f ⟨x,h⟩ else 0)=βˆ‘ x,f x := by
symm
apply Fintype.sum_of_injective Subtype.val Subtype.val_injective
Β· intro i hi
split_ifs with h
· exact (hi ⟨⟨i,h⟩,rfl⟩).elim
Β· rfl
Β· intro i; simp [i.property]
variable {m n a b : β„•} {r : Fin m β†’ β„•} {c : Fin n β†’ β„•}
theorem table_cell_le_row (X : Tables r c) (i : Fin m) (j : Fin n) : X.val i j≀r i := by
rw [← X.property.1 i]
exact Finset.single_le_sum (fun j _ => Nat.zero_le (X.val i j)) (Finset.mem_univ j)
theorem table_cell_le_col (X : Tables r c) (i : Fin m) (j : Fin n) : X.val i j≀c j := by
rw [← X.property.2 j]
exact Finset.single_le_sum (fun i _ => Nat.zero_le (X.val i j)) (Finset.mem_univ i)
variable (p : Fin m β†’ Prop) (q : Fin n β†’ Prop) [DecidablePred p] [DecidablePred q]
(er : Fin a ≃ {i // p i}) (ec : Fin b ≃ {j // q j})
(hr : βˆ€ i,Β¬p i β†’ r i=0) (hc : βˆ€ j,Β¬q j β†’ c j=0)
def restrictTable (X : Tables r c) :
Tables (fun i => r (er i).val) (fun j => c (ec j).val) := by
refine ⟨fun i j => X.val (er i).val (ec j).val,?_,?_⟩
Β· intro i
rw [ec.sum_comp (fun j => X.val (er i).val j.val)]
calc
_ = βˆ‘ j,X.val (er i).val j := by
apply Fintype.sum_of_injective Subtype.val Subtype.val_injective
Β· intro j hj
have hn : ¬q j := fun h => hj ⟨⟨j,h⟩,rfl⟩
have h := table_cell_le_col X (er i).val j
rw [hc j hn] at h
omega
Β· intro j; rfl
_ = _ := X.property.1 _
Β· intro j
rw [er.sum_comp (fun i => X.val i.val (ec j).val)]
calc
_ = βˆ‘ i,X.val i (ec j).val := by
apply Fintype.sum_of_injective Subtype.val Subtype.val_injective
Β· intro i hi
have hn : ¬p i := fun h => hi ⟨⟨i,h⟩,rfl⟩
have h := table_cell_le_row X i (ec j).val
rw [hr i hn] at h
omega
Β· intro i; rfl
_ = _ := X.property.2 _
def expandTable (X : Tables (fun i => r (er i).val) (fun j => c (ec j).val)) : Tables r c := by
refine ⟨fun i j => if hi : p i then if hj : q j then X.val (er.symm ⟨i,hi⟩) (ec.symm ⟨j,hj⟩) else 0 else 0,?_,?_⟩
Β· intro i
by_cases hi : p i
Β· simp only [dite_eq_left hi]
rw [sum_dite_subtype q (fun j => X.val (er.symm ⟨i,hi⟩) (ec.symm j))]
rw [← ec.sum_comp (fun j => X.val (er.symm ⟨i,hi⟩) (ec.symm j))]
simp only [Equiv.symm_apply_apply]
simp only [X.property.1,Equiv.apply_symm_apply]
Β· simp [hi,hr i hi]
Β· intro j
by_cases hj : q j
Β· simp only [dite_eq_left hj]
rw [sum_dite_subtype p (fun i => X.val (er.symm i) (ec.symm ⟨j,hj⟩))]
rw [← er.sum_comp (fun i => X.val (er.symm i) (ec.symm ⟨j,hj⟩))]
simp only [Equiv.symm_apply_apply]
simp only [X.property.2,Equiv.apply_symm_apply]
Β· simp [hj,hc j hj]
def restrictTableEquiv : Tables r c ≃ Tables (fun i => r (er i).val) (fun j => c (ec j).val) where
toFun := restrictTable p q er ec hr hc
invFun := expandTable p q er ec hr hc
left_inv X := by
apply Subtype.ext; funext i j
by_cases hi : p i
Β· by_cases hj : q j
Β· simp [expandTable,restrictTable,hi,hj]
Β· have hz : X.val i j=0 := by have h := table_cell_le_col X i j; rw [hc j hj] at h; omega
simp [expandTable,hi,hj,hz]
Β· have hz : X.val i j=0 := by have h := table_cell_le_row X i j; rw [hr i hi] at h; omega
simp [expandTable,hi,hz]
right_inv X := by
apply Subtype.ext; funext i j
simp [restrictTable,expandTable,(er i).property,(ec j).property]
end
end Contingency
open scoped BigOperators
namespace Contingency
noncomputable section
def maximizers {a : β„•} (R : Fin (a+1) β†’ β„•) : Finset (Fin (a+1)) :=
Finset.univ.filter (fun i => βˆ€ j,R j≀R i)
theorem maximizers_nonempty {a : β„•} (R : Fin (a+1) β†’ β„•) : (maximizers R).Nonempty := by
obtain ⟨i,_,hi⟩ := Finset.exists_max_image Finset.univ R Finset.univ_nonempty
exact ⟨i,by simpa [maximizers] using hi⟩
def maximumIndex {a : β„•} (R : Fin (a+1) β†’ β„•) : Fin (a+1) := (maximizers R).min' (maximizers_nonempty R)
theorem maximumIndex_max {a : β„•} (R : Fin (a+1) β†’ β„•) (i : Fin (a+1)) : R i≀R (maximumIndex R) := by
have hh := Finset.min'_mem (maximizers R) (maximizers_nonempty R)
exact (Finset.mem_filter.mp hh).2 i
theorem maximumIndex_first {a : β„•} (R : Fin (a+1) β†’ β„•) (i : Fin (a+1))
(hi : βˆ€ j,R j≀R i) : maximumIndex R ≀ i := Finset.min'_le _ i (by simp [maximizers,hi])
def marginOrder {a : β„•} (R : Fin (a+1) β†’ β„•) : Fin (a+1) ≃ Fin (a+1) :=
Equiv.swap (maximumIndex R) (Fin.last a)
theorem marginOrder_max {a : β„•} (R : Fin (a+1) β†’ β„•) (i : Fin (a+1)) :
R (marginOrder R i)≀R (marginOrder R (Fin.last a)) := by
rw [marginOrder,Equiv.swap_apply_right]
exact maximumIndex_max _ _
variable {m n a b : β„•} (r : Fin m β†’ β„•) (c : Fin n β†’ β„•) (er : Fin a ≃ Fin m) (ec : Fin b ≃ Fin n)
def permuteTable (X : Tables r c) : Tables (fun i => r (er i)) (fun j => c (ec j)) :=
⟨fun i j => X.val (er i) (ec j),fun row => (ec.sum_comp _).trans (X.property.1 (er row)),
fun column => (er.sum_comp _).trans (X.property.2 (ec column))⟩
def permuteTableEquiv : Tables r c ≃ Tables (fun i => r (er i)) (fun j => c (ec j)) where
toFun := permuteTable r c er ec
invFun X := ⟨fun i j => X.val (er.symm i) (ec.symm j),by
constructor
Β· intro i
rw [ec.symm.sum_comp (fun j => X.val (er.symm i) j),X.property.1]
simp
Β· intro j
rw [er.symm.sum_comp (fun i => X.val i (ec.symm j)),X.property.2]
simp⟩
left_inv X := by apply Subtype.ext; funext i j; simp [permuteTable]
right_inv X := by apply Subtype.ext; funext i j; simp [permuteTable]
namespace Law
variable {Ξ± Ξ² : Type*} [Fintype Ξ±] [Fintype Ξ²] [Nonempty Ξ±] [Nonempty Ξ²]
theorem uniform_map_equiv (e : Ξ± ≃ Ξ²) : (uniform Ξ±).map e=uniform Ξ² := by
classical
apply Law.ext; funext y
have he (x : Ξ±) : e x=y ↔ x=e.symm y := e.eq_symm_apply.symm
simp only [map,bind,pure,mul_ite,mul_one,mul_zero,he]
simp only [Finset.sum_ite_eq',Finset.mem_univ,ite_true,uniform,Fintype.card_congr e]
end Law
end
end Contingency
open scoped BigOperators
namespace Contingency.Dense
noncomputable section
variable {a c : β„•}
def complete (R : Fin (a+1) β†’ ℝ) (P : Fin (c+1) β†’ ℝ) (x : Fin a β†’ Fin c β†’ ℝ) :
Fin (a+1) β†’ Fin (c+1) β†’ ℝ :=
Fin.lastCases
(Fin.lastCases (R (Fin.last a)-(βˆ‘ j : Fin c,P j.castSucc)+(βˆ‘ i,βˆ‘ j,x i j))
(fun j => P j.castSucc-βˆ‘ i,x i j))
(fun i => Fin.lastCases (R i.castSucc-βˆ‘ j,x i j) (fun j => x i j))
@[simp] theorem complete_free (R : Fin (a+1) β†’ ℝ) (P : Fin (c+1) β†’ ℝ)
(x : Fin a β†’ Fin c β†’ ℝ) (i : Fin a) (j : Fin c) :
complete R P x i.castSucc j.castSucc=x i j := by simp [complete]
@[simp] theorem complete_right (R : Fin (a+1) β†’ ℝ) (P : Fin (c+1) β†’ ℝ)
(x : Fin a β†’ Fin c β†’ ℝ) (i : Fin a) :
complete R P x i.castSucc (Fin.last c)=R i.castSucc-βˆ‘ j,x i j := by simp [complete]
@[simp] theorem complete_bottom (R : Fin (a+1) β†’ ℝ) (P : Fin (c+1) β†’ ℝ)
(x : Fin a β†’ Fin c β†’ ℝ) (j : Fin c) :
complete R P x (Fin.last a) j.castSucc=P j.castSucc-βˆ‘ i,x i j := by simp [complete]
@[simp] theorem complete_corner (R : Fin (a+1) β†’ ℝ) (P : Fin (c+1) β†’ ℝ)
(x : Fin a β†’ Fin c β†’ ℝ) :
complete R P x (Fin.last a) (Fin.last c)=
R (Fin.last a)-(βˆ‘ j : Fin c,P j.castSucc)+(βˆ‘ i,βˆ‘ j,x i j) := by simp [complete]
@[simp] theorem complete_row (R : Fin (a+1) β†’ ℝ) (P : Fin (c+1) β†’ ℝ)
(x : Fin a β†’ Fin c β†’ ℝ) (i : Fin (a+1)) : βˆ‘ j,complete R P x i j=R i := by
refine Fin.lastCases ?_ (fun i => ?_) i
Β· rw [Fin.sum_univ_castSucc]
simp only [complete_bottom,complete_corner,Finset.sum_sub_distrib]
rw [Finset.sum_comm (f:=fun i j => x i j)]
ring
Β· rw [Fin.sum_univ_castSucc]
simp only [complete_free,complete_right]
ring
@[simp] theorem complete_col (R : Fin (a+1) β†’ ℝ) (P : Fin (c+1) β†’ ℝ)
(x : Fin a β†’ Fin c β†’ ℝ) (h : (βˆ‘ i,R i)=βˆ‘ j,P j) (j : Fin (c+1)) :
βˆ‘ i,complete R P x i j=P j := by
refine Fin.lastCases ?_ (fun j => ?_) j
Β· rw [Fin.sum_univ_castSucc]
simp only [complete_right,complete_corner,Finset.sum_sub_distrib]
rw [Fin.sum_univ_castSucc,Fin.sum_univ_castSucc] at h
linarith
Β· rw [Fin.sum_univ_castSucc]
simp only [complete_free,complete_bottom]
ring
theorem complete_represents (R : Fin (a+1) β†’ ℝ) (P : Fin (c+1) β†’ ℝ)
(X : Fin (a+1) β†’ Fin (c+1) β†’ ℝ)
(hR : βˆ€ i,βˆ‘ j,X i j=R i) (hP : βˆ€ j,βˆ‘ i,X i j=P j) :
complete R P (fun i j => X i.castSucc j.castSucc)=X := by
funext i j
refine Fin.lastCases ?_ (fun i => ?_) i
Β· refine Fin.lastCases ?_ (fun j => ?_) j
Β· have hr := hR (Fin.last a)
rw [Fin.sum_univ_castSucc] at hr
have hp (j : Fin c) := hP j.castSucc
simp only [Fin.sum_univ_castSucc] at hp
have he : (βˆ‘ j : Fin c,X (Fin.last a) j.castSucc)=
(βˆ‘ j : Fin c,P j.castSucc)-(βˆ‘ i : Fin a,βˆ‘ j : Fin c,X i.castSucc j.castSucc) := by
rw [Finset.sum_comm (f:=fun (i : Fin a) (j : Fin c) => X i.castSucc j.castSucc),← Finset.sum_sub_distrib]
apply Finset.sum_congr rfl; intro j _; linarith [hp j]
rw [he] at hr
rw [complete_corner]
linarith
Β· rw [complete_bottom]
have hp := hP j.castSucc
rw [Fin.sum_univ_castSucc] at hp
linarith
Β· refine Fin.lastCases ?_ (fun j => ?_) j
Β· rw [complete_right]
have hr := hR i.castSucc
rw [Fin.sum_univ_castSucc] at hr
linarith
Β· exact complete_free R P _ i j
def scale (R : Fin (a+1) β†’ ℝ) (P : Fin (c+1) β†’ ℝ) (i : Fin (a+1)) (j : Fin (c+1)) : ℝ :=
min (R i) (P j)
def scaled (R : Fin (a+1) β†’ ℝ) (P : Fin (c+1) β†’ ℝ)
(w : Fin a β†’ Fin c β†’ ℝ) (u : Fin aΓ—Fin c β†’ ℝ) :=
complete R P (fun i j => w i j*u (i,j))
def normalized (R : Fin (a+1) β†’ ℝ) (P : Fin (c+1) β†’ ℝ)
(w : Fin a β†’ Fin c β†’ ℝ) (u : Fin aΓ—Fin c β†’ ℝ) (i : Fin (a+1)) (j : Fin (c+1)) : ℝ :=
scaled R P w u i j/scale R P i j
def polytope (R : Fin (a+1) β†’ ℝ) (P : Fin (c+1) β†’ ℝ)
(w : Fin a β†’ Fin c β†’ ℝ) : Set (Fin aΓ—Fin c β†’ ℝ) :=
{u | βˆ€ i j, 0 ≀ normalized R P w u i j}
theorem width_bounds (s B : β„•) (hB : 0<B) (h : 2*B ≀ s) :
(s:ℝ)/(2*B) ≀ ((s/B:β„•):ℝ) ∧ ((s/B:β„•):ℝ) ≀ (s:ℝ)/B := by
have hq : 1 ≀ s/B := Nat.div_pos (by omega) hB
have hu := Nat.div_mul_le_self s B
have hl := Nat.lt_mul_div_succ s hB
have hb : (0:ℝ)<B := by exact_mod_cast hB
constructor
Β· apply (div_le_iffβ‚€ (by positivity : (0:ℝ)<2*B)).mpr
have hi : s ≀ (s/B)*(2*B) := by nlinarith only [hq,hl]
exact_mod_cast hi
Β· apply (le_div_iffβ‚€ hb).mpr
exact_mod_cast hu
theorem entry_le_scale (R : Fin (a+1) β†’ ℝ) (P : Fin (c+1) β†’ ℝ)
(X : Fin (a+1) β†’ Fin (c+1) β†’ ℝ) (hX : βˆ€ i j,0 ≀ X i j)
(hR : βˆ€ i,βˆ‘ j,X i j=R i) (hP : βˆ€ j,βˆ‘ i,X i j=P j) (i j) : X i j ≀ scale R P i j := by
apply le_min
Β· simpa only [hR] using Finset.single_le_sum (fun j _ => hX i j) (Finset.mem_univ j)
Β· simpa only [hP] using Finset.single_le_sum (fun i _ => hX i j) (Finset.mem_univ i)
theorem polytope_bounded (R : Fin (a+1) β†’ ℝ) (P : Fin (c+1) β†’ ℝ)
(w : Fin a β†’ Fin c β†’ ℝ) (B : ℝ) (hB : 0<B)
(hR : βˆ€ i,0<R i) (hP : βˆ€ j,0<P j) (heq : (βˆ‘ i,R i)=βˆ‘ j,P j)
(hw : βˆ€ i j,scale R P i.castSucc j.castSucc/(2*B) ≀ w i j)
(u : Fin aΓ—Fin c β†’ ℝ) (hu : u∈polytope R P w) :
βˆ€ s,0 ≀ u s ∧ u s ≀ 2*B := by
have hs (i j) : 0<scale R P i j := lt_min (hR i) (hP j)
have hpos (i j) : 0<w i j := (div_pos (hs i.castSucc j.castSucc) (by positivity)).trans_le (hw i j)
have hX (i j) : 0 ≀ scaled R P w u i j := by
have h := hu i j
change 0 ≀ scaled R P w u i j/scale R P i j at h
simpa only [zero_mul] using (le_div_iffβ‚€ (hs i j)).mp h
rintro ⟨i,j⟩
have he : scaled R P w u i.castSucc j.castSucc=w i j*u (i,j) := complete_free R P _ i j
have h0 := hX i.castSucc j.castSucc
rw [he] at h0
have hl := entry_le_scale R P (scaled R P w u) hX
(fun i => complete_row R P _ i) (fun j => complete_col R P _ heq j) i.castSucc j.castSucc
rw [he] at hl
constructor
Β· exact nonneg_of_mul_nonneg_right h0 (hpos i j)
Β· have hw' : scale R P i.castSucc j.castSucc ≀ w i j*(2*B) :=
(div_le_iffβ‚€ (by positivity)).mp (hw i j)
exact (mul_le_mul_iff_rightβ‚€ (hpos i j)).mp (hl.trans hw')
end
end Contingency.Dense
open scoped BigOperators
namespace Contingency.Dense
noncomputable section
variable {a c : β„•}
theorem real_completion {I J : Type*} [Fintype I] [Fintype J]
(R : I β†’ ℝ) (P : J β†’ ℝ) (hR : βˆ€ i,0 ≀ R i) (hP : βˆ€ j,0 ≀ P j)
(heq : (βˆ‘ i,R i)=βˆ‘ j,P j) :
βˆƒ X : I β†’ J β†’ ℝ, (βˆ€ i j,0 ≀ X i j) ∧ (βˆ€ i,βˆ‘ j,X i j=R i) ∧ (βˆ€ j,βˆ‘ i,X i j=P j) := by
classical
by_cases h0 : (βˆ‘ i,R i)=0
Β· have hR0 (i) : R i=0 := by
have := Finset.single_le_sum (fun i _ => hR i) (Finset.mem_univ i)
rw [h0] at this; linarith [hR i]
have hP0 (j) : P j=0 := by
have := Finset.single_le_sum (fun j _ => hP j) (Finset.mem_univ j)
rw [← heq,h0] at this; linarith [hP j]
refine ⟨fun _ _ => 0,fun _ _ => le_rfl,?_,?_⟩
Β· intro i; simp [hR0]
Β· intro j; simp [hP0]
Β· have hS : 0<(βˆ‘ i,R i) := lt_of_le_of_ne (Finset.sum_nonneg (fun i _ => hR i)) (Ne.symm h0)
refine ⟨fun i j => R i*P j/(βˆ‘ i,R i),?_,?_,?_⟩
Β· intro i j; exact div_nonneg (mul_nonneg (hR i) (hP j)) hS.le
Β· intro i
rw [← Finset.sum_div,← Finset.mul_sum,← heq]
field_simp
Β· intro j
rw [← Finset.sum_div,← Finset.sum_mul]
field_simp
theorem interior_table (d : ℝ) (hd : 0<d)
(R : Fin (a+1) β†’ ℝ) (P : Fin (c+1) β†’ ℝ)
(hR : βˆ€ i,0<R i) (hP : βˆ€ j,0<P j) (heq : (βˆ‘ i,R i)=βˆ‘ j,P j)
(ha : (a+1:β„•) ≀ d) (hc : (c+1:β„•) ≀ d) :
βˆƒ X : Fin (a+1) β†’ Fin (c+1) β†’ ℝ,
(βˆ€ i j,scale R P i j/(2*d) ≀ X i j) ∧
(βˆ€ i,βˆ‘ j,X i j=R i) ∧ (βˆ€ j,βˆ‘ i,X i j=P j) := by
let B := fun i j => scale R P i j/(2*d)
have hBn (i j) : 0 ≀ B i j := div_nonneg (le_min (hR i).le (hP j).le) (by positivity)
have hrow (i) : (βˆ‘ j,B i j) ≀ R i/2 := by
calc
_ ≀ βˆ‘ j : Fin (c+1), R i/(2*d) := by
apply Finset.sum_le_sum; intro j _
exact div_le_div_of_nonneg_right (min_le_left _ _) (by positivity)
_ = (c+1:β„•)*(R i/(2*d)) := by simp
_ ≀ d*(R i/(2*d)) := mul_le_mul_of_nonneg_right hc (div_nonneg (hR i).le (by positivity))
_ = R i/2 := by field_simp
have hcol (j) : (βˆ‘ i,B i j) ≀ P j/2 := by
calc
_ ≀ βˆ‘ i : Fin (a+1), P j/(2*d) := by
apply Finset.sum_le_sum; intro i _
exact div_le_div_of_nonneg_right (min_le_right _ _) (by positivity)
_ = (a+1:β„•)*(P j/(2*d)) := by simp
_ ≀ d*(P j/(2*d)) := mul_le_mul_of_nonneg_right ha (div_nonneg (hP j).le (by positivity))
_ = P j/2 := by field_simp
let R' := fun i => R i-βˆ‘ j,B i j
let P' := fun j => P j-βˆ‘ i,B i j
have hR' (i) : 0 ≀ R' i := by dsimp [R']; linarith [hrow i,hR i]
have hP' (j) : 0 ≀ P' j := by dsimp [P']; linarith [hcol j,hP j]
have heq' : (βˆ‘ i,R' i)=βˆ‘ j,P' j := by
simp only [R',P',Finset.sum_sub_distrib,heq]
rw [Finset.sum_comm]
obtain ⟨Y,hY,hYR,hYP⟩ := real_completion R' P' hR' hP' heq'
refine ⟨fun i j => B i j+Y i j,?_,?_,?_⟩
Β· intro i j; exact le_add_of_nonneg_right (hY i j)
Β· intro i; rw [Finset.sum_add_distrib,hYR]; dsimp [R']; ring
Β· intro j; rw [Finset.sum_add_distrib,hYP]; dsimp [P']; ring
theorem interior_point (d : ℝ) (hd : 0<d)
(R : Fin (a+1) β†’ ℝ) (P : Fin (c+1) β†’ ℝ) (w : Fin a β†’ Fin c β†’ ℝ)
(hR : βˆ€ i,0<R i) (hP : βˆ€ j,0<P j) (heq : (βˆ‘ i,R i)=βˆ‘ j,P j)
(ha : (a+1:β„•) ≀ d) (hc : (c+1:β„•) ≀ d) (hw : βˆ€ i j,0<w i j) :
βˆƒ u : Fin aΓ—Fin c β†’ ℝ, βˆ€ i j, 1/(2*d) ≀ normalized R P w u i j := by
obtain ⟨X,hX,hXR,hXP⟩ := interior_table d hd R P hR hP heq ha hc
let u := fun s : Fin aΓ—Fin c => X s.1.castSucc s.2.castSucc/w s.1 s.2
have he : scaled R P w u=X := by
unfold scaled
have hx : (fun i j => w i j*u (i,j))=fun i j => X i.castSucc j.castSucc := by
funext i j
dsimp [u]
field_simp [(hw i j).ne']
rw [hx]
exact complete_represents R P X hXR hXP
refine ⟨u,fun i j => ?_⟩
unfold normalized
rw [he]
have hs : 0<scale R P i j := lt_min (hR i) (hP j)
apply (le_div_iffβ‚€ hs).mpr
calc
_ = scale R P i j/(2*d) := by ring
_ ≀ _ := hX i j
end
end Contingency.Dense
end
end OAI