File size: 18,539 Bytes
f18e7de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import OAI.LinearAlgebra.MatrixMultiplication.Recovery.InheritedMaskMargins
import OAI.LinearAlgebra.MatrixMultiplication.Tensor.CompleteWordPair
import Mathlib.Algebra.Group.Equiv.Basic

namespace OAI

/-! Finite orbit symmetries, masks and exact recovery operations. -/

namespace MatrixMultiplication.PermutationMatching

open scoped BigOperators
open MatrixMultiplication.InheritedMasks
open Classical

noncomputable section

variable {C : Type*} {P X Y : C → Type*}
variable [Fintype C] [DecidableEq C]
  [∀ c, Fintype (P c)] [∀ c, DecidableEq (P c)]
variable {S T : C → Type*}

def wordPairLeftPositions (w : CompleteWordPair P X Y)
    (sl : ∀ c, X c → S c) (s : ∀ c, S c) : ∀ c, Finset (P c) :=
  fun c => statisticPositions (w.left c) (sl c) (s c)

def wordPairRightPositions (w : CompleteWordPair P X Y)
    (sr : ∀ c, Y c → T c) (t : ∀ c, T c) : ∀ c, Finset (P c) :=
  fun c => statisticPositions (w.right c) (sr c) (t c)

def wordPairCount (w : CompleteWordPair P X Y)
    (sl : ∀ c, X c → S c) (sr : ∀ c, Y c → T c)
    (s : ∀ c, S c) (t : ∀ c, T c) : ℝ :=
  ∑ c, ∑ i : P c,
    if sl c (w.left c i) = s c ∧ sr c (w.right c i) = t c then 1 else 0

omit [DecidableEq C] in
theorem wordPairCount_smul (w : CompleteWordPair P X Y)
    (sl : ∀ c, X c → S c) (sr : ∀ c, Y c → T c)
    (s : ∀ c, S c) (t : ∀ c, T c) (g : HalfClassPermutations P) :
    wordPairCount (g • w) sl sr s t =
      totalTwoHalfCount (wordPairLeftPositions w sl s)
        (wordPairRightPositions w sr t) g⁻¹ := by
  unfold wordPairCount totalTwoHalfCount wordPairLeftPositions wordPairRightPositions
  apply Finset.sum_congr rfl
  intro c _
  rw [twoHalfCount_statisticPositions]
  rfl

theorem average_wordPairCount_shift (w : CompleteWordPair P X Y)
    (sl : ∀ c, X c → S c) (sr : ∀ c, Y c → T c)
    (s : ∀ c, S c) (t : ∀ c, T c) (f : ℝ → ℝ) :
    average (fun g : HalfClassPermutations P => f (wordPairCount (g • w) sl sr s t)) =
      average (fun g : HalfClassPermutations P =>
        f (totalTwoHalfCount (wordPairLeftPositions w sl s)
          (wordPairRightPositions w sr t) g)) := by
  simp_rw [wordPairCount_smul]
  exact average_equiv (Equiv.inv (HalfClassPermutations P))
    (fun g => f (totalTwoHalfCount (wordPairLeftPositions w sl s)
      (wordPairRightPositions w sr t) g))

def wordPairMaskBad (sl : ∀ c, X c → S c) (sr : ∀ c, Y c → T c)
    (s : ∀ c, S c) (t : ∀ c, T c) (η ν : ℝ)
    (w : CompleteWordPair P X Y) : Prop :=
  η < |wordPairCount w sl sr s t / populationSize P - ν|

theorem wordPair_orbit_fraction_eq (w : CompleteWordPair P X Y)
    (sl : ∀ c, X c → S c) (sr : ∀ c, Y c → T c)
    (s : ∀ c, S c) (t : ∀ c, T c) (η ν : ℝ) :
    (((OrbitCounting.orbitSet (G := HalfClassPermutations P) w).filter
        (wordPairMaskBad sl sr s t η ν)).card : ℝ) /
      (OrbitCounting.orbitSet (G := HalfClassPermutations P) w).card =
        average (fun g : HalfClassPermutations P =>
          if η < |totalTwoHalfCount (wordPairLeftPositions w sl s)
              (wordPairRightPositions w sr t) g / populationSize P - ν|
            then 1 else 0) := by
  rw [← average_bad_shift_eq_orbit_fraction]
  exact average_wordPairCount_shift w sl sr s t
    (fun k => if η < |k / populationSize P - ν| then 1 else 0)

theorem wordPair_orbit_rejection_le (w : CompleteWordPair P X Y)
    (sl : ∀ c, X c → S c) (sr : ∀ c, Y c → T c)
    (s : ∀ c, S c) (t : ∀ c, T c)
    (hn : ∀ c, 2 ≤ Fintype.card (P c)) (hm : 0 < populationSize P)
    (η ν : ℝ) (hη : 0 < η)
    (hmargin : |totalMatchingMean (wordPairLeftPositions w sl s)
        (wordPairRightPositions w sr t) / populationSize P - ν| ≤ η / 2) :
    (((OrbitCounting.orbitSet (G := HalfClassPermutations P) w).filter
        (wordPairMaskBad sl sr s t η ν)).card : ℝ) /
      (OrbitCounting.orbitSet (G := HalfClassPermutations P) w).card ≤
        1 / (2 * populationSize P * η ^ 2) := by
  rw [wordPair_orbit_fraction_eq]
  exact twoHalf_inheritedMask_rejection_le _ _ hn hm η ν hη hmargin

theorem wordPair_orbit_rejection_of_windows_le [∀ c, Nonempty (P c)]
    (w : CompleteWordPair P X Y)
    (sl : ∀ c, X c → S c) (sr : ∀ c, Y c → T c)
    (s : ∀ c, S c) (t : ∀ c, T c)
    (hn : ∀ c, 2 ≤ Fintype.card (P c)) (hm : 0 < populationSize P)
    (left right : C → ℝ) (η τ α ν : ℝ) (hη : 0 < η)
    (hright0 : ∀ c, 0 ≤ right c) (hright1 : ∀ c, right c ≤ 1)
    (hleft : ∀ c, |classDensity (wordPairLeftPositions w sl s) c - left c| ≤ τ)
    (hright : ∀ c, |classDensity (wordPairRightPositions w sr t) c - right c| ≤ τ)
    (happrox : |classProductMixture P left right - ν| ≤ α)
    (hτ : τ ≤ η / 8) (hα : α ≤ η / 4) :
    (((OrbitCounting.orbitSet (G := HalfClassPermutations P) w).filter
        (wordPairMaskBad sl sr s t η ν)).card : ℝ) /
      (OrbitCounting.orbitSet (G := HalfClassPermutations P) w).card ≤
        1 / (2 * populationSize P * η ^ 2) := by
  exact wordPair_orbit_rejection_le w sl sr s t hn hm η ν hη
    (totalMatchingMean_inherited_margin _ _ hm left right η τ α ν
      hright0 hright1 hleft hright happrox hτ hα)

def selectedWordPairCount (D : Finset C) (w : CompleteWordPair P X Y)
    (sl : ∀ c, X c → S c) (sr : ∀ c, Y c → T c)
    (s : ∀ c, S c) (t : ∀ c, T c) : ℝ :=
  ∑ c : D, ∑ i : P c,
    if sl c (w.left c i) = s c ∧ sr c (w.right c i) = t c then 1 else 0

omit [Fintype C] [DecidableEq C] in
theorem selectedWordPairCount_smul (D : Finset C) (w : CompleteWordPair P X Y)
    (sl : ∀ c, X c → S c) (sr : ∀ c, Y c → T c)
    (s : ∀ c, S c) (t : ∀ c, T c) (g : HalfClassPermutations P) :
    selectedWordPairCount D (g • w) sl sr s t =
      selectedTwoHalfCount D (wordPairLeftPositions w sl s)
        (wordPairRightPositions w sr t) g⁻¹ := by
  unfold selectedWordPairCount selectedTwoHalfCount totalTwoHalfCount
    wordPairLeftPositions wordPairRightPositions
  apply Finset.sum_congr rfl
  intro c _
  rw [twoHalfCount_statisticPositions]
  rfl

theorem average_selectedWordPairCount_shift (D : Finset C)
    (w : CompleteWordPair P X Y)
    (sl : ∀ c, X c → S c) (sr : ∀ c, Y c → T c)
    (s : ∀ c, S c) (t : ∀ c, T c) (f : ℝ → ℝ) :
    average (fun g : HalfClassPermutations P =>
      f (selectedWordPairCount D (g • w) sl sr s t)) =
      average (fun g : HalfClassPermutations P =>
        f (selectedTwoHalfCount D (wordPairLeftPositions w sl s)
          (wordPairRightPositions w sr t) g)) := by
  simp_rw [selectedWordPairCount_smul]
  exact average_equiv (Equiv.inv (HalfClassPermutations P))
    (fun g => f (selectedTwoHalfCount D (wordPairLeftPositions w sl s)
      (wordPairRightPositions w sr t) g))

def selectedWordPairMaskBad (D : Finset C)
    (sl : ∀ c, X c → S c) (sr : ∀ c, Y c → T c)
    (s : ∀ c, S c) (t : ∀ c, T c) (η ν : ℝ)
    (w : CompleteWordPair P X Y) : Prop :=
  η < |selectedWordPairCount D w sl sr s t /
    populationSize (fun c : D => P c) - ν|

theorem selectedWordPair_orbit_fraction_eq (D : Finset C)
    (w : CompleteWordPair P X Y)
    (sl : ∀ c, X c → S c) (sr : ∀ c, Y c → T c)
    (s : ∀ c, S c) (t : ∀ c, T c) (η ν : ℝ) :
    (((OrbitCounting.orbitSet (G := HalfClassPermutations P) w).filter
        (selectedWordPairMaskBad D sl sr s t η ν)).card : ℝ) /
      (OrbitCounting.orbitSet (G := HalfClassPermutations P) w).card =
        average (fun g : HalfClassPermutations P =>
          if η < |selectedTwoHalfCount D (wordPairLeftPositions w sl s)
              (wordPairRightPositions w sr t) g /
                populationSize (fun c : D => P c) - ν| then 1 else 0) := by
  rw [← average_bad_shift_eq_orbit_fraction]
  exact average_selectedWordPairCount_shift D w sl sr s t
    (fun k => if η < |k / populationSize (fun c : D => P c) - ν| then 1 else 0)

theorem selectedWordPair_orbit_rejection_le (D : Finset C)
    (w : CompleteWordPair P X Y)
    (sl : ∀ c, X c → S c) (sr : ∀ c, Y c → T c)
    (s : ∀ c, S c) (t : ∀ c, T c)
    (hn : ∀ c ∈ D, 2 ≤ Fintype.card (P c))
    (hm : 0 < populationSize (fun c : D => P c)) (η ν : ℝ) (hη : 0 < η)
    (hmargin : |totalMatchingMean
        (fun c : D => wordPairLeftPositions w sl s c)
        (fun c : D => wordPairRightPositions w sr t c) /
          populationSize (fun c : D => P c) - ν| ≤ η / 2) :
    (((OrbitCounting.orbitSet (G := HalfClassPermutations P) w).filter
        (selectedWordPairMaskBad D sl sr s t η ν)).card : ℝ) /
      (OrbitCounting.orbitSet (G := HalfClassPermutations P) w).card ≤
        1 / (2 * populationSize (fun c : D => P c) * η ^ 2) := by
  rw [selectedWordPair_orbit_fraction_eq]
  exact selectedTwoHalfMask_rejection_le D _ _ hn hm η ν hη hmargin

theorem selectedWordPair_orbit_rejection_of_windows_le (D : Finset C)
    (w : CompleteWordPair P X Y)
    (sl : ∀ c, X c → S c) (sr : ∀ c, Y c → T c)
    (s : ∀ c, S c) (t : ∀ c, T c)
    (hn : ∀ c ∈ D, 2 ≤ Fintype.card (P c))
    (hm : 0 < populationSize (fun c : D => P c))
    (left right : C → ℝ) (η τ α ν : ℝ) (hη : 0 < η)
    (hright0 : ∀ c ∈ D, 0 ≤ right c) (hright1 : ∀ c ∈ D, right c ≤ 1)
    (hleft : ∀ c ∈ D, |classDensity (wordPairLeftPositions w sl s) c - left c| ≤ τ)
    (hright : ∀ c ∈ D, |classDensity (wordPairRightPositions w sr t) c - right c| ≤ τ)
    (happrox : |classProductMixture (fun c : D => P c)
      (fun c => left c) (fun c => right c) - ν| ≤ α)
    (hτ : τ ≤ η / 8) (hα : α ≤ η / 4) :
    (((OrbitCounting.orbitSet (G := HalfClassPermutations P) w).filter
        (selectedWordPairMaskBad D sl sr s t η ν)).card : ℝ) /
      (OrbitCounting.orbitSet (G := HalfClassPermutations P) w).card ≤
        1 / (2 * populationSize (fun c : D => P c) * η ^ 2) := by
  rw [selectedWordPair_orbit_fraction_eq]
  exact selectedTwoHalfMask_rejection_of_windows_le D _ _ hn hm left right
    η τ α ν hη hright0 hright1 hleft hright happrox hτ hα

theorem selectedWordPair_orbit_rejection_of_completeWindows_le
    [∀ c, Fintype (X c)] [∀ c, DecidableEq (X c)]
    [∀ c, Fintype (Y c)] [∀ c, DecidableEq (Y c)]
    [∀ c, Fintype (S c)] [∀ c, DecidableEq (S c)]
    [∀ c, Fintype (T c)] [∀ c, DecidableEq (T c)]
    (D : Finset C) (w : CompleteWordPair P X Y)
    (sl : ∀ c, X c → S c) (sr : ∀ c, Y c → T c)
    (s : ∀ c, S c) (t : ∀ c, T c)
    (hn : ∀ c ∈ D, 2 ≤ Fintype.card (P c))
    (hm : 0 < populationSize (fun c : D => P c))
    (leftLaw : ∀ c, X c → ℝ) (rightLaw : ∀ c, Y c → ℝ)
    (leftε rightε : C → ℝ) (η α ν : ℝ) (hη : 0 < η)
    (hright0 : ∀ c ∈ D, ∀ y, 0 ≤ rightLaw c y)
    (hrightSum : ∀ c ∈ D, ∑ y, rightLaw c y = 1)
    (hleftε : ∀ c ∈ D, 0 ≤ leftε c) (hrightε : ∀ c ∈ D, 0 ≤ rightε c)
    (hleft : ∀ c ∈ D, typeWindow (leftLaw c) (leftε c) (w.left c))
    (hright : ∀ c ∈ D, typeWindow (rightLaw c) (rightε c) (w.right c))
    (hleftFactor : ∀ c ∈ D, (Fintype.card (X c) : ℝ) * leftε c ≤ η / 8)
    (hrightFactor : ∀ c ∈ D, (Fintype.card (Y c) : ℝ) * rightε c ≤ η / 8)
    (happrox : |classProductMixture (fun c : D => P c)
      (fun c => pushforwardLaw (sl c) (leftLaw c) (s c))
      (fun c => pushforwardLaw (sr c) (rightLaw c) (t c)) - ν| ≤ α)
    (hα : α ≤ η / 4) :
    (((OrbitCounting.orbitSet (G := HalfClassPermutations P) w).filter
        (selectedWordPairMaskBad D sl sr s t η ν)).card : ℝ) /
      (OrbitCounting.orbitSet (G := HalfClassPermutations P) w).card ≤
        1 / (2 * populationSize (fun c : D => P c) * η ^ 2) := by
  rw [selectedWordPair_orbit_fraction_eq]
  exact selectedTwoHalf_statistic_rejection_of_completeWindows_le D w.left w.right
    sl sr s t hn hm leftLaw rightLaw leftε rightε η α ν hη hright0 hrightSum
    hleftε hrightε hleft hright hleftFactor hrightFactor happrox hα

section FiniteMasks

variable {I : Type*} [Fintype I] {SL SR : I → C → Type*}

theorem finite_selectedWordPair_sum_orbit_fraction_le
    (D : I → Finset C) (w : CompleteWordPair P X Y)
    (sl : ∀ i c, X c → SL i c) (sr : ∀ i c, Y c → SR i c)
    (s : ∀ i c, SL i c) (t : ∀ i c, SR i c) (η : ℝ) (ν : I → ℝ)
    (hη : 0 ≤ η) :
    (((OrbitCounting.orbitSet (G := HalfClassPermutations P) w).filter
        (fun v => η < |(∑ i, selectedWordPairCount (D i) v
          (sl i) (sr i) (s i) (t i) / populationSize (fun c : D i => P c)) -
            ∑ i, ν i|)).card : ℝ) /
      (OrbitCounting.orbitSet (G := HalfClassPermutations P) w).card ≤
    (((OrbitCounting.orbitSet (G := HalfClassPermutations P) w).filter
        (fun v => ∃ i, selectedWordPairMaskBad (D i) (sl i) (sr i)
          (s i) (t i) (η / Fintype.card I) (ν i) v)).card : ℝ) /
      (OrbitCounting.orbitSet (G := HalfClassPermutations P) w).card := by
  by_cases hI : Fintype.card I = 0
  · have : IsEmpty I := Fintype.card_eq_zero_iff.mp hI
    simp [not_lt_of_ge hη]
  have : Nonempty I := Fintype.card_pos_iff.mp (Nat.pos_of_ne_zero hI)
  have hcard : (0 : ℝ) < Fintype.card I := by
    exact_mod_cast (Fintype.card_pos (α := I))
  let z (v : CompleteWordPair P X Y) (i : I) : ℝ :=
    selectedWordPairCount (D i) v (sl i) (sr i) (s i) (t i) /
      populationSize (fun c : D i => P c)
  let R := OrbitCounting.orbitSet (G := HalfClassPermutations P) w
  let sumBad (v : CompleteWordPair P X Y) : Prop :=
    η < |(∑ i, z v i) - ∑ i, ν i|
  let unionBad (v : CompleteWordPair P X Y) : Prop :=
    ∃ i, selectedWordPairMaskBad (D i) (sl i) (sr i)
      (s i) (t i) (η / Fintype.card I) (ν i) v
  have hEvent (v) (hv : sumBad v) : unionBad v := by
    change ∃ i, η / Fintype.card I < |z v i - ν i|
    change η < |(∑ i, z v i) - ∑ i, ν i| at hv
    by_contra h
    push Not at h
    have he : |(∑ i, z v i) - ∑ i, ν i| ≤ η := by
      rw [← Finset.sum_sub_distrib]
      calc
        _ ≤ ∑ i, |z v i - ν i| := Finset.abs_sum_le_sum_abs _ _
        _ ≤ ∑ _i : I, η / Fintype.card I := Finset.sum_le_sum fun i _ => h i
        _ = η := by
          simp only [Finset.sum_const, Finset.card_univ, nsmul_eq_mul]
          field_simp [ne_of_gt hcard]
    linarith
  have hsub : R.filter sumBad ⊆ R.filter unionBad :=
    Finset.monotone_filter_right R (fun v _ => hEvent v)
  have hcards : ((R.filter sumBad).card : ℝ) ≤ (R.filter unionBad).card := by
    exact_mod_cast Finset.card_le_card hsub
  exact div_le_div_of_nonneg_right hcards (Nat.cast_nonneg R.card)

theorem finite_selectedWordPair_orbit_rejection_of_windows_le
    (D : I → Finset C) (w : CompleteWordPair P X Y)
    (sl : ∀ i c, X c → SL i c) (sr : ∀ i c, Y c → SR i c)
    (s : ∀ i c, SL i c) (t : ∀ i c, SR i c)
    (hn : ∀ i c, c ∈ D i → 2 ≤ Fintype.card (P c))
    (hm : ∀ i, 0 < populationSize (fun c : D i => P c))
    (left right : I → C → ℝ) (η τ α ν : I → ℝ) (hη : ∀ i, 0 < η i)
    (hright0 : ∀ i c, c ∈ D i → 0 ≤ right i c)
    (hright1 : ∀ i c, c ∈ D i → right i c ≤ 1)
    (hleft : ∀ i c, c ∈ D i →
      |classDensity (wordPairLeftPositions w (sl i) (s i)) c - left i c| ≤ τ i)
    (hright : ∀ i c, c ∈ D i →
      |classDensity (wordPairRightPositions w (sr i) (t i)) c - right i c| ≤ τ i)
    (happrox : ∀ i, |classProductMixture (fun c : D i => P c)
      (fun c => left i c) (fun c => right i c) - ν i| ≤ α i)
    (hτ : ∀ i, τ i ≤ η i / 8) (hα : ∀ i, α i ≤ η i / 4) :
    (((OrbitCounting.orbitSet (G := HalfClassPermutations P) w).filter
        (fun v => ∃ i, selectedWordPairMaskBad (D i) (sl i) (sr i)
          (s i) (t i) (η i) (ν i) v)).card : ℝ) /
      (OrbitCounting.orbitSet (G := HalfClassPermutations P) w).card ≤
        ∑ i, 1 / (2 * populationSize (fun c : D i => P c) * (η i) ^ 2) := by
  rw [← average_bad_shift_eq_orbit_fraction]
  refine le_trans (average_indicator_exists_le_sum _) ?_
  apply Finset.sum_le_sum
  intro i _
  change average (fun g : HalfClassPermutations P =>
    if η i < |selectedWordPairCount (D i) (g • w) (sl i) (sr i) (s i) (t i) /
      populationSize (fun c : D i => P c) - ν i| then 1 else 0) ≤ _
  rw [average_selectedWordPairCount_shift (D i) w (sl i) (sr i) (s i) (t i)
    (fun k => if η i < |k / populationSize (fun c : D i => P c) - ν i|
      then 1 else 0)]
  exact selectedTwoHalfMask_rejection_of_windows_le (D i) _ _ (hn i) (hm i)
    (left i) (right i) (η i) (τ i) (α i) (ν i) (hη i)
    (hright0 i) (hright1 i) (hleft i) (hright i) (happrox i) (hτ i) (hα i)

theorem finite_selectedWordPair_orbit_inverse_linear_of_windows
    (D : I → Finset C) (w : CompleteWordPair P X Y)
    (sl : ∀ i c, X c → SL i c) (sr : ∀ i c, Y c → SR i c)
    (s : ∀ i c, SL i c) (t : ∀ i c, SR i c)
    (hn : ∀ i c, c ∈ D i → 2 ≤ Fintype.card (P c))
    (mass : I → ℝ) (N : ℝ) (hN : 0 < N) (hmass : ∀ i, 0 < mass i)
    (hsize : ∀ i, populationSize (fun c : D i => P c) = mass i * N)
    (left right : I → C → ℝ) (η τ α ν : I → ℝ) (hη : ∀ i, 0 < η i)
    (hright0 : ∀ i c, c ∈ D i → 0 ≤ right i c)
    (hright1 : ∀ i c, c ∈ D i → right i c ≤ 1)
    (hleft : ∀ i c, c ∈ D i →
      |classDensity (wordPairLeftPositions w (sl i) (s i)) c - left i c| ≤ τ i)
    (hright : ∀ i c, c ∈ D i →
      |classDensity (wordPairRightPositions w (sr i) (t i)) c - right i c| ≤ τ i)
    (happrox : ∀ i, |classProductMixture (fun c : D i => P c)
      (fun c => left i c) (fun c => right i c) - ν i| ≤ α i)
    (hτ : ∀ i, τ i ≤ η i / 8) (hα : ∀ i, α i ≤ η i / 4) :
    (((OrbitCounting.orbitSet (G := HalfClassPermutations P) w).filter
        (fun v => ∃ i, selectedWordPairMaskBad (D i) (sl i) (sr i)
          (s i) (t i) (η i) (ν i) v)).card : ℝ) /
      (OrbitCounting.orbitSet (G := HalfClassPermutations P) w).card ≤
        (∑ i, 1 / (2 * mass i * (η i) ^ 2)) / N := by
  have hm (i : I) : 0 < populationSize (fun c : D i => P c) := by
    rw [hsize]
    exact mul_pos (hmass i) hN
  have h := finite_selectedWordPair_orbit_rejection_of_windows_le D w sl sr s t hn hm
    left right η τ α ν hη hright0 hright1 hleft hright happrox hτ hα
  simpa only [hsize, inverse_linear_mask_constant] using h

end FiniteMasks

end

end MatrixMultiplication.PermutationMatching

end OAI