import OAI.Probability.PosteriorReplicas.Measures.BaseDisintegration namespace OAI open MeasureTheory ProbabilityTheory Set Matrix InnerProductSpace Metric EuclideanGeometry open scoped ENNReal NNReal RealInnerProductSpace Topology noncomputable section namespace PosteriorReplicas def baseProjectionConstant (base : BaseLaw) (d k : ℕ) : ℝ := match base with | .gaussian => (2 * Real.pi) ^ (-(k : ℝ) / 2) | .spherical => Real.Gamma ((d : ℝ) / 2) / (Real.pi ^ ((k : ℝ) / 2) * Real.Gamma (((d : ℝ) - k) / 2)) lemma baseProjectionConstant_pos {d k : ℕ} (hk : k < d) (base : BaseLaw) : 0 < baseProjectionConstant base d k := by have hd : 0 < (d : ℝ) := by exact_mod_cast (by omega : 0 < d) have hdk : 0 < (d : ℝ) - k := sub_pos.mpr (by exact_mod_cast hk) cases base <;> unfold baseProjectionConstant <;> positivity lemma baseDensity_le_sup {d k : ℕ} (hk : k + 2 ≤ d) (base : BaseLaw) (A : Rows d k) (y : Labels k) : baseDensity base A y ≤ ENNReal.ofReal (baseProjectionConstant base d k / Real.sqrt (rowGram A).det) := by have hC := (baseProjectionConstant_pos (by omega : k < d) base).le by_cases hA : LinearIndependent ℝ A · rw [baseDensity.eq_def, ite_eq_left hA] cases base · dsimp only [baseProjectionConstant] at hC ⊢ split_ifs with hy · apply ENNReal.ofReal_le_ofReal apply div_le_div_of_nonneg_right _ (Real.sqrt_nonneg _) have hb : 0 ≤ 1 - ‖liftLabel A y‖^2 := by nlinarith [norm_nonneg (liftLabel A y)] have he : 0 ≤ ((d : ℝ) - k - 2) / 2 := by have : (k : ℝ) + 2 ≤ d := by exact_mod_cast hk linarith exact mul_le_of_le_one_right hC (Real.rpow_le_one hb (by nlinarith [sq_nonneg ‖liftLabel A y‖]) he) · exact bot_le · dsimp only [baseProjectionConstant] at hC ⊢ apply ENNReal.ofReal_le_ofReal apply div_le_div_of_nonneg_right _ (Real.sqrt_nonneg _) exact mul_le_of_le_one_right hC (Real.exp_le_one_iff.mpr (div_nonpos_of_nonpos_of_nonneg (neg_nonpos.mpr (sq_nonneg _)) (by norm_num))) · rw [baseDensity.eq_def, ite_eq_right hA] exact bot_le lemma baseDensity_le_orthonormal {d k : ℕ} (hk : k + 2 ≤ d) (base : BaseLaw) {A : Rows d k} (hA : Orthonormal ℝ A) (y : Labels k) : baseDensity base A y ≤ ENNReal.ofReal (baseProjectionConstant base d k) := by simpa only [rowGram_eq_gram, Matrix.gram_eq_one_iff_orthonormal.mpr hA, Matrix.det_one, Real.sqrt_one, div_one] using baseDensity_le_sup hk base A y lemma projected_base_measure {d k : ℕ} {A : Rows d k} (hA : LinearIndependent ℝ A) (hk : k < d) (base : BaseLaw) : (base.measure d).map (labels A) = volume.withDensity (baseDensity base A) := by have h := projected_measure hA hk base (f := fun _ => 1) measurable_const have hμ : (base.measure d).withDensity (fun _ => 1) = base.measure d := by exact withDensity_one have hp : projectedDensity base (fun _ => 1) A = baseDensity base A := by funext y simp only [projectedDensity, tiltMass, lintegral_one, measure_univ, mul_one] rw [hμ, hp] at h exact h lemma orthonormal_in_orthogonal {d q : ℕ} (U : Submodule ℝ (Vec d)) (hq : q ≤ Module.finrank ℝ Uᗮ) : ∃ v : Fin q → Vec d, Orthonormal ℝ v ∧ ∀ i, v i ∈ Uᗮ := by let b := stdOrthonormalBasis ℝ Uᗮ let v : Fin q → Uᗮ := fun i => b (Fin.castLE hq i) refine ⟨fun i => (v i : Vec d), ?_, fun i => (v i).2⟩ exact (b.orthonormal.comp (Fin.castLE hq) (Fin.castLE_injective hq)).comp_linearIsometry Uᗮ.subtypeₗᵢ lemma pairing_le_infDist {d : ℕ} (L : AffineSubspace ℝ (Vec d)) [Nonempty L] {p v : Vec d} (hp : p ∈ L) (hv : v ∈ L.directionᗮ) (hvnorm : ‖v‖ = 1) (x : Vec d) : |⟪v, x - p⟫| ≤ Metric.infDist x L := by have ho : ⟪v, (orthogonalProjection L x : Vec d) - p⟫ = 0 := Submodule.inner_left_of_mem_orthogonal (L.vsub_mem_direction (orthogonalProjection_mem x) hp) hv have he : ⟪v, x - p⟫ = ⟪v, x - (orthogonalProjection L x : Vec d)⟫ := by rw [inner_sub_right] at ho simp only [inner_sub_right] linarith rw [he, ← dist_orthogonalProjection_eq_infDist, dist_eq_norm] simpa only [hvnorm, one_mul] using abs_real_inner_le_norm v (x - (orthogonalProjection L x : Vec d)) lemma base_affine_tube {d q : ℕ} (hq : q + 2 ≤ d) (base : BaseLaw) (L : AffineSubspace ℝ (Vec d)) [Nonempty L] (hL : Module.finrank ℝ L.direction + q ≤ d) {r : ℝ} (hr : 0 < r) : (base.measure d) {x | Metric.infDist x L ≤ r} ≤ (ENNReal.ofReal (baseProjectionConstant base d q) * 2^q) * ENNReal.ofReal (r^q) := by have hdim : q ≤ Module.finrank ℝ L.directionᗮ := by have he := L.direction.finrank_add_finrank_orthogonal simp only [finrank_euclideanSpace_fin] at he omega obtain ⟨v, hv, hvin⟩ := orthonormal_in_orthogonal L.direction hdim obtain ⟨p, hp⟩ := Set.nonempty_coe_sort.mp (inferInstance : Nonempty L) let c := labels v p let s : Set (Labels q) := Set.pi univ (fun i => Icc (c i - r) (c i + r)) have hs : MeasurableSet s := MeasurableSet.univ_pi (fun i => measurableSet_Icc) have hsub : {x | Metric.infDist x L ≤ r} ⊆ (labels v) ⁻¹' s := by intro x hx simp only [Set.mem_preimage, s, Set.mem_pi, Set.mem_univ, true_implies, Set.mem_Icc] intro i have hi := (pairing_le_infDist L hp (hvin i) (hv.norm_eq_one i) x).trans hx rw [inner_sub_right, abs_le] at hi exact ⟨by dsimp [c, labels]; linarith [hi.1], by dsimp [c, labels]; linarith [hi.2]⟩ calc _ ≤ (base.measure d) ((labels v) ⁻¹' s) := measure_mono hsub _ = ((base.measure d).map (labels v)) s := (Measure.map_apply (by unfold labels; fun_prop) hs).symm _ = ∫⁻ y in s, baseDensity base v y := by rw [projected_base_measure hv.linearIndependent (by omega) base, withDensity_apply _ hs] _ ≤ ∫⁻ _y in s, ENNReal.ofReal (baseProjectionConstant base d q) := setLIntegral_mono' hs (fun y _ => baseDensity_le_orthonormal hq base hv y) _ = ENNReal.ofReal (baseProjectionConstant base d q) * ENNReal.ofReal (2*r)^q := by rw [lintegral_const, Measure.restrict_apply_univ] congr 1 change (Measure.pi (fun _ : Fin q => volume)) s = _ rw [Measure.pi_pi] simp only [Real.volume_Icc, show ∀ i, c i + r - (c i - r) = 2*r from fun _ => by ring] simp _ = _ := by rw [ENNReal.ofReal_mul (by norm_num), mul_pow, ENNReal.ofReal_ofNat, ENNReal.ofReal_pow hr.le, mul_assoc] end PosteriorReplicas end end OAI