File size: 22,387 Bytes
7d9f027
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import Mathlib
import OAI.Probability.SKGap.Entropy.ProdFst
import OAI.Probability.SKGap.Entropy.BoundGood
import OAI.Probability.SKGap.Terminal.GibbsEventProbabilityReal

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
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 Filter MeasureTheory ProbabilityTheory
open scoped Topology NNReal ENNReal
open Filter MeasureTheory ProbabilityTheory
open scoped Topology NNReal ENNReal
open MeasureTheory Filter
open scoped Topology NNReal ENNReal
open MeasureTheory Filter ProbabilityTheory
open scoped Topology NNReal ENNReal
open MeasureTheory Filter
open scoped Topology
open MeasureTheory Filter ProbabilityTheory
open scoped Topology NNReal ENNReal
open MeasureTheory Filter ProbabilityTheory
open scoped Topology NNReal ENNReal
open MeasureTheory Filter ProbabilityTheory
open scoped Topology NNReal ENNReal
open MeasureTheory Filter ProbabilityTheory
open scoped Topology NNReal ENNReal
open MeasureTheory Filter ProbabilityTheory ContinuousLinearMap
open scoped Topology NNReal ENNReal
open Filter MeasureTheory ProbabilityTheory
open scoped Topology NNReal ENNReal
open MeasureTheory Filter
open scoped BigOperators Topology
open MeasureTheory Filter
open scoped BigOperators Topology
open MeasureTheory Filter
open scoped BigOperators Topology
open MeasureTheory Filter
open scoped BigOperators Topology
open MeasureTheory Filter
open scoped BigOperators Topology
open Filter Set Metric
open scoped Topology RealInnerProductSpace
open scoped BigOperators
open ContinuousLinearMap
open scoped BigOperators
open ContinuousLinearMap
open scoped Topology Interval
open MeasureTheory
open MeasureTheory
open scoped BigOperators Topology Interval
open MeasureTheory
open scoped BigOperators Topology Interval
open scoped Topology
open MeasureTheory
open scoped BigOperators Topology Interval
open scoped BigOperators Topology
open MeasureTheory
open scoped BigOperators Topology Interval RealInnerProductSpace
open MeasureTheory Filter
open scoped BigOperators Topology Interval
open MeasureTheory Filter
open scoped Topology
open MeasureTheory Filter
open scoped Topology
open MeasureTheory Filter
open scoped Topology
open MeasureTheory ProbabilityTheory Filter
open scoped BigOperators NNReal ENNReal Matrix.Norms.L2Operator
open MeasureTheory ProbabilityTheory Filter Matrix
open scoped BigOperators ENNReal NNReal Topology Matrix.Norms.L2Operator
namespace SKGapCutoff.Regression

noncomputable def plantedFieldArray (β : ℝ) (n : ℕ) (g : Option (Fin n) → ℝ) : Fin n → ℝ :=
  sharedGaussianField (β^2*((n:ℝ)-1)/(n:ℝ))
    (β*Real.sqrt (((n:ℝ)-2)/(n:ℝ))) (β/Real.sqrt (n:ℝ)) g

lemma plantedFieldArray_measurable (β : ℝ) (n : ℕ) : Measurable (plantedFieldArray β n) := by
  unfold plantedFieldArray sharedGaussianField
  fun_prop

lemma planted_field_event_eq (β : ℝ) {n : ℕ} (hn : 2≤n) (x : Spin n)
    (E : Set (Fin n → ℝ)) (hE : MeasurableSet E) :
    singleSpinPlantedLaw β x ((gaugedField x) ⁻¹' E) =
      standardArrayLaw (Option (Fin n)) ((plantedFieldArray β n) ⁻¹' E) := by
  have hh := congrArg (fun μ : Measure (Fin n → ℝ) => μ E) (planted_field_representation β hn x)
  change (singleSpinPlantedLaw β x).map (gaugedField x) E =
    (standardArrayLaw (Option (Fin n))).map (plantedFieldArray β n) E at hh
  rw [Measure.map_apply (continuous_gaugedField x).measurable hE,
    Measure.map_apply (plantedFieldArray_measurable β n) hE] at hh
  exact hh

lemma plantedField_common_convergence (β : ℝ) (hβ : 0<β) :
    ExponentialConvergence (fun n => standardArrayLaw (Option (Fin n)))
      (fun n g => (β/Real.sqrt (n:ℝ))*g none) 0 := by
  intro ε hε
  refine ⟨2,ε^2/(2*β^2),by norm_num,by positivity,?_⟩
  filter_upwards [eventually_gt_atTop 0] with n hn
  have hnR : (0:ℝ)<n := Nat.cast_pos.mpr hn
  have ht := subgaussian_abs_tail
    (gaussian_hasSubgaussianMGF (coordinate_hasLaw (none : Option (Fin n))))
      (ε*Real.sqrt (n:ℝ)/β) (by positivity)
  simp only [Real.dist_eq,sub_zero]
  apply (measure_mono (show {g : Option (Fin n) → ℝ | ε ≤ |β/Real.sqrt (n:ℝ)*g none|} ⊆
    {g | ε*Real.sqrt (n:ℝ)/β ≤ |g none|} from ?_)).trans
  · apply ht.trans_eq
    rw [ENNReal.ofReal_mul (by norm_num : (0:ℝ)≤2)]
    norm_num only [ENNReal.ofReal_ofNat,NNReal.coe_one,mul_one]
    congr 2
    rw [div_pow,mul_pow,Real.sq_sqrt hnR.le]
    ring_nf
  · intro g hg
    change ε ≤ |β/Real.sqrt (n:ℝ)*g none| at hg
    rw [abs_mul,abs_of_pos (by positivity : 0<β/Real.sqrt (n:ℝ))] at hg
    apply (div_le_iff₀ hβ).mpr
    have hh := (le_div_iff₀ (Real.sqrt_pos.mpr hnR)).mp
      (show ε ≤ β*|g none|/Real.sqrt (n:ℝ) by simpa only [div_mul_eq_mul_div] using hg)
    nlinarith

lemma plantedField_mean_bound (β : ℝ) (n : ℕ) :
    |β^2*((n:ℝ)-1)/(n:ℝ)| ≤ β^2 := by
  by_cases hn : n=0
  · simp [hn, sq_nonneg]
  have hn1 : (1:ℝ)≤n := by exact_mod_cast (Nat.one_le_iff_ne_zero.mpr hn)
  rw [abs_of_nonneg (by positivity)]
  apply (div_le_iff₀ (by positivity : (0:ℝ)<n)).mpr
  nlinarith [sq_nonneg β]

lemma plantedField_scale_bound (β : ℝ) (n : ℕ) :
    |β*Real.sqrt (((n:ℝ)-2)/(n:ℝ))| ≤ |β| := by
  have hs : Real.sqrt (((n:ℝ)-2)/(n:ℝ)) ≤ 1 := by
    apply Real.sqrt_le_one.mpr
    by_cases hn : n=0
    · simp [hn]
    · apply (div_le_iff₀ (Nat.cast_pos.mpr (Nat.pos_of_ne_zero hn))).mpr
      linarith
  rw [abs_mul,abs_of_nonneg (Real.sqrt_nonneg _)]
  nlinarith [abs_nonneg β]

lemma plantedFieldArray_squareTails (β : ℝ) (hβ : 0<β) :
    ExponentialSquareTails (fun n => standardArrayLaw (Option (Fin n)))
      (plantedFieldArray β) := by
  let ρ := fun n => standardArrayLaw (Option (Fin n))
  have hi : ExponentialSquareTails ρ (fun _ g i => g (some i)) := by
    exact iid_exponential_squareTails ρ _ (fun _ _ => measurable_pi_apply _)
      (fun _ => coordinates_independent.precomp (Option.some_injective _))
      (gaussianReal 0 1) (fun _ _ => coordinate_hasLaw _) gaussian_integrable_exp_quarter_sq
  have ha := hi.bounded_mul
    (fun n _ _ => β*Real.sqrt (((n:ℝ)-2)/(n:ℝ))) |β| (abs_nonneg β)
    (fun n _ _ => plantedField_scale_bound β n)
  have hm := exponentialSquareTails_of_bounded ρ
    (fun n _ _ => β^2*((n:ℝ)-1)/(n:ℝ)) (β^2) (fun n _ _ => plantedField_mean_bound β n)
  have hb := hm.add ha
  apply hb.l2_stability
  have hz := (plantedField_common_convergence β hβ).continuous_map
    (f := fun x : ℝ => x^2) (by fun_prop)
  simp only [zero_pow (by norm_num : (2:ℕ)≠0)] at hz
  intro ε hε
  apply (hz ε hε).mono
  filter_upwards [eventually_gt_atTop 0] with n hn
  intro g hg
  have he : (∑ i : Fin n,
      (β^2*((n:ℝ)-1)/(n:ℝ)+β*Real.sqrt (((n:ℝ)-2)/(n:ℝ))*g (some i) -
        plantedFieldArray β n g i)^2)/(n:ℝ) = ((β/Real.sqrt (n:ℝ))*g none)^2 := by
    simp only [plantedFieldArray,sharedGaussianField,sub_add_cancel_left,
      neg_sq,Finset.sum_const,Finset.card_univ,Fintype.card_fin,nsmul_eq_mul]
    field_simp
  change ε ≤ dist _ 0 at hg ⊢
  rwa [he] at hg

noncomputable def plantedFieldLimitLaw (β : ℝ) : Measure ℝ :=
  (gaussianReal 0 1).map (fun z => β^2+β*z)

instance plantedFieldLimitLaw_probability (β : ℝ) : IsProbabilityMeasure (plantedFieldLimitLaw β) := by
  unfold plantedFieldLimitLaw
  infer_instance

lemma plantedFieldArray_empirical (β : ℝ) (hβ : 0<β) :
    ExponentialEmpiricalConcentration (fun n => standardArrayLaw (Option (Fin n)))
      (plantedFieldArray β) (plantedFieldLimitLaw β) := by
  intro f K hf B hB ε hε
  have hf' : LipschitzWith (K+1) f := hf.weaken (by simp)
  obtain ⟨c,hc,ht⟩ := planted_field_average_exponential β hβ f (by positivity) hf'
    (|B|+1) (by positivity) (fun z => (hB z).trans (by linarith [le_abs_self B])) ε hε
  refine ⟨1,c,by norm_num,hc,?_⟩
  filter_upwards [ht,eventually_ge_atTop 2] with n hn hn2
  have hm : (∫ z, f z ∂plantedFieldLimitLaw β) = ∫ z, f (β^2+β*z) ∂gaussianReal 0 1 := by
    exact integral_map (by fun_prop) hf.continuous.aestronglyMeasurable
  rw [hm]
  have hh := hn (fun _ => false)
  have hE : MeasurableSet {v : Fin n → ℝ | ε ≤ |(∑ i, f (v i))/(n:ℝ) -
      ∫ z, f (β^2+β*z) ∂gaussianReal 0 1|} := by
    apply measurableSet_le measurable_const
    exact (((Finset.measurable_sum _ (fun i _ => hf.continuous.measurable.comp
      (measurable_pi_apply i))).div_const _).sub_const _).abs
  rw [show singleSpinPlantedLaw β (fun _ : Fin n => false) {g | ε ≤
      |(∑ i, f (gaugedField (fun _ => false) g i))/(n:ℝ) -
      ∫ z, f (β^2+β*z) ∂gaussianReal 0 1|} = _ from
        planted_field_event_eq β hn2 (fun _ => false) _ hE] at hh
  simpa only [one_mul,Set.preimage_ofPred_eq] using hh

lemma plantedFieldLimitLaw_secondMoment (β : ℝ) :
    Integrable (fun z : ℝ => z^2) (plantedFieldLimitLaw β) ∧
      (∫ z, z^2 ∂plantedFieldLimitLaw β)=β^2+β^4 := by
  have hl : HasLaw (fun z : ℝ => β^2+β*z) (plantedFieldLimitLaw β) (gaussianReal 0 1) :=
    ⟨by fun_prop,rfl⟩
  have hi2 : MemLp (id : ℝ → ℝ) 2 (plantedFieldLimitLaw β) := by
    apply (memLp_map_measure_iff (by fun_prop) (by fun_prop)).mpr
    exact (memLp_const (β^2)).add
      (((IsGaussian.hasGaussianLaw_id (μ := gaussianReal 0 1)).memLp_two).const_mul β)
  have hi := hi2.integrable_sq
  refine ⟨hi,?_⟩
  have hmean : (∫ z, z ∂plantedFieldLimitLaw β)=β^2 := by
    rw [← hl.integral_eq]
    have hm : Integrable (fun z : ℝ => β*z) (gaussianReal 0 1) :=
      (IsGaussian.hasGaussianLaw_id (μ := gaussianReal 0 1)).integrable.const_mul β
    rw [integral_add (integrable_const _) hm,integral_const,integral_const_mul]
    simp [integral_id_gaussianReal]
  have hv : Var[id; plantedFieldLimitLaw β]=β^2 := by
    rw [← hl.variance_eq,variance_const_add (by fun_prop),variance_const_mul]
    simp
  have hh := variance_eq_sub (X := id) hi2
  simp only [Pi.pow_apply,id_eq,hmean] at hh
  rw [hv] at hh
  nlinarith

lemma plantedFieldArray_secondMoment (β : ℝ) (hβ : 0<β) :
    ExponentialConvergence (fun n => standardArrayLaw (Option (Fin n)))
      (fun n g => (∑ i, plantedFieldArray β n g i^2)/(n:ℝ)) (β^2+β^4) := by
  have hh := (plantedFieldArray_empirical β hβ).secondMoment
    (plantedFieldArray_squareTails β hβ) (plantedFieldLimitLaw_secondMoment β).1
  rwa [(plantedFieldLimitLaw_secondMoment β).2] at hh

lemma ExponentiallyRare.without_prefactor {H : ℕ → Type*} [∀ n, MeasurableSpace (H n)]
    {ρ : ∀ n, Measure (H n)} {A : ∀ n, Set (H n)} (hA : ExponentiallyRare ρ A) :
    ∃ c : ℝ, 0<c ∧ ∀ᶠ n in atTop, ρ n (A n) ≤ ENNReal.ofReal (Real.exp (-c*n)) := by
  obtain ⟨C,c,hC,hc,he⟩ := hA
  refine ⟨c/2,by positivity,?_⟩
  have hnc := (tendsto_natCast_atTop_atTop : Tendsto (fun n : ℕ => (n:ℝ)) atTop atTop)
  filter_upwards [he,hnc.eventually (eventually_ge_atTop (2*Real.log C/c))] with n hn hsize
  apply hn.trans
  apply ENNReal.ofReal_le_ofReal
  rw [show C*Real.exp (-c*n)=Real.exp (Real.log C-c*n) by
    rw [Real.exp_sub,Real.exp_log hC,neg_mul,Real.exp_neg,div_eq_mul_inv] ]
  apply Real.exp_le_exp.mpr
  have hh := (div_le_iff₀ hc).mp hsize
  nlinarith

noncomputable def spinFieldSquareAverage {n : ℕ} (x : Spin n)
    (g : GaussianCoordinates n) : ℝ := (∑ i, gaugedField x g i^2)/(n:ℝ)

lemma continuous_spinFieldSquareAverage {n : ℕ} (x : Spin n) :
    Continuous (spinFieldSquareAverage x) := by
  change Continuous (fun g => (∑ i, gaugedField x g i^2)/(n:ℝ))
  exact (continuous_finsetSum _ (fun i _ =>
    ((continuous_apply i).comp (continuous_gaugedField x)).pow 2)).div_const _

lemma spinFieldSquareAverage_planted_exponential (β : ℝ) (hβ : 0<β)
    (ε : ℝ) (hε : 0<ε) : ∃ c : ℝ, 0<c ∧ ∀ᶠ n : ℕ in atTop,
      ∀ x : Spin n, singleSpinPlantedLaw β x {g | ε ≤ |spinFieldSquareAverage x g-(β^2+β^4)|} ≤
        ENNReal.ofReal (Real.exp (-c*n)) := by
  obtain ⟨c,hc,ht⟩ := ((plantedFieldArray_secondMoment β hβ) ε hε).without_prefactor
  refine ⟨c,hc,?_⟩
  filter_upwards [ht,eventually_ge_atTop 2] with n hn hn2
  intro x
  have hE : MeasurableSet {v : Fin n → ℝ | ε ≤ |(∑ i, v i^2)/(n:ℝ)-(β^2+β^4)|} := by
    apply measurableSet_le measurable_const
    exact (((Finset.measurable_sum _ (fun i _ => (measurable_pi_apply i).pow_const 2)).div_const _).sub_const _).abs
  have he := planted_field_event_eq β hn2 x _ hE
  change singleSpinPlantedLaw β x {g | ε ≤ |spinFieldSquareAverage x g-(β^2+β^4)|} = _ at he
  rw [he]
  simpa only [Real.dist_eq,Set.preimage_ofPred_eq] using hn

theorem quenched_spinFieldSquareAverage_exponential (β : ℝ) (hβ : 0<β) (hβ1 : β<1)
    (ε : ℝ) (hε : 0<ε) : ∃ c : ℝ, 0<c ∧ Tendsto (fun n => disorderLaw β n {g |
      ENNReal.ofReal (Real.exp (-c*n)) ≤ gibbsEventProbability
        (fun x : Spin n => {h | ε ≤ |spinFieldSquareAverage x h-(β^2+β^4)|}) g})
      atTop (nhds 0) := by
  obtain ⟨c,hc,hs⟩ := spinFieldSquareAverage_planted_exponential β hβ ε hε
  refine ⟨c/2,by positivity,?_⟩
  apply quenched_gibbs_events_exponential β (by nlinarith) c hc _ _ hs
  intro n x
  exact measurableSet_le measurable_const ((continuous_spinFieldSquareAverage x).measurable.sub_const _).abs

lemma spinFieldSquareAverage_abs_le_opNorm_sq {n : ℕ} (hn : 0<n) (x : Spin n)
    (g : GaussianCoordinates n) : |spinFieldSquareAverage x g| ≤
      ‖Matrix.toEuclideanCLM (n := Fin n) (𝕜 := ℝ) (sampledInteraction g)‖^2 := by
  have hh := matrix_mulVec_sq_bound (sampledInteraction g) (spin x) le_rfl
  have he : (∑ i, gaugedField x g i^2) = ∑ i, (sampledInteraction g *ᵥ spin x) i^2 := by
    simp only [gaugedField,mul_pow,spin_sq,one_mul,field,Matrix.mulVec,dotProduct]
  have hsum : (∑ i : Fin n, spin x i^2)=(n:ℝ) := by simp [spin_sq]
  rw [hsum] at hh
  rw [spinFieldSquareAverage,abs_of_nonneg (by positivity),he]
  exact (div_le_iff₀ (Nat.cast_pos.mpr hn)).mpr hh

noncomputable def gibbsSpinFieldSquareAverage {n : ℕ} (g : GaussianCoordinates n) : ℝ :=
  gibbsExpectation (sampledInteraction g) (fun x => spinFieldSquareAverage x g)

theorem gibbsSpinFieldSquareAverage_limit (β : ℝ) (hβ : 0<β) (hβ1 : β<1)
    (ε : ℝ) (hε : 0<ε) :
    Tendsto (fun n => disorderLaw β n {g | ε ≤ |gibbsSpinFieldSquareAverage g-(β^2+β^4)|})
      atTop (nhds 0) := by
  obtain ⟨c,hc,hevent⟩ := quenched_spinFieldSquareAverage_exponential β hβ hβ1 (ε/2) (by positivity)
  let C := 2*(β^2+10)
  let K := C^2+(β^2+β^4)
  have hK : 0 ≤ K := by dsimp [K,C]; positivity
  have hnorm := RandomMatrix.sampled_opNorm_tendsto β
  have ht : Tendsto (fun n : ℕ => K*Real.exp (-c*n)) atTop (nhds 0) := by
    have hh := Real.tendsto_exp_neg_atTop_nhds_zero.comp
      ((tendsto_natCast_atTop_atTop : Tendsto (fun n : ℕ => (n:ℝ)) atTop atTop).const_mul_atTop hc)
    simpa only [Function.comp_apply,neg_mul,mul_zero] using hh.const_mul K
  have hsum := hnorm.add hevent
  simp only [add_zero] at hsum
  apply tendsto_of_tendsto_of_tendsto_of_le_of_le' tendsto_const_nhds hsum
    (Eventually.of_forall (fun _ => bot_le))
  filter_upwards [eventually_gt_atTop 0,ht.eventually (gt_mem_nhds (by positivity : (0:ℝ)<ε/2))]
    with n hn hsmall
  apply (measure_mono (show {g | ε ≤ |gibbsSpinFieldSquareAverage g-(β^2+β^4)|} ⊆
      {g | C < ‖Matrix.toEuclideanCLM (n := Fin n) (𝕜 := ℝ) (sampledInteraction g)‖} ∪
      {g | ENNReal.ofReal (Real.exp (-c*n)) ≤
        gibbsEventProbability (fun x : Spin n => {h | ε/2 ≤ |spinFieldSquareAverage x h-(β^2+β^4)|}) g} from ?_)).trans
      (measure_union_le _ _)
  intro g hg
  by_cases ha : C < ‖Matrix.toEuclideanCLM (n := Fin n) (𝕜 := ℝ) (sampledInteraction g)‖
  · exact Or.inl ha
  by_cases hb : ENNReal.ofReal (Real.exp (-c*n)) ≤
      gibbsEventProbability (fun x : Spin n => {h | ε/2 ≤ |spinFieldSquareAverage x h-(β^2+β^4)|}) g
  · exact Or.inr hb
  exfalso
  have hp : (gibbsEventProbability (fun x : Spin n => {h | ε/2 ≤ |spinFieldSquareAverage x h-(β^2+β^4)|}) g).toReal
      ≤ Real.exp (-c*n) := by
    have hh := ENNReal.toReal_mono (show ENNReal.ofReal (Real.exp (-c*n))≠⊤ from ENNReal.ofReal_ne_top)
      (lt_of_not_ge hb).le
    simpa only [ENNReal.toReal_ofReal (Real.exp_pos _).le] using hh
  have hdev := gibbsExpectation_deviation_bound g
    (fun x h => spinFieldSquareAverage x h) ((β^2+β^4)) (ε/2) K (by positivity) (fun x => by
      calc
        _ ≤ |spinFieldSquareAverage x g|+|(β^2+β^4)| := by simpa only [sub_zero,zero_sub,abs_neg] using abs_sub_le (spinFieldSquareAverage x g) (0:ℝ) ((β^2+β^4))
        _ ≤ C^2+(β^2+β^4) := by
          rw [abs_of_nonneg (by positivity : 0≤β^2+β^4)]
          exact add_le_add_left ((spinFieldSquareAverage_abs_le_opNorm_sq hn x g).trans
            (pow_le_pow_left₀ (norm_nonneg _) (le_of_not_gt ha) 2)) _)
  change ε ≤ |gibbsExpectation (sampledInteraction g) (fun x => spinFieldSquareAverage x g)-(β^2+β^4)| at hg
  have he := mul_le_mul_of_nonneg_left hp hK
  linarith

end SKGapCutoff.Regression

open MeasureTheory ProbabilityTheory Filter Matrix
open scoped BigOperators ENNReal Matrix.Norms.L2Operator

end

end OAI