Download data/lean/OAI/Probability/Contingency/LawComposition.lean from introvoyz045/math: direct link, hf CLI and curl.
- Browser
- Download file 25 kB
-
https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Probability/Contingency/LawComposition.lean
- Command line
-
hf download hf://datasets/introvoyz045/math/data/lean/OAI/Probability/Contingency/LawComposition.lean
-
curl -L -o LawComposition.lean https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Probability/Contingency/LawComposition.lean
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 | |