File size: 14,777 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 Mathlib
import OAI.LinearAlgebra.MatrixFields.Entropy.GroupBranchLaws
import OAI.LinearAlgebra.MatrixFields.Entropy.JointAmbientEntropy

namespace OAI

namespace MatrixAllFields

open scoped BigOperators Topology Polynomial

section
namespace MatrixMultiplication.JointCapacityAlgebra

open scoped BigOperators

theorem total_sub_max_degrees (A X Y Z : ℝ) :
    A - max (A - X) (max (A - Y) (A - Z)) = min X (min Y Z) := by
  simp only [max_def, min_def]
  split_ifs <;> linarith

theorem weighted_joint_capacity {H : Type*} [Fintype H]
    (mass entropy : H → ℝ) (capacity : H → Fin 3 → ℝ) :
    (∑ h, mass h * entropy h) -
        max (∑ h, mass h * (entropy h - capacity h 0))
          (max (∑ h, mass h * (entropy h - capacity h 1))
            (∑ h, mass h * (entropy h - capacity h 2))) =
      min (∑ h, mass h * capacity h 0)
        (min (∑ h, mass h * capacity h 1) (∑ h, mass h * capacity h 2)) := by
  simp_rw [mul_sub, Finset.sum_sub_distrib]
  exact total_sub_max_degrees _ _ _ _

end MatrixMultiplication.JointCapacityAlgebra





noncomputable section

namespace MatrixMultiplication.JointOrdinaryPopulationSelection

open MatrixMultiplication.Foundation JointPopulation JointPopulationRates Filter
open scoped BigOperators Topology

attribute [local instance] Classical.propDecidable

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

def entropyRate (mass : Hist → ℝ) (p : Hist → FiniteLaw Shape) : ℝ :=
  ∑ h, mass h * finiteEntropy (p h).mass

def sideDegreeRate (mass : Hist → ℝ) (p : Hist → FiniteLaw Shape) (s : Fin 3) : ℝ :=
  ∑ h, mass h * (finiteEntropy (p h).mass - finiteEntropy (sideMass (p h).mass s))

def degreeRate (mass : Hist → ℝ) (p : Hist → FiniteLaw Shape) : ℝ :=
  max (sideDegreeRate mass p 0) (max (sideDegreeRate mass p 1) (sideDegreeRate mass p 2))

def sideCapacity (mass : Hist → ℝ) (p : Hist → FiniteLaw Shape) (s : Fin 3) : ℝ :=
  ∑ h, mass h * finiteEntropy (sideMass (p h).mass s)

omit [DecidableEq Hist] in
theorem entropyRate_sub_degreeRate_eq (mass : Hist → ℝ) (p : Hist → FiniteLaw Shape) :
    entropyRate mass p - degreeRate mass p =
      min (sideCapacity mass p 0) (min (sideCapacity mass p 1) (sideCapacity mass p 2)) := by
  exact JointCapacityAlgebra.weighted_joint_capacity mass
    (fun h => finiteEntropy (p h).mass) (fun h s => finiteEntropy (sideMass (p h).mass s))

theorem sideMass_entropy_le (p : FiniteLaw Shape) (s : Fin 3) :
    finiteEntropy (sideMass p.mass s) ≤ finiteEntropy p.mass := by
  simpa only [finiteEntropy, FiniteLaw.map_mass, sideMass] using
    p.map_entropy_le (shapeSide s)

omit [DecidableEq Hist] in
theorem sideDegreeRate_nonneg (mass : Hist → ℝ) (p : Hist → FiniteLaw Shape)
    (hmass : ∀ h, 0 ≤ mass h) (s : Fin 3) : 0 ≤ sideDegreeRate mass p s := by
  exact Finset.sum_nonneg fun h _ =>
    mul_nonneg (hmass h) (sub_nonneg.mpr (sideMass_entropy_le (p h) s))

omit [DecidableEq Hist] in
theorem degreeRate_nonneg (mass : Hist → ℝ) (p : Hist → FiniteLaw Shape)
    (hmass : ∀ h, 0 ≤ mass h) : 0 ≤ degreeRate mass p :=
  (sideDegreeRate_nonneg mass p hmass 0).trans (le_max_left _ _)

omit [DecidableEq Hist] in
theorem sideDegreeRate_le (mass : Hist → ℝ) (p : Hist → FiniteLaw Shape) (s : Fin 3) :
    sideDegreeRate mass p s ≤ degreeRate mass p := by
  unfold degreeRate
  fin_cases s
  · exact le_max_left _ _
  · exact (le_max_left _ _).trans (le_max_right _ _)
  · exact (le_max_right _ _).trans (le_max_right _ _)

def uniformDegree (counts : Hist → Shape → ℕ) (sum : Hist → ℕ) : ℕ :=
  Finset.univ.sup (fun e : Target counts =>
    (JointAmbientDegree.neighbors counts sum (triple counts e)).card)

theorem ordinary_neighbors_eq (counts : Hist → Shape → ℕ) (sum : Hist → ℕ)
    (e : JointCoarseHashing.Triple (Position counts)) :
    JointOrdinarySelection.neighbors (ambientSet counts sum) e =
      JointAmbientDegree.neighbors counts sum e := by
  ext neighbor
  rw [JointOrdinarySelection.mem_neighbors, JointAmbientDegree.neighbors,
    Finset.mem_filter]
  simp only [JointCoarseHashing.SharesSide, eq_comm]

theorem neighbors_card_le_uniformDegree (counts : Hist → Shape → ℕ)
    (sum : Hist → ℕ) (e : Target counts) :
    (JointOrdinarySelection.neighbors (ambientSet counts sum) (triple counts e)).card ≤
      uniformDegree counts sum := by
  rw [ordinary_neighbors_eq]
  exact Finset.le_sup
    (f := fun e : Target counts =>
      (JointAmbientDegree.neighbors counts sum (triple counts e)).card)
    (Finset.mem_univ e)

theorem uniformDegree_attained (counts : Hist → Shape → ℕ) (sum : Hist → ℕ) :
    ∃ e : Target counts,
      (JointAmbientDegree.neighbors counts sum (triple counts e)).card =
        uniformDegree counts sum := by
  let : Nonempty (Target counts) := target_nonempty counts
  obtain ⟨e, _, he⟩ := Finset.sup_mem_of_nonempty
    (s := Finset.univ)
    (f := fun e : Target counts =>
      (JointAmbientDegree.neighbors counts sum (triple counts e)).card)
    Finset.univ_nonempty
  exact ⟨e, he⟩

theorem uniformDegree_log_upper_of_all_targets
    (counts : ℕ → Hist → Shape → ℕ) (sum : Hist → ℕ) (B : ℝ)
    (hbound : ∀ᶠ N in atTop, ∀ e : Target (counts N),
      Real.log ((JointAmbientDegree.neighbors (counts N) sum (triple (counts N) e)).card : ℝ) /
        (N : ℝ) ≤ B) :
    ∀ᶠ N in atTop, Real.log (uniformDegree (counts N) sum : ℝ) / (N : ℝ) ≤ B := by
  filter_upwards [hbound] with N hN
  obtain ⟨e, he⟩ := uniformDegree_attained (counts N) sum
  rw [← he]
  exact hN e

omit [DecidableEq Hist] in
theorem tendsto_sum_gibbsExponent
    (counts : ℕ → Hist → Shape → ℕ) (mass : Hist → ℝ)
    (p : Hist → FiniteLaw Shape) (u v w : Hist → Fin 17 → ℝ)
    (hcounts : ∀ h a, Tendsto (fun N : ℕ => (counts N h a : ℝ) / (N : ℝ))
      atTop (𝓝 (mass h * (p h).mass a))) (s : Fin 3) :
    Tendsto (fun N => (∑ h, JointAmbientDegree.classExponent (counts N) s
      (triple (counts N) (canonicalTarget (counts N))) h
      (JointAmbientEntropy.gibbsCap (counts N) h (p h) (u h) (v h) (w h))) / (N : ℝ))
      atTop (𝓝 (sideDegreeRate mass p s)) := by
  have hsize (h : Hist) := tendsto_total_div_of_coordinate_rates
    (fun N => counts N h) (mass h) (p h).mass (p h).total (hcounts h)
  have hcap (h : Hist) := JointAmbientEntropy.tendsto_classScale_mul_gibbsCap
    counts h (p h) (mass h) (u h) (v h) (w h) (hcounts h)
  have hside (h : Hist) := tendsto_weighted_target_side_entropy counts
    (fun N => canonicalTarget (counts N)) mass (fun h => (p h).mass)
    (fun h => (p h).total) hcounts h s
  have h := JointAmbientRateLimit.tendsto_sum_classExponent_div_nat counts s
    (fun N h => JointAmbientEntropy.gibbsCap (counts N) h (p h) (u h) (v h) (w h))
    mass (fun h => mass h * finiteEntropy (p h).mass)
    (fun h => mass h * finiteEntropy (sideMass (p h).mass s)) hsize
    (fun h => by
      simpa only [JointAmbientEntropy.classScale, Positions, Fintype.card_fin] using hcap h)
    (fun h => by
      simpa only [Positions, Fintype.card_fin] using hside h)
  simpa only [sideDegreeRate, mul_sub] using h

theorem uniformDegree_log_upper_of_word_support
    (counts : ℕ → Hist → Shape → ℕ) (sum : Hist → ℕ) (mass : Hist → ℝ)
    (p : Hist → FiniteLaw Shape) (u v w : Hist → Fin 17 → ℝ) (c : Hist → ℝ)
    (hmass : ∀ h, 0 ≤ mass h)
    (hsupport : ∀ᶠ N in atTop, ∀ h word,
      JointAmbientDegree.localAllowed (counts N) 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)
    (hcounts : ∀ h a, Tendsto (fun N : ℕ => (counts N h a : ℝ) / (N : ℝ))
      atTop (𝓝 (mass h * (p h).mass a)))
    (η : ℝ) (hη : 0 < η) :
    ∀ᶠ N in atTop, Real.log (uniformDegree (counts N) sum : ℝ) / (N : ℝ) ≤
      degreeRate mass p + η := by
  apply uniformDegree_log_upper_of_all_targets counts sum
  apply JointAmbientRateLimit.eventually_all_target_degree_upper counts sum
    (fun N h => JointAmbientEntropy.gibbsCap (counts N) h (p h) (u h) (v h) (w h))
    (sideDegreeRate mass p) (degreeRate mass p) (degreeRate_nonneg mass p hmass)
    _ (tendsto_sum_gibbsExponent counts mass p u v w hcounts)
    (sideDegreeRate_le mass p) η hη
  filter_upwards [hsupport] with N hN
  intro h word hw
  exact JointAmbientEntropy.localAllowed_entropy_le_of_word_support
    (counts N) sum h word (p h) (u h) (v h) (w h) (c h)
    (hN h word hw) (hlog h) hw

theorem uniformDegree_log_upper
    (counts : ℕ → Hist → Shape → ℕ) (sum : Hist → ℕ) (mass : Hist → ℝ)
    (p : Hist → FiniteLaw Shape) (u v w : Hist → Fin 17 → ℝ) (c : Hist → ℝ)
    (hmass : ∀ h, 0 ≤ mass 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)
    (hcounts : ∀ h a, Tendsto (fun N : ℕ => (counts N h a : ℝ) / (N : ℝ))
      atTop (𝓝 (mass h * (p h).mass a)))
    (η : ℝ) (hη : 0 < η) :
    ∀ᶠ N in atTop, Real.log (uniformDegree (counts N) sum : ℝ) / (N : ℝ) ≤
      degreeRate mass p + η :=
  uniformDegree_log_upper_of_word_support counts sum mass p u v w c hmass
    (Eventually.of_forall fun _ h word hw i => hsupport h (word i) (hw.1 i))
    hlog hcounts η hη

def selectionHashRate (mass : Hist → ℝ) (p : Hist → FiniteLaw Shape) (ε : ℝ) : ℝ :=
  degreeRate mass p + ε / 2

theorem eventually_exists_selection_of_word_support
    (counts : ℕ → Hist → Shape → ℕ) (sum : Hist → ℕ) (mass : Hist → ℝ)
    (p : Hist → FiniteLaw Shape) (u v w : Hist → Fin 17 → ℝ) (c : Hist → ℝ)
    (hmass : ∀ h, 0 ≤ mass h)
    (hcountSupport : ∀ N h a, 0 < counts N h a →
      a.1.val + a.2.1.val + a.2.2.val = sum h)
    (hsupport : ∀ᶠ N in atTop, ∀ h word,
      JointAmbientDegree.localAllowed (counts N) 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)
    (hcounts : ∀ h a, Tendsto (fun N : ℕ => (counts N h a : ℝ) / (N : ℝ))
      atTop (𝓝 (mass h * (p h).mass a)))
    {ε : ℝ} (hε : 0 < ε) :
    ∀ᶠ N in atTop,
      ∃ (s : JointCoarseHashing.Sample (Position (counts N))
          (JointOrdinarySelection.hashLevel (selectionHashRate mass p ε) N))
        (G : Finset (Target (counts N))),
        G.card = ⌊Real.exp ((N : ℝ) * (entropyRate mass p - degreeRate mass p - ε))⌋₊ ∧
        (∀ e ∈ G, JointOrdinarySelection.Good
          (JointOrdinarySelection.hashLevel (selectionHashRate mass p ε) N)
          (JointOrdinarySelection.hashSet (selectionHashRate mass p ε) N)
          (ambientSet (counts N) sum) (triple (counts N) e) s) ∧
        (G : Set (Target (counts N))).Pairwise (fun e f =>
          ¬ JointCoarseHashing.SharesSide (triple (counts N) e) (triple (counts N) f)) := by
  have hD : 0 ≤ degreeRate mass p := degreeRate_nonneg mass p hmass
  have hH : 0 ≤ selectionHashRate mass p ε := by unfold selectionHashRate; linarith
  have hgap : degreeRate mass p < selectionHashRate mass p ε := by
    unfold selectionHashRate
    linarith
  have htarget (N : ℕ) (e : Target (counts N)) :
      triple (counts N) e ∈ ambientSet (counts N) sum := by
    apply targetSet_subset_ambient (counts N) sum (hcountSupport N)
    exact Finset.mem_image.mpr ⟨e, Finset.mem_univ _, rfl⟩
  have hpos : ∀ᶠ N in atTop, 0 < Fintype.card (Target (counts N)) :=
    Eventually.of_forall fun N => Fintype.card_pos_iff.mpr (target_nonempty (counts N))
  have htargetRate : Tendsto (fun N : ℕ =>
      Real.log (Fintype.card (Target (counts N)) : ℝ) / (N : ℝ))
      atTop (𝓝 (entropyRate mass p)) :=
    tendsto_log_target_card_div_of_coordinate_rates counts mass
      (fun h => (p h).mass) hmass (fun h => (p h).total) hcounts
  have hselection := JointOrdinarySelection.eventually_exists_selection
    (fun N => ambientSet (counts N) sum)
    (fun N (position : Position (counts N)) => sum position.1)
    (fun N => triple (counts N)) (fun N => triple_injective (counts N))
    (fun N f hf => (Finset.mem_filter.mp hf).2.1) htarget
    (fun N => uniformDegree (counts N) sum)
    (fun N e => neighbors_card_le_uniformDegree (counts N) sum e)
    hH hgap (uniformDegree_log_upper_of_word_support counts sum mass p u v w c
      hmass hsupport hlog hcounts)
    hpos htargetRate (show 0 < ε / 2 by linarith)
  have heq : entropyRate mass p - selectionHashRate mass p ε - ε / 2 =
      entropyRate mass p - degreeRate mass p - ε := by unfold selectionHashRate; ring
  simpa only [heq] using hselection

theorem eventually_exists_selection
    (counts : ℕ → Hist → Shape → ℕ) (sum : Hist → ℕ) (mass : Hist → ℝ)
    (p : Hist → FiniteLaw Shape) (u v w : Hist → Fin 17 → ℝ) (c : Hist → ℝ)
    (hmass : ∀ h, 0 ≤ mass h)
    (hcountSupport : ∀ N h a, 0 < counts N h a →
      a.1.val + a.2.1.val + a.2.2.val = sum 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)
    (hcounts : ∀ h a, Tendsto (fun N : ℕ => (counts N h a : ℝ) / (N : ℝ))
      atTop (𝓝 (mass h * (p h).mass a)))
    {ε : ℝ} (hε : 0 < ε) :
    ∀ᶠ N in atTop,
      ∃ (s : JointCoarseHashing.Sample (Position (counts N))
          (JointOrdinarySelection.hashLevel (selectionHashRate mass p ε) N))
        (G : Finset (Target (counts N))),
        G.card = ⌊Real.exp ((N : ℝ) * (entropyRate mass p - degreeRate mass p - ε))⌋₊ ∧
        (∀ e ∈ G, JointOrdinarySelection.Good
          (JointOrdinarySelection.hashLevel (selectionHashRate mass p ε) N)
          (JointOrdinarySelection.hashSet (selectionHashRate mass p ε) N)
          (ambientSet (counts N) sum) (triple (counts N) e) s) ∧
        (G : Set (Target (counts N))).Pairwise (fun e f =>
          ¬ JointCoarseHashing.SharesSide (triple (counts N) e) (triple (counts N) f)) :=
  eventually_exists_selection_of_word_support counts sum mass p u v w c
    hmass hcountSupport
    (Eventually.of_forall fun _ h word hw i => hsupport h (word i) (hw.1 i))
    hlog hcounts hε

end MatrixMultiplication.JointOrdinaryPopulationSelection

end
end

end MatrixAllFields

end OAI