math / data /lean /OAI /Computability /MinUncut /Estimates /Convolution.lean
introvoyz045's picture
Migrated from GitHub
4a56cd6 verified
Raw History Blame Contribute Delete
13.1 kB
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