Download data/lean/OAI/Combinatorics/ContingencyTables/Counting/CompanionBinPopulation.lean from introvoyz045/math: direct link, hf CLI and curl.
- Browser
- Download file 5.92 kB
-
https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Combinatorics/ContingencyTables/Counting/CompanionBinPopulation.lean
- Command line
-
hf download hf://datasets/introvoyz045/math/data/lean/OAI/Combinatorics/ContingencyTables/Counting/CompanionBinPopulation.lean
-
curl -L -o CompanionBinPopulation.lean https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Combinatorics/ContingencyTables/Counting/CompanionBinPopulation.lean
5.92 kB
| import OAI.Combinatorics.ContingencyTables.Machines.FairBits | |
| import Mathlib.Algebra.Order.Ring.Pow | |
| import Mathlib.Algebra.Order.BigOperators.Group.Finset | |
| import Mathlib.Algebra.Order.BigOperators.GroupWithZero.Finset | |
| import Mathlib.Algebra.BigOperators.Group.Finset.Piecewise | |
| import Mathlib.Data.Fintype.Pi | |
| import Mathlib.Data.Fintype.BigOperators | |
| namespace OAI | |
| /-! # Exact integer bin populations for the companion weight evaluator -/ | |
| namespace ContingencyTables.CompanionBinPopulation | |
| open scoped BigOperators Classical | |
| variable {C : Type*} [Fintype C] | |
| def coordinatePopulation (B N v : β) : β := B*(FairBits.fiber B N v).card/(N:β) | |
| def population (B : β) (N : C β β) (v : C β β) : β := | |
| β c,coordinatePopulation B (N c) (v c) | |
| abbrev Assignments (B : β) (N : C β β) (v : C β β) := | |
| (c : C) β {z : β // z β FairBits.fiber B (N c) (v c)} | |
| lemma assignment_card (B : β) (N : C β β) (v : C β β) : | |
| Fintype.card (Assignments B N v)=β c,(FairBits.fiber B (N c) (v c)).card := by | |
| simp only [Assignments,Fintype.card_pi,Fintype.card_coe] | |
| /-- The product formula is exactly B^e times the integer bin population | |
| divided by the total number of free assignments. -/ | |
| lemma population_eq_card (B : β) (N : C β β) (v : C β β) : | |
| population B N v=(B:β)^Fintype.card C*(Fintype.card (Assignments B N v):β)/ | |
| (β c,(N c:β)) := by | |
| rw [assignment_card] | |
| simp only [population,coordinatePopulation,Finset.prod_div_distrib,Finset.prod_mul_distrib, | |
| Finset.prod_const,Finset.card_univ,Nat.cast_prod] | |
| lemma coordinate_error (B N d v : β) (hB : 0 < B) (hd : 1 β€ d) | |
| (hN : 100*d*B β€ N) (hv : v < B) : | |
| |coordinatePopulation B N v-1| β€ 1/(100*(d:β)) := by | |
| have hdQ : (0:β) < d := by exact_mod_cast (show 0 < d by omega) | |
| have hNpos : 0 < N := lt_of_lt_of_le (by positivity) hN | |
| have hNQ : (0:β) < N := by exact_mod_cast hNpos | |
| have hBQ : (0:β) < B := by exact_mod_cast hB | |
| have hsize : (100:β)*d*B β€ N := by exact_mod_cast hN | |
| have heq : coordinatePopulation B N v-1= | |
| ((B:β)/N)*(((FairBits.fiber B N v).card:β)-(N:β)/B) := by | |
| unfold coordinatePopulation | |
| field_simp [hNQ.ne',hBQ.ne'] | |
| rw [heq,abs_mul,abs_of_nonneg (div_nonneg hBQ.le hNQ.le)] | |
| calc | |
| _ β€ ((B:β)/N)*1 := mul_le_mul_of_nonneg_left | |
| (FairBits.fiber_card_error hB hNpos hv).le (by positivity) | |
| _ = (B:β)/N := mul_one _ | |
| _ β€ _ := by | |
| rw [div_le_div_iffβ hNQ (by positivity : (0:β) < 100*d)] | |
| nlinarith | |
| private lemma reverse_bernoulli (u : β) (hu : 0 β€ u) (n : β) : | |
| (1+u)^n*(1-(n:β)*u) β€ 1 := by | |
| induction n with | |
| | zero => simp | |
| | succ n ih => | |
| have haux : (1+u)*(1-((n:β)+1)*u) β€ 1-(n:β)*u := by | |
| nlinarith [mul_nonneg (show (0:β) β€ n+1 by positivity) (sq_nonneg u)] | |
| calc | |
| _ = (1+u)^n*((1+u)*(1-((n:β)+1)*u)) := by | |
| rw [pow_succ,Nat.cast_add,Nat.cast_one] | |
| ring | |
| _ β€ (1+u)^n*(1-(n:β)*u) := | |
| mul_le_mul_of_nonneg_left haux (by positivity) | |
| _ β€ 1 := ih | |
| /-- The source's alpha_v lies between one half and two, including the | |
| empty-coordinate product. No large interval is enumerated to establish | |
| the bound; its exact cardinality is the proved ceiling difference. -/ | |
| theorem population_bounds (B d : β) (N : C β β) (v : C β β) | |
| (hB : 0 < B) (hd : 1 β€ d) (hcard : Fintype.card C β€ d) | |
| (hN : β c,100*d*B β€ N c) (hv : β c,v c < B) : | |
| 1/2 β€ population B N v β§ population B N v β€ 2 := by | |
| let u : β := 1/(100*(d:β)) | |
| have hdQ : (1:β) β€ d := by exact_mod_cast hd | |
| have hu : 0 β€ u := by dsimp only [u]; positivity | |
| have hu1 : u β€ 1/100 := by | |
| dsimp only [u] | |
| rw [div_le_div_iffβ (by positivity : (0:β)<100*d) (by norm_num : (0:β)<100)] | |
| linarith | |
| have hcountQ : (Fintype.card C:β) β€ d := by exact_mod_cast hcard | |
| have hsum : (Fintype.card C:β)*u β€ 1/100 := by | |
| dsimp only [u] | |
| rw [β mul_div_assoc,div_le_div_iffβ (by positivity : (0:β)<100*d) (by norm_num : (0:β)<100)] | |
| linarith | |
| have hfactor (c : C) : 1-u β€ coordinatePopulation B (N c) (v c) β§ | |
| coordinatePopulation B (N c) (v c) β€ 1+u := by | |
| have hh := abs_le.mp (coordinate_error B (N c) d (v c) hB hd (hN c) (hv c)) | |
| constructor <;> linarith | |
| have hpos (c : C) : 0 β€ coordinatePopulation B (N c) (v c) := by | |
| unfold coordinatePopulation | |
| positivity | |
| have hlo : (1-u)^Fintype.card C β€ population B N v := by | |
| unfold population | |
| calc | |
| _ = β _c : C,(1-u) := by simp | |
| _ β€ _ := Finset.prod_le_prodβ (fun _ _ => by linarith) (fun c _ => (hfactor c).1) | |
| have hup : population B N v β€ (1+u)^Fintype.card C := by | |
| unfold population | |
| calc | |
| _ β€ β _c : C,(1+u) := Finset.prod_le_prodβ (fun c _ => hpos c) (fun c _ => (hfactor c).2) | |
| _ = _ := by simp | |
| have hbern := one_add_mul_le_pow (show (-2:β) β€ -u by linarith) (Fintype.card C) | |
| have hreverse := reverse_bernoulli u hu (Fintype.card C) | |
| have hpower : 0 β€ (1+u)^Fintype.card C := by positivity | |
| constructor | |
| Β· have hh : 1-(Fintype.card C:β)*u β€ (1-u)^Fintype.card C := by | |
| simpa only [mul_neg,sub_eq_add_neg] using hbern | |
| linarith | |
| Β· nlinarith | |
| theorem coordinate_nonempty (B N d v : β) (hB : 0 < B) (hd : 1 β€ d) | |
| (hN : 100*d*B β€ N) (hv : v < B) : (FairBits.fiber B N v).Nonempty := by | |
| have hh := (abs_le.mp (coordinate_error B N d v hB hd hN hv)).1 | |
| have hdQ : (1:β) β€ d := by exact_mod_cast hd | |
| have hu : 1/(100*(d:β)) < 1 := by | |
| rw [div_lt_one (by positivity : (0:β) < 100*d)] | |
| linarith | |
| have hp : 0 < coordinatePopulation B N v := by linarith | |
| apply Finset.card_pos.mp | |
| by_contra hzero | |
| have hz : (FairBits.fiber B N v).card=0 := by omega | |
| simp only [coordinatePopulation,hz,Nat.cast_zero,mul_zero,zero_div] at hp | |
| exact lt_irrefl _ hp | |
| end ContingencyTables.CompanionBinPopulation | |
| end OAI | |