import Mathlib namespace OAI open MeasureTheory ProbabilityTheory Set open scoped ENNReal NNReal noncomputable section namespace PosteriorReplicas lemma map_withDensity_comp {α β : Type*} [MeasurableSpace α] [MeasurableSpace β] (μ : Measure α) {T : α → β} (hT : Measurable T) {f : β → ℝ≥0∞} (hf : Measurable f) : (μ.withDensity (f ∘ T)).map T = (μ.map T).withDensity f := by apply Measure.ext_of_lintegral intro g hg rw [lintegral_map hg hT, lintegral_withDensity_eq_lintegral_mul _ (hf.comp hT) (show Measurable (fun a => g (T a)) from hg.comp hT), lintegral_withDensity_eq_lintegral_mul _ hf hg, lintegral_map (hf.mul hg) hT] rfl lemma map_withDensity_equiv {α β : Type*} [MeasurableSpace α] [MeasurableSpace β] (μ : Measure α) (e : α ≃ᵐ β) {f : α → ℝ≥0∞} (hf : Measurable f) : (μ.withDensity f).map e = (μ.map e).withDensity (fun y => f (e.symm y)) := by apply Measure.ext_of_lintegral intro g hg rw [lintegral_map hg e.measurable, lintegral_withDensity_eq_lintegral_mul _ hf (show Measurable (fun a => g (e a)) from hg.comp e.measurable), lintegral_withDensity_eq_lintegral_mul _ (show Measurable (fun y => f (e.symm y)) from hf.comp e.symm.measurable) hg] simp only [Pi.mul_apply] rw [lintegral_map (show Measurable (fun y => f (e.symm y) * g y) from (hf.comp e.symm.measurable).mul hg) e.measurable] simp lemma lintegral_fin_prod {n : ℕ} {E : Fin n → Type*} [∀ i, MeasurableSpace (E i)] (μ : (i : Fin n) → Measure (E i)) [∀ i, SigmaFinite (μ i)] (f : (i : Fin n) → E i → ℝ≥0∞) (hf : ∀ i, Measurable (f i)) : ∫⁻ x, ∏ i, f i (x i) ∂Measure.pi μ = ∏ i, ∫⁻ x, f i x ∂μ i := by induction n with | zero => simp | succ n ih => calc _ = ∫⁻ x : E 0 × ((i : Fin n) → E i.succ), f 0 x.1 * ∏ i, f i.succ (x.2 i) ∂((μ 0).prod (Measure.pi (fun i => μ i.succ))) := by rw [← ((measurePreserving_piFinSuccAbove μ 0).symm).lintegral_comp (by exact Finset.measurable_prod _ (fun i _ => (hf i).comp (measurable_pi_apply i)))] simp only [Fin.prod_univ_succ, Fin.zero_succAbove] rfl _ = (∫⁻ x, f 0 x ∂μ 0) * ∏ i : Fin n, ∫⁻ x, f i.succ x ∂μ i.succ := by have hg : Measurable (fun x : (i : Fin n) → E i.succ => ∏ i, f i.succ (x i)) := Finset.measurable_prod _ (fun i _ => (hf i.succ).comp (measurable_pi_apply i)) rw [lintegral_prod_mul (hf 0).aemeasurable hg.aemeasurable, ih _ _ (fun i => hf i.succ)] _ = _ := by rw [Fin.prod_univ_succ] lemma lintegral_finite_prod {ι : Type*} [Fintype ι] {E : ι → Type*} [∀ i, MeasurableSpace (E i)] (μ : (i : ι) → Measure (E i)) [∀ i, SigmaFinite (μ i)] (f : (i : ι) → E i → ℝ≥0∞) (hf : ∀ i, Measurable (f i)) : ∫⁻ x, ∏ i, f i (x i) ∂Measure.pi μ = ∏ i, ∫⁻ x, f i x ∂μ i := by let e := (Fintype.equivFin ι).symm rw [← (measurePreserving_piCongrLeft _ e).lintegral_comp (show Measurable (fun x => ∏ i, f i (x i)) from Finset.measurable_prod _ (fun i _ => (hf i).comp (measurable_pi_apply i)))] simp_rw [← e.prod_comp, MeasurableEquiv.coe_piCongrLeft, Equiv.piCongrLeft_apply_apply] exact lintegral_fin_prod _ _ (fun i => hf (e i)) lemma pi_withDensity {ι : Type*} [Fintype ι] {X : ι → Type*} [∀ i, MeasurableSpace (X i)] (μ : ∀ i, Measure (X i)) [∀ i, SigmaFinite (μ i)] (f : ∀ i, X i → ℝ≥0∞) (hf : ∀ i, Measurable (f i)) [∀ i, SigmaFinite ((μ i).withDensity (f i))] : Measure.pi (fun i => (μ i).withDensity (f i)) = (Measure.pi μ).withDensity (fun x => ∏ i, f i (x i)) := by apply Measure.pi_eq intro s hs rw [withDensity_apply _ (MeasurableSet.univ_pi hs), Measure.restrict_pi_pi] rw [lintegral_finite_prod _ _ hf] congr 1 ext i rw [withDensity_apply _ (hs i)] lemma pi_gaussian_density (n : ℕ) : Measure.pi (fun _ : Fin n => gaussianReal 0 1) = (volume : Measure (Fin n → ℝ)).withDensity (fun x => ∏ i, gaussianPDF 0 1 (x i)) := by have h : gaussianReal 0 1 = (volume : Measure ℝ).withDensity (gaussianPDF 0 1) := gaussianReal_of_var_ne_zero _ (by norm_num) have : IsProbabilityMeasure ((volume : Measure ℝ).withDensity (gaussianPDF 0 1)) := by rw [← h] infer_instance rw [h] exact pi_withDensity _ _ (fun _ => measurable_gaussianPDF _ _) end PosteriorReplicas end end OAI