math / data /lean /OAI /LinearAlgebra /MatrixFields /Entropy /JointAmbientRateLimit.lean
introvoyz045's picture
Migrated from GitHub
4a56cd6 verified
Raw History Blame Contribute Delete
7.07 kB
import Mathlib
import OAI.LinearAlgebra.MatrixFields.Construction.JointMarginalSupport
namespace OAI
namespace MatrixAllFields
open scoped BigOperators Topology Polynomial
section
noncomputable section
namespace MatrixMultiplication.JointAmbientRateLimit
open MatrixMultiplication.Foundation JointPopulation JointAmbientDegree Filter
open scoped BigOperators Topology
attribute [local instance] Classical.propDecidable
theorem tendsto_log_succ_div_nat_of_ratio {n : β„• β†’ β„•} {mass : ℝ}
(hsize : Tendsto (fun N => (n N : ℝ) / (N : ℝ)) atTop (𝓝 mass)) :
Tendsto (fun N => Real.log (n N + 1 : β„•) / (N : ℝ)) atTop (𝓝 0) := by
let C : ℝ := |mass| + 2
have hC : 0 < C := by dsimp [C]; positivity
have hmC : mass < C := by dsimp [C]; linarith [le_abs_self mass]
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 tendsto_natCast_atTop_atTop
have hc : Tendsto (fun N : β„• => Real.log (C + 1) / (N : ℝ)) atTop (𝓝 0) :=
tendsto_const_nhds.div_atTop tendsto_natCast_atTop_atTop
have hu : Tendsto (fun N : β„• =>
(Real.log (C + 1) + Real.log (N : ℝ)) / (N : ℝ)) atTop (𝓝 0) := by
simpa only [add_div, add_zero] using hc.add hlog
apply tendsto_of_tendsto_of_tendsto_of_le_of_le' tendsto_const_nhds hu
Β· exact Eventually.of_forall fun N =>
div_nonneg (Real.log_nonneg (by exact_mod_cast Nat.succ_le_succ (Nat.zero_le (n N))))
(Nat.cast_nonneg _)
Β· filter_upwards [(tendsto_order.1 hsize).2 C hmC,
eventually_ge_atTop (1 : β„•)] with N hn hN
have hNr : (1 : ℝ) ≀ N := by exact_mod_cast hN
have hNp : (0 : ℝ) < N := lt_of_lt_of_le zero_lt_one hNr
have hsizeN := (div_lt_iffβ‚€ hNp).mp hn
have hbound : (n N + 1 : β„•) ≀ (C + 1) * (N : ℝ) := by
push_cast
nlinarith
have hl := Real.log_le_log (by positivity : (0 : ℝ) < (n N + 1 : β„•)) hbound
rw [Real.log_mul (by positivity : C + 1 β‰  0) hNp.ne'] at hl
exact div_le_div_of_nonneg_right hl hNp.le
variable {H : Type*} [Fintype H] [DecidableEq H]
omit [Fintype H] [DecidableEq H] in
theorem tendsto_classExponent_div_nat
(counts : β„• β†’ H β†’ Shape β†’ β„•) (s : Fin 3) (h : H)
(cap : β„• β†’ ℝ) (mass capRate marginalRate : ℝ)
(hsize : Tendsto (fun N => (βˆ‘ a, counts N h a : β„•) / (N : ℝ))
atTop (𝓝 mass))
(hcap : Tendsto (fun N => ((βˆ‘ a, counts N h a : β„•) / (N : ℝ)) * cap N)
atTop (𝓝 capRate))
(hmarginal : Tendsto (fun N =>
((βˆ‘ a, counts N h a : β„•) / (N : ℝ)) *
finiteEntropy (fun a => (wordPopulation
(sideWord (counts N) s (triple (counts N) (canonicalTarget (counts N))) h) a : ℝ) /
Fintype.card (Positions (counts N) h))) atTop (𝓝 marginalRate)) :
Tendsto (fun N =>
classExponent (counts N) s (triple (counts N) (canonicalTarget (counts N))) h (cap N) /
(N : ℝ)) atTop (𝓝 (capRate - marginalRate)) := by
let A : ℝ := (Fintype.card Shape : ℝ) + (Fintype.card (Fin 17) : ℝ) + 2
have hlog := tendsto_log_succ_div_nat_of_ratio hsize
have hinv : Tendsto (fun N : β„• => (1 : ℝ) / N) atTop (𝓝 0) :=
tendsto_const_nhds.div_atTop tendsto_natCast_atTop_atTop
have htotal := (hcap.sub hmarginal).add
(((hinv.add hlog).const_mul A).add (hlog.const_mul (Fintype.card Shape : ℝ)))
simp only [add_zero, mul_zero] at htotal
convert htotal using 1
ext N
dsimp only [classExponent, A, Positions]
simp only [Fintype.card_fin, div_eq_mul_inv]
ring
omit [DecidableEq H] in
theorem tendsto_sum_classExponent_div_nat
(counts : β„• β†’ H β†’ Shape β†’ β„•) (s : Fin 3)
(cap : β„• β†’ H β†’ ℝ) (mass capRate marginalRate : H β†’ ℝ)
(hsize : βˆ€ h, Tendsto (fun N => (βˆ‘ a, counts N h a : β„•) / (N : ℝ))
atTop (𝓝 (mass h)))
(hcap : βˆ€ h, Tendsto (fun N => ((βˆ‘ a, counts N h a : β„•) / (N : ℝ)) * cap N h)
atTop (𝓝 (capRate h)))
(hmarginal : βˆ€ h, Tendsto (fun N =>
((βˆ‘ a, counts N h a : β„•) / (N : ℝ)) *
finiteEntropy (fun a => (wordPopulation
(sideWord (counts N) s (triple (counts N) (canonicalTarget (counts N))) h) a : ℝ) /
Fintype.card (Positions (counts N) h))) atTop (𝓝 (marginalRate h))) :
Tendsto (fun N => (βˆ‘ h,
classExponent (counts N) s (triple (counts N) (canonicalTarget (counts N))) h (cap N h)) /
(N : ℝ)) atTop (𝓝 (βˆ‘ h, (capRate h - marginalRate h))) := by
have hs := tendsto_finsetSum Finset.univ (fun h _ =>
tendsto_classExponent_div_nat counts s h (fun N => cap N h)
(mass h) (capRate h) (marginalRate h) (hsize h) (hcap h) (hmarginal h))
simpa only [Finset.sum_div] using hs
theorem eventually_all_target_degree_upper
(counts : β„• β†’ H β†’ Shape β†’ β„•) (sum : H β†’ β„•)
(cap : β„• β†’ H β†’ ℝ) (sideRate : Fin 3 β†’ ℝ) (D : ℝ) (hD : 0 ≀ D)
(hentropy : βˆ€αΆ  N in atTop, βˆ€ h w, localAllowed (counts N) sum h w β†’
finiteEntropy (fun a => (wordPopulation w a : ℝ) /
Fintype.card (Positions (counts N) h)) ≀ cap N h)
(hrate : βˆ€ s, Tendsto (fun N => (βˆ‘ h,
classExponent (counts N) s (triple (counts N) (canonicalTarget (counts N))) h (cap N h)) /
(N : ℝ)) atTop (𝓝 (sideRate s)))
(hside : βˆ€ s, sideRate s ≀ D) (Ξ· : ℝ) (hΞ· : 0 < Ξ·) :
βˆ€αΆ  N in atTop, βˆ€ e : Target (counts N),
Real.log ((neighbors (counts N) sum (triple (counts N) e)).card : ℝ) / (N : ℝ) ≀
D + Ξ· := by
have hs : βˆ€αΆ  N in atTop, βˆ€ s : Fin 3,
(βˆ‘ h, classExponent (counts N) s
(triple (counts N) (canonicalTarget (counts N))) h (cap N h)) / (N : ℝ) ≀
D + Ξ· / 2 := by
rw [Filter.eventually_all]
intro s
filter_upwards [(tendsto_order.1 (hrate s)).2 (sideRate s + Ξ· / 2) (by linarith)]
with N hN
linarith [hside s]
have hc : Tendsto (fun N : β„• => Real.log 3 / (N : ℝ)) atTop (𝓝 0) :=
tendsto_const_nhds.div_atTop tendsto_natCast_atTop_atTop
filter_upwards [hentropy, hs, (tendsto_order.1 hc).2 (Ξ· / 2) (by linarith),
eventually_ge_atTop (1 : β„•)] with N he hs hc hN
intro e
by_cases hz : (neighbors (counts N) sum (triple (counts N) e)).card = 0
Β· simp only [hz, Nat.cast_zero, Real.log_zero, zero_div]
linarith
have hNp : (0 : ℝ) < N := by exact_mod_cast hN
have hupper (s : Fin 3) :
(βˆ‘ h, classExponent (counts N) s (triple (counts N) e) h (cap N h)) ≀
(N : ℝ) * (D + Ξ· / 2) := by
simp_rw [classExponent_target_eq]
simpa only [mul_comm] using (div_le_iffβ‚€ hNp).mp (hs s)
have hb := div_le_div_of_nonneg_right
(log_neighbors_card_le (counts N) sum (triple (counts N) e) (cap N) he
((N : ℝ) * (D + Ξ· / 2)) hupper (Nat.pos_of_ne_zero hz)) hNp.le
rw [add_div, mul_div_cancel_leftβ‚€ _ hNp.ne'] at hb
linarith
end MatrixMultiplication.JointAmbientRateLimit
end
end
end MatrixAllFields
end OAI