File size: 12,546 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
import Mathlib.Data.Fintype.Perm
import Mathlib.Data.Fintype.BigOperators
import Mathlib.Algebra.BigOperators.Field
import Mathlib.Algebra.BigOperators.Group.Finset.Piecewise
import Mathlib.Analysis.SpecialFunctions.Log.Basic
import Mathlib.Tactic.FieldSimp
import Mathlib.Tactic.NormNum
import Mathlib.Tactic.Ring

namespace OAI

/-! Finite entropy, rate estimates and ordered asymptotic limits. -/

noncomputable section

namespace MatrixMultiplication.Foundation
namespace OrientationRates

open scoped BigOperators

abbrev Orientation := Equiv.Perm (Fin 3)

@[simp] theorem orientation_card : Fintype.card Orientation = 6 := by
  rw [Fintype.card_perm]
  decide

def relativeOrientation (relative : Orientation) : Orientation ≃ Orientation where
  toFun outer := outer * relative
  invFun original := original * relative⁻¹
  left_inv outer := by simp [mul_assoc]
  right_inv original := by simp [mul_assoc]

@[simp] theorem relativeOrientation_apply (relative outer : Orientation) :
    relativeOrientation relative outer = outer * relative := rfl

theorem unique_outer_orientation (relative original : Orientation) :
    ∃! outer : Orientation, outer * relative = original :=
  (relativeOrientation relative).bijective.existsUnique original

theorem sum_relative_orientation {M : Type*} [AddCommMonoid M]
    (relative : Orientation) (value : Orientation → M) :
    (∑ outer, value (outer * relative)) = ∑ original, value original :=
  (relativeOrientation relative).sum_comp value

theorem prod_relative_orientation {M : Type*} [CommMonoid M]
    (relative : Orientation) (value : Orientation → M) :
    (∏ outer, value (outer * relative)) = ∏ original, value original :=
  (relativeOrientation relative).prod_comp value

theorem site_population_in_orientation {M : Type*} [AddCommMonoid M]
    (relative original : Orientation) (population : M) :
    (∑ outer : Orientation,
      if outer * relative = original then population else 0) = population := by
  classical
  rw [sum_relative_orientation relative
    (fun orientation => if orientation = original then population else 0)]
  simp

theorem histogram_in_original_orientation {Site M : Type*}
    [Fintype Site] [AddCommMonoid M]
    (relative : Site → Orientation) (population : Site → M)
    (original : Orientation) :
    (∑ site, ∑ outer : Orientation,
      if outer * relative site = original then population site else 0) =
      ∑ site, population site := by
  classical
  apply Finset.sum_congr rfl
  intro site _
  exact site_population_in_orientation (relative site) original (population site)

theorem target_multiplicity (targetCount : ℕ) :
    (∏ _ : Orientation, targetCount) = targetCount ^ 6 := by
  simp

section FiniteRates

variable {Node Grade : Type*} [Fintype Node] [Fintype Grade]

def outputLaw (n : ℕ) (population : Node → ℝ) (law : Node → Grade → ℝ)
    (grade : Grade) : ℝ :=
  (∑ node, population node * law node grade) / n

def affineScore (offset : ℝ) (coefficient law : Grade → ℝ) : ℝ :=
  offset + ∑ grade, coefficient grade * law grade

def totalNodeUtility (population utility : Node → ℝ) : ℝ :=
  ∑ node, population node * utility node

def totalCost (population : Node → ℝ) (law : Node → Grade → ℝ)
    (offset : ℝ) (coefficient : Grade → ℝ) (utility : Node → ℝ) : ℝ :=
  ∑ node, population node * (affineScore offset coefficient (law node) - utility node)

omit [Fintype Grade] in
theorem outputLaw_nonnegative (n : ℕ) (population : Node → ℝ)
    (law : Node → Grade → ℝ)
    (hp : ∀ node, 0 ≤ population node) (hq : ∀ node grade, 0 ≤ law node grade)
    (grade : Grade) : 0 ≤ outputLaw n population law grade := by
  apply div_nonneg
  · exact Finset.sum_nonneg fun node _ => mul_nonneg (hp node) (hq node grade)
  · exact Nat.cast_nonneg n

theorem outputLaw_total (n : ℕ) (hn : n ≠ 0) (population : Node → ℝ)
    (law : Node → Grade → ℝ)
    (hp : ∑ node, population node = (n : ℝ))
    (hq : ∀ node, ∑ grade, law node grade = 1) :
    ∑ grade, outputLaw n population law grade = 1 := by
  have hn' : (n : ℝ) ≠ 0 := Nat.cast_ne_zero.mpr hn
  simp only [outputLaw, ← Finset.sum_div]
  rw [Finset.sum_comm]
  simp only [← Finset.mul_sum, hq, mul_one, hp, div_self hn']

theorem weighted_affineScore (population : Node → ℝ) (law : Node → Grade → ℝ)
    (offset : ℝ) (coefficient : Grade → ℝ) :
    (∑ node, population node * affineScore offset coefficient (law node)) =
      (∑ node, population node) * offset +
        ∑ grade, coefficient grade * (∑ node, population node * law node grade) := by
  simp only [affineScore, mul_add, Finset.sum_add_distrib, ← Finset.sum_mul]
  congr 1
  calc
    (∑ node, population node * ∑ grade, coefficient grade * law node grade) =
        ∑ node, ∑ grade, coefficient grade * (population node * law node grade) := by
      apply Finset.sum_congr rfl
      intro node _
      rw [Finset.mul_sum]
      apply Finset.sum_congr rfl
      intro grade _
      ring
    _ = ∑ grade, coefficient grade * ∑ node, population node * law node grade := by
      rw [Finset.sum_comm]
      simp only [Finset.mul_sum]

theorem weighted_affineScore_eq (n : ℕ) (hn : n ≠ 0)
    (population : Node → ℝ) (law : Node → Grade → ℝ)
    (hp : ∑ node, population node = (n : ℝ))
    (offset : ℝ) (coefficient : Grade → ℝ) :
    (∑ node, population node * affineScore offset coefficient (law node)) =
      (n : ℝ) * affineScore offset coefficient (outputLaw n population law) := by
  have hn' : (n : ℝ) ≠ 0 := Nat.cast_ne_zero.mpr hn
  rw [weighted_affineScore, hp]
  simp only [affineScore, outputLaw]
  conv_rhs => rw [mul_add, Finset.mul_sum]
  congr 1
  apply Finset.sum_congr rfl
  intro grade _
  field_simp [hn']

theorem totalCost_eq (n : ℕ) (hn : n ≠ 0)
    (population : Node → ℝ) (law : Node → Grade → ℝ)
    (hp : ∑ node, population node = (n : ℝ))
    (offset : ℝ) (coefficient : Grade → ℝ) (utility : Node → ℝ) :
    totalCost population law offset coefficient utility =
      (n : ℝ) * affineScore offset coefficient (outputLaw n population law) -
        totalNodeUtility population utility := by
  simp only [totalCost, mul_sub, Finset.sum_sub_distrib, totalNodeUtility]
  rw [weighted_affineScore_eq n hn population law hp]

theorem utility_rate_identity (n : ℕ) (hn : n ≠ 0)
    (population : Node → ℝ) (law : Node → Grade → ℝ)
    (hp : ∑ node, population node = (n : ℝ))
    (offset targetRate : ℝ) (coefficient : Grade → ℝ) (utility : Node → ℝ) :
    (targetRate + totalNodeUtility population utility) / n =
      affineScore offset coefficient (outputLaw n population law) +
        (targetRate - totalCost population law offset coefficient utility) / n := by
  have hn' : (n : ℝ) ≠ 0 := Nat.cast_ne_zero.mpr hn
  rw [totalCost_eq n hn population law hp]
  field_simp [hn']
  ring

theorem finite_utility_bound (n : ℕ) (hn : n ≠ 0)
    (population : Node → ℝ) (law : Node → Grade → ℝ)
    (hp : ∑ node, population node = (n : ℝ))
    (offset targetRate error actualUtility : ℝ)
    (coefficient : Grade → ℝ) (utility : Node → ℝ)
    (construction : (targetRate + totalNodeUtility population utility) / n - error ≤
      actualUtility) :
    affineScore offset coefficient (outputLaw n population law) +
      (targetRate - totalCost population law offset coefficient utility) / n - error ≤
        actualUtility := by
  rwa [utility_rate_identity n hn population law hp offset targetRate coefficient utility]
    at construction

end FiniteRates

section PhysicalNormalization

variable {Node : Type*} [Fintype Node]

theorem log_resource_product (copies : Node → ℕ) (resource : Node → ℝ)
    (hresource : ∀ node, resource node ≠ 0) :
    Real.log (∏ node, resource node ^ copies node) =
      ∑ node, (copies node : ℝ) * Real.log (resource node) := by
  rw [Real.log_prod (s := Finset.univ) (f := fun node => resource node ^ copies node)
    (fun node _ => pow_ne_zero _ (hresource node))]
  simp only [Real.log_pow]

theorem log_direct_multiplicity (targetCount : ℝ) (htarget : targetCount ≠ 0)
    (copies : Node → ℕ) (labels : Node → ℝ) (hlabels : ∀ node, labels node ≠ 0) :
    Real.log (targetCount ^ 6 * ∏ node, labels node ^ copies node) =
      6 * Real.log targetCount + ∑ node, (copies node : ℝ) * Real.log (labels node) := by
  have hprod : (∏ node, labels node ^ copies node) ≠ 0 :=
    Finset.prod_ne_zero_iff.mpr (fun node _ => pow_ne_zero _ (hlabels node))
  rw [Real.log_mul (pow_ne_zero _ htarget) hprod, Real.log_pow,
    log_resource_product copies labels hlabels]
  norm_num

theorem raw_utility_accounting (targetCount : ℝ) (htarget : targetCount ≠ 0)
    (copies : Node → ℕ) (labels rank volume : Node → ℝ)
    (hlabels : ∀ node, labels node ≠ 0) (hrank : ∀ node, rank node ≠ 0)
    (hvolume : ∀ node, volume node ≠ 0) (volumeWeight : ℝ) :
    Real.log (targetCount ^ 6 * ∏ node, labels node ^ copies node) -
      Real.log (∏ node, rank node ^ copies node) +
      volumeWeight * Real.log (∏ node, volume node ^ copies node) =
      6 * Real.log targetCount +
        ∑ node, (copies node : ℝ) *
          (Real.log (labels node) - Real.log (rank node) +
            volumeWeight * Real.log (volume node)) := by
  rw [log_direct_multiplicity targetCount htarget copies labels hlabels,
    log_resource_product copies rank hrank, log_resource_product copies volume hvolume]
  simp only [mul_add, mul_sub, Finset.sum_add_distrib, Finset.sum_sub_distrib]
  rw [Finset.mul_sum]
  have hvolumeSum :
      (∑ node, volumeWeight * ((copies node : ℝ) * Real.log (volume node))) =
        ∑ node, (copies node : ℝ) * (volumeWeight * Real.log (volume node)) := by
    apply Finset.sum_congr rfl
    intro node _
    ring
  rw [hvolumeSum]
  ring

theorem physical_rate_normalization (bank horizon : ℕ)
    (hbank : bank ≠ 0) (hhorizon : horizon ≠ 0)
    (blockSize copies : Node → ℕ) (hsize : ∀ node, blockSize node ≠ 0)
    (population rawNodeUtility : Node → ℝ) (targetLog : ℝ)
    (tiling : ∀ node, (bank : ℝ) * population node =
      (copies node : ℝ) * blockSize node) :
    (6 * targetLog + ∑ node, (copies node : ℝ) * rawNodeUtility node) /
        (6 * bank * horizon) =
      (targetLog / bank + totalNodeUtility population
        (fun node => rawNodeUtility node / (6 * blockSize node))) / horizon := by
  have hbank' : (bank : ℝ) ≠ 0 := Nat.cast_ne_zero.mpr hbank
  have hhorizon' : (horizon : ℝ) ≠ 0 := Nat.cast_ne_zero.mpr hhorizon
  have hnode (node : Node) :
      population node * (rawNodeUtility node / (6 * blockSize node)) =
        (copies node : ℝ) * rawNodeUtility node / (6 * bank) := by
    have hsize' : (blockSize node : ℝ) ≠ 0 := Nat.cast_ne_zero.mpr (hsize node)
    have hp : population node = (copies node : ℝ) * blockSize node / bank := by
      apply (eq_div_iff hbank').mpr
      simpa only [mul_comm] using tiling node
    rw [hp]
    field_simp [hbank', hsize']
  simp only [totalNodeUtility, hnode, ← Finset.sum_div]
  field_simp [hbank', hhorizon']

theorem physical_cost_rate_identity {Grade : Type*} [Fintype Grade]
    (bank horizon : ℕ) (hbank : bank ≠ 0) (hhorizon : horizon ≠ 0)
    (blockSize copies : Node → ℕ) (hsize : ∀ node, blockSize node ≠ 0)
    (population rawNodeUtility : Node → ℝ) (law : Node → Grade → ℝ)
    (targetLog offset : ℝ) (coefficient : Grade → ℝ)
    (tiling : ∀ node, (bank : ℝ) * population node =
      (copies node : ℝ) * blockSize node)
    (total : ∑ node, population node = (horizon : ℝ)) :
    (6 * targetLog + ∑ node, (copies node : ℝ) * rawNodeUtility node) /
        (6 * bank * horizon) =
      affineScore offset coefficient (outputLaw horizon population law) +
        (targetLog / bank - totalCost population law offset coefficient
          (fun node => rawNodeUtility node / (6 * blockSize node))) / horizon := by
  rw [physical_rate_normalization bank horizon hbank hhorizon blockSize copies
    hsize population rawNodeUtility targetLog tiling]
  exact utility_rate_identity horizon hhorizon population law total offset
    (targetLog / bank) coefficient (fun node => rawNodeUtility node / (6 * blockSize node))

end PhysicalNormalization

end OrientationRates
end MatrixMultiplication.Foundation

end

end OAI