math / data /lean /OAI /Probability /SKRatio /Variational /FormContinuity.lean
introvoyz045's picture
Migrated from GitHub
4a56cd6 verified
Raw History Blame Contribute Delete
8.66 kB
import Mathlib
import OAI.Probability.SKRatio.Matrices.CompactMatrixMasks
import OAI.Probability.SKRatio.Variational.FormAlgebra
namespace OAI
noncomputable section
open scoped BigOperators Topology
open MeasureTheory ProbabilityTheory Filter Set Real
namespace SKRatio.Planted
open Scalar Calculus MatrixNet
attribute [local instance] Classical.propDecidable
variable {n : β„•}
lemma weightedForm_field_sub (A : Matrix (Fin n) (Fin n) ℝ)
(G : FieldWeight Γ— FieldWeight β†’ ℝ) (x y : Fin n β†’ FieldWeight) (p : Fin n β†’ ℝ) :
weightedForm A G x p-weightedForm A G y p =
quadratic (fun i j => A i j*(G (x i,x j)-G (y i,y j))) p := by
simp only [weightedForm,quadratic,mul_sub,sub_mul,Finset.sum_sub_distrib]
lemma weightedForm_field_bound (A : Matrix (Fin n) (Fin n) ℝ)
(G : FieldWeight Γ— FieldWeight β†’ ℝ) (x y : Fin n β†’ FieldWeight) (p : Fin n β†’ ℝ)
{a C : ℝ} (ha : 0≀a) (hC : 0≀C)
(hr : βˆ€ i, βˆ‘ j, |A i j| ≀ C) (hc : βˆ€ j, βˆ‘ i, |A i j| ≀ C)
(he : βˆ€ i j, |G (x i,x j)-G (y i,y j)| ≀ a) :
|weightedForm A G x p-weightedForm A G y p| ≀ a*C*βˆ‘ i, p i^2 := by
rw [weightedForm_field_sub]
exact (quadratic_abs_le _ p).trans (mul_le_mul_of_nonneg_right
(kernel_error_norm_le A _ _ ha hC hr hc he) (Finset.sum_nonneg (fun i _ => sq_nonneg (p i))))
lemma diagonalForm_field_bound (A : Matrix (Fin n) (Fin n) ℝ)
(G : FieldWeight Γ— FieldWeight β†’ ℝ) (x y : Fin n β†’ FieldWeight) (p : Fin n β†’ ℝ)
{a C : ℝ} (ha : 0≀a)
(hr : βˆ€ i, βˆ‘ j, |A i j| ≀ C)
(he : βˆ€ i j, |G (x i,x j)-G (y i,y j)| ≀ a) :
|diagonalForm A G x p-diagonalForm A G y p| ≀ a*C*βˆ‘ i, p i^2 := by
have hid : diagonalForm A G x p-diagonalForm A G y p =
βˆ‘ i, (βˆ‘ j, A i j*(G (x i,x j)-G (y i,y j)))*p i^2 := by
simp only [diagonalForm,mul_sub,sub_mul,Finset.sum_sub_distrib]
have hb (i : Fin n) : |βˆ‘ j, A i j*(G (x i,x j)-G (y i,y j))| ≀ a*C := by
calc
_ ≀ βˆ‘ j, |A i j*(G (x i,x j)-G (y i,y j))| := Finset.abs_sum_le_sum_abs _ _
_ ≀ βˆ‘ j, a*|A i j| := Finset.sum_le_sum (fun j _ => by
rw [abs_mul,mul_comm]
exact mul_le_mul_of_nonneg_right (he i j) (abs_nonneg _))
_ = a*(βˆ‘ j, |A i j|) := (Finset.mul_sum ..).symm
_ ≀ _ := mul_le_mul_of_nonneg_left (hr i) ha
rw [hid]
calc
_ ≀ βˆ‘ i, |(βˆ‘ j, A i j*(G (x i,x j)-G (y i,y j)))*p i^2| := Finset.abs_sum_le_sum_abs _ _
_ ≀ βˆ‘ i, (a*C)*p i^2 := Finset.sum_le_sum (fun i _ => by
rw [abs_mul,abs_of_nonneg (sq_nonneg (p i))]
exact mul_le_mul_of_nonneg_right (hb i) (sq_nonneg _))
_ = _ := (Finset.mul_sum ..).symm
lemma weightedForm_variance_eq (J : Interaction n) (x : Fin n β†’ FieldWeight) (p : Fin n β†’ ℝ) :
weightedForm J varianceKernel x p = quadratic ((Matrix.of J)*
Matrix.diagonal (fun i => compactV (x i))) p := by
simp only [weightedForm,quadratic,varianceKernel,Matrix.mul_diagonal,Matrix.of_apply]
lemma weightedForm_variance_field_bound (J : Interaction n) (x y : Fin n β†’ FieldWeight)
(p : Fin n β†’ ℝ) {a : ℝ} (ha : 0≀a)
(he : βˆ€ i, |compactV (x i)-compactV (y i)| ≀ a) :
|weightedForm J varianceKernel x p-weightedForm J varianceKernel y p| ≀
a*euclideanOpNorm J*βˆ‘ i, p i^2 := by
let D : Matrix (Fin n) (Fin n) ℝ := Matrix.diagonal (fun i => compactV (x i)-compactV (y i))
have heq : (Matrix.of J)*Matrix.diagonal (fun i => compactV (x i)) -
(Matrix.of J)*Matrix.diagonal (fun i => compactV (y i)) = (Matrix.of J)*D := by
apply Matrix.ext
intro i j
simp only [Matrix.sub_apply,Matrix.mul_diagonal,Matrix.of_apply,D]
ring
rw [weightedForm_variance_eq,weightedForm_variance_eq,←quadratic_sub,heq]
have hnD : β€–Matrix.toEuclideanCLM (n := Fin n) (π•œ := ℝ) Dβ€– ≀ a :=
matrix_diagonal_opNorm_le _ a ha he
have hn : euclideanOpNorm ((Matrix.of J)*D) ≀ a*euclideanOpNorm J := by
unfold euclideanOpNorm
rw [map_mul,mul_comm a]
apply (norm_mul_le _ _).trans
exact mul_le_mul_of_nonneg_left hnD (norm_nonneg _)
exact (quadratic_abs_le _ p).trans (mul_le_mul_of_nonneg_right hn
(Finset.sum_nonneg (fun i _ => sq_nonneg (p i))))
lemma compactForm_field_bound (J : Interaction n) (hJ : βˆ€ i j, J i j=J j i)
(x y : Fin n β†’ FieldWeight) (p : Fin n β†’ ℝ) {a C : ℝ} (ha : 0≀a) (hC : 0≀C)
(hr : βˆ€ i, βˆ‘ j, J i j^2 ≀ C)
(hv : βˆ€ i, |compactV (x i)-compactV (y i)| ≀ a)
(hm : βˆ€ i j, |meanVarianceKernel (x i,x j)-meanVarianceKernel (y i,y j)| ≀ a)
(hf : βˆ€ i j, |compactF (x i,x j)-compactF (y i,y j)| ≀ a)
(hk : βˆ€ i j, |compactK (x i,x j)-compactK (y i,y j)| ≀ a) :
|compactForm J x p-compactForm J y p| ≀ a*(euclideanOpNorm J+4*C)*(βˆ‘ i, p i^2) := by
have hr' (i : Fin n) : βˆ‘ j, |J i j^2| ≀ C := by simpa only [abs_pow,sq_abs] using hr i
have hc' (j : Fin n) : βˆ‘ i, |J i j^2| ≀ C := by simpa only [hJ,abs_pow,sq_abs] using hr j
let L := weightedForm J varianceKernel x p-weightedForm J varianceKernel y p
let M := weightedForm (fun i j => J i j^2) meanVarianceKernel x p-weightedForm (fun i j => J i j^2) meanVarianceKernel y p
let D := diagonalForm (fun i j => J i j^2) compactF x p-diagonalForm (fun i j => J i j^2) compactF y p
let K := weightedForm (fun i j => J i j^2) compactK x p-weightedForm (fun i j => J i j^2) compactK y p
have hid : compactForm J x p-compactForm J y p=L+2*M+D+K := by dsimp [compactForm,L,M,D,K]; ring
have hL : |L| ≀ a*euclideanOpNorm J*(βˆ‘ i, p i^2) := weightedForm_variance_field_bound J x y p ha hv
have hM : |M| ≀ a*C*(βˆ‘ i, p i^2) := weightedForm_field_bound _ _ x y p ha hC hr' hc' hm
have hD : |D| ≀ a*C*(βˆ‘ i, p i^2) := diagonalForm_field_bound _ _ x y p ha hr' hf
have hK : |K| ≀ a*C*(βˆ‘ i, p i^2) := weightedForm_field_bound _ _ x y p ha hC hr' hc' hk
rw [hid]
have ht := (abs_add_le (L+2*M+D) K).trans
(add_le_add ((abs_add_le (L+2*M) D).trans (add_le_add (abs_add_le L (2*M)) le_rfl)) le_rfl)
rw [abs_mul,abs_of_pos (by norm_num : (0:ℝ)<2)] at ht
nlinarith only [ht,hL,hM,hD,hK]
lemma form_kernels_uniform {a : ℝ} (ha : 0<a) :
βˆƒ Ξ΄ : ℝ, 0<Ξ΄ ∧ βˆ€ x y x' y' : FieldWeight,
dist x x'<Ξ΄ β†’ dist y y'<Ξ΄ β†’
|compactV y-compactV y'|≀a ∧
|meanVarianceKernel (x,y)-meanVarianceKernel (x',y')|≀a ∧
|compactF (x,y)-compactF (x',y')|≀a ∧
|compactK (x,y)-compactK (x',y')|≀a := by
let G : FieldWeight Γ— FieldWeight β†’ Fin 4 β†’ ℝ := fun p =>
![varianceKernel p,meanVarianceKernel p,compactF p,compactK p]
have hG : Continuous G := by
apply continuous_pi
intro i
fin_cases i
Β· exact continuous_varianceKernel
Β· exact continuous_meanVarianceKernel
Β· exact continuous_compactF
Β· exact continuous_compactK
obtain ⟨δ,hδ,hcont⟩ := Metric.uniformContinuous_iff.mp
(CompactSpace.uniformContinuous_of_continuous hG) a ha
refine ⟨δ,hδ,?_⟩
intro x y x' y' hx hy
have hh := (dist_pi_lt_iff ha).mp (hcont (a := (x,y)) (b := (x',y'))
(by simpa only [Prod.dist_eq,max_lt_iff] using And.intro hx hy))
exact ⟨(hh 0).le,(hh 1).le,(hh 2).le,(hh 3).le⟩
lemma compactForm_uniform_field_change {C K u : ℝ} (hC : 0≀C) (hK : 0≀K) (hu : 0<u) :
βˆƒ Ξ΄ : ℝ, 0<Ξ΄ ∧ βˆ€ {n : β„•} (J : Interaction n), (βˆ€ i j, J i j=J j i) β†’
euclideanOpNorm J≀K β†’ (βˆ€ i, βˆ‘ j, J i j^2≀C) β†’
βˆ€ H H' p : Fin n β†’ ℝ, (βˆ€ i, |H i-H' i|≀δ) β†’
|compactForm J (fun i => compactWeight (H i)) p-
compactForm J (fun i => compactWeight (H' i)) p| ≀ u*(βˆ‘ i, p i^2) := by
let a := u/(K+4*C+1)
have ha : 0<a := by dsimp [a]; positivity
obtain ⟨δ,hδ,hcont⟩ := form_kernels_uniform ha
refine ⟨δ/2,by positivity,?_⟩
intro n J hJ hnorm hr H H' p hnear
have hd (i : Fin n) : dist (compactWeight (H i)) (compactWeight (H' i))<Ξ΄ := by
have hb := compactWeight_lipschitz.dist_le_mul (H i) (H' i)
simp only [NNReal.coe_one,one_mul,Real.dist_eq] at hb
exact lt_of_le_of_lt (hb.trans (hnear i)) (by linarith)
have hb := compactForm_field_bound J hJ _ _ p ha.le hC hr
(fun i => (hcont _ _ _ _ (hd i) (hd i)).1)
(fun i j => (hcont _ _ _ _ (hd i) (hd j)).2.1)
(fun i j => (hcont _ _ _ _ (hd i) (hd j)).2.2.1)
(fun i j => (hcont _ _ _ _ (hd i) (hd j)).2.2.2)
apply hb.trans
apply mul_le_mul_of_nonneg_right _ (Finset.sum_nonneg (fun _ _ => sq_nonneg _))
have hden : 0<K+4*C+1 := by positivity
have hid : a*(K+4*C+1)=u := div_mul_cancelβ‚€ _ hden.ne'
nlinarith only [hid,mul_nonneg ha.le (sub_nonneg.mpr hnorm),ha.le]
end SKRatio.Planted
end
end OAI