import Mathlib import OAI.Analysis.HardSphere.Collisions.PartialPhysicalLaw import OAI.Analysis.HardSphere.Comparison.ExactFiberLikelihood import OAI.Analysis.HardSphere.Comparison.IntegratedFiber import OAI.Analysis.HardSphere.Comparison.LocalMoment namespace OAI section section noncomputable section open MeasureTheory ProbabilityTheory Set open scoped ENNReal namespace HardSphere.Comparison instance conditioned_finite {A : Type*} [MeasurableSpace A] (μ : Measure A) (S : Set A) : IsFiniteMeasure (conditionedMeasure μ S) := by constructor simp only [conditionedMeasure,Measure.smul_apply,smul_eq_mul,Measure.restrict_apply_univ] exact lt_of_le_of_lt (ENNReal.inv_mul_le_one _) (by norm_num) variable {I C : Type*} [Fintype I] [DecidableEq I] [Fintype C] {X : I→Type*} [∀i,MeasurableSpace (X i)] omit [Fintype I] in lemma lmarginal_updateFinset (μ : ∀i,Measure (X i)) (s : Finset I) (f : (∀i,X i)→ℝ≥0∞) (x : ∀i,X i) (u : ∀i:s,X i) : lmarginal μ s f (Function.updateFinset x s u)=lmarginal μ s f x := by apply lmarginal_congr intro i hi simp [Function.updateFinset,hi] lemma reference_resample (checks : C→LocalCheck X) (μ : ∀i,Measure (X i)) [∀i,IsProbabilityMeasure (μ i)] (s : Finset I) : (insertedLaw checks μ s (Finset.univ\s) (conditionedMeasure (Measure.pi μ) (retainedEvent checks Finset.univ))).map (fun p ↦ Function.updateFinset p.1 s p.2)= conditionedMeasure (Measure.pi μ) (retainedEvent checks Finset.univ) := by let w := checkWeight checks Finset.univ have hw : Measurable w := measurable_checkWeight checks _ have hs : s∪(Finset.univ\s)=Finset.univ := Finset.union_sdiff_of_subset (Finset.subset_univ s) let Z := (Measure.pi μ) (retainedEvent checks Finset.univ) have integral_ref (F : (∀i,X i)→ℝ≥0∞) : (∫⁻x,F x ∂conditionedMeasure (Measure.pi μ) (retainedEvent checks Finset.univ))= Z⁻¹*∫⁻x,w x*F x ∂Measure.pi μ := by rw [conditionedMeasure,lintegral_smul_measure,←lintegral_indicator (measurableSet_retainedEvent checks _)] congr 1 apply lintegral_congr intro x by_cases hx : x∈retainedEvent checks Finset.univ <;> simp [w,checkWeight,hx] apply Measure.ext_of_lintegral intro F hF rw [lintegral_map hF measurable_updateFinset',insertedLaw, Measure.lintegral_compProd (show Measurable (fun p : (∀i,X i)×(∀i:s,X i) ↦ F (Function.updateFinset p.1 s p.2)) from hF.comp (measurable_updateFinset' (s:=s)))] have hkernel (x : ∀i,X i) : (∫⁻u,F (Function.updateFinset x s u) ∂fiberNormalize (Kernel.const _ (Measure.pi fun i:s ↦ μ i)) (insertionFiberEvent checks s (Finset.univ\s)) x)= (lmarginal μ s w x)⁻¹*lmarginal μ s (fun y ↦ w y*F y) x := by have he : fiberNormalize (Kernel.const _ (Measure.pi fun i:s ↦ μ i)) (insertionFiberEvent checks s (Finset.univ\s)) x= referenceFiber checks μ s (Finset.univ\s) x := by change (fiberRestrict _ _ x univ)⁻¹ • fiberRestrict _ _ x=_ rw [fiberRestrict_apply _ (insertionFiberEvent_measurable checks s (Finset.univ\s))] simp only [Kernel.const_apply,Measure.restrict_apply_univ,referenceFiber,conditionedMeasure] rfl rw [he,referenceFiber,fiber_lintegral] simp only [hs,partialNormalizer] congr 1 apply lintegral_congr intro u exact mul_comm _ _ simp_rw [hkernel] rw [integral_ref,integral_ref] congr 1 have hA : Measurable (lmarginal μ s w) := hw.lmarginal μ have hB : Measurable (lmarginal μ s (fun y ↦ w y*F y)) := (hw.mul hF).lmarginal μ apply lintegral_eq_of_lmarginal_eq s (hw.mul (hA.inv.mul hB)) (hw.mul hF) funext x change lmarginal μ s (fun y ↦ w y*((lmarginal μ s w y)⁻¹*lmarginal μ s (fun z ↦ w z*F z) y)) x = lmarginal μ s (fun y ↦ w y*F y) x have hfin : lmarginal μ s w x≠⊤ := by apply ne_top_of_le_ne_top ENNReal.one_ne_top exact (lmarginal_mono (checkWeight_le_one checks _) x).trans_eq (by simp [lmarginal]) have he : (fun u : ∀i:s,X i ↦ w (Function.updateFinset x s u)* ((lmarginal μ s w (Function.updateFinset x s u))⁻¹* lmarginal μ s (fun y ↦ w y*F y) (Function.updateFinset x s u)))= (fun u ↦ w (Function.updateFinset x s u)*((lmarginal μ s w x)⁻¹* lmarginal μ s (fun y ↦ w y*F y) x)) := by funext u simp only [lmarginal_updateFinset] change (∫⁻u,w (Function.updateFinset x s u)* ((lmarginal μ s w (Function.updateFinset x s u))⁻¹* lmarginal μ s (fun y ↦ w y*F y) (Function.updateFinset x s u)) ∂Measure.pi (fun i:s ↦ μ i))=_ rw [he,lintegral_mul_const _ ((show Measurable (fun u : ∀i:s,X i ↦ w (Function.updateFinset x s u)) from hw.comp (measurable_updateFinset (s:=s) (x:=x))))] change lmarginal μ s w x*((lmarginal μ s w x)⁻¹*lmarginal μ s (fun y ↦ w y*F y) x)=_ by_cases hz : lmarginal μ s w x=0 · have hwz : ∀ᵐu ∂Measure.pi (fun i:s ↦ μ i), w (Function.updateFinset x s u)=0 := (lintegral_eq_zero_iff' ((show Measurable (fun u : ∀i:s,X i ↦ w (Function.updateFinset x s u)) from hw.comp (measurable_updateFinset (s:=s) (x:=x)))).aemeasurable).mp hz have hb : lmarginal μ s (fun y ↦ w y*F y) x=0 := lintegral_eq_zero_of_ae_eq_zero (hwz.mono fun u hu ↦ by simp [hu]) rw [hz,hb,mul_zero,mul_zero] · rw [←mul_assoc,ENNReal.mul_inv_cancel hz hfin,one_mul] lemma reference_fiber_positive (checks : C→LocalCheck X) (μ : ∀i,Measure (X i)) [∀i,IsProbabilityMeasure (μ i)] (s : Finset I) : ∀ᵐx ∂conditionedMeasure (Measure.pi μ) (retainedEvent checks Finset.univ), 0