Download data/lean/OAI/Combinatorics/ContingencyTables/Model.lean from introvoyz045/math: direct link, hf CLI and curl.
- Browser
- Download file 5.93 kB
-
https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Combinatorics/ContingencyTables/Model.lean
- Command line
-
hf download hf://datasets/introvoyz045/math/data/lean/OAI/Combinatorics/ContingencyTables/Model.lean
-
curl -L -o Model.lean https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Combinatorics/ContingencyTables/Model.lean
5.93 kB
| import Mathlib.Data.Fintype.BigOperators | |
| import Mathlib.Algebra.Order.BigOperators.Group.Finset | |
| import Mathlib.Data.Finset.Pi | |
| import Lean.Elab.Tactic.Omega | |
| namespace OAI | |
| /-! | |
| # Contingency tables with arbitrary margins | |
| The feasible sets in result 200 are represented by natural-valued | |
| matrices, without a positivity, balance, or sparsity restriction on | |
| the margins. The finite encoding bound is proved rather than assumed. | |
| -/ | |
| namespace ContingencyTables | |
| open Finset | |
| variable {I J : Type*} [Fintype I] [Fintype J] | |
| /-- The row and column equations in both papers. -/ | |
| def HasMargins (X : I β J β β) (r : I β β) (c : J β β) : Prop := | |
| (β i, β j, X i j = r i) β§ (β j, β i, X i j = c j) | |
| /-- Unrestricted nonnegative integer tables with the specified margins. -/ | |
| def Table (r : I β β) (c : J β β) := {X : I β J β β // HasMargins X r c} | |
| /-- The entry constraints for the companion paper, including structural zeros. -/ | |
| def BoundedTable (r : I β β) (c : J β β) (b : I β J β β) := | |
| {X : Table r c // β i j, X.val i j β€ b i j} | |
| theorem totals_equal {X : I β J β β} {r : I β β} {c : J β β} | |
| (h : HasMargins X r c) : β i, r i = β j, c j := by | |
| simp_rw [β h.1, β h.2] | |
| exact sum_comm | |
| theorem entry_le_row {X : I β J β β} {r : I β β} {c : J β β} | |
| (h : HasMargins X r c) (i : I) (j : J) : X i j β€ r i := by | |
| rw [β h.1 i] | |
| exact single_le_sum (fun _ _ => Nat.zero_le _) (mem_univ j) | |
| theorem entry_le_total {X : I β J β β} {r : I β β} {c : J β β} | |
| (h : HasMargins X r c) (i : I) (j : J) : X i j β€ β i, r i := | |
| (entry_le_row h i j).trans (single_le_sum (fun _ _ => Nat.zero_le _) (mem_univ i)) | |
| /-- Binary margins may be large, but the feasible set is finite. -/ | |
| instance table_finite (r : I β β) (c : J β β) : Finite (Table r c) := by | |
| let encode : Table r c β I β J β Fin ((β i, r i) + 1) := | |
| fun X i j => β¨X.val i j, Nat.lt_succ_of_le (entry_le_total X.property i j)β© | |
| apply Finite.of_injective encode | |
| intro X Y h | |
| apply Subtype.ext | |
| funext i j | |
| exact congrArg Fin.val (congrFun (congrFun h i) j) | |
| noncomputable instance table_fintype (r : I β β) (c : J β β) : Fintype (Table r c) := | |
| Fintype.ofFinite _ | |
| noncomputable instance boundedTable_fintype (r : I β β) (c : J β β) (b : I β J β β) : | |
| Fintype (BoundedTable r c b) := by | |
| classical | |
| unfold BoundedTable | |
| infer_instance | |
| /-- The bound used in the exact correction's exhaustive branch. -/ | |
| theorem table_card_le (r : I β β) (c : J β β) : | |
| Fintype.card (Table r c) β€ ((β i, r i) + 1) ^ (Fintype.card I * Fintype.card J) := by | |
| classical | |
| let encode : Table r c β I β J β Fin ((β i, r i) + 1) := | |
| fun X i j => β¨X.val i j, Nat.lt_succ_of_le (entry_le_total X.property i j)β© | |
| have hinj : Function.Injective encode := by | |
| intro X Y h | |
| apply Subtype.ext | |
| funext i j | |
| exact congrArg Fin.val (congrFun (congrFun h i) j) | |
| have h := Fintype.card_le_of_injective encode hinj | |
| simpa only [Fintype.card_fun, Fintype.card_fin, β pow_mul, Nat.mul_comm] using h | |
| section Existence | |
| variable [DecidableEq I] [DecidableEq J] | |
| private theorem sum_decrease_one (r : I β β) (i : I) (hi : 0 < r i) : | |
| (β k, Function.update r i (r i - 1) k) + 1 = β k, r k := by | |
| rw [sum_update_of_mem (mem_univ i)] | |
| have hsum := sum_erase_add (univ : Finset I) r (mem_univ i) | |
| simp only [sdiff_singleton_eq_erase] at * | |
| omega | |
| /-- Equal total nonnegative margins always admit an unrestricted table. | |
| The proof removes one unit at a positive row and a positive column. | |
| It is an existence argument; no polynomial runtime is asserted here. -/ | |
| theorem exists_table (r : I β β) (c : J β β) (htotal : β i, r i = β j, c j) : | |
| β X, HasMargins X r c := by | |
| suffices β N (r : I β β) (c : J β β), | |
| (β i, r i) = N β (β j, c j) = N β β X, HasMargins X r c by | |
| exact this _ r c rfl htotal.symm | |
| intro N | |
| induction N with | |
| | zero => | |
| intro r c hr hc | |
| refine β¨fun _ _ => 0, ?_, ?_β© | |
| Β· intro i | |
| have hri := single_le_sum (f := r) (fun _ _ => Nat.zero_le _) (mem_univ i) | |
| rw [hr] at hri | |
| simpa using (Nat.eq_zero_of_le_zero hri).symm | |
| Β· intro j | |
| have hcj := single_le_sum (f := c) (fun _ _ => Nat.zero_le _) (mem_univ j) | |
| rw [hc] at hcj | |
| simpa using (Nat.eq_zero_of_le_zero hcj).symm | |
| | succ N ih => | |
| intro r c hr hc | |
| obtain β¨iβ, _, hiββ© := exists_ne_zero_of_sum_ne_zero | |
| (s := (univ : Finset I)) (f := r) (by rw [hr]; exact Nat.succ_ne_zero _) | |
| obtain β¨jβ, _, hjββ© := exists_ne_zero_of_sum_ne_zero | |
| (s := (univ : Finset J)) (f := c) (by rw [hc]; exact Nat.succ_ne_zero _) | |
| let r' := Function.update r iβ (r iβ - 1) | |
| let c' := Function.update c jβ (c jβ - 1) | |
| have hr' : β i, r' i = N := by | |
| have h := sum_decrease_one r iβ (Nat.pos_of_ne_zero hiβ) | |
| dsimp [r'] | |
| omega | |
| have hc' : β j, c' j = N := by | |
| have h := sum_decrease_one c jβ (Nat.pos_of_ne_zero hjβ) | |
| dsimp [c'] | |
| omega | |
| obtain β¨X, hXr, hXcβ© := ih r' c' hr' hc' | |
| refine β¨fun i j => X i j + if i = iβ β§ j = jβ then 1 else 0, ?_, ?_β© | |
| Β· intro i | |
| rw [sum_add_distrib, hXr i] | |
| by_cases hi : i = iβ | |
| Β· subst i | |
| simp only [true_and, sum_ite_eq', mem_univ, ite_true, r', Function.update_self] | |
| omega | |
| Β· simp [hi, r'] | |
| Β· intro j | |
| rw [sum_add_distrib, hXc j] | |
| by_cases hj : j = jβ | |
| Β· subst j | |
| simp only [and_true, sum_ite_eq', mem_univ, ite_true, c', Function.update_self] | |
| omega | |
| Β· simp [hj, c'] | |
| theorem table_nonempty (r : I β β) (c : J β β) (htotal : β i, r i = β j, c j) : | |
| Nonempty (Table r c) := by | |
| obtain β¨X, hXβ© := exists_table r c htotal | |
| exact β¨β¨X, hXβ©β© | |
| end Existence | |
| end ContingencyTables | |
| end OAI | |