Download data/lean/OAI/Combinatorics/MatchingCount/Sampling/MetropolisChoice.lean from introvoyz045/math: direct link, hf CLI and curl.
- Browser
- Download file 13.3 kB
-
https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Combinatorics/MatchingCount/Sampling/MetropolisChoice.lean
- Command line
-
hf download hf://datasets/introvoyz045/math/data/lean/OAI/Combinatorics/MatchingCount/Sampling/MetropolisChoice.lean
-
curl -L -o MetropolisChoice.lean https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Combinatorics/MatchingCount/Sampling/MetropolisChoice.lean
13.3 kB
| import Mathlib | |
| import OAI.Combinatorics.MatchingCount.Probability.Tv | |
| import OAI.Combinatorics.MatchingCount.Sampling.Component | |
| namespace OAI | |
| namespace MatchingFPRAS.WeightedOptions | |
| open Finset | |
| variable {Ξ© : Type*} [Fintype Ξ©] [DecidableEq Ξ©] | |
| def metropolisChoice (c : WeightedOptions Ξ©) (w : Ξ© β β) (hw : β a, 0 < w a) (a : Ξ©) : | |
| WeightedOptions Ξ© := c.accept a (fun b => min 1 (w b / w a)) | |
| (fun b => le_min zero_le_one (div_nonneg (hw b).le (hw a).le)) | |
| (fun _ => min_le_left _ _) | |
| lemma real_law_sum (c : WeightedOptions Ξ©) : β b, (c.law b : β)=1 := by | |
| exact_mod_cast c.law_sum | |
| lemma row_eq_of_off (f g : Ξ© β β) (hf : β x, f x=1) (hg : β x, g x=1) | |
| (a : Ξ©) (h : β b, aβ b β f b=g b) : f=g := by | |
| funext b | |
| by_cases hab : a=b | |
| Β· subst b | |
| have he : β b β univ.erase a, f b = β b β univ.erase a, g b := by | |
| apply sum_congr rfl | |
| intro b hb | |
| exact h b (ne_of_mem_erase hb).symm | |
| rw [βsum_erase_add _ _ (mem_univ a)] at hf hg | |
| linarith | |
| Β· exact h b hab | |
| lemma min_ratio (x y : β) (hx : 0 < x) : min (1:β) (y/x) = min x y / x := by | |
| by_cases h : xβ€y | |
| Β· rw [min_eq_left ((le_div_iffβ hx).mpr (by simpa using h)),min_eq_left h,div_self hx.ne'] | |
| Β· rw [min_eq_right ((div_le_one hx).mpr (le_of_not_ge h)),min_eq_right (le_of_not_ge h)] | |
| theorem metropolisChoice_law (P : Replica.SymmetricProposal Ξ©) (c : Ξ© β WeightedOptions Ξ©) | |
| (hc : β a b, ((c a).law b : β)=P.q a b) | |
| (w : Ξ© β β) (hw : β a, 0 < w a) (a b : Ξ©) : | |
| (((c a).metropolisChoice w hw a).law b : β) = | |
| Replica.metropolis P (fun x => (w x:β)) a b := by | |
| have hpos : β x, (0:β)<w x := fun x => by exact_mod_cast hw x | |
| have hsum := real_law_sum ((c a).metropolisChoice w hw a) | |
| have he := row_eq_of_off _ _ hsum (Replica.metropolis_row P _ hpos a) a (fun b hab => ?_) | |
| Β· exact congrFun he b | |
| rw [metropolisChoice,accept_law,ite_eq_right hab,add_zero] | |
| push_cast | |
| rw [hc,min_ratio _ _ (hpos a)] | |
| simp only [Replica.metropolis,Replica.metropolisCapacity,ite_eq_right hab,Replica.offCapacity] | |
| ring | |
| omit [Fintype Ξ©] [DecidableEq Ξ©] in | |
| lemma metropolisChoice_size (c : WeightedOptions Ξ©) (w : Ξ© β β) (hw : β a, 0 < w a) (a : Ξ©) : | |
| (c.metropolisChoice w hw a).size = 2*c.size := by | |
| simp only [metropolisChoice,accept] | |
| omega | |
| end MatchingFPRAS.WeightedOptions | |
| namespace MatchingFPRAS.Variation | |
| open Finset | |
| open scoped Classical | |
| variable {A B C : Type*} [Fintype A] [Fintype B] [Fintype C] | |
| noncomputable def uniform (A : Type*) [Fintype A] (_ : A) : β := 1/Fintype.card A | |
| lemma uniform_nonneg (a : A) : 0β€uniform A a := by unfold uniform; positivity | |
| lemma uniform_sum [Nonempty A] : β a : A,uniform A a=1 := by | |
| simp only [uniform,sum_const,nsmul_eq_mul] | |
| exact mul_one_div_cancel (by exact_mod_cast Fintype.card_ne_zero : (Fintype.card A:β)β 0) | |
| noncomputable def imageLaw (f : A β B) : B β β := push (uniform A) f | |
| omit [Fintype B] in | |
| lemma imageLaw_nonneg (f : A β B) (b : B) : 0 β€ imageLaw f b := push_nonneg _ uniform_nonneg _ _ | |
| lemma imageLaw_sum [Nonempty A] (f : A β B) : β b,imageLaw f b=1 := push_sum _ uniform_sum _ | |
| omit [Fintype B] in | |
| lemma imageLaw_eq_count (f : A β B) (b : B) : | |
| imageLaw f b=((univ.filter (fun a=>f a=b)).card:β)/Fintype.card A := by | |
| simp only [imageLaw,push,uniform] | |
| rw [show (β x,if f x=b then (1:β)/Fintype.card A else 0)= | |
| (β x,if f x=b then (1:β) else 0)/Fintype.card A by | |
| simp only [sum_div,ite_div,zero_div]] | |
| simp | |
| lemma imageLaw_equiv (e : A β B) : imageLaw e=uniform B := by | |
| funext b | |
| unfold imageLaw push uniform | |
| rw [e.sum_comp (fun x=>if x=b then (1:β)/Fintype.card A else 0)] | |
| simp [Fintype.card_congr e] | |
| omit [Fintype C] in | |
| lemma imageLaw_comp_equiv (e : A β B) (f : B β C) : imageLaw (f β e)=imageLaw f := by | |
| funext c | |
| unfold imageLaw push uniform | |
| rw [Fintype.card_congr e,βe.sum_comp (fun b=>if f b=c then (1:β)/Fintype.card B else 0)] | |
| rfl | |
| omit [Fintype C] in | |
| lemma push_comp (p : A β β) (f : A β B) (g : B β C) : | |
| push (push p f) g=push p (g β f) := by | |
| funext c | |
| unfold push | |
| have h (b : B) : (if g b=c then β a,if f a=b then p a else 0 else 0)= | |
| β a,if g b=c then (if f a=b then p a else 0) else 0 := by | |
| by_cases hh : g b=c <;> simp [hh] | |
| simp_rw [h] | |
| rw [sum_comm] | |
| apply sum_congr rfl | |
| intro a _ | |
| simp only [βite_and] | |
| rw [show (fun b=>if g b=c β§ f a=b then p a else 0)= | |
| (fun b=>if f a=b then (if g b=c then p a else 0) else 0) by | |
| funext b; split_ifs <;> simp_all] | |
| simp | |
| omit [Fintype C] in | |
| lemma imageLaw_comp (f : A β B) (g : B β C) : imageLaw (g β f)=push (imageLaw f) g := | |
| (push_comp _ _ _).symm | |
| lemma uniform_prod (a : AΓB) : uniform (AΓB) a=uniform A a.1*uniform B a.2 := by | |
| simp [uniform,Fintype.card_prod,Nat.cast_mul,mul_comm] | |
| variable {D : Type*} [Fintype D] | |
| omit [Fintype C] in | |
| lemma imageLaw_pair (f : A β B β C) : | |
| imageLaw (fun x : AΓB=>f x.1 x.2)=bind (uniform A) (fun a=>imageLaw (f a)) := by | |
| funext c | |
| simp only [imageLaw,push,bind,Fintype.sum_prod_type,uniform_prod,mul_sum] | |
| apply sum_congr rfl | |
| intro a _ | |
| apply sum_congr rfl | |
| intro b _ | |
| split_ifs <;> simp | |
| omit [Fintype C] in | |
| lemma bind_push (p : A β β) (f : A β B) (k : B β C β β) : | |
| bind (push p f) k=bind p (fun a=>k (f a)) := by | |
| funext c | |
| simp only [bind,push,sum_mul,ite_mul,zero_mul] | |
| rw [sum_comm] | |
| apply sum_congr rfl | |
| intro a _ | |
| simp | |
| omit [Fintype C] in | |
| lemma imageLaw_adaptive_pair (d : A β D) (f : D β B β C) : | |
| imageLaw (fun x : AΓB=>f (d x.1) x.2)=bind (imageLaw d) (fun a=>imageLaw (f a)) := by | |
| rw [imageLaw_pair (fun a b=>f (d a) b), imageLaw,bind_push] | |
| theorem uniform_sigma [Nonempty A] {E : B β Type*} [β b,Fintype (E b)] | |
| (e : (Ξ£ b,E b) β A) (z : A) : | |
| push (sigmaLift (fun b=>(Fintype.card (E b):β)/Fintype.card A) | |
| (fun b=>uniform (E b))) e z=uniform A z := by | |
| have h (s : Sigma E) : sigmaLift (fun b=>(Fintype.card (E b):β)/Fintype.card A) | |
| (fun b=>uniform (E b)) s=uniform (Sigma E) s := by | |
| have hn : (Fintype.card (E s.1):β)β 0 := by | |
| let : Nonempty (E s.1) := β¨s.2β© | |
| exact_mod_cast Fintype.card_ne_zero | |
| simp only [sigmaLift,uniform,Fintype.card_congr e] | |
| field_simp | |
| have hh := congrArg (fun p=>push p e z) (funext h) | |
| rw [hh] | |
| exact congrFun (imageLaw_equiv e) z | |
| open Finset | |
| open scoped Classical | |
| variable {A B C : Type*} [Fintype A] [Fintype B] [Fintype C] | |
| omit [Fintype C] in | |
| lemma imageLaw_ignore [Nonempty A] (f : B β C) : | |
| imageLaw (fun x : AΓB=>f x.2)=imageLaw f := by | |
| rw [imageLaw_pair (fun _ b=>f b)] | |
| funext c | |
| simp only [bind,βsum_mul,uniform_sum,one_mul] | |
| omit [Fintype B] in | |
| lemma push_sigmaLift {E : A β Type*} [β a,Fintype (E a)] | |
| (p : A β β) (k : β a,E a β β) (f : (Ξ£ a,E a) β B) : | |
| push (sigmaLift p k) f=bind p (fun a=>push (k a) (fun e=>f β¨a,eβ©)) := by | |
| funext b | |
| simp only [push,sigmaLift,bind,Fintype.sum_sigma,mul_sum] | |
| apply sum_congr rfl | |
| intro a _ | |
| apply sum_congr rfl | |
| intro e _ | |
| split_ifs <;> simp | |
| lemma imageLaw_subsingleton [Nonempty A] [Subsingleton B] (f : A β B) : | |
| imageLaw f=uniform B := by | |
| let : Nonempty B := β¨f (Classical.choice (inferInstance : Nonempty A))β© | |
| funext b | |
| simp only [imageLaw,push,Subsingleton.elim (f _) b,ite_true,uniform_sum] | |
| let : Unique B := β¨β¨bβ©,fun x=>Subsingleton.elim x bβ© | |
| simp [uniform] | |
| lemma tv_self (p : A β β) : tv p p=0 := by simp [tv] | |
| open scoped Classical | |
| lemma imageLaw_fin_skip {B C D : Type*} [Fintype B] [Nonempty B] [Fintype C] [Fintype D] | |
| (n : β) (g : (Fin (n+2) β B) β C) (d : B β D) (f : D β (Fin n β B) β C) | |
| (hg : β bits,g bits=f (d (bits (Fin.last (n+1)))) (fun i=>bits i.castSucc.castSucc)) : | |
| imageLaw g=bind (imageLaw d) (fun c=>imageLaw (f c)) := by | |
| let e : (BΓ(BΓ(Fin n β B))) β (Fin (n+2) β B) := | |
| (Equiv.prodCongr (Equiv.refl B) (Fin.snocEquiv (fun _ : Fin (n+1)=>B))).trans | |
| (Fin.snocEquiv (fun _ : Fin (n+2)=>B)) | |
| have hE : g β e=(fun x : BΓ(BΓ(Fin nβB))=>f (d x.1) x.2.2) := by | |
| funext x | |
| rw [Function.comp_apply,hg] | |
| simp [e,Fin.snocEquiv] | |
| rw [βimageLaw_comp_equiv e,hE,imageLaw_adaptive_pair d (fun c (u : BΓ(Fin nβB))=>f c u.2)] | |
| simp_rw [imageLaw_ignore] | |
| end MatchingFPRAS.Variation | |
| namespace MatchingFPRAS.WeightedOptions | |
| open Finset Variation FiniteChoice | |
| variable {Ξ© : Type*} [Fintype Ξ©] [DecidableEq Ξ©] | |
| def draw (c : WeightedOptions Ξ©) (k : β) (bits : Fin k β Bool) : Ξ© := | |
| c.out (chooseBits c.mass c.total k bits) | |
| omit [Fintype Ξ©] [DecidableEq Ξ©] in | |
| lemma chooseBits_imageLaw (c : WeightedOptions Ξ©) (k : β) : | |
| imageLaw (chooseBits c.mass c.total k) = fun i => (tapeLaw c.mass c.total k i:β) := by | |
| funext i | |
| rw [imageLaw_eq_count] | |
| simp only [Fintype.card_fun,Fintype.card_fin,Fintype.card_bool,Nat.cast_pow, | |
| Nat.cast_ofNat,tapeLaw,Rat.cast_div,Rat.cast_natCast,Rat.cast_pow,Rat.cast_ofNat] | |
| congr 2 | |
| congr 1 | |
| ext bits | |
| simp only [mem_filter,mem_univ,true_and] | |
| omit [Fintype Ξ©] in | |
| lemma realLaw_push (c : WeightedOptions Ξ©) : | |
| (fun b => (c.law b:β))=push (fun i=>(c.mass i:β)) c.out := by | |
| funext b | |
| unfold law push | |
| simp only [Rat.cast_sum,apply_ite,Rat.cast_zero] | |
| theorem draw_tv (c : WeightedOptions Ξ©) (k : β) : | |
| tv (imageLaw (c.draw k)) (fun b=>(c.law b:β)) β€ 2*c.size/(2:β)^k := by | |
| have h := fixed_bit_choice c.mass c.nonneg c.total k | |
| rw [βtapeLaw_eq] at h | |
| have hr : tv (fun i=>(tapeLaw c.mass c.total k i:β)) (fun i=>(c.mass i:β)) | |
| β€ 2*c.size/(2:β)^k := by | |
| change (β i : Fin c.size, |(tapeLaw c.mass c.total k i:β)-(c.mass i:β)|)/2 β€ _ | |
| have hh := (Rat.cast_le (K := β)).mpr h | |
| simpa only [Rat.cast_div,Rat.cast_sum,Rat.cast_abs,Rat.cast_sub,Rat.cast_mul, | |
| Rat.cast_ofNat,Rat.cast_natCast,Rat.cast_pow] using hh | |
| have hd : c.draw k = c.out β chooseBits c.mass c.total k := rfl | |
| rw [hd,imageLaw_comp,realLaw_push,chooseBits_imageLaw] | |
| exact (push_contraction _ _ _).trans hr | |
| def runDraw (c : β β Ξ© β WeightedOptions Ξ©) (z : Ξ©) (k : β) : | |
| (n : β) β (Fin n β Fin k β Bool) β Ξ© | |
| | 0, _ => z | |
| | n+1, bits => (c n (runDraw c z k n (Fin.init bits))).draw k (bits (Fin.last n)) | |
| noncomputable def exactRun (c : β β Ξ© β WeightedOptions Ξ©) (z : Ξ©) : β β Ξ© β β | |
| | 0 => fun b => if z=b then 1 else 0 | |
| | n+1 => bind (exactRun c z n) (fun s b => ((c n s).law b:β)) | |
| lemma exactRun_nonneg (c : β β Ξ© β WeightedOptions Ξ©) (z : Ξ©) (n : β) (b : Ξ©) : | |
| 0β€exactRun c z n b := by | |
| induction n generalizing b with | |
| | zero => simp only [exactRun]; split_ifs <;> norm_num | |
| | succ n ih => exact bind_nonneg _ _ ih (fun s b => by exact_mod_cast (c n s).law_nonneg b) b | |
| lemma exactRun_sum (c : β β Ξ© β WeightedOptions Ξ©) (z : Ξ©) (n : β) : | |
| β b,exactRun c z n b=1 := by | |
| induction n with | |
| | zero => simp [exactRun] | |
| | succ n ih => exact bind_sum _ _ ih (fun s => real_law_sum (c n s)) | |
| omit [DecidableEq Ξ©] in | |
| lemma runDraw_law (c : β β Ξ© β WeightedOptions Ξ©) (z : Ξ©) (k n : β) : | |
| imageLaw (runDraw c z k (n+1)) = bind (imageLaw (runDraw c z k n)) | |
| (fun s => imageLaw ((c n s).draw k)) := by | |
| let e : ((Fin n β Fin k β Bool) Γ (Fin k β Bool)) β (Fin (n+1) β Fin k β Bool) := | |
| (Equiv.prodComm _ _).trans (Fin.snocEquiv (fun _ => Fin k β Bool)) | |
| have h : runDraw c z k (n+1) β e = | |
| (fun u => (c n (runDraw c z k n u.1)).draw k u.2) := by | |
| funext u | |
| change (c n (runDraw c z k n (Fin.init (Fin.snoc (Ξ± := fun _ : Fin (n+1) => Fin k β Bool) u.1 u.2)))).draw k | |
| (Fin.snoc (Ξ± := fun _ : Fin (n+1) => Fin k β Bool) u.1 u.2 (Fin.last n)) = _ | |
| simp only [Fin.init_snoc,Fin.snoc_last] | |
| rw [βimageLaw_comp_equiv e,h] | |
| exact imageLaw_adaptive_pair (runDraw c z k n) (fun s bits => (c n s).draw k bits) | |
| theorem runDraw_tv (c : β β Ξ© β WeightedOptions Ξ©) (z : Ξ©) (k R n : β) | |
| (hR : β j s, (c j s).size β€ R) : | |
| tv (imageLaw (runDraw c z k n)) (exactRun c z n) β€ 2*n*R/(2:β)^k := by | |
| induction n with | |
| | zero => | |
| have h : imageLaw (runDraw c z k 0)=exactRun c z 0 := by | |
| funext b | |
| simp [imageLaw,push,Variation.uniform,runDraw,exactRun] | |
| rw [h,tv_self] | |
| norm_num | |
| | succ n ih => | |
| rw [runDraw_law] | |
| change tv (bind _ _) (bind _ _)β€_ | |
| let p := imageLaw (runDraw c z k n) | |
| let q := exactRun c z n | |
| let u := fun s => imageLaw ((c n s).draw k) | |
| let v := fun s b => ((c n s).law b:β) | |
| have hu : β s, tv (u s) (v s) β€ 2*R/(2:β)^k := by | |
| intro s | |
| exact ((c n s).draw_tv k).trans (div_le_div_of_nonneg_right | |
| (mul_le_mul_of_nonneg_left (by exact_mod_cast hR n s) (by norm_num)) (by positivity)) | |
| have h1 := bind_perturbation p u v (imageLaw_nonneg _) (imageLaw_sum _) _ hu | |
| have h2 := bind_contraction p q v | |
| (fun s b => by dsimp [v]; exact_mod_cast (c n s).law_nonneg b) (fun s => real_law_sum (c n s)) | |
| calc | |
| _ β€ tv (bind p u) (bind p v)+tv (bind p v) (bind q v) := tv_triangle _ _ _ | |
| _ β€ 2*R/(2:β)^k+2*n*R/(2:β)^k := add_le_add h1 (h2.trans ih) | |
| _ = _ := by push_cast; ring | |
| end MatchingFPRAS.WeightedOptions | |
| end OAI | |