File size: 19,423 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
import OAI.LinearAlgebra.MatrixMultiplication.JointExtraction.CompatibilityRateLimit
import OAI.LinearAlgebra.MatrixMultiplication.JointExtraction.CompatibilityControls

namespace OAI

/-! Joint tensor extraction, compatibility and entropy estimates. -/

noncomputable section

namespace MatrixMultiplication.JointCompatibilityCountProducer

open MatrixMultiplication.Foundation JointTypeCounts JointCompatibilityIncidence
open JointCompatibilityControls JointCompatibilityScaling JointCompatibilityRateLimit Filter
open scoped BigOperators Topology

attribute [local instance] Classical.propDecidable

private theorem exists_pos_le_family {K : Type*} [Fintype K]
    (δ : K → ℝ) (hδ : ∀ k, 0 < δ k) :
    ∃ ε : ℝ, 0 < ε ∧ ∀ k, ε ≤ δ k := by
  classical
  have aux : ∀ s : Finset K, ∃ ε : ℝ, 0 < ε ∧ ∀ k ∈ s, ε ≤ δ k := by
    intro s
    induction s using Finset.induction_on with
    | empty => exact ⟨1, zero_lt_one, by simp⟩
    | @insert k s hk ih =>
        obtain ⟨ε, hε, hb⟩ := ih
        refine ⟨min (δ k) ε, lt_min (hδ k) hε, ?_⟩
        intro j hj
        rcases Finset.mem_insert.mp hj with rfl | hj
        · exact min_le_left _ _
        · exact (min_le_right _ _).trans (hb j hj)
  obtain ⟨ε, hε, hb⟩ := aux Finset.univ
  exact ⟨ε, hε, fun k => hb k (Finset.mem_univ k)⟩

variable {C : Type*} [Fintype C] [DecidableEq C]
  {Shape A : C → Type*}
  [∀ c, Fintype (Shape c)] [∀ c, DecidableEq (Shape c)]
  [∀ c, Fintype (A c)] [∀ c, DecidableEq (A c)]

omit [DecidableEq C] [∀ c, DecidableEq (Shape c)] [∀ c, DecidableEq (A c)] in
private theorem exists_group_entropy_window (base : ∀ c, Shape c → ℕ)
    (d : ∀ c, Shape c → Prop) (law : ∀ c, Shape c → A c → ℝ)
    {ε : ℝ} (hε : 0 < ε) :
    ∃ δ : ℝ, 0 < δ ∧ ∀ (g : Σ c, Option (Shape c)) (q : A g.1 → ℝ),
      (∀ a, |q a - groupCenter base d law g a| ≤ δ) →
      finiteEntropy q ≤ groupCap base d law ε g := by
  have hex : ∀ g : Σ c, Option (Shape c), ∃ δ : ℝ, 0 < δ ∧
      ∀ q : A g.1 → ℝ, (∀ a, |q a - groupCenter base d law g a| ≤ δ) →
        |finiteEntropy q - finiteEntropy (groupCenter base d law g)| < ε :=
    fun g => exists_entropy_window (groupCenter base d law g) hε
  choose δ hδ hw using hex
  obtain ⟨δ₀, hδ₀, hsmall⟩ := exists_pos_le_family δ hδ
  refine ⟨δ₀, hδ₀, ?_⟩
  intro g q hq
  have hh := (abs_lt.mp (hw g q (fun a => (hq a).trans (hsmall g)))).2
  change finiteEntropy q ≤ finiteEntropy (groupCenter base d law g) + ε
  linarith

omit [Fintype C] [DecidableEq C] in
private theorem repeated_residual_width_le
    {Pos : C → Type*} [∀ c, Fintype (Pos c)] [∀ c, DecidableEq (Pos c)]
    (base : ∀ c, Shape c → ℕ) (d : ∀ c, Shape c → Prop)
    (t : ℕ) (w : FixedClassWords Pos Shape (fun c u => t * base c u))
    (χ η δ : ℝ) (hδ : 0 ≤ δ)
    (hnum : ∀ c, η + (designatedBase base d c : ℝ) * χ ≤ δ) (c : C) :
    ((t : ℝ) * η + (∑ u : {u : Shape c // d c u}, ((t * base c u.val : ℕ) : ℝ)) * χ) /
        Fintype.card {i : Pos c // ¬d c ((w c).val i)} ≤ δ := by
  have hsum : (∑ u : {u : Shape c // d c u}, ((t * base c u.val : ℕ) : ℝ)) =
      (t : ℝ) * designatedBase base d c := by
    simp only [Nat.cast_mul, designatedBase, Nat.cast_sum, Finset.mul_sum]
  rw [residual_card_repeated, hsum, Nat.cast_mul]
  by_cases ht : t = 0
  · simpa only [ht, Nat.cast_zero, zero_mul, add_zero, zero_add, zero_div] using hδ
  by_cases hr : residualBase base d c = 0
  · simpa only [hr, Nat.cast_zero, mul_zero, div_zero] using hδ
  have htpos : (0 : ℝ) < t := Nat.cast_pos.mpr (Nat.pos_of_ne_zero ht)
  have hrpos : (0 : ℝ) < residualBase base d c := Nat.cast_pos.mpr (Nat.pos_of_ne_zero hr)
  have hrone : (1 : ℝ) ≤ residualBase base d c := by exact_mod_cast Nat.one_le_iff_ne_zero.mpr hr
  have hres : η + (designatedBase base d c : ℝ) * χ ≤ δ * residualBase base d c :=
    (hnum c).trans (by simpa only [mul_one] using mul_le_mul_of_nonneg_left hrone hδ)
  apply (div_le_iff₀ (mul_pos htpos hrpos)).mpr
  calc
    (t : ℝ) * η + ((t : ℝ) * designatedBase base d c) * χ =
        (t : ℝ) * (η + (designatedBase base d c : ℝ) * χ) := by ring
    _ ≤ (t : ℝ) * (δ * residualBase base d c) :=
      mul_le_mul_of_nonneg_left hres htpos.le
    _ = δ * ((t : ℝ) * residualBase base d c) := by ring

omit [DecidableEq C] in
theorem exists_uniform_sideEntropyControl_all_positions
    (Pair : C → Type*) [∀ c, Fintype (Pair c)] [∀ c, DecidableEq (Pair c)]
    (base : ∀ c, Shape c → ℕ) (d : ∀ c, Shape c → Prop)
    (law : ∀ c, Shape c → A c → ℝ) (part : ∀ c, Pair c → A c)
    (h0 : ∀ c u a, 0 ≤ law c u a) (h1 : ∀ c u a, law c u a ≤ 1)
    {ε χMax ηMax : ℝ} (hε : 0 < ε) (hχMax : 0 < χMax) (hηMax : 0 < ηMax) :
    ∃ χ η : ℝ, 0 < χ ∧ χ ≤ χMax ∧ 0 < η ∧ η ≤ ηMax ∧
      ∀ (t : ℕ) (Pos : C → Type*) [∀ c, Fintype (Pos c)] [∀ c, DecidableEq (Pos c)]
        (pairCounts : ∀ c, Pair c → ℕ)
        (w : FixedClassWords Pos Shape (fun c u => t * base c u)),
        (∀ c a, |((∑ s : {s : Pair c // part c s = a}, pairCounts c s.val : ℕ) : ℝ) -
          (t : ℝ) * totalCenterCount base law c a| ≤ (t : ℝ) * η) →
        Nonempty (SideEntropyControl Pos Shape Pair A (fun c u => t * base c u)
          pairCounts part d law χ (groupCap base d law ε) w) := by
  obtain ⟨δ, hδ, hwindow⟩ := exists_group_entropy_window base d law hε
  let D : ℕ := ∑ c, designatedBase base d c
  have hD : (0 : ℝ) ≤ D := Nat.cast_nonneg _
  have hden : 0 < 2 * ((D : ℝ) + 1) := by positivity
  let χ₀ : ℝ := δ / (2 * ((D : ℝ) + 1))
  have hχ₀ : 0 < χ₀ := div_pos hδ hden
  have heq : (2 * ((D : ℝ) + 1)) * χ₀ = δ := mul_div_cancel₀ _ hden.ne'
  have hχδ : χ₀ ≤ δ := by nlinarith [mul_nonneg hD hχ₀.le]
  have hDχ : (D : ℝ) * χ₀ ≤ δ / 2 := by nlinarith
  let χ := min χMax χ₀
  let η := min ηMax (δ / 2)
  have hχ : 0 < χ := lt_min hχMax hχ₀
  have hη : 0 < η := lt_min hηMax (half_pos hδ)
  have hχδ' : χ ≤ δ := (min_le_right _ _).trans hχδ
  have hnum (c : C) : η + (designatedBase base d c : ℝ) * χ ≤ δ := by
    have hc : (designatedBase base d c : ℝ) ≤ D := by
      exact_mod_cast (Finset.single_le_sum (fun _ _ => Nat.zero_le _)
        (Finset.mem_univ c) : designatedBase base d c ≤ ∑ c, designatedBase base d c)
    have hm : (designatedBase base d c : ℝ) * χ ≤ (D : ℝ) * χ₀ :=
      mul_le_mul hc (min_le_right _ _) hχ.le hD
    have he : η ≤ δ / 2 := min_le_right _ _
    linarith
  refine ⟨χ, η, hχ, min_le_left _ _, hη, min_le_left _ _, ?_⟩
  intro t Pos _ _ pairCounts w haggr
  refine ⟨{
    residualLaw := residualCenter base d law
    totalTarget := fun c a => (t : ℝ) * totalCenterCount base law c a
    totalError := fun _ => (t : ℝ) * η
    aggregate_window := haggr
    mixture_balance := repeated_mixture_balance base d law t w
    nonneg := groupCap_nonneg base d law h0 h1 hε.le
    designated_entropy := ?_
    residual_entropy := ?_ }⟩
  · intro c u _ q hq
    exact hwindow ⟨c, some u⟩ q (fun a => (hq a).trans hχδ')
  · intro c q hq
    exact hwindow ⟨c, none⟩ q (fun a => (hq a).trans
      (repeated_residual_width_le base d t w χ η δ hδ.le hnum c))

section PairMarginals

variable {Pair : C → Type*}
  [∀ c, Fintype (Pair c)] [∀ c, DecidableEq (Pair c)]

def aggregateCoefficient (base : ∀ c, Shape c → ℕ)
    (Pair : C → Type*) [∀ c, Fintype (Pair c)] : ℕ :=
  ∑ c, baseSize base c * Fintype.card (Pair c)

omit [DecidableEq C] [∀ c, Fintype (A c)] in
theorem aggregate_error_of_pairWindow
    (base : ∀ c, Shape c → ℕ) (q : ∀ c, Pair c → ℝ)
    (law : ∀ c, Shape c → A c → ℝ) (part : ∀ c, Pair c → A c)
    (hbalance : ∀ c a, (baseSize base c : ℝ) *
      (∑ b : {b : Pair c // part c b = a}, q c b.val) = totalCenterCount base law c a)
    (t : ℕ) (ht : 0 < t) (Pos : C → Type*)
    [∀ c, Fintype (Pos c)] [∀ c, DecidableEq (Pos c)]
    (pairCounts : ∀ c, Pair c → ℕ)
    (w : FixedClassWords Pos Shape (fun c u => t * base c u))
    (z : FixedClassWords Pos Pair pairCounts) {η : ℝ} (hη : 0 ≤ η)
    (hp : PairWindow base q Pos pairCounts η) (c : C) (a : A c) :
    |((∑ b : {b : Pair c // part c b = a}, pairCounts c b.val : ℕ) : ℝ) -
      (t : ℝ) * totalCenterCount base law c a| ≤
      (t : ℝ) * (aggregateCoefficient base Pair : ℝ) * η := by
  by_cases hz : baseSize base c = 0
  · have hsum : (∑ b, pairCounts c b) = 0 := by
      rw [← JointCompatibilityEntropyBound.fixedClassWords_size Pos Pair pairCounts z c,
        repeated_positions_card Pos base t w c, hz, Nat.mul_zero]
    have hcount (b : Pair c) : pairCounts c b = 0 :=
      Nat.eq_zero_of_le_zero ((Finset.single_le_sum (fun _ _ => Nat.zero_le _)
        (Finset.mem_univ b)).trans_eq hsum)
    have hcenter : totalCenterCount base law c a = 0 := by
      rw [← hbalance c a, hz, Nat.cast_zero, zero_mul]
    simpa only [hcount, Finset.sum_const_zero, Nat.cast_zero, hcenter, mul_zero,
      sub_self, abs_zero] using
      mul_nonneg (mul_nonneg (Nat.cast_nonneg t)
        (Nat.cast_nonneg (aggregateCoefficient base Pair))) hη
  have hb : 0 < baseSize base c := Nat.pos_of_ne_zero hz
  have hn : (0 : ℝ) < Fintype.card (Pos c) := by
    rw [repeated_positions_card Pos base t w c, Nat.cast_mul]
    exact mul_pos (Nat.cast_pos.mpr ht) (Nat.cast_pos.mpr hb)
  have hcoord (b : Pair c) :
      |(pairCounts c b : ℝ) - (t : ℝ) * (baseSize base c : ℝ) * q c b| ≤
        (t : ℝ) * (baseSize base c : ℝ) * η := by
    have heq : (Fintype.card (Pos c) : ℝ) *
        ((pairCounts c b : ℝ) / Fintype.card (Pos c) - q c b) =
        (pairCounts c b : ℝ) - (Fintype.card (Pos c) : ℝ) * q c b := by
      rw [mul_sub, mul_div_cancel₀ _ hn.ne']
    have h := calc
      |(pairCounts c b : ℝ) - (Fintype.card (Pos c) : ℝ) * q c b| =
          |(Fintype.card (Pos c) : ℝ) *
            ((pairCounts c b : ℝ) / Fintype.card (Pos c) - q c b)| :=
        congrArg abs heq.symm
      _ = (Fintype.card (Pos c) : ℝ) *
          |(pairCounts c b : ℝ) / Fintype.card (Pos c) - q c b| := by
        rw [abs_mul, abs_of_pos hn]
      _ ≤ (Fintype.card (Pos c) : ℝ) * η :=
        mul_le_mul_of_nonneg_left (hp c hb b) hn.le
    simpa only [repeated_positions_card Pos base t w c, Nat.cast_mul] using h
  have hm := marginal_coordinate_error
    (fun b : {b : Pair c // part c b = a} => (pairCounts c b.val : ℝ))
    (fun b => (t : ℝ) * (baseSize base c : ℝ) * q c b.val)
    ((t : ℝ) * (baseSize base c : ℝ) * η) (fun b => hcoord b.val)
  have hsum : (∑ b : {b : Pair c // part c b = a},
      (t : ℝ) * (baseSize base c : ℝ) * q c b.val) =
      (t : ℝ) * totalCenterCount base law c a := by
    rw [← Finset.mul_sum, mul_assoc, hbalance c a]
  have hm' :
      |((∑ b : {b : Pair c // part c b = a}, pairCounts c b.val : ℕ) : ℝ) -
        (t : ℝ) * totalCenterCount base law c a| ≤
      (Fintype.card {b : Pair c // part c b = a} : ℝ) *
        ((t : ℝ) * (baseSize base c : ℝ) * η) := by
    simpa only [Nat.cast_sum, hsum] using hm
  have hKnat : baseSize base c * Fintype.card {b : Pair c // part c b = a} ≤
      aggregateCoefficient base Pair :=
    (Nat.mul_le_mul_left _ (Fintype.card_subtype_le _)).trans
      (Finset.single_le_sum
        (f := fun d => baseSize base d * Fintype.card (Pair d))
        (fun _ _ => Nat.zero_le _) (Finset.mem_univ c))
  have hK : (baseSize base c : ℝ) *
      (Fintype.card {b : Pair c // part c b = a} : ℝ) ≤
      (aggregateCoefficient base Pair : ℝ) := by exact_mod_cast hKnat
  have hmul := mul_le_mul_of_nonneg_left hK (mul_nonneg (Nat.cast_nonneg t) hη)
  nlinarith

end PairMarginals

section CountProducer

variable {Pair Left Right : C → Type*}
  [∀ c, Fintype (Pair c)] [∀ c, DecidableEq (Pair c)]
  [∀ c, Fintype (Left c)] [∀ c, DecidableEq (Left c)]
  [∀ c, Fintype (Right c)] [∀ c, DecidableEq (Right c)]

theorem exists_widths_eventually_count_le
    (base : ∀ c, Shape c → ℕ) (q : ∀ c, Pair c → ℝ)
    (leftPart : ∀ c, Pair c → Left c) (rightPart : ∀ c, Pair c → Right c)
    (designatedLeft designatedRight : ∀ c, Shape c → Prop)
    (leftLaw : ∀ c, Shape c → Left c → ℝ)
    (rightLaw : ∀ c, Shape c → Right c → ℝ)
    (hparts : ∀ c, Function.Injective (fun a : Pair c => (leftPart c a, rightPart c a)))
    (hL0 : ∀ c u a, 0 ≤ leftLaw c u a) (hL1 : ∀ c u a, leftLaw c u a ≤ 1)
    (hR0 : ∀ c u a, 0 ≤ rightLaw c u a) (hR1 : ∀ c u a, rightLaw c u a ≤ 1)
    (hleft : ∀ c a, (baseSize base c : ℝ) *
      (∑ b : {b : Pair c // leftPart c b = a}, q c b.val) =
        totalCenterCount base leftLaw c a)
    (hright : ∀ c a, (baseSize base c : ℝ) *
      (∑ b : {b : Pair c // rightPart c b = a}, q c b.val) =
        totalCenterCount base rightLaw c a)
    {ε χMax ηMax : ℝ} (hε : 0 < ε) (hχMax : 0 < χMax) (hηMax : 0 < ηMax) :
    ∃ χ η : ℝ, 0 < χ ∧ χ ≤ χMax ∧ 0 < η ∧ η ≤ ηMax ∧
      ∀ᶠ t : ℕ in atTop,
        ∀ (Pos : C → Type*) [∀ c, Fintype (Pos c)] [∀ c, DecidableEq (Pos c)]
          (pairCounts : ∀ c, Pair c → ℕ)
          (_ : FixedClassWords Pos Shape (fun c u => t * base c u))
          (z : FixedClassWords Pos Pair pairCounts),
          PairWindow base q Pos pairCounts η →
          (compatibleCandidateCount Pos Shape Pair Left Right (fun c u => t * base c u)
            pairCounts leftPart rightPart designatedLeft designatedRight leftLaw rightLaw χ z : ℝ) ≤
            Real.exp ((t : ℝ) *
              (compatibilityRate base q designatedLeft designatedRight
                (groupCap base designatedLeft leftLaw 0)
                (groupCap base designatedRight rightLaw 0) + ε)) ∧
          (0 < compatibleCandidateCount Pos Shape Pair Left Right (fun c u => t * base c u)
            pairCounts leftPart rightPart designatedLeft designatedRight leftLaw rightLaw χ z →
            Real.log (compatibleCandidateCount Pos Shape Pair Left Right (fun c u => t * base c u)
              pairCounts leftPart rightPart designatedLeft designatedRight leftLaw rightLaw χ z : ℝ) /
              (t : ℝ) ≤ compatibilityRate base q designatedLeft designatedRight
                (groupCap base designatedLeft leftLaw 0)
                (groupCap base designatedRight rightLaw 0) + ε) := by
  let δ : ℝ := ε / (4 * ((totalBase base : ℝ) + 1))
  have hden : 0 < 4 * ((totalBase base : ℝ) + 1) := by positivity
  have hδ : 0 < δ := div_pos hε hden
  have hδeq : (4 * ((totalBase base : ℝ) + 1)) * δ = ε :=
    mul_div_cancel₀ _ hden.ne'
  have hδbound : 2 * (totalBase base : ℝ) * δ ≤ ε / 2 := by nlinarith
  obtain ⟨χL, ηL, hχL, hχLMax, hηL, _, hCL⟩ :=
    exists_uniform_sideEntropyControl_all_positions Pair base designatedLeft leftLaw leftPart
      hL0 hL1 (ηMax := 1) hδ hχMax zero_lt_one
  obtain ⟨χR, ηR, hχR, hχRMax, hηR, _, hCR⟩ :=
    exists_uniform_sideEntropyControl_all_positions Pair base designatedRight rightLaw rightPart
      hR0 hR1 (ηMax := 1) hδ hχMax zero_lt_one
  let χ := min χL χR
  have hχ : 0 < χ := lt_min hχL hχR
  have hχMax' : χ ≤ χMax := (min_le_left _ _).trans hχLMax
  let K : ℕ := aggregateCoefficient base Pair
  have hKden : (0 : ℝ) < (K : ℝ) + 1 := by positivity
  let ηUpper : ℝ := min ηMax (min ηL ηR / ((K : ℝ) + 1))
  have hηUpper : 0 < ηUpper := lt_min hηMax (div_pos (lt_min hηL hηR) hKden)
  obtain ⟨η, hη, hηUpper', hbound⟩ := exists_window_eventually_count_le
    base q leftPart rightPart designatedLeft designatedRight leftLaw rightLaw hparts
    (groupCap base designatedLeft leftLaw δ) (groupCap base designatedRight rightLaw δ)
    (half_pos hε) hηUpper
  have hηMax' : η ≤ ηMax := hηUpper'.trans (min_le_left _ _)
  have hηscale : (K : ℝ) * η ≤ min ηL ηR := by
    have hle : η ≤ min ηL ηR / ((K : ℝ) + 1) :=
      hηUpper'.trans (min_le_right _ _)
    have hmul := (le_div_iff₀ hKden).mp hle
    nlinarith
  have hrate :
      compatibilityRate base q designatedLeft designatedRight
        (groupCap base designatedLeft leftLaw δ) (groupCap base designatedRight rightLaw δ) +
          ε / 2 ≤
      compatibilityRate base q designatedLeft designatedRight
        (groupCap base designatedLeft leftLaw 0) (groupCap base designatedRight rightLaw 0) + ε := by
    have heq := compatibilityRate_add_caps base q designatedLeft designatedRight
      (groupCap base designatedLeft leftLaw 0) (groupCap base designatedRight rightLaw 0) δ
    have hcapL : (fun g => groupCap base designatedLeft leftLaw 0 g + δ) =
        groupCap base designatedLeft leftLaw δ := by
      funext g
      simp only [groupCap, add_zero]
    have hcapR : (fun g => groupCap base designatedRight rightLaw 0 g + δ) =
        groupCap base designatedRight rightLaw δ := by
      funext g
      simp only [groupCap, add_zero]
    rw [hcapL, hcapR] at heq
    linarith only [heq, hδbound]
  refine ⟨χ, η, hχ, hχMax', hη, hηMax', ?_⟩
  filter_upwards [hbound, eventually_ge_atTop (1 : ℕ)] with t hb ht
  intro Pos _ _ pairCounts w z hp
  have htpos : 0 < t := lt_of_lt_of_le Nat.zero_lt_one ht
  have haggrL : ∀ c a,
      |((∑ b : {b : Pair c // leftPart c b = a}, pairCounts c b.val : ℕ) : ℝ) -
        (t : ℝ) * totalCenterCount base leftLaw c a| ≤ (t : ℝ) * ηL := by
    intro c a
    have h := aggregate_error_of_pairWindow base q leftLaw leftPart hleft
      t htpos Pos pairCounts w z hη.le hp c a
    have hscale := mul_le_mul_of_nonneg_left
      (hηscale.trans (min_le_left _ _)) (Nat.cast_nonneg t)
    change _ ≤ (t : ℝ) * (K : ℝ) * η at h
    nlinarith
  have haggrR : ∀ c a,
      |((∑ b : {b : Pair c // rightPart c b = a}, pairCounts c b.val : ℕ) : ℝ) -
        (t : ℝ) * totalCenterCount base rightLaw c a| ≤ (t : ℝ) * ηR := by
    intro c a
    have h := aggregate_error_of_pairWindow base q rightLaw rightPart hright
      t htpos Pos pairCounts w z hη.le hp c a
    have hscale := mul_le_mul_of_nonneg_left
      (hηscale.trans (min_le_right _ _)) (Nat.cast_nonneg t)
    change _ ≤ (t : ℝ) * (K : ℝ) * η at h
    nlinarith
  obtain ⟨controlL⟩ := hCL t Pos pairCounts w haggrL
  obtain ⟨controlR⟩ := hCR t Pos pairCounts w haggrR
  have controlL' := control_mono_width (fun c u => t * base c u) pairCounts
    leftPart designatedLeft leftLaw (groupCap base designatedLeft leftLaw δ) w
    controlL (show χ ≤ χL from min_le_left _ _)
  have controlR' := control_mono_width (fun c u => t * base c u) pairCounts
    rightPart designatedRight rightLaw (groupCap base designatedRight rightLaw δ) w
    controlR (show χ ≤ χR from min_le_right _ _)
  have hcount := hb Pos pairCounts w z χ hp controlL' controlR'
  refine ⟨hcount.1.trans (Real.exp_le_exp.mpr
    (mul_le_mul_of_nonneg_left hrate (Nat.cast_nonneg t))), ?_⟩
  intro hpos
  exact (hcount.2 hpos).trans hrate

end CountProducer

end MatrixMultiplication.JointCompatibilityCountProducer

end

end OAI