math / data /lean /OAI /Combinatorics /ContingencyTables /Counting /CompanionBinPopulation.lean
introvoyz045's picture
Migrated from GitHub
4a56cd6 verified
Raw History Blame Contribute Delete
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