Download data/lean/OAI/LinearAlgebra/MatrixMultiplication/JointExtraction/PopulationRates.lean from introvoyz045/math: direct link, hf CLI and curl.
- Browser
- Download file 13 kB
-
https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/LinearAlgebra/MatrixMultiplication/JointExtraction/PopulationRates.lean
- Command line
-
hf download hf://datasets/introvoyz045/math/data/lean/OAI/LinearAlgebra/MatrixMultiplication/JointExtraction/PopulationRates.lean
-
curl -L -o PopulationRates.lean https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/LinearAlgebra/MatrixMultiplication/JointExtraction/PopulationRates.lean
13 kB
| import OAI.LinearAlgebra.MatrixMultiplication.JointExtraction.Population | |
| import OAI.LinearAlgebra.MatrixMultiplication.JointExtraction.TypeCounts | |
| import OAI.LinearAlgebra.MatrixMultiplication.Entropy.ComplexTypeEntropy | |
| import OAI.LinearAlgebra.MatrixMultiplication.Entropy.ComplexSeparationAsymptotics | |
| import Mathlib.Analysis.SpecificLimits.Basic | |
| namespace OAI | |
| /-! Joint tensor extraction, compatibility and entropy estimates. -/ | |
| noncomputable section | |
| namespace MatrixMultiplication.JointPopulationRates | |
| open MatrixMultiplication.Foundation Filter JointPopulation | |
| open scoped BigOperators Topology | |
| theorem tendsto_log_multinomial_div_of_total_div_zero | |
| {A : Type*} [Fintype A] [DecidableEq A] (counts : β β A β β) | |
| (hratio : Tendsto | |
| (fun n : β => ((β a, counts n a : β) : β) / (n : β)) | |
| atTop (π 0)) : | |
| Tendsto (fun n : β => | |
| Real.log (Nat.multinomial Finset.univ (counts n) : β) / (n : β)) | |
| atTop (π 0) := by | |
| have hupperlim : Tendsto | |
| (fun n : β => (((β a, counts n a : β) : β) / (n : β)) * | |
| Real.log (Fintype.card A : β)) atTop (π 0) := by | |
| simpa using hratio.mul_const (Real.log (Fintype.card A : β)) | |
| refine squeeze_zero ?_ ?_ hupperlim | |
| Β· intro n | |
| apply div_nonneg | |
| Β· apply Real.log_nonneg | |
| exact_mod_cast (Nat.succ_le_of_lt (Nat.multinomial_pos Finset.univ (counts n))) | |
| Β· exact Nat.cast_nonneg n | |
| Β· intro n | |
| have hcardNat : Nat.multinomial Finset.univ (counts n) β€ | |
| (Fintype.card A) ^ (β a, counts n a) := by | |
| simpa only [exactWords_card] using exactWords_card_le (counts n) | |
| have hcard : (Nat.multinomial Finset.univ (counts n) : β) β€ | |
| (Fintype.card A : β) ^ (β a, counts n a) := by | |
| exact_mod_cast hcardNat | |
| have hlog : Real.log (Nat.multinomial Finset.univ (counts n) : β) β€ | |
| ((β a, counts n a : β) : β) * Real.log (Fintype.card A : β) := by | |
| simpa only [Real.log_pow] using | |
| Real.log_le_log (Nat.cast_pos.mpr (Nat.multinomial_pos Finset.univ (counts n))) hcard | |
| calc | |
| Real.log (Nat.multinomial Finset.univ (counts n) : β) / (n : β) β€ | |
| (((β a, counts n a : β) : β) * Real.log (Fintype.card A : β)) / (n : β) := | |
| div_le_div_of_nonneg_right hlog (Nat.cast_nonneg n) | |
| _ = (((β a, counts n a : β) : β) / (n : β)) * | |
| Real.log (Fintype.card A : β) := by ring | |
| theorem tendsto_log_multinomial_div_of_mass | |
| {A : Type*} [Fintype A] [DecidableEq A] (counts : β β A β β) | |
| (mass : β) (p : A β β) (hmass : 0 β€ mass) | |
| (htotal : Tendsto (fun n : β => (β a, counts n a : β) / (n : β)) | |
| atTop (π mass)) | |
| (hempirical : 0 < mass β β a, Tendsto | |
| (fun n : β => (counts n a : β) / (β a, counts n a : β)) atTop (π (p a))) : | |
| Tendsto (fun n : β => Real.log (Nat.multinomial Finset.univ (counts n) : β) / | |
| (n : β)) atTop (π (mass * finiteEntropy p)) := by | |
| by_cases hzero : mass = 0 | |
| Β· subst mass | |
| simpa only [zero_mul] using tendsto_log_multinomial_div_of_total_div_zero counts htotal | |
| have hpos : 0 < mass := lt_of_le_of_ne hmass (Ne.symm hzero) | |
| have htop : Tendsto (fun n => β a, counts n a) atTop atTop := | |
| (tendsto_natCast_atTop_iff (R := β)).mp | |
| (Tendsto.num (tendsto_natCast_atTop_atTop (R := β)) hpos htotal) | |
| have hlog := tendsto_log_multinomial_of_empirical_tendsto counts p htop (hempirical hpos) | |
| apply (htotal.mul hlog).congr' | |
| filter_upwards [htop.eventually (eventually_gt_atTop (0 : β))] with n hn | |
| have hn' : ((β a, counts n a : β) : β) β 0 := Nat.cast_ne_zero.mpr (Nat.ne_of_gt hn) | |
| rw [div_mul_div_comm, | |
| mul_comm (n : β) ((β a, counts n a : β) : β), mul_div_mul_left _ _ hn'] | |
| theorem tendsto_total_div_of_coordinate_rates | |
| {A : Type*} [Fintype A] (counts : β β A β β) | |
| (mass : β) (p : A β β) (hp : β a, p a = 1) | |
| (hcounts : β a, Tendsto (fun n : β => (counts n a : β) / (n : β)) | |
| atTop (π (mass * p a))) : | |
| Tendsto (fun n : β => (β a, counts n a : β) / (n : β)) atTop (π mass) := by | |
| have h := tendsto_finsetSum Finset.univ (fun a _ => hcounts a) | |
| simpa only [β Finset.sum_div, β Nat.cast_sum, β Finset.mul_sum, hp, mul_one] using h | |
| theorem tendsto_empirical_of_coordinate_rates | |
| {A : Type*} [Fintype A] (counts : β β A β β) | |
| (mass : β) (p : A β β) (hmass : 0 < mass) (hp : β a, p a = 1) | |
| (hcounts : β a, Tendsto (fun n : β => (counts n a : β) / (n : β)) | |
| atTop (π (mass * p a))) (a : A) : | |
| Tendsto (fun n : β => (counts n a : β) / (β a, counts n a : β)) | |
| atTop (π (p a)) := by | |
| have htotal := tendsto_total_div_of_coordinate_rates counts mass p hp hcounts | |
| have h := (hcounts a).div htotal hmass.ne' | |
| rw [mul_div_cancel_leftβ _ hmass.ne'] at h | |
| apply h.congr' | |
| filter_upwards [eventually_ne_atTop (0 : β)] with n hn | |
| exact div_div_div_cancel_rightβ (Nat.cast_ne_zero.mpr hn) _ _ | |
| theorem finiteEntropy_scalar {A : Type*} [Fintype A] | |
| (p : A β β) (mass : β) (hp : β a, p a = 1) : | |
| finiteEntropy (fun a => mass * p a) = entropyTerm mass + mass * finiteEntropy p := by | |
| simp only [finiteEntropy, entropyTerm_mul, Finset.sum_add_distrib, | |
| β Finset.sum_mul, β Finset.mul_sum, hp, one_mul] | |
| theorem weighted_empirical_entropy_eq {A : Type*} [Fintype A] | |
| (counts : A β β) (scale : β) : | |
| ((β a, counts a : β) / scale) * | |
| finiteEntropy (fun a => (counts a : β) / (β a, counts a : β)) = | |
| finiteEntropy (fun a => (counts a : β) / scale) - | |
| entropyTerm ((β a, counts a : β) / scale) := by | |
| by_cases hzero : (β a, counts a) = 0 | |
| Β· have hc : β a, counts a = 0 := by | |
| intro a | |
| have hle : counts a β€ β b, counts b := | |
| Finset.single_le_sum (fun b _ => Nat.zero_le (counts b)) (Finset.mem_univ a) | |
| rw [hzero] at hle | |
| exact Nat.eq_zero_of_le_zero hle | |
| simp [hc, finiteEntropy] | |
| have hD : ((β a, counts a : β) : β) β 0 := Nat.cast_ne_zero.mpr hzero | |
| have hsum : (β a, (counts a : β) / (β a, counts a : β)) = 1 := by | |
| rw [β Finset.sum_div, β Nat.cast_sum, div_self hD] | |
| have hfac (a : A) : (counts a : β) / scale = | |
| ((β a, counts a : β) / scale) * ((counts a : β) / (β a, counts a : β)) := by | |
| rw [div_mul_div_comm, | |
| mul_comm ((β a, counts a : β) : β) (counts a : β), mul_div_mul_right _ _ hD] | |
| have h := finiteEntropy_scalar | |
| (fun a => (counts a : β) / (β a, counts a : β)) | |
| ((β a, counts a : β) / scale) hsum | |
| simp_rw [β hfac] at h | |
| linarith | |
| theorem tendsto_weighted_empirical_entropy_of_coordinate_rates | |
| {A : Type*} [Fintype A] (counts : β β A β β) | |
| (mass : β) (p : A β β) (hp : β a, p a = 1) | |
| (hcounts : β a, Tendsto (fun n : β => (counts n a : β) / (n : β)) | |
| atTop (π (mass * p a))) : | |
| Tendsto (fun n : β => ((β a, counts n a : β) / (n : β)) * | |
| finiteEntropy (fun a => (counts n a : β) / (β a, counts n a : β))) | |
| atTop (π (mass * finiteEntropy p)) := by | |
| have htotal := tendsto_total_div_of_coordinate_rates counts mass p hp hcounts | |
| have h := (tendsto_finiteEntropy_of_tendsto hcounts).sub (tendsto_entropyTerm htotal) | |
| rw [finiteEntropy_scalar p mass hp, add_sub_cancel_left] at h | |
| simpa only [weighted_empirical_entropy_eq] using h | |
| def sideCounts (counts : Shape β β) (s : Fin 3) (a : Fin 17) : β := | |
| β u, if shapeSide s u = a then counts u else 0 | |
| def sideMass (p : Shape β β) (s : Fin 3) (a : Fin 17) : β := | |
| β u, if shapeSide s u = a then p u else 0 | |
| theorem sideCounts_total (counts : Shape β β) (s : Fin 3) : | |
| (β a, sideCounts counts s a) = β u, counts u := by | |
| unfold sideCounts | |
| rw [Finset.sum_comm] | |
| simp | |
| theorem sideMass_total (p : Shape β β) (s : Fin 3) : | |
| (β a, sideMass p s a) = β u, p u := by | |
| unfold sideMass | |
| rw [Finset.sum_comm] | |
| simp | |
| theorem tendsto_sideCounts_div_of_coordinate_rates | |
| (counts : β β Shape β β) (mass : β) (p : Shape β β) | |
| (hcounts : β u, Tendsto (fun n : β => (counts n u : β) / (n : β)) | |
| atTop (π (mass * p u))) (s : Fin 3) (a : Fin 17) : | |
| Tendsto (fun n : β => (sideCounts (counts n) s a : β) / (n : β)) | |
| atTop (π (mass * sideMass p s a)) := by | |
| have hfun (n : β) : (sideCounts (counts n) s a : β) / (n : β) = | |
| β u, if shapeSide s u = a then (counts n u : β) / (n : β) else 0 := by | |
| unfold sideCounts | |
| rw [Nat.cast_sum, Finset.sum_div] | |
| apply Finset.sum_congr rfl | |
| intro u _ | |
| by_cases h : shapeSide s u = a <;> simp [h] | |
| simp_rw [hfun] | |
| rw [sideMass, Finset.mul_sum] | |
| apply tendsto_finsetSum | |
| intro u _ | |
| by_cases h : shapeSide s u = a | |
| Β· simpa only [h, ite_true] using hcounts u | |
| Β· simp only [h, ite_false, mul_zero] | |
| exact tendsto_const_nhds | |
| variable {H : Type*} [Fintype H] [DecidableEq H] | |
| omit [Fintype H] [DecidableEq H] in | |
| theorem target_side_population (counts : H β Shape β β) | |
| (e : Target counts) (h : H) (s : Fin 3) (a : Fin 17) : | |
| wordPopulation (sideWord counts s (triple counts e) h) a = sideCounts (counts h) s a := by | |
| let w : JointTypeCounts.FixedCoarseWords (counts h) (shapeSide s) | |
| (sideWord counts s (triple counts e) h) := | |
| β¨(e h).val, (e h).property, fun _ => rflβ© | |
| exact ((conditionalWords_nonempty_iff | |
| (sideWord counts s (triple counts e) h) | |
| (fun b u => if shapeSide s u = b then counts h u else 0)).mp | |
| β¨JointTypeCounts.fixedCoarseWordsEquiv _ _ _ wβ©) a | |
| omit [Fintype H] [DecidableEq H] in | |
| theorem tendsto_weighted_target_side_entropy | |
| (counts : β β H β Shape β β) (e : β n, Target (counts n)) | |
| (mass : H β β) (p : H β Shape β β) (hp : β h, β u, p h u = 1) | |
| (hcounts : β h u, Tendsto (fun n : β => (counts n h u : β) / (n : β)) | |
| atTop (π (mass h * p h u))) (h : H) (s : Fin 3) : | |
| Tendsto (fun n : β => ((β u, counts n h u : β) / (n : β)) * | |
| finiteEntropy (fun a => | |
| (wordPopulation (sideWord (counts n) s (triple (counts n) (e n)) h) a : β) / | |
| (β u, counts n h u : β))) | |
| atTop (π (mass h * finiteEntropy (sideMass (p h) s))) := by | |
| have hside := tendsto_weighted_empirical_entropy_of_coordinate_rates | |
| (fun n => sideCounts (counts n h) s) (mass h) (sideMass (p h) s) | |
| ((sideMass_total (p h) s).trans (hp h)) | |
| (tendsto_sideCounts_div_of_coordinate_rates (fun n => counts n h) | |
| (mass h) (p h) (hcounts h) s) | |
| simpa only [target_side_population, sideCounts_total] using hside | |
| theorem tendsto_log_target_card_div | |
| (counts : β β H β Shape β β) (mass : H β β) (p : H β Shape β β) | |
| (hmass : β h, 0 β€ mass h) | |
| (htotal : β h, Tendsto (fun n : β => (β a, counts n h a : β) / (n : β)) | |
| atTop (π (mass h))) | |
| (hempirical : β h, 0 < mass h β β a, Tendsto | |
| (fun n : β => (counts n h a : β) / (β a, counts n h a : β)) | |
| atTop (π (p h a))) : | |
| Tendsto (fun n : β => Real.log (Fintype.card (Target (counts n)) : β) / (n : β)) | |
| atTop (π (β h, mass h * finiteEntropy (p h))) := by | |
| simp_rw [target_card] | |
| exact Separation.tendsto_log_nat_prod_div_nat Finset.univ | |
| (fun h _ n => Nat.multinomial_pos _ _) | |
| (fun h _ => tendsto_log_multinomial_div_of_mass | |
| (fun n => counts n h) (mass h) (p h) (hmass h) (htotal h) (hempirical h)) | |
| theorem tendsto_log_target_card_div_of_coordinate_rates | |
| (counts : β β H β Shape β β) (mass : H β β) (p : H β Shape β β) | |
| (hmass : β h, 0 β€ mass h) (hp : β h, β a, p h a = 1) | |
| (hcounts : β h a, Tendsto (fun n : β => (counts n h a : β) / (n : β)) | |
| atTop (π (mass h * p h a))) : | |
| Tendsto (fun n : β => Real.log (Fintype.card (Target (counts n)) : β) / (n : β)) | |
| atTop (π (β h, mass h * finiteEntropy (p h))) := by | |
| apply tendsto_log_target_card_div counts mass p hmass | |
| Β· intro h | |
| exact tendsto_total_div_of_coordinate_rates (fun n => counts n h) | |
| (mass h) (p h) (hp h) (hcounts h) | |
| Β· intro h hpos a | |
| exact tendsto_empirical_of_coordinate_rates (fun n => counts n h) | |
| (mass h) (p h) hpos (hp h) (hcounts h) a | |
| theorem tendsto_log_targetSet_card_div_of_coordinate_rates | |
| (counts : β β H β Shape β β) (mass : H β β) (p : H β Shape β β) | |
| (hmass : β h, 0 β€ mass h) (hp : β h, β a, p h a = 1) | |
| (hcounts : β h a, Tendsto (fun n : β => (counts n h a : β) / (n : β)) | |
| atTop (π (mass h * p h a))) : | |
| Tendsto (fun n : β => Real.log ((targetSet (counts n)).card : β) / (n : β)) | |
| atTop (π (β h, mass h * finiteEntropy (p h))) := by | |
| simpa only [target_card, targetSet_card] using | |
| tendsto_log_target_card_div_of_coordinate_rates counts mass p hmass hp hcounts | |
| end MatrixMultiplication.JointPopulationRates | |
| end | |
| end OAI | |