File size: 25,793 Bytes
b317718
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
import OAI.Probability.GaussianReplacement.FiberModel

namespace OAI

open MeasureTheory ProbabilityTheory Set Filter
open scoped ENNReal NNReal RealInnerProductSpace Topology
noncomputable section
namespace CurrentProjection
open InnerProductSpace
open InformationTheory



section GaussianDirection
variable {E : Type*} [NormedAddCommGroup E] [InnerProductSpace ℝ E]
  [finiteDimensionalRealE : FiniteDimensional ℝ E] [measurableSpaceE : MeasurableSpace E] [borelSpaceE : BorelSpace E]

def directionComplement (u : E) : E →L[ℝ] E :=
  ContinuousLinearMap.id ℝ E - (innerSL ℝ u).smulRight u

omit finiteDimensionalRealE measurableSpaceE borelSpaceE in
lemma directionComplement_apply (u g : E) : directionComplement u g = g - ⟪u,g⟫ • u := rfl

omit finiteDimensionalRealE measurableSpaceE borelSpaceE in
lemma directionComplement_inner (u g y : E) :
    ⟪y,directionComplement u g⟫ = ⟪y-⟪u,y⟫ • u,g⟫ := by
  simp only [directionComplement_apply,inner_sub_right,inner_smul_right,inner_sub_left,
    inner_smul_left,conj_trivial,real_inner_comm u y]
  ring

lemma gaussian_direction_indep {u : E} (hu : ‖u‖ = 1) :
    IndepFun (fun g : E => ⟪u,g⟫) (directionComplement u) (stdGaussian E) := by
  have hG := (IsGaussian.hasGaussianLaw_id (μ:=stdGaussian E)).map_fun
    ((innerSL ℝ u).prod (directionComplement u))
  change HasGaussianLaw (fun g : E => (⟪u,g⟫,directionComplement u g)) (stdGaussian E) at hG
  apply hG.indepFun_of_covariance_inner
  intro x y
  have he : (fun g : E => ⟪x,⟪u,g⟫⟫) = fun g => ⟪x • u,g⟫ := by
    funext g
    simp only [Real.inner_apply,inner_smul_left,conj_trivial]
  simp_rw [he,directionComplement_inner]
  rw [←covarianceBilin_apply_eq_cov IsGaussian.memLp_two_id,covarianceBilin_stdGaussian]
  change ⟪x • u,y-⟪u,y⟫ • u⟫ = 0
  simp only [inner_smul_left,conj_trivial,inner_sub_right,inner_smul_right,
    real_inner_self_eq_norm_sq,hu,one_pow,mul_one]
  ring

lemma gaussian_direction_real {u : E} (hu : ‖u‖ = 1) :
    (stdGaussian E).map (fun g => ⟪u,g⟫) = gaussianReal 0 1 := by
  have hG := (IsGaussian.hasGaussianLaw_id (μ:=stdGaussian E)).map_fun (innerSL ℝ u)
  change HasGaussianLaw (fun g : E => ⟪u,g⟫) (stdGaussian E) at hG
  rw [hG.map_eq_gaussianReal]
  have hi : (∫ g : E, ⟪u,g⟫ ∂stdGaussian E) = 0 := integral_strongDual_stdGaussian (innerSL ℝ u)
  have hv : Var[fun g : E => ⟪u,g⟫; stdGaussian E] = 1 := by
    rw [←covariance_self (by fun_prop),←covarianceBilin_apply_eq_cov IsGaussian.memLp_two_id,
      covarianceBilin_stdGaussian,innerSL_apply_apply,real_inner_self_eq_norm_sq,hu,one_pow]
  simp only [hi,hv,Real.toNNReal_one]

lemma gaussian_direction_split {u : E} (hu : ‖u‖ = 1) :
    (stdGaussian E).map (fun g => (⟪u,g⟫,directionComplement u g)) =
      (gaussianReal 0 1).prod ((stdGaussian E).map (directionComplement u)) := by
  rw [(gaussian_direction_indep hu).map_prod_eq_prod_map_map (by fun_prop) (by fun_prop),
    gaussian_direction_real hu]

end GaussianDirection

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

lemma gaussian_direction_rows_split {u : E} (hu : ‖u‖ = 1) (k : ℕ) :
    (Measure.pi (fun _ : Fin k => stdGaussian E)).map
      (fun G => ((fun i => ⟪u,G i⟫),(fun i => directionComplement u (G i)))) =
      (Measure.pi (fun _ : Fin k => gaussianReal 0 1)).prod
        (Measure.pi (fun _ : Fin k => (stdGaussian E).map (directionComplement u))) := by
  let e := MeasurableEquiv.arrowProdEquivProdArrow ℝ E (Fin k)
  have he : (fun G : Fin k→E => ((fun i => ⟪u,G i⟫),(fun i => directionComplement u (G i)))) =
      e ∘ (fun G => fun i => (⟪u,G i⟫,directionComplement u (G i))) := rfl
  rw [he,←Measure.map_map e.measurable (by fun_prop),
    Measure.pi_map_pi (f := fun (_ : Fin k) => fun g : E => (⟪u,g⟫,directionComplement u g))
      (μ := fun _ => stdGaussian E) (fun i => by fun_prop)]
  simp_rw [gaussian_direction_split hu]
  exact (measurePreserving_arrowProdEquivProdArrow ℝ E (Fin k)
    (fun _ => gaussianReal 0 1) (fun _ => (stdGaussian E).map (directionComplement u))).map_eq

lemma gaussian_direction_rows_recover {u : E} (hu : ‖u‖ = 1) (k : ℕ) :
    ((Measure.pi (fun _ : Fin k => gaussianReal 0 1)).prod
        (Measure.pi (fun _ : Fin k => (stdGaussian E).map (directionComplement u)))).map
      (fun z => fun i => z.1 i • u+z.2 i) = Measure.pi (fun _ : Fin k => stdGaussian E) := by
  rw [←gaussian_direction_rows_split hu k,Measure.map_map (by fun_prop) (by fun_prop)]
  have he : (fun z : (Fin k→ℝ)×(Fin k→E) => fun i => z.1 i • u+z.2 i) ∘
      (fun G : Fin k→E => ((fun i => ⟪u,G i⟫),(fun i => directionComplement u (G i)))) = id := by
    funext G i
    simp [directionComplement_apply]
  rw [he,Measure.map_id]
end GaussianDirectionRows




section FiberAverage
variable {X : Type*} [PseudoMetricSpace X] [MeasurableSpace X] [OpensMeasurableSpace X]
  (μ : Measure X)

lemma tendsto_normalized_closedBall {f : X → ℝ≥0∞} {x : X}
    (hf : ContinuousAt f x)
    (hvol : ∀ r : ℝ, 0 < r → μ (Metric.closedBall x r) ≠ 0 ∧
      μ (Metric.closedBall x r) ≠ ∞) :
    Tendsto (fun r : ℝ => (μ (Metric.closedBall x r))⁻¹ *
      ∫⁻ y in Metric.closedBall x r, f y ∂μ) (𝓝[>] 0) (𝓝 (f x)) := by
  apply tendsto_order.2
  constructor
  · intro a ha
    obtain ⟨b,hab,hb⟩ := exists_between ha
    have hnb : {y | b < f y} ∈ 𝓝 x := hf (Ioi_mem_nhds hb)
    have he : ∀ᶠ r in 𝓝[>] (0:ℝ), Metric.closedBall x r ⊆ _ :=
      (eventually_closedBall_subset hnb).filter_mono inf_le_left
    filter_upwards [he, self_mem_nhdsWithin] with r hr hr0
    have hv := hvol r hr0
    apply hab.trans_le
    calc
      b = (μ (Metric.closedBall x r))⁻¹ * (b * μ (Metric.closedBall x r)) := by
        rw [mul_left_comm, ENNReal.inv_mul_cancel hv.1 hv.2, mul_one]
      _ ≤ _ := mul_le_mul_right (by
        rw [←setLIntegral_const]
        apply setLIntegral_mono' Metric.isClosed_closedBall.measurableSet
        intro y hy
        exact (hr hy).le) _
  · intro a ha
    obtain ⟨b,hb,hba⟩ := exists_between ha
    have hnb : {y | f y < b} ∈ 𝓝 x := hf (Iio_mem_nhds hb)
    have he : ∀ᶠ r in 𝓝[>] (0:ℝ), Metric.closedBall x r ⊆ _ :=
      (eventually_closedBall_subset hnb).filter_mono inf_le_left
    filter_upwards [he, self_mem_nhdsWithin] with r hr hr0
    have hv := hvol r hr0
    apply lt_of_le_of_lt _ hba
    calc
      _ ≤ (μ (Metric.closedBall x r))⁻¹ * (b * μ (Metric.closedBall x r)) := by
        apply mul_le_mul_right
        rw [←setLIntegral_const]
        apply setLIntegral_mono' Metric.isClosed_closedBall.measurableSet
        intro y hy
        exact (hr hy).le
      _ = b := by rw [mul_left_comm, ENNReal.inv_mul_cancel hv.1 hv.2,mul_one]
end FiberAverage

lemma ennreal_double_mul_le_sq_add (a b : ℝ≥0∞) : 2*(a*b) ≤ a^2+b^2 := by
  have h := ENNReal.young_inequality a b Real.HolderConjugate.two_two
  norm_num only [ENNReal.rpow_two,ENNReal.ofReal_ofNat] at h
  calc
    _ ≤ 2*(a^2/2+b^2/2) := mul_le_mul_right h 2
    _ = _ := by
      rw [mul_add,div_eq_mul_inv,div_eq_mul_inv]
      rw [mul_left_comm 2, mul_left_comm 2]
      rw [ENNReal.mul_inv_cancel (by norm_num) (by norm_num)]
      simp

lemma cube_energy_contraction {k : ℕ} (p K : (Fin k → ℝ) → ℝ≥0∞)
    (hp : Measurable p) (hK : Measurable K) (hKn : ∫⁻ z, K z = 1) :
    (∫⁻ x, ∫⁻ y, p x*p y*K (y-x)) ≤ ∫⁻ x, p x^2 := by
  have hA : (∫⁻ x, ∫⁻ y, p x^2*K (y-x)) = ∫⁻ x, p x^2 := by
    congr 1
    funext x
    rw [lintegral_const_mul _ (show Measurable (fun y => K (y-x)) by fun_prop),
      lintegral_sub_right_eq_self,hKn,mul_one]
  have hB : (∫⁻ x, ∫⁻ y, p y^2*K (y-x)) = ∫⁻ y, p y^2 := by
    rw [lintegral_lintegral_swap (by fun_prop)]
    congr 1
    funext y
    rw [lintegral_const_mul _ (show Measurable (fun x => K (y-x)) by fun_prop),
      lintegral_sub_left_eq_self,hKn,mul_one]
  have he : 2*(∫⁻ x, ∫⁻ y, p x*p y*K (y-x)) ≤ 2*(∫⁻ x, p x^2) := by
    calc
      _ = ∫⁻ x, ∫⁻ y, 2*(p x*p y)*K (y-x) := by
        simp_rw [mul_assoc,lintegral_const_mul' (2:ℝ≥0∞) _ (by norm_num)]
      _ ≤ ∫⁻ x, ∫⁻ y, (p x^2+p y^2)*K (y-x) := by
        apply lintegral_mono
        intro x
        apply lintegral_mono
        intro y
        exact mul_le_mul_left (ennreal_double_mul_le_sq_add _ _) _
      _ = (∫⁻ x, ∫⁻ y, p x^2*K (y-x)) + (∫⁻ x, ∫⁻ y, p y^2*K (y-x)) := by
        simp_rw [add_mul]
        rw [show (fun x => ∫⁻ y, p x^2*K (y-x)+p y^2*K (y-x)) =
          (fun x => (∫⁻ y, p x^2*K (y-x))+(∫⁻ y, p y^2*K (y-x))) by
            funext x; exact lintegral_add_left (by fun_prop) _]
        exact lintegral_add_left (by fun_prop) _
      _ = _ := by rw [hA,hB,two_mul]
  exact (ENNReal.mul_le_mul_iff_right (by norm_num : (2:ℝ≥0∞) ≠ 0) (by norm_num)).mp he



lemma continuous_lintegral_bounded {A X : Type*} [TopologicalSpace A] [FirstCountableTopology A]
    [MeasurableSpace X] {μ : Measure X} [IsFiniteMeasure μ]
    (f : A → X → ℝ≥0∞) (hf : ∀ x, Continuous (fun a => f a x))
    (hm : ∀ a, Measurable (f a)) (M : ℝ≥0∞) (hM : M ≠ ∞)
    (hb : ∀ a x, f a x ≤ M) : Continuous (fun a => ∫⁻ x, f a x ∂μ) := by
  apply continuous_iff_continuousAt.2
  intro a
  exact tendsto_lintegral_filter_of_dominated_convergence (fun _ => M)
    (Eventually.of_forall hm) (Eventually.of_forall (fun a => ae_of_all _ (hb a)))
    (by simp only [lintegral_const]; exact ENNReal.mul_ne_top hM (measure_ne_top _ _))
    (ae_of_all _ (fun x => (hf x).continuousAt))

lemma tendsto_scaled_cube_average {k : ℕ} (F : (Fin k→ℝ) → ℝ≥0∞)
    (hF : ContinuousAt F 0) {r : ℝ} (hr : 0 < r) :
    Tendsto (fun δ : ℝ => ENNReal.ofReal ((2*δ)⁻¹ ^ k) *
      ∫⁻ z in Metric.closedBall (0 : Fin k→ℝ) (δ/r), F z)
      (𝓝[>] 0) (𝓝 (ENNReal.ofReal (r⁻¹ ^ k) * F 0)) := by
  have hv : ∀ a : ℝ, 0 < a →
      (volume : Measure (Fin k→ℝ)) (Metric.closedBall 0 a) ≠ 0 ∧
      (volume : Measure (Fin k→ℝ)) (Metric.closedBall 0 a) ≠ ∞ := by
    intro a ha
    rw [Real.volume_pi_closedBall 0 ha.le]
    constructor
    · exact (ENNReal.ofReal_pos.mpr (by positivity)).ne'
    · exact ENNReal.ofReal_ne_top
  have hz : Tendsto (fun δ : ℝ => δ/r) (𝓝[>] (0:ℝ)) (𝓝 (0/r)) :=
    ((tendsto_id : Tendsto (fun δ : ℝ => δ) (𝓝 (0:ℝ)) (𝓝 (0:ℝ))).mono_left
      (show 𝓝[>] (0:ℝ) ≤ 𝓝 (0:ℝ) from inf_le_left)).div_const r
  rw [zero_div] at hz
  have ht := (tendsto_normalized_closedBall volume hF hv).comp
    (show Tendsto (fun δ : ℝ => δ/r) (𝓝[>] 0) (𝓝[>] 0) from
      tendsto_nhdsWithin_iff.2 ⟨hz,
        by filter_upwards [self_mem_nhdsWithin] with δ hδ; exact div_pos hδ hr⟩)
  have ht' := ENNReal.Tendsto.const_mul ht (Or.inr (ENNReal.ofReal_ne_top : ENNReal.ofReal (r⁻¹ ^ k) ≠ ∞))
  apply ht'.congr'
  filter_upwards [self_mem_nhdsWithin] with δ hδ
  change 0 < δ at hδ
  dsimp only [Function.comp_def]
  rw [Real.volume_pi_closedBall 0 (div_nonneg hδ.le hr.le)]
  simp only [Fintype.card_fin]
  rw [←mul_assoc]
  congr 1
  rw [←ENNReal.ofReal_inv_of_pos (by positivity : (0:ℝ) < (2*(δ/r))^k),
    ←ENNReal.ofReal_mul (by positivity)]
  congr 1
  simp only [inv_pow]
  rw [←mul_inv_rev,←mul_pow]
  congr 2
  field_simp

section GaussianSlab
variable {E : Type*} [NormedAddCommGroup E] [InnerProductSpace ℝ E]
  [finiteDimensionalRealE : FiniteDimensional ℝ E] [measurableSpaceE : MeasurableSpace E] [borelSpaceE : BorelSpace E]

def gaussianReconstruct (u : E) {k : ℕ} (zG : (Fin k→ℝ)×(Fin k→E)) : Fin k→E :=
  fun i => zG.1 i • u + directionComplement u (zG.2 i)

lemma gaussian_reconstruct_law {u : E} (hu : ‖u‖ = 1) (k : ℕ) :
    ((Measure.pi (fun _ : Fin k => gaussianReal 0 1)).prod
      (Measure.pi (fun _ : Fin k => stdGaussian E))).map (gaussianReconstruct u) =
      Measure.pi (fun _ : Fin k => stdGaussian E) := by
  have hm : Measurable (fun G : Fin k→E => fun i => directionComplement u (G i)) := by fun_prop
  have he := Measure.pi_map_pi (μ := fun _ : Fin k => stdGaussian E)
    (f := fun _ => directionComplement u) (fun _ => (directionComplement u).measurable.aemeasurable)
  calc
    _ = (((Measure.pi (fun _ : Fin k => gaussianReal 0 1)).prod
      ((Measure.pi (fun _ : Fin k => stdGaussian E)).map
        (fun G => fun i => directionComplement u (G i)))).map
        (fun z => fun i => z.1 i • u+z.2 i)) := by
      rw [show Measure.pi (fun _ : Fin k => gaussianReal 0 1) =
        (Measure.pi (fun _ : Fin k => gaussianReal 0 1)).map id by rw [Measure.map_id],
        Measure.map_prod_map _ _ measurable_id hm,
        Measure.map_map (by fun_prop) (by fun_prop)]
      rw [Measure.map_id]
      rfl
    _ = _ := by rw [he]; exact gaussian_direction_rows_recover hu k

omit finiteDimensionalRealE measurableSpaceE borelSpaceE in
lemma gaussian_reconstruct_pairing {u : E} (hu : ‖u‖ = 1) {k : ℕ}
    (zG : (Fin k→ℝ)×(Fin k→E)) (i : Fin k) :
    ⟪u,gaussianReconstruct u zG i⟫ = zG.1 i := by
  simp [gaussianReconstruct,directionComplement_apply,inner_add_right,
    inner_sub_right,inner_smul_right,hu]

lemma continuous_gaussian_slice {u : E} {k : ℕ} (H : BoundedContinuousFunction (Fin k→E) ℝ≥0) :
    Continuous (fun z : Fin k→ℝ => ∫⁻ G : Fin k→E,
      (H (gaussianReconstruct u (z,G)) : ℝ≥0∞) ∂Measure.pi (fun _ => stdGaussian E)) := by
  apply continuous_lintegral_bounded _ (fun G => by
    apply ENNReal.continuous_coe.comp
    exact H.continuous.comp (by unfold gaussianReconstruct; fun_prop))
    (fun z => by apply ENNReal.continuous_coe.measurable.comp
                 exact H.continuous.measurable.comp (by unfold gaussianReconstruct; fun_prop))
    ((nndist H 0 : ℝ≥0) : ℝ≥0∞) ENNReal.coe_ne_top
  intro z G
  apply ENNReal.coe_le_coe.mpr
  simpa using (BoundedContinuousFunction.nndist_coe_le_nndist (f:=H) (g:=0) (gaussianReconstruct u (z,G)))
end GaussianSlab




def fiberEnergyBound (d : ℕ) : ℝ≥0∞ := momentConstant 9 d (3*4^d) (3*4^d)

lemma fiberEnergyBound_ne_top (d : ℕ) : fiberEnergyBound d ≠ ∞ := ENNReal.ofReal_ne_top

lemma uniformSphere_ae_ne {d : ℕ} (hd : 2 ≤ d) (s : Sphere d) :
    ∀ᵐ t ∂uniformSphere d, t ≠ s := by
  have hz := (uniformSphere_growth (by omega : 1 ≤ d) (le_refl (d-1))).sphereLift.affine_span_null
    (by positivity) (⊥ : Submodule ℝ (Vector d)) (s:Vector d) (by simp; omega)
    (sphereLift_ae_norm_sub_le (uniformSphere d) s)
  simp only [Submodule.mem_bot,sub_eq_zero] at hz
  rw [sphereLift,Measure.map_apply continuous_subtype_val.measurable (show MeasurableSet {u : Vector d | u = (s:Vector d)} from isClosed_eq continuous_id continuous_const |>.measurableSet)] at hz
  simpa only [ae_iff,not_not,Set.preimage_ofPred_eq,Subtype.val_inj] using hz

lemma spherical_inverse_bound {d k : ℕ} (hk : 1 ≤ k) (hkd : k < d-1)
    (s : Sphere d) :
    (∫⁻ t : Sphere d, ENNReal.ofReal (‖(t:Vector d)-(s:Vector d)‖⁻¹ ^ k)
      ∂uniformSphere d) ≤ fiberEnergyBound d := by
  have hB : AmbientGrowth (sphereLift (uniformSphere d)) (0+k) (3*4^d) := by
    simpa only [zero_add] using (uniformSphere_growth (by omega : 1 ≤ d) hkd.le).sphereLift
  have hh := hB.mixed_inverse_uniform
    ((uniformSphere_growth (by omega : 1 ≤ d) (le_refl (d-1))).sphereLift)
    (by positivity) (by positivity) (m:=0) (by simpa using hkd) (Nat.sub_le d 1)
    (⊥ : Submodule ℝ (Vector d)) (s:Vector d) (by simpa using hk)
    (sphereLift_ae_norm_sub_le (uniformSphere d) s)
  rw [sphereLift,lintegral_map (measurable_mixedInverse _ _ _ _) continuous_subtype_val.measurable] at hh
  simpa only [mixedInverse,pow_zero,mul_one,fiberEnergyBound] using hh

lemma spherical_cube_expectation {d k : ℕ} (hk : 1 ≤ k) (hkd : k < d-1)
    (s : Sphere d) {δ : ℝ} (hδ : 0 < δ) :
    (∫⁻ C, cubeAverage (uniformSphere d) C (label C (s:Vector d)) δ
      ∂gaussianRows k d) ≤ fiberEnergyBound d := by
  have he : ∀ C : Rows k d, cubeAverage (uniformSphere d) C (label C (s:Vector d)) δ =
      ∫⁻ t : Sphere d, rowKernel (cubeKernel δ) (s:Vector d) (t:Vector d) C ∂uniformSphere d := by
    intro C
    rw [cubeAverage_eq_kernel _ _ hδ.le,sphereLift,
      lintegral_map (measurable_rowKernel_left _ (measurable_cubeKernel δ) _ _ C)
        continuous_subtype_val.measurable]
  simp_rw [he]
  have hK := measurable_cubeKernel δ
  rw [lintegral_lintegral_swap (by dsimp [rowKernel]; fun_prop)]
  apply le_trans _ (spherical_inverse_bound hk hkd s)
  apply lintegral_mono_ae
  filter_upwards [uniformSphere_ae_ne (by omega) s] with t ht
  exact gaussian_fresh_kernel_bound (cubeKernel δ) (measurable_cubeKernel δ)
    (lintegral_cubeKernel hδ) k (s:Vector d) (t:Vector d) (Subtype.coe_injective.ne ht)

lemma spherical_extended_energy {d k : ℕ} (hk : 1 ≤ k) (hkd : k < d-1) :
    (∫⁻ z : Rows k d×Sphere d,
      extendedCubeDensity (uniformSphere d) z.1 (label z.1 (z.2:Vector d))
      ∂(gaussianRows k d).prod (uniformSphere d)) ≤ fiberEnergyBound d := by
  let : IsProbabilityMeasure (uniformSphere d) := uniformSphere_probability_of_pos (by omega)
  change (∫⁻ z, liminf (fun j : ℕ => cubeAverage (uniformSphere d) z.1
    (label z.1 (z.2:Vector d)) (1/(j:ℝ))) atTop
    ∂(gaussianRows k d).prod (uniformSphere d)) ≤ _
  apply (lintegral_liminf_le (fun j => measurable_own_cube (uniformSphere d) _)).trans
  apply liminf_le_of_frequently_le'
  apply Eventually.frequently
  filter_upwards [eventually_ge_atTop 1] with j hj
  rw [lintegral_prod _ (measurable_own_cube (k:=k) (uniformSphere d) _).aemeasurable,
    lintegral_lintegral_swap (measurable_own_cube (k:=k) (uniformSphere d) _).aemeasurable]
  calc
    _ ≤ ∫⁻ s : Sphere d, fiberEnergyBound d ∂uniformSphere d := by
      apply lintegral_mono
      intro s
      exact spherical_cube_expectation hk hkd s (by positivity)
    _ = _ := by simp

lemma spherical_projection_ac {d k : ℕ} (hk : 1 ≤ k) (hkd : k < d-1) :
    ∀ᵐ C ∂gaussianRows k d, labelLaw (uniformSphere d) C ≪ volume := by
  have hf := ae_lt_top (measurable_own_extended (k:=k) (uniformSphere d))
    (ne_of_lt (lt_of_le_of_lt (spherical_extended_energy hk hkd) (lt_top_iff_ne_top.mpr (fiberEnergyBound_ne_top d))))
  have hff := Measure.ae_ae_of_ae_prod hf
  filter_upwards [hff] with C hC
  exact labelLaw_ac_of_finite_extended (uniformSphere d) C hC




lemma projection_convolution_contraction {k : ℕ} (μ : Measure (Fin k→ℝ))
    (p K : (Fin k→ℝ) → ℝ≥0∞) (hp : Measurable p) (hK : Measurable K)
    (hμ : volume.withDensity p = μ) (hKn : ∫⁻ z, K z = 1) :
    (∫⁻ x, ∫⁻ y, K (y-x) ∂μ ∂μ) ≤ ∫⁻ x, p x ∂μ := by
  rw [←hμ]
  have hi (x : Fin k→ℝ) : (∫⁻ y, K (y-x) ∂volume.withDensity p) =
      ∫⁻ y, p y*K (y-x) := by
    exact lintegral_withDensity_eq_lintegral_mul _ hp (by fun_prop)
  simp_rw [hi]
  rw [lintegral_withDensity_eq_lintegral_mul _ hp (by fun_prop),
    lintegral_withDensity_eq_lintegral_mul _ hp hp]
  simp only [Pi.mul_apply]
  have he : (∫⁻ x, p x*(∫⁻ y, p y*K (y-x))) =
      ∫⁻ x, ∫⁻ y, p x*p y*K (y-x) := by
    congr 1
    funext x
    rw [←lintegral_const_mul _ (show Measurable (fun y => p y*K (y-x)) by fun_prop)]
    simp only [mul_assoc]
  rw [he]
  simpa only [pow_two] using cube_energy_contraction p K hp hK hKn

lemma cubeVectorKernel_normalized {k : ℕ} {δ : ℝ} (hδ : 0 < δ) :
    (∫⁻ z : Fin k→ℝ, ∏ i, cubeKernel δ (z i)) = 1 := by
  rw [volume_pi,lintegral_fin_prod (volume : Measure ℝ) k (fun _ => cubeKernel δ) (fun _ => (measurable_cubeKernel δ))]
  simp [lintegral_cubeKernel hδ]

lemma cubeAverage_labelKernel {d k : ℕ} (ρ : Measure (Sphere d)) (C : Rows k d)
    {δ : ℝ} (hδ : 0 < δ) (s : Sphere d) :
    cubeAverage ρ C (label C (s:Vector d)) δ =
      ∫⁻ y, ∏ i, cubeKernel δ (y i-label C (s:Vector d) i) ∂labelLaw ρ C := by
  rw [cubeAverage_eq_kernel _ _ hδ.le,sphereLift,labelLaw,
    lintegral_map (measurable_rowKernel_left _ (measurable_cubeKernel δ) _ _ C) continuous_subtype_val.measurable,
    lintegral_map (by have := measurable_cubeKernel δ; fun_prop) (by unfold label; fun_prop)]
  congr 1
  funext t
  simp only [rowKernel,label,inner_sub_left]

lemma spherical_cube_energy_eq {d k : ℕ} (ρ : Measure (Sphere d)) [IsFiniteMeasure ρ]
    (C : Rows k d) {δ : ℝ} (hδ : 0 < δ) :
    (∫⁻ s : Sphere d, cubeAverage ρ C (label C (s:Vector d)) δ ∂ρ) =
      ∫⁻ x, ∫⁻ y, (∏ i, cubeKernel δ ((y-x) i)) ∂labelLaw ρ C ∂labelLaw ρ C := by
  have hK : Measurable (fun z : Fin k→ℝ => ∏ i, cubeKernel δ (z i)) := by
    have := measurable_cubeKernel δ
    fun_prop
  have hf : Measurable (fun s : Sphere d => label C (s:Vector d)) := by unfold label; fun_prop
  have hm : Measurable (fun x : Fin k→ℝ => ∫⁻ y,
      (∏ i, cubeKernel δ ((y-x) i)) ∂labelLaw ρ C) :=
    Measurable.lintegral_prod_right (hK.comp (measurable_snd.sub measurable_fst))
  calc
    _ = ∫⁻ s : Sphere d, ∫⁻ y, (∏ i, cubeKernel δ ((y-label C (s:Vector d)) i)) ∂labelLaw ρ C ∂ρ := by
      apply lintegral_congr
      intro s
      exact cubeAverage_labelKernel ρ C hδ s
    _ = _ := (lintegral_map hm hf).symm

lemma lintegral_labelLaw {d k : ℕ} (ρ : Measure (Sphere d)) (C : Rows k d)
    (f : (Fin k→ℝ) → ℝ≥0∞) (hf : Measurable f) :
    (∫⁻ y, f y ∂labelLaw ρ C) = ∫⁻ s : Sphere d, f (label C (s:Vector d)) ∂ρ :=
  lintegral_map hf (by unfold label; fun_prop)

lemma spherical_cube_energy_le {d k : ℕ} (ρ : Measure (Sphere d)) [IsFiniteMeasure ρ]
    (C : Rows k d) (hC : labelLaw ρ C ≪ volume) {δ : ℝ} (hδ : 0 < δ) :
    (∫⁻ s : Sphere d, cubeAverage ρ C (label C (s:Vector d)) δ ∂ρ) ≤
      ∫⁻ s : Sphere d, exactDensity ρ C (label C (s:Vector d)) ∂ρ := by
  have hpair : Measurable (fun y : Fin k→ℝ => (C,y)) := measurable_const.prodMk measurable_id
  have hp := (measurable_exactDensity (k:=k) ρ).comp hpair
  change Measurable (fun y : Fin k→ℝ => exactDensity ρ C y) at hp
  have hK : Measurable (fun z : Fin k→ℝ => ∏ i, cubeKernel δ (z i)) := by
    have := measurable_cubeKernel δ
    fun_prop
  rw [spherical_cube_energy_eq ρ C hδ]
  calc
    _ ≤ ∫⁻ y, exactDensity ρ C y ∂labelLaw ρ C :=
      projection_convolution_contraction (labelLaw ρ C) (fun y => exactDensity ρ C y)
        (fun z => ∏ i, cubeKernel δ (z i)) hp hK
        (labelLaw_withDensity_exact ρ C hC) (cubeVectorKernel_normalized (k:=k) hδ)
    _ = _ := lintegral_labelLaw ρ C _ hp

lemma spherical_quadratic_limit {d k : ℕ} (ρ : Measure (Sphere d)) [IsFiniteMeasure ρ]
    (H : BoundedContinuousFunction (Rows k d) ℝ≥0)
    (hAC : ∀ᵐ C ∂gaussianRows k d, labelLaw ρ C ≪ volume) :
    Tendsto (fun j : ℕ => ∫⁻ z : Rows k d×Sphere d,
      (H z.1 : ℝ≥0∞)*cubeAverage ρ z.1 (label z.1 (z.2:Vector d)) (1/(j:ℝ))
      ∂(gaussianRows k d).prod ρ) atTop
      (𝓝 (∫⁻ z : Rows k d×Sphere d, (H z.1 : ℝ≥0∞)*exactDensity ρ z.1 (label z.1 (z.2:Vector d))
        ∂(gaussianRows k d).prod ρ)) := by
  have hH : Measurable (fun C => (H C : ℝ≥0∞)) := ENNReal.continuous_coe.measurable.comp H.continuous.measurable
  have hm (j : ℕ) : Measurable (fun z : Rows k d×Sphere d =>
      (H z.1 : ℝ≥0∞)*cubeAverage ρ z.1 (label z.1 (z.2:Vector d)) (1/(j:ℝ))) :=
    (hH.comp measurable_fst).mul (measurable_own_cube ρ _)
  have hlim : ∀ᵐ z : Rows k d×Sphere d ∂(gaussianRows k d).prod ρ,
      Tendsto (fun j : ℕ => cubeAverage ρ z.1 (label z.1 (z.2:Vector d)) (1/(j:ℝ)))
        atTop (𝓝 (exactDensity ρ z.1 (label z.1 (z.2:Vector d)))) := by
    apply (Measure.ae_prod_iff_ae_ae (measurableSet_tendsto_fun
      (fun j : ℕ => measurable_own_cube (k:=k) ρ (1/(j:ℝ))) (measurable_own_exact ρ))).mpr
    filter_upwards [hAC] with C hC
    exact ae_of_ae_map (by unfold label; fun_prop) (hC.ae_le (volume_ae_cube_limit ρ C))
  have hlow : (∫⁻ z : Rows k d×Sphere d, (H z.1 : ℝ≥0∞)*exactDensity ρ z.1 (label z.1 (z.2:Vector d))
        ∂(gaussianRows k d).prod ρ) ≤
      liminf (fun j : ℕ => ∫⁻ z : Rows k d×Sphere d,
      (H z.1 : ℝ≥0∞)*cubeAverage ρ z.1 (label z.1 (z.2:Vector d)) (1/(j:ℝ))
      ∂(gaussianRows k d).prod ρ) atTop := by
    calc
      _ = ∫⁻ z : Rows k d×Sphere d, liminf (fun j : ℕ =>
          (H z.1 : ℝ≥0∞)*cubeAverage ρ z.1 (label z.1 (z.2:Vector d)) (1/(j:ℝ))) atTop
          ∂(gaussianRows k d).prod ρ := by
        apply lintegral_congr_ae
        filter_upwards [hlim] with z hz
        exact (ENNReal.Tendsto.const_mul hz (Or.inr ENNReal.coe_ne_top)).liminf_eq.symm
      _ ≤ _ := lintegral_liminf_le hm
  apply tendsto_of_le_liminf_of_limsup_le hlow
  apply limsup_le_of_le (by isBoundedDefault)
  filter_upwards [eventually_ge_atTop 1] with j hj
  have hmExact : Measurable (fun z : Rows k d×Sphere d =>
      (H z.1 : ℝ≥0∞)*exactDensity ρ z.1 (label z.1 (z.2:Vector d))) :=
    (hH.comp measurable_fst).mul (measurable_own_exact ρ)
  rw [lintegral_prod _ (hm j).aemeasurable,lintegral_prod _ hmExact.aemeasurable]
  apply lintegral_mono_ae
  filter_upwards [hAC] with C hC
  rw [lintegral_const_mul' _ _ ENNReal.coe_ne_top,lintegral_const_mul' _ _ ENNReal.coe_ne_top]
  exact mul_le_mul_right (spherical_cube_energy_le ρ C hC (by positivity)) _



end CurrentProjection

end

end OAI