import OAI.Probability.PosteriorReplicas.Gaussian.RadialIntegrals import OAI.Probability.PosteriorReplicas.Geometry.BallJacobian namespace OAI open MeasureTheory ProbabilityTheory Set Matrix InnerProductSpace Metric open scoped ENNReal NNReal RealInnerProductSpace Topology noncomputable section namespace PosteriorReplicas variable {E : Type*} [NormedAddCommGroup E] [InnerProductSpace ℝ E] [FiniteDimensional ℝ E] [MeasurableSpace E] [BorelSpace E] lemma gaussian_orthogonalProjectionOnto (U : Submodule ℝ E) : (stdGaussian E).map U.orthogonalProjectionOnto = stdGaussian U := by apply Measure.ext_of_charFun ext t rw [charFun_apply, integral_map (by fun_prop) (by fun_prop)] have he (x : E) : ⟪U.orthogonalProjectionOnto x, t⟫ = ⟪x, (t : E)⟫ := by change ⟪U.starProjection x, (t : E)⟫ = _ rw [real_inner_comm, pairing_starProjection U t.2, real_inner_comm] simp_rw [he] change charFun (stdGaussian E) (t : E) = _ rw [charFun_stdGaussian, charFun_stdGaussian] rfl lemma gaussian_subspace_map (U : Submodule ℝ E) : (stdGaussian U).map (Subtype.val : U → E) = (stdGaussian E).map U.starProjection := by rw [← gaussian_orthogonalProjectionOnto U, Measure.map_map (by fun_prop) (by fun_prop)] rfl lemma gaussian_subspace_sum (U : Submodule ℝ E) : ((stdGaussian U).prod (stdGaussian Uᗮ)).map (fun z => (z.1 : E) + (z.2 : E)) = stdGaussian E := by rw [← gaussian_orthogonal_sum U, ← gaussian_subspace_map, ← gaussian_subspace_map, Measure.map_prod_map _ _ (by fun_prop) (by fun_prop), Measure.map_map (by fun_prop) (by fun_prop)] rfl omit [FiniteDimensional ℝ E] [MeasurableSpace E] [BorelSpace E] in lemma subspace_sum_norm_sq [FiniteDimensional ℝ E] [MeasurableSpace E] [BorelSpace E] (U : Submodule ℝ E) (x : U) (z : Uᗮ) : ‖(x : E) + (z : E)‖^2 = ‖x‖^2 + ‖z‖^2 := by have ho : ⟪(x : E), (z : E)⟫ = 0 := (real_inner_comm _ _).trans ((Submodule.mem_orthogonal' U (z : E)).mp z.2 _ x.2) simp [norm_add_sq_real, ho] omit [FiniteDimensional ℝ E] [MeasurableSpace E] [BorelSpace E] in lemma normalDirection_pos_smul [FiniteDimensional ℝ E] [MeasurableSpace E] [BorelSpace E] {r : ℝ} (hr : 0 < r) (x : E) : normalDirection (r • x) = normalDirection x := by simp [normalDirection, norm_smul, Real.norm_eq_abs, abs_of_pos hr, _root_.mul_inv_rev, smul_smul, hr.ne'] lemma sphere_subspace_gaussian_integral (U : Submodule ℝ E) [Nontrivial E] {g : E → ℝ≥0∞} (hg : Measurable g) : ∫⁻ x, g x ∂uniformDirection E = ∫⁻ x : U, ∫⁻ z : Uᗮ, g (normalDirection ((x : E) + (z : E))) ∂stdGaussian Uᗮ ∂stdGaussian U := by rw [← gaussian_normalDirection, lintegral_map hg (by fun_prop), ← gaussian_subspace_sum U, lintegral_map (by fun_prop) (by fun_prop), lintegral_prod _ (by fun_prop)] lemma sphere_subspace_polar_integral (U : Submodule ℝ E) [Nontrivial E] [Nontrivial Uᗮ] {g : E → ℝ≥0∞} (hg : Measurable g) : ∫⁻ y, g y ∂uniformDirection E = ∫⁻ z : sphere (0 : Uᗮ) 1, ∫⁻ r : Ioi (0 : ℝ), ∫⁻ x : U, gaussianRadialDensity (Module.finrank ℝ U) ‖x‖ * g (normalDirection ((x : E) + r.1 • ((z : Uᗮ) : E))) ∂volume ∂gaussianPolarRadius Uᗮ ∂(volume : Measure Uᗮ).toSphere := by rw [sphere_subspace_gaussian_integral U hg] have hp (x : U) : (∫⁻ z : Uᗮ, g (normalDirection ((x : E) + (z : E))) ∂stdGaussian Uᗮ) = ∫⁻ z : sphere (0 : Uᗮ) 1, ∫⁻ r : Ioi (0 : ℝ), g (normalDirection ((x : E) + r.1 • ((z : Uᗮ) : E))) ∂gaussianPolarRadius Uᗮ ∂(volume : Measure Uᗮ).toSphere := by rw [← gaussian_polar_reconstruction (E := Uᗮ), lintegral_map (by fun_prop) (by fun_prop), lintegral_prod _ (by fun_prop)] rfl simp_rw [hp] rw [lintegral_lintegral_swap (by fun_prop)] apply lintegral_congr intro z rw [lintegral_lintegral_swap (by fun_prop)] apply lintegral_congr intro r rw [stdGaussian_density, lintegral_withDensity_eq_lintegral_mul _ (by fun_prop) (by fun_prop)] rfl lemma radial_subspace_integral (U : Submodule ℝ E) [Nontrivial E] [Nontrivial Uᗮ] (s : ℝ) : ∫⁻ r : Ioi (0 : ℝ), ENNReal.ofReal (r.1 ^ Module.finrank ℝ U) * gaussianRadialDensity (Module.finrank ℝ U) (r.1 * s) ∂gaussianPolarRadius Uᗮ = ENNReal.ofReal ((1 + s^2) ^ (-((Module.finrank ℝ E : ℕ) : ℝ) / 2)) * ((volume : Measure E).toSphere univ)⁻¹ := by rw [lintegral_gaussianPolarRadius (E := Uᗮ) (g := fun r : ℝ => ENNReal.ofReal (r ^ Module.finrank ℝ U) * gaussianRadialDensity (Module.finrank ℝ U) (r * s)) (by fun_prop)] calc _ = ∫⁻ r in Ioi (0 : ℝ), ENNReal.ofReal (r ^ (Module.finrank ℝ E - 1)) * ENNReal.ofReal ((2 * Real.pi) ^ (-((Module.finrank ℝ E : ℕ) : ℝ) / 2) * Real.exp (-(1 + s^2) * r^2 / 2)) := by apply setLIntegral_congr_fun measurableSet_Ioi intro r hr exact radial_power_gaussian_product Module.finrank_pos U.finrank_add_finrank_orthogonal hr.le s _ = _ := radial_gaussian_integral (by positivity) lemma scaled_subspace_integral (U : Submodule ℝ E) (z : sphere (0 : Uᗮ) 1) (r : Ioi (0 : ℝ)) {g : E → ℝ≥0∞} (hg : Measurable g) : ∫⁻ x : U, gaussianRadialDensity (Module.finrank ℝ U) ‖x‖ * g (normalDirection ((x : E) + r.1 • ((z : Uᗮ) : E))) = ∫⁻ x : U, ENNReal.ofReal (r.1 ^ Module.finrank ℝ U) * gaussianRadialDensity (Module.finrank ℝ U) (r.1 * ‖x‖) * g (normalDirection ((x : E) + ((z : Uᗮ) : E))) := by rw [lintegral_pos_smul r.2 (by fun_prop), ← lintegral_const_mul _ (by fun_prop)] apply lintegral_congr intro x rw [norm_smul, Real.norm_eq_abs, abs_of_pos r.2] have he : ((r.1 • x : U) : E) + r.1 • ((z : Uᗮ) : E) = r.1 • ((x : E) + ((z : Uᗮ) : E)) := by simp [smul_add] rw [he, normalDirection_pos_smul r.2, mul_assoc] lemma sphere_subspace_slope_integral (U : Submodule ℝ E) [Nontrivial E] [Nontrivial Uᗮ] {g : E → ℝ≥0∞} (hg : Measurable g) : ∫⁻ y, g y ∂uniformDirection E = ((volume : Measure E).toSphere univ)⁻¹ * ∫⁻ z : sphere (0 : Uᗮ) 1, ∫⁻ x : U, ENNReal.ofReal ((1 + ‖x‖^2) ^ (-((Module.finrank ℝ E : ℕ) : ℝ) / 2)) * g (normalDirection ((x : E) + ((z : Uᗮ) : E))) ∂volume ∂(volume : Measure Uᗮ).toSphere := by rw [sphere_subspace_polar_integral U hg, ← lintegral_const_mul _ (by fun_prop)] apply lintegral_congr intro z simp_rw [scaled_subspace_integral U z _ hg] rw [lintegral_lintegral_swap (by fun_prop), ← lintegral_const_mul _ (by fun_prop)] apply lintegral_congr intro x rw [lintegral_mul_const _ (by fun_prop), radial_subspace_integral] ac_rfl lemma normalDirection_ballStretch (U : Submodule ℝ E) {x : U} (hx : x ∈ ball (0 : U) 1) (z : sphere (0 : Uᗮ) 1) : normalDirection (((ballStretch x : U) : E) + ((z : Uᗮ) : E)) = (x : E) + Real.sqrt (1 - ‖x‖^2) • ((z : Uᗮ) : E) := by have hb := one_sub_norm_sq_pos hx have hs := Real.sqrt_pos.mpr hb have hz : ‖(z : Uᗮ)‖ = 1 := mem_sphere_zero_iff_norm.mp z.2 have hn : ‖(x : E) + Real.sqrt (1 - ‖x‖^2) • ((z : Uᗮ) : E)‖ = 1 := by have h := subspace_sum_norm_sq U x (Real.sqrt (1 - ‖x‖^2) • (z : Uᗮ)) simp only [Submodule.coe_smul_of_tower, norm_smul, Real.norm_eq_abs, abs_of_pos hs, hz, mul_one, Real.sq_sqrt hb.le] at h nlinarith [norm_nonneg ((x : E) + Real.sqrt (1 - ‖x‖^2) • ((z : Uᗮ) : E))] have he : ((ballStretch x : U) : E) + ((z : Uᗮ) : E) = (Real.sqrt (1 - ‖x‖^2))⁻¹ • ((x : E) + Real.sqrt (1 - ‖x‖^2) • ((z : Uᗮ) : E)) := by rw [ballStretch_eq_inv_sqrt hx] change (Real.sqrt (1 - ‖x‖^2))⁻¹ • (x : E) + ((z : Uᗮ) : E) = _ rw [smul_add, smul_smul, inv_mul_cancel₀ hs.ne', one_smul] rw [he, normalDirection_smul_sphere (inv_pos.mpr hs) hn] lemma sphere_subspace_surface_integral (U : Submodule ℝ E) [Nontrivial E] [Nontrivial Uᗮ] {g : E → ℝ≥0∞} (hg : Measurable g) : ∫⁻ y, g y ∂uniformDirection E = ((volume : Measure E).toSphere univ)⁻¹ * ∫⁻ x : U in ball 0 1, ENNReal.ofReal ((1 - ‖x‖^2) ^ ((((Module.finrank ℝ Uᗮ : ℕ) : ℝ) - 2) / 2)) * ∫⁻ z : sphere (0 : Uᗮ) 1, g ((x : E) + Real.sqrt (1 - ‖x‖^2) • ((z : Uᗮ) : E)) ∂(volume : Measure Uᗮ).toSphere := by rw [sphere_subspace_slope_integral U hg] congr 1 have ht (z : sphere (0 : Uᗮ) 1) : (∫⁻ x : U, ENNReal.ofReal ((1 + ‖x‖^2) ^ (-((Module.finrank ℝ E : ℕ) : ℝ) / 2)) * g (normalDirection ((x : E) + ((z : Uᗮ) : E)))) = ∫⁻ x : U in ball 0 1, ENNReal.ofReal ((1 - ‖x‖^2) ^ ((((Module.finrank ℝ Uᗮ : ℕ) : ℝ) - 2) / 2)) * g ((x : E) + Real.sqrt (1 - ‖x‖^2) • ((z : Uᗮ) : E)) := by rw [lintegral_ballStretch] apply setLIntegral_congr_fun measurableSet_ball intro x hx dsimp only rw [normalDirection_ballStretch U hx z, ← mul_assoc, ← U.finrank_add_finrank_orthogonal, ballStretch_weight hx] simp_rw [ht] rw [lintegral_lintegral_swap (by fun_prop)] apply setLIntegral_congr_fun measurableSet_ball intro x hx dsimp only rw [lintegral_const_mul _ (by fun_prop)] lemma surface_lintegral_eq_gaussian [Nontrivial E] {g : E → ℝ≥0∞} (hg : Measurable g) : ∫⁻ z : sphere (0 : E) 1, g (z : E) ∂(volume : Measure E).toSphere = (volume : Measure E).toSphere univ * ∫⁻ x, g (normalDirection x) ∂stdGaussian E := by have he : (∫⁻ x, g (normalDirection x) ∂stdGaussian E) = ∫⁻ x, g x ∂uniformDirection E := by rw [← gaussian_normalDirection, lintegral_map hg (by fun_prop)] rw [he, uniformDirection, lintegral_map hg (by fun_prop), lintegral_smul_measure] change _ = (volume : Measure E).toSphere univ * (((volume : Measure E).toSphere univ)⁻¹ * ∫⁻ z : sphere (0 : E) 1, g (z : E) ∂(volume : Measure E).toSphere) rw [← mul_assoc, ENNReal.mul_inv_cancel (Measure.measure_univ_ne_zero.mpr (Measure.toSphere_ne_zero volume)) (measure_ne_top _ _), one_mul] lemma sphere_subspace_gaussian_fiber_integral (U : Submodule ℝ E) [Nontrivial E] [Nontrivial Uᗮ] {g : E → ℝ≥0∞} (hg : Measurable g) : ∫⁻ y, g y ∂uniformDirection E = ((volume : Measure E).toSphere univ)⁻¹ * (volume : Measure Uᗮ).toSphere univ * ∫⁻ x : U in ball 0 1, ENNReal.ofReal ((1 - ‖x‖^2) ^ ((((Module.finrank ℝ Uᗮ : ℕ) : ℝ) - 2) / 2)) * ∫⁻ z : E, g ((x : E) + Real.sqrt (1 - ‖x‖^2) • normalDirection (Uᗮ.starProjection z)) ∂stdGaussian E := by rw [sphere_subspace_surface_integral U hg, mul_assoc] congr 1 rw [← lintegral_const_mul _ (by fun_prop)] apply setLIntegral_congr_fun measurableSet_ball intro x hx dsimp only rw [surface_lintegral_eq_gaussian (E := Uᗮ) (g := fun z => g ((x : E) + Real.sqrt (1 - ‖x‖^2) • (z : E))) (by fun_prop)] have he : (∫⁻ z : Uᗮ, g ((x : E) + Real.sqrt (1 - ‖x‖^2) • ((normalDirection z : Uᗮ) : E)) ∂stdGaussian Uᗮ) = ∫⁻ z : E, g ((x : E) + Real.sqrt (1 - ‖x‖^2) • normalDirection (Uᗮ.starProjection z)) ∂stdGaussian E := by rw [← gaussian_orthogonalProjectionOnto Uᗮ, lintegral_map (by fun_prop) (by fun_prop)] rfl rw [he] ac_rfl lemma sphere_area_ratio (U : Submodule ℝ E) [Nontrivial E] [Nontrivial Uᗮ] : ((volume : Measure E).toSphere univ)⁻¹ * (volume : Measure Uᗮ).toSphere univ = ENNReal.ofReal (Real.Gamma (((Module.finrank ℝ E : ℕ) : ℝ) / 2) / (Real.pi ^ (((Module.finrank ℝ U : ℕ) : ℝ) / 2) * Real.Gamma (((Module.finrank ℝ Uᗮ : ℕ) : ℝ) / 2))) := by have hd : 0 < ((Module.finrank ℝ E : ℕ) : ℝ) := Nat.cast_pos.mpr Module.finrank_pos have hq : 0 < ((Module.finrank ℝ Uᗮ : ℕ) : ℝ) := Nat.cast_pos.mpr Module.finrank_pos have he : ((Module.finrank ℝ E : ℕ) : ℝ) = ((Module.finrank ℝ U : ℕ) : ℝ) + ((Module.finrank ℝ Uᗮ : ℕ) : ℝ) := by exact_mod_cast U.finrank_add_finrank_orthogonal.symm have hp : Real.pi ^ (((Module.finrank ℝ E : ℕ) : ℝ) / 2) = Real.pi ^ (((Module.finrank ℝ U : ℕ) : ℝ) / 2) * Real.pi ^ (((Module.finrank ℝ Uᗮ : ℕ) : ℝ) / 2) := by rw [he, add_div, Real.rpow_add Real.pi_pos] rw [sphere_area, sphere_area, ← ENNReal.ofReal_inv_of_pos (by positivity), ← ENNReal.ofReal_mul (by positivity)] congr 1 rw [hp] field_simp end PosteriorReplicas end end OAI