File size: 19,499 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
import Mathlib
import OAI.Probability.SKGap.Terminal.GibbsExpectation

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
namespace SKGapCutoff.Static

noncomputable def conditionalMean {n : ℕ} (P : Spin n → ℝ) (x : Spin n)
    (i : Fin n) : ℝ :=
  spin x i * ((P x - P (flip x i)) / (P x + P (flip x i)))

noncomputable def lawDirichlet {n : ℕ} (P : Spin n → ℝ) (f : Spin n → ℝ) : ℝ :=
  (∑ i, ∑ x, (P x * P (flip x i) / (P x + P (flip x i))) *
    (f x - f (flip x i)) ^ 2) / 2

noncomputable def lawDensityEnergy {n : ℕ} (P Q : Spin n → ℝ) : ℝ :=
  lawDirichlet P (fun x => Real.sqrt (Q x / P x))

lemma binary_root_energy (p r q s : ℝ) (hp : 0 < p) (hr : 0 < r)
    (hq : 0 ≤ q) (hs : 0 ≤ s) :
    p * r / (p+r) * (Real.sqrt (q/p) - Real.sqrt (s/r))^2 =
      (Real.sqrt r * Real.sqrt q - Real.sqrt p * Real.sqrt s)^2 / (p+r) := by
  rw [Real.sqrt_div hq, Real.sqrt_div hs]
  have hp0 := Real.sqrt_pos.2 hp
  have hr0 := Real.sqrt_pos.2 hr
  have hp2 := Real.sq_sqrt hp.le
  have hr2 := Real.sq_sqrt hr.le
  field_simp
  rw [hp2, hr2]

lemma binary_conditional_mean_energy (p r q s : ℝ) (hp : 0 < p) (hr : 0 < r)
    (hq : 0 ≤ q) (hs : 0 ≤ s) :
    (q+s) * ((q-s)/(q+s) - (p-r)/(p+r))^2 ≤
      4 * (Real.sqrt r * Real.sqrt q - Real.sqrt p * Real.sqrt s)^2 / (p+r) := by
  by_cases hqs : q+s=0
  · have hq0 : q=0 := by linarith
    have hs0 : s=0 := by linarith
    simp [hq0, hs0]
  have hqt : 0 < q+s := lt_of_le_of_ne (add_nonneg hq hs) (Ne.symm hqs)
  have hpt : 0 < p+r := add_pos hp hr
  have hp2 := Real.sq_sqrt hp.le
  have hr2 := Real.sq_sqrt hr.le
  have hq2 := Real.sq_sqrt hq
  have hs2 := Real.sq_sqrt hs
  let a := Real.sqrt r * Real.sqrt q - Real.sqrt p * Real.sqrt s
  let b := Real.sqrt r * Real.sqrt q + Real.sqrt p * Real.sqrt s
  have hab : a*b = r*q-p*s := by
    dsimp [a,b]
    nlinarith only [hp2,hr2,hq2,hs2]
  have hb : b^2 ≤ (p+r)*(q+s) := by
    have he : (p+r)*(q+s)-b^2 =
      (Real.sqrt p * Real.sqrt q - Real.sqrt r * Real.sqrt s)^2 := by
      dsimp [b]
      nlinarith only [hp2,hr2,hq2,hs2]
    nlinarith only [he, sq_nonneg (Real.sqrt p * Real.sqrt q - Real.sqrt r * Real.sqrt s)]
  have hd : (q-s)/(q+s) - (p-r)/(p+r) = 2*(a*b)/((q+s)*(p+r)) := by
    rw [hab]
    field_simp
    ring
  rw [hd]
  change (q+s)*(2*(a*b)/((q+s)*(p+r)))^2 ≤ 4*a^2/(p+r)
  apply (le_div_iff₀ hpt).2
  have hd2 : (q+s)*(2*(a*b)/((q+s)*(p+r)))^2*(p+r) =
      4*a^2*b^2/((q+s)*(p+r)) := by field_simp; ring
  rw [hd2]
  apply (div_le_iff₀ (mul_pos hqt hpt)).2
  nlinarith only [mul_le_mul_of_nonneg_left hb (show 0 ≤ 4*a^2 by positivity)]

@[simp] lemma conditionalMean_flip {n : ℕ} (P : Spin n → ℝ)
    (x : Spin n) (i : Fin n) : conditionalMean P (flip x i) i = conditionalMean P x i := by
  simp only [conditionalMean, flip_flip, spin_flip_self]
  rw [add_comm (P (flip x i)), ← neg_sub (P x) (P (flip x i)), neg_div, neg_mul_neg]

lemma lawDensityEnergy_eq {n : ℕ} (P Q : Spin n → ℝ)
    (hP : ∀ x, 0 < P x) (hQ : ∀ x, 0 ≤ Q x) :
    lawDensityEnergy P Q = (∑ i, ∑ x,
      (Real.sqrt (P (flip x i)) * Real.sqrt (Q x) -
        Real.sqrt (P x) * Real.sqrt (Q (flip x i)))^2 / (P x + P (flip x i))) / 2 := by
  unfold lawDensityEnergy lawDirichlet
  congr 1
  apply Finset.sum_congr rfl
  intro i _
  apply Finset.sum_congr rfl
  intro x _
  exact binary_root_energy _ _ _ _ (hP x) (hP _) (hQ x) (hQ _)

theorem conditional_means_energy {n : ℕ} (P Q : Spin n → ℝ)
    (hP : ∀ x, 0 < P x) (hQ : ∀ x, 0 ≤ Q x) :
    (∑ x, Q x * ∑ i, (conditionalMean Q x i - conditionalMean P x i)^2) ≤
      4 * lawDensityEnergy P Q := by
  rw [lawDensityEnergy_eq P Q hP hQ]
  simp_rw [Finset.mul_sum]
  rw [Finset.sum_comm]
  have hsym (i : Fin n) :
      (∑ x, Q (flip x i) * (conditionalMean Q x i - conditionalMean P x i)^2) =
      ∑ x, Q x * (conditionalMean Q x i - conditionalMean P x i)^2 := by
    simpa only [conditionalMean_flip] using sum_flip i
      (fun x => Q x * (conditionalMean Q x i - conditionalMean P x i)^2)
  have hlocal (i : Fin n) (x : Spin n) :
      (Q x + Q (flip x i)) * (conditionalMean Q x i - conditionalMean P x i)^2 ≤
      4 * (Real.sqrt (P (flip x i)) * Real.sqrt (Q x) -
        Real.sqrt (P x) * Real.sqrt (Q (flip x i)))^2 / (P x + P (flip x i)) := by
    have he : (conditionalMean Q x i - conditionalMean P x i)^2 =
        ((Q x-Q (flip x i))/(Q x+Q (flip x i)) -
          (P x-P (flip x i))/(P x+P (flip x i)))^2 := by
      simp only [conditionalMean, ← mul_sub, mul_pow, spin_sq, one_mul]
    rw [he]
    exact binary_conditional_mean_energy _ _ _ _ (hP x) (hP _) (hQ x) (hQ _)
  have hh (i : Fin n) := Finset.sum_le_sum (fun x (_ : x ∈ Finset.univ) => hlocal i x)
  simp only [add_mul, Finset.sum_add_distrib, hsym] at hh
  have hsum := Finset.sum_le_sum (fun i (_ : i ∈ Finset.univ) => hh i)
  simp only [Finset.sum_add_distrib, mul_div_assoc, ← Finset.mul_sum] at hsum ⊢
  linarith only [hsum]

lemma conditionalMean_mul_mass {n : ℕ} (Q : Spin n → ℝ)
    (hQ : ∀ x, 0 ≤ Q x) (x : Spin n) (i : Fin n) :
    (Q x + Q (flip x i)) * conditionalMean Q x i =
      spin x i * (Q x-Q (flip x i)) := by
  unfold conditionalMean
  by_cases h : Q x + Q (flip x i) = 0
  · have ha : Q x=0 := by have := hQ (flip x i); have := hQ x; linarith
    have hb : Q (flip x i)=0 := by have := hQ x; have := hQ (flip x i); linarith
    simp [ha,hb]
  · field_simp

lemma conditionalMean_abs_le {n : ℕ} (Q : Spin n → ℝ)
    (hQ : ∀ x, 0 ≤ Q x) (x : Spin n) (i : Fin n) :
    |conditionalMean Q x i| ≤ 1 := by
  have hs : |spin x i| = 1 := by cases hx : x i <;> simp [spin,hx]
  simp only [conditionalMean, abs_mul, hs, one_mul, abs_div]
  rw [abs_of_nonneg (add_nonneg (hQ x) (hQ _))]
  apply div_le_one_of_le₀ _ (add_nonneg (hQ x) (hQ _))
  exact abs_sub_le_iff.2 ⟨by linarith [hQ (flip x i)], by linarith [hQ x]⟩

lemma law_coordinate_mean {n : ℕ} (Q : Spin n → ℝ) (hQ : ∀ x, 0 ≤ Q x)
    (i : Fin n) (F : Spin n → ℝ) (hF : ∀ x, F (flip x i)=F x) :
    (∑ x, Q x * spin x i * F x) = ∑ x, Q x * conditionalMean Q x i * F x := by
  have ha := sum_flip i (fun x => Q x * spin x i * F x)
  have hb := sum_flip i (fun x => Q x * conditionalMean Q x i * F x)
  simp only [spin_flip_self, conditionalMean_flip, hF] at ha hb
  have he (x : Spin n) :
      Q x * spin x i * F x + Q (flip x i) * -spin x i * F x =
      Q x * conditionalMean Q x i * F x +
        Q (flip x i) * conditionalMean Q x i * F x := by
    have hm := conditionalMean_mul_mass Q hQ x i
    have := congrArg (fun z => z * F x) hm
    nlinarith only [this]
  have hh := Finset.sum_congr (s₁ := Finset.univ) (s₂ := Finset.univ) rfl
    (fun x _ => he x)
  simp only [Finset.sum_add_distrib, ha, hb] at hh
  linarith only [hh]

lemma law_integration_by_parts {n : ℕ} (Q : Spin n → ℝ) (hQ : ∀ x, 0 ≤ Q x)
    (i : Fin n) (F : Spin n → ℝ) :
    (∑ x, Q x * (spin x i - conditionalMean Q x i) * F x) =
      ∑ x, Q x * (1-spin x i*conditionalMean Q x i) * halfDiff i F x := by
  have hh : ∀ x, (F (flip x i) - spin (flip x i) i * halfDiff i F (flip x i)) =
      F x - spin x i * halfDiff i F x := by
    intro x
    rw [spin_flip_self, halfDiff_flip]
    linarith only [flip_sub i F x]
  have he := law_coordinate_mean Q hQ i
    (fun x => F x-spin x i*halfDiff i F x) hh
  have h1 (x : Spin n) : Q x * spin x i * (F x-spin x i*halfDiff i F x) =
      Q x*spin x i*F x - Q x*halfDiff i F x := by
    have hs := spin_sq x i
    nlinarith only [congrArg (fun z : ℝ => Q x*halfDiff i F x*z) hs]
  simp_rw [h1] at he
  simp only [mul_sub, Finset.sum_sub_distrib] at he
  have h2 (x : Spin n) : Q x * (spin x i - conditionalMean Q x i) * F x =
      Q x * spin x i * F x - Q x * conditionalMean Q x i * F x := by ring
  have h3 (x : Spin n) : Q x * (1-spin x i*conditionalMean Q x i) * halfDiff i F x =
      Q x * halfDiff i F x -
        Q x * conditionalMean Q x i * (spin x i * halfDiff i F x) := by ring
  simp_rw [h2, h3, Finset.sum_sub_distrib]
  linarith only [he]

lemma law_integration_by_parts_variance {n : ℕ} (Q : Spin n → ℝ)
    (hQ : ∀ x, 0 ≤ Q x) (i : Fin n) (F : Spin n → ℝ) :
    (∑ x, Q x * (spin x i-conditionalMean Q x i) * F x) =
      ∑ x, Q x*(1-(conditionalMean Q x i)^2)*halfDiff i F x := by
  rw [law_integration_by_parts Q hQ]
  have hh := law_coordinate_mean Q hQ i
    (fun x => conditionalMean Q x i*halfDiff i F x)
    (by intro x; rw [conditionalMean_flip,halfDiff_flip])
  simp only [mul_sub,sub_mul,mul_one,Finset.sum_sub_distrib,pow_two,← mul_assoc] at hh ⊢
  rw [hh]

lemma gibbs_flipRate_fraction {n : ℕ} (J : Interaction n)
    (hJ : ∀ i j, J i j = J j i) (hdiag : ∀ i, J i i = 0)
    (x : Spin n) (i : Fin n) :
    flipRate J x i = gibbs J (flip x i)/(gibbs J x+gibbs J (flip x i)) := by
  have hf : flipRate J (flip x i) i = 1-flipRate J x i := by
    simp only [flipRate, spin_flip_self, mean_flip J hdiag]
    ring
  have hh := gibbs_detailed_balance J hJ hdiag x i
  rw [hf] at hh
  apply (eq_div_iff (ne_of_gt (add_pos (gibbs_pos J x) (gibbs_pos J _)))).2
  nlinarith only [hh]

lemma conditionalMean_gibbs {n : ℕ} (J : Interaction n)
    (hJ : ∀ i j, J i j = J j i) (hdiag : ∀ i, J i i = 0)
    (x : Spin n) (i : Fin n) : conditionalMean (gibbs J) x i = mean J x i := by
  have hh := gibbs_flipRate_fraction J hJ hdiag x i
  have hs := spin_sq x i
  have hr : (gibbs J x-gibbs J (flip x i))/(gibbs J x+gibbs J (flip x i)) =
      1-2*flipRate J x i := by
    rw [hh]
    field_simp [ne_of_gt (add_pos (gibbs_pos J x) (gibbs_pos J (flip x i)))]
    ring
  rw [conditionalMean, hr, flipRate]
  nlinarith only [congrArg (fun z : ℝ => mean J x i*z) hs]

lemma lawDirichlet_gibbs {n : ℕ} (J : Interaction n)
    (hJ : ∀ i j, J i j = J j i) (hdiag : ∀ i, J i i = 0)
    (f : Spin n → ℝ) : lawDirichlet (gibbs J) f = dirichlet J f f := by
  have he (i : Fin n) :
      (∑ x, gibbs J x * flipRate J x i * (f x - f (flip x i))^2) =
        2 * (∑ x, gibbs J x * siteVariance J x i * halfDiff i f x * halfDiff i f x) := by
    have hh := sum_flip i (fun x => gibbs J x * flipRate J x i * f x * (f x-f (flip x i)))
    simp only [gibbs_detailed_balance J hJ hdiag, flip_flip] at hh
    have hk (x : Spin n) : gibbs J x * flipRate J x i * (f x-f (flip x i))^2 =
        gibbs J x * flipRate J x i * f x * (f x-f (flip x i)) +
        gibbs J x * flipRate J x i * f (flip x i) * (f (flip x i)-f x) := by ring
    simp_rw [hk]
    rw [Finset.sum_add_distrib, hh]
    have hd : (∑ x, gibbs J x * flipRate J x i * f x * (f x-f (flip x i))) =
        ∑ x, gibbs J x * siteVariance J x i * halfDiff i f x * halfDiff i f x := by
      convert gibbs_edge_dirichlet J hJ hdiag i f f using 1
      apply Finset.sum_congr rfl
      intro x _
      ring
    rw [hd]
    ring
  unfold lawDirichlet
  simp_rw [mul_div_assoc, ← gibbs_flipRate_fraction J hJ hdiag]
  simp_rw [he]
  rw [← Finset.mul_sum]
  have hc (z : ℝ) : 2*z/2=z := by ring
  rw [hc]
  unfold dirichlet gibbsExpectation
  simp only [Finset.mul_sum]
  rw [Finset.sum_comm]
  apply Finset.sum_congr rfl
  intro i _
  apply Finset.sum_congr rfl
  intro x _
  ring

theorem gibbs_conditional_means_energy {n : ℕ} (J : Interaction n)
    (hJ : ∀ i j, J i j = J j i) (hdiag : ∀ i, J i i = 0)
    (Q : Spin n → ℝ) (hQ : ∀ x, 0 ≤ Q x) :
    (∑ x, Q x * ∑ i, (conditionalMean Q x i - mean J x i)^2) ≤
      4 * dirichlet J (fun x => Real.sqrt (Q x/gibbs J x))
        (fun x => Real.sqrt (Q x/gibbs J x)) := by
  simpa only [conditionalMean_gibbs J hJ hdiag, lawDensityEnergy,
    lawDirichlet_gibbs J hJ hdiag] using conditional_means_energy (gibbs J) Q (gibbs_pos J) hQ

lemma lawDensityEnergy_nonneg {n : ℕ} (P Q : Spin n → ℝ)
    (hP : ∀ x, 0 < P x) : 0 ≤ lawDensityEnergy P Q := by
  unfold lawDensityEnergy lawDirichlet
  apply div_nonneg _ (by norm_num)
  exact Finset.sum_nonneg (fun i _ => Finset.sum_nonneg (fun x _ =>
    mul_nonneg (div_nonneg (mul_nonneg (hP x).le (hP _).le)
      (add_nonneg (hP x).le (hP _).le)) (sq_nonneg _)))

lemma lawDensityEnergy_le {n : ℕ} (P Q : Spin n → ℝ)
    (hP : ∀ x, 0 < P x) (hQ : ∀ x, 0 ≤ Q x) :
    lawDensityEnergy P Q ≤ (n:ℝ) * ∑ x, Q x := by
  rw [lawDensityEnergy_eq P Q hP hQ]
  have hlocal (i : Fin n) (x : Spin n) :
      (Real.sqrt (P (flip x i))*Real.sqrt (Q x)-
        Real.sqrt (P x)*Real.sqrt (Q (flip x i)))^2/(P x+P (flip x i)) ≤ Q x+Q (flip x i) := by
    apply (div_le_iff₀ (add_pos (hP x) (hP _))).2
    have ha := Real.sq_sqrt (hP x).le
    have hb := Real.sq_sqrt (hP (flip x i)).le
    have hc := Real.sq_sqrt (hQ x)
    have hd := Real.sq_sqrt (hQ (flip x i))
    have he : (Real.sqrt (P (flip x i))*Real.sqrt (Q x))^2 = P (flip x i)*Q x := by
      rw [mul_pow, hb, hc]
    have hf : (Real.sqrt (P x)*Real.sqrt (Q (flip x i)))^2 = P x*Q (flip x i) := by
      rw [mul_pow, ha, hd]
    nlinarith only [he, hf,
      mul_nonneg (hP x).le (hQ x), mul_nonneg (hP (flip x i)).le (hQ (flip x i)),
      mul_nonneg (mul_nonneg (Real.sqrt_nonneg (P (flip x i))) (Real.sqrt_nonneg (Q x)))
        (mul_nonneg (Real.sqrt_nonneg (P x)) (Real.sqrt_nonneg (Q (flip x i))))]
  calc
    _ ≤ (∑ i : Fin n, ∑ x, (Q x+Q (flip x i)))/2 := by
      exact div_le_div_of_nonneg_right
        (Finset.sum_le_sum (fun i _ => Finset.sum_le_sum (fun x _ => hlocal i x)))
          (by norm_num)
    _ = _ := by simp only [Finset.sum_add_distrib, sum_flip, Finset.sum_const,
      Finset.card_univ, Fintype.card_fin, nsmul_eq_mul]; ring

lemma weighted_cauchy_sq {α : Type*} [Fintype α] (w f g : α → ℝ)
    (hw : ∀ a, 0 ≤ w a) :
    (∑ a, w a*f a*g a)^2 ≤ (∑ a, w a*f a^2)*(∑ a, w a*g a^2) := by
  apply Finset.sum_sq_le_sum_mul_sum_of_sq_le_mul
    Finset.univ (fun a _ => mul_nonneg (hw a) (sq_nonneg _))
      (fun a _ => mul_nonneg (hw a) (sq_nonneg _))
  intro a _
  exact le_of_eq (by ring)

theorem weak_residual_base {n : ℕ} (P Q : Spin n → ℝ)
    (hP : ∀ x, 0 < P x) (hQ : ∀ x, 0 ≤ Q x) (hQsum : ∑ x, Q x=1)
    (U : Spin n → Fin n → ℝ) (A B : ℝ) (hB : 0 ≤ B)
    (hA : ∀ x, ∑ i, |halfDiff i (fun y => U y i) x| ≤ A)
    (hU : ∀ x, ∑ i, U x i^2 ≤ B^2) :
    |∑ x, Q x * ∑ i, (spin x i-conditionalMean P x i)*U x i| ≤
      2*A+2*B*Real.sqrt (lawDensityEnergy P Q) := by
  let a := ∑ x, Q x * ∑ i, (1-spin x i*conditionalMean Q x i) * halfDiff i (fun y => U y i) x
  let b := ∑ x, Q x * ∑ i, (conditionalMean Q x i-conditionalMean P x i)*U x i
  have he : (∑ x, Q x * ∑ i, (spin x i-conditionalMean P x i)*U x i) = a+b := by
    have hs (x : Spin n) (i : Fin n) :
        (spin x i-conditionalMean P x i)*U x i =
          (spin x i-conditionalMean Q x i)*U x i+
          (conditionalMean Q x i-conditionalMean P x i)*U x i := by ring
    simp_rw [hs, Finset.sum_add_distrib, mul_add, Finset.sum_add_distrib]
    congr 1
    dsimp [a]
    simp_rw [Finset.mul_sum]
    rw [Finset.sum_comm, Finset.sum_comm (f := fun x i =>
      Q x*((1-spin x i*conditionalMean Q x i)*halfDiff i (fun y => U y i) x))]
    apply Finset.sum_congr rfl
    intro i _
    simpa only [mul_assoc] using law_integration_by_parts Q hQ i (fun y => U y i)
  have ha : |a| ≤ 2*A := by
    have hs (x : Spin n) (i : Fin n) : |1-spin x i*conditionalMean Q x i| ≤ 2 := by
      have hsp : |spin x i|=1 := by cases hx : x i <;> simp [spin,hx]
      calc
        _ ≤ |(1:ℝ)|+|spin x i*conditionalMean Q x i| := abs_sub _ _
        _ ≤ 2 := by rw [abs_one, abs_mul,hsp,one_mul]; linarith [conditionalMean_abs_le Q hQ x i]
    calc
      |a| ≤ ∑ x, |Q x * ∑ i, (1-spin x i*conditionalMean Q x i)*halfDiff i (fun y => U y i) x| :=
        Finset.abs_sum_le_sum_abs ..
      _ = ∑ x, Q x * |∑ i, (1-spin x i*conditionalMean Q x i)*halfDiff i (fun y => U y i) x| := by
        simp only [abs_mul, abs_of_nonneg (hQ _)]
      _ ≤ ∑ x, Q x * (2*A) := by
        apply Finset.sum_le_sum
        intro x _
        apply mul_le_mul_of_nonneg_left _ (hQ x)
        calc
          _ ≤ ∑ i, |(1-spin x i*conditionalMean Q x i)*halfDiff i (fun y => U y i) x| :=
            Finset.abs_sum_le_sum_abs ..
          _ ≤ ∑ i, 2*|halfDiff i (fun y => U y i) x| := by
            apply Finset.sum_le_sum
            intro i _
            rw [abs_mul]
            exact mul_le_mul_of_nonneg_right (hs x i) (abs_nonneg _)
          _ ≤ 2*A := by rw [← Finset.mul_sum]; exact mul_le_mul_of_nonneg_left (hA x) (by norm_num)
      _ = 2*A := by rw [← Finset.sum_mul, hQsum,one_mul]
  have hb : |b| ≤ 2*B*Real.sqrt (lawDensityEnergy P Q) := by
    have hcs := weighted_cauchy_sq (fun a : Spin n × Fin n => Q a.1)
      (fun a => conditionalMean Q a.1 a.2-conditionalMean P a.1 a.2)
      (fun a => U a.1 a.2) (fun a => hQ a.1)
    simp only [Fintype.sum_prod_type, mul_assoc, ← Finset.mul_sum] at hcs
    have hUb : (∑ x, Q x*∑ i, U x i^2) ≤ B^2 := by
      calc
        _ ≤ ∑ x, Q x*B^2 := Finset.sum_le_sum (fun x _ => mul_le_mul_of_nonneg_left (hU x) (hQ x))
        _ = B^2 := by rw [← Finset.sum_mul,hQsum,one_mul]
    have hc := conditional_means_energy P Q hP hQ
    have hd := lawDensityEnergy_nonneg P Q hP
    have hUI : 0 ≤ ∑ x, Q x*∑ i, U x i^2 := by
      exact Finset.sum_nonneg (fun x _ => mul_nonneg (hQ x)
        (Finset.sum_nonneg (fun i _ => sq_nonneg _)))
    have hbound := mul_le_mul hc hUb hUI (show 0 ≤ 4*lawDensityEnergy P Q by positivity)
    have he2 := Real.sq_sqrt hd
    have hpos : 0 ≤ 2*B*Real.sqrt (lawDensityEnergy P Q) := by positivity
    apply (sq_le_sq₀ (abs_nonneg b) hpos).1
    rw [sq_abs]
    have hid : (2*B*Real.sqrt (lawDensityEnergy P Q))^2 = 4*lawDensityEnergy P Q*B^2 := by
      rw [mul_pow,he2]
      ring
    rw [hid]
    exact hcs.trans hbound
  rw [he]
  exact (abs_add_le a b).trans (add_le_add ha hb)

end SKGapCutoff.Static

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

end

end OAI