math / data /lean /OAI /LinearAlgebra /MatrixMultiplication /Entropy /ComplexHierarchySeparationRates.lean
introvoyz045's picture
Migrated from GitHub
4a56cd6 verified
Raw History Blame Contribute Delete
15.4 kB
import OAI.LinearAlgebra.MatrixMultiplication.Entropy.ComplexTypeEntropy
import OAI.LinearAlgebra.MatrixMultiplication.Separation.ComplexHierarchyTypeCounting
import OAI.LinearAlgebra.MatrixMultiplication.Entropy.ComplexSeparationAsymptotics
import OAI.LinearAlgebra.MatrixMultiplication.Separation.ComplexLabelHierarchySeparation
namespace OAI
/-! Finite entropy, rate estimates and ordered asymptotic limits. -/
noncomputable section
namespace MatrixMultiplication.Foundation
open Filter
open scoped BigOperators Topology
namespace LabelHierarchySeparation
theorem poolAuxiliaryCost_pos (k : β„•) : 0 < poolAuxiliaryCost k := by
unfold poolAuxiliaryCost
split_ifs
Β· exact Nat.zero_lt_one
Β· exact Separation.gridGroupOrder_pos k
theorem population_le_poolAuxiliaryCost (k : β„•) : k ≀ poolAuxiliaryCost k := by
unfold poolAuxiliaryCost
split_ifs with hk
Β· simp [hk]
Β· exact Separation.population_le_gridGroupOrder k
theorem poolAuxiliaryCost_le_gridGroupOrder (k : β„•) :
poolAuxiliaryCost k ≀ Separation.gridGroupOrder k := by
unfold poolAuxiliaryCost
split_ifs with hk
Β· simpa only [hk] using Separation.population_le_gridGroupOrder k
Β· exact le_rfl
theorem tendsto_log_poolAuxiliaryCost_div_nat {K : β„• β†’ β„•} {H : ℝ}
(hK : βˆ€ n, 0 < K n) (hH : 0 ≀ H)
(hpop : Tendsto (fun n => Real.log (K n : ℝ) / (n : ℝ)) atTop (𝓝 H)) :
Tendsto (fun n => Real.log (poolAuxiliaryCost (K n) : ℝ) / (n : ℝ))
atTop (𝓝 H) := by
refine tendsto_of_tendsto_of_tendsto_of_le_of_le hpop
(Separation.tendsto_log_gridGroupOrder_div_nat hK hH hpop) ?_ ?_
Β· intro n
apply div_le_div_of_nonneg_right _ (Nat.cast_nonneg n)
exact Real.log_le_log (Nat.cast_pos.mpr (hK n))
(by exact_mod_cast population_le_poolAuxiliaryCost (K n))
Β· intro n
apply div_le_div_of_nonneg_right _ (Nat.cast_nonneg n)
exact Real.log_le_log (Nat.cast_pos.mpr (poolAuxiliaryCost_pos (K n)))
(by exact_mod_cast poolAuxiliaryCost_le_gridGroupOrder (K n))
end LabelHierarchySeparation
namespace HierarchySeparationRates
universe u v w
def populationEntropyRate {B : Type*} [Fintype B] (counts : B β†’ β„•) : ℝ :=
(βˆ‘ b, counts b : β„•) *
finiteEntropy (fun b => (counts b : ℝ) / (βˆ‘ b, counts b : β„•))
theorem tendsto_log_multinomial_mul_div_repetition {B : Type*} [Fintype B]
(counts : B β†’ β„•) :
Tendsto
(fun t : β„• =>
Real.log (Nat.multinomial Finset.univ (fun b => t * counts b) : ℝ) / (t : ℝ))
atTop (𝓝 (populationEntropyRate counts)) := by
by_cases hD : 0 < βˆ‘ b, counts b
Β· have hD' : ((βˆ‘ b, counts b : β„•) : ℝ) β‰  0 :=
Nat.cast_ne_zero.mpr (Nat.ne_of_gt hD)
have h := (tendsto_log_multinomial_mul counts hD).const_mul
((βˆ‘ b, counts b : β„•) : ℝ)
apply h.congr
intro t
by_cases ht : t = 0
Β· simp [ht]
have ht' : (t : ℝ) β‰  0 := Nat.cast_ne_zero.mpr ht
change ((βˆ‘ b, counts b : β„•) : ℝ) *
(Real.log (Nat.multinomial Finset.univ (fun b => t * counts b) : ℝ) /
((t : ℝ) * (βˆ‘ b, counts b : β„•))) = _
field_simp [hD', ht']
Β· have hz : (βˆ‘ b, counts b) = 0 := Nat.eq_zero_of_not_pos hD
have hc (b : B) : counts b = 0 := by
apply Nat.eq_zero_of_le_zero
calc
counts b ≀ βˆ‘ c, counts c :=
Finset.single_le_sum (fun c _ => Nat.zero_le _) (Finset.mem_univ b)
_ = 0 := hz
simp only [populationEntropyRate, hz, Nat.cast_zero, zero_mul]
simp [Nat.multinomial, hc]
theorem populationEntropyRate_nonneg {B : Type*} [Fintype B] (counts : B β†’ β„•) :
0 ≀ populationEntropyRate counts := by
apply ge_of_tendsto' (tendsto_log_multinomial_mul_div_repetition counts)
intro t
apply div_nonneg _ (Nat.cast_nonneg t)
apply Real.log_nonneg
exact_mod_cast Nat.one_le_iff_ne_zero.mpr
(Nat.ne_of_gt (Nat.multinomial_pos Finset.univ (fun b => t * counts b)))
section FinitePools
variable {I : Type u} [Fintype I] {Alphabet : I β†’ Type v}
[βˆ€ i, Fintype (Alphabet i)]
def poolTypeCount (counts : βˆ€ i, Alphabet i β†’ β„•) (i : I) (t : β„•) : β„• :=
Nat.multinomial Finset.univ (fun b => t * counts i b)
def poolEntropyTotal (counts : βˆ€ i, Alphabet i β†’ β„•) : ℝ :=
βˆ‘ i, populationEntropyRate (counts i)
def pairingProduct (counts : βˆ€ i, Alphabet i β†’ β„•) (t : β„•) : β„• :=
∏ i, poolTypeCount counts i t
def gridProduct (counts : βˆ€ i, Alphabet i β†’ β„•) (t : β„•) : β„• :=
∏ i, Separation.gridGroupOrder (poolTypeCount counts i t)
def auxiliaryProduct (counts : βˆ€ i, Alphabet i β†’ β„•) (t : β„•) : β„• :=
∏ i, LabelHierarchySeparation.poolAuxiliaryCost (poolTypeCount counts i t)
omit [Fintype I] in
theorem poolTypeCount_pos (counts : βˆ€ i, Alphabet i β†’ β„•) (i : I) (t : β„•) :
0 < poolTypeCount counts i t := Nat.multinomial_pos _ _
theorem pairingProduct_le_auxiliaryProduct (counts : βˆ€ i, Alphabet i β†’ β„•) (t : β„•) :
pairingProduct counts t ≀ auxiliaryProduct counts t := by
exact Finset.prod_le_prod (fun i _ =>
LabelHierarchySeparation.population_le_poolAuxiliaryCost (poolTypeCount counts i t))
theorem auxiliaryProduct_le_gridProduct (counts : βˆ€ i, Alphabet i β†’ β„•) (t : β„•) :
auxiliaryProduct counts t ≀ gridProduct counts t := by
exact Finset.prod_le_prod (fun i _ =>
LabelHierarchySeparation.poolAuxiliaryCost_le_gridGroupOrder (poolTypeCount counts i t))
omit [Fintype I] in
theorem tendsto_log_poolTypeCount_div (counts : βˆ€ i, Alphabet i β†’ β„•) (i : I) :
Tendsto (fun t : β„• => Real.log (poolTypeCount counts i t : ℝ) / (t : ℝ))
atTop (𝓝 (populationEntropyRate (counts i))) :=
tendsto_log_multinomial_mul_div_repetition (counts i)
theorem tendsto_log_pairingProduct_div (counts : βˆ€ i, Alphabet i β†’ β„•) :
Tendsto (fun t : β„• => Real.log (pairingProduct counts t : ℝ) / (t : ℝ))
atTop (𝓝 (poolEntropyTotal counts)) := by
exact Separation.tendsto_log_nat_prod_div_nat Finset.univ
(fun i _ t => poolTypeCount_pos counts i t)
(fun i _ => tendsto_log_poolTypeCount_div counts i)
theorem tendsto_log_gridProduct_div (counts : βˆ€ i, Alphabet i β†’ β„•) :
Tendsto (fun t : β„• => Real.log (gridProduct counts t : ℝ) / (t : ℝ))
atTop (𝓝 (poolEntropyTotal counts)) := by
exact Separation.tendsto_log_gridGroupOrder_prod_div_nat Finset.univ
(fun i _ t => poolTypeCount_pos counts i t)
(fun i _ => populationEntropyRate_nonneg (counts i))
(fun i _ => tendsto_log_poolTypeCount_div counts i)
theorem tendsto_log_auxiliaryProduct_div (counts : βˆ€ i, Alphabet i β†’ β„•) :
Tendsto (fun t : β„• => Real.log (auxiliaryProduct counts t : ℝ) / (t : ℝ))
atTop (𝓝 (poolEntropyTotal counts)) := by
exact Separation.tendsto_log_nat_prod_div_nat Finset.univ
(fun i _ t => LabelHierarchySeparation.poolAuxiliaryCost_pos (poolTypeCount counts i t))
(fun i _ => LabelHierarchySeparation.tendsto_log_poolAuxiliaryCost_div_nat
(poolTypeCount_pos counts i) (populationEntropyRate_nonneg (counts i))
(tendsto_log_poolTypeCount_div counts i))
end FinitePools
section SourceHierarchy
variable {A : Type u} [Fintype A] {Label : β„• β†’ Type v}
[βˆ€ n, Fintype (Label n)]
theorem pushforwardCounts_mul {B : Type*} (counts : A β†’ β„•) (label : A β†’ B)
(t : β„•) (b : B) :
pushforwardCounts (fun a => t * counts a) label b =
t * pushforwardCounts counts label b := by
classical
unfold pushforwardCounts
rw [Finset.mul_sum]
apply Finset.sum_congr rfl
intro a _
by_cases h : label a = b <;> simp [h]
omit [βˆ€ n, Fintype (Label n)] in
theorem sourcePrefixCounts_mul (counts : A β†’ β„•) (labels : βˆ€ n, A β†’ Label n)
(t n : β„•) (r : LabelRecord Label n) :
LabelHierarchyCounts.sourcePrefixCounts (fun a => t * counts a) labels n r =
t * LabelHierarchyCounts.sourcePrefixCounts counts labels n r :=
pushforwardCounts_mul counts (labelRecordOf labels n) t r
abbrev PrefixPool (Label : β„• β†’ Type v) (depth : β„•) :=
Ξ£ n : Fin depth, LabelRecord Label n.val
def sourcePoolCounts (counts : A β†’ β„•) (labels : βˆ€ n, A β†’ Label n) (depth : β„•)
(j : PrefixPool Label depth) (s : Label j.1.val) : β„• :=
LabelHierarchyCounts.sourcePrefixCounts counts labels (j.1.val + 1) (j.2, s)
theorem pairingProduct_source_eq (counts : A β†’ β„•) (labels : βˆ€ n, A β†’ Label n)
(depth : β„•) (hinjective : Function.Injective (labelRecordOf labels depth)) (t : β„•) :
pairingProduct (sourcePoolCounts counts labels depth) t =
Nat.multinomial Finset.univ (fun a => t * counts a) := by
classical
unfold pairingProduct
rw [Fintype.prod_sigma]
have hpool (n : Fin depth) :
(∏ r : LabelRecord Label n.val,
poolTypeCount (sourcePoolCounts counts labels depth) ⟨n, r⟩ t) =
LabelHierarchyCounts.refinementCount
(LabelHierarchyCounts.sourcePrefixCounts (fun a => t * counts a) labels) n.val := by
apply Finset.prod_congr rfl
intro r _
change Nat.multinomial Finset.univ
(fun s : Label n.val => t *
LabelHierarchyCounts.sourcePrefixCounts counts labels (n.val + 1) (r, s)) =
Nat.multinomial Finset.univ
(fun s : Label n.val =>
LabelHierarchyCounts.sourcePrefixCounts (fun a => t * counts a)
labels (n.val + 1) (r, s))
apply Nat.multinomial_congr
intro s _
exact (sourcePrefixCounts_mul counts labels t (n.val + 1) (r, s)).symm
simp_rw [hpool]
rw [Fin.prod_univ_eq_prod_range]
exact LabelHierarchyCounts.source_refinementCount_product
(fun a => t * counts a) labels depth hinjective
theorem pairingProduct_source_eq_exactWords_card [DecidableEq A]
(counts : A β†’ β„•) (labels : βˆ€ n, A β†’ Label n) (depth : β„•)
(hinjective : Function.Injective (labelRecordOf labels depth)) (t : β„•) :
pairingProduct (sourcePoolCounts counts labels depth) t =
Fintype.card (ExactWords (fun a => t * counts a)) := by
rw [pairingProduct_source_eq counts labels depth hinjective, exactWords_card]
theorem source_poolEntropyTotal_eq (counts : A β†’ β„•) (labels : βˆ€ n, A β†’ Label n)
(depth : β„•) (hinjective : Function.Injective (labelRecordOf labels depth)) :
poolEntropyTotal (sourcePoolCounts counts labels depth) = populationEntropyRate counts := by
have hsource : Tendsto
(fun t : β„• => Real.log (pairingProduct (sourcePoolCounts counts labels depth) t : ℝ) /
(t : ℝ)) atTop (𝓝 (populationEntropyRate counts)) := by
simpa only [pairingProduct_source_eq counts labels depth hinjective] using
tendsto_log_multinomial_mul_div_repetition counts
exact tendsto_nhds_unique
(tendsto_log_pairingProduct_div (sourcePoolCounts counts labels depth)) hsource
private theorem normalized_of_per_repetition (counts : A β†’ β„•)
(hD : 0 < βˆ‘ a, counts a) (K : β„• β†’ β„•)
(h : Tendsto (fun t : β„• => Real.log (K t : ℝ) / (t : ℝ))
atTop (𝓝 (populationEntropyRate counts))) :
Tendsto (fun t : β„• => Real.log (K t : ℝ) / ((t : ℝ) * (βˆ‘ a, counts a : β„•)))
atTop (𝓝 (finiteEntropy (fun a => (counts a : ℝ) / (βˆ‘ a, counts a : β„•)))) := by
have hD' : ((βˆ‘ a, counts a : β„•) : ℝ) β‰  0 :=
Nat.cast_ne_zero.mpr (Nat.ne_of_gt hD)
have hmain : populationEntropyRate counts / (βˆ‘ a, counts a : β„•) =
finiteEntropy (fun a => (counts a : ℝ) / (βˆ‘ a, counts a : β„•)) := by
unfold populationEntropyRate
exact mul_div_cancel_leftβ‚€ _ hD'
simpa only [div_div, hmain] using h.div_const ((βˆ‘ a, counts a : β„•) : ℝ)
theorem tendsto_log_source_pairingProduct (counts : A β†’ β„•)
(hD : 0 < βˆ‘ a, counts a) (labels : βˆ€ n, A β†’ Label n) (depth : β„•)
(hinjective : Function.Injective (labelRecordOf labels depth)) :
Tendsto
(fun t : β„• => Real.log (pairingProduct (sourcePoolCounts counts labels depth) t : ℝ) /
((t : ℝ) * (βˆ‘ a, counts a : β„•)))
atTop (𝓝 (finiteEntropy (fun a => (counts a : ℝ) / (βˆ‘ a, counts a : β„•)))) := by
simpa only [pairingProduct_source_eq counts labels depth hinjective] using
tendsto_log_multinomial_mul counts hD
theorem tendsto_log_source_gridProduct (counts : A β†’ β„•)
(hD : 0 < βˆ‘ a, counts a) (labels : βˆ€ n, A β†’ Label n) (depth : β„•)
(hinjective : Function.Injective (labelRecordOf labels depth)) :
Tendsto
(fun t : β„• => Real.log (gridProduct (sourcePoolCounts counts labels depth) t : ℝ) /
((t : ℝ) * (βˆ‘ a, counts a : β„•)))
atTop (𝓝 (finiteEntropy (fun a => (counts a : ℝ) / (βˆ‘ a, counts a : β„•)))) := by
apply normalized_of_per_repetition counts hD
simpa only [source_poolEntropyTotal_eq counts labels depth hinjective] using
tendsto_log_gridProduct_div (sourcePoolCounts counts labels depth)
theorem tendsto_log_source_auxiliaryProduct (counts : A β†’ β„•)
(hD : 0 < βˆ‘ a, counts a) (labels : βˆ€ n, A β†’ Label n) (depth : β„•)
(hinjective : Function.Injective (labelRecordOf labels depth)) :
Tendsto
(fun t : β„• => Real.log (auxiliaryProduct (sourcePoolCounts counts labels depth) t : ℝ) /
((t : ℝ) * (βˆ‘ a, counts a : β„•)))
atTop (𝓝 (finiteEntropy (fun a => (counts a : ℝ) / (βˆ‘ a, counts a : β„•)))) := by
apply normalized_of_per_repetition counts hD
simpa only [source_poolEntropyTotal_eq counts labels depth hinjective] using
tendsto_log_auxiliaryProduct_div (sourcePoolCounts counts labels depth)
theorem rationalLaw_exists_source_rates [DecidableEq A] (p : RationalLaw A)
(labels : βˆ€ n, A β†’ Label n) (depth : β„•)
(hinjective : Function.Injective (labelRecordOf labels depth)) :
βˆƒ counts : A β†’ β„•, 0 < βˆ‘ a, counts a ∧
(βˆ€ a, (counts a : ℝ) / (βˆ‘ a, counts a : β„•) = p.toFiniteLaw.mass a) ∧
Tendsto
(fun t : β„• => Real.log (Fintype.card (ExactWords (fun a => t * counts a)) : ℝ) /
((t : ℝ) * (βˆ‘ a, counts a : β„•)))
atTop (𝓝 (finiteEntropy p.toFiniteLaw.mass)) ∧
Tendsto
(fun t : β„• => Real.log (pairingProduct (sourcePoolCounts counts labels depth) t : ℝ) /
((t : ℝ) * (βˆ‘ a, counts a : β„•)))
atTop (𝓝 (finiteEntropy p.toFiniteLaw.mass)) ∧
Tendsto
(fun t : β„• => Real.log (gridProduct (sourcePoolCounts counts labels depth) t : ℝ) /
((t : ℝ) * (βˆ‘ a, counts a : β„•)))
atTop (𝓝 (finiteEntropy p.toFiniteLaw.mass)) ∧
Tendsto
(fun t : β„• => Real.log (auxiliaryProduct (sourcePoolCounts counts labels depth) t : ℝ) /
((t : ℝ) * (βˆ‘ a, counts a : β„•)))
atTop (𝓝 (finiteEntropy p.toFiniteLaw.mass)) := by
obtain ⟨counts, hD, hmass⟩ := p.exists_type_representation
have hfun : (fun a => (counts a : ℝ) / (βˆ‘ a, counts a : β„•)) =
p.toFiniteLaw.mass := funext hmass
refine ⟨counts, hD, hmass, ?_, ?_, ?_, ?_⟩
Β· simpa only [hfun] using tendsto_log_exactWords_card_mul counts hD
Β· simpa only [hfun] using tendsto_log_source_pairingProduct counts hD labels depth hinjective
Β· simpa only [hfun] using tendsto_log_source_gridProduct counts hD labels depth hinjective
Β· simpa only [hfun] using tendsto_log_source_auxiliaryProduct counts hD labels depth hinjective
end SourceHierarchy
end HierarchySeparationRates
end MatrixMultiplication.Foundation
end
end OAI