File size: 15,093 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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import Mathlib
import OAI.LinearAlgebra.MatrixFields.Histories.HistorySums
import OAI.LinearAlgebra.MatrixFields.Tensors.CWWordDimensionParameters

namespace OAI

namespace MatrixAllFields

open scoped BigOperators Topology Polynomial

namespace MatrixMultiplication.AllFieldParameters

open MatrixMultiplication.Foundation
open scoped BigOperators

noncomputable section

theorem entropy_two_points {A : Type*} [Fintype A] [DecidableEq A]
    (a b : A) (hab : a ≠ b) (p q : ℝ) :
    finiteEntropy (fun i => if i = a then p else if i = b then q else 0) =
      entropyTerm p + entropyTerm q := by
  unfold finiteEntropy
  calc
    _ = ∑ i : A, ((if i = a then entropyTerm p else 0) +
        (if i = b then entropyTerm q else 0)) := by
      apply Finset.sum_congr rfl
      intro i _
      by_cases hi : i = a
      · subst i
        simp [hab]
      · by_cases hj : i = b
        · subst i
          simp [hab.symm]
        · simp [hi, hj]
    _ = _ := by simp [Finset.sum_add_distrib]

theorem weighted_two_points {A : Type*} [Fintype A] [DecidableEq A]
    (a b : A) (hab : a ≠ b) (p q : ℝ) (f : A → ℝ) :
    (∑ i : A, (if i = a then p else if i = b then q else 0) * f i) =
      p * f a + q * f b := by
  calc
    _ = ∑ i : A, ((if i = a then p * f a else 0) +
        (if i = b then q * f b else 0)) := by
      apply Finset.sum_congr rfl
      intro i _
      by_cases hi : i = a
      · subst i
        simp [hab]
      · by_cases hj : i = b
        · subst i
          simp [hi]
        · simp [hi, hj]
    _ = _ := by simp [Finset.sum_add_distrib]

def statisticRate (d : ℚ) (k : Fin 5) : ℝ :=
  finiteEntropy (fun i : Fin 6 => (statisticLaw d k i : ℝ)) +
    ∑ i : Fin 6, (statisticLaw d k i : ℝ) * Real.log (multiplicity i : ℝ)

def binaryEntropyRate (d : ℚ) : ℝ :=
  entropyTerm (1 - (d : ℝ)) + entropyTerm (d : ℝ) + (d : ℝ) * Real.log 2

theorem statisticLaw_two_real (d : ℚ) (i : Fin 6) :
    (statisticLaw d 2 i : ℝ) =
      if i = 2 then 1 - (d : ℝ) else if i = 3 then (d : ℝ) else 0 := by
  by_cases hi : i = 2
  · simp [statisticLaw, hi]
  · by_cases hj : i = 3 <;> simp [statisticLaw, hi, hj]

theorem statisticRate_two (d : ℚ) :
    statisticRate d 2 = binaryEntropyRate d + 2 * (1 - (d : ℝ)) * Real.log 5 := by
  unfold statisticRate
  simp_rw [statisticLaw_two_real]
  rw [entropy_two_points (2 : Fin 6) 3 (by decide),
    weighted_two_points (2 : Fin 6) 3 (by decide)]
  have h25 : Real.log (25 : ℝ) = 2 * Real.log 5 := by
    have hp : (5 : ℝ) ^ 2 = 25 := by norm_num
    rw [← hp, Real.log_pow]
    norm_num
  change entropyTerm (1 - (d : ℝ)) + entropyTerm (d : ℝ) +
    ((1 - (d : ℝ)) * Real.log 25 + (d : ℝ) * Real.log 2) = _
  rw [h25]
  unfold binaryEntropyRate
  ring

theorem statisticRate_deterministic (d : ℚ) (k : Fin 5) (hk : k.val ≠ 2) :
    statisticRate d k = Real.log (multiplicity (singletonSlot k.val) : ℝ) := by
  have hmass (i : Fin 6) : (statisticLaw d k i : ℝ) =
      if i = singletonSlot k.val then 1 else 0 := by
    by_cases hi : i = singletonSlot k.val <;> simp [statisticLaw, hk, hi]
  unfold statisticRate finiteEntropy
  simp_rw [hmass]
  simp [entropyTerm, apply_ite]

theorem statisticRate_zero (d : ℚ) : statisticRate d 0 = 0 := by
  rw [statisticRate_deterministic d 0 (by decide)]
  norm_num [singletonSlot, multiplicity]

theorem statisticRate_one (d : ℚ) : statisticRate d 1 = Real.log 10 := by
  rw [statisticRate_deterministic d 1 (by decide)]
  norm_num [singletonSlot, multiplicity]

theorem statisticRate_three (d : ℚ) : statisticRate d 3 = Real.log 10 := by
  rw [statisticRate_deterministic d 3 (by decide)]
  change Real.log (10 : ℝ) = Real.log 10
  rfl

theorem statisticRate_four (d : ℚ) : statisticRate d 4 = 0 := by
  rw [statisticRate_deterministic d 4 (by decide)]
  have hv : multiplicity (singletonSlot (4 : Fin 5).val) = 1 := by decide +kernel
  rw [hv]
  norm_num

end

end MatrixMultiplication.AllFieldParameters

noncomputable section

namespace MatrixMultiplication.AllFieldTerminalRates

open AllFieldParameters AllFieldHistory
open scoped BigOperators
attribute [local instance] Classical.propDecidable Classical.decEq

def initialRate (g : Shape) : ℝ :=
  Real.log (Contractions.coefficients[shapeMax g]?.getD 0 : ℝ)

def S0 : ℝ :=
  ((sortedInitial.filter (fun g => !positive g)).map
    (fun g => (initialLaw g : ℝ) * initialRate g)).sum

def S1 : ℝ :=
  (zeroSecond.map (fun t => (secondMass t : ℝ) * AllFieldZeroLeafRates.fourRate t)).sum

def secondLeafRate (t u : Shape) : ℝ :=
  if shapeMax u = 4 then 0
  else if shapeMax u = 3 then Real.log 10
  else if !positive u then
    binaryEntropyRate (binaryParameter t u) +
      2 * (1 - (binaryParameter t u : ℝ)) * Real.log 5
  else (2 - (binaryParameter t u : ℝ)) * Real.log 5

def S2 : ℝ :=
  (positiveSecond.map (fun t =>
    ((below t).map (fun u => (littleMass t u : ℝ) * secondLeafRate t u)).sum)).sum

def S : ℝ := (S0 + S1) + S2

private theorem four_shape_max_lt_five : ∀ u ∈ shapes 4, shapeMax u < 5 := by
  decide +kernel

private theorem four_zero_max : ∀ u ∈ shapes 4, positive u = false →
    shapeMax u = 2 ∨ shapeMax u = 3 ∨ shapeMax u = 4 := by
  decide +kernel

private theorem four_positive_max : ∀ u ∈ shapes 4, positive u = true →
    shapeMax u = 2 := by
  decide +kernel

theorem second_shapeMax_lt_five (t : Shape) (ht : t ∈ positiveSecond)
    (u : Shape) (hu : u ∈ below t) : shapeMax u < 5 :=
  four_shape_max_lt_five u (stageB_children_size t ht u hu)

theorem secondLeafRate_zero (t : Shape) (ht : t ∈ positiveSecond)
    (u : Shape) (hu : u ∈ below t) (hz : positive u = false) :
    secondLeafRate t u = statisticRate (binaryParameter t u)
      ⟨shapeMax u, second_shapeMax_lt_five t ht u hu⟩ := by
  rcases four_zero_max u (stageB_children_size t ht u hu) hz with h2 | h3 | h4
  · have hk : (⟨shapeMax u, second_shapeMax_lt_five t ht u hu⟩ : Fin 5) = 2 :=
      Fin.ext h2
    rw [hk, statisticRate_two]
    simp [secondLeafRate, h2, hz]
  · have hk : (⟨shapeMax u, second_shapeMax_lt_five t ht u hu⟩ : Fin 5) = 3 :=
      Fin.ext h3
    rw [hk, statisticRate_three]
    simp [secondLeafRate, h3]
  · have hk : (⟨shapeMax u, second_shapeMax_lt_five t ht u hu⟩ : Fin 5) = 4 :=
      Fin.ext h4
    rw [hk, statisticRate_four]
    simp [secondLeafRate, h4]

theorem secondLeafRate_positive (t : Shape) (ht : t ∈ positiveSecond)
    (u : Shape) (hu : u ∈ below t) (hp : positive u = true) :
    secondLeafRate t u = (2 - (binaryParameter t u : ℝ)) * Real.log 5 := by
  have hm := four_positive_max u (stageB_children_size t ht u hu) hp
  simp [secondLeafRate, hm, hp]

theorem secondLeafRate_builder_expression (t u : Shape) :
    secondLeafRate t u =
      if shapeMax u = 4 then 0
      else if shapeMax u = 3 then Real.log 10
      else if !positive u then
        (MatrixMultiplication.Foundation.entropyTerm (1 - (binaryParameter t u : ℝ)) +
          MatrixMultiplication.Foundation.entropyTerm (binaryParameter t u : ℝ) +
          (binaryParameter t u : ℝ) * Real.log 2) +
          ((1 - (binaryParameter t u : ℝ)) * 2) * Real.log 5
      else (2 - (binaryParameter t u : ℝ)) * Real.log 5 := by
  unfold secondLeafRate
  split_ifs <;> try rfl
  unfold binaryEntropyRate
  ring

private theorem list_filter_sum_inline_MatrixMultiplication_AllFieldTerminalRateFormulas {A : Type*} (xs : List A) (p : A → Bool)
    (f : A → ℝ) :
    ((xs.filter p).map f).sum = (xs.map (fun a => if p a then f a else 0)).sum := by
  induction xs with
  | nil => simp
  | cons a xs ih => cases hp : p a <;> simp [hp, ih]

theorem initial_zero_history_rate (K : ℕ) :
    (∑ h : {h : Initial K // initialShape h ∉ positiveInitial},
      (initialAmount h.val : ℝ) * initialRate (initialShape h.val)) = (K : ℝ) * S0 := by
  calc
    _ = ∑ h : Initial K, if initialShape h ∉ positiveInitial then
        (initialAmount h : ℝ) * initialRate (initialShape h) else 0 := by
      rw [← Finset.sum_filter]
      exact (Finset.sum_subtype
        (p := fun h : Initial K => initialShape h ∉ positiveInitial)
        (Finset.univ.filter (fun h : Initial K => initialShape h ∉ positiveInitial))
        (by simp)
        (fun h : Initial K => (initialAmount h : ℝ) * initialRate (initialShape h))).symm
    _ = ∑ h : Initial K, (initialAmount h : ℝ) *
        (if initialShape h ∉ positiveInitial then initialRate (initialShape h) else 0) := by
      apply Finset.sum_congr rfl
      intro h _
      split_ifs <;> simp
    _ = _ := by
      rw [initial_history_sum K
        (fun g => if g ∉ positiveInitial then initialRate g else 0), S0, list_filter_sum_inline_MatrixMultiplication_AllFieldTerminalRateFormulas]
      apply congrArg (fun x : ℝ => (K : ℝ) * x)
      apply congrArg List.sum
      apply List.map_congr_left
      intro g hg
      cases hp : positive g <;> simp [positiveInitial, hg, hp]

end MatrixMultiplication.AllFieldTerminalRates

namespace MatrixMultiplication.AllFieldInitialLeafRates

open AllFieldHistory AllFieldParameters AllFieldTerminalRates
open CWWindowedLeaves Filter
open scoped BigOperators Topology
attribute [local instance] Classical.propDecidable Classical.decEq

abbrev InitialZero (K : ℕ) :=
  {h : Initial K // initialShape h ∉ positiveInitial}

abbrev PopulationHistory (K : ℕ) := InitialZero K × Placement

theorem shapeMax_lt {K : ℕ} (h : InitialZero K) :
    shapeMax (initialShape h.val) < 17 := by
  have hb := (root_shape_spec _ (initialShape_mem h.val)).1
  exact Nat.lt_succ_of_le (max_le (hb 0) (max_le (hb 1) (hb 2)))

def shapeIndex {K : ℕ} (h : InitialZero K) : Fin 17 :=
  ⟨shapeMax (initialShape h.val), shapeMax_lt h⟩

theorem card_alphabet {K : ℕ} (h : InitialZero K) :
    Fintype.card (Alphabet 8 (shapeMax (initialShape h.val))) =
      Contractions.coefficients[shapeMax (initialShape h.val)]?.getD 0 := by
  have hc := CWWordDimensionParameters.table_eq_card (shapeIndex h)
  rw [Fintype.card_eq_nat_card] at hc ⊢
  exact hc.symm

theorem card_alphabet_pos {K : ℕ} (h : InitialZero K) :
    0 < Fintype.card (Alphabet 8 (shapeMax (initialShape h.val))) := by
  have hc := CWWordDimensionParameters.word_card_positive (shapeIndex h)
  rw [Fintype.card_eq_nat_card] at hc ⊢
  exact hc

abbrev Words {K : ℕ} (allocation : Allocation) (dilation : ℕ)
    (h : PopulationHistory K) :=
  Fin (population allocation dilation (.initial h.1.val, h.2)) →
    Alphabet 8 (shapeMax (initialShape h.1.val))

theorem card_words {K : ℕ} (allocation : Allocation) (dilation : ℕ)
    (h : PopulationHistory K) :
    Fintype.card (Words allocation dilation h) =
      (Contractions.coefficients[shapeMax (initialShape h.1.val)]?.getD 0) ^
        population allocation dilation (.initial h.1.val, h.2) := by
  simp only [Words, Fintype.card_fun, Fintype.card_fin, card_alphabet]

theorem card_words_pos {K : ℕ} (allocation : Allocation) (dilation : ℕ)
    (h : PopulationHistory K) :
    0 < Fintype.card (Words allocation dilation h) := by
  simp only [Words, Fintype.card_fun, Fintype.card_fin]
  exact pow_pos (card_alphabet_pos h.1) _

theorem log_card_words {K : ℕ} (allocation : Allocation) (dilation : ℕ)
    (h : PopulationHistory K) :
    Real.log (Fintype.card (Words allocation dilation h) : ℝ) =
      (population allocation dilation (.initial h.1.val, h.2) : ℝ) *
        initialRate (initialShape h.1.val) := by
  simp only [card_words, Nat.cast_pow, Real.log_pow, initialRate]

abbrev ProductWords {K : ℕ} (allocation : Allocation) (dilation : ℕ) :=
  ∀ h : PopulationHistory K, Words allocation dilation h

def volume {K : ℕ} (allocation : Allocation) (dilation : ℕ) : ℕ :=
  Fintype.card (ProductWords (K := K) allocation dilation)

theorem volume_eq_prod {K : ℕ} (allocation : Allocation) (dilation : ℕ) :
    volume (K := K) allocation dilation =
      ∏ h : PopulationHistory K, Fintype.card (Words allocation dilation h) := by
  rw [volume, Fintype.card_pi]

theorem volume_pos {K : ℕ} (allocation : Allocation) (dilation : ℕ) :
    0 < volume (K := K) allocation dilation := by
  rw [volume_eq_prod]
  exact Finset.prod_pos (fun h _ => card_words_pos allocation dilation h)

theorem log_volume {K : ℕ} (allocation : Allocation) (dilation : ℕ) :
    Real.log (volume (K := K) allocation dilation : ℝ) =
      (populationLength (K := K) allocation dilation : ℝ) *
        ∑ h : PopulationHistory K,
          (amount allocation (.initial h.1.val, h.2) : ℝ) *
            initialRate (initialShape h.1.val) := by
  rw [volume_eq_prod, Nat.cast_prod, Real.log_prod
    (fun h _ => Nat.cast_ne_zero.mpr
      (Nat.ne_of_gt (card_words_pos allocation dilation h)))]
  simp only [log_card_words]
  rw [Finset.mul_sum]
  apply Finset.sum_congr rfl
  intro h _
  have hc : (population allocation dilation (.initial h.1.val, h.2) : ℝ) =
      (populationLength (K := K) allocation dilation : ℝ) *
        (amount allocation (.initial h.1.val, h.2) : ℝ) := by
    exact_mod_cast population_cast allocation dilation (.initial h.1.val, h.2)
  rw [hc, mul_assoc]

theorem sum_populationHistory_rate {K : ℕ} (allocation : Allocation) :
    (∑ h : PopulationHistory K,
      (amount allocation (.initial h.1.val, h.2) : ℝ) *
        initialRate (initialShape h.1.val)) =
      ∑ h : InitialZero K,
        (initialAmount h.val : ℝ) * initialRate (initialShape h.val) := by
  rw [Fintype.sum_prod_type]
  apply Finset.sum_congr rfl
  intro h _
  dsimp only
  rw [← Finset.sum_mul]
  have hm : (∑ phi : Placement, (amount allocation (.initial h.val, phi) : ℝ)) =
      (initialAmount h.val : ℝ) := by
    exact_mod_cast sum_placement_amount allocation (.initial h.val)
  rw [hm]

theorem log_volume_eq_S0 {K : ℕ} (allocation : Allocation) (dilation : ℕ) :
    Real.log (volume (K := K) allocation dilation : ℝ) =
      (populationLength (K := K) allocation dilation : ℝ) * ((K : ℝ) * S0) := by
  rw [log_volume, sum_populationHistory_rate, initial_zero_history_rate]

theorem normalized_log_volume_eq_S0 {K : ℕ} (allocation : Allocation)
    (dilation : ℕ) (hd : 0 < dilation) :
    Real.log (volume (K := K) allocation dilation : ℝ) /
      populationLength (K := K) allocation dilation = (K : ℝ) * S0 := by
  rw [log_volume_eq_S0]
  have hn : (populationLength (K := K) allocation dilation : ℝ) ≠ 0 :=
    Nat.cast_ne_zero.mpr
      (Nat.ne_of_gt (AllFieldPopulationCounts.blockLength_pos _ hd))
  exact mul_div_cancel_left₀ _ hn

theorem tendsto_normalized_log_volume {K : ℕ} (allocation : Allocation) :
    Tendsto (fun dilation : ℕ =>
      Real.log (volume (K := K) allocation dilation : ℝ) /
        populationLength (K := K) allocation dilation) atTop (𝓝 ((K : ℝ) * S0)) := by
  apply tendsto_const_nhds.congr'
  filter_upwards [eventually_gt_atTop (0 : ℕ)] with dilation hd
  exact (normalized_log_volume_eq_S0 allocation dilation hd).symm

end MatrixMultiplication.AllFieldInitialLeafRates

end

end MatrixAllFields

end OAI