File size: 17,921 Bytes
135052a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import Mathlib
import OAI.Probability.SKGap.Localization.IntegrableResidualRecipeSq
import OAI.Probability.SKGap.Localization.RegeneratedAnswer

namespace OAI

section
open scoped BigOperators
open scoped BigOperators
open scoped BigOperators
open scoped BigOperators
open scoped BigOperators
open scoped BigOperators NNReal
open MeasureTheory ProbabilityTheory
open MeasureTheory ProbabilityTheory Filter
open scoped BigOperators NNReal
open MeasureTheory ProbabilityTheory
open scoped BigOperators NNReal ENNReal
open MeasureTheory ProbabilityTheory Filter
open scoped BigOperators NNReal ENNReal
open MeasureTheory ProbabilityTheory
open scoped BigOperators Matrix Matrix.Norms.Elementwise
open scoped BigOperators
open MeasureTheory ProbabilityTheory
open scoped BigOperators Matrix Matrix.Norms.Elementwise
open scoped BigOperators
open scoped BigOperators NNReal ENNReal
open MeasureTheory Metric Set
open scoped BigOperators NNReal ENNReal
open MeasureTheory ProbabilityTheory Filter Set
open scoped BigOperators NNReal ENNReal Matrix.Norms.L2Operator
open MeasureTheory ProbabilityTheory Filter Set
open scoped BigOperators Matrix.Norms.L2Operator
open MeasureTheory ProbabilityTheory Filter Set
open scoped BigOperators Matrix Matrix.Norms.Elementwise
open MeasureTheory ProbabilityTheory Filter Set
open MeasureTheory ProbabilityTheory Filter
open scoped BigOperators ENNReal NNReal
open MeasureTheory ProbabilityTheory Filter
open scoped BigOperators NNReal ENNReal Matrix
open MeasureTheory ProbabilityTheory Filter
open scoped BigOperators ENNReal NNReal
open MeasureTheory ProbabilityTheory Filter
open scoped BigOperators NNReal ENNReal
open scoped BigOperators
open MeasureTheory ProbabilityTheory
open scoped BigOperators Matrix Matrix.Norms.Elementwise NNReal ENNReal
open scoped BigOperators
open Filter Topology
open MeasureTheory ProbabilityTheory Filter
open scoped NNReal ENNReal BigOperators Topology
open MeasureTheory ProbabilityTheory Filter
open Matrix
open scoped NNReal ENNReal BigOperators Topology Matrix.Norms.Elementwise
open MeasureTheory ProbabilityTheory Filter
open scoped BigOperators NNReal ENNReal Topology
open MeasureTheory ProbabilityTheory Filter Matrix
open scoped NNReal ENNReal BigOperators Topology
open MeasureTheory ProbabilityTheory Filter
open scoped BigOperators NNReal ENNReal Topology
open MeasureTheory ProbabilityTheory Filter
open scoped NNReal ENNReal BigOperators Topology
open MeasureTheory ProbabilityTheory Filter
open scoped NNReal ENNReal BigOperators Topology
open MeasureTheory ProbabilityTheory Filter
open scoped NNReal ENNReal BigOperators Topology
open MeasureTheory ProbabilityTheory Filter
open scoped NNReal ENNReal BigOperators Topology
open MeasureTheory ProbabilityTheory Filter
open scoped ENNReal Topology
open MeasureTheory ProbabilityTheory Filter
open scoped ENNReal NNReal Topology BigOperators
open MeasureTheory ProbabilityTheory Filter
open scoped ENNReal NNReal Topology BigOperators
open MeasureTheory ProbabilityTheory Filter
open scoped ENNReal NNReal Topology BigOperators
open MeasureTheory ProbabilityTheory Filter
open scoped ENNReal NNReal Topology BigOperators
open MeasureTheory ProbabilityTheory Filter Matrix
open scoped NNReal ENNReal BigOperators Topology
open MeasureTheory ProbabilityTheory Filter Matrix
open scoped NNReal ENNReal BigOperators Topology
open MeasureTheory ProbabilityTheory Filter Matrix
open scoped NNReal ENNReal BigOperators Topology
open MeasureTheory ProbabilityTheory Filter Matrix
open scoped NNReal ENNReal BigOperators Topology
open MeasureTheory ProbabilityTheory Filter Matrix
open scoped NNReal ENNReal BigOperators Topology
open MeasureTheory ProbabilityTheory Filter Matrix
open scoped NNReal ENNReal BigOperators Topology Matrix Matrix.Norms.Elementwise
open MeasureTheory ProbabilityTheory Filter Matrix
open scoped NNReal ENNReal BigOperators Topology Matrix Matrix.Norms.Elementwise
open MeasureTheory ProbabilityTheory Filter Matrix
open scoped NNReal ENNReal BigOperators Topology Matrix Matrix.Norms.Elementwise
open MeasureTheory ProbabilityTheory Filter Matrix
open scoped NNReal ENNReal BigOperators Topology Matrix Matrix.Norms.Elementwise
open MeasureTheory ProbabilityTheory Filter Matrix
open scoped NNReal ENNReal BigOperators Topology Matrix Matrix.Norms.Elementwise
open MeasureTheory ProbabilityTheory Filter Matrix
open scoped NNReal ENNReal BigOperators Topology Matrix Matrix.Norms.Elementwise
open MeasureTheory ProbabilityTheory Filter Matrix
open scoped NNReal ENNReal BigOperators Topology Matrix Matrix.Norms.Elementwise
open MeasureTheory ProbabilityTheory Filter Set Matrix
open scoped BigOperators NNReal ENNReal Matrix.Norms.L2Operator
open MeasureTheory ProbabilityTheory Filter Matrix
open scoped NNReal ENNReal BigOperators Topology Matrix Matrix.Norms.Elementwise
open MeasureTheory ProbabilityTheory Filter Matrix
open scoped NNReal ENNReal BigOperators Topology Matrix Matrix.Norms.Elementwise
open MeasureTheory ProbabilityTheory Filter Matrix
open scoped NNReal ENNReal BigOperators Topology Matrix Matrix.Norms.Elementwise
namespace SKGapCutoff.Regression

local instance iteratedMatrixMeasurable {n m : ℕ} : MeasurableSpace (Matrix (Fin n) (Fin m) ℝ) :=
  inferInstanceAs (MeasurableSpace (Fin n → Fin m → ℝ))

theorem ExponentialEmpiricalConcentration.regenerated_normalized_query
    {E : Type*} [PseudoMetricSpace E] [MeasurableSpace E] [BorelSpace E] [SecondCountableTopology E]
    {H : ℕ → Type*} [∀ n, MeasurableSpace (H n)]
    (ρ : ∀ n, Measure (H n)) [∀ n, IsProbabilityMeasure (ρ n)]
    (ν : Measure E) [IsProbabilityMeasure ν]
    (X : ∀ n, H n → Fin n → E) (hXm : ∀ n, Measurable (X n))
    (hX : ExponentialEmpiricalConcentration ρ X ν)
    (r : ℕ) (U Y : ∀ n, H n → Matrix (Fin n) (Fin r) ℝ) (q : ∀ n, H n → Fin n → ℝ)
    (hUm : ∀ n, Measurable (U n)) (hqm : ∀ n, Measurable (q n))
    (hU : ∀ n h a, ∑ i, U n h i a^2 ≤ 1) (hq : ∀ n h, ∑ i, q n h i^2 ≤ 1)
    (u y : Fin r → E → ℝ) (g : E → ℝ) {K : ℝ≥0}
    (hu : ∀ a, LipschitzWith K (u a)) (hy : ∀ a, LipschitzWith K (y a)) (hg : LipschitzWith K g)
    (huT : ∀ a, ExponentialSquareTails ρ (fun n h i => u a (X n h i)))
    (hyT : ∀ a, ExponentialSquareTails ρ (fun n h i => y a (X n h i)))
    (hgT : ExponentialSquareTails ρ (fun n h i => g (X n h i)))
    (hui : ∀ a, Integrable (fun x => u a x^2) ν)
    (hyi : ∀ a, Integrable (fun x => y a x^2) ν) (hgi : Integrable (fun x => g x^2) ν)
    (heu : ∀ (n : ℕ) h i a, Real.sqrt n*U n h i a=u a (X n h i))
    (hey : ∀ (n : ℕ) h i a, Real.sqrt n*Y n h i a=y a (X n h i))
    (heq : ∀ (n : ℕ) h i, Real.sqrt n*q n h i=g (X n h i)) :
    let W := fun n (z : H n × ((Fin n ⊕ Unit) → ℝ)) i =>
      Real.sqrt n*regeneratedAnswer (U n z.1) (Y n z.1) (q n z.1) z.2 i
    let law := (ν.prod (gaussianReal 0 1)).map
      (fun z => (z.1,z.2+∑ a, (∫ x, y a x*g x ∂ν)*u a z.1))
    ExponentialEmpiricalConcentration (fun n => (ρ n).prod (standardArrayLaw (Fin n ⊕ Unit)))
      (fun n z i => (X n z.1 i,W n z i)) law ∧
    ExponentialSquareTails (fun n => (ρ n).prod (standardArrayLaw (Fin n ⊕ Unit))) W := by
  let C : ∀ n, H n → Option (Fin r) → ℝ := fun n h a => a.elim 1
    (fun a => (∑ i, y a (X n h i)*g (X n h i))/(n:ℝ))
  let c : Option (Fin r) → ℝ := fun a => a.elim 1 (fun a => ∫ x, y a x*g x ∂ν)
  have hC : ExponentialConvergence ρ C c := by
    apply ExponentialConvergence.pi_finite
    intro a
    cases a with
    | none => exact ExponentialConvergence.constant ρ 1
    | some a => exact hX.product_average (y a) g (hy a) hg (hyT a) hgT (hyi a) hgi
  have hp := hX.adaptive_affine_query ρ ν X hXm r U q hUm hqm hU hq
    u hu huT hui C c hC
  simp only [C,c,Option.elim_none,Option.elim_some,one_mul] at hp
  let φ : (E × ℝ) × ℝ → E × ℝ := fun z => (z.1.1,z.2)
  have hφ : LipschitzWith 1 φ := by
    simpa using (LipschitzWith.prod_fst.comp
      (LipschitzWith.prod_fst (α := E × ℝ) (β := ℝ))).prodMk LipschitzWith.prod_snd
  have hpE := hp.1.map φ hφ
  have hm : Measurable (fun z : E × ℝ =>
      (z,z.2+∑ a, (∫ x, y a x*g x ∂ν)*u a z.1)) := by
    apply measurable_id.prodMk
    exact measurable_snd.add (Finset.measurable_sum _ (fun a _ =>
      measurable_const.mul ((hu a).continuous.measurable.comp measurable_fst)))
  rw [Measure.map_map hφ.continuous.measurable hm] at hpE
  have he (n : ℕ) (z : H n × ((Fin n ⊕ Unit) → ℝ)) (i : Fin n) :
      Real.sqrt n*regeneratedAnswer (U n z.1) (Y n z.1) (q n z.1) z.2 i =
        Real.sqrt n*queryInnovation (residualProjection (U n z.1)) (q n z.1) z.2 i+
          ∑ a, ((∑ j, y a (X n z.1 j)*g (X n z.1 j))/(n:ℝ))*u a (X n z.1 i) := by
    have hn : 0<n := Nat.pos_of_ne_zero (fun hn => by subst n; exact Fin.elim0 i)
    simp only [regeneratedAnswer,Pi.add_apply,mul_add,scaled_query_predictor hn,heu,hey,heq]
    simp only [mul_comm,add_comm]
  dsimp only
  simp only [he]
  exact ⟨hpE,hp.2⟩

theorem ExponentialEmpiricalConcentration.regenerated_nondegenerate_step
    {E : Type*} [PseudoMetricSpace E] [MeasurableSpace E] [BorelSpace E]
    [SecondCountableTopology E]
    {H : ℕ → Type*} [∀ n, MeasurableSpace (H n)]
    (ρ : ∀ n, Measure (H n)) [∀ n, IsProbabilityMeasure (ρ n)]
    (ν : Measure E) [IsProbabilityMeasure ν]
    (X : ∀ n, H n → Fin n → E) (hXm : ∀ n, Measurable (X n))
    (hX : ExponentialEmpiricalConcentration ρ X ν)
    (r : ℕ) (U Y : ∀ n, H n → Matrix (Fin n) (Fin r) ℝ)
    (hUm : ∀ n, Measurable (U n))
    (hU : ∀ n h a, ∑ i, U n h i a^2 ≤ 1)
    (u y : Fin r → E → ℝ) (g : E → ℝ) {K : ℝ≥0} (hK : 1 ≤ K)
    (hu : ∀ a, LipschitzWith K (u a)) (hy : ∀ a, LipschitzWith K (y a))
    (hg : LipschitzWith K g)
    (huT : ∀ a, ExponentialSquareTails ρ (fun n h i => u a (X n h i)))
    (hyT : ∀ a, ExponentialSquareTails ρ (fun n h i => y a (X n h i)))
    (hgT : ExponentialSquareTails ρ (fun n h i => g (X n h i)))
    (hui : ∀ a, Integrable (fun x => u a x^2) ν)
    (hyi : ∀ a, Integrable (fun x => y a x^2) ν) (hgi : Integrable (fun x => g x^2) ν)
    (heu : ∀ (n : ℕ) h i a, Real.sqrt n*U n h i a=u a (X n h i))
    (hey : ∀ (n : ℕ) h i a, Real.sqrt n*Y n h i a=y a (X n h i))
    (hs : 0 < ∫ x, residualRecipe u g (fun a => ∫ z, u a z*g z ∂ν) x^2 ∂ν) :
    let R := fun n h => residualProjection (U n h) *ᵥ (fun i => g (X n h i))
    let q := fun n h => unitVector (R n h)
    let s := ∫ x, residualRecipe u g (fun a => ∫ z, u a z*g z ∂ν) x^2 ∂ν
    let φ := fun x => (x,residualRecipe u g (fun a => ∫ z, u a z*g z ∂ν) x/Real.sqrt s)
    let ν' := ν.map φ
    let W := fun n (z : H n × ((Fin n ⊕ Unit) → ℝ)) i =>
      Real.sqrt n*(regeneratedAnswer (U n z.1) (Y n z.1) (q n z.1) z.2) i
    ExponentialConvergence ρ (fun n h => (∑ i, R n h i^2)/(n:ℝ)) s ∧
    ExponentialEmpiricalConcentration
      (fun n => (ρ n).prod (standardArrayLaw (Fin n ⊕ Unit)))
      (fun n z i => ((X n z.1 i,Real.sqrt n*q n z.1 i),W n z i))
      ((ν'.prod (gaussianReal 0 1)).map
        (fun z => (z.1,z.2+∑ a, (∫ x, y a x.1*x.2 ∂ν')*u a z.1.1))) ∧
    ExponentialSquareTails ρ (fun n h i => Real.sqrt n*q n h i) ∧
    ExponentialSquareTails (fun n => (ρ n).prod (standardArrayLaw (Fin n ⊕ Unit))) W := by
  dsimp only
  let R := fun n h => residualProjection (U n h) *ᵥ (fun i => g (X n h i))
  let q := fun n h => unitVector (R n h)
  let S := fun n h => (∑ i, R n h i^2)/(n:ℝ)
  let c := fun a => ∫ z, u a z*g z ∂ν
  let s := ∫ x, residualRecipe u g c x^2 ∂ν
  let φ := fun x => (x,residualRecipe u g c x/Real.sqrt s)
  let ν' := ν.map φ
  have hφ : Measurable φ := by
    apply measurable_id.prodMk
    apply Measurable.div_const
    exact hg.continuous.measurable.sub (Finset.measurable_sum _ (fun a _ =>
      (hu a).continuous.measurable.const_mul (c a)))
  let : IsProbabilityMeasure ν' := inferInstance
  have hh := hX.normalized_projected_query U u g hu hg huT hgT hui hgi heu hs
  change ExponentialConvergence ρ S s ∧
    ExponentialEmpiricalConcentration ρ
      (fun n h i => (X n h i,Real.sqrt n*q n h i)) ν' ∧
    ExponentialSquareTails ρ (fun n h i => Real.sqrt n*q n h i) at hh
  let X' := fun n h i => (X n h i,Real.sqrt n*q n h i)
  have hRm n : Measurable (R n) := by
    have hP := measurable_residualProjection_comp (U n) (hUm n)
    have hgX : Measurable (fun h i => g (X n h i)) := by
      exact Measurable.of_eval (fun i => hg.continuous.measurable.comp
        ((measurable_pi_apply i).comp (hXm n)))
    apply Measurable.of_eval
    intro i
    simp only [R,Matrix.mulVec,dotProduct]
    apply Finset.measurable_sum
    intro j _
    exact (((measurable_pi_apply j).comp (measurable_pi_apply i)).comp hP).mul
      ((measurable_pi_apply j).comp hgX)
  have hqm n : Measurable (q n) := measurable_unitVector.comp (hRm n)
  have hX'm n : Measurable (X' n) := by
    apply Measurable.of_eval
    intro i
    exact ((measurable_pi_apply i).comp (hXm n)).prodMk
      (((measurable_pi_apply i).comp (hqm n)).const_mul (Real.sqrt n))
  have hu' a : LipschitzWith K (fun z : E × ℝ => u a z.1) := by
    simpa only [Function.comp_def,mul_one] using (hu a).comp (LipschitzWith.prod_fst (α := E) (β := ℝ))
  have hy' a : LipschitzWith K (fun z : E × ℝ => y a z.1) := by
    simpa only [Function.comp_def,mul_one] using (hy a).comp (LipschitzWith.prod_fst (α := E) (β := ℝ))
  have hg' : LipschitzWith K (Prod.snd : E × ℝ → ℝ) :=
    LipschitzWith.prod_snd.weaken hK
  have hui' a : Integrable (fun z : E × ℝ => u a z.1^2) ν' := by
    apply (integrable_map_measure (((hu a).continuous.measurable.comp measurable_fst).pow_const 2).aestronglyMeasurable hφ.aemeasurable).2
    exact hui a
  have hyi' a : Integrable (fun z : E × ℝ => y a z.1^2) ν' := by
    apply (integrable_map_measure (((hy a).continuous.measurable.comp measurable_fst).pow_const 2).aestronglyMeasurable hφ.aemeasurable).2
    exact hyi a
  have hgi' : Integrable (fun z : E × ℝ => z.2^2) ν' := by
    rw [integrable_map_measure (by fun_prop) hφ.aemeasurable]
    simp only [φ,Function.comp_def,div_pow]
    exact (integrable_residualRecipe_sq u g c
      (fun a => (hu a).continuous.measurable.aestronglyMeasurable)
      hg.continuous.measurable.aestronglyMeasurable hui hgi).div_const _
  have hqle n h : ∑ i, q n h i^2 ≤ 1 := unitVector_sq_le _
  have hstep := hh.2.1.regenerated_normalized_query ρ ν' X' hX'm r U Y q
    hUm hqm hU hqle
    (fun a z => u a z.1) (fun a z => y a z.1) Prod.snd hu' hy' hg'
    huT hyT hh.2.2 hui' hyi' hgi' heu hey (fun _ _ _ => rfl)
  exact ⟨hh.1,hstep.1,hh.2.2,hstep.2⟩

@[fun_prop] lemma measurable_extendFrame_comp {n r : ℕ} {H : Type*} [MeasurableSpace H]
    (U : H → Matrix (Fin n) (Fin r) ℝ) (q : H → Fin n → ℝ)
    (hU : Measurable U) (hq : Measurable q) : Measurable (fun h => extendFrame (U h) (q h)) := by
  apply Measurable.of_eval
  intro i
  apply Measurable.of_eval
  intro a
  obtain rfl | ⟨a,rfl⟩ := Fin.eq_zero_or_eq_succ a
  · exact (measurable_pi_apply i).comp hq
  · exact ((measurable_pi_apply a).comp (measurable_pi_apply i)).comp hU

lemma extended_normalized_columns_bound {n r : ℕ} (U : Matrix (Fin n) (Fin r) ℝ)
    (v : Fin n → ℝ) (hU : ∀ a, ∑ i, U i a^2 ≤ 1) (a : Fin (r+1)) :
    ∑ i, extendFrame U (unitVector v) i a^2 ≤ 1 := by
  obtain rfl | ⟨a,rfl⟩ := Fin.eq_zero_or_eq_succ a
  · exact unitVector_sq_le v
  · exact hU a

theorem regenerated_good_history
    {H : ℕ → Type*} [∀ n, MeasurableSpace (H n)]
    (ρ : ∀ n, Measure (H n)) [∀ n, IsProbabilityMeasure (ρ n)]
    (r : ℕ) (U Y : ∀ n, H n → Matrix (Fin n) (Fin r) ℝ)
    (v : ∀ n, H n → Fin n → ℝ)
    (G : ∀ n, Set (H n)) (hrare : ExponentiallyRare ρ (fun n => (G n)ᶜ))
    (hgood : ∀ n h, h ∈ G n → (U n h)ᵀ*U n h=1 ∧ (U n h)ᵀ*Y n h=(Y n h)ᵀ*U n h)
    (s : ℝ) (hs : 0 < s)
    (hS : ExponentialConvergence ρ
      (fun n h => (∑ i, (residualProjection (U n h) *ᵥ v n h) i^2)/(n:ℝ)) s) :
    let q := fun n h => unitVector (residualProjection (U n h) *ᵥ v n h)
    let G' := fun n => G n ∩ {h | 0 < (∑ i, (residualProjection (U n h) *ᵥ v n h) i^2)/(n:ℝ)}
    ExponentiallyRare (fun n => (ρ n).prod (standardArrayLaw (Fin n ⊕ Unit)))
      (fun n => {z | z.1 ∉ G' n}) ∧
    (∀ n (z : H n × ((Fin n ⊕ Unit) → ℝ)), z.1 ∈ G' n →
      let U' := extendFrame (U n z.1) (q n z.1)
      let Y' := extendFrame (Y n z.1) (regeneratedAnswer (U n z.1) (Y n z.1) (q n z.1) z.2)
      U'ᵀ*U'=1 ∧ U'ᵀ*Y'=Y'ᵀ*U') := by
  dsimp only
  let S := fun n h => (∑ i, (residualProjection (U n h) *ᵥ v n h) i^2)/(n:ℝ)
  have hr : ExponentiallyRare ρ (fun n => (G n ∩ {h | 0 < S n h})ᶜ) := by
    apply (hrare.union (hS.positive_rare hs)).mono
    refine Filter.Eventually.of_forall ?_
    intro n h he
    by_cases hh : h ∈ G n
    · apply Or.inr
      change S n h ≤ 0
      exact le_of_not_gt (fun hp => he ⟨hh,hp⟩)
    · exact Or.inl hh
  refine ⟨hr.prod_fst (fun n => standardArrayLaw (Fin n ⊕ Unit)), ?_⟩
  intro n z hz
  obtain ⟨hU,hY⟩ := hgood n z.1 hz.1
  have ho := normalized_residual_perpendicular (U n z.1) hU (v n z.1)
  have hq : ∑ i, (unitVector (residualProjection (U n z.1) *ᵥ v n z.1)) i^2=1 := by
    apply unitVector_sq_eq
    by_contra! hn
    have hnp := div_nonpos_of_nonpos_of_nonneg hn (Nat.cast_nonneg n : (0:ℝ) ≤ n)
    exact not_le_of_gt hz.2 hnp
  exact ⟨extendFrame_orthonormal _ _ hU hq ho,
    regenerated_frame_compatible _ _ _ hU hY ho z.2⟩

end SKGapCutoff.Regression

open MeasureTheory ProbabilityTheory Filter Matrix
open scoped NNReal ENNReal BigOperators Topology Matrix Matrix.Norms.Elementwise

end

end OAI