File size: 21,816 Bytes
a2ee19a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
import OAI.Probability.ProjectionMoments.Realization

namespace OAI

noncomputable section

open MeasureTheory ProbabilityTheory Set Filter
open scoped ENNReal NNReal RealInnerProductSpace
namespace NoiselessRegression

section
variable {E : Type*} [NormedAddCommGroup E] [InnerProductSpace ℝ E]
  [FiniteDimensional ℝ E] [MeasurableSpace E] [BorelSpace E] [Nontrivial E]

lemma capWeight_mass_mixed (y : E) {ε : ℝ} (hε : 0 < ε) (hε1 : ε ≤ 1)
    (a : ℕ) {b : ℝ} (hb : 0 ≤ b) (hab : (a:ℝ)+b ≤ (Module.finrank ℝ E-1:ℕ)) :
    PolynomialMass ((ambientSphereLaw (E := E)).withDensity (capWeight y ε)) a
      (ENNReal.ofReal ((3 : ℝ)*4^Module.finrank ℝ E) * ENNReal.ofReal (ε^b)) := by
  change PolynomialMass ((ambientSphereLaw (E := E)).withDensity ((Metric.closedBall y ε).indicator 1)) a _
  rw [withDensity_indicator_one Metric.isClosed_closedBall.measurableSet]
  intro z r hr
  rw [Measure.restrict_apply Metric.isClosed_closedBall.measurableSet]
  by_cases h : r ≤ ε
  · apply (measure_mono Set.inter_subset_left).trans
    apply (ambientSphereLaw_closedBall z hr (Module.finrank ℝ E-1) le_rfl).trans
    rw [mul_assoc, ← ENNReal.ofReal_mul (Real.rpow_nonneg hε.le _)]
    apply mul_le_mul' le_rfl
    apply ENNReal.ofReal_le_ofReal
    have hpow : r^(Module.finrank ℝ E-1) ≤ r^((a:ℝ)+b) := by
      rw [← Real.rpow_natCast]
      exact Real.rpow_le_rpow_of_exponent_ge hr (h.trans hε1) hab
    apply hpow.trans
    rw [Real.rpow_add hr,Real.rpow_natCast,mul_comm]
    exact mul_le_mul_of_nonneg_right (Real.rpow_le_rpow hr.le h hb) (pow_nonneg hr.le _)
  · apply (measure_mono Set.inter_subset_right).trans
    apply (ambientSphereLaw_closedBall y hε (Module.finrank ℝ E-1) le_rfl).trans
    rw [mul_assoc, ← ENNReal.ofReal_mul (Real.rpow_nonneg hε.le _)]
    apply mul_le_mul' le_rfl
    apply ENNReal.ofReal_le_ofReal
    have hpow : ε^(Module.finrank ℝ E-1) ≤ ε^((a:ℝ)+b) := by
      rw [← Real.rpow_natCast]
      exact Real.rpow_le_rpow_of_exponent_ge hε hε1 hab
    apply hpow.trans
    rw [Real.rpow_add hε,Real.rpow_natCast,mul_comm]
    exact mul_le_mul_of_nonneg_left (pow_le_pow_left₀ hε.le (le_of_not_ge h) a) (Real.rpow_nonneg hε.le _)

variable {J : Type*} [Fintype J] [MeasurableSpace J] [MeasurableSingletonClass J] [Nonempty J]

lemma continuation_mixed_integral_bound {δ : ℕ → J → (E × ℝ) → J}
    (hδ : ∀ n j, Measurable (δ n j)) (y : ℕ → J → E)
    {ε : ℝ} (hε : 0 < ε) (hε1 : ε ≤ 1) {a q : ℕ} {b : ℝ}
    (hb : 0 ≤ b) (hab : (a:ℝ)+b ≤ (Module.finrank ℝ E-1:ℕ))
    (hq : 2 ≤ q) (hqa : 2*q ≤ a) (n k : ℕ) (j : J) :
    (∫⁻ s, continuation δ (fun n j => capWeight (y n j) ε) n k j s ∂ambientSphereLaw) ≤
      (ENNReal.ofReal ((3 : ℝ)*4^Module.finrank ℝ E) * ENNReal.ofReal (ε^b)) *
        streamFactor (E := E) (J := J) q ^ ((k+q-1)/q) := by
  have ha : a ≤ Module.finrank ℝ E := by
    have : (a:ℝ) ≤ (Module.finrank ℝ E:ℝ) := by
      have hh : ((Module.finrank ℝ E-1:ℕ):ℝ) ≤ Module.finrank ℝ E := by exact_mod_cast Nat.sub_le (Module.finrank ℝ E) 1
      linarith
    exact_mod_cast this
  apply ambient_mass_bound
  apply continuation_mass_bound_ceil _ hδ (fun n j => measurable_capWeight _ _)
    (fun n j s => capWeight_le_one _ _ _)
    (mul_ne_zero (by positivity) (by positivity)) (ENNReal.mul_ne_top ENNReal.ofReal_ne_top ENNReal.ofReal_ne_top)
    (fun n j => capWeight_mass_mixed (y n j) hε hε1 a hb hab)
    (Module.finrank_pos (R := ℝ) (M := E)) ha hq hqa

lemma route_mixed_bound {δ : ℕ → J → (E × ℝ) → J}
    (hδ : ∀ n j, Measurable (δ n j)) (y : J → E)
    {ε : ℝ} (hε : 0 < ε) (hε1 : ε ≤ 1) {a q : ℕ} {b : ℝ}
    (hb : 0 ≤ b) (hab : (a:ℝ)+b ≤ (Module.finrank ℝ E-1:ℕ))
    (hq : 2 ≤ q) (hqa : 2*q ≤ a) (k : ℕ) (j : J) :
    ((sphereLaw (E := E)).prod (Measure.pi (fun _ : Fin k => stdGaussian E)))
      {z | dist z.1.val (y (streamRoute δ 0 k j (z.2,fun i => ⟪z.1.val,z.2 i⟫))) ≤ ε} ≤
      (ENNReal.ofReal ((3 : ℝ)*4^Module.finrank ℝ E) * ENNReal.ofReal (ε^b)) *
        streamFactor (E := E) (J := J) q ^ ((k+q-1)/q) := by
  let w : ℕ → J → E → ℝ≥0∞ := fun _ j => capWeight (y j) ε
  have hV := measurable_continuation (w := w) hδ (fun _ j => measurable_capWeight (y j) ε) 0 k j
  have he (s : E) : continuation δ w 0 k j s =
      ∫⁻ A : Fin k → E, capWeight (y (streamRoute δ 0 k j (A,fun i => ⟪s,A i⟫))) ε s
        ∂Measure.pi (fun _ => stdGaussian E) := by
    simpa [w, blockValue, continuation] using continuation_unroll (w := w) hδ 0 k 0 j s
  let S : Set ((Metric.sphere (0 : E) 1) × (Fin k → E)) :=
    {z | dist z.1.val (y (streamRoute δ 0 k j (z.2,fun i => ⟪z.1.val,z.2 i⟫))) ≤ ε}
  have hS : MeasurableSet S := by
    apply measurableSet_le _ measurable_const
    exact measurable_subtype_coe.comp measurable_fst |>.dist
      ((measurable_of_finite y).comp ((measurable_streamRoute hδ 0 k j).comp (by fun_prop)))
  change ((sphereLaw (E := E)).prod _) S ≤ _
  rw [← one_mul (((sphereLaw (E := E)).prod _) S), ← lintegral_indicator_const hS 1,
    lintegral_prod _ (measurable_const.indicator hS).aemeasurable]
  have hf : ∀ s : Metric.sphere (0 : E) 1,
      (∫⁻ A, S.indicator (fun _ => (1 : ℝ≥0∞)) (s,A) ∂Measure.pi (fun _ => stdGaussian E)) =
        continuation δ w 0 k j s.val := by
    intro s
    rw [he]
    apply lintegral_congr
    intro A
    classical
    change (if dist s.val (y (streamRoute δ 0 k j (A,fun i => ⟪s.val,A i⟫))) ≤ ε then 1 else 0) = _
    unfold capWeight
    rw [Set.indicator_apply]
    rfl
  simp_rw [hf]
  rw [← lintegral_map hV measurable_subtype_coe]
  exact continuation_mixed_integral_bound hδ (fun _ => y) hε hε1 hb hab hq hqa 0 k j

end

namespace Learner

section
variable {d M : ℕ}

def haltShift {k : ℕ} : TaggedState M k → TaggedState M (k+1)
  | .inl j => .inl j
  | .inr (t,j) => .inr (t.succ,j)

def haltRoute (τ : ℕ → State M → (Vector d × ℝ) → Bool × State M) (n : ℕ) :
    (k : ℕ) → State M → ((Fin k → Vector d) × (Fin k → ℝ)) → TaggedState M k
  | 0, j, _ => .inl j
  | k+1, j, X =>
    let a := τ (n+1) j (X.1 0,X.2 0)
    if a.1 then .inr (⟨1,by omega⟩,a.2)
    else haltShift (haltRoute τ (n+1) k a.2 (Fin.tail X.1,Fin.tail X.2))

lemma measurable_haltRoute {τ : ℕ → State M → (Vector d × ℝ) → Bool × State M}
    (hτ : ∀ t j, Measurable (τ t j)) (n k : ℕ) (j : State M) :
    Measurable (haltRoute τ n k j) := by
  induction k generalizing n j with
  | zero => exact measurable_const
  | succ k ih =>
    let h : (Bool × State M) → ((Fin (k+1) → Vector d) × (Fin (k+1) → ℝ)) → TaggedState M (k+1) :=
      fun a X => if a.1 then .inr (⟨1,by omega⟩,a.2)
        else haltShift (haltRoute τ (n+1) k a.2 (Fin.tail X.1,Fin.tail X.2))
    change Measurable (fun X => h (τ (n+1) j (X.1 0,X.2 0)) X)
    apply measurable_select (J := Bool × State M) _ ((hτ (n+1) j).comp (by fun_prop))
    intro a
    dsimp [h]
    split_ifs
    · exact measurable_const
    · exact (measurable_of_finite haltShift).comp ((ih (n+1) a.2).comp (by
        change Measurable (fun X : ((Fin (k+1) → Vector d) × (Fin (k+1) → ℝ)) =>
          ((fun i : Fin k => X.1 i.succ),(fun i : Fin k => X.2 i.succ)))
        fun_prop))

def haltChild (w : ℕ → State M → Vector d → ℝ≥0∞)
    (v : State M → Vector d → ℝ≥0∞) (n : ℕ) {k : ℕ} : TaggedState M k → Vector d → ℝ≥0∞
  | .inl j => v j
  | .inr (t,j) => w (n+t) j

def stoppedContinuation (τ : ℕ → State M → (Vector d × ℝ) → Bool × State M)
    (w : ℕ → State M → Vector d → ℝ≥0∞) (n : ℕ) : ℕ → State M → Vector d → ℝ≥0∞
  | 0, j, s => w n j s
  | k+1, j, s => ∫⁻ x, let a := τ (n+1) j (x,⟪s,x⟫)
      if a.1 then w (n+1) a.2 s else stoppedContinuation τ w (n+1) k a.2 s ∂stdGaussian (Vector d)

lemma measurable_stoppedContinuation {τ : ℕ → State M → (Vector d × ℝ) → Bool × State M}
    {w : ℕ → State M → Vector d → ℝ≥0∞}
    (hτ : ∀ t j, Measurable (τ t j)) (hw : ∀ n j, Measurable (w n j)) (n k : ℕ) (j : State M) :
    Measurable (stoppedContinuation τ w n k j) := by
  induction k generalizing n j with
  | zero => exact hw n j
  | succ k ih =>
    apply Measurable.lintegral_prod_left
    change Measurable (fun z : Vector d × Vector d =>
      if (τ (n+1) j (z.1,⟪z.2,z.1⟫)).1 then w (n+1) (τ (n+1) j (z.1,⟪z.2,z.1⟫)).2 z.2
      else stoppedContinuation τ w (n+1) k (τ (n+1) j (z.1,⟪z.2,z.1⟫)).2 z.2)
    apply measurable_select (J := Bool × State M) (h := fun a (z : Vector d × Vector d) =>
      if a.1 then w (n+1) a.2 z.2 else stoppedContinuation τ w (n+1) k a.2 z.2) _
      ((hτ (n+1) j).comp (by fun_prop))
    intro a
    split_ifs
    · exact (hw (n+1) a.2).comp measurable_snd
    · exact (ih (n+1) a.2).comp measurable_snd

lemma stoppedContinuation_le_one
    {τ : ℕ → State M → (Vector d × ℝ) → Bool × State M}
    {w : ℕ → State M → Vector d → ℝ≥0∞}
    (hw : ∀ n j s, w n j s ≤ 1) (n k : ℕ) (j : State M) (s : Vector d) :
    stoppedContinuation τ w n k j s ≤ 1 := by
  induction k generalizing n j with
  | zero => exact hw n j s
  | succ k ih =>
    change (∫⁻ x, (if (τ (n+1) j (x,⟪s,x⟫)).1 then w (n+1) (τ (n+1) j (x,⟪s,x⟫)).2 s else stoppedContinuation τ w (n+1) k (τ (n+1) j (x,⟪s,x⟫)).2 s) ∂stdGaussian (Vector d)) ≤ 1
    apply (lintegral_mono (g := fun _ => (1:ℝ≥0∞)) (fun x => ?_)).trans_eq (by simp)
    split_ifs
    · exact hw _ _ _
    · exact ih _ _

lemma stoppedContinuation_unroll
    {τ : ℕ → State M → (Vector d × ℝ) → Bool × State M}
    {w : ℕ → State M → Vector d → ℝ≥0∞}
    (hτ : ∀ t j, Measurable (τ t j)) (n m k : ℕ) (j : State M) (s : Vector d) :
    stoppedContinuation τ w n (m+k) j s =
      blockValue (haltChild w (stoppedContinuation τ w (n+m) k) n) (haltRoute τ n m j) s := by
  induction m generalizing n j with
  | zero => simp [blockValue,haltRoute,haltChild]
  | succ m ih =>
    rw [Nat.succ_add,stoppedContinuation]
    unfold blockValue
    have hf : Measurable (fun X : Fin (m+1) → Vector d =>
        haltChild w (stoppedContinuation τ w (n+(m+1)) k) n
          (haltRoute τ n (m+1) j (X,fun i => ⟪s,X i⟫)) s) :=
      (measurable_of_finite (fun a => haltChild w (stoppedContinuation τ w (n+(m+1)) k) n a s)).comp
        ((measurable_haltRoute hτ n (m+1) j).comp (by fun_prop))
    rw [lintegral_pi_cons _ _ hf]
    apply lintegral_congr
    intro x
    have htail (X : Fin m → Vector d) : Fin.tail (fun i : Fin (m+1) => ⟪s,(Fin.cons x X : Fin (m+1) → Vector d) i⟫) = (fun i => ⟪s,X i⟫) := by funext i; rfl
    simp only [haltRoute,Fin.cons_zero,Fin.tail_cons,htail]
    split_ifs with h
    · simp [haltChild]
    · rw [ih]
      unfold blockValue
      apply lintegral_congr
      intro X
      have h1 : n+(m+1) = n+1+m := by omega
      rw [h1]
      generalize haltRoute τ (n+1) m (τ (n+1) j (x,⟪s,x⟫)).2 (X,fun i => ⟪s,X i⟫) = a
      cases a with
      | inl j => rfl
      | inr done =>
        simp only [haltShift,haltChild,Fin.val_succ]
        rw [show n+1+done.1.val = n+(done.1.val+1) by omega]

lemma stoppedContinuation_mass_bound {τ : ℕ → State M → (Vector d × ℝ) → Bool × State M}
    {w : ℕ → State M → Vector d → ℝ≥0∞}
    (hτ : ∀ t j, Measurable (τ t j)) (hw : ∀ n j, Measurable (w n j))
    (hw1 : ∀ n j s, w n j s ≤ 1)
    {a q : ℕ} {C : ℝ≥0∞} (hC : C ≠ 0) (hCt : C ≠ ∞)
    (hlocal : ∀ n j, PolynomialMass (ambientSphereLaw.withDensity (w n j)) a C)
    (hd : 1 ≤ d) (ha : a ≤ d) (hq : 2 ≤ q) (hqa : 2*q ≤ a)
    (n k : ℕ) (j : State M) :
    PolynomialMass (ambientSphereLaw.withDensity (stoppedContinuation τ w n k j)) a
      (C * streamFactor (E := Vector d) (J := TaggedState M q) q ^ ((k+q-1)/q)) := by
  let : NeZero d := ⟨by omega⟩
  let K := streamFactor (E := Vector d) (J := TaggedState M q) q
  have hK1 : 1 ≤ K := one_le_streamFactor (by omega)
  have hK0 : K ≠ 0 := ne_of_gt (lt_of_lt_of_le zero_lt_one hK1)
  have hKt : K ≠ ∞ := streamFactor_ne_top _
  have hden : ((q-1 : ℕ) : ℝ)+1 = (q : ℝ) := by rw [Nat.cast_sub (by omega),Nat.cast_one]; ring
  have hq0 : 0 < q := by omega
  change PolynomialMass _ a (C*K^_)
  induction k using Nat.strong_induction_on generalizing n j with
  | h k ih =>
    by_cases hk0 : k = 0
    · subst k
      simpa only [Nat.zero_add,Nat.div_eq_of_lt (by omega : q-1 < q),pow_zero,mul_one,stoppedContinuation] using hlocal n j
    let m := min k q
    have hm0 : 0 < m := by dsimp [m]; omega
    have hm : m ≤ q := min_le_right _ _
    have hmk : m ≤ k := min_le_left _ _
    have hki : k-m < k := by omega
    let C' := C*K^((k-m+q-1)/q)
    have hC' : C' ≠ 0 := mul_ne_zero hC (pow_ne_zero _ hK0)
    have hC't : C' ≠ ∞ := ENNReal.mul_ne_top hCt (ENNReal.pow_ne_top hKt)
    have hCC' : C ≤ C' := le_mul_of_one_le_right' (one_le_pow₀ hK1)
    let f := haltChild w (stoppedContinuation τ w (n+m) (k-m)) n (k := m)
    have hf (a : TaggedState M m) : Measurable (f a) := by
      cases a with
      | inl j => exact measurable_stoppedContinuation hτ hw _ _ _
      | inr done => exact hw _ _
    have hf1 (a : TaggedState M m) (s : Vector d) : f a s ≤ 1 := by
      cases a with
      | inl j => exact stoppedContinuation_le_one hw1 _ _ _ _
      | inr done => exact hw1 _ _ _
    have hflocal (a' : TaggedState M m) : PolynomialMass (ambientSphereLaw.withDensity (f a')) a C' := by
      cases a' with
      | inl j => exact ih (k-m) hki (n+m) j
      | inr done =>
        intro z r hr
        exact (hlocal (n+done.1) done.2 z r hr).trans (mul_le_mul' hCC' le_rfl)
    have hb := blockValue_mass_bound ambientSphereLaw hf hf1 hflocal hC' hC't
      (by simpa using hd) (by simpa using ha) (by simpa using (show m ≤ d by omega))
      (by omega : 1 ≤ q-1) (by omega : q-1+m ≤ a) (measurable_haltRoute hτ n m j)
    have he : stoppedContinuation τ w n k j = blockValue f (haltRoute τ n m j) := by
      funext s
      simpa [f,Nat.add_sub_of_le hmk] using stoppedContinuation_unroll hτ n m (k-m) j s
    rw [he]
    intro z r hr
    apply (hb z r hr).trans
    apply mul_le_mul' _ le_rfl
    have hdiv : (k+q-1)/q = (k-m+q-1)/q+1 := by
      by_cases hqk : q ≤ k
      · have hme : m = q := min_eq_right hqk
        rw [hme]
        have he : k+q-1 = q+(k-q+q-1) := by omega
        rw [he,Nat.add_div_left _ hq0]
      · have hme : m = k := min_eq_left (by omega)
        rw [hme,Nat.sub_self,Nat.zero_add,Nat.div_eq_of_lt (by omega : q-1 < q)]
        have he : k+q-1 = q+(k-1) := by omega
        rw [he,Nat.add_div_left _ hq0,Nat.div_eq_of_lt (by omega : k-1 < q)]
    rw [hden]
    have hcard : (Fintype.card (TaggedState M m):ℝ≥0∞)^(1/(q:ℝ)) ≤
        (Fintype.card (TaggedState M q):ℝ≥0∞)^(1/(q:ℝ)) := by
      apply ENNReal.rpow_le_rpow _ (by positivity)
      exact_mod_cast (show Fintype.card (TaggedState M m) ≤ Fintype.card (TaggedState M q) by
        rw [card_taggedState,card_taggedState]
        exact Nat.mul_le_mul_right _ (Nat.add_le_add_right hm 2))
    calc
      _ ≤ K*C' := mul_le_mul' (mul_le_mul' le_rfl hcard) le_rfl
      _ = _ := by rw [hdiv,pow_succ]; dsimp [C']; ring

end

variable {d M T : ℕ}

def taggedWeight (w : ℕ → State M → Vector d → ℝ≥0∞) (n : ℕ) : TaggedState M T → Vector d → ℝ≥0∞
  | .inl j => w n j
  | .inr (t,j) => w t j

lemma measurable_taggedWeight {w : ℕ → State M → Vector d → ℝ≥0∞}
    (hw : ∀ n j, Measurable (w n j)) (n : ℕ) (j : TaggedState M T) :
    Measurable (taggedWeight w n j) := by cases j <;> exact hw _ _

lemma continuation_tagged_eq {τ : ℕ → State M → (Vector d × ℝ) → Bool × State M}
    {w : ℕ → State M → Vector d → ℝ≥0∞}
    (n k : ℕ) (hnk : n+k ≤ T) (j : TaggedState M T) (s : Vector d) :
    continuation (taggedStep τ) (taggedWeight w) n k j s =
      match j with
      | .inl a => stoppedContinuation τ w n k a s
      | .inr (t,a) => w t a s := by
  induction k generalizing n j with
  | zero => cases j <;> rfl
  | succ k ih =>
    cases j with
    | inl j =>
      simp only [continuation,stoppedContinuation]
      apply lintegral_congr
      intro x
      rw [ih (n+1) (by omega)]
      simp only [taggedStep,dite_eq_left (by omega : n+1 ≤ T)]
      split_ifs <;> rfl
    | inr done =>
      simp only [continuation,taggedStep]
      simp only [ih (n+1) (by omega),lintegral_const,measure_univ,mul_one]

lemma tagged_continuation_mass {τ : ℕ → State M → (Vector d × ℝ) → Bool × State M}
    {w : ℕ → State M → Vector d → ℝ≥0∞}
    (hτ : ∀ t j, Measurable (τ t j)) (hw : ∀ n j, Measurable (w n j))
    (hw1 : ∀ n j s, w n j s ≤ 1)
    {a q : ℕ} {C : ℝ≥0∞} (hC : C ≠ 0) (hCt : C ≠ ∞)
    (hlocal : ∀ n j, PolynomialMass (ambientSphereLaw.withDensity (w n j)) a C)
    (hd : 1 ≤ d) (ha : a ≤ d) (hq : 2 ≤ q) (hqa : 2*q ≤ a)
    (j : TaggedState M T) :
    PolynomialMass (ambientSphereLaw.withDensity
      (continuation (taggedStep τ) (taggedWeight w) 0 T j)) a
      (C * streamFactor (E := Vector d) (J := TaggedState M q) q ^ ((T+q-1)/q)) := by
  have he := fun s => continuation_tagged_eq (τ := τ) (w := w) 0 T (by omega) j s
  cases j with
  | inl j =>
    have he' : continuation (taggedStep (T := T) τ) (taggedWeight w) 0 T (.inl j) = stoppedContinuation τ w 0 T j :=
      funext he
    rw [he']
    exact stoppedContinuation_mass_bound hτ hw hw1 hC hCt hlocal hd ha hq hqa 0 T j
  | inr done =>
    have he' : continuation (taggedStep (T := T) τ) (taggedWeight w) 0 T (.inr done) = w done.1 done.2 :=
      funext he
    rw [he']
    intro z r hr
    apply (hlocal _ _ z r hr).trans
    apply mul_le_mul' _ le_rfl
    exact le_mul_of_one_le_right' (one_le_pow₀ (one_le_streamFactor (by omega)))

variable [NeZero d]

lemma stopped_route_mixed_bound {τ : ℕ → State M → (Vector d × ℝ) → Bool × State M}
    (hτ : ∀ t j, Measurable (τ t j)) (y : ℕ → State M → Vector d)
    {ε : ℝ} (hε : 0 < ε) (hε1 : ε ≤ 1) {a q : ℕ} {b : ℝ}
    (hb : 0 ≤ b) (hab : (a:ℝ)+b ≤ (d-1:ℕ))
    (hq : 2 ≤ q) (hqa : 2*q ≤ a) (j : TaggedState M T) :
    ((uniformSphere d).prod (sampleLaw d T))
      {z | dist z.1.val
        (match streamRoute (taggedStep (T := T) τ) 0 T j (z.2,fun i => ⟪z.1.val,z.2 i⟫) with
        | .inl a => y T a
        | .inr (t,a) => y t a) ≤ ε} ≤
      (ENNReal.ofReal ((3:ℝ)*4^d)*ENNReal.ofReal (ε^b)) *
        streamFactor (E := Vector d) (J := TaggedState M q) q ^ ((T+q-1)/q) := by
  let w : ℕ → State M → Vector d → ℝ≥0∞ := fun n j => capWeight (y n j) ε
  let δ := taggedStep (T := T) τ
  let w' : ℕ → TaggedState M T → Vector d → ℝ≥0∞ := taggedWeight w
  let yy : TaggedState M T → Vector d := fun a => match a with
    | .inl a => y T a
    | .inr (t,a) => y t a
  have hw' (n : ℕ) (a : TaggedState M T) : Measurable (w' n a) :=
    measurable_taggedWeight (fun n j => measurable_capWeight _ _) n a
  have hδ := measurable_taggedStep (T := T) hτ
  have hV := measurable_continuation hδ hw' 0 T j
  have he (s : Vector d) : continuation δ w' 0 T j s =
      ∫⁻ A : Fin T → Vector d, capWeight (yy (streamRoute δ 0 T j (A,fun i => ⟪s,A i⟫))) ε s
        ∂Measure.pi (fun _ => stdGaussian (Vector d)) := by
    have hu := continuation_unroll (w := w') hδ 0 T 0 j s
    simp only [Nat.add_zero,continuation] at hu
    rw [hu]
    unfold blockValue
    apply lintegral_congr
    intro A
    cases streamRoute δ 0 T j (A,fun i => ⟪s,A i⟫) <;> simp [w',w,yy,taggedWeight]
  let S : Set (UnitSphere d × Samples d T) :=
    {z | dist z.1.val (yy (streamRoute δ 0 T j (z.2,fun i => ⟪z.1.val,z.2 i⟫))) ≤ ε}
  have hS : MeasurableSet S := by
    apply measurableSet_le _ measurable_const
    exact (measurable_subtype_coe.comp measurable_fst).dist
      ((measurable_of_finite yy).comp ((measurable_streamRoute hδ 0 T j).comp (by fun_prop)))
  change ((sphereLaw (E := Vector d)).prod _) S ≤ _
  rw [← one_mul (((sphereLaw (E := Vector d)).prod _) S), ← lintegral_indicator_const hS 1,
    lintegral_prod _ (measurable_const.indicator hS).aemeasurable]
  have hf : ∀ s : UnitSphere d,
      (∫⁻ A, S.indicator (fun _ => (1:ℝ≥0∞)) (s,A) ∂sampleLaw d T) = continuation δ w' 0 T j s.val := by
    intro s
    rw [he]
    apply lintegral_congr
    intro A
    classical
    change (if dist s.val (yy (streamRoute δ 0 T j (A,fun i => ⟪s.val,A i⟫))) ≤ ε then 1 else 0) = _
    unfold capWeight
    rw [Set.indicator_apply]
    rfl
  simp_rw [hf]
  rw [← lintegral_map hV measurable_subtype_coe]
  apply ambient_mass_bound (a := a)
  have ha : a ≤ d := by
    have hdn : ((d-1:ℕ):ℝ) ≤ (d:ℝ) := by exact_mod_cast Nat.sub_le d 1
    have : (a:ℝ) ≤ d := by linarith
    exact_mod_cast this
  have hl (n : ℕ) (j : State M) : PolynomialMass (ambientSphereLaw.withDensity (w n j)) a
      (ENNReal.ofReal ((3:ℝ)*4^d)*ENNReal.ofReal (ε^b)) := by
    simpa only [finrank_euclideanSpace_fin] using capWeight_mass_mixed (y n j) hε hε1 a hb (by simpa using hab)
  exact tagged_continuation_mass hτ (fun n j => measurable_capWeight _ _)
    (fun n j s => capWeight_le_one _ _ _)
    (mul_ne_zero (by positivity) (by positivity))
    (ENNReal.mul_ne_top ENNReal.ofReal_ne_top ENNReal.ofReal_ne_top)
    hl
    (Nat.pos_of_ne_zero (NeZero.ne d)) ha hq hqa j

end Learner
end NoiselessRegression

end

end OAI