import Mathlib import OAI.Probability.LogConcave.OraclePrograms.Shape import OAI.Probability.LogConcave.OraclePrograms.SampleCorrect namespace OAI section noncomputable section namespace LogConcaveSampling.OracleCompiler open Filter MeanTree MeasureTheory ProbabilityTheory open scoped Classical NNReal Topology variable {d : ℕ} def StandaloneCorrect (F : Point d → ℝ) (lam q K e ηmin : ℝ) : {k : RoutineKind} → RoutineValue d k → Prop | .mean,M => MeanCorrect F lam q K e M | .sample,S => SampleCorrect F lam q K e ηmin S def UniformAccuracy (κ t w : ℝ) (_ : ℝ) (n N : ℕ) (ht : 0 < t) (hts : t≤1/44) (L : RoutineLabel) : Prop := ∀q : ℕ → ℝ,LogPowerRate q (κ*L.b) → (∀ᶠ d : ℕ in atTop,0 < q d) → ∃K : ℝ,0 ≤ K ∧ ∃e : ℕ → ℝ,LogPowerRate e (κ*L.p) ∧ (∀ᶠ d : ℕ in atTop,0 < e d) ∧ ∀ᶠ d : ℕ in atTop,∀C : CircuitParameters,SourceShape κ t w n N d C → ∀{F : Point d → ℝ} {lam : ℝ≥0},∀_ : Primitive F lam,0 < lam → (lam:ℝ)≤1 → StandaloneCorrect F lam (q d) K (e d) ((d:ℝ)^(-(κ*t))) (constructRoutine d C ht hts L) lemma sourceA_pos (κ t w : ℝ) (n d : ℕ) : 0 < sourceA κ t w n d := by have hB := (fixedCenteringBudget_spec n).1 have hP := (fixedProbabilityBudget_spec n).1 unfold sourceA positivity lemma childSizes_positive {κ t w : ℝ} (n : ℕ) {q : ℕ → ℝ} (hq : ∀ᶠ d : ℕ in atTop,0 < q d) : (∀ᶠ d : ℕ in atTop,0 < meanFinalSize κ t n q d) ∧ (∀ᶠ d : ℕ in atTop,0 < sampleFinalSize n q d) ∧ (∀ᶠ d : ℕ in atTop,0 < internalSize κ t w n q d) ∧ (∀ᶠ d : ℕ in atTop,0 < anchorSize κ t q d) := by have h : ∀ᶠ d : ℕ in atTop,0 < meanFinalSize κ t n q d ∧ 0 < sampleFinalSize n q d ∧ 0 < internalSize κ t w n q d ∧ 0 < anchorSize κ t q d := by filter_upwards [hq,eventually_ge_atTop (1:ℕ)] with d hd hd1 have hd0 : (0:ℝ) h.1),h.mono (fun _ h => h.2.1),h.mono (fun _ h => h.2.2.1),h.mono (fun _ h => h.2.2.2)⟩ lemma uniformAccuracy_terminal {κ t w J K₀ : ℝ} {n N : ℕ} (hκ : 0 < κ) (ht : 0 < t) (hts : t≤1/44) (L : RoutineLabel) (hL : L.Admissible t K₀) (hterm : L.terminal) : UniformAccuracy κ t w J n N ht hts L := by intro q hq hqpos have hb := hL.b_lower ht.le have hqsmall := hq.eventually_lt_const (show 0 < κ*L.b by positivity [hb]) (by norm_num : (0:ℝ)<1/2) cases L with | mk kind b p => cases kind with | mean => let c := 4*(Real.pi+2) have hc : 0 < c := by dsimp [c]; positivity [Real.pi_pos] refine ⟨1,by norm_num,(fun d => c*q d),(hq.const_mul c).mono (by dsimp [RoutineLabel.terminal] at hterm; nlinarith),?_,?_⟩ · exact hqpos.mono (fun _ h => mul_pos hc h) · filter_upwards [hqsmall] with d hd C hC F lam hF hlam hlam1 rw [constructRoutine,dite_eq_left hterm] exact terminalMean_correct hF ((le_abs_self _).trans hd.le) | sample => let c := 2*terminalTransportConstant*(Real.pi+3) have hc : 0 < c := by dsimp [c]; positivity [terminalTransportConstant_pos,Real.pi_pos] refine ⟨1,by norm_num,(fun d => c*q d),(hq.const_mul c).mono (by dsimp [RoutineLabel.terminal] at hterm; nlinarith),?_,?_⟩ · exact hqpos.mono (fun _ h => mul_pos hc h) · filter_upwards [hqsmall] with d hd C hC F lam hF hlam hlam1 rw [constructRoutine,dite_eq_left hterm] exact terminalSample_correct hF ((le_abs_self _).trans hd.le) _ theorem uniform_routine_accuracy {κ t w J K₀ : ℝ} {n N : ℕ} (hκ : 0 < κ) (ht : 0 < t) (hts : t<1/100) (hw : 0 < w) (hwt : w cases kind with | mean => have hLF := RoutineStep.meanFinal (t:=t) b p have hLI := RoutineStep.internal (t:=t) RoutineKind.mean RoutineKind.mean b p have hLS := RoutineStep.internal (t:=t) RoutineKind.mean RoutineKind.sample b p have hAF := hL.child ht.le hnt hLF have hAI := hL.child ht.le hnt hLI have hAS := hL.child ht.le hnt hLS obtain ⟨Kf,hKf,ef,hef,hefpos,hf⟩ := ih _ hLF hAF (meanFinalSize κ t n q) (meanFinalSize_rate n hq) hqp.1 obtain ⟨Ki,hKi,ei,hei,heipos,hi⟩ := ih _ hLI hAI (internalSize κ t w n q) hqi hqp.2.2.1 obtain ⟨Ks,hKs,es,hes,hespos,hS⟩ := ih _ hLS hAS (anchorSize κ t q) (anchorSize_rate hκ.le ht.le hq) hqp.2.2.2 let e := fun d => 2*(sourceAf n*Kf*H*q d)*es d+ 10*(20+16*circuitGrowthConstant^2)*(2*(1+2*H)*(sourceAf n*Kf*q d*ei d+ef d))+ 2*(d:ℝ)^(-(κ*J)) have he : LogPowerRate e (κ*p) := by apply (meanError_rate hκ.le hb hpJ (sourceAf n) H Kf hq hes hei hef).congr filter_upwards [] with d dsimp only [e] ring have hepos : ∀ᶠ d : ℕ in atTop,0 < e d := by filter_upwards [hqpos,hefpos,heipos,hespos,eventually_ge_atTop (1:ℕ)] with d hqd hfd hid hsd hd1 have hd0 : (0:ℝ) by cases s; exact hbud _ hAI r τ; exact hbud _ hAF r τ) hf' hi' hs' (fun r => (hbud _ hAS r _ (Or.inl rfl)).reserve) (hnd C hC hF hlam hlam1 hqle).1 rw [constructRoutine,dite_eq_right hnt] simpa only [StandaloneCorrect,hC.Af,hC.depth,CircuitParameters.meanError, CircuitParameters.compilerError,e,H] using hp | sample => have hLF := RoutineStep.sampleFinal (t:=t) b p have hLI := RoutineStep.internal (t:=t) RoutineKind.sample RoutineKind.mean b p have hAF := hL.child ht.le hnt hLF have hAI := hL.child ht.le hnt hLI obtain ⟨Kf,hKf,ef,hef,hefpos,hf⟩ := ih _ hLF hAF (sampleFinalSize n q) (sampleFinalSize_rate n hq) hqp.2.1 obtain ⟨Ki,hKi,ei,hei,heipos,hi⟩ := ih _ hLI hAI (internalSize κ t w n q) hqi hqp.2.2.1 let e := fun d => 20*(2*(1+2*H)*(sourceAf n*Kf*q d*ei d+ef d))+2*(d:ℝ)^(-(κ*J)) have he : LogPowerRate e (κ*p) := sampleError_rate hκ.le hb hpJ (sourceAf n) H Kf hq hei hef have hepos : ∀ᶠ d : ℕ in atTop,0 < e d := by filter_upwards [hqpos,hefpos,heipos,eventually_ge_atTop (1:ℕ)] with d hqd hfd hid hd1 have hd0 : (0:ℝ) by cases s; exact hbud _ hAI r τ; exact hbud _ hAF r τ) hf' hi' (hnd C hC hF hlam hlam1 hqle).2 rw [constructRoutine,dite_eq_right hnt] simpa only [StandaloneCorrect,hC.Af,hC.depth,CircuitParameters.sampleError, CircuitParameters.compilerError,e,H] using hp end LogConcaveSampling.OracleCompiler end end section noncomputable section namespace LogConcaveSampling open Filter MeasureTheory ProbabilityTheory open scoped Classical NNReal theorem calibrated_sample_circuit_order {κ J b w : ℝ} (hκ : 0<κ) (hJ : 1≤J) (hb : 1/2≤b) (hw : 0 ‖sampleMeanCircuit F x r T h n (numericalLayerCount J) e z- T⁻¹ • fullProbabilityFlow hF x hr.le hl (probabilityNodeTime T h n e) z‖^2) (stdGaussian (Point d)) ∧ (∫z,‖sampleMeanCircuit F x r T h n (numericalLayerCount J) e z- T⁻¹ • fullProbabilityFlow hF x hr.le hl (probabilityNodeTime T h n e) z‖^2 ∂stdGaussian (Point d))≤ (circuitD F x)^2*(d:ℝ)^(-(2*κ*J)) := by obtain ⟨A,C,hC,k,hproducer⟩ := sampleMeanCircuit_rms n hn intro S hS ksize have hc := sampleScalarCalibration_uniform k n (numericalLayerCount J) ksize A C hS hκ hJ hb hw hnw (numericalLayerCount_bound J) have hhsmall := (LogPowerRate.power (κ*w)).eventually_lt_const (mul_pos hκ hw) (Real.log_pos (by norm_num : (1:ℝ)<2)) filter_upwards [hc,hhsmall,eventually_ge_atTop (1:ℕ)] with d hd hdh hdim intro h have hdpos : (0:ℝ)