introvoyz045's picture
Migrated from GitHub
4a56cd6 verified
Raw History Blame Contribute Delete
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