File size: 11,216 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
import OAI.LinearAlgebra.MatrixMultiplication.FieldHistory.Counts
import OAI.LinearAlgebra.MatrixMultiplication.FieldHistory.Priority
import OAI.LinearAlgebra.MatrixMultiplication.FieldHistory.Bounds
import OAI.LinearAlgebra.MatrixMultiplication.JointExtraction.PopulationRates

namespace OAI

/-! Tensor extraction over arbitrary fields and its asymptotic rate. -/

noncomputable section

namespace MatrixMultiplication.AllFieldActiveLaws

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

def branchProbability {K : ℕ} (w : Work K) (b : w.Branch) : ℚ :=
  match w with
  | .stageA h => stageALaw (initialShape h.val) (aSplit ⟨h, b, false⟩)
  | .stageB h => stageBLaw (aShape h.val) (bSplit ⟨h, b, false⟩)
  | .stageC h => stageCWeight (cParameterParent h) (cShapeParent h) b

theorem branchProbability_nonneg {K : ℕ} (w : Work K) (b : w.Branch) :
    0 ≤ branchProbability w b := by
  cases w with
  | stageA h => exact (stageALaw_positive _ _ (aSplit_mem ⟨h, b, false⟩)).le
  | stageB h => exact (stageBLaw_positive _ _ (bSplit_mem ⟨h, b, false⟩)).le
  | stageC h => exact stageCWeight_nonnegative _ _ b

theorem branchProbability_total {K : ℕ} (w : Work K) :
    ∑ b : w.Branch, branchProbability w b = 1 := by
  cases w with
  | stageA h =>
      change (∑ b : ASplit h,
        stageALaw (initialShape h.val) (below (initialShape h.val))[b.val]) = 1
      rw [Fin.sum_univ_fun_getElem]
      exact stageALaw_normalized _ h.property
  | stageB h =>
      change (∑ b : BSplit h,
        stageBLaw (aShape h.val) (below (aShape h.val))[b.val]) = 1
      rw [Fin.sum_univ_fun_getElem]
      exact stageBLaw_normalized _ h.property
  | stageC h => exact stageCWeight_normalized _ _

def branchLaw {K : ℕ} (w : Work K) : FiniteLaw w.Branch where
  mass b := (branchProbability w b : ℝ)
  nonneg b := by exact_mod_cast branchProbability_nonneg w b
  total := by exact_mod_cast branchProbability_total w

@[simp] theorem branchLaw_mass {K : ℕ} (w : Work K) (b : w.Branch) :
    (branchLaw w).mass b = (branchProbability w b : ℝ) := rfl

def placedLaw {K : ℕ} (w : PlacedWork K) : FiniteLaw JointPopulation.Shape :=
  (branchLaw w.1).map (branchShape w)

@[simp] theorem placedLaw_mass {K : ℕ} (w : PlacedWork K)
    (u : JointPopulation.Shape) :
    (placedLaw w).mass u =
      ∑ b : w.1.Branch, if branchShape w b = u then
        (branchProbability w.1 b : ℝ) else 0 := by
  simp only [placedLaw, FiniteLaw.map_mass, branchLaw_mass]

theorem placedLaw_mass_branch {K : ℕ} (w : PlacedWork K) (b : w.1.Branch) :
    (placedLaw w).mass (branchShape w b) = (branchProbability w.1 b : ℝ) :=
  FiniteLaw.map_mass_apply _ _ (branchShape_injective w) b

theorem placedLaw_entropy {K : ℕ} (w : PlacedWork K) :
    finiteEntropy (placedLaw w).mass = finiteEntropy (branchLaw w.1).mass :=
  FiniteLaw.map_entropy_of_injective _ _ (branchShape_injective w)

theorem placedLaw_sideMass {K : ℕ} (w : PlacedWork K) (s : Fin 3)
    (k : Fin 17) :
    JointPopulationRates.sideMass (placedLaw w).mass s k =
      ∑ b : w.1.Branch, if JointPopulation.shapeSide s (branchShape w b) = k
        then (branchProbability w.1 b : ℝ) else 0 := by
  unfold JointPopulationRates.sideMass
  simp only [placedLaw_mass]
  calc
    _ = ∑ u : JointPopulation.Shape, ∑ b : w.1.Branch,
        if branchShape w b = u then
          if JointPopulation.shapeSide s u = k then (branchProbability w.1 b : ℝ) else 0
        else 0 := by
      apply Finset.sum_congr rfl
      intro u _
      by_cases hu : JointPopulation.shapeSide s u = k <;> simp [hu]
    _ = _ := by rw [Finset.sum_comm]; simp

theorem branchPopulation_cast_rat {K : ℕ} (allocation : Allocation) (dilation : ℕ)
    (w : PlacedWork K) (b : w.1.Branch) :
    (branchPopulation allocation dilation w b : ℚ) =
      (population allocation dilation (w.1.source, w.2) : ℚ) *
        branchProbability w.1 b := by
  rcases w with ⟨w, phi⟩
  cases w <;>
    simp only [branchPopulation, Work.child, Work.source, population_cast,
      amount, canonicalAmount, aAmount, bAmount, cAmount, branchProbability] <;> ring

theorem branchPopulation_cast {K : ℕ} (allocation : Allocation) (dilation : ℕ)
    (w : PlacedWork K) (b : w.1.Branch) :
    (branchPopulation allocation dilation w b : ℝ) =
      (population allocation dilation (w.1.source, w.2) : ℝ) *
        (branchLaw w.1).mass b := by
  rw [branchLaw_mass]
  exact_mod_cast branchPopulation_cast_rat allocation dilation w b

theorem jointCounts_cast {K : ℕ} (allocation : Allocation) (dilation : ℕ)
    (w : PlacedWork K) (u : JointPopulation.Shape) :
    (jointCounts allocation dilation w u : ℝ) =
      (population allocation dilation (w.1.source, w.2) : ℝ) *
        (placedLaw w).mass u := by
  simp only [jointCounts, shapeCounts, Nat.cast_sum, placedLaw_mass, Finset.mul_sum]
  apply Finset.sum_congr rfl
  intro b _
  by_cases hb : branchShape w b = u
  · simp only [hb, ite_true, branchPopulation_cast, branchLaw_mass]
  · simp [hb]

theorem jointCounts_ratio {K dilation : ℕ} (allocation : Allocation)
    (hd : 0 < dilation) (w : PlacedWork K) (u : JointPopulation.Shape) :
    (jointCounts allocation dilation w u : ℝ) /
      population allocation dilation (w.1.source, w.2) = (placedLaw w).mass u := by
  rw [jointCounts_cast]
  have hp : (population allocation dilation (w.1.source, w.2) : ℝ) ≠ 0 := by
    exact_mod_cast Nat.ne_of_gt (work_source_population_pos allocation hd w)
  exact mul_div_cancel_left₀ _ hp

theorem jointCounts_normalized {K dilation : ℕ} (allocation : Allocation)
    (hd : 0 < dilation) (w : PlacedWork K) :
    (∑ u, (jointCounts allocation dilation w u : ℝ) /
      population allocation dilation (w.1.source, w.2)) = 1 := by
  simp only [jointCounts_ratio allocation hd]
  exact (placedLaw w).total

theorem population_cast_real {K : ℕ} (allocation : Allocation) (dilation : ℕ)
    (h : History K) :
    (population allocation dilation h : ℝ) =
      (populationLength (K := K) allocation dilation : ℝ) * (amount allocation h : ℝ) := by
  exact_mod_cast population_cast allocation dilation h

theorem jointCounts_source_cast {K : ℕ} (allocation : Allocation) (dilation : ℕ)
    (w : PlacedWork K) (u : JointPopulation.Shape) :
    (jointCounts allocation dilation w u : ℝ) =
      (populationLength (K := K) allocation dilation : ℝ) *
        ((amount allocation (w.1.source, w.2) : ℝ) * (placedLaw w).mass u) := by
  rw [jointCounts_cast, population_cast_real, mul_assoc]

theorem jointCounts_source_ratio {K dilation : ℕ} (allocation : Allocation)
    (hd : 0 < dilation) (w : PlacedWork K) (u : JointPopulation.Shape) :
    (jointCounts allocation dilation w u : ℝ) /
      populationLength (K := K) allocation dilation =
        (amount allocation (w.1.source, w.2) : ℝ) * (placedLaw w).mass u := by
  rw [jointCounts_source_cast]
  have hp : (populationLength (K := K) allocation dilation : ℝ) ≠ 0 := by
    exact_mod_cast Nat.ne_of_gt
      (AllFieldPopulationCounts.blockLength_pos (amount (K := K) allocation) hd)
  exact mul_div_cancel_left₀ _ hp

theorem jointCounts_source_rate {K : ℕ} (allocation : Allocation)
    (w : PlacedWork K) (u : JointPopulation.Shape) :
    Tendsto (fun dilation : ℕ => (jointCounts allocation dilation w u : ℝ) /
      populationLength (K := K) allocation dilation) atTop
      (𝓝 ((amount allocation (w.1.source, w.2) : ℝ) * (placedLaw w).mass u)) := by
  apply tendsto_const_nhds.congr'
  filter_upwards [eventually_gt_atTop (0 : ℕ)] with dilation hd
  exact (jointCounts_source_ratio allocation hd w u).symm

theorem populationLength_dilation {K : ℕ} (allocation : Allocation) (dilation : ℕ) :
    populationLength (K := K) allocation dilation =
      dilation * populationLength (K := K) allocation 1 := by
  simp only [populationLength, AllFieldPopulationCounts.blockLength, one_mul]

theorem jointCounts_dilation_ratio {K dilation : ℕ} (allocation : Allocation)
    (hd : 0 < dilation) (w : PlacedWork K) (u : JointPopulation.Shape) :
    (jointCounts allocation dilation w u : ℝ) / (dilation : ℝ) =
      ((populationLength (K := K) allocation 1 : ℝ) *
        (amount allocation (w.1.source, w.2) : ℝ)) * (placedLaw w).mass u := by
  rw [jointCounts_source_cast, populationLength_dilation, Nat.cast_mul]
  have hd' : (dilation : ℝ) ≠ 0 := Nat.cast_ne_zero.mpr (Nat.ne_of_gt hd)
  field_simp [hd']

theorem jointCounts_dilation_rate {K : ℕ} (allocation : Allocation)
    (w : PlacedWork K) (u : JointPopulation.Shape) :
    Tendsto (fun dilation : ℕ => (jointCounts allocation dilation w u : ℝ) /
      (dilation : ℝ)) atTop
      (𝓝 (((populationLength (K := K) allocation 1 : ℝ) *
        (amount allocation (w.1.source, w.2) : ℝ)) * (placedLaw w).mass u)) := by
  apply tendsto_const_nhds.congr'
  filter_upwards [eventually_gt_atTop (0 : ℕ)] with dilation hd
  exact (jointCounts_dilation_ratio allocation hd w u).symm

def orderLaw {K tick : ℕ} {sigma : Placement} (h : ActiveOrder K tick sigma) :
    FiniteLaw JointPopulation.Shape := placedLaw h.val.val

def orderMass {K tick : ℕ} {sigma : Placement} (allocation : Allocation)
    (h : ActiveOrder K tick sigma) : ℝ :=
  amount allocation (h.val.val.1.source, h.val.val.2)

def orderCounts {K tick : ℕ} {sigma : Placement} (allocation : Allocation)
    (dilation : ℕ) (h : ActiveOrder K tick sigma) : JointPopulation.Shape → ℕ :=
  activeCounts allocation dilation h.val

theorem orderMass_nonneg {K tick : ℕ} {sigma : Placement} (allocation : Allocation)
    (h : ActiveOrder K tick sigma) : 0 ≤ orderMass allocation h := by
  unfold orderMass
  exact_mod_cast amount_nonneg allocation (h.val.val.1.source, h.val.val.2)

theorem orderCounts_source_cast {K tick : ℕ} {sigma : Placement}
    (allocation : Allocation) (dilation : ℕ) (h : ActiveOrder K tick sigma)
    (u : JointPopulation.Shape) :
    (orderCounts allocation dilation h u : ℝ) =
      (populationLength (K := K) allocation dilation : ℝ) *
        (orderMass allocation h * (orderLaw h).mass u) :=
  jointCounts_source_cast allocation dilation h.val.val u

theorem orderCounts_source_rate {K tick : ℕ} {sigma : Placement}
    (allocation : Allocation) (h : ActiveOrder K tick sigma) (u : JointPopulation.Shape) :
    Tendsto (fun dilation : ℕ => (orderCounts allocation dilation h u : ℝ) /
      populationLength (K := K) allocation dilation) atTop
      (𝓝 (orderMass allocation h * (orderLaw h).mass u)) :=
  jointCounts_source_rate allocation h.val.val u

theorem orderCounts_dilation_rate {K tick : ℕ} {sigma : Placement}
    (allocation : Allocation) (h : ActiveOrder K tick sigma) (u : JointPopulation.Shape) :
    Tendsto (fun dilation : ℕ => (orderCounts allocation dilation h u : ℝ) /
      (dilation : ℝ)) atTop
      (𝓝 (((populationLength (K := K) allocation 1 : ℝ) * orderMass allocation h) *
        (orderLaw h).mass u)) :=
  jointCounts_dilation_rate allocation h.val.val u

end MatrixMultiplication.AllFieldActiveLaws

end

end OAI