import Mathlib import OAI.Probability.LogConcave.Numerics.RowGaussian namespace OAI section noncomputable section namespace LogConcaveSampling.MeanTree open MeasureTheory OracleCompiler OracleCompiler.Expression SeedCompiler open scoped Classical BigOperators variable {X : Type*} [MeasurableSpace X] {d : ℕ} def cost : MeanTree X d → ℕ | .node _ _ _ _ _ C => ∑i,(1+cost (C i)) @[simp] lemma cost_anchor (b : X → Point d) (hb : Measurable b) : cost (anchor b hb)=0 := by simp [cost,anchor] @[simp] lemma cost_zero : cost (zero : MeanTree X d)=0 := by simp [zero] @[simp] lemma cost_scale (s : ℝ) (E : MeanTree X d) : cost (scale s E)=cost E := by cases E; rfl @[simp] lemma cost_add (E D : MeanTree X d) : cost (add E D)=cost E+cost D := by cases E; cases D simp only [cost,add,Fin.sum_univ_add,Fin.addCases_left,Fin.addCases_right] @[simp] lemma cost_mean (r : ℝ) (E : MeanTree X d) : cost (mean r E)=1+cost E := by simp [cost,mean] @[simp] lemma cost_conditional (r ρ : ℝ) (E D : MeanTree X d) : cost (conditional r ρ E D)=1+cost E+cost D := by simp [conditional,Nat.add_assoc] @[simp] lemma cost_sumFin (k : ℕ) (E : Fin k → MeanTree X d) : cost (sumFin k E)=∑i,cost (E i) := by induction k with | zero => simp [sumFin] | succ k ih => simp [sumFin,ih,Fin.sum_univ_succ] @[simp] lemma cost_sumFamily {I : Type*} [Fintype I] (E : I → MeanTree X d) : cost (sumFamily E)=∑i,cost (E i) := by rw [sumFamily,cost_sumFin] exact Fintype.sum_equiv (Fintype.equivFin I).symm _ _ (fun _ => rfl) theorem compileDeclared_size (D A : ℝ) (P : ℝ → ℝ → Prop) (M : ℝ → ℝ → SeedProgram d) (S Q : ℕ) (hM : ∀r τ,P r τ → (M r τ).slots≤S ∧ (M r τ).calls≤Q) (E : MeanTree X d) (n : ℝ) (hready : ShiftReady D A P E n) : (compileDeclared D A M E n).slots≤1+cost E*(S+1) ∧ (compileDeclared D A M E n).calls≤cost E*Q := by induction E generalizing n with | node k b hb a r C ih => have hc (i) := ih i (r i/(2*D)) (hready i).2.2 have hm (i) := hM (r i) (declaredNoise n A) (hready i).2.1 constructor · simp only [compileDeclared,slots_compileTerms,Expression.slots,cost] apply Nat.add_le_add_left rw [Finset.sum_mul] apply Finset.sum_le_sum intro i _ have h₁ := (hc i).1 have h₂ := (hm i).1 nlinarith · simp only [calls_compileDeclared,cost,Finset.sum_mul] apply Finset.sum_le_sum intro i _ have h₁ := (hc i).2 have h₂ := (hm i).2 nlinarith def DriftBound (R : ℝ) : (E : MeanTree X d) → DriftData E → Prop | .node _ _ _ _ r C,p => ∀i,|((p.2 i).root (C i))/(r i)|≤R ∧ DriftBound R (C i) (p.2 i) theorem compileDeclaredShift_energy_bound {D A R B : ℝ} (hD : 0 change (∑j,compileTermsShift D k a (fun i => compileDeclared D A M (C i) (r i/(2*D))) (fun i => M (r i) (declaredNoise n A)) (.noise b hb (declaredReserve n A a)) (fun i => compileDeclaredShift D A M (C i) (r i/(2*D)) (p.2 i)) (fun i => inverseChildShift D (r i) ((p.2 i).root (C i)) (M (r i) (declaredNoise n A))) (fun _ => 0) j^2)≤_ rw [compileTermsShift_energy] simp only [zero_pow (by decide : (2:ℕ)≠0),Finset.sum_const_zero,zero_add,cost,Nat.cast_sum,Nat.cast_add,Nat.cast_one,Finset.sum_mul] apply Finset.sum_le_sum intro i _ have hc := ih i (r i/(2*D)) (p.2 i) (hready i).2.2 (hg i).2 have hi := inverse_shift_energy (fun j => (M (r i) (declaredNoise n A)).shift j/(2*D)) (M (r i) (declaredNoise n A)).shift (normalized_shift_bound hD _ (hv _ _ (hready i).2.1)) (((p.2 i).root (C i))/(r i)) have hs := hBv _ _ (hready i).2.1 have hh : (((p.2 i).root (C i))/(r i))^2≤R^2 := by have hb := pow_le_pow_left₀ (abs_nonneg _) (hg i).1 2 simpa only [sq_abs] using hb have hm := mul_le_mul_of_nonneg_right (mul_le_mul_of_nonneg_left hh (by norm_num : (0:ℝ)≤4)) hB have hv' := mul_le_mul_of_nonneg_left hs (show 0≤4*(((p.2 i).root (C i))/(r i))^2 by positivity) change (∑j,compileDeclaredShift D A M (C i) (r i/(2*D)) (p.2 i) j^2)+ (∑j,inverseChildShift D (r i) ((p.2 i).root (C i)) (M (r i) (declaredNoise n A)) j^2)≤_ change (∑j,inverseChildShift D (r i) ((p.2 i).root (C i)) (M (r i) (declaredNoise n A)) j^2)≤_ at hi nlinarith end LogConcaveSampling.MeanTree end end section noncomputable section namespace LogConcaveSampling.MeanTree open Set MeasureTheory open scoped Classical BigOperators variable {X : Type*} [MeasurableSpace X] {d : ℕ} lemma sqrt_one_sub_sq_gap {ρ : ℝ} (hρ : ρ∈Icc (0:ℝ) 1) : 1-ρ≤Real.sqrt (1-ρ^2) := by have hp : 0≤1-ρ^2 := by nlinarith [hρ.1,hρ.2] have hs := Real.sq_sqrt hp nlinarith [Real.sqrt_nonneg (1-ρ^2),hρ.1,hρ.2] lemma mean_direct_ratio {ρ T θ : ℝ} (hT : 1/2≤T) (hT1 : T<1) (hρ : ρ∈Icc (0:ℝ) T) (hθ : θ^2≤Real.sqrt (1-T^2)) : 0 ∃c : ℝ,(∀Δ z,b (f Δ z)=b z+c • Δ) ∧ |c/r|≤R) E) : ∃p : DriftData E,DriftValid f E p ∧ p.root E=c ∧ DriftBound R E p := by induction E generalizing c with | node k b hb a r C ih => choose c' hshift hratio using fun i => (hE i).1 have hh (i : Fin k) := ih i (c' i) (hshift i) (hE i).2 choose p hp hroot hbound using hh exact ⟨(c,p),⟨hc,hp⟩,rfl,fun i => ⟨by simpa only [hroot i] using hratio i,hbound i⟩⟩ omit [MeasurableSpace X] in lemma DirectCenter.shift {r T v α r' : ℝ} {x Y G b : X → Point d} (f : Point d → X → X) (hx : ∀Δ z,x (f Δ z)=x z+r • Δ) (hY : ∀Δ z,Y (f Δ z)=Y z-α • Δ) (hG : ∀Δ z,G (f Δ z)=G z) (hb : DirectCenter r T v x Y G r' b) : ∃ρ∈Icc (0:ℝ) T,∃θ,|θ|≤v ∧ r'=r*Real.sqrt (1-ρ^2) ∧ ∀Δ z,b (f Δ z)=b z+(r*(1-ρ*Real.cos θ*α)) • Δ := by obtain ⟨ρ,hρ,θ,hθ,hr,hb⟩ := hb refine ⟨ρ,hρ,θ,hθ,hr,?_⟩ intro Δ z rw [hb] simp only [hx,hY,hG,oriented,smul_add,smul_sub,smul_smul] module omit [MeasurableSpace X] in lemma DirectCenter.mean_shift {r T v r' : ℝ} (hr : 0 z.2.1) (by fun_prop)) (anchor (fun z => z.2.2) (by fun_prop)) def literalSample (r T h : ℝ) (n N : ℕ) (e : ProbabilityNode T h n) : MeanTree (SampleInput d) d := scale T⁻¹ (forward r T h n N (anchor Prod.fst (by fun_prop)) (anchor Prod.snd (by fun_prop)) e) lemma literalMean_geometry {r T h ψ σ v : ℝ} {n m N nc Nc : ℕ} (hT : 0 (σ/2) • z.2.2) ∧ centers (DirectCenter r T v Prod.fst (fun z : MeanInput d => z.2.1) (fun z => z.2.2)) (literalMean r T h ψ σ n m N nc Nc e) := by exact meanCircuit_centers_direct hT hT1 hh hv _ _ _ rfl rfl rfl hψ (centers_anchor _ _ _) (centers_anchor _ _ _) (centers_anchor _ _ _) e lemma literalSample_geometry {r T h : ℝ} {n N : ℕ} (hn : 0 T⁻¹ • z.2) ∧ centers (DirectCenter r T 0 Prod.fst Prod.snd (fun _ : SampleInput d => 0)) (literalSample r T h n N e) := by have hg := forward_centers_direct (r:=r) (N:=N) (v:=0) (θ:=0) (x:=Prod.fst) (Y:=Prod.snd) (G:=fun _ : SampleInput d => 0) hn hT hT1 hh (anchor (Prod.fst : SampleInput d → Point d) (by fun_prop)) (anchor Prod.snd (by fun_prop)) rfl (by simp only [base_anchor,oriented_zero]) (by norm_num) (centers_anchor _ _ _) (centers_anchor _ _ _) e exact ⟨by simp only [literalSample,base_scale,hg.1,base_anchor], (centers_scale _ _ _).2 hg.2⟩ theorem literalMean_drift {r T h ψ σ v : ℝ} {n m N nc Nc : ℕ} (hr : 0 ?_) hg.2 exact (hb.mean_shift hr hT hT1 hvsmall (meanInputMove r T) (fun _ _ => rfl) (fun _ _ => rfl) (fun _ _ => rfl)).2 theorem literalSample_drift {r T h : ℝ} {n N : ℕ} (hr : 0 ?_) hg.2 obtain ⟨ρ,hρ,θ,hθ,hr',hshift⟩ := hb.shift (sampleInputMove r T) (fun _ _ => rfl) (fun _ _ => rfl) (fun _ _ => rfl) have hθ0 : θ=0 := abs_nonpos_iff.mp hθ refine ⟨r*(1-ρ*T),?_,?_⟩ · simpa only [hθ0,Real.cos_zero,mul_one] using hshift · rw [hr',mul_div_mul_left _ _ hr.ne'] exact (sample_direct_ratio hT.le hT1 hρ).2 end LogConcaveSampling.MeanTree end end end OAI