import OAI.Combinatorics.ContingencyTables.Sampling.ExactIntegerProgram import OAI.Combinatorics.ContingencyTables.Transport.RejectionLaw import Mathlib.Analysis.SpecificLimits.Basic namespace OAI /-! # Exact law and geometric tail of integer rejection -/ namespace ContingencyTables.ExactIntegerChoice open MatchingFPRAS ResidualMixture FirstSuccess open scoped BigOperators Classical noncomputable section variable {r Q : ℕ} (w : Fin r → ℕ) (h : 0 x.map some) (some i)=p.mass (some i) := by simp [successMass] private lemma lifted_no_success (p : RationalLaw (Option (Fin r))) : successMass p (fun x => x.map some) none=0 := by simp [successMass] private lemma lifted_failure (p : RationalLaw (Option (Fin r))) : failureMass p (fun x => x.map some)=p.mass none := by simp [failureMass] /-- `none` records that all supplied independent trials rejected; it is not a fallback table. This is the finite prefix law of exact rejection. -/ def retryIntegerLaw (N : ℕ) : RationalLaw (Option (Fin r)) := retryLaw (trialLaw w h hQ) (fun x => x.map some) none N lemma retryIntegerLaw_none (N : ℕ) : (retryIntegerLaw w h hQ N).mass none=(failure w (Q := Q))^N := by rw [retryIntegerLaw,retryLaw_mass,outputMass,lifted_no_success,lifted_failure,trialLaw_none] simp only [zero_mul,ite_true,zero_add] lemma retryIntegerLaw_some (N : ℕ) (i : Fin r) : (retryIntegerLaw w h hQ N).mass (some i)= normalized w i*(1-(failure w (Q := Q))^N) := by rw [retryIntegerLaw,retryLaw_mass,outputMass,lifted_success,lifted_failure,trialLaw_none, weightedTrial_mass] simp only [reduceCtorEq,ite_false,add_zero] have hV : (totalWeight w:ℚ)≠0 := by exact_mod_cast h.ne' have hQt : (Q:ℚ)≠0 := by exact_mod_cast (NeZero.ne Q) have he : (w i:ℚ)/Q=normalized w i*(1-failure w (Q := Q)) := by unfold normalized failure field_simp ring rw [he,mul_assoc] congr 1 have hg := geometric_balance (failure w (Q := Q)) N linarith include hQ in lemma failure_nonnegative : 0≤failure w (Q := Q) := by unfold failure have hQt : (0:ℚ) ((retryIntegerLaw w h hQ N).mass (some i):ℝ)) Filter.atTop (nhds (normalized w i:ℝ)) := by have hf0 : (0:ℝ)≤failure w (Q := Q) := by exact_mod_cast failure_nonnegative w hQ have hf1 : (failure w (Q := Q):ℝ)<1 := by have hh : (failure w (Q := Q):ℝ)<1/2 := by have hh := (Rat.cast_lt (K := ℝ)).mpr (failure_lt_half w hshort) norm_num at hh ⊢ exact hh linarith have ht := tendsto_pow_atTop_nhds_zero_of_lt_one hf0 hf1 have hout : Filter.Tendsto (fun N => (normalized w i:ℝ)*(1-(failure w (Q := Q):ℝ)^N)) Filter.atTop (nhds ((normalized w i:ℝ)*(1-0))) := tendsto_const_nhds.mul (tendsto_const_nhds.sub ht) simpa only [retryIntegerLaw_some,Rat.cast_mul,Rat.cast_sub,Rat.cast_one,Rat.cast_pow, sub_zero,mul_one] using hout /-- The expected number of independent integer proposals is less than two. -/ lemma retryIntegerLaw_expected_attempts (hshort : Q<2*totalWeight w) : Summable (fun N => ((retryIntegerLaw w h hQ N).mass none:ℝ)) ∧ (∑' N,((retryIntegerLaw w h hQ N).mass none:ℝ))<2 := by have hf0 : (0:ℝ)≤failure w (Q := Q) := by exact_mod_cast failure_nonnegative w hQ have hfhalf : (failure w (Q := Q):ℝ)<1/2 := by have hh := (Rat.cast_lt (K := ℝ)).mpr (failure_lt_half w hshort) norm_num at hh ⊢ exact hh have hf1 : (failure w (Q := Q):ℝ)<1 := by linarith simp only [retryIntegerLaw_none,Rat.cast_pow] refine ⟨summable_geometric_of_lt_one hf0 hf1,?_⟩ rw [tsum_geometric_of_lt_one hf0 hf1] rw [inv_lt_comm₀ (by linarith : (0:ℝ)<1-failure w (Q := Q)) (by norm_num)] linarith lemma clog_power_lt_twice (V : ℕ) (hV : 0