File size: 9,302 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
import OAI.LinearAlgebra.MatrixMultiplication.FieldHistory.Populations
import OAI.LinearAlgebra.MatrixMultiplication.Entropy.ConditionalRateBridge

namespace OAI

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

noncomputable section

namespace MatrixMultiplication.AllFieldTerminalPopulations

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

variable {K : ℕ} {H : Type*} [Fintype H]
  {A : H → Type*} [∀ h, Fintype (A h)]

def law (ν : ∀ h, A h → ℚ) (x : Sigma A) : ℚ := ν x.1 x.2

def denominator (ν : ∀ h, A h → ℚ) : ℕ :=
  AllFieldPopulationCounts.denominator (law ν)

def dilation (ν : ∀ h, A h → ℚ) (m : ℕ) : ℕ := m * denominator ν

def count (allocation : Allocation) (history : H → History K)
    (ν : ∀ h, A h → ℚ) (m : ℕ) (h : H) (a : A h) : ℕ :=
  population allocation m (history h) *
    AllFieldPopulationCounts.baseCount (law ν) ⟨h, a⟩

theorem denominator_pos (ν : ∀ h, A h → ℚ) : 0 < denominator ν :=
  AllFieldPopulationCounts.denominator_pos (law ν)

theorem dilation_pos (ν : ∀ h, A h → ℚ) {m : ℕ} (hm : 0 < m) :
    0 < dilation ν m := Nat.mul_pos hm (denominator_pos ν)

theorem baseCount_cast (ν : ∀ h, A h → ℚ) (hν : ∀ h a, 0 ≤ ν h a)
    (h : H) (a : A h) :
    (AllFieldPopulationCounts.baseCount (law ν) ⟨h, a⟩ : ℚ) =
      (denominator ν : ℚ) * ν h a :=
  AllFieldPopulationCounts.baseCount_cast (law ν) (fun x => hν x.1 x.2) ⟨h, a⟩

theorem sum_baseCount (ν : ∀ h, A h → ℚ) (hν : ∀ h a, 0 ≤ ν h a)
    (htotal : ∀ h, ∑ a, ν h a = 1) (h : H) :
    (∑ a, AllFieldPopulationCounts.baseCount (law ν) ⟨h, a⟩) = denominator ν := by
  apply Nat.cast_injective (R := ℚ)
  simp only [Nat.cast_sum, baseCount_cast ν hν, ← Finset.mul_sum, htotal, mul_one]

theorem population_dilation (allocation : Allocation) (ν : ∀ h, A h → ℚ)
    (m : ℕ) (h : History K) :
    population allocation (dilation ν m) h =
      population allocation m h * denominator ν := by
  simp only [population, AllFieldPopulationCounts.count, dilation]
  ac_rfl

theorem populationLength_dilation (allocation : Allocation) (ν : ∀ h, A h → ℚ)
    (m : ℕ) :
    populationLength (K := K) allocation (dilation ν m) =
      populationLength (K := K) allocation m * denominator ν := by
  simp only [populationLength, AllFieldPopulationCounts.blockLength, dilation]
  ac_rfl

theorem sum_count (allocation : Allocation) (history : H → History K)
    (ν : ∀ h, A h → ℚ) (hν : ∀ h a, 0 ≤ ν h a)
    (htotal : ∀ h, ∑ a, ν h a = 1) (m : ℕ) (h : H) :
    (∑ a, count allocation history ν m h a) =
      population allocation (dilation ν m) (history h) := by
  simp only [count, ← Finset.mul_sum, sum_baseCount ν hν htotal,
    population_dilation]

theorem count_cast (allocation : Allocation) (history : H → History K)
    (ν : ∀ h, A h → ℚ) (hν : ∀ h a, 0 ≤ ν h a)
    (m : ℕ) (h : H) (a : A h) :
    (count allocation history ν m h a : ℚ) =
      (populationLength (K := K) allocation (dilation ν m) : ℚ) *
        amount allocation (history h) * ν h a := by
  rw [count, Nat.cast_mul, population_cast, baseCount_cast ν hν,
    populationLength_dilation, Nat.cast_mul]
  ring

theorem count_cast_real (allocation : Allocation) (history : H → History K)
    (ν : ∀ h, A h → ℚ) (hν : ∀ h a, 0 ≤ ν h a)
    (m : ℕ) (h : H) (a : A h) :
    (count allocation history ν m h a : ℝ) =
      (populationLength (K := K) allocation (dilation ν m) : ℝ) *
        (amount allocation (history h) : ℝ) * (ν h a : ℝ) := by
  exact_mod_cast count_cast allocation history ν hν m h a

theorem count_dilate (allocation : Allocation) (history : H → History K)
    (ν : ∀ h, A h → ℚ) (m n : ℕ) (h : H) (a : A h) :
    count allocation history ν (m * n) h a =
      m * count allocation history ν n h a := by
  simp only [count, population, AllFieldPopulationCounts.count]
  ac_rfl

theorem count_mul_one (allocation : Allocation) (history : H → History K)
    (ν : ∀ h, A h → ℚ) (m : ℕ) (h : H) (a : A h) :
    count allocation history ν m h a = m * count allocation history ν 1 h a := by
  simpa only [Nat.mul_one] using count_dilate allocation history ν m 1 h a

theorem source_population_pos (allocation : Allocation) (ν : ∀ h, A h → ℚ)
    {m : ℕ} (hm : 0 < m) (h : History K) (hh : 0 < amount allocation h) :
    0 < population allocation (dilation ν m) h :=
  (AllFieldPopulationCounts.count_pos_iff (amount allocation)
    (amount_nonneg allocation) (dilation_pos ν hm) h).2 hh

theorem sum_count_pos (allocation : Allocation) (history : H → History K)
    (ν : ∀ h, A h → ℚ) (hν : ∀ h a, 0 ≤ ν h a)
    (htotal : ∀ h, ∑ a, ν h a = 1) {m : ℕ} (hm : 0 < m)
    (h : H) (hh : 0 < amount allocation (history h)) :
    0 < ∑ a, count allocation history ν m h a := by
  rw [sum_count allocation history ν hν htotal]
  exact source_population_pos allocation ν hm (history h) hh

theorem count_positive_law_positive (allocation : Allocation) (history : H → History K)
    (ν : ∀ h, A h → ℚ) (hν : ∀ h a, 0 ≤ ν h a)
    (m : ℕ) (h : H) (a : A h) (ha : 0 < count allocation history ν m h a) :
    0 < ν h a := by
  have hb : 0 < AllFieldPopulationCounts.baseCount (law ν) ⟨h, a⟩ :=
    Nat.pos_of_mul_pos_left ha
  have hbq : (0 : ℚ) < AllFieldPopulationCounts.baseCount (law ν) ⟨h, a⟩ := by
    exact_mod_cast hb
  rw [baseCount_cast ν hν] at hbq
  have hD : (0 : ℚ) < denominator ν := by exact_mod_cast denominator_pos ν
  exact (mul_pos_iff_of_pos_left hD).mp hbq

theorem count_pos_iff (allocation : Allocation) (history : H → History K)
    (ν : ∀ h, A h → ℚ) (hν : ∀ h a, 0 ≤ ν h a)
    {m : ℕ} (hm : 0 < m) (h : H) (hh : 0 < amount allocation (history h))
    (a : A h) : 0 < count allocation history ν m h a ↔ 0 < ν h a := by
  constructor
  · exact count_positive_law_positive allocation history ν hν m h a
  · intro ha
    apply Nat.mul_pos
    · exact (AllFieldPopulationCounts.count_pos_iff (amount allocation)
        (amount_nonneg allocation) hm (history h)).2 hh
    · have hb := (AllFieldPopulationCounts.count_pos_iff (law ν)
        (fun x => hν x.1 x.2) (m := 1) (by decide) ⟨h, a⟩).2 ha
      simpa only [AllFieldPopulationCounts.count, one_mul] using hb

theorem count_eq_zero_of_law_eq_zero (allocation : Allocation) (history : H → History K)
    (ν : ∀ h, A h → ℚ) (hν : ∀ h a, 0 ≤ ν h a)
    (m : ℕ) (h : H) (a : A h) (ha : ν h a = 0) :
    count allocation history ν m h a = 0 := by
  apply Nat.cast_injective (R := ℚ)
  simp only [count_cast allocation history ν hν, ha, mul_zero, Nat.cast_zero]

theorem count_ratio (allocation : Allocation) (history : H → History K)
    (ν : ∀ h, A h → ℚ) (hν : ∀ h a, 0 ≤ ν h a)
    (htotal : ∀ h, ∑ a, ν h a = 1) {m : ℕ} (hm : 0 < m)
    (h : H) (hh : 0 < amount allocation (history h)) (a : A h) :
    (count allocation history ν m h a : ℚ) /
      (∑ b, count allocation history ν m h b : ℕ) = ν h a := by
  rw [sum_count allocation history ν hν htotal, count_cast allocation history ν hν,
    population_cast]
  have hp : (populationLength (K := K) allocation (dilation ν m) : ℚ) *
      amount allocation (history h) ≠ 0 := by
    rw [← population_cast]
    exact_mod_cast (Nat.ne_of_gt (source_population_pos allocation ν hm (history h) hh))
  exact mul_div_cancel_left₀ _ hp

theorem count_ratio_real (allocation : Allocation) (history : H → History K)
    (ν : ∀ h, A h → ℚ) (hν : ∀ h a, 0 ≤ ν h a)
    (htotal : ∀ h, ∑ a, ν h a = 1) {m : ℕ} (hm : 0 < m)
    (h : H) (hh : 0 < amount allocation (history h)) (a : A h) :
    (count allocation history ν m h a : ℝ) /
      (∑ b, count allocation history ν m h b : ℕ) = (ν h a : ℝ) := by
  have hq := congrArg (fun q : ℚ => (q : ℝ))
    (count_ratio allocation history ν hν htotal hm h hh a)
  simpa only [Rat.cast_div, Rat.cast_natCast] using hq

theorem normalizedCountLaw_mass (allocation : Allocation) (history : H → History K)
    (ν : ∀ h, A h → ℚ) (hν : ∀ h a, 0 ≤ ν h a)
    (htotal : ∀ h, ∑ a, ν h a = 1) {m : ℕ} (hm : 0 < m)
    (h : H) (hh : 0 < amount allocation (history h)) (a : A h) :
    (ConditionalLabels.normalizedCountLaw (count allocation history ν m h)
      (sum_count_pos allocation history ν hν htotal hm h hh)).mass a = (ν h a : ℝ) :=
  count_ratio_real allocation history ν hν htotal hm h hh a

theorem normalizedCountLaw_mass_eq (allocation : Allocation) (history : H → History K)
    (ν : ∀ h, A h → ℚ) (hν : ∀ h a, 0 ≤ ν h a)
    (htotal : ∀ h, ∑ a, ν h a = 1) {m : ℕ} (hm : 0 < m)
    (h : H) (hh : 0 < amount allocation (history h)) :
    (ConditionalLabels.normalizedCountLaw (count allocation history ν m h)
      (sum_count_pos allocation history ν hν htotal hm h hh)).mass =
        fun a => (ν h a : ℝ) :=
  funext (normalizedCountLaw_mass allocation history ν hν htotal hm h hh)

end MatrixMultiplication.AllFieldTerminalPopulations

end

end OAI