cfahlgren1's picture
download
raw
17 kB
import OAI.Probability.GaussianReplacement.MixedGeometry
namespace OAI
open MeasureTheory ProbabilityTheory Set Filter
open scoped ENNReal NNReal RealInnerProductSpace Topology
noncomputable section
namespace CurrentProjection
open InnerProductSpace
section GramFactorization
variable {E : Type*} [NormedAddCommGroup E] [InnerProductSpace ℝ E]
{ι : Type*} [Fintype ι]
lemma gram_linear_combination (w : ι → E) (T : Matrix ι ι ℝ) :
Matrix.gram ℝ (fun j => ∑ i, (T i j) • w i) =
T.transpose * Matrix.gram ℝ w * T := by
ext a b
simp only [Matrix.gram_apply, sum_inner, inner_sum, real_inner_smul_left,
real_inner_smul_right, Matrix.mul_apply, Matrix.transpose_apply]
simp only [Finset.sum_mul]
congr 1
ext j
congr 1
ext i
ring
variable (n : ℕ)
def gsUpper (v : Fin n → E) : Matrix (Fin n) (Fin n) ℝ := fun i j =>
(if i = j then 1 else 0) +
if i < j then ⟪gramSchmidt ℝ v i, v j⟫ / ‖gramSchmidt ℝ v i‖ ^ 2 else 0
lemma gsUpper_expansion (v : Fin n → E) (j : Fin n) :
∑ i, (gsUpper n v i j) • gramSchmidt ℝ v i = v j := by
simp only [gsUpper, add_smul, Finset.sum_add_distrib]
simp only [ite_smul, one_smul, zero_smul, Finset.sum_ite_eq', Finset.mem_univ, ite_true]
conv_rhs => rw [gramSchmidt_def'' ℝ v j]
congr 1
rw [← Finset.sum_filter]
congr 1
ext i
simp
lemma gsUpper_det (v : Fin n → E) : (gsUpper n v).det = 1 := by
have htri : (gsUpper n v).IsUpperTriangular := by
intro i j hij
change j < i at hij
simp [gsUpper, (ne_of_gt hij), (not_lt_of_ge hij.le)]
rw [Matrix.det_of_isUpperTriangular htri]
simp [gsUpper]
lemma gram_gramSchmidt_diagonal (v : Fin n → E) :
Matrix.gram ℝ (gramSchmidt ℝ v) = Matrix.diagonal (fun i => ‖gramSchmidt ℝ v i‖ ^ 2) := by
ext i j
by_cases hij : i = j
· subst j
simp [Matrix.gram_apply]
· simp [Matrix.gram_apply, hij,
gramSchmidt_orthogonal ℝ v hij]
theorem det_gram_eq_prod_gramSchmidt (v : Fin n → E) :
(Matrix.gram ℝ v).det = ∏ i, ‖gramSchmidt ℝ v i‖ ^ 2 := by
have hv : (fun j => ∑ i, (gsUpper n v i j) • gramSchmidt ℝ v i) = v :=
funext (gsUpper_expansion n v)
have h := gram_linear_combination (gramSchmidt ℝ v) (gsUpper n v)
rw [hv] at h
rw [h, Matrix.det_mul, Matrix.det_mul, Matrix.det_transpose, gsUpper_det,
one_mul, mul_one, gram_gramSchmidt_diagonal, Matrix.det_diagonal]
end GramFactorization
section GramDistances
variable {E : Type*} [NormedAddCommGroup E] [InnerProductSpace ℝ E]
[FiniteDimensional ℝ E] {n : ℕ}
omit [FiniteDimensional ℝ E] in
lemma sub_gramSchmidt_mem_span (v : Fin n → E) (j : Fin n) :
v j - gramSchmidt ℝ v j ∈ Submodule.span ℝ (v '' Set.Iio j) := by
rw [← span_gramSchmidt_Iio ℝ v j]
conv => arg 2; lhs; rw [gramSchmidt_def'' ℝ v j]
rw [add_sub_cancel_left]
exact Submodule.sum_mem _ fun i hi => Submodule.smul_mem _ _
(Submodule.subset_span ⟨i, Finset.mem_Iio.mp hi, rfl⟩)
omit [FiniteDimensional ℝ E] in
lemma gramSchmidt_mem_span_orthogonal (v : Fin n → E) (j : Fin n) :
gramSchmidt ℝ v j ∈ (Submodule.span ℝ (v '' Set.Iio j))ᗮ := by
rw [Submodule.mem_orthogonal']
intro w hw
induction hw using Submodule.span_induction with
| mem x hx =>
rcases hx with ⟨i, hi, rfl⟩
exact gramSchmidt_inv_triangular ℝ v hi
| zero => simp
| add x y _ _ hx hy => simp [inner_add_right, hx, hy]
| smul c x _ hx => simp [inner_smul_right, hx]
lemma gramSchmidt_eq_sub_starProjection (v : Fin n → E) (j : Fin n) :
gramSchmidt ℝ v j = v j -
(Submodule.span ℝ (v '' Set.Iio j)).starProjection (v j) := by
have h := (Submodule.span ℝ (v '' Set.Iio j)).eq_starProjection_of_mem_orthogonal
(u := v j) (sub_gramSchmidt_mem_span v j)
(by simpa using gramSchmidt_mem_span_orthogonal v j)
rw [h, sub_sub_cancel]
lemma norm_gramSchmidt_eq_infDist (v : Fin n → E) (j : Fin n) :
‖gramSchmidt ℝ v j‖ = Metric.infDist (v j)
(Submodule.span ℝ (v '' Set.Iio j) : Set E) := by
rw [gramSchmidt_eq_sub_starProjection, Submodule.starProjection_minimal,
Metric.infDist_eq_iInf]
simp only [dist_eq_norm]
rfl
lemma infDist_affine_mk' (p x : E) (K : Submodule ℝ E) :
Metric.infDist x (AffineSubspace.mk' p K : Set E) =
Metric.infDist (x - p) (K : Set E) := by
rw [← EuclideanGeometry.dist_orthogonalProjection_eq_infDist,
EuclideanGeometry.orthogonalProjection_apply_mem _ (AffineSubspace.self_mem_mk' p K),
dist_eq_norm, Metric.infDist_eq_iInf]
simp only [dist_eq_norm]
change ‖x - _‖ = ⨅ y : K, ‖(x - p) - y‖
rw [← Submodule.starProjection_minimal]
simp only [AffineSubspace.direction_mk', vsub_eq_sub, vadd_eq_add,
Submodule.coe_orthogonalProjectionOnto_apply]
congr 1
abel
lemma norm_gramSchmidt_sub_eq_affine_infDist (p : E) (t : Fin n → E) (j : Fin n) :
‖gramSchmidt ℝ (fun i => t i - p) j‖ =
Metric.infDist (t j) (AffineSubspace.mk' p
(Submodule.span ℝ ((fun i => t i - p) '' Set.Iio j)) : Set E) := by
rw [infDist_affine_mk', norm_gramSchmidt_eq_infDist]
omit [NormedAddCommGroup E] [InnerProductSpace ℝ E] [FiniteDimensional ℝ E] in
lemma image_Iio_castSucc (v : Fin (n+1) → E) (j : Fin n) :
v '' Set.Iio j.castSucc = (fun i => v i.castSucc) '' Set.Iio j := by
ext x
constructor
· rintro ⟨i, hi, rfl⟩
have hin : i.val < n := lt_trans hi j.isLt
exact ⟨⟨i.val, hin⟩, hi, rfl⟩
· rintro ⟨i, hi, rfl⟩
exact ⟨i.castSucc, hi, rfl⟩
omit [NormedAddCommGroup E] [InnerProductSpace ℝ E] [FiniteDimensional ℝ E] in
lemma image_Iio_last (v : Fin (n+1) → E) :
v '' Set.Iio (Fin.last n) = Set.range (fun i : Fin n => v i.castSucc) := by
ext x
constructor
· rintro ⟨i, hi, rfl⟩
exact ⟨⟨i.val, hi⟩, rfl⟩
· rintro ⟨i, rfl⟩
exact ⟨i.castSucc, i.isLt, rfl⟩
lemma gramSchmidt_castSucc (v : Fin (n+1) → E) (j : Fin n) :
gramSchmidt ℝ v j.castSucc = gramSchmidt ℝ (fun i => v i.castSucc) j := by
rw [gramSchmidt_eq_sub_starProjection, gramSchmidt_eq_sub_starProjection,
image_Iio_castSucc]
variable [MeasurableSpace E] [BorelSpace E]
lemma measurable_gramSchmidt (j : Fin n) :
Measurable (fun v : Fin n → E => gramSchmidt ℝ v j) := by
refine (wellFounded_lt : WellFounded ((· < ·) : Fin n → Fin n → Prop)).induction
(C := fun j : Fin n => Measurable (fun v : Fin n → E => gramSchmidt ℝ v j)) j ?_
intro j ih
have heq : (fun v : Fin n → E => gramSchmidt ℝ v j) =
fun v => v j - ∑ i ∈ Finset.Iio j,
(⟪gramSchmidt ℝ v i, v j⟫ / ‖gramSchmidt ℝ v i‖ ^ 2) • gramSchmidt ℝ v i := by
ext v
rw [gramSchmidt_def]
simp only [Submodule.starProjection_singleton]
rfl
rw [heq]
apply (measurable_pi_apply j).sub
apply Finset.measurable_sum
intro i hi
have hi' := ih i (Finset.mem_Iio.mp hi)
exact ((hi'.inner (measurable_pi_apply j)).div (hi'.norm.pow_const 2)).smul hi'
end GramDistances
section PiLast
variable {E : Type*} [MeasurableSpace E] {n : ℕ} (ν : Measure E) [SigmaFinite ν]
lemma lintegral_pi_snoc (f : (Fin (n+1) → E) → ℝ≥0∞) (hf : Measurable f) :
∫⁻ v, f v ∂Measure.pi (fun _ => ν) =
∫⁻ v : Fin n → E, ∫⁻ x, f (Fin.snoc v x) ∂ν ∂Measure.pi (fun _ => ν) := by
have hp := (measurePreserving_piFinSuccAbove (fun _ : Fin (n+1) => ν) (Fin.last n)).symm
rw [← hp.lintegral_comp_emb (MeasurableEquiv.measurableEmbedding _) f,
lintegral_prod_symm]
· congr 1
ext v
congr 1
ext x
congr 1
simp only [MeasurableEquiv.piFinSuccAbove_symm_apply, Fin.insertNthEquiv]
exact Fin.insertNth_last' x v
· exact (hf.comp (MeasurableEquiv.measurable _)).aemeasurable
end PiLast
section AePiLast
variable {E : Type*} [MeasurableSpace E] {n : ℕ} (ν : Measure E) [SigmaFinite ν]
lemma ae_pi_snoc {P : (Fin (n+1) → E) → Prop} (hP : MeasurableSet {v | P v}) :
(∀ᵐ v ∂Measure.pi (fun _ => ν), P v) ↔
∀ᵐ v : Fin n → E ∂Measure.pi (fun _ => ν), ∀ᵐ x ∂ν, P (Fin.snoc v x) := by
have hp := (measurePreserving_piFinSuccAbove (fun _ : Fin (n+1) => ν) (Fin.last n)).symm
rw [← hp.map_eq, MeasurableEmbedding.ae_map_iff (MeasurableEquiv.measurableEmbedding _)]
have heq : (fun z : E × (Fin n → E) =>
(MeasurableEquiv.piFinSuccAbove (fun _ : Fin (n+1) => E) (Fin.last n)).symm z) =
fun z => Fin.snoc z.2 z.1 := by
ext z : 1
change (Fin.last n).insertNth (α := fun _ => E) z.1 z.2 = _
exact Fin.insertNth_last' z.1 z.2
have hm : Measurable (fun z : E × (Fin n → E) => Fin.snoc (α := fun _ => E) z.2 z.1) := by
rw [← heq]
exact hp.measurable
change (∀ᵐ z ∂ν.prod (Measure.pi (fun _ : Fin n => ν)),
P ((fun z => (MeasurableEquiv.piFinSuccAbove (fun _ : Fin (n+1) => E)
(Fin.last n)).symm z) z)) ↔ _
have hPP : MeasurableSet {z : E × (Fin n → E) | P (Fin.snoc z.2 z.1)} := hP.preimage hm
rw [heq, Measure.ae_prod_iff_ae_ae hPP]
exact Measure.ae_ae_comm hPP
end AePiLast
section ReplicaNondegenerate
variable {E : Type*} [NormedAddCommGroup E] [InnerProductSpace ℝ E]
[FiniteDimensional ℝ E] [MeasurableSpace E] [BorelSpace E]
lemma measurableSet_gramSchmidt_ne_zero (p : E) (n : ℕ) :
MeasurableSet {v : Fin n → E | ∀ i, gramSchmidt ℝ (fun j => v j - p) i ≠ 0} := by
rw [Set.ofPred_forall]
apply MeasurableSet.iInter
intro i
have h := (measurable_gramSchmidt i).comp
(show Measurable (fun v : Fin n → E => fun j => v j - p) by fun_prop)
exact (measurableSet_eq_fun h measurable_const).compl
end ReplicaNondegenerate
section GaussianDomination
variable {E : Type*} [NormedAddCommGroup E] [InnerProductSpace ℝ E]
[FiniteDimensional ℝ E] [MeasurableSpace E] [BorelSpace E]
lemma orthonormal_gaussian_map {n : ℕ} {u : Fin n → E} (hu : Orthonormal ℝ u) :
(stdGaussian E).map (fun x => fun i => ⟪u i, x⟫) =
Measure.pi (fun _ : Fin n => gaussianReal 0 1) := by
let f : E → EuclideanSpace ℝ (Fin n) := fun x => WithLp.toLp 2 (fun i => ⟪u i, x⟫)
have hf : Measurable f := by dsimp [f]; fun_prop
have he : (stdGaussian E).map f = stdGaussian (EuclideanSpace ℝ (Fin n)) := by
apply Measure.ext_of_charFun
ext t
have hi (x : E) : ⟪f x, t⟫ = ⟪x, ∑ i, t i • u i⟫ := by
simp [f, PiLp.inner_apply, inner_sum, inner_smul_right, real_inner_comm x]
have hn : ‖∑ i, t i • u i‖ ^ 2 = ‖t‖ ^ 2 := by
rw [← real_inner_self_eq_norm_sq, hu.inner_sum, EuclideanSpace.real_norm_sq_eq]
simp [pow_two]
rw [charFun_apply, integral_map hf.aemeasurable (by fun_prop)]
simp_rw [hi]
change charFun (stdGaussian E) (∑ i, t i • u i) = _
rw [charFun_stdGaussian, charFun_stdGaussian]
congr 2
exact_mod_cast congrArg Neg.neg hn
have h := congrArg (Measure.map (WithLp.ofLp : EuclideanSpace ℝ (Fin n) → (Fin n → ℝ))) he
rw [← map_pi_eq_stdGaussian] at h
simpa [Measure.map_map (WithLp.measurable_ofLp _ _) hf,
Measure.map_map (WithLp.measurable_ofLp _ _) (WithLp.measurable_toLp _ _),
Function.comp_def, f, WithLp.ofLp_toLp, Measure.map_id] using h
lemma gaussianReal_le_volume : gaussianReal 0 1 ≤ (volume : Measure ℝ) := by
rw [gaussianReal_of_var_ne_zero _ (by norm_num)]
conv_rhs => rw [← withDensity_one (μ := (volume : Measure ℝ))]
apply withDensity_mono
filter_upwards [] with x
change ENNReal.ofReal (gaussianPDFReal 0 1 x) ≤ 1
rw [← ENNReal.ofReal_one]
apply ENNReal.ofReal_le_ofReal
unfold gaussianPDFReal
have hs : 1 ≤ Real.sqrt (2 * Real.pi * (1 : ℝ≥0)) := by
norm_num only [NNReal.coe_one, mul_one]
apply (Real.le_sqrt (by norm_num) (by positivity)).mpr
nlinarith [Real.pi_gt_three]
have he : Real.exp (-(x - 0)^2 / (2 * (1 : ℝ≥0))) ≤ 1 := by
apply Real.exp_le_one_iff.mpr
exact div_nonpos_of_nonpos_of_nonneg (neg_nonpos.mpr (sq_nonneg _)) (by positivity)
exact (mul_le_of_le_one_left (by positivity) (inv_le_one_of_one_le₀ hs)).trans he
lemma measure_pi_mono {ι : Type*} [Fintype ι] {X : ι → Type*}
[∀ i, MeasurableSpace (X i)] {μ ν : ∀ i, Measure (X i)}
(h : ∀ i, μ i ≤ ν i) : Measure.pi μ ≤ Measure.pi ν := by
have ho : OuterMeasure.pi (fun i => (μ i).toOuterMeasure) ≤
OuterMeasure.pi (fun i => (ν i).toOuterMeasure) := by
apply OuterMeasure.le_pi.mpr
intro s _
exact (OuterMeasure.pi_pi_le _ s).trans (Finset.prod_le_prod fun i _ => h i (s i))
apply Measure.le_iff.mpr
intro s hs
rw [Measure.pi_def, Measure.pi_def, toMeasure_apply _ _ hs, toMeasure_apply _ _ hs]
exact ho s
lemma pi_gaussianReal_le_volume (n : ℕ) :
Measure.pi (fun _ : Fin n => gaussianReal 0 1) ≤ (volume : Measure (Fin n → ℝ)) :=
measure_pi_mono (fun _ => gaussianReal_le_volume)
lemma gaussian_pairings_le {n : ℕ} (v : Fin n → E) (hv : LinearIndependent ℝ v) :
(stdGaussian E).map (fun x => fun i => ⟪v i, x⟫) ≤
ENNReal.ofReal ((∏ i, ‖gramSchmidt ℝ v i‖)⁻¹) •
(volume : Measure (Fin n → ℝ)) := by
let u := gramSchmidtNormed ℝ v
let R : Matrix (Fin n) (Fin n) ℝ :=
Matrix.diagonal (fun i => ‖gramSchmidt ℝ v i‖) * gsUpper n v
have hu : Orthonormal ℝ u := gramSchmidtNormed_orthonormal hv
have hn (i : Fin n) : ‖gramSchmidt ℝ v i‖ ≠ 0 :=
norm_ne_zero_iff.mpr (gramSchmidt_ne_zero i hv)
have hexp (j : Fin n) : ∑ i, R i j • u i = v j := by
rw [← gsUpper_expansion n v j]
apply Finset.sum_congr rfl
intro i _
simp only [R, u, Matrix.diagonal_mul, gramSchmidtNormed, smul_smul]
congr 1
simp [hn i, mul_comm]
have hdet : R.transpose.det = ∏ i, ‖gramSchmidt ℝ v i‖ := by
dsimp only [R]
rw [Matrix.det_transpose, Matrix.det_mul, Matrix.det_diagonal, gsUpper_det, mul_one]
have hd : R.transpose.det ≠ 0 := by
rw [hdet]
exact Finset.prod_ne_zero_iff.mpr (fun i _ => hn i)
have hpos : 0 ≤ R.transpose.det := by rw [hdet]; positivity
have hf : Measurable (Matrix.toLin' R.transpose) :=
(Matrix.toLin' R.transpose).continuous_of_finiteDimensional.measurable
have heq : (fun x : E => fun j => ⟪v j, x⟫) =
(Matrix.toLin' R.transpose) ∘ (fun x => fun i => ⟪u i, x⟫) := by
funext x j
rw [← hexp j]
simp [Matrix.toLin'_apply, Matrix.mulVec, dotProduct, sum_inner,
inner_smul_left]
rw [heq, ← Measure.map_map hf (by fun_prop), orthonormal_gaussian_map hu]
calc
_ ≤ (volume : Measure (Fin n → ℝ)).map (Matrix.toLin' R.transpose) :=
Measure.map_mono (pi_gaussianReal_le_volume n) hf
_ = _ := by
rw [Real.map_matrix_volume_pi_eq_smul_volume_pi hd,
abs_of_nonneg (inv_nonneg.mpr hpos), hdet]
lemma measure_pi_smul {ι : Type*} [Fintype ι] {X : ι → Type*}
[∀ i, MeasurableSpace (X i)] (μ : ∀ i, Measure (X i)) [∀ i, SigmaFinite (μ i)]
(c : ℝ≥0∞) (hc : c ≠ ∞) :
Measure.pi (fun i => c • μ i) = c ^ Fintype.card ι • Measure.pi μ := by
lift c to ℝ≥0 using hc
simp_rw [Measure.coe_nnreal_smul]
apply Measure.pi_eq
intro s _
simp only [Measure.smul_apply, Measure.pi_pi, ENNReal.smul_def, smul_eq_mul, Finset.prod_mul_distrib,
Finset.prod_const, Finset.card_univ]
lemma gaussian_rows_pairings_le {n : ℕ} (v : Fin n → E) (hv : LinearIndependent ℝ v)
(m : ℕ) :
(Measure.pi (fun _ : Fin m => stdGaussian E)).map
(fun A => fun j i => ⟪v i, A j⟫) ≤
ENNReal.ofReal ((∏ i, ‖gramSchmidt ℝ v i‖)⁻¹ ^ m) •
(volume : Measure (Fin m → Fin n → ℝ)) := by
rw [Measure.pi_map_pi (fun _ => (by fun_prop : Measurable
(fun x : E => fun i => ⟪v i, x⟫)).aemeasurable)]
have h := measure_pi_mono (fun _ : Fin m => gaussian_pairings_le v hv)
rw [measure_pi_smul _ _ ENNReal.ofReal_ne_top] at h
rw [ENNReal.ofReal_pow (by positivity)]
simpa only [Fintype.card_fin, volume_pi] using h
end GaussianDomination
section ReplicaKernel
variable {X : Type*} [MeasurableSpace X] (μ : Measure X) [SigmaFinite μ]
lemma lintegral_fin_prod (n : ℕ) (f : Fin n → X → ℝ≥0∞) (hf : ∀ i, Measurable (f i)) :
∫⁻ x : Fin n → X, ∏ i, f i (x i) ∂Measure.pi (fun _ => μ) =
∏ i, ∫⁻ x, f i x ∂μ := by
induction n with
| zero => simp
| succ n ih =>
rw [lintegral_pi_snoc μ _ (by fun_prop)]
simp_rw [Fin.prod_univ_castSucc, Fin.snoc_castSucc, Fin.snoc_last]
simp_rw [lintegral_const_mul _ (hf (Fin.last n))]
rw [lintegral_mul_const _ (by fun_prop), ih _ (fun i => hf i.castSucc)]
lemma lintegral_pow_pi (f : X → ℝ≥0∞) (hf : Measurable f) (n : ℕ) :
(∫⁻ x, f x ∂μ)^n =
∫⁻ x : Fin n → X, ∏ i, f (x i) ∂Measure.pi (fun _ => μ) := by
rw [lintegral_fin_prod μ n _ (fun _ => hf)]
simp
end ReplicaKernel
end CurrentProjection
end
end OAI

Xet Storage Details

Size:
17 kB
·
Xet hash:
e1f143b3b928e2b49c9216f04f5267f2f9091fc86ac125f8f532acc89e1c83a2

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.