File size: 7,901 Bytes
cdec422 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | import Mathlib
import OAI.LinearAlgebra.MatrixFields.Extraction.ConditionalLabels
namespace OAI
namespace MatrixAllFields
open scoped BigOperators Topology Polynomial
noncomputable section
namespace MatrixMultiplication.ConditionalLabels
open MatrixMultiplication.Foundation Filter StageHierarchyResources HierarchySeparationRates
open LabelHierarchyCounts
open scoped BigOperators Topology Classical
universe u v
variable {A : Type u} [Fintype A] {Label : ℕ → Type v}
[∀ n, Fintype (Label n)]
def normalizedCountLaw (counts : A → ℕ) (hD : 0 < ∑ a, counts a) : FiniteLaw A where
mass a := (counts a : ℝ) / (∑ b, counts b : ℕ)
nonneg a := div_nonneg (Nat.cast_nonneg _) (Nat.cast_nonneg _)
total := by
rw [← Finset.sum_div, ← Nat.cast_sum]
exact div_self (Nat.cast_ne_zero.mpr (Nat.ne_of_gt hD))
@[simp] theorem normalizedCountLaw_mass (counts : A → ℕ)
(hD : 0 < ∑ a, counts a) (a : A) :
(normalizedCountLaw counts hD).mass a =
(counts a : ℝ) / (∑ b, counts b : ℕ) := rfl
theorem normalized_prefix_mass (counts : A → ℕ) (p : FiniteLaw A)
(mass : ∀ a, p.mass a = (counts a : ℝ) / (∑ b, counts b : ℕ))
(labels : ∀ n, A → Label n) (n : ℕ) (r : LabelRecord Label n) :
(p.map (labelRecordOf labels n)).mass r =
(sourcePrefixCounts counts labels n r : ℝ) / (∑ a, counts a : ℕ) := by
simp only [FiniteLaw.map_mass, mass, sourcePrefixCounts, pushforwardCounts,
Nat.cast_sum, Finset.sum_div]
apply Finset.sum_congr rfl
intro a _
by_cases h : labelRecordOf labels n a = r <;> simp [h]
theorem zero_prefix_contribution_of_count_eq_zero (counts : A → ℕ) (p : FiniteLaw A)
(mass : ∀ a, p.mass a = (counts a : ℝ) / (∑ b, counts b : ℕ))
(labels : ∀ n, A → Label n) (n : ℕ) (r : LabelRecord Label n)
(hzero : sourcePrefixCounts counts labels n r = 0) :
(p.map (labelRecordOf labels n)).mass r *
finiteEntropy ((p.conditional (labelRecordOf labels n) r).map (labels n)).mass = 0 := by
rw [normalized_prefix_mass counts p mass labels n r, hzero,
Nat.cast_zero, zero_div, zero_mul]
theorem tendsto_log_prefix_multinomial (counts : A → ℕ)
(hD : 0 < ∑ a, counts a) (p : FiniteLaw A)
(mass : ∀ a, p.mass a = (counts a : ℝ) / (∑ b, counts b : ℕ))
(labels : ∀ n, A → Label n) (n : ℕ) :
Tendsto (fun t : ℕ => Real.log
(Nat.multinomial Finset.univ
(sourcePrefixCounts (fun a => t * counts a) labels n) : ℝ) /
((t : ℝ) * (∑ a, counts a : ℕ))) atTop
(𝓝 (finiteEntropy (p.map (labelRecordOf labels n)).mass)) := by
have hsum : (∑ r, sourcePrefixCounts counts labels n r) = ∑ a, counts a :=
pushforwardCounts_sum counts (labelRecordOf labels n)
have hpositive : 0 < ∑ r, sourcePrefixCounts counts labels n r := by rwa [hsum]
have hmass : (fun r => (sourcePrefixCounts counts labels n r : ℝ) /
(∑ r, sourcePrefixCounts counts labels n r : ℕ)) =
(p.map (labelRecordOf labels n)).mass := by
funext r
rw [hsum]
exact (normalized_prefix_mass counts p mass labels n r).symm
have h := tendsto_log_multinomial_mul (sourcePrefixCounts counts labels n) hpositive
rw [hmass, hsum] at h
have hscale (t : ℕ) : sourcePrefixCounts (fun a => t * counts a) labels n =
(fun r => t * sourcePrefixCounts counts labels n r) :=
funext (sourcePrefixCounts_mul counts labels t n)
simpa only [hscale] using h
theorem stageEntropyRate_div_eq_refinementEntropy (counts : A → ℕ)
(hD : 0 < ∑ a, counts a) (p : FiniteLaw A)
(mass : ∀ a, p.mass a = (counts a : ℝ) / (∑ b, counts b : ℕ))
(labels : ∀ n, A → Label n) (depth n : ℕ) (hn : n < depth) :
stageEntropyRate counts labels n / (∑ a, counts a : ℕ) =
(FiniteLabelHierarchy.ofLawLabels p labels depth).refinementEntropy n := by
have hstage := (tendsto_log_stageRefinementCount_div counts labels n).div_const
((∑ a, counts a : ℕ) : ℝ)
simp only [div_div] at hstage
have hcoarse := tendsto_log_prefix_multinomial counts hD p mass labels n
have hfine := tendsto_log_prefix_multinomial counts hD p mass labels (n + 1)
have heq : finiteEntropy (p.map (labelRecordOf labels (n + 1))).mass =
finiteEntropy (p.map (labelRecordOf labels n)).mass +
stageEntropyRate counts labels n / (∑ a, counts a : ℕ) := by
apply tendsto_nhds_unique hfine
apply (hcoarse.add hstage).congr
intro t
rw [multinomial_succ (sourcePrefixCounts (fun a => t * counts a) labels) n
(sourcePrefixCounts_compatible (fun a => t * counts a) labels n)]
have hcoarse_pos : 0 < Nat.multinomial Finset.univ
(sourcePrefixCounts (fun a => t * counts a) labels n) := Nat.multinomial_pos _ _
have hrefinement_pos : 0 <
refinementCount (sourcePrefixCounts (fun a => t * counts a) labels) n :=
stageRefinementCount_pos counts labels n t
rw [Nat.cast_mul, Real.log_mul
(Nat.cast_ne_zero.mpr (Nat.ne_of_gt hcoarse_pos))
(Nat.cast_ne_zero.mpr (Nat.ne_of_gt hrefinement_pos)), add_div]
rfl
have hchain := (FiniteLabelHierarchy.ofLawLabels p labels depth).prefixLaw_entropy_succ
n (Nat.succ_le_of_lt hn)
rw [FiniteLabelHierarchy.ofLawLabels_prefixLaw_mass,
FiniteLabelHierarchy.ofLawLabels_prefixLaw_mass] at hchain
linarith
theorem stagePairingRate_div_eq_lawPairingRate (counts : A → ℕ)
(hD : 0 < ∑ a, counts a) (p : FiniteLaw A)
(mass : ∀ a, p.mass a = (counts a : ℝ) / (∑ b, counts b : ℕ))
(labels : ∀ n, A → Label n) (depth : ℕ)
(reader : Fin depth → ReaderPair) (pair : ReaderPair) :
stagePairingRate counts labels depth reader pair / (∑ a, counts a : ℕ) =
lawPairingRate p labels depth reader pair := by
unfold stagePairingRate lawPairingRate pairingRate
rw [Finset.sum_div]
apply Finset.sum_congr rfl
intro n _
by_cases h : reader n = pair
· simp only [h, ite_true]
exact stageEntropyRate_div_eq_refinementEntropy counts hD p mass labels depth n.val n.isLt
· simp [h]
theorem tendsto_log_stagePairingSize_law (counts : A → ℕ)
(hD : 0 < ∑ a, counts a) (p : FiniteLaw A)
(mass : ∀ a, p.mass a = (counts a : ℝ) / (∑ b, counts b : ℕ))
(labels : ∀ n, A → Label n) (depth : ℕ)
(reader : Fin depth → ReaderPair) (pair : ReaderPair) :
Tendsto (fun t => Real.log (stagePairingSize counts labels depth reader pair t : ℝ) /
((t : ℝ) * (∑ a, counts a : ℕ))) atTop
(𝓝 (lawPairingRate p labels depth reader pair)) := by
rw [← stagePairingRate_div_eq_lawPairingRate counts hD p mass labels depth reader pair]
exact tendsto_log_stagePairingSize_normalized counts labels depth reader pair
theorem stageEntropyRate_div_eq_normalizedCountLaw_refinementEntropy
(counts : A → ℕ) (hD : 0 < ∑ a, counts a)
(labels : ∀ n, A → Label n) (depth n : ℕ) (hn : n < depth) :
stageEntropyRate counts labels n / (∑ a, counts a : ℕ) =
(FiniteLabelHierarchy.ofLawLabels (normalizedCountLaw counts hD)
labels depth).refinementEntropy n :=
stageEntropyRate_div_eq_refinementEntropy counts hD (normalizedCountLaw counts hD)
(fun _ => rfl) labels depth n hn
theorem tendsto_log_stagePairingSize_normalizedCountLaw
(counts : A → ℕ) (hD : 0 < ∑ a, counts a)
(labels : ∀ n, A → Label n) (depth : ℕ)
(reader : Fin depth → ReaderPair) (pair : ReaderPair) :
Tendsto (fun t => Real.log (stagePairingSize counts labels depth reader pair t : ℝ) /
((t : ℝ) * (∑ a, counts a : ℕ))) atTop
(𝓝 (lawPairingRate (normalizedCountLaw counts hD) labels depth reader pair)) :=
tendsto_log_stagePairingSize_law counts hD (normalizedCountLaw counts hD)
(fun _ => rfl) labels depth reader pair
end MatrixMultiplication.ConditionalLabels
end
end MatrixAllFields
end OAI
|