Download data/lean/OAI/Probability/LogConcave/Sampling/ChangingChain.lean from introvoyz045/math: direct link, hf CLI and curl.
- Browser
- Download file 15 kB
-
https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Probability/LogConcave/Sampling/ChangingChain.lean
- Command line
-
hf download hf://datasets/introvoyz045/math/data/lean/OAI/Probability/LogConcave/Sampling/ChangingChain.lean
-
curl -L -o ChangingChain.lean https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Probability/LogConcave/Sampling/ChangingChain.lean
15 kB
| import Mathlib | |
| import OAI.Probability.LogConcave.Sampling.StateLaw | |
| import OAI.Probability.LogConcave.Sampling.NoiseKernel | |
| namespace OAI | |
| section | |
| noncomputable section | |
| namespace LogConcaveSampling | |
| open MeasureTheory ProbabilityTheory Function | |
| open scoped Classical | |
| variable {E R : Type*} [MeasurableSpace E] [MeasurableSpace R] | |
| def changingChain (ΞΊ : β β Kernel E E) (ΞΌ : Measure E) : β β Measure E | |
| | 0 => ΞΌ | |
| | n+1 => ΞΊ n ββ changingChain ΞΊ ΞΌ n | |
| instance changingChain_probability (ΞΊ : β β Kernel E E) [βn,IsMarkovKernel (ΞΊ n)] | |
| (ΞΌ : Measure E) [IsProbabilityMeasure ΞΌ] (n : β) : IsProbabilityMeasure (changingChain ΞΊ ΞΌ n) := by | |
| induction n with | |
| | zero => exact inferInstanceAs (IsProbabilityMeasure ΞΌ) | |
| | succ n ih => let := ih; exact inferInstanceAs (IsProbabilityMeasure (ΞΊ n ββ changingChain ΞΊ ΞΌ n)) | |
| lemma changingChain_constant (ΞΊ : Kernel E E) (ΞΌ : Measure E) (n : β) : | |
| changingChain (fun _ => ΞΊ) ΞΌ n=markovChain ΞΊ ΞΌ n := by | |
| induction n with | |
| | zero => rfl | |
| | succ n ih => rw [changingChain,markovChain,ih] | |
| lemma changingChain_tv (ΞΊ : β β Kernel E E) [βn,IsMarkovKernel (ΞΊ n)] | |
| {ΞΌ Ξ½ : Measure E} [IsProbabilityMeasure ΞΌ] [IsProbabilityMeasure Ξ½] {e : β} | |
| (he : TVAtMost ΞΌ Ξ½ e) (n : β) : | |
| TVAtMost (changingChain ΞΊ ΞΌ n) (changingChain ΞΊ Ξ½ n) e := by | |
| induction n with | |
| | zero => exact he | |
| | succ n ih => exact ih.kernel (ΞΊ n) | |
| lemma stateLaw_eq_changingChain (ΞΌ : Measure E) (Ξ³ : Measure R) | |
| [IsProbabilityMeasure ΞΌ] [IsProbabilityMeasure Ξ³] | |
| (f : β β E Γ R β E) (hf : βn,Measurable (f n)) | |
| (ΞΊ : β β Kernel E E) [βn,IsMarkovKernel (ΞΊ n)] | |
| (hk : βn x,ΞΊ n x=Ξ³.map (fun r => f n (x,r))) (n : β) : | |
| stateLaw ΞΌ Ξ³ f n=changingChain ΞΊ ΞΌ n := by | |
| induction n with | |
| | zero => rfl | |
| | succ n ih => | |
| rw [stateLaw,changingChain,ih] | |
| apply measure_eq_of_bounded_integral | |
| intro g hg hb | |
| rw [kernel_integral_comp _ _ hg hb,integral_map (hf n).aemeasurable hg.aestronglyMeasurable] | |
| rw [integral_prod _ (show Integrable (fun p => g (f n p)) _ from bounded_integrable (hg.comp (hf n)) (fun p => hb _))] | |
| apply integral_congr_ae | |
| filter_upwards [] with x | |
| rw [hk] | |
| exact (integral_map ((hf n).comp (show Measurable (fun r : R => (x,r)) by fun_prop)).aemeasurable hg.aestronglyMeasurable).symm | |
| end LogConcaveSampling | |
| end | |
| end | |
| section | |
| noncomputable section | |
| namespace LogConcaveSampling | |
| open MeasureTheory ProbabilityTheory Function | |
| open scoped Classical | |
| variable {d : β} | |
| lemma integral_norm_le_sqrt_moment {E : Type*} [NormedAddCommGroup E] | |
| [MeasurableSpace E] [BorelSpace E] {ΞΌ : Measure E} [IsProbabilityMeasure ΞΌ] | |
| (hm : MemLp (fun x : E => x) 2 ΞΌ) : | |
| (β«x,βxβ βΞΌ)β€Real.sqrt (β«x,βxβ^2 βΞΌ) := by | |
| have hh := integral_mul_sq_le hm.norm (memLp_const (1:β) (ΞΌ:=ΞΌ)) | |
| simp only [mul_one,one_pow,integral_const,probReal_univ,smul_eq_mul] at hh | |
| exact (Real.le_sqrt (integral_nonneg (fun _ => norm_nonneg _)) | |
| (integral_nonneg (fun _ => sq_nonneg _))).2 hh | |
| lemma Admissible.first_moment_bound {V : Point d β β} (hV : Admissible V) : | |
| Integrable (fun x => βxβ) (gibbs V) β§ (β«x,βxβ βgibbs V)β€Real.sqrt d := by | |
| let := hV.isProbabilityMeasure_gibbs | |
| have hm : MemLp (fun x : Point d => x) 2 (gibbs V) := | |
| (memLp_two_iff_integrable_sq_norm (by fun_prop)).2 hV.second_moment_bound.1 | |
| exact β¨hm.norm.integrable (by norm_num),(integral_norm_le_sqrt_moment hm).trans | |
| (Real.sqrt_le_sqrt hV.second_moment_bound.2)β© | |
| lemma stdGaussian_first_moment_bound (d : β) : | |
| (β«x : Point d,βxβ βstdGaussian (Point d))β€Real.sqrt d := by | |
| simpa only [stdGaussian_sq_norm] using | |
| integral_norm_le_sqrt_moment (IsGaussian.memLp_two_id (ΞΌ:=stdGaussian (Point d))) | |
| lemma noisedLaw_first_moment {V : Point d β β} (hV : Admissible V) (h : β) : | |
| Integrable (fun x => βxβ) (noisedLaw V h) β§ | |
| (β«x,βxβ βnoisedLaw V h)β€(1+Real.sqrt h)*Real.sqrt d := by | |
| let := hV.isProbabilityMeasure_gibbs | |
| have hiX : Integrable (fun p : Point d Γ Point d => βp.1β) | |
| ((gibbs V).prod (stdGaussian (Point d))) := hV.first_moment_bound.1.comp_fst _ | |
| have hiG : Integrable (fun p : Point d Γ Point d => βp.2β) | |
| ((gibbs V).prod (stdGaussian (Point d))) := | |
| (IsGaussian.memLp_two_id (ΞΌ:=stdGaussian (Point d))).norm.integrable (by norm_num) |>.comp_snd _ | |
| have hi : Integrable (fun p : Point d Γ Point d => βp.1+Real.sqrt h β’ p.2β) | |
| ((gibbs V).prod (stdGaussian (Point d))) := | |
| (hiX.add (hiG.const_mul (Real.sqrt h))).mono' (by fun_prop) (Filter.Eventually.of_forall (fun p => by | |
| rw [Real.norm_eq_abs,abs_of_nonneg (norm_nonneg _)] | |
| change βp.1+Real.sqrt h β’ p.2ββ€βp.1β+Real.sqrt h*βp.2β | |
| simpa only [norm_smul,Real.norm_eq_abs,abs_of_nonneg (Real.sqrt_nonneg _)] using | |
| norm_add_le p.1 (Real.sqrt h β’ p.2))) | |
| refine β¨(integrable_map_measure (by fun_prop) (by fun_prop)).2 hi,?_β© | |
| rw [noisedLaw,integral_map (by fun_prop) (by fun_prop)] | |
| calc | |
| _ β€ β«p : Point d Γ Point d,βp.1β+Real.sqrt h*βp.2β β((gibbs V).prod (stdGaussian (Point d))) := by | |
| apply integral_mono hi (hiX.add (hiG.const_mul _)) | |
| intro p | |
| change βp.1+Real.sqrt h β’ p.2ββ€βp.1β+Real.sqrt h*βp.2β | |
| simpa only [norm_smul,Real.norm_eq_abs,abs_of_nonneg (Real.sqrt_nonneg _)] using | |
| norm_add_le p.1 (Real.sqrt h β’ p.2) | |
| _ = (β«x,βxβ βgibbs V)+Real.sqrt h*(β«x : Point d,βxβ βstdGaussian (Point d)) := by | |
| rw [integral_add hiX (hiG.const_mul _),integral_const_mul] | |
| simp only [integral_fun_fst,integral_fun_snd,probReal_univ,one_smul] | |
| _ β€ Real.sqrt d+Real.sqrt h*Real.sqrt d := by | |
| exact add_le_add hV.first_moment_bound.2 | |
| (mul_le_mul_of_nonneg_left (stdGaussian_first_moment_bound d) (Real.sqrt_nonneg _)) | |
| _ = _ := by ring | |
| end LogConcaveSampling | |
| end | |
| end | |
| section | |
| noncomputable section | |
| namespace LogConcaveSampling | |
| open MeasureTheory ProbabilityTheory Function | |
| open scoped Classical ENNReal RealInnerProductSpace | |
| variable {d : β} | |
| lemma Admissible.value_difference_bound {V : Point d β β} (hV : Admissible V) | |
| (x g : Point d) {s : β} (hs : 0 β€ s) : | |
| |V (x+s β’ g)-V x|β€ s*βxβ^2+(s+s^2)*βgβ^2 := by | |
| have ht := quadratic_remainder_of_gradient_lipschitz hV.smooth hV.gradient_lipschitz x (s β’ g) | |
| have hi := abs_real_inner_le_norm (s β’ g) (gradient V x) | |
| have hg := hV.gradient_norm_le x | |
| simp only [norm_smul,Real.norm_eq_abs,abs_of_nonneg hs,NNReal.coe_ofNat] at ht hi | |
| have hh := abs_add_le (V (x+s β’ g)-V x-inner β (s β’ g) (gradient V x)) | |
| (inner β (s β’ g) (gradient V x)) | |
| have hm := mul_le_mul_of_nonneg_left hg (mul_nonneg hs (norm_nonneg g)) | |
| have hsq := mul_nonneg hs (sq_nonneg (βxβ-βgβ)) | |
| ring_nf at hh | |
| nlinarith | |
| theorem noisedLaw_tv {V : Point d β β} (hV : Admissible V) {h : β} (hh : 0<h) : | |
| TVAtMost (noisedLaw V h) (gibbs V) (2*(2*Real.sqrt h+h)*d) := by | |
| let := hV.isProbabilityMeasure_gibbs | |
| have hVm := hV.smooth.continuous.measurable | |
| let := jointPosteriorLaw_probability hV h | |
| let H := fun p : Point d Γ Point d => -V p.1-βp.2-p.1β^2/(2*h) | |
| let c := (partition V)β»ΒΉ*EulerDensity.noiseCoefficient d (Real.sqrt h) | |
| have hm : Measurable H := by have := hV.smooth.continuous.measurable; dsimp [H]; fun_prop | |
| have hc0 : cβ 0 := mul_ne_zero (ENNReal.inv_ne_zero.mpr hV.partition_ne_top) | |
| (EulerDensity.noiseCoefficient_positive d (Real.sqrt_pos.mpr hh).ne').ne' | |
| have hct : cβ β€ := ENNReal.mul_ne_top (ENNReal.inv_ne_top.mpr hV.partition_pos.ne') | |
| (EulerDensity.noiseCoefficient_finite _ _) | |
| have hd : jointPosteriorLaw V h=MetropolisBalance.densityLaw volume c H := by | |
| have hd := EulerDensity.joint_density hV.smooth.continuous.measurable | |
| (fun z : Point d => z) measurable_id (Real.sqrt_pos.mpr hh).ne' | |
| simpa only [jointPosteriorLaw,MetropolisBalance.densityLaw,c,H,Real.sq_sqrt hh.le] using hd | |
| have he (p : Point d Γ Point d) : |H (p.1,p.1+Real.sqrt h β’ p.2)-H (p.1+Real.sqrt h β’ p.2,p.1)| | |
| =|V (p.1+Real.sqrt h β’ p.2)-V p.1| := by | |
| dsimp [H] | |
| rw [norm_sub_rev p.1 (p.1+Real.sqrt h β’ p.2)] | |
| congr 1 | |
| ring | |
| let b := fun p : Point d Γ Point d => Real.sqrt h*βp.1β^2+(Real.sqrt h+h)*βp.2β^2 | |
| have hib : Integrable b ((gibbs V).prod (stdGaussian (Point d))) := | |
| (hV.second_moment_bound.1.comp_fst _ |>.const_mul _).add | |
| (((memLp_two_iff_integrable_sq_norm (by fun_prop)).mp (IsGaussian.memLp_two_id (ΞΌ:=stdGaussian (Point d)))).comp_snd _ |>.const_mul _) | |
| have hb (p : Point d Γ Point d) : |V (p.1+Real.sqrt h β’ p.2)-V p.1|β€b p := by | |
| simpa only [Real.sq_sqrt hh.le] using hV.value_difference_bound p.1 p.2 (Real.sqrt_nonneg h) | |
| have hir : Integrable (fun p : Point d Γ Point d => |H p-H p.swap|) (jointPosteriorLaw V h) := by | |
| apply (integrable_map_measure (by fun_prop) (by fun_prop)).2 | |
| apply hib.mono' (by fun_prop) | |
| exact Filter.Eventually.of_forall (fun p => by simpa only [comp_apply,Prod.swap_prod_mk,Real.norm_eq_abs,abs_abs,he] using hb p) | |
| have hr : (β«p,|H p-H p.swap| βjointPosteriorLaw V h)β€(2*Real.sqrt h+h)*d := by | |
| rw [jointPosteriorLaw,integral_map (by fun_prop) (by fun_prop)] | |
| simp only [Prod.swap_prod_mk] | |
| simp_rw [he] | |
| have hi : Integrable (fun p : Point d Γ Point d => |V (p.1+Real.sqrt h β’ p.2)-V p.1|) | |
| ((gibbs V).prod (stdGaussian (Point d))) := | |
| hib.mono' (by fun_prop) (Filter.Eventually.of_forall (fun p => by simpa only [Real.norm_eq_abs,abs_abs] using hb p)) | |
| calc | |
| _ β€ β«p,b p β((gibbs V).prod (stdGaussian (Point d))) := integral_mono hi hib hb | |
| _ = Real.sqrt h*(β«x,βxβ^2 βgibbs V)+(Real.sqrt h+h)*d := by | |
| dsimp only [b] | |
| rw [integral_add] | |
| Β· rw [integral_const_mul,integral_const_mul,integral_fun_fst (fun x : Point d => βxβ^2),integral_fun_snd (fun x : Point d => βxβ^2)] | |
| simp only [probReal_univ,one_smul,stdGaussian_sq_norm] | |
| Β· exact hV.second_moment_bound.1.comp_fst _ |>.const_mul _ | |
| Β· simpa only [id_eq] using ((memLp_two_iff_integrable_sq_norm (by fun_prop)).mp (IsGaussian.memLp_two_id (ΞΌ:=stdGaussian (Point d)))).comp_snd ((gibbs V)) |>.const_mul (Real.sqrt h+h) | |
| _ β€ _ := by nlinarith [mul_le_mul_of_nonneg_left hV.second_moment_bound.2 (Real.sqrt_nonneg h)] | |
| let : IsFiniteMeasure (MetropolisBalance.densityLaw volume c H) := hd βΈ inferInstance | |
| intro A hA | |
| let f : Point d β β := A.indicator (fun _ => 1) | |
| have hf : Measurable f := measurable_const.indicator hA | |
| have hbf (x) : |f x|β€1 := by by_cases hx : xβA <;> simp [f,hx] | |
| have ht := MetropolisBalance.probability_one_step_defect volume hm hc0 hct | |
| (hd βΈ hir) hf (by norm_num : (0:β)β€1) hbf | |
| rw [βhd] at ht | |
| have hs : (β«p : Point d Γ Point d,f p.2 βjointPosteriorLaw V h)=(noisedLaw V h).real A := by | |
| rw [βjointPosterior_snd (V:=V) h,βintegral_map measurable_snd.aemeasurable hf.aestronglyMeasurable] | |
| simp only [f,integral_indicator hA,setIntegral_const,smul_eq_mul,mul_one] | |
| have ht' : (β«p : Point d Γ Point d,f p.1 βjointPosteriorLaw V h)=(gibbs V).real A := by | |
| rw [βjointPosterior_fst (V:=V) h,βintegral_map measurable_fst.aemeasurable hf.aestronglyMeasurable] | |
| simp only [f,integral_indicator hA,setIntegral_const,smul_eq_mul,mul_one] | |
| rw [hs,ht'] at ht | |
| nlinarith | |
| end LogConcaveSampling | |
| end | |
| end | |
| section | |
| noncomputable section | |
| namespace LogConcaveSampling | |
| open MeasureTheory ProbabilityTheory Function | |
| open scoped Classical | |
| variable {d : β} | |
| def descentScale (h : β) (j : β) : β := h/2^j | |
| lemma descentScale_pos {h : β} (hh : 0<h) (j : β) : 0<descentScale h j := by unfold descentScale; positivity | |
| lemma descentScale_le {h : β} (hh : 0β€h) (j : β) : descentScale h jβ€h := by | |
| exact div_le_self hh (one_le_powβ (by norm_num)) | |
| @[simp] lemma descentScale_zero (h : β) : descentScale h 0=h := by simp [descentScale] | |
| lemma descentScale_succ (h : β) (j : β) : descentScale h (j+1)=descentScale h j/2 := by simp [descentScale,pow_succ,div_div] | |
| lemma halving_chain_error {V : Point d β β} (hV : Admissible V) {h e : β} | |
| (hh : 0<h) (hh1 : hβ€1) (he : 0β€e) | |
| (ΞΊ : β β Kernel (Point d) (Point d)) [βj,IsMarkovKernel (ΞΊ j)] | |
| (hc : βj x,TVAtMost (ΞΊ j x) (halvingKernel V hV (descentScale h j) x) | |
| (2*e*(Real.sqrt d+2*Real.sqrt (descentScale h j)*βxβ))) (j : β) : | |
| TVAtMost (changingChain ΞΊ (noisedLaw V h) j) (noisedLaw V (descentScale h j)) | |
| (j*(10*e*Real.sqrt d)) := by | |
| let := noisedLaw_probability hV h | |
| induction j with | |
| | zero => intro A hA; simp [changingChain] | |
| | succ j ih => | |
| let hj := descentScale h j | |
| have hjp : 0<hj := descentScale_pos hh j | |
| have hj1 : hjβ€1 := (descentScale_le hh.le j).trans hh1 | |
| have hs : Real.sqrt hjβ€1 := by simpa using Real.sqrt_le_sqrt hj1 | |
| let := noisedLaw_probability hV hj | |
| let := halvingKernel_markov hV hjp | |
| let ei := fun x : Point d => 2*e*(Real.sqrt d+2*Real.sqrt hj*βxβ) | |
| have hei : Integrable ei (noisedLaw V hj) := by | |
| exact ((integrable_const _).add ((noisedLaw_first_moment hV hj).1.const_mul _)).const_mul _ | |
| have heint : (β«x,ei x βnoisedLaw V hj)β€10*e*Real.sqrt d := by | |
| have hx := (noisedLaw_first_moment hV hj).2 | |
| have hx' : (β«x,βxβ βnoisedLaw V hj)β€2*Real.sqrt d := by | |
| nlinarith [Real.sqrt_nonneg (d:β)] | |
| have hxs : Real.sqrt hj*(β«x,βxβ βnoisedLaw V hj)β€2*Real.sqrt d := by | |
| exact (mul_le_mul_of_nonneg_right hs (integral_nonneg (fun _ => norm_nonneg _))).trans (by simpa using hx') | |
| dsimp only [ei] | |
| rw [integral_const_mul,integral_add (integrable_const _) ((noisedLaw_first_moment hV hj).1.const_mul _),integral_const_mul] | |
| simp only [integral_const,probReal_univ,smul_eq_mul,one_mul] | |
| nlinarith | |
| have hl := TVAtMost.kernel_mixture (noisedLaw V hj) (ΞΊ j) (halvingKernel V hV hj) hei (hc j) | |
| rw [halvingKernel_invariant hV hjp] at hl | |
| have ht := (ih.kernel (ΞΊ j)).trans hl | |
| change TVAtMost _ (noisedLaw V (hj/2)) _ at ht | |
| rw [changingChain,descentScale_succ] | |
| apply ht.mono | |
| push_cast | |
| nlinarith | |
| namespace OracleCompiler | |
| lemma approximateHalvingKernel_program (S : ReservedProgram d) (h : β) | |
| {V : Point d β β} (hV : Admissible V) (x : Point d) : | |
| approximateHalvingKernel S h V hV x=(gaussianTape d S.full.slots).map | |
| (fun g => (halvingProgram S h).run V (x,g)) := by | |
| have hm : Measurable (fun g => (proximalProgram S h).run V (x,g)) := | |
| ((proximalProgram S h).measurable_run V hV.continuous_firstOrderReply.measurable).comp (by fun_prop) | |
| rw [approximateHalvingKernel_apply,approximateProximalKernel_apply,Measure.map_map (by fun_prop) hm] | |
| congr 1 | |
| funext g | |
| rw [halvingProgram,Program.map_run] | |
| rfl | |
| end OracleCompiler | |
| end LogConcaveSampling | |
| end | |
| end | |
| end OAI | |