Download data/lean/OAI/LinearAlgebra/MatrixFields/Entropy/ZeroLeafRates.lean from introvoyz045/math: direct link, hf CLI and curl.
- Browser
- Download file 46.5 kB
-
https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/LinearAlgebra/MatrixFields/Entropy/ZeroLeafRates.lean
- Command line
-
hf download hf://datasets/introvoyz045/math/data/lean/OAI/LinearAlgebra/MatrixFields/Entropy/ZeroLeafRates.lean
-
curl -L -o ZeroLeafRates.lean https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/LinearAlgebra/MatrixFields/Entropy/ZeroLeafRates.lean
46.5 kB
import Mathlib
import OAI.LinearAlgebra.MatrixFields.Entropy.InitialLeafRates
import OAI.LinearAlgebra.MatrixFields.Extraction.ComplexLabelHierarchySeparation
import OAI.LinearAlgebra.MatrixFields.Extraction.ComplexRationalTypes
namespace OAI
namespace MatrixAllFields
open scoped BigOperators Topology Polynomial
section
namespace MatrixMultiplication.Foundation
open Filter
open scoped Topology
noncomputable def factorialLogRemainder (n : β) : β :=
Real.log (Nat.factorial n : β) - ((n : β) * Real.log n - n)
theorem factorialLogRemainder_eq (n : β) :
factorialLogRemainder n =
Real.log (Nat.factorial n : β) - ((n : β) * Real.log n - n) := rfl
@[simp] theorem factorialLogRemainder_zero : factorialLogRemainder 0 = 0 := by
simp [factorialLogRemainder]
theorem factorialLogRemainder_stirling (n : β) :
factorialLogRemainder n =
Real.log (Stirling.stirlingSeq n) + (1 / 2 : β) * Real.log (2 * n) := by
cases n with
| zero => simp
| succ n =>
have h := Stirling.log_stirlingSeq_formula (n + 1)
rw [Real.log_div (by positivity) (Real.exp_ne_zero _), Real.log_exp] at h
unfold factorialLogRemainder
linarith
theorem tendsto_factorialLogRemainder_div :
Tendsto (fun n : β => factorialLogRemainder n / (n : β)) atTop (π 0) := by
have hnat : Tendsto (fun n : β => (n : β)) atTop atTop :=
tendsto_natCast_atTop_atTop
have hstirling :
Tendsto (fun n : β => Real.log (Stirling.stirlingSeq n) / (n : β))
atTop (π 0) :=
(Stirling.tendsto_stirlingSeq_sqrt_pi.log (by positivity)).div_atTop hnat
have hlog : Tendsto (fun n : β => Real.log (n : β) / (n : β))
atTop (π 0) := by
simpa only [Function.comp_def, id_eq] using
Real.isLittleO_log_id_atTop.tendsto_div_nhds_zero.comp hnat
have hconstant : Tendsto (fun n : β => Real.log 2 / (n : β))
atTop (π 0) := tendsto_const_nhds.div_atTop hnat
have hsum : Tendsto
(fun n : β => Real.log (Stirling.stirlingSeq n) / (n : β) +
(1 / 2 : β) * (Real.log 2 / (n : β) + Real.log (n : β) / (n : β)))
atTop (π 0) := by
simpa using hstirling.add ((hconstant.add hlog).const_mul (1 / 2 : β))
apply hsum.congr'
filter_upwards [eventually_ne_atTop (0 : β)] with n hn
rw [factorialLogRemainder_stirling,
Real.log_mul (by norm_num) (Nat.cast_ne_zero.mpr hn)]
ring
theorem tendsto_factorialLogRemainder_mul_div (c : β) :
Tendsto (fun t : β => factorialLogRemainder (t * c) / (t : β))
atTop (π 0) := by
by_cases hc : c = 0
Β· subst c
simp
have hmul : Tendsto (fun t : β => t * c) atTop atTop :=
tendsto_id.atTop_mul_const' (Nat.pos_of_ne_zero hc)
have hscaled : Tendsto
(fun t : β => factorialLogRemainder (t * c) / (t * c : β) * (c : β))
atTop (π 0) := by
simpa using (tendsto_factorialLogRemainder_div.comp hmul).mul_const (c : β)
apply hscaled.congr
intro t
by_cases ht : t = 0
Β· simp [ht]
rw [Nat.cast_mul]
field_simp [Nat.cast_ne_zero.mpr hc, Nat.cast_ne_zero.mpr ht]
theorem factorialLogRemainder_succ (n : β) :
factorialLogRemainder (n + 1) = factorialLogRemainder n + 1 -
(n : β) * (Real.log ((n : β) + 1) - Real.log n) := by
unfold factorialLogRemainder
rw [Nat.factorial_succ, Nat.cast_mul,
Real.log_mul (by positivity) (by positivity)]
push_cast
ring
private theorem nat_mul_log_increment_le_one (n : β) :
(n : β) * (Real.log ((n : β) + 1) - Real.log n) β€ 1 := by
by_cases hn : n = 0
Β· simp [hn]
have hpos : 0 < (n : β) := Nat.cast_pos.mpr (Nat.pos_of_ne_zero hn)
have hsucc : 0 < (n : β) + 1 := by positivity
have hlog := Real.log_le_sub_one_of_pos (div_pos hsucc hpos)
rw [Real.log_div hsucc.ne' hpos.ne'] at hlog
have hmul := mul_le_mul_of_nonneg_left hlog hpos.le
have heq : (n : β) * (((n : β) + 1) / (n : β) - 1) = 1 := by
field_simp [hpos.ne']
ring
rwa [heq] at hmul
private theorem one_le_succ_mul_log_increment (n : β) (hn : 0 < n) :
1 β€ ((n : β) + 1) * (Real.log ((n : β) + 1) - Real.log n) := by
have hpos : 0 < (n : β) := Nat.cast_pos.mpr hn
have hsucc : 0 < (n : β) + 1 := by positivity
have hlog := Real.one_sub_inv_le_log_of_pos (div_pos hsucc hpos)
rw [Real.log_div hsucc.ne' hpos.ne'] at hlog
have hmul := mul_le_mul_of_nonneg_left hlog hsucc.le
have heq : ((n : β) + 1) * (1 - (((n : β) + 1) / (n : β))β»ΒΉ) = 1 := by
rw [inv_div]
field_simp [hsucc.ne']
ring
rwa [heq] at hmul
theorem factorialLogRemainder_nonneg (n : β) : 0 β€ factorialLogRemainder n := by
induction n with
| zero => simp
| succ n ih =>
have hlog := nat_mul_log_increment_le_one n
rw [factorialLogRemainder_succ]
linarith
theorem factorialLogRemainder_le_one_add_log_pos (n : β) :
factorialLogRemainder (n + 1) β€ 1 + Real.log (n + 1 : β) := by
induction n with
| zero => norm_num [factorialLogRemainder]
| succ n ih =>
have hlog := one_le_succ_mul_log_increment (n + 1) (Nat.succ_pos n)
have hstep := factorialLogRemainder_succ (n + 1)
push_cast at ih hlog hstep β’
nlinarith
theorem factorialLogRemainder_le_one_add_log (n : β) :
factorialLogRemainder n β€ 1 + Real.log (n + 1 : β) := by
cases n with
| zero => norm_num
| succ n =>
refine (factorialLogRemainder_le_one_add_log_pos n).trans ?_
apply add_le_add le_rfl
apply Real.log_le_log
Β· positivity
Β· exact_mod_cast Nat.le_succ (n + 1)
theorem abs_factorialLogRemainder_le (n : β) :
|factorialLogRemainder n| β€ 1 + Real.log (n + 1 : β) := by
rw [abs_of_nonneg (factorialLogRemainder_nonneg n)]
exact factorialLogRemainder_le_one_add_log n
end MatrixMultiplication.Foundation
namespace MatrixMultiplication.Foundation
open Filter
open scoped BigOperators Topology
variable {A : Type*} [Fintype A]
theorem log_multinomial (counts : A β β) :
Real.log (Nat.multinomial Finset.univ counts : β) =
Real.log (Nat.factorial (β a, counts a) : β) -
β a, Real.log (Nat.factorial (counts a) : β) := by
have hspec :
(β a, (Nat.factorial (counts a) : β)) *
(Nat.multinomial Finset.univ counts : β) =
(Nat.factorial (β a, counts a) : β) := by
exact_mod_cast Nat.multinomial_spec Finset.univ counts
have hprod : (β a, (Nat.factorial (counts a) : β)) β 0 := by
exact Finset.prod_ne_zero_iff.mpr (fun a _ => by positivity)
have hmulti : (Nat.multinomial Finset.univ counts : β) β 0 := by
exact_mod_cast (Nat.ne_of_gt (Nat.multinomial_pos Finset.univ counts))
have hlog := congrArg Real.log hspec
rw [Real.log_mul hprod hmulti,
Real.log_prod (fun a _ => by positivity)] at hlog
linarith
private theorem entropyTerm_div_scalar (x y : β) :
entropyTerm (x / y) = entropyTerm x / y + (x / y) * Real.log y := by
by_cases hx : x = 0
Β· simp [hx]
by_cases hy : y = 0
Β· simp [hy]
rw [entropyTerm, Real.log_div hx hy]
unfold entropyTerm
ring
theorem finiteEntropy_nat_normalize (counts : A β β)
(hD : 0 < β a, counts a) :
finiteEntropy (fun a => (counts a : β) / (β a, counts a : β)) =
Real.log (β a, counts a : β) -
(β a, (counts a : β) * Real.log (counts a : β)) /
(β a, counts a : β) := by
have hD' : ((β a, counts a : β) : β) β 0 := by
exact_mod_cast (Nat.ne_of_gt hD)
unfold finiteEntropy
simp only [entropyTerm_div_scalar]
simp only [entropyTerm, neg_mul,
Finset.sum_add_distrib, β Finset.sum_div, β Finset.sum_mul,
Finset.sum_neg_distrib]
rw [β Nat.cast_sum, div_self hD']
ring
theorem log_multinomial_eq_main_add_remainder (counts : A β β) :
Real.log (Nat.multinomial Finset.univ counts : β) =
(β a, counts a : β) * Real.log (β a, counts a : β) -
(β a, (counts a : β) * Real.log (counts a : β)) +
factorialLogRemainder (β a, counts a) -
β a, factorialLogRemainder (counts a) := by
rw [log_multinomial]
unfold factorialLogRemainder
simp only [Finset.sum_sub_distrib, β Nat.cast_sum]
ring
theorem log_multinomial_eq_entropy_add_remainder (counts : A β β)
(hD : 0 < β a, counts a) :
Real.log (Nat.multinomial Finset.univ counts : β) =
(β a, counts a : β) *
finiteEntropy (fun a => (counts a : β) / (β a, counts a : β)) +
factorialLogRemainder (β a, counts a) -
β a, factorialLogRemainder (counts a) := by
have hD' : ((β a, counts a : β) : β) β 0 := by
exact_mod_cast (Nat.ne_of_gt hD)
rw [log_multinomial_eq_main_add_remainder, finiteEntropy_nat_normalize counts hD,
mul_sub, mul_div_cancelβ _ hD']
theorem log_multinomial_sub_entropy_eq_remainder (counts : A β β) :
Real.log (Nat.multinomial Finset.univ counts : β) -
(β a, counts a : β) *
finiteEntropy (fun a => (counts a : β) / (β a, counts a : β)) =
factorialLogRemainder (β a, counts a) -
β a, factorialLogRemainder (counts a) := by
by_cases hD : 0 < β a, counts a
Β· rw [log_multinomial_eq_entropy_add_remainder counts hD]
ring
Β· have hzero : (β a, counts a) = 0 := Nat.eq_zero_of_not_pos hD
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, Nat.multinomial]
theorem abs_log_multinomial_sub_entropy_le (counts : A β β) :
|Real.log (Nat.multinomial Finset.univ counts : β) -
(β a, counts a : β) *
finiteEntropy (fun a => (counts a : β) / (β a, counts a : β))| β€
((Fintype.card A : β) + 1) *
(1 + Real.log ((β a, counts a) + 1 : β)) := by
rw [log_multinomial_sub_entropy_eq_remainder]
have hsum_nonneg : 0 β€ β a, factorialLogRemainder (counts a) :=
Finset.sum_nonneg (fun a _ => factorialLogRemainder_nonneg (counts a))
have heach (a : A) : factorialLogRemainder (counts a) β€
1 + Real.log ((β b, counts b) + 1 : β) := by
refine (factorialLogRemainder_le_one_add_log (counts a)).trans ?_
apply add_le_add le_rfl
apply Real.log_le_log
Β· positivity
Β· have hle : counts a β€ β b, counts b :=
Finset.single_le_sum (fun b _ => Nat.zero_le (counts b)) (Finset.mem_univ a)
exact_mod_cast Nat.add_le_add_right hle 1
calc
|factorialLogRemainder (β a, counts a) - β a, factorialLogRemainder (counts a)| β€
|factorialLogRemainder (β a, counts a)| +
|β a, factorialLogRemainder (counts a)| := by
simpa only [sub_eq_add_neg, abs_neg] using
abs_add_le (factorialLogRemainder (β a, counts a))
(-(β a, factorialLogRemainder (counts a)))
_ = factorialLogRemainder (β a, counts a) +
β a, factorialLogRemainder (counts a) := by
rw [abs_of_nonneg (factorialLogRemainder_nonneg _), abs_of_nonneg hsum_nonneg]
_ β€ (1 + Real.log ((β a, counts a) + 1 : β)) +
β _a : A, (1 + Real.log ((β a, counts a) + 1 : β)) :=
add_le_add (factorialLogRemainder_le_one_add_log _)
(Finset.sum_le_sum (fun a _ => heach a))
_ = ((Fintype.card A : β) + 1) *
(1 + Real.log ((β a, counts a) + 1 : β)) := by
simp only [Finset.sum_const, Finset.card_univ, nsmul_eq_mul]
ring
theorem abs_log_multinomial_div_sub_entropy_le (counts : A β β)
(hD : 0 < β a, counts a) :
|Real.log (Nat.multinomial Finset.univ counts : β) / (β a, counts a : β) -
finiteEntropy (fun a => (counts a : β) / (β a, counts a : β))| β€
((Fintype.card A : β) + 1) *
((1 + Real.log ((β a, counts a) + 1 : β)) / (β a, counts a : β)) := by
have hD' : (0 : β) < (β a, counts a : β) := Nat.cast_pos.mpr hD
calc
|Real.log (Nat.multinomial Finset.univ counts : β) / (β a, counts a : β) -
finiteEntropy (fun a => (counts a : β) / (β a, counts a : β))| =
|(Real.log (Nat.multinomial Finset.univ counts : β) -
(β a, counts a : β) *
finiteEntropy (fun a => (counts a : β) / (β a, counts a : β))) /
(β a, counts a : β)| := by
rw [sub_div, mul_div_cancel_leftβ _ hD'.ne']
_ = |Real.log (Nat.multinomial Finset.univ counts : β) -
(β a, counts a : β) *
finiteEntropy (fun a => (counts a : β) / (β a, counts a : β))| /
(β a, counts a : β) := by
rw [abs_div, abs_of_pos hD']
_ β€ ((Fintype.card A : β) + 1) *
((1 + Real.log ((β a, counts a) + 1 : β)) / (β a, counts a : β)) := by
simpa only [mul_div_assoc] using
div_le_div_of_nonneg_right (abs_log_multinomial_sub_entropy_le counts) hD'.le
private theorem tendsto_one_add_log_succ_div :
Tendsto (fun n : β => (1 + Real.log (n + 1 : β)) / (n : β))
atTop (π 0) := by
have hnat : Tendsto (fun n : β => (n : β)) atTop atTop :=
tendsto_natCast_atTop_atTop
have hconstant : Tendsto (fun n : β => (1 : β) / n) atTop (π 0) :=
tendsto_const_nhds.div_atTop hnat
have hlog : Tendsto (fun n : β => Real.log (n : β) / n) atTop (π 0) := by
simpa only [Function.comp_def, id_eq] using
Real.isLittleO_log_id_atTop.tendsto_div_nhds_zero.comp hnat
have hshift := Real.tendsto_log_nat_add_one_sub_log.div_atTop hnat
have hsum : Tendsto
(fun n : β => (1 : β) / n +
((Real.log ((n : β) + 1) - Real.log n) / n + Real.log n / n))
atTop (π 0) := by
simpa using hconstant.add (hshift.add hlog)
apply hsum.congr
intro n
push_cast
ring
theorem tendsto_log_multinomial_div_sub_entropy {I : Type*} {l : Filter I}
(counts : I β A β β)
(htotal : Tendsto (fun i => β a, counts i a) l atTop) :
Tendsto
(fun i => Real.log (Nat.multinomial Finset.univ (counts i) : β) /
(β a, counts i a : β) -
finiteEntropy (fun a => (counts i a : β) / (β a, counts i a : β)))
l (π 0) := by
have hbound : Tendsto
(fun i => ((Fintype.card A : β) + 1) *
((1 + Real.log ((β a, counts i a) + 1 : β)) / (β a, counts i a : β)))
l (π 0) := by
simpa using (tendsto_one_add_log_succ_div.comp htotal).const_mul
((Fintype.card A : β) + 1)
refine squeeze_zero_norm' ?_ hbound
filter_upwards [htotal.eventually (eventually_gt_atTop (0 : β))] with i hi
simpa only [Real.norm_eq_abs] using
abs_log_multinomial_div_sub_entropy_le (counts i) hi
theorem tendsto_log_multinomial_of_empirical_tendsto {I : Type*} {l : Filter I}
(counts : I β A β β) (p : A β β)
(htotal : Tendsto (fun i => β a, counts i a) l atTop)
(hmass : β a, Tendsto
(fun i => (counts i a : β) / (β b, counts i b : β)) l (π (p a))) :
Tendsto
(fun i => Real.log (Nat.multinomial Finset.univ (counts i) : β) /
(β a, counts i a : β))
l (π (finiteEntropy p)) := by
have herror := tendsto_log_multinomial_div_sub_entropy counts htotal
have hentropy := tendsto_finiteEntropy_of_tendsto hmass
simpa only [zero_add, sub_add_cancel] using herror.add hentropy
private theorem mul_log_mul (x y : β) :
(x * y) * Real.log (x * y) =
(x * Real.log x) * y + x * (y * Real.log y) := by
calc
(x * y) * Real.log (x * y) = -entropyTerm (x * y) := by
simp [entropyTerm]
_ = -(y * entropyTerm x + x * entropyTerm y) := by
rw [entropyTerm_mul]
_ = (x * Real.log x) * y + x * (y * Real.log y) := by
unfold entropyTerm
ring
theorem log_multinomial_mul_eq (counts : A β β) (t : β) :
Real.log (Nat.multinomial Finset.univ (fun a => t * counts a) : β) =
(t : β) *
((β a, counts a : β) * Real.log (β a, counts a : β) -
β a, (counts a : β) * Real.log (counts a : β)) +
factorialLogRemainder (t * β a, counts a) -
β a, factorialLogRemainder (t * counts a) := by
have hsum : (β a, t * counts a) = t * β a, counts a :=
(Finset.mul_sum Finset.univ counts t).symm
have hlogs :
(β a, ((t : β) * counts a) * Real.log ((t : β) * counts a)) =
((t : β) * Real.log t) * (β a, counts a : β) +
(t : β) * β a, (counts a : β) * Real.log (counts a : β) := by
simp only [mul_log_mul, Finset.sum_add_distrib, β Finset.mul_sum,
β Nat.cast_sum]
rw [log_multinomial_eq_main_add_remainder, hsum]
simp only [Nat.cast_mul]
rw [mul_log_mul, hlogs]
ring
theorem tendsto_log_multinomial_mul (counts : A β β)
(hD : 0 < β a, counts a) :
Tendsto
(fun t : β =>
Real.log (Nat.multinomial Finset.univ (fun a => t * counts a) : β) /
((t : β) * (β a, counts a : β)))
atTop
(π (finiteEntropy (fun a => (counts a : β) / (β a, counts a : β)))) := by
have hD' : ((β a, counts a : β) : β) β 0 := by
exact_mod_cast (Nat.ne_of_gt hD)
have hsum :
Tendsto (fun t : β => β a, factorialLogRemainder (t * counts a) / (t : β))
atTop (π 0) := by
simpa using tendsto_finsetSum Finset.univ
(fun a _ => tendsto_factorialLogRemainder_mul_div (counts a))
have herror : Tendsto
(fun t : β =>
(factorialLogRemainder (t * β a, counts a) / (t : β) -
β a, factorialLogRemainder (t * counts a) / (t : β)) /
(β a, counts a : β)) atTop (π 0) := by
simpa using ((tendsto_factorialLogRemainder_mul_div (β a, counts a)).sub
hsum).div_const (β a, counts a : β)
have hlimit : Tendsto
(fun t : β =>
finiteEntropy (fun a => (counts a : β) / (β a, counts a : β)) +
(factorialLogRemainder (t * β a, counts a) / (t : β) -
β a, factorialLogRemainder (t * counts a) / (t : β)) /
(β a, counts a : β))
atTop
(π (finiteEntropy (fun a => (counts a : β) / (β a, counts a : β)))) := by
simpa only [add_zero] using tendsto_const_nhds.add herror
apply hlimit.congr'
filter_upwards [eventually_ne_atTop (0 : β)] with t ht
rw [log_multinomial_mul_eq, finiteEntropy_nat_normalize counts hD,
β Finset.sum_div]
have ht' : (t : β) β 0 := Nat.cast_ne_zero.mpr ht
field_simp [ht', hD']
ring
theorem tendsto_log_multinomial_repetitions {I : Type*} {l : Filter I}
(counts : A β β) (hD : 0 < β a, counts a)
(repetitions : I β β) (hrep : Tendsto repetitions l atTop) :
Tendsto
(fun i =>
Real.log (Nat.multinomial Finset.univ
(fun a => repetitions i * counts a) : β) /
((repetitions i : β) * (β a, counts a : β)))
l (π (finiteEntropy (fun a => (counts a : β) / (β a, counts a : β)))) := by
exact (tendsto_log_multinomial_mul counts hD).comp hrep
theorem tendsto_log_exactWords_card_mul [DecidableEq A] (counts : A β β)
(hD : 0 < β a, counts a) :
Tendsto
(fun t : β => Real.log (Fintype.card (ExactWords (fun a => t * counts a)) : β) /
((t : β) * (β a, counts a : β)))
atTop
(π (finiteEntropy (fun a => (counts a : β) / (β a, counts a : β)))) := by
simpa only [exactWords_card] using tendsto_log_multinomial_mul counts hD
theorem abs_log_exactWords_card_sub_entropy_le [DecidableEq A] (counts : A β β) :
|Real.log (Fintype.card (ExactWords counts) : β) -
(β a, counts a : β) *
finiteEntropy (fun a => (counts a : β) / (β a, counts a : β))| β€
((Fintype.card A : β) + 1) *
(1 + Real.log ((β a, counts a) + 1 : β)) := by
simpa only [exactWords_card] using abs_log_multinomial_sub_entropy_le counts
theorem log_card_le_entropy_of_injective_exactWords [DecidableEq A]
{O : Type*} [Fintype O] [Nonempty O] (counts : A β β)
(label : O β ExactWords counts) (hinj : Function.Injective label) :
Real.log (Fintype.card O : β) β€
(β a, counts a : β) *
finiteEntropy (fun a => (counts a : β) / (β a, counts a : β)) +
((Fintype.card A : β) + 1) *
(1 + Real.log ((β a, counts a) + 1 : β)) := by
have hcard := Fintype.card_le_of_injective label hinj
have hlog : Real.log (Fintype.card O : β) β€
Real.log (Fintype.card (ExactWords counts) : β) :=
Real.log_le_log (Nat.cast_pos.mpr Fintype.card_pos) (by exact_mod_cast hcard)
have herror := (abs_le.mp (abs_log_exactWords_card_sub_entropy_le counts)).2
linarith
theorem log_card_div_le_entropy_of_injective_exactWords [DecidableEq A]
{O : Type*} [Fintype O] [Nonempty O] (counts : A β β)
(hD : 0 < β a, counts a)
(label : O β ExactWords counts) (hinj : Function.Injective label) :
Real.log (Fintype.card O : β) / (β a, counts a : β) β€
finiteEntropy (fun a => (counts a : β) / (β a, counts a : β)) +
((Fintype.card A : β) + 1) *
((1 + Real.log ((β a, counts a) + 1 : β)) / (β a, counts a : β)) := by
have hD' : (0 : β) < (β a, counts a : β) := Nat.cast_pos.mpr hD
have h := div_le_div_of_nonneg_right
(log_card_le_entropy_of_injective_exactWords counts label hinj) hD'.le
rw [add_div, mul_div_cancel_leftβ _ hD'.ne', mul_div_assoc] at h
exact h
theorem tendsto_log_exactWords_card_of_empirical_tendsto [DecidableEq A]
{I : Type*} {l : Filter I} (counts : I β A β β) (p : A β β)
(htotal : Tendsto (fun i => β a, counts i a) l atTop)
(hmass : β a, Tendsto
(fun i => (counts i a : β) / (β b, counts i b : β)) l (π (p a))) :
Tendsto
(fun i => Real.log (Fintype.card (ExactWords (counts i)) : β) /
(β a, counts i a : β))
l (π (finiteEntropy p)) := by
simpa only [exactWords_card] using
tendsto_log_multinomial_of_empirical_tendsto counts p htotal hmass
namespace RationalLaw
theorem exists_exactWords_entropy_limit [DecidableEq A] (p : RationalLaw A) :
β 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)) := by
obtain β¨counts, hD, hmassβ© := p.exists_type_representation
refine β¨counts, hD, hmass, ?_β©
have hfun : (fun a => (counts a : β) / (β a, counts a : β)) =
p.toFiniteLaw.mass := funext hmass
rw [β hfun]
exact tendsto_log_exactWords_card_mul counts hD
end RationalLaw
end MatrixMultiplication.Foundation
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
namespace MatrixMultiplication.LeafRates
open MatrixMultiplication.Foundation Filter
open scoped BigOperators Topology
variable {A U : Type*} [Fintype A] [DecidableEq A] [Fintype U] [DecidableEq U]
omit [DecidableEq U] in
theorem log_card_supported (statistic : U β A) (counts b : A β β)
(hb : β a, 0 < counts a β Fintype.card {u : U // statistic u = a} = b a)
(hbpos : β a, 0 < b a) :
Real.log (Fintype.card (ExactStatisticWords.Words statistic counts) : β) =
Real.log (Nat.multinomial Finset.univ counts : β) +
β a, (counts a : β) * Real.log (b a : β) := by
rw [ExactStatisticWords.card_of_supported statistic counts b hb, Nat.cast_mul, Nat.cast_prod]
have hm : (Nat.multinomial Finset.univ counts : β) β 0 := by
exact_mod_cast (Nat.ne_of_gt (Nat.multinomial_pos Finset.univ counts))
have hp : (β a, (b a ^ counts a : β) : β) β 0 := by
apply Finset.prod_ne_zero_iff.mpr
intro a _
exact_mod_cast (Nat.ne_of_gt (pow_pos (hbpos a) _))
rw [Real.log_mul hm hp, Real.log_prod
(fun a _ => by exact_mod_cast (Nat.ne_of_gt (pow_pos (hbpos a) (counts a))))]
simp only [Nat.cast_pow, Real.log_pow]
omit [DecidableEq U] in
theorem log_card (statistic : U β A) (counts b : A β β)
(hb : β a, Fintype.card {u : U // statistic u = a} = b a)
(hbpos : β a, 0 < b a) :
Real.log (Fintype.card (ExactStatisticWords.Words statistic counts) : β) =
Real.log (Nat.multinomial Finset.univ counts : β) +
β a, (counts a : β) * Real.log (b a : β) :=
log_card_supported statistic counts b (fun a _ => hb a) hbpos
omit [DecidableEq U] in
theorem tendsto_log_card_supported (statistic : U β A) (counts b : A β β)
(hb : β a, 0 < counts a β Fintype.card {u : U // statistic u = a} = b a)
(hbpos : β a, 0 < b a) (hD : 0 < β a, counts a) :
Tendsto (fun t : β =>
Real.log (Fintype.card (ExactStatisticWords.Words statistic (fun a => t * counts a)) : β) /
((t : β) * (β a, counts a : β))) atTop
(π (finiteEntropy (fun a => (counts a : β) / (β a, counts a : β)) +
β a, ((counts a : β) / (β a, counts a : β)) * Real.log (b a : β))) := by
have hl := (tendsto_log_multinomial_mul counts hD).add_const
(β a, ((counts a : β) / (β a, counts a : β)) * Real.log (b a : β))
apply hl.congr'
filter_upwards [eventually_ne_atTop (0 : β)] with t ht
have hbt : β a, 0 < t * counts a β Fintype.card {u : U // statistic u = a} = b a :=
fun a ha => hb a (by nlinarith)
rw [log_card_supported statistic (fun a => t * counts a) b hbt hbpos, add_div]
congr 1
rw [Finset.sum_div]
apply Finset.sum_congr rfl
intro a _
have ht' : (t : β) β 0 := Nat.cast_ne_zero.mpr ht
have hD' : ((β a, counts a : β) : β) β 0 := by exact_mod_cast (Nat.ne_of_gt hD)
have hDsum : (β a, (counts a : β)) β 0 := by
simpa only [Nat.cast_sum] using hD'
simp only [Nat.cast_mul]
field_simp [ht', hD', hDsum]
omit [DecidableEq U] in
theorem tendsto_log_card (statistic : U β A) (counts b : A β β)
(hb : β a, Fintype.card {u : U // statistic u = a} = b a)
(hbpos : β a, 0 < b a) (hD : 0 < β a, counts a) :
Tendsto (fun t : β =>
Real.log (Fintype.card (ExactStatisticWords.Words statistic (fun a => t * counts a)) : β) /
((t : β) * (β a, counts a : β))) atTop
(π (finiteEntropy (fun a => (counts a : β) / (β a, counts a : β)) +
β a, ((counts a : β) / (β a, counts a : β)) * Real.log (b a : β))) :=
tendsto_log_card_supported statistic counts b (fun a _ => hb a) hbpos hD
end MatrixMultiplication.LeafRates
namespace MatrixMultiplication.AllFieldZeroLeafRates
open MatrixMultiplication.Foundation Filter AllFieldParameters CWWindowedLeaves CWCompleteStatistics
open scoped BigOperators Topology
attribute [local instance] Classical.propDecidable
abbrev fourWords (t : Shape) (counts : PairSlot β β) (m : β) :=
ExactStatisticWords.Words
(fun w : Alphabet 4 (shapeMax t) => fourStatistic w.val) (fun s => m * counts s)
def twoWords (k : Fin 5) (counts : Fin 6 β β) (m : β) :=
ExactStatisticWords.Words
(fun w : Alphabet 2 k.val => twoStatistic w.val) (fun s => m * counts s)
instance twoWordsFintype (k : Fin 5) (counts : Fin 6 β β) (m : β) :
Fintype (twoWords k counts m) := by
unfold twoWords
infer_instance
theorem multiplicity_pos (s : Fin 6) : 0 < AllFieldParameters.multiplicity s := by
fin_cases s <;> decide
theorem four_supported (t : Shape) (counts : PairSlot β β)
(hD : 0 < β s, counts s)
(hlaw : β s, (counts s : β) / (β s, counts s : β) =
(zeroPairLaw t s.1 s.2 : β)) (s : PairSlot) (hs : 0 < counts s) :
Fintype.card {w : Alphabet 4 (shapeMax t) // fourStatistic w.val = s} =
AllFieldParameters.multiplicity s.1 * AllFieldParameters.multiplicity s.2 := by
apply four_fixedWeight_multiplicity
by_contra hweight
have hzero := zeroPairLaw_outside_support t s.1 s.2 hweight
have hpos : (0 : β) < (counts s : β) / (β s, counts s : β) :=
div_pos (by exact_mod_cast hs) (by exact_mod_cast hD)
rw [hlaw s, hzero, Rat.cast_zero] at hpos
exact (lt_irrefl 0) hpos
theorem two_supported (d : β) (k : Fin 5) (counts : Fin 6 β β)
(hD : 0 < β s, counts s)
(hlaw : β s, (counts s : β) / (β s, counts s : β) =
(statisticLaw d k s : β)) (s : Fin 6) (hs : 0 < counts s) :
Fintype.card {w : Alphabet 2 k.val // twoStatistic w.val = s} =
AllFieldParameters.multiplicity s := by
apply two_fixedWeight_multiplicity
by_contra hweight
have hzero := statisticLaw_outside_support d k s hweight
have hpos : (0 : β) < (counts s : β) / (β s, counts s : β) :=
div_pos (by exact_mod_cast hs) (by exact_mod_cast hD)
rw [hlaw s, hzero, Rat.cast_zero] at hpos
exact (lt_irrefl 0) hpos
theorem four_card_pos (t : Shape) (counts : PairSlot β β)
(hD : 0 < β s, counts s)
(hlaw : β s, (counts s : β) / (β s, counts s : β) =
(zeroPairLaw t s.1 s.2 : β)) (m : β) :
0 < Fintype.card (fourWords t counts m) := by
apply ExactStatisticWords.card_pos_of_supported
(fun w : Alphabet 4 (shapeMax t) => fourStatistic w.val) (fun s => m * counts s)
(fun s => AllFieldParameters.multiplicity s.1 * AllFieldParameters.multiplicity s.2)
Β· intro s hs
exact four_supported t counts hD hlaw s (Nat.pos_of_mul_pos_left hs)
Β· intro s _
exact Nat.mul_pos (multiplicity_pos s.1) (multiplicity_pos s.2)
theorem two_card_pos (d : β) (k : Fin 5) (counts : Fin 6 β β)
(hD : 0 < β s, counts s)
(hlaw : β s, (counts s : β) / (β s, counts s : β) =
(statisticLaw d k s : β)) (m : β) :
0 < Fintype.card (twoWords k counts m) := by
have h := ExactStatisticWords.card_pos_of_supported
(fun w : Alphabet 2 k.val => twoStatistic w.val) (fun s => m * counts s)
AllFieldParameters.multiplicity
(fun s hs => two_supported d k counts hD hlaw s (Nat.pos_of_mul_pos_left hs))
(fun s _ => multiplicity_pos s)
rw [Fintype.card_eq_nat_card] at h β’
exact h
theorem four_tendsto_log_card (t : Shape) (counts : PairSlot β β)
(hD : 0 < β s, counts s)
(hlaw : β s, (counts s : β) / (β s, counts s : β) =
(zeroPairLaw t s.1 s.2 : β)) :
Tendsto (fun m : β => Real.log (Fintype.card (fourWords t counts m) : β) /
((m : β) * (β s, counts s : β))) atTop (π (fourRate t)) := by
have h := LeafRates.tendsto_log_card_supported
(fun w : Alphabet 4 (shapeMax t) => fourStatistic w.val) counts
(fun s => AllFieldParameters.multiplicity s.1 * AllFieldParameters.multiplicity s.2)
(four_supported t counts hD hlaw)
(fun s => Nat.mul_pos (multiplicity_pos s.1) (multiplicity_pos s.2)) hD
simpa only [fourWords, hlaw, fourRate] using h
theorem two_tendsto_log_card (d : β) (k : Fin 5) (counts : Fin 6 β β)
(hD : 0 < β s, counts s)
(hlaw : β s, (counts s : β) / (β s, counts s : β) =
(statisticLaw d k s : β)) :
Tendsto (fun m : β => Real.log (Fintype.card (twoWords k counts m) : β) /
((m : β) * (β s, counts s : β))) atTop (π (statisticRate d k)) := by
have h := LeafRates.tendsto_log_card_supported
(fun w : Alphabet 2 k.val => twoStatistic w.val) counts AllFieldParameters.multiplicity
(two_supported d k counts hD hlaw) multiplicity_pos hD
simp only [Fintype.card_eq_nat_card] at h β’
simpa only [twoWords, hlaw, statisticRate] using h
end MatrixMultiplication.AllFieldZeroLeafRates
end
end
end MatrixAllFields
end OAI
import Mathlib
import OAI.LinearAlgebra.MatrixFields.Entropy.InitialLeafRates
import OAI.LinearAlgebra.MatrixFields.Extraction.ComplexLabelHierarchySeparation
import OAI.LinearAlgebra.MatrixFields.Extraction.ComplexRationalTypes
namespace OAI
namespace MatrixAllFields
open scoped BigOperators Topology Polynomial
section
namespace MatrixMultiplication.Foundation
open Filter
open scoped Topology
noncomputable def factorialLogRemainder (n : β) : β :=
Real.log (Nat.factorial n : β) - ((n : β) * Real.log n - n)
theorem factorialLogRemainder_eq (n : β) :
factorialLogRemainder n =
Real.log (Nat.factorial n : β) - ((n : β) * Real.log n - n) := rfl
@[simp] theorem factorialLogRemainder_zero : factorialLogRemainder 0 = 0 := by
simp [factorialLogRemainder]
theorem factorialLogRemainder_stirling (n : β) :
factorialLogRemainder n =
Real.log (Stirling.stirlingSeq n) + (1 / 2 : β) * Real.log (2 * n) := by
cases n with
| zero => simp
| succ n =>
have h := Stirling.log_stirlingSeq_formula (n + 1)
rw [Real.log_div (by positivity) (Real.exp_ne_zero _), Real.log_exp] at h
unfold factorialLogRemainder
linarith
theorem tendsto_factorialLogRemainder_div :
Tendsto (fun n : β => factorialLogRemainder n / (n : β)) atTop (π 0) := by
have hnat : Tendsto (fun n : β => (n : β)) atTop atTop :=
tendsto_natCast_atTop_atTop
have hstirling :
Tendsto (fun n : β => Real.log (Stirling.stirlingSeq n) / (n : β))
atTop (π 0) :=
(Stirling.tendsto_stirlingSeq_sqrt_pi.log (by positivity)).div_atTop hnat
have hlog : Tendsto (fun n : β => Real.log (n : β) / (n : β))
atTop (π 0) := by
simpa only [Function.comp_def, id_eq] using
Real.isLittleO_log_id_atTop.tendsto_div_nhds_zero.comp hnat
have hconstant : Tendsto (fun n : β => Real.log 2 / (n : β))
atTop (π 0) := tendsto_const_nhds.div_atTop hnat
have hsum : Tendsto
(fun n : β => Real.log (Stirling.stirlingSeq n) / (n : β) +
(1 / 2 : β) * (Real.log 2 / (n : β) + Real.log (n : β) / (n : β)))
atTop (π 0) := by
simpa using hstirling.add ((hconstant.add hlog).const_mul (1 / 2 : β))
apply hsum.congr'
filter_upwards [eventually_ne_atTop (0 : β)] with n hn
rw [factorialLogRemainder_stirling,
Real.log_mul (by norm_num) (Nat.cast_ne_zero.mpr hn)]
ring
theorem tendsto_factorialLogRemainder_mul_div (c : β) :
Tendsto (fun t : β => factorialLogRemainder (t * c) / (t : β))
atTop (π 0) := by
by_cases hc : c = 0
Β· subst c
simp
have hmul : Tendsto (fun t : β => t * c) atTop atTop :=
tendsto_id.atTop_mul_const' (Nat.pos_of_ne_zero hc)
have hscaled : Tendsto
(fun t : β => factorialLogRemainder (t * c) / (t * c : β) * (c : β))
atTop (π 0) := by
simpa using (tendsto_factorialLogRemainder_div.comp hmul).mul_const (c : β)
apply hscaled.congr
intro t
by_cases ht : t = 0
Β· simp [ht]
rw [Nat.cast_mul]
field_simp [Nat.cast_ne_zero.mpr hc, Nat.cast_ne_zero.mpr ht]
theorem factorialLogRemainder_succ (n : β) :
factorialLogRemainder (n + 1) = factorialLogRemainder n + 1 -
(n : β) * (Real.log ((n : β) + 1) - Real.log n) := by
unfold factorialLogRemainder
rw [Nat.factorial_succ, Nat.cast_mul,
Real.log_mul (by positivity) (by positivity)]
push_cast
ring
private theorem nat_mul_log_increment_le_one (n : β) :
(n : β) * (Real.log ((n : β) + 1) - Real.log n) β€ 1 := by
by_cases hn : n = 0
Β· simp [hn]
have hpos : 0 < (n : β) := Nat.cast_pos.mpr (Nat.pos_of_ne_zero hn)
have hsucc : 0 < (n : β) + 1 := by positivity
have hlog := Real.log_le_sub_one_of_pos (div_pos hsucc hpos)
rw [Real.log_div hsucc.ne' hpos.ne'] at hlog
have hmul := mul_le_mul_of_nonneg_left hlog hpos.le