import OAI.Probability.FreeIsing.Gaussian.PiIntegrationByParts import OAI.Probability.FreeIsing.Posterior.GaussianGradient import OAI.Probability.FreeIsing.Posterior.GaussianTesting namespace OAI noncomputable section open MeasureTheory ProbabilityTheory open scoped NNReal namespace Problem367.FinitePosterior variable {S V : Type*} [Fintype S] [Nonempty S] [Fintype V] [DecidableEq V] theorem integral_fieldSquareGradient_mul_noise (energy : S → ℝ) (spin : S → V → ℝ) (observable : S → ℝ) (A : ℝ) (hbound : ∀ s, |observable s| ≤ A) (hcoords : ∀ s v, spin s v = -1 ∨ spin s v = 1) (signal : V → ℝ) (t : ℝ) (v : V) : (∫ z, fieldSquareGradient energy spin observable (gaussianObservationCurve signal z t) v * z v ∂Measure.pi (fun _ : V => gaussianReal 0 1)) = Real.sqrt t * ∫ z, fieldSquareHessianDiagonal energy spin observable (gaussianObservationCurve signal z t) v ∂Measure.pi (fun _ : V => gaussianReal 0 1) := by have h := Problem367.gaussian_pi_integral_mul_affine_of_bounded_deriv (fun j => t * signal j) (fun _ => Real.sqrt t) v (continuous_fieldSquareGradient energy spin observable v) (continuous_fieldSquareHessianDiagonal energy spin observable v) (fun z r => hasDerivAt_fieldSquareGradient_update energy spin observable z v r (fun s => hcoords s v)) (fun z => abs_fieldSquareGradient_le energy spin observable z v A (fun s => hcoords s v) hbound) (fun z => abs_fieldSquareHessianDiagonal_le energy spin observable z v A (fun s => hcoords s v) hbound) simpa only [gaussianObservationCurve, Pi.add_def, Pi.smul_def, smul_eq_mul, mul_comm] using h section Planted variable [MeasurableSpace S] [MeasurableSingletonClass S] def finiteStandardObservation (spin : S → V → ℝ) (t : ℝ) (x : S × (V → ℝ)) : V → ℝ := gaussianObservationCurve (spin x.1) x.2 t omit [Nonempty S] [Fintype V] [DecidableEq V] in lemma measurable_finiteStandardObservation (spin : S → V → ℝ) (t : ℝ) : Measurable (finiteStandardObservation spin t) := by unfold finiteStandardObservation gaussianObservationCurve exact ((measurable_of_finite spin).comp measurable_fst).const_smul t |>.add (measurable_snd.const_smul (Real.sqrt t)) omit [DecidableEq V] in private theorem integrable_bounded_test {Ω : Type*} [MeasurableSpace Ω] (μ : Measure Ω) [IsFiniteMeasure μ] (X : Ω → V → ℝ) (hX : Measurable X) (f : (V → ℝ) → ℝ) (hf : Continuous f) (C : ℝ) (hbound : ∀ z, |f z| ≤ C) : Integrable (fun x => f (X x)) μ := by apply Integrable.of_bound (hf.measurable.comp hX).aestronglyMeasurable C exact Filter.Eventually.of_forall fun x => by simpa only [Real.norm_eq_abs, Function.comp_def] using hbound (X x) omit [Fintype S] [Nonempty S] [DecidableEq V] [MeasurableSingletonClass S] in theorem integrable_finiteStandardNoise (μ : Measure S) [IsFiniteMeasure μ] (v : V) : Integrable (fun x : S × (V → ℝ) => x.2 v) (μ.prod (Measure.pi (fun _ : V => gaussianReal 0 1))) := by have h := (measurePreserving_eval (fun _ : V => gaussianReal 0 1) v).integrable_comp_of_integrable ((memLp_id_gaussianReal 1).integrable (by norm_num)) exact h.comp_snd μ omit [DecidableEq V] in theorem integrable_finiteStandardGradient (μ : Measure S) [IsFiniteMeasure μ] (energy : S → ℝ) (spin : S → V → ℝ) (observable : S → ℝ) (A : ℝ) (hbound : ∀ s, |observable s| ≤ A) (hcoords : ∀ s v, spin s v = -1 ∨ spin s v = 1) (t : ℝ) (v : V) : Integrable (fun x => fieldSquareGradient energy spin observable (finiteStandardObservation spin t x) v) (μ.prod (Measure.pi (fun _ : V => gaussianReal 0 1))) := integrable_bounded_test _ _ (measurable_finiteStandardObservation spin t) _ (continuous_fieldSquareGradient energy spin observable v) (4 * A ^ 2) (fun z => abs_fieldSquareGradient_le energy spin observable z v A (fun s => hcoords s v) hbound) theorem integrable_finiteStandardHessian (μ : Measure S) [IsFiniteMeasure μ] (energy : S → ℝ) (spin : S → V → ℝ) (observable : S → ℝ) (A : ℝ) (hbound : ∀ s, |observable s| ≤ A) (hcoords : ∀ s v, spin s v = -1 ∨ spin s v = 1) (t : ℝ) (v : V) : Integrable (fun x => fieldSquareHessianDiagonal energy spin observable (finiteStandardObservation spin t x) v) (μ.prod (Measure.pi (fun _ : V => gaussianReal 0 1))) := integrable_bounded_test _ _ (measurable_finiteStandardObservation spin t) _ (continuous_fieldSquareHessianDiagonal energy spin observable v) (16 * A ^ 2) (fun z => abs_fieldSquareHessianDiagonal_le energy spin observable z v A (fun s => hcoords s v) hbound) omit [DecidableEq V] in theorem integrable_finiteStandardGradient_mul_noise (μ : Measure S) [IsFiniteMeasure μ] (energy : S → ℝ) (spin : S → V → ℝ) (observable : S → ℝ) (A : ℝ) (hbound : ∀ s, |observable s| ≤ A) (hcoords : ∀ s v, spin s v = -1 ∨ spin s v = 1) (t : ℝ) (v : V) : Integrable (fun x => fieldSquareGradient energy spin observable (finiteStandardObservation spin t x) v * x.2 v) (μ.prod (Measure.pi (fun _ : V => gaussianReal 0 1))) := by have hg := integrable_finiteStandardGradient μ energy spin observable A hbound hcoords t v have h := (integrable_finiteStandardNoise μ v).mul_bdd hg.aestronglyMeasurable (Filter.Eventually.of_forall fun x => by simpa only [Real.norm_eq_abs, Function.comp_def] using abs_fieldSquareGradient_le energy spin observable (finiteStandardObservation spin t x) v A (fun s => hcoords s v) hbound) simpa only [mul_comm] using h theorem integral_finiteStandardGradient_mul_noise (μ : Measure S) [IsFiniteMeasure μ] (energy : S → ℝ) (spin : S → V → ℝ) (observable : S → ℝ) (A : ℝ) (hbound : ∀ s, |observable s| ≤ A) (hcoords : ∀ s v, spin s v = -1 ∨ spin s v = 1) (t : ℝ) (v : V) : (∫ x, fieldSquareGradient energy spin observable (finiteStandardObservation spin t x) v * x.2 v ∂μ.prod (Measure.pi (fun _ : V => gaussianReal 0 1))) = Real.sqrt t * ∫ x, fieldSquareHessianDiagonal energy spin observable (finiteStandardObservation spin t x) v ∂μ.prod (Measure.pi (fun _ : V => gaussianReal 0 1)) := by rw [integral_prod _ (integrable_finiteStandardGradient_mul_noise μ energy spin observable A hbound hcoords t v), integral_prod _ (integrable_finiteStandardHessian μ energy spin observable A hbound hcoords t v), ← integral_const_mul] apply integral_congr_ae exact Filter.Eventually.of_forall fun s => integral_fieldSquareGradient_mul_noise energy spin observable A hbound hcoords (spin s) t v theorem hasDerivAt_finiteStandardEnergy_of_posterior_test (μ : Measure S) [IsProbabilityMeasure μ] (energy : S → ℝ) (spin : S → V → ℝ) (observable : S → ℝ) (A : ℝ) (hbound : ∀ s, |observable s| ≤ A) (hcoords : ∀ s v, spin s v = -1 ∨ spin s v = 1) {t : ℝ} (ht : 0 < t) (hposterior : ∀ v, (∫ x, fieldSquareGradient energy spin observable (finiteStandardObservation spin t x) v * spin x.1 v ∂μ.prod (Measure.pi (fun _ : V => gaussianReal 0 1))) = ∫ x, fieldSquareGradient energy spin observable (finiteStandardObservation spin t x) v * fieldMean energy spin (fun s => spin s v) (finiteStandardObservation spin t x) ∂μ.prod (Measure.pi (fun _ : V => gaussianReal 0 1))) : HasDerivAt (fun r => ∫ x, fieldMean energy spin observable (finiteStandardObservation spin r x) ^ 2 ∂μ.prod (Measure.pi (fun _ : V => gaussianReal 0 1))) (∫ x, ∑ v, fieldCovariance energy spin observable (finiteStandardObservation spin t x) v ^ 2 ∂μ.prod (Measure.pi (fun _ : V => gaussianReal 0 1))) t := by let ρ := μ.prod (Measure.pi (fun _ : V => gaussianReal 0 1)) let Y := finiteStandardObservation spin t let g := fun x v => fieldSquareGradient energy spin observable (Y x) v let H := fun x v => fieldSquareHessianDiagonal energy spin observable (Y x) v let p := fun x v => fieldMean energy spin (fun s => spin s v) (Y x) let c := 1 / (2 * Real.sqrt t) have hy : Measurable Y := measurable_finiteStandardObservation spin t have hg (v : V) : Integrable (fun x => g x v) ρ := integrable_finiteStandardGradient μ energy spin observable A hbound hcoords t v have hH (v : V) : Integrable (fun x => H x v) ρ := integrable_finiteStandardHessian μ energy spin observable A hbound hcoords t v have hn (v : V) : Integrable (fun x => g x v * x.2 v) ρ := integrable_finiteStandardGradient_mul_noise μ energy spin observable A hbound hcoords t v have hs (v : V) : Integrable (fun x => g x v * spin x.1 v) ρ := by apply (hg v).mul_bdd (((measurable_of_finite (fun s => spin s v)).comp measurable_fst).aestronglyMeasurable) exact Filter.Eventually.of_forall fun x => by simpa only [Real.norm_eq_abs, Function.comp_def] using (abs_spin_eq_one (fun s => spin s v) (fun s => hcoords s v) x.1).le have hp (v : V) : Integrable (fun x => g x v * p x v) ρ := by apply (hg v).mul_bdd (((continuous_fieldMean energy spin (fun s => spin s v)).measurable.comp hy).aestronglyMeasurable) exact Filter.Eventually.of_forall fun x => by simpa only [Real.norm_eq_abs, Function.comp_def] using abs_fieldMean_le energy spin (fun s => spin s v) (Y x) 1 (fun s => (abs_spin_eq_one (fun s => spin s v) (fun s => hcoords s v) s).le) have hvEq (v : V) : (fun x : S × (V → ℝ) => g x v * gaussianObservationVelocity (spin x.1) x.2 t v) = (fun x => g x v * spin x.1 v + c * (g x v * x.2 v)) := by funext x change g x v * (spin x.1 v + c * x.2 v) = _ ring have hv (v : V) : Integrable (fun x => g x v * gaussianObservationVelocity (spin x.1) x.2 t v) ρ := by rw [hvEq] exact (hs v).add ((hn v).const_mul c) have hcancel (x : S × (V → ℝ)) (v : V) : g x v * p x v + (1 / 2 : ℝ) * H x v = fieldCovariance energy spin observable (Y x) v ^ 2 := by dsimp [g, H, p, fieldSquareGradient, fieldSquareHessianDiagonal] ring have hc (v : V) : Integrable (fun x => fieldCovariance energy spin observable (Y x) v ^ 2) ρ := by exact ((hp v).add ((hH v).const_mul (1 / 2 : ℝ))).congr (Filter.Eventually.of_forall fun x => hcancel x v) have hstep (v : V) : (∫ x, g x v * gaussianObservationVelocity (spin x.1) x.2 t v ∂ρ) = ∫ x, fieldCovariance energy spin observable (Y x) v ^ 2 ∂ρ := by rw [hvEq, integral_add (hs v) ((hn v).const_mul c), integral_const_mul] have hnoise : (∫ x, g x v * x.2 v ∂ρ) = Real.sqrt t * ∫ x, H x v ∂ρ := integral_finiteStandardGradient_mul_noise μ energy spin observable A hbound hcoords t v rw [hnoise] have hcoef : c * Real.sqrt t = (1 / 2 : ℝ) := by dsimp [c] field_simp [ne_of_gt (Real.sqrt_pos.mpr ht)] rw [← mul_assoc, hcoef, hposterior v] rw [← integral_const_mul, ← integral_add (hp v) ((hH v).const_mul (1 / 2 : ℝ))] exact integral_congr_ae (Filter.Eventually.of_forall fun x => hcancel x v) have hd := hasDerivAt_integral_fieldMean_square_observation ρ energy spin observable A hbound hcoords (fun x : S × (V → ℝ) => spin x.1) Prod.snd ((measurable_of_finite spin).comp measurable_fst) measurable_snd (fun x v => (abs_spin_eq_one (fun s => spin s v) (fun s => hcoords s v) x.1).le) (fun v => integrable_finiteStandardNoise μ v) ht apply hd.congr_deriv change (∫ x, ∑ v, g x v * gaussianObservationVelocity (spin x.1) x.2 t v ∂ρ) = _ rw [integral_finsetSum _ (fun v _ => hv v), integral_finsetSum _ (fun v _ => hc v)] exact Finset.sum_congr rfl fun v _ => hstep v theorem hasDerivAt_finiteStandardEnergy [StandardBorelSpace S] (μ : Measure S) [IsProbabilityMeasure μ] (energy : S → ℝ) (hprior : ∀ s, μ.real {s} = Real.exp (energy s) / ∑ a, Real.exp (energy a)) (spin : S → V → ℝ) (observable : S → ℝ) (A : ℝ) (hbound : ∀ s, |observable s| ≤ A) (hcoords : ∀ s v, spin s v = -1 ∨ spin s v = 1) {t : ℝ} (ht : 0 < t) : HasDerivAt (fun r => ∫ x, fieldMean energy spin observable (finiteStandardObservation spin r x) ^ 2 ∂μ.prod (Measure.pi (fun _ : V => gaussianReal 0 1))) (∫ x, ∑ v, fieldCovariance energy spin observable (finiteStandardObservation spin t x) v ^ 2 ∂μ.prod (Measure.pi (fun _ : V => gaussianReal 0 1))) t := by apply hasDerivAt_finiteStandardEnergy_of_posterior_test μ energy spin observable A hbound hcoords ht intro v have hsq : ∀ s i, spin s i ^ 2 = 1 := by intro s i rcases hcoords s i with h | h <;> simp [h] exact Problem367.finiteStandardGaussianPlanted_integral_test_fieldMean μ energy hprior t ht.le spin hsq (fun s => spin s v) (fun z => fieldSquareGradient energy spin observable z v) (continuous_fieldSquareGradient energy spin observable v).measurable (4 * A ^ 2) (fun z => by simpa only [Real.norm_eq_abs] using abs_fieldSquareGradient_le energy spin observable z v A (fun s => hcoords s v) hbound) end Planted end Problem367.FinitePosterior end end OAI