File size: 13,054 Bytes
ea76aac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import Mathlib
import OAI.Computability.MinUncut.Estimates.F2
import OAI.Computability.MinUncut.Analysis.Gradient

namespace OAI

noncomputable section
open scoped BigOperators
open MeasureTheory ProbabilityTheory Filter
open scoped Topology NNReal
open scoped BigOperators
open MeasureTheory ProbabilityTheory Polynomial Filter
open scoped BigOperators Topology
namespace MinUncut.GaussianHermite
variable {ι : Type*} [Fintype ι] [DecidableEq ι]

lemma noise_derivative (ρ s : ℝ) (hs : s ≠ 0) {Q : (ι → ℝ) → ℝ}
    (hQ : Measurable Q) (hb : ∀ x, |Q x| ≤ 1) (x : ι → ℝ) (i : ι) :
    HasDerivAt (fun t => noise ρ s Q (Function.update x i t)) (gradient ρ s Q i x) (x i) := by
  have hd := GaussianSmoothing.product_convolution_derivative hQ hb hs (ρ • x) i
  have hm : HasDerivAt (fun t : ℝ => ρ*t) ρ (x i) := by
    simpa only [mul_one, id_eq] using (hasDerivAt_id (x i)).const_mul ρ
  have hc := hd.comp (x i) hm
  convert! hc using 1
  · funext t
    unfold noise
    congr 1
    funext g
    congr 2
    ext j
    by_cases hj : j = i
    · subst j; simp; rfl
    · simp [Function.update_of_ne hj]
  · dsimp [gradient, score]
    ring

def convolution (σ : ℝ) (Q : (ι → ℝ) → ℝ) (x : ι → ℝ) : ℝ :=
  ∫ g, Q (x + σ • g) ∂γpi ι

def convolutionGradient (σ : ℝ) (Q : (ι → ℝ) → ℝ) (i : ι) (x : ι → ℝ) : ℝ :=
  σ⁻¹ * ∫ g, g i * Q (x + σ • g) ∂γpi ι

lemma convolution_derivative {σ : ℝ} (hσ : σ ≠ 0) {Q : (ι → ℝ) → ℝ}
    (hQ : Measurable Q) (hb : ∀ x, |Q x| ≤ 1) (x : ι → ℝ) (i : ι) :
    HasDerivAt (fun t => convolution σ Q (Function.update x i t))
      (convolutionGradient σ Q i x) (x i) :=
  GaussianSmoothing.product_convolution_derivative hQ hb hσ x i

omit [DecidableEq ι] in
lemma convolution_as_noise {ρ σ : ℝ} (hρ : ρ ≠ 0) (Q : (ι → ℝ) → ℝ) :
    convolution σ Q = noise ρ (σ*ρ) (fun v => Q (ρ⁻¹ • v)) := by
  funext x
  unfold convolution noise
  congr 1
  funext g
  congr 1
  ext i
  dsimp
  field_simp

omit [DecidableEq ι] in
lemma convolutionGradient_as_noise {ρ σ : ℝ} (hρ : ρ ≠ 0) (hσ : σ ≠ 0)
    (Q : (ι → ℝ) → ℝ) (i : ι) :
    convolutionGradient σ Q i = gradient ρ (σ*ρ) (fun v => Q (ρ⁻¹ • v)) i := by
  funext x
  unfold convolutionGradient gradient score
  have hc : ρ/(σ*ρ) = σ⁻¹ := by field_simp
  rw [hc]
  congr 1
  apply integral_congr_ae
  filter_upwards [] with g
  congr 2
  ext j
  dsimp
  field_simp

lemma convolution_poincare {ρ σ : ℝ} (hρ : ρ ≠ 0) (hσ : σ ≠ 0)
    (h : ρ^2+(σ*ρ)^2=1) {Q : (ι → ℝ) → ℝ} (hQ : Measurable Q)
    (hb : ∀ x, |Q x| ≤ 1) (hmean : (∫ x, convolution σ Q x ∂γpi ι) = 0) :
    (∫ x, (convolution σ Q x)^2 ∂γpi ι) ≤
      ∑ i, ∫ x, (convolutionGradient σ Q i x)^2 ∂γpi ι := by
  simp_rw [convolution_as_noise hρ, convolutionGradient_as_noise hρ hσ] at *
  exact poincare ρ (σ*ρ) h (mul_ne_zero hσ hρ) (hQ.comp (by fun_prop))
    (fun x => hb _) hmean

lemma convolution_gradient_tail {ρ σ ε : ℝ} (hρ : ρ ≠ 0) (hσ : σ ≠ 0)
    (h : ρ^2+(σ*ρ)^2=1) (hε : 0 ≤ ε) (d : ℕ)
    (hd : ∀ k : ℕ, d+2 ≤ k → (k:ℝ)*ρ^(2*k) ≤ ε)
    {Q : (ι → ℝ) → ℝ} (hQ : Measurable Q) (hb : ∀ x, |Q x| ≤ 1) :
    (∑ i, ∫ x, (convolutionGradient σ Q i x -
      project d (convolutionGradient σ Q i) x)^2 ∂γpi ι) ≤ ε := by
  simp_rw [convolutionGradient_as_noise hρ hσ]
  exact gradient_tail_le ρ (σ*ρ) h (mul_ne_zero hσ hρ) (hQ.comp (by fun_prop))
    (fun x => hb _) d hε hd

end MinUncut.GaussianHermite

namespace MinUncut.RowNoise
open BinaryFourier
local instance convolutionDualFintype {U : Type*} [AddCommGroup U] [Module F₂ U] [Fintype U] :
    Fintype (Module.Dual F₂ U) := BinaryFourier.dualFintype
variable {R W : Type*} [Fintype R] [DecidableEq R] [Fintype W] [DecidableEq W]
  [AddCommGroup W] [Module F₂ W]

lemma expect_prod {A : R → Type*} [∀ r, Fintype (A r)] (f : ∀ r, A r → ℝ) :
    (𝔼 x : ∀ r, A r, ∏ r, f r (x r)) = ∏ r, 𝔼 a, f r a := by
  simp only [Fintype.expect_eq_sum_div_card, Fintype.card_pi]
  rw [← Fintype.prod_sum, Nat.cast_prod, Finset.prod_div_distrib]

def rowDensity (a : ℝ) (b c : W) : ℝ :=
  (1-a)*(if c=b then (Fintype.card W : ℝ) else 0) + a

omit [Module F₂ W] in
lemma rowDensity_mean (a : ℝ) (b : W) : (𝔼 c, rowDensity a b c) = 1 := by
  rw [Fintype.expect_eq_sum_div_card]
  simp only [rowDensity, Finset.sum_add_distrib, ← Finset.mul_sum,
    Finset.sum_ite_eq', Finset.mem_univ, ite_true, Finset.sum_const,
    Finset.card_univ, nsmul_eq_mul]
  have hn : (Fintype.card W : ℝ) ≠ 0 := Nat.cast_ne_zero.mpr Fintype.card_ne_zero
  field_simp
  ring

omit [AddCommGroup W] [Module F₂ W] in
lemma rowDensity_symmetric (a : ℝ) (b c : W) : rowDensity a b c = rowDensity a c b := by
  simp [rowDensity, eq_comm]

omit [Module F₂ W] in
lemma rowDensity_nonneg {a : ℝ} (ha : 0 ≤ a) (ha1 : a ≤ 1) (b c : W) :
    0 ≤ rowDensity a b c := by
  unfold rowDensity
  split_ifs <;> positivity

def density (a : ℝ) (B C : R → W) : ℝ := ∏ r, rowDensity a (B r) (C r)

omit [Module F₂ W] in
lemma density_mean (a : ℝ) (B : R → W) : (𝔼 C, density a B C) = 1 := by
  simp only [density, expect_prod, rowDensity_mean, Finset.prod_const_one]

omit [DecidableEq R] [AddCommGroup W] [Module F₂ W] in
lemma density_symmetric (a : ℝ) (B C : R → W) : density a B C = density a C B := by
  simp only [density, rowDensity_symmetric a]

omit [DecidableEq R] [Module F₂ W] in
lemma density_nonneg {a : ℝ} (ha : 0 ≤ a) (ha1 : a ≤ 1) (B C : R → W) :
    0 ≤ density a B C := Finset.prod_nonneg (fun _ _ => rowDensity_nonneg ha ha1 _ _)

def frequency (α : Module.Dual F₂ (R → W)) (r : R) : Module.Dual F₂ W :=
  α.comp (LinearMap.single F₂ (fun _ : R => W) r)

omit [Fintype W] [DecidableEq W] in
lemma dual_apply (α : Module.Dual F₂ (R → W)) (B : R → W) :
    α B = ∑ r, frequency α r (B r) := by
  change α B = ∑ r, α (Pi.single r (B r))
  rw [← map_sum]
  congr 1
  ext r
  simp

lemma sign_sum {S : Type*} (s : Finset S) (f : S → F₂) :
    sign (∑ i ∈ s, f i) = ∏ i ∈ s, sign (f i) := by
  classical
  induction s using Finset.induction_on with
  | empty => simp
  | insert i s hi ih => simp [hi, sign_add, ih]

omit [Fintype W] [DecidableEq W] in
lemma character_product (α : Module.Dual F₂ (R → W)) (B : R → W) :
    character α B = ∏ r, character (frequency α r) (B r) := by
  simp only [character, dual_apply, sign_sum]

def mask (α : Module.Dual F₂ (R → W)) : Finset R :=
  Finset.univ.filter (fun r => frequency α r ≠ 0)

def degree (α : Module.Dual F₂ (R → W)) : ℕ := (mask α).card

lemma row_character_eigen (a : ℝ) (b : W) (α : Module.Dual F₂ W) :
    (𝔼 c, rowDensity a b c * character α c) =
      (if α=0 then 1 else 1-a)*character α b := by
  unfold rowDensity
  simp only [add_mul, Finset.expect_add_distrib, mul_assoc, ← Finset.mul_expect]
  have hh : (𝔼 c, (if c=b then (Fintype.card W : ℝ) else 0) * character α c) =
      character α b := by
    rw [Fintype.expect_eq_sum_div_card]
    simp only [ite_mul, zero_mul, Finset.sum_ite_eq', Finset.mem_univ, ite_true]
    exact mul_div_cancel_left₀ _ (Nat.cast_ne_zero.mpr Fintype.card_ne_zero)
  rw [hh, character_mean]
  split_ifs with hα
  · subst α; simp
  · ring

def noise (a : ℝ) (f : (R → W) → ℝ) (B : R → W) : ℝ :=
  𝔼 C, density a B C * f C

lemma noise_character (a : ℝ) (α : Module.Dual F₂ (R → W)) (B : R → W) :
    noise a (character α) B = (1-a)^degree α*character α B := by
  simp only [noise, density, character_product]
  simp_rw [← Finset.prod_mul_distrib]
  rw [expect_prod (fun r c => rowDensity a (B r) c * character (frequency α r) c)]
  simp only [row_character_eigen, Finset.prod_mul_distrib]
  congr 1
  simp only [Finset.prod_ite, Finset.prod_const_one, one_mul, Finset.prod_const]
  rfl

lemma noise_expansion (a : ℝ) (f : (R → W) → ℝ) (B : R → W) :
    noise a f B = ∑ α, coefficient f α * (1-a)^degree α * character α B := by
  unfold noise
  simp_rw [← inversion f, Finset.mul_sum, Finset.expect_sum_comm]
  apply Finset.sum_congr rfl
  intro α _
  have he : (𝔼 C, density a B C * (coefficient f α * character α C)) =
      coefficient f α * noise a (character α) B := by
    unfold noise
    rw [Finset.mul_expect]
    apply Finset.expect_congr rfl
    intro C _
    ring
  rw [he, noise_character]
  ring

lemma correlation (a : ℝ) (f : (R → W) → ℝ) :
    (𝔼 B, f B * noise a f B) = ∑ α, (1-a)^degree α*(coefficient f α)^2 := by
  simp_rw [noise_expansion, Finset.mul_sum, Finset.expect_sum_comm]
  apply Finset.sum_congr rfl
  intro α _
  calc
    _ = ((1-a)^degree α*coefficient f α) * (𝔼 B, f B * character α B) := by
      rw [Finset.mul_expect]
      apply Finset.expect_congr rfl
      intro B _
      ring
    _ = _ := by rw [← coefficient]; ring

omit [Module F₂ W] in
lemma stationary (a : ℝ) (f : (R → W) → ℝ) : (𝔼 B, noise a f B) = 𝔼 B, f B := by
  unfold noise
  rw [Finset.expect_comm]
  apply Finset.expect_congr rfl
  intro C _
  simp_rw [density_symmetric a _ C, ← Finset.expect_mul, density_mean, one_mul]

lemma dirichlet (a : ℝ) (f : (R → W) → ℝ) :
    (𝔼 B, 𝔼 C, density a B C * (f B - f C)^2) =
      2 * ∑ α, (1-(1-a)^degree α)*(coefficient f α)^2 := by
  have he (B C : R → W) : density a B C*(f B-f C)^2 =
      (f B)^2*density a B C + density a B C*(f C)^2 - 2*f B*(density a B C*f C) := by ring
  simp_rw [he, Finset.expect_sub_distrib, Finset.expect_add_distrib,
    ← Finset.mul_expect, density_mean, mul_one]
  have ht : (𝔼 B, 2*f B*(𝔼 C, density a B C*f C)) =
      2*(𝔼 B, f B*noise a f B) := by
    simp only [noise, Finset.mul_expect, mul_assoc]
  rw [ht]
  change (𝔼 B, f B^2)+(𝔼 B, noise a (fun C => (f C)^2) B) -
    2*(𝔼 B, f B*noise a f B) = _
  rw [stationary, correlation, ← parseval]
  simp_rw [sub_mul, one_mul, Finset.sum_sub_distrib]
  ring

def project (D : ℕ) (f : (R → W) → ℝ) (B : R → W) : ℝ :=
  ∑ α ∈ Finset.univ.filter (fun α => degree α ≤ D), coefficient f α * character α B

omit [DecidableEq W] in
lemma coefficient_project (D : ℕ) (f : (R → W) → ℝ) (β : Module.Dual F₂ (R → W)) :
    coefficient (project D f) β = if degree β ≤ D then coefficient f β else 0 := by
  unfold coefficient project
  simp_rw [Finset.sum_mul, Finset.expect_sum_comm, mul_assoc, ← Finset.mul_expect,
    character_orthogonality]
  simp [Finset.mem_filter, coefficient]

omit [DecidableEq W] in
lemma coefficient_residual (D : ℕ) (f : (R → W) → ℝ) (β : Module.Dual F₂ (R → W)) :
    coefficient (fun B => f B-project D f B) β =
      if degree β ≤ D then 0 else coefficient f β := by
  have he : coefficient (fun B => f B-project D f B) β =
      coefficient f β - coefficient (project D f) β := by
    simp only [coefficient, sub_mul, Finset.expect_sub_distrib]
  rw [he, coefficient_project]
  split_ifs <;> ring

omit [DecidableEq W] in
lemma residual_energy (D : ℕ) (f : (R → W) → ℝ) :
    (𝔼 B, (f B-project D f B)^2) =
      ∑ α, if D < degree α then (coefficient f α)^2 else 0 := by
  rw [← parseval]
  apply Finset.sum_congr rfl
  intro α _
  rw [coefficient_residual]
  split_ifs <;> simp_all
  omega

lemma geometric_bernoulli {a : ℝ} (_ha : 0 ≤ a) (ha1 : a ≤ 1) (k : ℕ) :
    (1-a)^k*(1+(k:ℝ)*a) ≤ 1 := by
  induction k with
  | zero => simp
  | succ k ih =>
    have hp : 0 ≤ (1-a)^k := pow_nonneg (by linarith) _
    have hh : (1-a)*(1+((k:ℝ)+1)*a) ≤ 1+(k:ℝ)*a := by
      nlinarith [mul_nonneg (by positivity : (0:ℝ) ≤ (k:ℝ)+1) (sq_nonneg a)]
    calc
      _ = (1-a)^k*((1-a)*(1+((k:ℝ)+1)*a)) := by rw [pow_succ]; push_cast; ring
      _ ≤ (1-a)^k*(1+(k:ℝ)*a) := mul_le_mul_of_nonneg_left hh hp
      _ ≤ 1 := ih

lemma spectral_weight_ge_one {a : ℝ} (ha : 0 ≤ a) (ha1 : a ≤ 1)
    (k : ℕ) (hk : 1 ≤ (k:ℝ)*a) : 1 ≤ 2*(1-(1-a)^k) := by
  have hp := pow_nonneg (by linarith : 0 ≤ 1-a) k
  have hb := geometric_bernoulli ha ha1 k
  nlinarith [mul_nonneg hp (sub_nonneg.mpr hk)]

lemma row_tail_le {a : ℝ} (ha : 0 ≤ a) (ha1 : a ≤ 1) (D : ℕ)
    (hD : ∀ k : ℕ, D < k → 1 ≤ (k:ℝ)*a) (f : (R → W) → ℝ) :
    (𝔼 B, (f B-project D f B)^2) ≤
      𝔼 B, 𝔼 C, density a B C*(f B-f C)^2 := by
  rw [residual_energy, dirichlet, Finset.mul_sum]
  apply Finset.sum_le_sum
  intro α _
  by_cases hd : D < degree α
  · rw [ite_eq_left hd, ← mul_assoc]
    simpa using mul_le_mul_of_nonneg_right
      (spectral_weight_ge_one ha ha1 (degree α) (hD _ hd))
      (sq_nonneg (coefficient f α))
  · rw [ite_eq_right hd]
    have hp : (1-a)^degree α ≤ 1 := pow_le_one₀ (by linarith) (by linarith)
    positivity

end MinUncut.RowNoise

end

end OAI