import Mathlib import OAI.Probability.LogConcave.Model import OAI.Probability.LogConcave.Dynamics.SpaceTimeForward import OAI.Probability.LogConcave.Sampling.FDerivTimePartial namespace OAI section section noncomputable section open MeasureTheory Filter open scoped ENNReal NNReal Topology section UpperProof open MeasureTheory ProbabilityTheory Filter open scoped ENNReal NNReal RealInnerProductSpace Topology open Function MeasureTheory Set Filter open scoped Topology NNReal namespace LogConcaveSampling.GlobalODE open Set Function Filter open scoped Topology variable {E : Type*} [NormedAddCommGroup E] [NormedSpace ℝ E] def triangularEquiv (v : E) (J : E ≃L[ℝ] E) : (ℝ × E) ≃L[ℝ] (ℝ × E) where toLinearEquiv := { toFun := fun p => (p.1,p.1 •v+J p.2) invFun := fun p => (p.1,J.symm (p.2-p.1 •v)) left_inv := fun p => by simp right_inv := fun p => by simp map_add' := fun p q => by simp [add_smul]; abel map_smul' := fun c p => by simp [smul_add,smul_smul] } continuous_toFun := continuous_fst.prodMk ((continuous_fst.smul continuous_const).add (J.continuous.comp continuous_snd)) continuous_invFun := continuous_fst.prodMk (J.symm.continuous.comp (continuous_snd.sub (continuous_fst.smul continuous_const))) variable [CompleteSpace E] [ProperSpace E] {a b : ℝ} (hab : a≤b) {f : ℝ → E → E} (hc : Continuous (uncurry f)) {K : ℝ≥0} (hL : ∀ t∈Icc a b,LipschitzWith K (f t)) {D : ℝ → E → E →L[ℝ] E} (hD : Continuous (uncurry D)) (hf : ∀ t z,HasFDerivAt (f t) (D t z) z) omit [ProperSpace E] in lemma spaceTimeForward_time_deriv (p : ℝ × E) (hp : p.1∈Ioo a b) : HasDerivAt (fun t => (spaceTimeForward hab hc hL (t,p.2)).2) (f p.1 (spaceTimeForward hab hc hL p).2) p.1 := by have hpi : p.1∈Icc a b := ⟨hp.1.le,hp.2.le⟩ have hh := (flow_deriv hL hc ⟨a,le_rfl,hab⟩ p.2 p.1 hpi).hasDerivAt (Icc_mem_nhds hp.1 hp.2) have he : (spaceTimeForward hab hc hL p).2=flow hL hc ⟨a,le_rfl,hab⟩ p.2 p.1 := by simp [spaceTimeForward,projIcc_of_mem _ hpi] rw [he] apply hh.congr_of_eventuallyEq filter_upwards [Icc_mem_nhds hp.1 hp.2] with t ht simp [spaceTimeForward,projIcc_of_mem _ ht] theorem spaceTimeForward_hasFDerivAt (p : ℝ × E) (hp : p.1∈Ioo a b) : HasFDerivAt (spaceTimeForward hab hc hL) (triangularEquiv (f p.1 (spaceTimeForward hab hc hL p).2) (fundamentalEquiv (flowSpatialCoefficient_continuous hab hc hL hD p.2) (flowSpatialCoefficient_bound hab hc hL hf p.2) ⟨a,le_rfl,hab⟩ ⟨p.1,hp.1.le,hp.2.le⟩) : (ℝ × E) →L[ℝ] (ℝ × E)) p := by let J := flowLinearization hab hc hL hD hf p.2 p.1 have hsp : HasFDerivAt (fun z => (spaceTimeForward hab hc hL (p.1,z)).2) J p.2 := by simpa [spaceTimeForward,projIcc_of_mem _ ⟨hp.1.le,hp.2.le⟩] using flow_hasFDerivAt_left hab hc hL hD hf p.2 ⟨p.1,hp.1.le,hp.2.le⟩ have hg : Continuous (fun q : ℝ × E => f q.1 (spaceTimeForward hab hc hL q).2) := hc.comp (continuous_fst.prodMk (spaceTimeForward_continuous hab hc hL).snd) have hh := LogConcaveSampling.hasFDerivAt_of_time_partial (f:=fun t z => (spaceTimeForward hab hc hL (t,z)).2) (g:=fun t z => f t (spaceTimeForward hab hc hL (t,z)).2) (isOpen_Ioo.prod isOpen_univ) (show p∈Ioo a b ×ˢ (univ:Set E) from ⟨hp,mem_univ _⟩) hg.continuousAt (fun q hq => spaceTimeForward_time_deriv hab hc hL q hq.1) hsp convert! hasFDerivAt_fst.prodMk hh using 1 include hD hf in theorem spaceTimeBackward_differentiableAt (p : ℝ × E) (hp : p.1∈Ioo a b) : DifferentiableAt ℝ (spaceTimeBackward hab hc hL) p := by have hh := spaceTimeForward_hasFDerivAt hab hc hL hD hf (spaceTimeBackward hab hc hL p) hp exact (hh.of_local_left_inverse (spaceTimeBackward_continuous hab hc hL).continuousAt (Filter.Eventually.of_forall (spaceTime_right_inverse hab hc hL))).differentiableAt end LogConcaveSampling.GlobalODE namespace LogConcaveSampling.GlobalODE open Set Function Filter open scoped Topology variable {E : Type*} [NormedAddCommGroup E] [NormedSpace ℝ E] [CompleteSpace E] {a b : ℝ} (hab : a≤b) {f : ℝ → E → E} (hc : Continuous (uncurry f)) {K : ℝ≥0} (hL : ∀ t∈Icc a b,LipschitzWith K (f t)) def backwardTest (T : Icc a b) (φ : E → ℝ) (p : ℝ × E) : ℝ := φ (flow hL hc ⟨a,le_rfl,hab⟩ (spaceTimeBackward hab hc hL p).2 T) lemma backwardTest_eq (T : Icc a b) (φ : E → ℝ) (p : ℝ × E) : backwardTest hab hc hL T φ p=φ (flow hL hc (projIcc a b hab p.1) p.2 T) := by unfold backwardTest spaceTimeBackward rw [flow_cocycle hL hc (projIcc a b hab p.1) ⟨a,le_rfl,hab⟩ T] lemma backwardTest_continuous (T : Icc a b) {φ : E → ℝ} (hφ : Continuous φ) : Continuous (backwardTest hab hc hL T φ) := hφ.comp ((flow_lipschitz hL hc ⟨a,le_rfl,hab⟩ T).continuous.comp (spaceTimeBackward_continuous hab hc hL).snd) lemma backwardTest_lipschitz (T : Icc a b) {φ : E → ℝ} {L : ℝ≥0} (hφ : LipschitzWith L φ) (t : ℝ) : LipschitzWith (L*⟨Real.exp ((K:ℝ)*(b-a)),(Real.exp_pos _).le⟩) (fun z => backwardTest hab hc hL T φ (t,z)) := by simp only [backwardTest_eq] apply hφ.comp apply (flow_lipschitz hL hc (projIcc a b hab t) T).weaken change Real.exp _≤Real.exp _ apply Real.exp_le_exp.mpr apply mul_le_mul_of_nonneg_left _ K.coe_nonneg rw [abs_le] constructor <;> linarith [T.2.1,T.2.2,(projIcc a b hab t).2.1,(projIcc a b hab t).2.2] variable [ProperSpace E] {D : ℝ → E → E →L[ℝ] E} (hD : Continuous (uncurry D)) (hf : ∀ t z,HasFDerivAt (f t) (D t z) z) include hD hf in lemma backwardTest_differentiableAt (T : Icc a b) {φ : E → ℝ} (hφ : Differentiable ℝ φ) (p : ℝ × E) (hp : p.1∈Ioo a b) : DifferentiableAt ℝ (backwardTest hab hc hL T φ) p := (hφ _).comp p (((flow_contDiff_one_left hab hc hL hD hf T).differentiable (by norm_num) _).comp p (spaceTimeBackward_differentiableAt hab hc hL hD hf p hp).snd) include hD hf in theorem backwardTest_material_zero (T : Icc a b) {φ : E → ℝ} (hφ : Differentiable ℝ φ) (p : ℝ × E) (hp : p.1∈Ioo a b) : fderiv ℝ (backwardTest hab hc hL T φ) p (1,f p.1 p.2)=0 := by let B := spaceTimeBackward hab hc hL let G := fun z => φ (flow hL hc ⟨a,le_rfl,hab⟩ z T) have hG : Differentiable ℝ G := hφ.comp ((flow_contDiff_one_left hab hc hL hD hf T).differentiable (by norm_num)) have hv : f (B p).1 (spaceTimeForward hab hc hL (B p)).2=f p.1 p.2 := by rw [spaceTime_right_inverse hab hc hL p] rfl have hB := (spaceTimeForward_hasFDerivAt hab hc hL hD hf (B p) hp).of_local_left_inverse (spaceTimeBackward_continuous hab hc hL).continuousAt (Filter.Eventually.of_forall (spaceTime_right_inverse hab hc hL)) have hcomp := (hG (B p).2).hasFDerivAt.comp p hB.snd change (fderiv ℝ (G ∘ fun x => (spaceTimeBackward hab hc hL x).2) p) (1,f p.1 p.2)=0 rw [hcomp.fderiv] change (fderiv ℝ G (B p).2) _=0 have he : (((triangularEquiv (f (B p).1 (spaceTimeForward hab hc hL (B p)).2) (fundamentalEquiv (flowSpatialCoefficient_continuous hab hc hL hD (B p).2) (flowSpatialCoefficient_bound hab hc hL hf (B p).2) ⟨a,le_rfl,hab⟩ ⟨(B p).1,hp.1.le,hp.2.le⟩)).symm : (ℝ × E) →L[ℝ] (ℝ × E)) (1,f p.1 p.2)).2=0 := by change (fundamentalEquiv _ _ _ _).symm (f p.1 p.2-1 • _)=0 rw [hv,one_smul,sub_self,map_zero] convert! (congrArg (fderiv ℝ G (B p).2) he).trans (map_zero _) using 1 end LogConcaveSampling.GlobalODE namespace LogConcaveSampling.GlobalODE open Set Function Filter open scoped Topology variable {E : Type*} [NormedAddCommGroup E] [NormedSpace ℝ E] [CompleteSpace E] [ProperSpace E] {a b : ℝ} (hab : a≤b) {f : ℝ → E → E} (hc : Continuous (uncurry f)) {K : ℝ≥0} (hL : ∀ t∈Icc a b,LipschitzWith K (f t)) {D : ℝ → E → E →L[ℝ] E} (hD : Continuous (uncurry D)) (hf : ∀ t z,HasFDerivAt (f t) (D t z) z) include hD hf in theorem backwardTest_chain (T : Icc a b) {φ : E → ℝ} (hφ : Differentiable ℝ φ) {γ : ℝ → E} {γ' : E} {t : ℝ} (ht : t∈Ioo a b) (hγ : HasDerivAt γ γ' t) : HasDerivAt (fun s => backwardTest hab hc hL T φ (s,γ s)) (fderiv ℝ (fun z => backwardTest hab hc hL T φ (t,z)) (γ t) (γ'-f t (γ t))) t := by let A := fderiv ℝ (backwardTest hab hc hL T φ) (t,γ t) have hd := (backwardTest_differentiableAt hab hc hL hD hf T hφ (t,γ t) ht).hasFDerivAt have hsp := hd.comp (γ t) ((hasFDerivAt_const (𝕜:=ℝ) t (γ t)).prodMk (hasFDerivAt_id _)) have hchain := hd.comp_hasDerivAt t ((hasDerivAt_id t).prodMk hγ) have hzero : A (1,f t (γ t))=0 := backwardTest_material_zero hab hc hL hD hf T hφ _ ht have he : A (1,γ')=A (0,γ'-f t (γ t)) := by have hp : ((1:ℝ),γ')=(1,f t (γ t))+(0,γ'-f t (γ t)) := by ext <;> simp rw [hp,map_add,hzero,zero_add] convert! hchain using 1 change (fderiv ℝ (backwardTest hab hc hL T φ ∘ Prod.mk t) (γ t)) (γ'-f t (γ t))=A (1,γ') rw [hsp.fderiv] change A (0,γ'-f t (γ t))=A (1,γ') exact he.symm omit [ProperSpace E] in lemma backwardTest_spatial_derivative_bound (T : Icc a b) {φ : E → ℝ} {L : ℝ≥0} (hφ : LipschitzWith L φ) (t : ℝ) (z v : E) : ‖fderiv ℝ (fun z => backwardTest hab hc hL T φ (t,z)) z v‖≤ ((L:ℝ)*Real.exp ((K:ℝ)*(b-a)))*‖v‖ := by apply ((fderiv ℝ (fun z => backwardTest hab hc hL T φ (t,z)) z).le_opNorm v).trans apply mul_le_mul_of_nonneg_right _ (norm_nonneg _) exact norm_fderiv_le_of_lipschitz ℝ (backwardTest_lipschitz hab hc hL T hφ t) end LogConcaveSampling.GlobalODE namespace LogConcaveSampling.GlobalODE open Set Function Filter MeasureTheory open scoped Topology theorem transport_expectation_of_flux {d : ℕ} {Ω : Type*} [MeasurableSpace Ω] {μ : Measure Ω} [IsFiniteMeasure μ] {a b : ℝ} (hab : a X t ω)) (hXp : ∀ t∈Ioo a b,∀ ω,HasDerivAt (fun t => X t ω) (X' t ω) t) (hX'm : ∀ t,Measurable (X' t)) {g : Ω → ℝ} (hg : Integrable g μ) (hgb : ∀ t∈Ioo a b,∀ ω,‖X' t ω-f t (X t ω)‖≤g ω) (hflux : ∀ t∈Ioo a b,∀ S : Point d → Point d →L[ℝ] ℝ,Measurable S → (∃ C : ℝ,∀ y,‖S y‖≤C) → (∫ ω,S (X t ω) (X' t ω-f t (X t ω)) ∂μ)=0) {φ : Point d → ℝ} (hφ : Differentiable ℝ φ) {L : ℝ≥0} (hφL : LipschitzWith L φ) {B : ℝ} (hφB : ∀ z,‖φ z‖≤B) : (∫ ω,φ (X b ω) ∂μ) = ∫ ω,φ (flow hL hc ⟨a,le_rfl,hab.le⟩ (X a ω) b) ∂μ := by let T : Icc a b := ⟨b,hab.le,le_rfl⟩ let ψ := backwardTest hab.le hc hL T φ let S := fun t => fderiv ℝ (fun z => ψ (t,z)) let U := fun t ω => ψ (t,X t ω) let U' := fun t ω => S t (X t ω) (X' t ω-f t (X t ω)) let C : ℝ := (L:ℝ)*Real.exp ((K:ℝ)*(b-a)) have hC : 0≤C := by dsimp [C]; positivity have hSm : ∀ t,Measurable (S t) := fun t => measurable_fderiv ℝ _ have hSb : ∀ t y,‖S t y‖≤C := by intro t y exact norm_fderiv_le_of_lipschitz ℝ (backwardTest_lipschitz hab.le hc hL T hφL t) have hψ : Continuous ψ := backwardTest_continuous hab.le hc hL T hφ.continuous have hUm : ∀ t,Measurable (U t) := fun t => hψ.measurable.comp (measurable_const.prodMk (hXm t)) have hUb : ∀ t ω,‖U t ω‖≤B := by intro t ω; exact hφB _ have hU'm : ∀ t,Measurable (U' t) := by intro t exact (continuous_fst.clm_apply continuous_snd).measurable.comp (((hSm t).comp (hXm t)).prodMk ((hX'm t).sub (hc.measurable.comp (measurable_const.prodMk (hXm t))))) have hUi : ∀ t,Integrable (U t) μ := fun t => (integrable_const B).mono' (hUm t).aestronglyMeasurable (Eventually.of_forall (hUb t)) have hU'b : ∀ t∈Ioo a b,∀ ω,‖U' t ω‖≤C*g ω := by intro t ht ω exact ((S t (X t ω)).le_opNorm _).trans ((mul_le_mul_of_nonneg_right (hSb t _) (norm_nonneg _)).trans (mul_le_mul_of_nonneg_left (hgb t ht ω) hC)) have hUd : ∀ t∈Ioo a b,∀ ω,HasDerivAt (fun t => U t ω) (U' t ω) t := by intro t ht ω exact backwardTest_chain hab.le hc hL hD hf T hφ ht (hXp t ht ω) have hQd : ∀ t∈Ioo a b,HasDerivAt (fun t => ∫ ω,U t ω ∂μ) 0 t := by intro t ht have hh := hasDerivAt_integral_of_dominated_loc_of_deriv_le (F:=U) (F':=U') (Ioo_mem_nhds ht.1 ht.2) (Eventually.of_forall fun s => (hUm s).aestronglyMeasurable) (hUi t) (hU'm t).aestronglyMeasurable (Eventually.of_forall fun ω s hs => hU'b s hs ω) (hg.const_mul C) (Eventually.of_forall fun ω s hs => hUd s hs ω) have hz := hflux t ht (S t) (hSm t) ⟨C,hSb t⟩ change (∫ ω,U' t ω ∂μ)=0 at hz rw [hz] at hh exact hh.2 have hQc : Continuous (fun t => ∫ ω,U t ω ∂μ) := continuous_of_dominated (fun t => (hUm t).aestronglyMeasurable) (fun t => Eventually.of_forall (hUb t)) (integrable_const B) (Eventually.of_forall fun ω => hψ.comp (continuous_id.prodMk (hXc ω))) obtain ⟨t,ht,he⟩ := exists_deriv_eq_slope (fun t => ∫ ω,U t ω ∂μ) hab hQc.continuousOn (fun t ht => (hQd t ht).differentiableAt.differentiableWithinAt) rw [(hQd t ht).deriv] at he have heq : (∫ ω,U b ω ∂μ)=(∫ ω,U a ω ∂μ) := sub_eq_zero.mp ((div_eq_zero_iff).mp he.symm |>.resolve_right (sub_ne_zero.mpr hab.ne')) convert! heq using 1 <;> apply integral_congr_ae <;> filter_upwards [] with ω · simp only [U,ψ,backwardTest_eq] rw [projIcc_of_mem _ (show b∈Icc a b from ⟨hab.le,le_rfl⟩)] exact (congrArg φ (flow_initial hL hc ⟨b,hab.le,le_rfl⟩ (X b ω))).symm · simp only [U,ψ,backwardTest_eq] rw [projIcc_of_mem _ (show a∈Icc a b from ⟨le_rfl,hab.le⟩)] end LogConcaveSampling.GlobalODE end UpperProof end end end end OAI