File size: 15,784 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import OAI.LinearAlgebra.MatrixMultiplication.JointExtraction.AmbientDegree
import OAI.LinearAlgebra.MatrixMultiplication.JointExtraction.GibbsApproximation
import OAI.LinearAlgebra.MatrixMultiplication.Recovery.InheritedStatisticWindows

namespace OAI

/-! Joint tensor extraction, compatibility and entropy estimates. -/

noncomputable section

open scoped BigOperators Topology
open Filter MatrixMultiplication.Foundation
open MatrixMultiplication.JointPopulation MatrixMultiplication.JointAmbientDegree
open MatrixMultiplication.JointGibbsApproximation

namespace MatrixMultiplication.JointAmbientEntropy

section Empirical

variable {I A B : Type*} [Fintype I] [DecidableEq I]
  [Fintype A] [DecidableEq A] [Fintype B] [DecidableEq B]

def empiricalFiniteLaw (w : I → A) (hpos : 0 < Fintype.card I) : FiniteLaw A where
  mass a := (wordPopulation w a : ℝ) / Fintype.card I
  nonneg a := div_nonneg (Nat.cast_nonneg _) (Nat.cast_nonneg _)
  total := by
    rw [← Finset.sum_div, ← Nat.cast_sum, wordPopulation_sum]
    exact div_self (Nat.cast_ne_zero.mpr (Nat.ne_of_gt hpos))

omit [DecidableEq I] in
@[simp] theorem empiricalFiniteLaw_mass (w : I → A) (hpos : 0 < Fintype.card I) (a : A) :
    (empiricalFiniteLaw w hpos).mass a =
      (wordPopulation w a : ℝ) / Fintype.card I := rfl

omit [DecidableEq I] in
theorem empiricalFiniteLaw_map_mass (w : I → A) (hpos : 0 < Fintype.card I)
    (f : A → B) (b : B) :
    ((empiricalFiniteLaw w hpos).map f).mass b =
      (wordPopulation (f ∘ w) b : ℝ) / Fintype.card I := by
  rw [FiniteLaw.map_mass, InheritedMasks.wordPopulation_statistic,
    Nat.cast_sum, Finset.sum_div, Finset.sum_filter]
  simp only [empiricalFiniteLaw_mass]

omit [DecidableEq I] in
theorem empiricalFiniteLaw_supported (w : I → A) (hpos : 0 < Fintype.card I)
    (p : FiniteLaw A) (hsupport : ∀ i, 0 < p.mass (w i)) :
    ∀ a, p.mass a = 0 → (empiricalFiniteLaw w hpos).mass a = 0 := by
  intro a ha
  have : IsEmpty {i : I // w i = a} := ⟨fun i => by
    have hp := hsupport i.val
    rw [i.property, ha] at hp
    exact lt_irrefl 0 hp⟩
  simp only [empiricalFiniteLaw_mass, wordPopulation, Fintype.card_of_isEmpty,
    Nat.cast_zero, zero_div]

end Empirical

variable {H : Type*} [Fintype H] [DecidableEq H]

def canonicalLaw (counts : H → Shape → ℕ) (h : H) : FiniteLaw Shape :=
  if hpos : 0 < Fintype.card (Positions counts h) then
    empiricalFiniteLaw ((canonicalTarget counts) h).val hpos
  else FiniteLaw.pure (0, 0, 0)

omit [Fintype H] [DecidableEq H] in
theorem canonicalLaw_mass_of_pos (counts : H → Shape → ℕ) (h : H)
    (hpos : 0 < Fintype.card (Positions counts h)) (a : Shape) :
    (canonicalLaw counts h).mass a =
      (counts h a : ℝ) / Fintype.card (Positions counts h) := by
  rw [canonicalLaw, dite_eq_left hpos, empiricalFiniteLaw_mass,
    ((canonicalTarget counts) h).property]

omit [Fintype H] [DecidableEq H] in
theorem total_mul_canonicalLaw_mass (counts : H → Shape → ℕ) (h : H) (a : Shape) :
    (Fintype.card (Positions counts h) : ℝ) * (canonicalLaw counts h).mass a =
      counts h a := by
  by_cases hpos : 0 < Fintype.card (Positions counts h)
  · rw [canonicalLaw_mass_of_pos counts h hpos]
    exact mul_div_cancel₀ _ (Nat.cast_ne_zero.mpr (Nat.ne_of_gt hpos))
  · have hzero : Fintype.card (Positions counts h) = 0 := Nat.eq_zero_of_not_pos hpos
    have hsum : ∑ a, counts h a = 0 := by simpa only [Positions, Fintype.card_fin] using hzero
    have ha : counts h a = 0 := by
      have hle : counts h a ≤ ∑ b, counts h b :=
        Finset.single_le_sum (fun _ _ => Nat.zero_le _) (Finset.mem_univ a)
      rw [hsum] at hle
      exact Nat.eq_zero_of_le_zero hle
    simp only [hzero, ha, Nat.cast_zero, zero_mul]

omit [Fintype H] [DecidableEq H] in
theorem localAllowed_marginal_eq (counts : H → Shape → ℕ) (sum : H → ℕ)
    (h : H) (w : Positions counts h → Shape)
    (hpos : 0 < Fintype.card (Positions counts h))
    (hw : localAllowed counts sum h w) (s : Fin 3) :
    ((empiricalFiniteLaw w hpos).map (shapeSide s)).mass =
      ((canonicalLaw counts h).map (shapeSide s)).mass := by
  rw [canonicalLaw, dite_eq_left hpos]
  funext a
  rw [empiricalFiniteLaw_map_mass, empiricalFiniteLaw_map_mass]
  exact congrArg (fun n : ℕ => (n : ℝ) / Fintype.card (Positions counts h)) (hw.2 s a)

def gibbsCap (counts : H → Shape → ℕ) (h : H) (p : FiniteLaw Shape)
    (u v w : Fin 17 → ℝ) : ℝ :=
  finiteEntropy p.mass + marginalError p (canonicalLaw counts h) (shapeSide 0) u +
    marginalError p (canonicalLaw counts h) (shapeSide 1) v +
    marginalError p (canonicalLaw counts h) (shapeSide 2) w

omit [Fintype H] [DecidableEq H] in
theorem gibbsCap_nonneg (counts : H → Shape → ℕ) (h : H) (p : FiniteLaw Shape)
    (u v w : Fin 17 → ℝ) : 0 ≤ gibbsCap counts h p u v w := by
  exact add_nonneg
    (add_nonneg (add_nonneg p.entropy_nonneg (marginalError_nonneg _ _ _ _))
      (marginalError_nonneg _ _ _ _)) (marginalError_nonneg _ _ _ _)

omit [Fintype H] [DecidableEq H] in
theorem localAllowed_entropy_le_of_word_support (counts : H → Shape → ℕ) (sum : H → ℕ)
    (h : H) (w : Positions counts h → Shape) (p : FiniteLaw Shape)
    (u v z : Fin 17 → ℝ) (c : ℝ)
    (hsupport : ∀ i, 0 < p.mass (w i))
    (hlog : ∀ a, 0 < p.mass a → Real.log (p.mass a) =
      u (shapeSide 0 a) + v (shapeSide 1 a) + z (shapeSide 2 a) - c)
    (hw : localAllowed counts sum h w) :
    finiteEntropy (fun a => (wordPopulation w a : ℝ) /
      Fintype.card (Positions counts h)) ≤ gibbsCap counts h p u v z := by
  by_cases hpos : 0 < Fintype.card (Positions counts h)
  · have hs := empiricalFiniteLaw_supported w hpos p hsupport
    have hg := entropy_le_of_marginal_errors_of_support p (empiricalFiniteLaw w hpos)
      (shapeSide 0) (shapeSide 1) (shapeSide 2) u v z c hs hlog
    have hx := localAllowed_marginal_eq counts sum h w hpos hw (0 : Fin 3)
    have hy := localAllowed_marginal_eq counts sum h w hpos hw (1 : Fin 3)
    have hz := localAllowed_marginal_eq counts sum h w hpos hw (2 : Fin 3)
    have hmass : (empiricalFiniteLaw w hpos).mass =
        (fun a => (wordPopulation w a : ℝ) / Fintype.card (Positions counts h)) := by
      funext a
      exact empiricalFiniteLaw_mass w hpos a
    simpa only [gibbsCap, marginalError, empiricalFiniteLaw_mass, hmass, hx, hy, hz] using hg
  · have hzero : Fintype.card (Positions counts h) = 0 := Nat.eq_zero_of_not_pos hpos
    simpa only [hzero, Nat.cast_zero, div_zero, finiteEntropy, entropyTerm_zero,
      Finset.sum_const_zero] using gibbsCap_nonneg counts h p u v z

omit [Fintype H] [DecidableEq H] in
theorem localAllowed_entropy_le (counts : H → Shape → ℕ) (sum : H → ℕ)
    (h : H) (w : Positions counts h → Shape) (p : FiniteLaw Shape)
    (u v z : Fin 17 → ℝ) (c : ℝ)
    (hsupport : ∀ a : Shape, a.1.val + a.2.1.val + a.2.2.val = sum h → 0 < p.mass a)
    (hlog : ∀ a, 0 < p.mass a → Real.log (p.mass a) =
      u (shapeSide 0 a) + v (shapeSide 1 a) + z (shapeSide 2 a) - c)
    (hw : localAllowed counts sum h w) :
    finiteEntropy (fun a => (wordPopulation w a : ℝ) /
      Fintype.card (Positions counts h)) ≤ gibbsCap counts h p u v z :=
  localAllowed_entropy_le_of_word_support counts sum h w p u v z c
    (fun i => hsupport (w i) (hw.1 i)) hlog hw

theorem sharedAmbient_card_le_of_supported_gibbs (counts : H → Shape → ℕ) (sum : H → ℕ)
    (s : Fin 3) (e : JointCoarseHashing.Triple (Position counts))
    (p : H → FiniteLaw Shape) (u v w : H → Fin 17 → ℝ) (c : H → ℝ)
    (hsupport : ∀ h word, localAllowed counts sum h word → ∀ i, 0 < (p h).mass (word i))
    (hlog : ∀ h a, 0 < (p h).mass a → Real.log ((p h).mass a) =
      u h (shapeSide 0 a) + v h (shapeSide 1 a) + w h (shapeSide 2 a) - c h) :
    ((sharedAmbient counts sum s e).card : ℝ) ≤
      ∏ h, classBound counts s e h (gibbsCap counts h (p h) (u h) (v h) (w h)) :=
  sharedAmbient_card_le counts sum s e _
    (fun h word hw => localAllowed_entropy_le_of_word_support counts sum h word (p h)
      (u h) (v h) (w h) (c h) (hsupport h word hw) (hlog h) hw)

theorem neighbors_card_le_of_supported_gibbs (counts : H → Shape → ℕ) (sum : H → ℕ)
    (e : JointCoarseHashing.Triple (Position counts))
    (p : H → FiniteLaw Shape) (u v w : H → Fin 17 → ℝ) (c : H → ℝ)
    (hsupport : ∀ h word, localAllowed counts sum h word → ∀ i, 0 < (p h).mass (word i))
    (hlog : ∀ h a, 0 < (p h).mass a → Real.log ((p h).mass a) =
      u h (shapeSide 0 a) + v h (shapeSide 1 a) + w h (shapeSide 2 a) - c h) :
    ((neighbors counts sum e).card : ℝ) ≤
      ∑ s : Fin 3, ∏ h, classBound counts s e h
        (gibbsCap counts h (p h) (u h) (v h) (w h)) :=
  neighbors_card_le counts sum e _
    (fun h word hw => localAllowed_entropy_le_of_word_support counts sum h word (p h)
      (u h) (v h) (w h) (c h) (hsupport h word hw) (hlog h) hw)

theorem sharedAmbient_card_le_of_gibbs (counts : H → Shape → ℕ) (sum : H → ℕ)
    (s : Fin 3) (e : JointCoarseHashing.Triple (Position counts))
    (p : H → FiniteLaw Shape) (u v w : H → Fin 17 → ℝ) (c : H → ℝ)
    (hsupport : ∀ h (a : Shape), a.1.val + a.2.1.val + a.2.2.val = sum h → 0 < (p h).mass a)
    (hlog : ∀ h a, 0 < (p h).mass a → Real.log ((p h).mass a) =
      u h (shapeSide 0 a) + v h (shapeSide 1 a) + w h (shapeSide 2 a) - c h) :
    ((sharedAmbient counts sum s e).card : ℝ) ≤
      ∏ h, classBound counts s e h (gibbsCap counts h (p h) (u h) (v h) (w h)) :=
  sharedAmbient_card_le counts sum s e _
    (fun h word hw => localAllowed_entropy_le counts sum h word (p h)
      (u h) (v h) (w h) (c h) (hsupport h) (hlog h) hw)

theorem neighbors_card_le_of_gibbs (counts : H → Shape → ℕ) (sum : H → ℕ)
    (e : JointCoarseHashing.Triple (Position counts))
    (p : H → FiniteLaw Shape) (u v w : H → Fin 17 → ℝ) (c : H → ℝ)
    (hsupport : ∀ h (a : Shape), a.1.val + a.2.1.val + a.2.2.val = sum h → 0 < (p h).mass a)
    (hlog : ∀ h a, 0 < (p h).mass a → Real.log ((p h).mass a) =
      u h (shapeSide 0 a) + v h (shapeSide 1 a) + w h (shapeSide 2 a) - c h) :
    ((neighbors counts sum e).card : ℝ) ≤
      ∑ s : Fin 3, ∏ h, classBound counts s e h
        (gibbsCap counts h (p h) (u h) (v h) (w h)) :=
  neighbors_card_le counts sum e _
    (fun h word hw => localAllowed_entropy_le counts sum h word (p h)
      (u h) (v h) (w h) (c h) (hsupport h) (hlog h) hw)

def classScale (counts : H → Shape → ℕ) (h : H) (N : ℕ) : ℝ :=
  (Fintype.card (Positions counts h) : ℝ) / N

omit [Fintype H] [DecidableEq H] in
theorem classScale_nonneg (counts : H → Shape → ℕ) (h : H) (N : ℕ) :
    0 ≤ classScale counts h N := div_nonneg (Nat.cast_nonneg _) (Nat.cast_nonneg _)

omit [Fintype H] [DecidableEq H] in
theorem classScale_mul_canonicalLaw_mass (counts : H → Shape → ℕ) (h : H)
    (N : ℕ) (a : Shape) :
    classScale counts h N * (canonicalLaw counts h).mass a = (counts h a : ℝ) / N := by
  unfold classScale
  calc
    _ = ((Fintype.card (Positions counts h) : ℝ) * (canonicalLaw counts h).mass a) / N := by ring
    _ = _ := by rw [total_mul_canonicalLaw_mass]

omit [Fintype H] [DecidableEq H] in
theorem classScale_mul_canonicalLaw_map_mass {B : Type*} [Fintype B] [DecidableEq B]
    (counts : H → Shape → ℕ) (h : H) (N : ℕ) (f : Shape → B) (b : B) :
    classScale counts h N * ((canonicalLaw counts h).map f).mass b =
      ∑ a, if f a = b then (counts h a : ℝ) / N else 0 := by
  rw [FiniteLaw.map_mass, Finset.mul_sum]
  apply Finset.sum_congr rfl
  intro a _
  by_cases ha : f a = b
  · simp only [ha, ite_true]
    exact classScale_mul_canonicalLaw_mass counts h N a
  · simp only [ha, ite_false, mul_zero]

omit [Fintype H] [DecidableEq H] in
theorem classScale_mul_marginalError {B : Type*} [Fintype B] [DecidableEq B]
    (counts : H → Shape → ℕ) (h : H) (N : ℕ) (p : FiniteLaw Shape)
    (f : Shape → B) (v : B → ℝ) :
    classScale counts h N * marginalError p (canonicalLaw counts h) f v =
      ∑ b, |(∑ a, if f a = b then (counts h a : ℝ) / N else 0) -
        classScale counts h N * (p.map f).mass b| * |v b| := by
  unfold marginalError
  rw [Finset.mul_sum]
  apply Finset.sum_congr rfl
  intro b _
  have habs : classScale counts h N *
      |((canonicalLaw counts h).map f).mass b - (p.map f).mass b| =
      |classScale counts h N * ((canonicalLaw counts h).map f).mass b -
        classScale counts h N * (p.map f).mass b| := by
    rw [← mul_sub, abs_mul, abs_of_nonneg (classScale_nonneg counts h N)]
  rw [← mul_assoc, habs, classScale_mul_canonicalLaw_map_mass]

omit [Fintype H] [DecidableEq H] in
theorem tendsto_classScale (counts : ℕ → H → Shape → ℕ) (h : H)
    (p : FiniteLaw Shape) (mass : ℝ)
    (hcounts : ∀ a, Tendsto (fun N => (counts N h a : ℝ) / N) atTop (𝓝 (mass * p.mass a))) :
    Tendsto (fun N => classScale (counts N) h N) atTop (𝓝 mass) := by
  have hsum := tendsto_finsetSum Finset.univ (fun a _ => hcounts a)
  simpa only [classScale, Positions, Fintype.card_fin, Nat.cast_sum,
    Finset.sum_div, ← Finset.mul_sum, p.total, mul_one] using hsum

omit [Fintype H] [DecidableEq H] in
theorem tendsto_classScale_mul_marginalError {B : Type*} [Fintype B] [DecidableEq B]
    (counts : ℕ → H → Shape → ℕ) (h : H) (p : FiniteLaw Shape)
    (mass : ℝ) (f : Shape → B) (v : B → ℝ)
    (hcounts : ∀ a, Tendsto (fun N => (counts N h a : ℝ) / N) atTop (𝓝 (mass * p.mass a))) :
    Tendsto (fun N => classScale (counts N) h N *
      marginalError p (canonicalLaw (counts N) h) f v) atTop (𝓝 0) := by
  have htotal := tendsto_classScale counts h p mass hcounts
  have hside (b : B) :
      Tendsto (fun N => ∑ a, if f a = b then (counts N h a : ℝ) / N else 0)
        atTop (𝓝 (mass * (p.map f).mass b)) := by
    have hterm (a : Shape) :
        Tendsto (fun N => if f a = b then (counts N h a : ℝ) / N else 0)
          atTop (𝓝 (if f a = b then mass * p.mass a else 0)) := by
      by_cases ha : f a = b
      · simpa only [ha, ite_true] using hcounts a
      · simp only [ha, ite_false]
        exact tendsto_const_nhds
    simpa only [FiniteLaw.map_mass, Finset.mul_sum, mul_ite, mul_zero] using
      tendsto_finsetSum Finset.univ (fun a _ => hterm a)
  have hterm (b : B) :
      Tendsto (fun N => |(∑ a, if f a = b then (counts N h a : ℝ) / N else 0) -
        classScale (counts N) h N * (p.map f).mass b| * |v b|) atTop (𝓝 0) := by
    simpa only [sub_self, abs_zero, zero_mul] using
      ((hside b).sub (htotal.mul_const ((p.map f).mass b))).abs.mul_const |v b|
  simpa only [classScale_mul_marginalError, Finset.sum_const_zero] using
    tendsto_finsetSum Finset.univ (fun b _ => hterm b)

omit [Fintype H] [DecidableEq H] in
theorem tendsto_classScale_mul_gibbsCap (counts : ℕ → H → Shape → ℕ) (h : H)
    (p : FiniteLaw Shape) (mass : ℝ) (u v w : Fin 17 → ℝ)
    (hcounts : ∀ a, Tendsto (fun N => (counts N h a : ℝ) / N) atTop (𝓝 (mass * p.mass a))) :
    Tendsto (fun N => classScale (counts N) h N *
      gibbsCap (counts N) h p u v w) atTop (𝓝 (mass * finiteEntropy p.mass)) := by
  have hm := (tendsto_classScale counts h p mass hcounts).mul_const (finiteEntropy p.mass)
  have hx := tendsto_classScale_mul_marginalError counts h p mass (shapeSide 0) u hcounts
  have hy := tendsto_classScale_mul_marginalError counts h p mass (shapeSide 1) v hcounts
  have hz := tendsto_classScale_mul_marginalError counts h p mass (shapeSide 2) w hcounts
  simpa only [gibbsCap, mul_add, add_zero] using ((hm.add hx).add hy).add hz

end MatrixMultiplication.JointAmbientEntropy

end

end OAI