import Mathlib import OAI.Probability.LogConcave.Numerics.CentersBoundPicard import OAI.Probability.LogConcave.Numerics.CompileDeclaredRootShift import OAI.Probability.LogConcave.OraclePrograms.TerminalMeanProgram namespace OAI section noncomputable section namespace LogConcaveSampling.MeanTree open MeasureTheory ProbabilityTheory OracleCompiler Coupling SeedCompiler open scoped Classical BigOperators variable {X : Type*} [MeasurableSpace X] {d : ℕ} theorem compileDeclaredRoot_normalized (F V : Point d → ℝ) (hV : Measurable (firstOrderReply V)) {D A Af hi n Li Lf ei ef Q : ℝ} (hD : 0 (M s r τ).program.run V (c,g))) (henergy : ∀s r τ,P s r τ → (∑j,(M s r τ).shift j^2)≤D^2) (hLipi : ∀r τ,P false r τ → ∀u v g, ‖(M false r τ).program.run V (u,g)-(M false r τ).program.run V (v,g)‖≤Li*‖u-v‖) (hLipf : ∀r τ,P true r τ → ∀u v g, ‖(M true r τ).program.run V (u,g)-(M true r τ).program.run V (v,g)‖≤Lf*‖u-v‖) (hMi : ∀r τ,P false r τ → ∀c,SquaredAt (gaussianTape d (M false r τ).slots) (stdGaussian (Point d)) (fun g => (M false r τ).program.run V (c,g)) (fun g => primitiveExpectedField F c r+τ • g) (τ^2*ei^2*(circuitD F c)^2)) (hMf : ∀r τ,P true r τ → ∀c,SquaredAt (gaussianTape d (M true r τ).slots) (stdGaussian (Point d)) (fun g => (M true r τ).program.run V (c,g)) (fun g => primitiveExpectedField F c r+τ • g) (τ^2*ef^2*(circuitD F c)^2)) (E : MeanTree X d) (x : X) (hE : DeclaredRootValid D A Af (P true) (P false) E n) (hc : centersBound A E) (hs : centers (fun r _ => 0≤r ∧ r≤hi) E) (hv : centerValues F Q E x) : SquaredAt (gaussianTape d (compileDeclaredRoot D A Af (M true) (M false) E n).slots) (stdGaussian (Point d)) ((compileDeclaredRoot D A Af (M true) (M false) E n).eval V x) (fun g => E.eval F x+n • g) ((2*Af^2*Lf^2*(hi/(2*D))^2*ei^2+n^2*ef^2/2)*Q^2) := by let K : Bool → ℝ → ℝ → ℝ := fun s _ _ => if s then Lf else Li let B : Bool → ℝ → ℝ → Point d → ℝ := fun s _ τ c => τ^2*(if s then ef else ei)^2*(circuitD F c)^2 have hK : ∀s r τ,P s r τ → 0≤K s r τ := by intro s r τ _; cases s <;> assumption have hLip : ∀s r τ,P s r τ → ∀u v g, ‖(M s r τ).program.run V (u,g)-(M s r τ).program.run V (v,g)‖≤K s r τ*‖u-v‖ := by intro s; cases s; exact hLipi; exact hLipf have hM : ∀s r τ,P s r τ → ∀c,SquaredAt (gaussianTape d (M s r τ).slots) (stdGaussian (Point d)) (fun g => (M s r τ).program.run V (c,g)) (fun g => primitiveExpectedField F c r+τ • g) (B s r τ c) := by intro s; cases s; exact hMi; exact hMf have hh := compileDeclaredRoot_squared F V hV hD hA hAf P M K B hK henergy hshift hLip hM E n x hE apply hh.mono cases E with | node k b hb a r C => exact declaredRootError_normalized F hD hA hAf hcon (K false) (K true) (fun _ _ _ _ _ _ => le_rfl) k b hb a r C x hE.2.1 hc hs hv (fun _ => le_rfl) end LogConcaveSampling.MeanTree end end section noncomputable section namespace LogConcaveSampling.MeanTree open MeasureTheory Quadrature FinitePicard open scoped Classical BigOperators variable {X : Type*} [MeasurableSpace X] {d : ℕ} lemma forward_weights {r T h P A : ℝ} (hr : 0≤r) (hP : 0≤P) (n N : ℕ) (ht : ∀j : ProbabilityNode T h n,|probabilityNodeTime T h n j|≤1) (hw : ∀i,∑j,|probabilityWeight T h n i j|≤P) (x y : MeanTree X d) (hxw : weight x=0) (hx : centersBound A x) (hy : centersBound A y) (hA : r*(weight y+P*r)≤A) (i : ProbabilityNode T h n) : weight (forward r T h n N x y i)≤weight y+P*r ∧ centersBound A (forward r T h n N x y i) := by have hc (j : ProbabilityNode T h n) (E : MeanTree X d) : weight (scale (-r) (conditional r (probabilityNodeTime T h n j) x E))≤r := by simp [abs_of_nonneg hr] constructor · exact weight_picard _ _ _ hr hw hc N i · apply centersBound_picard _ _ _ hP hr hw hc (fun _ => le_rfl) (fun _ => hy) ?_ N i intro j E hE hEb apply (centersBound_scale _ _ _).2 apply centersBound_conditional hx hEb rw [hxw,zero_add,abs_mul,abs_of_nonneg hr] calc r*|probabilityNodeTime T h n j| * weight E≤r*weight E := by nlinarith [weight_nonneg E,mul_le_mul_of_nonneg_left (ht j) hr] _≤r*(weight y+P*r) := mul_le_mul_of_nonneg_left hE hr _≤A := hA lemma literalSample_weights {r T h P A Af : ℝ} (hr : 0 z.2.1) (by fun_prop)) (anchor (fun z => z.2.2) (by fun_prop)) (by simp) (by simp) (by simp) (by simp) (by simpa only [weight_anchor,zero_add] using hbound) simp only [weight_anchor,mul_zero,zero_add] at hh exact ⟨hh.1.trans hAf,hh.2⟩ end LogConcaveSampling.MeanTree end end section noncomputable section namespace LogConcaveSampling.MeanTree open MeasureTheory variable {X : Type*} [MeasurableSpace X] {d : ℕ} lemma DeclaredValid.shiftReady {D A : ℝ} (P : ℝ → ℝ → Prop) (hp : ∀r τ,P r τ → r≠0) (E : MeanTree X d) (n : ℝ) (he : DeclaredValid D A P E n) : ShiftReady D A P E n := by induction E generalizing n with | node k b hb a r C ih => intro i exact ⟨hp _ _ (he.2.2 i).1,(he.2.2 i).1,ih i _ (he.2.2 i).2⟩ lemma DeclaredRootValid.shiftReady {D A Af : ℝ} (Pf Pi : ℝ → ℝ → Prop) (hpf : ∀r τ,Pf r τ → r≠0) (hpi : ∀r τ,Pi r τ → r≠0) (E : MeanTree X d) (n : ℝ) (he : DeclaredRootValid D A Af Pf Pi E n) : RootShiftReady D A Af Pf Pi E n := by cases E with | node k b hb a r C => intro i exact ⟨hpf _ _ (he.2.2 i).1,(he.2.2 i).1, (he.2.2 i).2.shiftReady Pi hpi (C i) (r i/(2*D))⟩ lemma declaredRootValid_of_radius_bounds {D A Af lo hi n : ℝ} (hD : 0 lo≤r ∧ r≤hi) E) (hfinal : rootScales (fun r => Pf r (declaredNoise n Af)) E) (hPi : ∀r q,lo≤r → r≤hi → lo/(2*D)≤q → q≤hi/(2*D) → Pi r (declaredNoise q A)) : DeclaredRootValid D A Af Pf Pi E n := by cases E with | node k b hb a r C => refine ⟨hn,hw,?_⟩ intro i refine ⟨hfinal i,?_⟩ exact declaredValid_of_radius_bounds hD hlo Pi (C i) (hC i).1 (hC i).2 (hscale i).2 ⟨div_le_div_of_nonneg_right (hscale i).1.1 (by positivity), div_le_div_of_nonneg_right (hscale i).1.2 (by positivity)⟩ hPi end LogConcaveSampling.MeanTree end end end OAI