math / data /lean /OAI /LinearAlgebra /MatrixMultiplication /JointExtraction /CompatibilityEntropyBound.lean
introvoyz045's picture
Migrated from GitHub
4a56cd6 verified
Raw History Blame Contribute Delete
10.8 kB
import OAI.LinearAlgebra.MatrixMultiplication.JointExtraction.CompatibilityIncidence
namespace OAI
/-! Joint tensor extraction, compatibility and entropy estimates. -/
noncomputable section
open MatrixMultiplication.Foundation
open MatrixMultiplication.JointTypeCounts MatrixMultiplication.JointCompatibilityIncidence
open scoped BigOperators
namespace MatrixMultiplication.JointCompatibilityEntropyBound
attribute [local instance] Classical.propDecidable
section ClassCounts
variable {C : Type*} [Fintype C] [DecidableEq C]
variable (Pos A : C β†’ Type*)
[βˆ€ c, Fintype (Pos c)] [βˆ€ c, DecidableEq (Pos c)]
[βˆ€ c, Fintype (A c)] [βˆ€ c, DecidableEq (A c)]
def classEntropy (counts : βˆ€ c, A c β†’ β„•) : ℝ :=
βˆ‘ c, (Fintype.card (Pos c) : ℝ) *
finiteEntropy (fun a => (counts c a : ℝ) / Fintype.card (Pos c))
def classTypeError : ℝ :=
βˆ‘ c, ((Fintype.card (A c) : ℝ) + 1) *
(1 + Real.log (Fintype.card (Pos c) + 1 : β„•))
omit [Fintype C] [DecidableEq C] [βˆ€ index, DecidableEq (Pos index)] in
theorem fixedClassWords_size (counts : βˆ€ c, A c β†’ β„•)
(w : FixedClassWords Pos A counts) (c : C) :
Fintype.card (Pos c) = βˆ‘ a, counts c a := by
calc
Fintype.card (Pos c) = βˆ‘ a, wordPopulation (w c).val a :=
(wordPopulation_sum (w c).val).symm
_ = βˆ‘ a, counts c a := Finset.sum_congr rfl fun a _ => (w c).property a
theorem abs_log_card_sub_classEntropy_le (counts : βˆ€ c, A c β†’ β„•)
(w : FixedClassWords Pos A counts) :
|Real.log (Fintype.card (FixedClassWords Pos A counts) : ℝ) -
classEntropy Pos A counts| ≀ classTypeError Pos A := by
have hsize := fixedClassWords_size Pos A counts w
simpa only [classEntropy, classTypeError, ← hsize] using
abs_log_fixedClassWords_card_sub_entropy_le Pos A counts hsize
theorem fixedClassWords_card_le_exp (counts : βˆ€ c, A c β†’ β„•)
(w : FixedClassWords Pos A counts) :
(Fintype.card (FixedClassWords Pos A counts) : ℝ) ≀
Real.exp (classEntropy Pos A counts + classTypeError Pos A) := by
have hpos : 0 < (Fintype.card (FixedClassWords Pos A counts) : ℝ) := by
exact_mod_cast (Fintype.card_pos_iff.mpr ⟨w⟩)
have h := (abs_le.mp (abs_log_card_sub_classEntropy_le Pos A counts w)).2
calc
(Fintype.card (FixedClassWords Pos A counts) : ℝ) =
Real.exp (Real.log (Fintype.card (FixedClassWords Pos A counts) : ℝ)) :=
(Real.exp_log hpos).symm
_ ≀ Real.exp (classEntropy Pos A counts + classTypeError Pos A) :=
Real.exp_le_exp.mpr (by linarith)
theorem exp_le_fixedClassWords_card (counts : βˆ€ c, A c β†’ β„•)
(w : FixedClassWords Pos A counts) :
Real.exp (classEntropy Pos A counts - classTypeError Pos A) ≀
(Fintype.card (FixedClassWords Pos A counts) : ℝ) := by
have hpos : 0 < (Fintype.card (FixedClassWords Pos A counts) : ℝ) := by
exact_mod_cast (Fintype.card_pos_iff.mpr ⟨w⟩)
have h := (abs_le.mp (abs_log_card_sub_classEntropy_le Pos A counts w)).1
calc
Real.exp (classEntropy Pos A counts - classTypeError Pos A) ≀
Real.exp (Real.log (Fintype.card (FixedClassWords Pos A counts) : ℝ)) :=
Real.exp_le_exp.mpr (by linarith)
_ = (Fintype.card (FixedClassWords Pos A counts) : ℝ) := Real.exp_log hpos
end ClassCounts
section GroupCounts
def wordTypeError (I A : Type*) [Fintype I] [Fintype A] : ℝ :=
((Fintype.card A : ℝ) + 1) * (1 + Real.log (Fintype.card I + 1 : β„•)) +
(Fintype.card A : ℝ) * Real.log (Fintype.card I + 1 : β„•)
theorem wordEntropyBound_eq_exp (I A : Type*) [Fintype I] [Fintype A] (H : ℝ) :
wordEntropyBound I A H =
Real.exp ((Fintype.card I : ℝ) * H + wordTypeError I A) := by
unfold wordEntropyBound wordTypeError
rw [show (Fintype.card I : ℝ) * H +
(((Fintype.card A : ℝ) + 1) * (1 + Real.log (Fintype.card I + 1 : β„•)) +
(Fintype.card A : ℝ) * Real.log (Fintype.card I + 1 : β„•)) =
(Fintype.card A : ℝ) * Real.log (Fintype.card I + 1 : β„•) +
((Fintype.card I : ℝ) * H + ((Fintype.card A : ℝ) + 1) *
(1 + Real.log (Fintype.card I + 1 : β„•))) by ring]
conv_rhs => rw [Real.exp_add, Real.exp_nat_mul, Real.exp_log (by positivity)]
simp only [Nat.cast_add, Nat.cast_one]
variable {C : Type*} [Fintype C] [DecidableEq C]
variable (Pos Shape A : C β†’ Type*)
[βˆ€ c, Fintype (Pos c)] [βˆ€ c, DecidableEq (Pos c)]
[βˆ€ c, Fintype (Shape c)] [βˆ€ c, DecidableEq (Shape c)]
[βˆ€ c, Fintype (A c)] [βˆ€ c, DecidableEq (A c)]
variable (counts : βˆ€ c, Shape c β†’ β„•)
(designated : βˆ€ c, Shape c β†’ Prop)
(H : (Ξ£ c, Option (Shape c)) β†’ ℝ)
(w : FixedClassWords Pos Shape counts)
def groupedEntropy : ℝ :=
βˆ‘ g : Ξ£ c, Option (Shape c),
(Fintype.card (GroupPos (w g.1).val (designated g.1) g.2) : ℝ) * H g
def groupedTypeError : ℝ :=
βˆ‘ g : Ξ£ c, Option (Shape c),
wordTypeError (GroupPos (w g.1).val (designated g.1) g.2) (A g.1)
omit [DecidableEq C] [βˆ€ index, DecidableEq (Pos index)]
[βˆ€ index, DecidableEq (A index)] in
theorem prod_wordEntropyBound_eq_exp :
(∏ g : Σ c, Option (Shape c), wordEntropyBound
(GroupPos (w g.1).val (designated g.1) g.2) (A g.1) (H g)) =
Real.exp (groupedEntropy Pos Shape counts designated H w +
groupedTypeError Pos Shape A counts designated w) := by
simp only [wordEntropyBound_eq_exp, groupedEntropy, groupedTypeError,
← Real.exp_sum, Finset.sum_add_distrib]
end GroupCounts
section Compatibility
variable {C : Type*} [Fintype C] [DecidableEq C]
variable (Pos Shape Pair Left Right : C β†’ Type*)
[βˆ€ c, Fintype (Pos c)] [βˆ€ c, DecidableEq (Pos c)]
[βˆ€ c, Fintype (Shape c)] [βˆ€ c, DecidableEq (Shape c)]
[βˆ€ c, Fintype (Pair c)] [βˆ€ c, DecidableEq (Pair c)]
[βˆ€ c, Fintype (Left c)] [βˆ€ c, DecidableEq (Left c)]
[βˆ€ c, Fintype (Right c)] [βˆ€ c, DecidableEq (Right c)]
variable (shapeCounts : βˆ€ c, Shape c β†’ β„•) (pairCounts : βˆ€ c, Pair c β†’ β„•)
(leftPart : βˆ€ c, Pair c β†’ Left c) (rightPart : βˆ€ c, Pair c β†’ Right c)
(designatedLeft designatedRight : βˆ€ c, Shape c β†’ Prop)
(leftLaw : βˆ€ c, Shape c β†’ Left c β†’ ℝ)
(rightLaw : βˆ€ c, Shape c β†’ Right c β†’ ℝ) (Ο‡ : ℝ)
def compatibilityExponent (HL HR : (Ξ£ c, Option (Shape c)) β†’ ℝ)
(w : FixedClassWords Pos Shape shapeCounts) : ℝ :=
classEntropy Pos Shape shapeCounts - classEntropy Pos Pair pairCounts +
groupedEntropy Pos Shape shapeCounts designatedLeft HL w +
groupedEntropy Pos Shape shapeCounts designatedRight HR w +
classTypeError Pos Shape + classTypeError Pos Pair +
groupedTypeError Pos Shape Left shapeCounts designatedLeft w +
groupedTypeError Pos Shape Right shapeCounts designatedRight w
theorem compatibleCandidateCount_le_exp
(w : FixedClassWords Pos Shape shapeCounts)
(z : FixedClassWords Pos Pair pairCounts)
(hpair : βˆ€ c, Function.Injective (fun s : Pair c => (leftPart c s, rightPart c s)))
(HL HR : (Ξ£ c, Option (Shape c)) β†’ ℝ)
(controlLeft : SideEntropyControl Pos Shape Pair Left shapeCounts pairCounts
leftPart designatedLeft leftLaw Ο‡ HL w)
(controlRight : SideEntropyControl Pos Shape Pair Right shapeCounts pairCounts
rightPart designatedRight rightLaw Ο‡ HR w) :
(compatibleCandidateCount Pos Shape Pair Left Right shapeCounts pairCounts
leftPart rightPart designatedLeft designatedRight leftLaw rightLaw Ο‡ z : ℝ) ≀
Real.exp (compatibilityExponent Pos Shape Pair Left Right shapeCounts pairCounts
designatedLeft designatedRight HL HR w) := by
have h := compatibleCandidateCount_le_of_controls Pos Shape Pair Left Right
shapeCounts pairCounts leftPart rightPart designatedLeft designatedRight
leftLaw rightLaw Ο‡ w z hpair HL HR controlLeft controlRight
rw [prod_wordEntropyBound_eq_exp, prod_wordEntropyBound_eq_exp] at h
let E := groupedEntropy Pos Shape shapeCounts designatedLeft HL w +
groupedTypeError Pos Shape Left shapeCounts designatedLeft w +
(groupedEntropy Pos Shape shapeCounts designatedRight HR w +
groupedTypeError Pos Shape Right shapeCounts designatedRight w)
rw [← Real.exp_add] at h
have hnum : (Fintype.card (FixedClassWords Pos Shape shapeCounts) : ℝ) * Real.exp E ≀
Real.exp (classEntropy Pos Shape shapeCounts + classTypeError Pos Shape + E) := by
conv_rhs => rw [Real.exp_add]
exact mul_le_mul_of_nonneg_right
(fixedClassWords_card_le_exp Pos Shape shapeCounts w) (Real.exp_pos E).le
calc
(compatibleCandidateCount Pos Shape Pair Left Right shapeCounts pairCounts
leftPart rightPart designatedLeft designatedRight leftLaw rightLaw Ο‡ z : ℝ) ≀
(Fintype.card (FixedClassWords Pos Shape shapeCounts) : ℝ) * Real.exp E /
Fintype.card (FixedClassWords Pos Pair pairCounts) := h
_ ≀ Real.exp (classEntropy Pos Shape shapeCounts + classTypeError Pos Shape + E) /
Real.exp (classEntropy Pos Pair pairCounts - classTypeError Pos Pair) :=
div_le_divβ‚€ (Real.exp_pos _).le hnum (Real.exp_pos _)
(exp_le_fixedClassWords_card Pos Pair pairCounts z)
_ = Real.exp (compatibilityExponent Pos Shape Pair Left Right shapeCounts pairCounts
designatedLeft designatedRight HL HR w) := by
rw [← Real.exp_sub]
congr 1
dsimp [E, compatibilityExponent]
ring
theorem log_compatibleCandidateCount_le
(w : FixedClassWords Pos Shape shapeCounts)
(z : FixedClassWords Pos Pair pairCounts)
(hpair : βˆ€ c, Function.Injective (fun s : Pair c => (leftPart c s, rightPart c s)))
(HL HR : (Ξ£ c, Option (Shape c)) β†’ ℝ)
(controlLeft : SideEntropyControl Pos Shape Pair Left shapeCounts pairCounts
leftPart designatedLeft leftLaw Ο‡ HL w)
(controlRight : SideEntropyControl Pos Shape Pair Right shapeCounts pairCounts
rightPart designatedRight rightLaw Ο‡ HR w)
(hpos : 0 < compatibleCandidateCount Pos Shape Pair Left Right shapeCounts pairCounts
leftPart rightPart designatedLeft designatedRight leftLaw rightLaw Ο‡ z) :
Real.log (compatibleCandidateCount Pos Shape Pair Left Right shapeCounts pairCounts
leftPart rightPart designatedLeft designatedRight leftLaw rightLaw Ο‡ z : ℝ) ≀
compatibilityExponent Pos Shape Pair Left Right shapeCounts pairCounts
designatedLeft designatedRight HL HR w := by
have h := Real.log_le_log (Nat.cast_pos.mpr hpos)
(compatibleCandidateCount_le_exp Pos Shape Pair Left Right shapeCounts pairCounts
leftPart rightPart designatedLeft designatedRight leftLaw rightLaw Ο‡
w z hpair HL HR controlLeft controlRight)
simpa only [Real.log_exp] using h
end Compatibility
end MatrixMultiplication.JointCompatibilityEntropyBound
end
end OAI