math / data /lean /OAI /Combinatorics /MatchingCount /Sampling /MetropolisChoice.lean
introvoyz045's picture
Migrated from GitHub
4a56cd6 verified
Raw History Blame Contribute Delete
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