Download data/lean/OAI/Computability/Logspace/ProbabilityHierarchy.lean from introvoyz045/math: direct link, hf CLI and curl.
- Browser
- Download file 50.8 kB
-
https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Computability/Logspace/ProbabilityHierarchy.lean
- Command line
-
hf download hf://datasets/introvoyz045/math/data/lean/OAI/Computability/Logspace/ProbabilityHierarchy.lean
-
curl -L -o ProbabilityHierarchy.lean https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Computability/Logspace/ProbabilityHierarchy.lean
50.8 kB
import OAI.Computability.Logspace.AffineSampling
namespace OAI
/-!
Strictly forward substochastic matrices and the hierarchy of probability rewards.
From OpenAI, *Exact Derandomization of Logarithmic Space: L = RL = BPL*.
-/
set_option autoImplicit true
section
/-! The exact hierarchy. Nat IDs, ordered (strictly forward). -/
namespace ExactDerandomization
noncomputable section
open Finset Analysis
namespace Hie
abbrev Domain := Finset β
abbrev Vec := β β β
abbrev Mat := β β Vec
def mv (S : Domain) (C : Mat) (v : Vec) : Vec := fun x=> β yβS,C x y*v y
def mm (S : Domain) (C D : Mat) : Mat := fun x y=> β zβS,C x z * D z y
def wt (S : Domain) (C : Mat) x := β yβS, C x y
def nodes (S : Domain) (C : Mat) :=
S.filter fun x=> β yβS, 0<C x y β¨ 0<C y x
/-- support restricted to active set S' -/
structure SB (S S' : Domain) (f : β) (k : β) (C : Mat) : Prop where
nn : β x y, 0β€ C x y
key : β x y, C x yβ€k*f
row : β x, wt S C x β€ k
loc : β x y, 0<C x y β xβS' β§ yβS' β§ x<y
abbrev Step (S) (f) (C) := SB S S f 1 C
lemma wt_mm (S : Domain) (C D : Mat) (x) :
wt S (mm S C D) x = β yβS, C x y * wt S D y := by
simp_rw [wt,mm,mul_sum]
rw [sum_comm]
lemma mm_mv (S:Domain) (C D : Mat) (f : Vec) :
mv S (mm S C D) f=mv S C (mv S D f) := by
ext x; simp only [mv,mm,sum_mul,mul_sum,mul_assoc]; rw [sum_comm]
lemma restrict {S S' : Domain} {f k C} (h : SB S S f k C)
(e : nodes S C β S') : SB S S' f k C :=
β¨h.nn,h.key,h.row,fun x y hk=>by
obtain β¨hx,hy,hzβ© := h.loc x y hk
exact β¨e (mem_filter.2 β¨hx,y,hy,Or.inl hkβ©),
e (mem_filter.2 β¨hy,x,hx,Or.inr hkβ©),hzβ©β©
lemma relax {S S' S'' : Domain} {f k C} (h : SB S S' f k C) (e : S'β S'') :
SB S S'' f k C := by
refine β¨h.nn,h.key,h.row,fun x y hk=>?_β©
obtain β¨hx,hy,hzβ© := h.loc x y hk
exact β¨e hx,e hy,hzβ©
def zero : Mat := fun _ _=>0
lemma sb0 (S S' f) : SB S S' f 0 zero := by
constructor <;> simp [wt,zero]
def combine (S : Domain) (C F : Mat) := C+mm S F C
lemma data_bounds {S S' f k C F} (h : SB S S' f 1 C) (e : SB S S' f k F)
(hf:0<f) : SB S S' f (k+1) (combine S C F) := by
have h0 (x y) : 0β€ mm S F C x y :=
sum_nonneg (fun z _=> mul_nonneg (e.nn x z) (h.nn z y))
constructor
Β· intro x y; exact add_nonneg (h.nn x y) (h0 x y)
Β· intro x y
have hee : mm S F C x y β€ wt S F x *f := by
unfold wt mm; rw [sum_mul]
exact sum_le_sum fun z _=>mul_le_mul_of_nonneg_left
(by simpa using h.key z y) (e.nn x z)
have ht := e.row x
change C x y+mm S F C x yβ€_
nlinarith [h.key x y]
Β· intro x
have hz : wt S (mm S F C) x β€ wt S F x := by
rw [wt_mm]; unfold wt
apply sum_le_sum
intro y _
exact (mul_le_mul_of_nonneg_left (h.row y) (e.nn x y)).trans_eq (by simp)
simp only [wt,combine,Pi.add_apply, sum_add_distrib]
change wt S C x+wt S (mm S F C) x β€ _
linarith [h.row x,e.row x]
Β· intro x y he
by_cases hx: 0<C x y
Β· exact h.loc x y hx
change 0<C x y+_ at he
have hp : 0 < mm S F C x y := by linarith
obtain β¨z,hz,htβ© : β zβS, 0<F x z*C z y := by
by_contra! hn
exact (not_lt_of_ge (show mm S F C x yβ€0 from sum_nonpos hn)) hp
have hs := (mul_pos_iff.mp ht).resolve_right (by have:=h.nn z y; grind)
obtain β¨h1,h2,h3β© := h.loc z y hs.2
obtain β¨h4,h5,h6β© := e.loc x z hs.1
exact β¨h4,h2,h6.trans h3β©
abbrev A : β := 1/4096
def Q := shoulder A A
def correct (f : β) (G : Mat) (g : Vec) : Mat := fun x y=> f*Q (G x y/f)*g y
lemma Qnn (t) : 0β€Q t :=
(le_max_left _ _).trans (shoulder_spec A A t (by norm_num [A])).1
lemma Qhi (t) : Q t β€ max 0 (t-A) :=
(shoulder_spec A A t (by norm_num [A])).2
lemma Qlo (t) : t-2*Aβ€Q t :=
le_trans (b:=max 0 (t-A-A)) (by grind)
(shoulder_spec A A t (by norm_num [A])).1
lemma Qic : Ic 5 Q := shoulder_ic A A (by norm_num [A])
lemma Fbounds {f G g} (hp : 0<f) (h : β x, in01 (g x)) (x y) :
0β€ correct f G g x y β§
correct f G g x y β€ max 0 (G x y-A*f) := by
unfold correct
have hn := Qnn (G x y/f)
have hk := h y
refine β¨mul_nonneg (mul_nonneg (le_of_lt hp) hn) hk.1,?_β©
have hu := mul_le_mul_of_nonneg_left hk.2
(show 0β€f*Q (G x y/f) by positivity)
rw [mul_one] at hu
have e := Qhi (G x y/f)
have hs := mul_le_mul_of_nonneg_left e (le_of_lt hp)
have hz : f*(G x y/f-A)=G x y-A*f := by field_simp
rcases le_total 0 (G x y/f-A) with he|he
Β· rw [max_eq_right he,hz] at hs
exact (hu.trans hs).trans (le_max_right _ _)
Β· rw [max_eq_left he,mul_zero] at hs
exact (hu.trans hs).trans (le_max_left _ _)
lemma Fle {f G g} (hp : 0<f) (h : β x,in01 (g x)) {x y} (e : 0β€G x y) :
correct f G g x y β€ G x y := by
refine le_trans (Fbounds hp h x y).2 (max_le e ?_)
have : 0 β€ A*f := mul_nonneg (by norm_num [A]) (le_of_lt hp)
linarith
lemma Fsb {f G g S S' k} (hp : 0<f) (h : β x,in01 (g x)) (e : SB S S' f k G) :
SB S S' f k (correct f G g) := by
have he (x y) := Fle hp h (e.nn x y)
exact β¨fun x y=> (Fbounds hp h x y).1,fun x y=> le_trans (he x y) (e.key x y),
fun x=> le_trans (sum_le_sum (fun i _=>he x i)) (e.row x),
fun x y hh=>e.loc x y (lt_of_lt_of_le hh (he x y))β©
lemma Fgrow {S S' f k C} {F F' g g'} (h : SB S S' f k C) (hp : 0<f)
(e : β x,0 β€ g x) (hg : g β€ g') (ht : Fβ€F') :
correct f (combine S C F) g β€ correct f (combine S C F') g' := by
intro x y
have hu : combine S C F x yβ€ combine S C F' x y := by
exact add_le_add_right (
sum_le_sum fun i _=>mul_le_mul_of_nonneg_right (ht x i) (h.nn i y)) _
have he := (Qic _ _ (div_le_div_of_nonneg_right hu (le_of_lt hp))).1
exact mul_le_mul (mul_le_mul_of_nonneg_left he (le_of_lt hp))
(hg y) (e y) (mul_nonneg (le_of_lt hp) (Qnn _))
def adjust (S : Domain) (f : β) (g : Vec) (C : Mat) : β β Mat
| 0=>zero
| n+1=>correct f (combine S C (adjust S f g C n)) g
lemma csr {f C g S S'} (hp : 0<f) (h : β x,in01 (g x)) (hc : SB S S' f 1 C) (t : β) :
SB S S' f t (adjust S f g C t) := by
induction t with
| zero=> simpa [adjust] using sb0 S S' f
| succ t ih=>
simpa [adjust] using Fsb hp h (data_bounds hc ih hp)
lemma mono_steps {f C g S S'} (hp : 0<f) (h : β x,in01 (g x)) (hc : SB S S' f 1 C) :
Monotone (adjust S f g C) := by
apply monotone_nat_of_le_succ; intro t
induction t with
| zero=> exact (csr hp h hc 1).nn
| succ t ih=> exact Fgrow hc hp (fun i=>(h i).1) le_rfl ih
def residual (S : Domain) (C F : Mat) :=
combine S C F-F
lemma substochastic {f S C g S'} (hp : 0<f) (h : β x,in01 (g x)) (hc : SB S S' f 1 C)
(t : β) :
let F := adjust S f g C t
SB S S' ((t+1:β)*f) 1 (residual S C F) β§
(β x y, 0<F x y β A*fβ€residual S C F x y) := by
let F:=adjust S f g C t
have he := csr hp h hc t; change SB S S' f t F at he
have hv := data_bounds hc he hp
let H:=combine S C F
have hsub (x y) : F x y β€ max 0 (H x y-A*f) := by
have he := mono_steps hp h hc (show tβ€t+1 by omega)
exact le_trans (he x y) (Fbounds hp h x y).2
let T:=residual S C F
have hk (x y) : T x y=H x y-F x y := rfl
have hm (x y) (hg:0<F x y) : A*f β€ T x y := by
rw [hk]; have he := hsub x y
grind
refine β¨?_,hmβ©
have hu (x y) : T x y β€ H x y := by rw [hk]; exact sub_le_self _ (he.nn _ _)
constructor
Β· intro x y
rcases le_total (F x y) 0 with h0|h0
Β· change 0β€T x y
have eh : 0β€H x y:=hv.nn x y
rw [hk]; linarith
Β· rcases eq_or_lt_of_le h0 with h'|h'
Β· change 0β€T x y
rw [hk,β h',sub_zero]; exact hv.nn x y
Β· exact le_trans (mul_nonneg (by norm_num [A]) (le_of_lt hp)) (hm x y h')
Β· intro x y
simpa using le_trans (hu x y) (hv.key x y)
Β· intro x
have h1 : wt S (mm S F C) xβ€ wt S F x := by
rw [wt_mm]
exact sum_le_sum fun i _=>by simpa using
mul_le_mul_of_nonneg_left (hc.row i) (he.nn x i)
have e : wt S T x=wt S C x+wt S (mm S F C) x-wt S F x := by
simp [T,residual,wt,combine,sum_sub_distrib,sum_add_distrib]
change wt S T xβ€_
rw [e]; linarith [hc.row x]
Β· intro x y hi; exact hv.loc x y (lt_of_lt_of_le hi (hu x y))
end Hie
end
end ExactDerandomization
end
section
namespace ExactDerandomization
noncomputable section
namespace Hie
open Analysis Finset
abbrev diff (S : Domain) (f : β) (g : Vec) (C : Mat) (n : β) :=
adjust S f g C (n+1) - adjust S f g C n
lemma transfer_diff {S f g C} (hc : Step S f C) (hf : 0<f) (hg : β i,in01 (g i))
(n x y : β) : diff S f g C (n+1) x y β€ 5 * mm S (diff S f g C n) C x y := by
let v t := combine S C (adjust S f g C t) x y
let h t := diff S f g C t x y
have H : 0 β€ mm S (diff S f g C n) C x y :=
sum_nonneg fun i _=> mul_nonneg (sub_nonneg.2 (mono_steps hf hg hc n.le_succ x i)) (hc.nn i y)
have hdiff : v (n+1)-v n = mm S (diff S f g C n) C x y := by
simp only [v,diff,combine,Pi.add_apply,Pi.sub_apply,mm,sum_sub_distrib,sub_mul]
ring
have he : v n/f β€ v (n+1)/f := div_le_div_of_nonneg_right (by linarith) (by positivity)
obtain β¨h1,h2β© := Qic _ _ he
have hu : h (n+1)=f*(Q (v (n+1)/f)-Q (v n/f))*g y := by
change f*_ *_-f*_ *_=_; ring
change h (n+1) β€ _
have hv : f*(v (n+1)/f - v n/f)=mm S (diff S f g C n) C x y := by rw [β hdiff]; field_simp
rw [hu]
have hh := mul_le_mul_of_nonneg_left (hg y).2
(show 0β€f*(Q (v (n+1)/f)-Q (v n/f)) by exact mul_nonneg (le_of_lt hf) (sub_nonneg.2 h1))
have ht := mul_le_mul_of_nonneg_left h2 (le_of_lt hf)
linarith
def sub (S : Domain) (y : β) := S.filter (fun x=>x<y)
lemma sub_choose (S : Domain) (y k : β) :
β xβsub S y, ((sub S x).card.choose k : β) = ((sub S y).card.choose (k+1) : β) := by
induction y with
| zero => simp [sub]
| succ y ih =>
by_cases he:yβ S
Β· have e : sub S (y+1)=insert y (sub S y) := by ext x; simp [sub]; grind
have hu : yβsub S y := by simp [sub]
rw [e,sum_insert hu,ih,card_insert_of_notMem hu]
push_cast [Nat.choose]
ring
Β· rw [show sub S (y+1)=sub S y by ext x; simp [sub]; grind]
exact ih
lemma factorial {S f g C} (hc : Step S f C) (hf : 0<f) (hg : β i,in01 (g i))
(T x y : β) : diff S f g C T x y β€ f*((5/A)^T/T.factorial) := by
let U t := adjust S f g C t
let R := residual S C (U (T+1))
let M := S.filter (fun y=>0<U (T+1) x y)
obtain β¨hsb,hminβ© := substochastic hf hg hc (T+1)
have hcap : M.card*(A*f) β€ (1:β) := by
calc
(M.card:β)*(A*f) = β _y β M, A*f := by simp
_ β€ β iβM, R x i := sum_le_sum fun i hi=>hmin x i (mem_filter.1 hi).2
_ β€ β iβS,R x i := sum_le_sum_of_subset_of_nonneg (filter_subset ..) fun i _ _=>hsb.nn x i
_ β€ 1 := hsb.row x
let D t := diff S f g C t
have HD (t z) : 0β€D t x z := sub_nonneg.2 (mono_steps hf hg hc (Nat.le_add_right _ 1) x z)
have E (t : β) (h : t β€ T) :
β y, D t x y β€ f * (5*f)^t * ((sub M y).card.choose t) := by
induction t with
| zero =>
have hp := (csr hf hg hc 1).key
intro y
simp
simpa [D,diff,zero,adjust] using hp x y
| succ t ih =>
intro y
refine le_trans (transfer_diff hc hf hg t x y) ?_
have he : mm S (D t) C x y = β iβsub M y, D t x i*C i y := by
have ht : sub M y β S:= fun z hz=> (mem_filter.1 (mem_filter.1 hz).1).1
apply (sum_subset ht ?_).symm
intro z hz hy
by_cases H: 0<D t x z
Β· have hk : 0<U (T+1) x z := by
have ht : 0 β€ U t x z:=(csr hf hg hc t).nn x z
have hu : U (t+1) x zβ€U (T+1) x z:=mono_steps hf hg hc (by omega) x z
change 0<U (t+1) x z-U t x z at H
linarith
have hv : yβ€z := by
by_contra! hv
exact hy (mem_filter.2 β¨mem_filter.2 β¨hz,hkβ©,hvβ©)
have hs : C z y=0 := by
by_contra e
have hi := (hc.loc z y (lt_of_le_of_ne (hc.nn _ _) (Ne.symm e))).2.2
omega
simp [hs]
Β· have h0 : D t x z=0:=le_antisymm (le_of_not_gt H) (HD t z)
simp [h0]
change 5*mm S (D t) C x y β€ _
rw [he,mul_sum]
calc
_ β€ β iβsub M y, f*(5*f)^(t+1) * ((sub M i).card.choose t) := by
apply sum_le_sum; intro i _
have ht := ih (by omega) i
rw [pow_succ]
calc
_ β€ 5*(D t x i*f) :=
mul_le_mul_of_nonneg_left
(mul_le_mul_of_nonneg_left (by simpa using hc.key i y) (HD t i)) (by norm_num)
_ β€ 5*(f*(5*f)^t*((sub M i).card.choose t)*f) := by gcongr
_ = _ := by ring
_ = _ := by rw [β mul_sum,sub_choose]
refine le_trans (E T le_rfl y) ?_
let n : β := (sub M y).card
have he := Nat.choose_le_pow_div (Ξ±:=β) T n
have ht : 5*f*(n:β) β€ 5/A := by
have hu : (n:β) β€ M.card := Nat.cast_le.mpr (card_le_card (filter_subset ..))
unfold A at *; nlinarith
change f*(5*f)^T*(n.choose T) β€ _
have hF := pow_le_pow_leftβ (show 0β€ 5*f*(n:β) by positivity) ht T
calc _ β€ f*(5*f)^T*((n:β)^T/T.factorial) := by gcongr
_ = f*((5*f*(n:β))^T/T.factorial) := by ring
_ β€ _ := by gcongr
def kOK (T : β) := 0<T β§ (5/A)^T/T.factorial β€ (1/1024:β)
lemma can_cut : β T : β, kOK T := by
have h := FloorSemiring.tendsto_pow_div_factorial_atTop (5/A)
obtain β¨b,hbβ© := Filter.eventually_atTop.1
(h.eventually_lt_const (show 0<(1/1024:β) by norm_num))
exact β¨b+1,Nat.zero_lt_succ _,le_of_lt (hb _ (Nat.le_succ b))β©
abbrev good : β := 1/256
lemma good_col {S f g C T} (hc : Step S f C) (hf : 0<f) (hg : β i,in01 (g i))
(ht : kOK T) (x y : β) (h : g y=1) :
residual S C (adjust S f g C T) x y β€ good*f := by
let F:=adjust S f g C T
let w:=combine S C F x y
have he := le_trans (factorial hc hf hg T x y)
(mul_le_mul_of_nonneg_left ht.2 (le_of_lt hf))
change correct _ _ _ _ _-F x y β€ _ at he
change w-F x yβ€_
have hh := mul_le_mul_of_nonneg_left (Qlo (w/f)) (le_of_lt hf)
change f*Q (w/f)*g y - F x y β€ f*_ at he
have hs : f*(w/f)=w := by field_simp
rw [h] at he
unfold good A at *
linarith
/-- Unsigned terms of residual dominated by residual. -/
lemma cover {S f g C T} (hc : Step S f C) (hf : 0<f) (hg : β i,in01 (g i)) (x y : β) :
let F:=adjust S f g C T
C x y+ F x y+mm S F C x y β€ (1+2*T/A)*residual S C F x y := by
let F:=adjust S f g C T
let D:=residual S C F
change _ β€ (1+2*T/A)*D x y
have hu := (csr hf hg hc T).nn x y
obtain β¨ht,hsβ© := substochastic hf hg hc T
have h := ht.nn x y
have HH : D x y=C x y+mm S F C x y-F x y:=rfl
have hv := (csr hf hg hc T).key x y
change 0β€F x y at hu
rcases eq_or_lt_of_le hu with e|e
Β· change 0β€ D x y at h; unfold A; nlinarith
have he := hs x y e
change A*fβ€D x y at he
change F x yβ€ _ at hv
unfold A at *; nlinarith
end Hie
end
end ExactDerandomization
end
section
namespace ExactDerandomization
noncomputable section
namespace Hie
open Analysis Finset
abbrev delta : β := A/400
abbrev eta (z : β) := z/1000
def gate (z : β) (v : β) := 1-rising (eta z) (2*eta z) v
def light (v : β) := rising delta (2*delta) v
lemma gate_mem (z v) : in01 (gate z v) := by
obtain β¨h,eβ© := rising_mem (eta z) (2*eta z) v
unfold in01 gate; constructor <;> linarith
lemma gate_full {z : β} (h:0<z) (v) (e:vβ€eta z) : gate z v=1 := by
unfold gate; rw [rising_bot _ e]; simp
unfold eta; linarith
lemma gate_shut {z : β} (h:0<z) (v) (e:2*eta zβ€v) : gate z v=0 := by
unfold gate; rw [rising_top _ e]; simp
unfold eta; linarith
variable {n : β} [Fact n.Prime]
def retain (l) (S : Domain) (s : Channel n) := S.filter fun i : β=>hit l i s
def tally (l : β) (S : Domain) (s : Channel n) : β := (retain l S s).card
lemma tally_eq (l) (S : Domain) (s : Channel n) : tally l S s=β iβS,ind l i s := by
unfold tally retain ind
rw [card_filter]; push_cast; rfl
abbrev legal (n) (S : Domain) := Sβrange n
lemma injNat (S : Domain) (h : legal n S) : Set.InjOn (fun i:β=>(i:ZMod n)) βS := by
intro i hi j hj e; have he:=congrArg ZMod.val e
simpa [ZMod.val_natCast,Nat.mod_eq_of_lt (mem_range.1 (h hi)),
Nat.mod_eq_of_lt (mem_range.1 (h hj))] using he
lemma tally_av (l) (S : Domain) :
av (fun i : Channel n=> tally l S i)=S.card*pct n l := by
simp only [tally_eq,av_sum,marginal,sum_const,nsmul_eq_mul]
lemma tally_var (l) (S:Domain) (h:legal n S) :
var2 (fun i:Channel n=>tally l S i)β€S.card*pct n l := by
simp only [tally_eq]; exact (count_var _ _ (injNat S h) _).2
lemma tally_cond {S: Domain} (h:legal n S) {j k : β} (e : 2^kβ€n)
(x : β) (hx:x<n) :
atP j (x:ZMod n) (tally k S) β€ 1+S.card*(32*rate k) := by
have he (i) (hi : iβS) :
atP j (x:ZMod n) (ind k i)β€ 32*rate k+(if i=x then 1 else 0) := by
rcases eq_or_ne i x with h'|h'
Β· rw [h',ite_eq_left rfl]
unfold atP
have H : 1 β€ 32*rate k+1 := by have:=rate_pos k; linarith
refine le_trans (b:=av (fun _ : pc n j=>1)) (av_mono (fun i=>?_)) ?_
Β· dsimp only [ind]; split <;> linarith
rw [av_const]; exact H
Β· rw [ite_eq_right h',add_zero]
have hk : (i:ZMod n)β x := fun he=> h' (injNat (range n) (subset_refl _) (h hi) (mem_range.2 hx) he)
have hu := (pairMean (k:=j) (j:=k) hk).1
linarith [(pct_bounds e).2]
have hu := sum_le_sum (s:=S) he
unfold atP
simp only [tally_eq,av_sum]
simp only [atP] at hu
refine le_trans hu ?_
by_cases hx:xβS <;> simp [sum_add_distrib,hx] ; linarith
def panel_rank (S : Domain) (G : Mat) (l K x) (s : Channel n) :=
rank (retain l S s) (fun y=>G x y / rate l) K
def throttle (S : Domain) (G : Mat) (l K x) (s : Channel n) (z : β) :=
gate z (panel_rank S G l K x s)
def cost (S : Domain) (G : Mat) (l K x : β) (z : β) (s : Channel n) : β :=
if throttle S G l K x s z=1 then 0 else 1
lemma gate_bound {z : β} (hp:0<z) {l : β} (hl : 2^lβ€n)
{K j : β} {G S r} (hg : β x y, 0β€G x y) (hr : β x, wt S G xβ€r)
(h : legal n S) {x y : β} (hy:y<n)
(he : (1+32*r/eta z)*128 β€ (K+1:β)) :
atP j (y:ZMod n) (cost S G l K x z) β€ 1/128 := by
let d := rate l
have hd : 0<d := rate_pos _
have hz : 0 < eta z := div_pos hp (by norm_num)
let W := S.filter (fun i=> eta z*d<G x i)
have hh : (W.card:β)*(eta z*d)β€r := by
refine le_trans (b:=β iβW,G x i) ?_ (le_trans (b:=wt S G x) ?_ (hr x))
Β· rw [β nsmul_eq_mul,β sum_const]
exact sum_le_sum fun i hi=>le_of_lt (mem_filter.1 hi).2
Β· exact sum_le_sum_of_subset_of_nonneg (filter_subset ..) fun i _ _=>hg _ _
have h1 (s : Channel n) : (βK+1:β)*cost S G l K x z sβ€ tally l W s := by
unfold cost
split
Β· unfold tally; simp
Β· next e=>
have he : eta z < panel_rank S G l K x s := lt_of_not_ge (fun h=>e (gate_full hp _ h))
have ht := (rank_gt (le_of_lt hz)).1 he
have hu : (((retain l S s).filter fun i=> eta z < G x i/rate l))=retain l W s := by
ext i; simp only [retain,W,mem_filter,lt_div_iffβ (rate_pos l),d]; tauto
rw [hu] at ht; rw [mul_one]; unfold tally
exact_mod_cast (show K+1β€ _ from ht)
have h2 : (K+1:β)*atP j (y:ZMod n) (cost S G l K x z)β€atP j (y:ZMod n) (tally l W) := by
unfold atP; rw [β av_cmul]
exact av_mono fun i=>h1 _
have hs : atP j (y:ZMod n) (tally l W) β€ 1+32*r/eta z := by
refine le_trans (tally_cond (fun i hi=> h (mem_filter.1 hi).1) hl _ hy) ?_
apply add_le_add_right
rw [le_div_iffβ hz]; dsimp only [d] at hh; linarith
have hw : (0:β)<K+1 := by positivity
apply le_of_mul_le_mul_left _ hw
linarith
end Hie
end
end ExactDerandomization
end
section
namespace ExactDerandomization
noncomputable section
namespace Hie
open Analysis Finset
variable {n : β} [Fact n.Prime]
def mark (S G l K x y) (s : Channel n) :=
light (G x y/rate l)*throttle S G l K x s delta
def score (S G l K J y) (s t : Channel n) :=
rank (retain l S s) (fun i=> mark S G l K i y t) J
def U (n : β) [Fact n.Prime] (S G) (l K J y : β) :=
atP l (βy:ZMod n) (fun t=>av (fun s=> score S G l K J y s t))
lemma mark_mem (S G l K x y) (s : Channel n) : in01 (mark S G l K x y s) := by
unfold mark light throttle
obtain β¨h,eβ© := rising_mem delta (2*delta) (G x y/rate l)
obtain β¨w,vβ© := gate_mem delta (panel_rank S G l K x s)
exact β¨mul_nonneg h w,(mul_le_mul e v w (zero_le_one)).trans_eq (by ring)β©
lemma score_mem (S G l K J y) (s t : Channel n) :
in01 (score S G l K J y s t) :=
β¨rank_nn _ _ _,rank_le (by norm_num) fun i _=>(mark_mem ..).2β©
lemma expect_mem {Ξ± : Type} [Fintype Ξ±] [Nonempty Ξ±] {f : Ξ± β β}
(h : β x, in01 (f x)) : in01 (av f) :=
β¨av_nonneg _ (fun x=>(h x).1),le_trans
(av_mono (f:=f) (g:=fun _=>1) (fun i=>(h i).2)) (le_of_eq (av_const _))β©
lemma U_mem (S G l K J y) : in01 (U n S G l K J y) :=
expect_mem fun _=>expect_mem fun _=>score_mem ..
lemma rfull (t) (h:2*delta β€ t) : light t=1 := rising_top (by norm_num [delta,A]) h
lemma rzero (t) (h:tβ€delta) : light t=0 := rising_bot (by norm_num [delta,A]) h
def rowhi (S : Domain) (G : Mat) l y :=
S.filter fun i=> 2*delta*rate l β€ G i y
def rowlo (S : Domain) (G : Mat) l y :=
S.filter fun i=> delta*rate l < G i y
lemma U_light (l K J y : β) (S G) :
(J+1:β)*U n S G l K J y β€ (rowlo S G l y).card*pct n l := by
have he (s t : Channel n) :
(J+1:β)*score S G l K J y s t β€ tally l (rowlo S G l y) s := by
let f := score S G l K J y s t
change _*f β€ _
obtain β¨h1,h2β© := score_mem S G l K J y s t
change _ β€ f at h1; change f β€ _ at h2
rcases h1.eq_or_lt with e|e
Β· rw [β e]; unfold tally; simp
have hi := rank_gt (s:=retain l S s) (f:=fun i=>mark S G l K i y t)
(le_refl (0:β))|>.1 e
have ht : J<(retain l (rowlo S G l y) s).card := by
apply lt_of_lt_of_le hi (card_le_card ?_)
intro i he
simp only [retain,rowlo,mem_filter] at *
refine β¨β¨he.1.1,?_β©,he.1.2β©
by_contra! h
have hk : light (G i y/rate l)=0 := rzero _ ((div_le_iffβ (rate_pos l)).mpr h)
have hh := he.2
simp [mark,hk] at hh
have hh : (J+1:β) β€ tally l (rowlo S G l y) s := by
unfold tally; exact_mod_cast ht
nlinarith
dsimp only [U,atP]
rw [β av_cmul]
have hk : β (t:Channel n),
(J+1:β)*av (fun s=>score S G l K J y s t) β€ (rowlo S G l y).card*pct n l := by
intro t; rw [β av_cmul,β tally_av]
exact av_mono fun i=>he i t
exact le_trans (av_mono (f:=_) (g:=fun (_:pc n l)=> (rowlo S G l y).card*pct n l)
(fun i=>hk _)) (le_of_eq (av_const _))
/-- A threshold usable for incoming normalization. -/
abbrev limitJ (J : β) := 128 + 4*(J+1:β)
lemma U_heavy {S S' G k l} (he:legal n S) (hr : SB S S' (rate l) k G) (hl : 2^l β€ n)
(K J y : β) (hy:yβS)
(hK : (1+32*k / eta delta)*128β€(K+1:β))
(hJ : limitJ J β€ (rowhi S G l y).card*rate l) :
(9/10 : β) β€ U n S G l K J y := by
let W:=rowhi S G l y
let z i (t:pc n l) := cost S G l K i delta (openP l (y:ZMod n) t)
let T (s:Channel n) := tally l W s
let m:= (W.card:β)*pct n l
have hu : limitJ J β€ m := le_trans hJ
(mul_le_mul_of_nonneg_left (pct_bounds hl).1 (Nat.cast_nonneg _))
have hm : 128 β€ m := by unfold limitJ at hu; linarith
have hm0 : 0 < m := by linarith
let loss (s:Channel n) (t : pc n l) := β iβretain l W s,z i t
let v s t := score S G l K J y s (openP l (y:ZMod n) t)
have hh (s : Channel n) t :
m^2*(1-v s t) β€ 4*(T s-m)^2 + 4*m*loss s t := by
let B:=retain l W s
let S0 := B.filter fun i=> throttle S G l K i (openP l (y:ZMod n) t) delta=1
have e : loss s t=T s - S0.card := by
have he (i) : z i t=1-(if throttle S G l K i (openP l (y:ZMod n) t) delta=1 then 1 else 0) := by
dsimp [z,cost]; split <;> norm_num
simp only [loss,T,tally, S0,B, he,card_filter,sum_sub_distrib,sum_const,nsmul_eq_mul, mul_one]
push_cast; rfl
have h1 : 0 β€ v s t := (score_mem ..).1
have h2 : 0 β€ loss s t := sum_nonneg fun i _=>by unfold z cost; split <;> norm_num
by_cases ht : T s < m/2
Β· nlinarith [sq_nonneg m,mul_nonneg (sq_nonneg m) h1]
by_cases hv : m/4 < loss s t
Β· nlinarith [sq_nonneg (T s-m),mul_nonneg (sq_nonneg m) h1]
have ha : 1 β€ v s t := by
have hg : J<S0.card := by
have hk : (J:β)< S0.card := by unfold limitJ at hu; linarith
exact_mod_cast hk
apply (rank_ge (s:=retain l S s) (f:=fun i=>mark S G l K i y (openP l (y:ZMod n) t))
(show (0:β)<1 by norm_num)).2 (lt_of_lt_of_le hg (card_le_card ?_))
intro i hi
simp only [retain,S0,B,W,rowhi,mark,mem_filter] at *
obtain β¨β¨β¨h,kβ©,haβ©,heβ© := hi
apply Finset.mem_filter.mpr
refine β¨Finset.mem_filter.mpr β¨h, haβ©, ?_β©
rw [he, rfull _ ((le_div_iffβ (rate_pos l)).2 k),mul_one]
nlinarith [mul_le_mul_of_nonneg_left ha (sq_nonneg m),sq_nonneg (T s-m)]
have hc : U n S G l K J y=av (fun t=> av (fun s=>v s t)) := rfl
have ht : av T=m := tally_av ..
have hv2 : av (fun s=>(T s -m)^2) β€ m := by
rw [β ht]
exact le_trans (tally_var l W (fun i e=>he (mem_filter.1 e).1)) (le_of_eq ht.symm)
have hz : av (fun t=>av fun s=> loss s t) β€ m/128 := by
rw [av_swap]
have e1 (s) : av (loss s) β€ T s /128 := by
have hh (i) : av (z i) β€ 1/128 := gate_bound (by norm_num [delta,A])
hl hr.nn hr.row he (mem_range.1 (he hy)) hK
unfold loss
rw [av_sum]
refine le_trans (sum_le_sum (fun i _=>hh i)) (le_of_eq ?_)
simp [T,tally]; ring
rw [β ht,β av_div]
exact av_mono (fun s=>e1 s)
have H : av (fun t=>av fun s=>m^2*(1-v s t)) β€
av (fun t=>av fun s=>4*(T s-m)^2+4*m*loss s t) :=
av_mono fun t=>av_mono fun s=>hh s t
simp only [av_cmul,av_add,av_sub,av_const] at H
rw [hc]
by_contra! ht
set V := av (fun t=> av (fun s=>v s t))
have hg : m*m*(1-V) β€4*m+4*m*(m/128) := by
nlinarith [mul_le_mul_of_nonneg_left hz (show 0β€4*m by positivity)]
nlinarith [mul_pos (show 0<(9/10-V) by linarith)
(show 0 < m^2 by positivity)]
end Hie
end
end ExactDerandomization
end
section
namespace ExactDerandomization
noncomputable section
namespace Hie
open Analysis Finset
structure Const where
T : β
K : β
J : β
M : β
tOK : kOK T
KOK : (1+32*(T+1)/eta delta)*128 β€ (K+1:β)
mOK : 16*(T+1)<M
jOK : 64*M*(T+1:β)^2 β€ delta*(J+1)
lemma exists_const : Nonempty Const := by
obtain β¨T,hTβ© := can_cut
let M := 16*(T+1)+1
obtain β¨K,hKβ© := exists_nat_ge ((1+32*(T+1)/eta delta)*128)
obtain β¨J,hJβ© := exists_nat_ge (64*M*(T+1:β)^2/delta)
refine β¨β¨T,K,J,M,hT,by linarith,Nat.lt_succ_self _,?_β©β©
unfold delta A at *; linarith
structure Pilot where
V : Vec
E : Mat
variable (n : β) [Fact n.Prime]
def gamma (x : β) := 1-rising (1/2) (4/5) x
lemma gamma_mem (t) : in01 (gamma t) := by
obtain β¨e,hβ© := rising_mem (1/2) (4/5) t
unfold in01 gamma; constructor <;> linarith
lemma gamma_anti : Antitone gamma := by
intro x y h; have e := (rising_ic (a:=1/2) (b:=4/5) (by norm_num)) x y h
unfold gamma; linarith [e.1]
lemma gamma_full {x} (h:xβ€1/2) : gamma x=1 := by rw [gamma,rising_bot (by norm_num) h]; simp
lemma gamma_stop {x} (h:4/5β€x) : gamma x=0 := by rw [gamma,rising_top (by norm_num) h]; simp
structure Stg where
p : Const
i : β
S : Domain
C : Mat
namespace Stg
variable {n} (P : Stg)
abbrev f := rate P.i
variable (n)
def update (w : Pilot) : Pilot :=
let G:=combine P.S P.C w.E
let v x:= max (w.V x) (U n P.S G P.i P.p.K P.p.J x)
β¨v,correct P.f G (fun x=> gamma (v x))β©
def pilots : β β Pilot
| 0 => β¨fun _=>0,zeroβ©
| k+1 => P.update n (pilots k)
def testG (k : β) := combine P.S P.C (P.pilots n k).E
def testU (k) (x) := U n P.S (P.testG n k) P.i P.p.K P.p.J x
def probed (k : β) := (P.pilots n k).V
def gm (k : β) := fun x => gamma (P.probed n k x)
def corr (k) := adjust P.S P.f (P.gm n P.p.T) P.C k
def nextG (k) := combine P.S P.C (P.corr n k)
def D := residual P.S P.C (P.corr n P.p.T)
theorem V_recur (k x) :
P.probed n (k+1) x = max (P.probed n k x) (P.testU n k x) := rfl
lemma V_mem (k x) : in01 (P.probed n k x) := by
induction k with
| zero=> change in01 0; unfold in01; norm_num
| succ k ih=>
obtain β¨e,hβ© := ih
have hk : in01 (P.testU n k x):=U_mem ..
rw [V_recur]; unfold in01 at *; grind
lemma V_mono : Monotone (P.probed n) := by
apply monotone_nat_of_le_succ; intro k x
rw [V_recur]; exact le_max_left _ _
lemma prob_inc (k x) : P.probed n k x β€ β jβrange k, P.testU n j x := by
induction k with
| zero => simp [probed,pilots]
| succ k ih =>
rw [V_recur,sum_range_succ]
have ht : 0 β€ P.testU n k x := (U_mem ..).1
have hs := (P.V_mem n k x).1
grind
lemma pilot_bounds {S'} (h : SB P.S S' P.f 1 P.C) (k : β) :
SB P.S S' P.f k (P.pilots n k).E := by
induction k with
| zero=> simpa [pilots] using sb0 P.S S' P.f
| succ k ih=>
simpa only [probed,pilots,f,update,Nat.cast_add,Nat.cast_one] using
Fsb (g:=fun x=> gamma (P.probed n (k+1) x)) (rate_pos _) (fun x=>gamma_mem _)
(data_bounds h ih (rate_pos _))
lemma tbounds {S'} (h : SB P.S S' P.f 1 P.C) (k : β) :
SB P.S S' P.f (k+1:β) (P.testG n k) :=
data_bounds h (P.pilot_bounds n h k) (rate_pos _)
lemma cbounds {S'} (h : SB P.S S' P.f 1 P.C) (k : β) :
SB P.S S' P.f k (P.corr n k) :=
csr (rate_pos _) (fun _=>gamma_mem _) h k
lemma cpilot {S'} (h : SB P.S S' P.f 1 P.C) {k : β} (hk:kβ€P.p.T) :
P.corr n kβ€ (P.pilots n k).E := by
induction k with
| zero => exact le_rfl
| succ k ih =>
refine Fgrow h (rate_pos _) (g:=P.gm n P.p.T) (g':=P.gm n (k+1)) (fun _=>(gamma_mem _).1)
?_ (ih (by omega))
exact fun x=>gamma_anti (P.V_mono n hk x)
lemma dbounds {S'} (h : SB P.S S' P.f 1 P.C) :
SB P.S S' ((P.p.T+1:β)*P.f) 1 (P.D n) :=
(substochastic (rate_pos _) (fun _=> gamma_mem _) h P.p.T).1
lemma columns (he : legal n P.S) (h : Step P.S P.f P.C) (hl:2^P.i β€ n)
{k x : β} (hk:k<P.p.T) (hx:xβP.S) (hg:0<P.gm n (k+1) x) :
(rowhi P.S (P.testG n k) P.i x).card*P.f β€ limitJ P.p.J := by
by_contra! e
have hn : ((k:β)+1)<=P.p.T := by exact_mod_cast (show k+1β€ P.p.T from hk)
have hK := P.p.KOK
have hs := U_heavy he (P.tbounds n h k) hl P.p.K P.p.J x hx (by unfold eta delta A at *; linarith) (le_of_lt e)
have ht := le_max_right (P.probed n k x) (P.testU n k x)
have hh : P.gm n (k+1) x=0 := by
unfold gm; rw [V_recur,gamma_stop]
unfold testU at ht β’; linarith
linarith
lemma cols (he : legal n P.S) (h : Step P.S P.f P.C) (hl:2^P.i β€ n)
{k x : β} (hk:k<P.p.T) (hx:xβP.S) (hg:0<P.gm n P.p.T x) :
(rowhi P.S (P.nextG n k) P.i x).card*P.f β€ limitJ P.p.J := by
have hp := P.columns n he h hl hk hx
(lt_of_lt_of_le hg (gamma_anti (P.V_mono n hk x)))
have hu i j : P.nextG n k i jβ€ P.testG n k i j := add_le_add_right
(sum_le_sum fun z _=>mul_le_mul_of_nonneg_right (P.cpilot n h hk.le i z) (h.nn z j)) _
refine le_trans (mul_le_mul_of_nonneg_right (Nat.cast_le.mpr (card_le_card ?_))
(le_of_lt (rate_pos _))) hp
intro j hj; obtain β¨e,hjβ© := mem_filter.1 hj; exact mem_filter.2 β¨e,hj.trans (hu ..)β©
private lemma mark_sum (P : Stg) (n : β) [Fact n.Prime]
(h : Step P.S P.f P.C) (k : β) :
β xβP.S, ((rowlo P.S (P.testG n k) P.i x).card:β) * (delta * P.f) β€
(nodes P.S P.C).card*((k:β)+1) := by
let A0:=nodes P.S P.C
let G:=P.testG n k
have he := P.tbounds n (restrict h (subset_refl A0)) k
have hf:=rate_pos P.i
have hu (x) : rowlo P.S G P.i x β A0 := by
intro i hi; obtain β¨e,hiβ©:=mem_filter.1 hi
have ht : 0< G i x:=lt_trans (by unfold delta A; positivity) hi
exact (he.loc i x ht).1
calc _ β€ β xβP.S,β yβA0,G y x := by
apply sum_le_sum; intro x hx
refine le_trans (b:=β yβrowlo P.S G P.i x,G y x) ?_ (sum_le_sum_of_subset_of_nonneg
(hu x) fun y _ _=>he.nn y x)
change β(rowlo P.S G P.i x).card*(delta*P.f) β€ _
rw [β nsmul_eq_mul,βsum_const]
exact sum_le_sum fun i h=>le_of_lt (mem_filter.1 h).2
_ β€ _ := by
rw [sum_comm,β nsmul_eq_mul,βsum_const]
exact sum_le_sum fun i _=> he.row i
def active := P.S.filter fun i=>1/4<P.probed n P.p.T i
lemma rare (h : Step P.S P.f P.C) (hl : 2^P.iβ€n) :
(P.active n).card*P.p.M β€ (nodes P.S P.C).card := by
let U0:=nodes P.S P.C
let T:=P.p.T
let J:β := βP.p.J+1
let d:β := delta*J/64
have hd : 0 < d := by dsimp [d,J,delta,A]; positivity
have H (x) (hx:xβP.active n) : d β€
β k β range T, ((rowlo P.S (P.testG n k) P.i x).card:β)*(delta*P.f) := by
have hz := (mem_filter.mp hx).2
have hu := P.prob_inc n T x
have he (k : β) : P.testU n k x*(4*d) β€
((rowlo P.S (P.testG n k) P.i x).card:β)*(delta*P.f) := by
have e := U_light (n:=n) P.i P.p.K P.p.J x P.S (P.testG n k)
have hk := mul_le_mul_of_nonneg_left (pct_bounds hl).2
(show 0 β€ ((rowlo P.S (P.testG n k) P.i x).card:β) by positivity)
change J*P.testU n k x β€ _ at e
dsimp only [d,delta,A,f]; linarith
refine le_trans ?_ (sum_le_sum (fun k _=>he k))
rw [β sum_mul]
change 1/4<P.probed n T x at hz
nlinarith
have hv :
((P.active n).card:β)*d β€ (U0.card:β)*(T+1:β)^2 := by
calc
_ = β xβP.active n,d := by simp
_ β€ β xβP.active n,β kβrange T,((rowlo P.S (P.testG n k) P.i x).card:β)*(delta*P.f) :=
sum_le_sum H
_ β€ β xβP.S,β kβrange T,((rowlo P.S (P.testG n k) P.i x).card:β)*(delta*P.f) := by
apply sum_le_sum_of_subset_of_nonneg (filter_subset ..)
intro i _ _
apply sum_nonneg; intro k _
have he:0<P.f:=rate_pos _
unfold delta A; positivity
_ β€ _ := by
rw [sum_comm]
refine le_trans (sum_le_sum (fun k (_:kβrange T)=>mark_sum P n h k)) ?_
have hs := sum_le_sum (f:=fun k:β=> (U0.card:β)*((k:β)+1))
(g:=fun _=>(U0.card:β)*(T+1:β))
(fun k (hk:kβrange T)=> by
have hv : (k:β)β€T := Nat.cast_le.mpr (le_of_lt (mem_range.mp hk))
gcongr)
simp only [sum_const,nsmul_eq_mul,card_range] at hs
refine hs.trans ?_
nlinarith [show (0:β)β€U0.card by positivity]
suffices ((P.active n).card:β)*P.p.M β€ (nodes P.S P.C).card by exact_mod_cast this
have he := P.p.jOK
have h' : P.p.M*((T:β)+1)^2 β€ d := by dsimp only [d,J,T]; unfold delta A at he β’; nlinarith
have hz := mul_le_mul_of_nonneg_left h'
(show (0:β)β€ (P.active n).card by positivity)
apply le_of_mul_le_mul_right _ (show 0<((T:β)+1)^2 by positivity)
linarith
end Stg
end Hie
end
end ExactDerandomization
end
section
namespace ExactDerandomization.Hie
noncomputable section
open Analysis Finset
abbrev radix (M x z : β) := z+M*x
def copyZone (M) (S : Domain) := (S ΓΛ’ range M).image fun i=>radix M i.1 i.2
lemma fromrad (M x i) (h:i < M) :
radix M x i / M=x β§ radix M x i % M=i := by
simp [radix,Nat.div_eq_of_lt h,Nat.mod_eq_of_lt h,Nat.add_mul_div_left i x (show 0<M by omega)]
lemma inside {M x j : β} {S : Domain} (h:xβS) (e:j<M) : radix M x jβcopyZone M S :=
mem_image.2 β¨(x,j),mem_product.2 β¨h,mem_range.2 eβ©,rflβ©
lemma repr_copy {M x} {S:Domain} (h:xβcopyZone M S) :
β yβS,β j<M, x=radix M y j := by
obtain β¨β¨y,jβ©,e,rflβ©:=mem_image.1 h
exact β¨y,(mem_product.1 e).1,j,mem_range.1 (mem_product.1 e).2,rflβ©
lemma member_copy {M x} {S:Domain} (h:0<M) :
xβcopyZone M S β x/MβS := by
constructor
Β· intro h; obtain β¨y,e,j,k,rflβ©:=repr_copy h; rwa [(fromrad M y j k).1]
intro he
simpa [radix,Nat.mod_add_div] using inside he (Nat.mod_lt x h)
lemma eval_copies {M S} (f : Vec) :
β xβcopyZone M S,f x = β xβS,β iβrange M,f (radix M x i) := by
rw [copyZone,sum_image]; exact sum_product ..
intro a ha b hb e
have h1:=fromrad M a.1 a.2 (mem_range.1 (mem_product.1 ha).2)
have h2:=fromrad M b.1 b.2 (mem_range.1 (mem_product.1 hb).2)
change radix M a.1 a.2 = radix M b.1 b.2 at e; rw [e] at h1; grind
abbrev bound0 : β := 1/128
abbrev bound1 : β := 1/64
def slice (t : β) := rising bound0 bound1 t
def share (M i : β) (t : β) :=
(if i=0 then 1-slice t else 0)+slice t / M
lemma share_nn (M i) (t) : 0 β€ share M i t := by
have he : in01 (slice t) := rising_mem _ _ _
unfold share in01 at *
obtain β¨he,hβ©:=he
split
Β· exact add_nonneg (by linarith) (by positivity)
Β· positivity
lemma share_sum (M) (h:0<M) (t) : β jβrange M,share M j t = 1 := by
simp only [share,sum_add_distrib,sum_const,nsmul_eq_mul,card_range]
simp [h]
field_simp
linarith
lemma share_le {M i} (he:i<M) (t) : share M i tβ€1 := by
rw [β share_sum M (by omega) t]
exact single_le_sum (fun i _=>share_nn M i t) (mem_range.2 he)
abbrev boost (x : β) := rising (1/4) (1/2) x
namespace Stg
variable (P : Stg) (n : β) [Fact n.Prime]
def fac (x j : β) := if j=0 then 1 else boost (P.probed n P.p.T x)
def newMat (x y : β) :=
let M:=P.p.M
let w:=P.D n (x/M) (y/M)
P.fac n (x/M) (x%M) *w*share M (y%M) (w/P.f)
def next := Stg.mk P.p (P.i+1) (copyZone P.p.M P.S) (P.newMat n)
def project (f : Vec) (x) :=
let M:=P.p.M
P.fac n (x/M) (x%M)*f (x/M)
lemma mpos : 0 < P.p.M := by have := P.p.mOK; omega
lemma fac_mem (x i) : in01 (P.fac n x i) := by
unfold fac; split; norm_num [in01]
exact rising_mem _ _ _
lemma edge_w (x y) {i} (h:i<P.p.M) :
P.newMat n x (radix P.p.M y i) =
P.fac n (x/P.p.M) (x%P.p.M)*P.D n (x/P.p.M) y *
share P.p.M i (P.D n (x/P.p.M) y/P.f) := by
dsimp only [newMat]; rw [(fromrad _ y _ h).1,(fromrad _ y _ h).2]
lemma edge_small (h : Step P.S P.f P.C) (x y) :
P.newMat n x yβ€rate (P.i+1) := by
let f := P.fac n (x/P.p.M) (x%P.p.M)
let d := P.D n (x/P.p.M) (y/P.p.M)
let s := share P.p.M (y%P.p.M) (d/P.f)
have hb : rate (P.i+1)=P.f/16 := by unfold Stg.f rate; rw [pow_succ]; ring
rw [hb]; change f*d*sβ€_
have hD:=P.dbounds n h
have hp : 0β€d:= hD.nn ..
have hm : 0 β€ s:=share_nn ..
have hf : fβ€1:=(P.fac_mem ..).2
have he := mul_le_mul_of_nonneg_right (show f*dβ€d by nlinarith) hm
refine le_trans he ?_
have hz : 0<P.f:=rate_pos _
rcases le_total bound1 (d/P.f) with e|e
Β· have hw : s=(P.p.M:β)β»ΒΉ := by
dsimp only [s,share,slice]
rw [rising_top (by norm_num) e]; split <;> simp
have hd : d β€ (P.p.T+1:β)*P.f := by
simpa only [Nat.cast_add,Nat.cast_one,one_mul] using hD.key (x/P.p.M) (y/P.p.M)
rw [hw,β div_eq_mul_inv]
have h' : 16*(P.p.T+1:β) < P.p.M := by exact_mod_cast P.p.mOK
apply (div_le_iffβ (show 0<(P.p.M:β) by linarith)).2
nlinarith
Β· have hs : sβ€1 := share_le (Nat.mod_lt _ P.mpos) _
have he:= (div_le_iffβ hz).1 e
unfold bound1 at he; nlinarith
lemma newGood (h : Step P.S P.f P.C) :
Step (P.next n).S (P.next n).f (P.next n).C := by
have he := P.dbounds n (S':=P.S) h
constructor
Β· intro x y
exact mul_nonneg (mul_nonneg (P.fac_mem ..).1 (he.nn ..)) (share_nn ..)
Β· intro x y; rw [one_mul]; exact P.edge_small n h x y
Β· intro x
have H (y : β) : β jβrange P.p.M, P.newMat n x (radix P.p.M y j) =
P.fac n (x/P.p.M) (x%P.p.M)*P.D n (x/P.p.M) y := by
let a := P.fac n (x/P.p.M) (x%P.p.M)
let t := P.D n (x/P.p.M) y
refine Eq.trans (b:=β jβrange P.p.M,a*t*share P.p.M j (t/P.f)) (sum_congr rfl ?_) ?_
Β· intro i hi; exact P.edge_w n _ _ (mem_range.1 hi)
Β· rw [β mul_sum,share_sum _ P.mpos, mul_one]
change β yβcopyZone P.p.M P.S,P.newMat n x yβ€1
rw [eval_copies]
simp only [H,β mul_sum]
exact (mul_le_mul (P.fac_mem ..).2 (he.row _) (sum_nonneg (fun _ _=>he.nn ..))
(by norm_num)).trans_eq (one_mul _)
Β· intro x y hi
let M:=P.p.M
have ha : 0<P.D n (x/M) (y/M) := by
have ht := he.nn (x/M) (y/M)
by_contra! hc
change 0 < _*P.D n (x/M) (y/M)*_ at hi
rw [le_antisymm hc ht] at hi; simp at hi
obtain β¨ha,hb,hcβ©:=he.loc _ _ ha
refine β¨(member_copy P.mpos).2 ha, (member_copy P.mpos).2 hb,?_β©
by_contra! H
exact (not_lt_of_ge (Nat.div_le_div_right H)) hc
lemma nonprimary (h : Step P.S P.f P.C) (x y j) (hj:jβ 0)
(ht : share P.p.M j (P.D n x y/P.f)β 0) :
1/2 < P.probed n P.p.T y β§ P.fac n y j=1 := by
have hp : 1/2 < P.probed n P.p.T y := by
by_contra! e
have hu := good_col h (rate_pos _) (fun x => (show in01 (P.gm n P.p.T x) from gamma_mem _)) P.p.tOK x y (gamma_full e)
change P.D n x yβ€_ at hu
apply ht
dsimp [share,slice]
rw [rising_bot (by norm_num)
((div_le_iffβ (rate_pos P.i)).mpr (le_trans hu (by
change good*rate P.i β€ bound0*rate P.i; exact mul_le_mul_of_nonneg_right (by norm_num [good,bound0]) (le_of_lt (rate_pos _))))),ite_eq_right hj]
simp
refine β¨hp,?_β©; unfold fac boost; rw [ite_eq_right hj,rising_top (by norm_num) hp.le]
lemma commute (h : Step P.S P.f P.C) (v : Vec) (x) :
mv (P.next n).S (P.next n).C (P.project n v) x =
P.project n (mv P.S (P.D n) v) x := by
let M:=P.p.M
let a:=P.fac n (x/M) (x%M)
have e (y) : β jβrange M, P.newMat n x (radix M y j)*P.project n v (radix M y j)
=a*(P.D n (x/M) y * v y) := by
let D:=P.D n (x/M) y
let f (j) := share M j (D/P.f)
refine Eq.trans (b:=β jβrange M,(a*(D*v y))*f j) (sum_congr rfl ?_) ?_
Β· intro i hi
have ht : i<P.p.M:=mem_range.1 hi
rw [P.edge_w n _ _ ht]
unfold project; dsimp only
rw [(fromrad _ _ _ ht).1,(fromrad _ _ _ ht).2]
change a*D*f i* (P.fac n y i * v y)=_
by_cases he:f i=0
Β· simp [he]
have hp : P.fac n y i=1 := by
by_cases hs:i=0
Β· simp [fac,hs]
exact (P.nonprimary n h (x/M) y i hs he).2
rw [hp]; ring
Β· rw [β mul_sum, show β j β range M,f j=1 from share_sum M P.mpos _,mul_one]
change β yβcopyZone M P.S,_=a*_
rw [eval_copies]; change β yβP.S,_=a*β yβP.S,_
rw [mul_sum]; exact sum_congr rfl fun i _=> e i
lemma few_nodes (h : Step P.S P.f P.C) (hl: 2^P.i β€ n) :
(nodes (P.next n).S (P.next n).C).card β€ 2*(nodes P.S P.C).card := by
let M:=P.p.M
let T:=nodes P.S P.C
let E:=P.active n
let A':=T.image (fun i=>radix M i 0)
have he:= P.dbounds n (restrict h (subset_refl T))
have hz (i j) (hi:0<P.newMat n i j) :
0 < P.D n (i/M) (j/M) β§
P.fac n (i/M) (i%M)β 0 β§ share M (j%M) (P.D n (i/M) (j/M)/P.f)β 0 := by
let x:=P.fac n (i/M) (i%M)
let y:=P.D n (i/M) (j/M)
let z:=share M (j%M) (y/P.f)
change 0<x*y*z at hi
change 0<y β§ xβ 0 β§ zβ 0
have hh : 0β€ y:=he.nn (i/M) (j/M)
have : x*y*zβ 0 := ne_of_gt hi
simp only [ne_eq,mul_eq_zero,not_or] at this
exact β¨lt_of_le_of_ne hh (Ne.symm this.1.2), this.1.1,this.2β©
have hh : nodes (P.next n).S (P.next n).C β A' βͺ copyZone M E := by
intro i hi
obtain β¨e,y,hy,h'β©:= mem_filter.mp hi
have hk := (member_copy P.mpos).1 e
have hu : radix M (i/M) (i%M)=i := Nat.mod_add_div i M
rcases h' with hp|hp
Β· obtain β¨h1,h2,-β©:=hz i y hp
by_cases hs:i%M=0
Β· exact mem_union_left _ (mem_image.mpr β¨_,(he.loc _ _ h1).1,(hs βΈ hu)β©)
Β· refine mem_union_right _ ((member_copy P.mpos).mpr (mem_filter.2 β¨hk,?_β©))
by_contra! en
exact h2 (by unfold fac boost; rw [ite_eq_right hs]; exact rising_bot (by norm_num) en)
Β· obtain β¨h1,-,h2β©:=hz y i hp
by_cases hs:i%M=0
Β· exact mem_union_left _ (mem_image.mpr β¨_,(he.loc _ _ h1).2.1,(hs βΈ hu)β©)
Β· have ht := (P.nonprimary n h (y/M) (i/M) (i%M) hs h2).1
exact mem_union_right _ ((member_copy P.mpos).mpr (mem_filter.2 β¨hk,
lt_trans (by norm_num) htβ©))
have H := P.rare n h hl
change E.card*Mβ€T.card at H
calc _ β€ _ := card_le_card hh
_ β€ _ := card_union_le _ _
_ β€ T.card + T.card := add_le_add card_image_le (le_trans card_image_le (by simpa using H))
_ = _ := by dsimp [T]; omega
end Stg
end
end ExactDerandomization.Hie
end
section
namespace ExactDerandomization.Hie
noncomputable section
open Analysis Finset
lemma apply_add (S:Domain) (C:Mat) (f g : Vec) : mv S C (f+g)=mv S C f+mv S C g := by
ext; simp [mv,mul_add,sum_add_distrib]
lemma add_apply (S:Domain) (C D:Mat) (f : Vec) : mv S (C+D) f=mv S C f+mv S D f := by
ext; simp [mv,add_mul,sum_add_distrib]
lemma sub_apply (S:Domain) (C D:Mat) (f : Vec) : mv S (C-D) f=mv S C f-mv S D f := by
ext; simp [mv,sub_mul,sum_sub_distrib]
structure Prize where
P : Stg
W : Vec
/-- conceptual transported true probability -/
v : Vec
namespace Prize
variable (n : β) [Fact n.Prime]
def plus (H : Prize) :=
{P:=H.P.next n
W:=H.P.project n (H.W+mv H.P.S (H.P.corr n H.P.p.T) H.W)
v:=H.P.project n H.v : Prize}
structure Correct (H : Prize) : Prop where
step : Step H.P.S H.P.f H.P.C
Wnn : 0β€H.W
v01 : β x,in01 (H.v x)
eq : H.v=H.W+mv H.P.S H.P.C H.v
lemma Correct.win {H : Prize} (h: H.Correct) (x : β) : in01 (H.W x) := by
have he := congrFun h.eq x
have hu : 0 β€ mv H.P.S H.P.C H.v x := sum_nonneg fun i _=>
mul_nonneg (h.step.nn _ _) (h.v01 i).1
obtain β¨_,hhβ©:=h.v01 x
exact β¨h.Wnn x,by simp only [Pi.add_apply] at he; linarithβ©
lemma Correct.raise {H : Prize} (h: H.Correct) : (plus n H).Correct := by
let P:=H.P
let T:=P.S
let F:=P.corr n P.p.T
have he:=P.cbounds n h.step P.p.T
constructor
Β· exact P.newGood n h.step
Β· intro x
have ht: 0β€ (H.W+mv T F H.W) := fun i=> add_nonneg (h.Wnn i)
(sum_nonneg fun j _=> mul_nonneg (he.nn i j) (h.Wnn j))
exact mul_nonneg (P.fac_mem n _ _).1 (ht _)
Β· intro i
let x:=i/P.p.M
change in01 (_*H.v _)
exact β¨mul_nonneg (P.fac_mem n x _).1 (h.v01 x).1,
le_trans (mul_le_mul (P.fac_mem n x _).2 (h.v01 x).2 (h.v01 x).1 zero_le_one)
(le_of_eq (one_mul _))β©
Β· ext x
let W' := H.W+mv T F H.W
change P.project n H.v x=P.project n W' x + _
have e := P.commute n h.step H.v x
change _=P.project n W' x+ mv (P.next n).S (P.next n).C (P.project n H.v) x
rw [e]; unfold Stg.project; simp only []; rw [β mul_add]
have heq : H.v=W'+mv T (P.D n) H.v := by
change _=_+mv T ((P.C+mm T F P.C)-F) _
rw [sub_apply,add_apply,mm_mv]
have hf : mv T F H.v=mv T F H.W+mv T F (mv T P.C H.v) :=
Eq.trans (congrArg (mv T F) h.eq) (apply_add T F ..)
rw [hf]
calc H.v = _ := h.eq
_ = _ := by dsimp only [W',T,P,F]; abel
have hu := congrFun heq (x/H.P.p.M)
rw [show W' (x/P.p.M)+_ = H.v (x/P.p.M) from hu.symm]
def iterate (H : Prize) : ββPrize
| 0=>H
| k+1=>plus n (iterate H k)
lemma all {H : Prize} (h:H.Correct) (k) : (H.iterate n k).Correct := by
induction k with
| zero=>exact h
| succ k ih=>exact ih.raise n
lemma par (H : Prize) (k) : (H.iterate n k).P.p=H.P.p := by
induction k with
| zero=>rfl
| succ k ih=>exact ih
lemma index (H : Prize) (k) : (H.iterate n k).P.i=H.P.i+k := by
induction k with
| zero=>rfl
| succ k ih=>
change _ + 1=_
rw [ih]
omega
def child (H : Prize) (x : β) : βββ
| 0=>x
| k+1=>radix H.P.p.M (child H x k) 0
lemma unchanged (H : Prize) (k x) :
(H.iterate n k).v (child H x k)=H.v x := by
induction k with
| zero=>rfl
| succ k ih=>
simp only [child,iterate,plus,Stg.project,par]
have he := fromrad _ (child H x k) 0 H.P.mpos
simp only [he.1,he.2,Stg.fac,ite_true,one_mul]; exact ih
lemma reach (H : Prize) (k) {x} (hx:xβH.P.S) :
child H x k β (H.iterate n k).P.S := by
induction k with
| zero=>exact hx
| succ k ih=>
have he := inside ih H.P.mpos
simp only [child,iterate,plus,Stg.next,par]; exact he
lemma density {H : Prize} (h:H.Correct) (k) (hl : 2^(H.P.i+k)β€n) :
(nodes (H.iterate n k).P.S (H.iterate n k).P.C).card β€ H.P.S.card*2^k := by
induction k with
| zero=> simpa only [iterate,nodes,pow_zero,mul_one] using card_le_card
(filter_subset (s:=H.P.S) _)
| succ k ih=>
have hk : 2^(H.P.i+k)β€n:=le_trans (Nat.pow_le_pow_right (by norm_num) (by omega)) hl
let P:=(H.iterate n k).P
calc _ β€ 2*(nodes P.S P.C).card :=
P.few_nodes n ((all n h k).step) (by simp only [P,index]; exact hk)
_ β€ _ := by
rw [pow_succ]
have he : (nodes P.S P.C).cardβ€_:=ih hk
rw [β mul_assoc]; omega
lemma finish {H : Prize} (h:H.Correct) (k) (hl : 2^(H.P.i+k) β€ n) (x) :
0 β€ H.v x-(H.iterate n k).W (child H x k) β§
H.v x-(H.iterate n k).W (child H x k) β€
((H.P.S.card * 2^k : β) : β) * rate (H.P.i+k) := by
let P:=H.iterate n k
let T:=P.P
let y:=child H x k
rw [β unchanged n H k x]
have hs : P.Correct:=all n h k
have he : (P.v y)-P.W y = mv T.S T.C P.v y:= by
have h:=congrFun hs.eq y; dsimp only [Pi.add_apply] at h; rw [h]; ring
change 0β€ P.v y-P.W y β§ P.v y-P.W yβ€_
rw [he]
have hu (i) := mul_nonneg (hs.step.nn y i) (hs.v01 i).1
refine β¨sum_nonneg fun i _=>hu i,?_β©
let S:=nodes T.S T.C
have hp:S β T.S := filter_subset ..
have hh := restrict hs.step (subset_refl S)
unfold mv
rw [β sum_subset hp]
Β· have hf : T.f=rate (H.P.i+k) := by simp only [Stg.f,T,P,index]
calc
_ β€ β _iβS,T.f := sum_le_sum fun i _=> (mul_le_mul (hh.key ..) (hs.v01 i).2
(hs.v01 i).1 (by unfold Stg.f; have := rate_pos T.i; positivity)).trans_eq (by ring)
_ β€ _ := by
simp only [sum_const,nsmul_eq_mul,hf]
exact mul_le_mul_of_nonneg_right (by exact_mod_cast density n h k hl) (le_of_lt (rate_pos _))
Β· intro i hi hk
have hu : Β¬0<T.C y i := fun h=> hk (hh.loc y i h).2.1
rw [le_antisymm (le_of_not_gt hu) (hs.step.nn ..),zero_mul]
end Prize
end
end ExactDerandomization.Hie
end
end OAI
import OAI.Computability.Logspace.AffineSampling
namespace OAI
/-!
Strictly forward substochastic matrices and the hierarchy of probability rewards.
From OpenAI, *Exact Derandomization of Logarithmic Space: L = RL = BPL*.
-/
set_option autoImplicit true
section
/-! The exact hierarchy. Nat IDs, ordered (strictly forward). -/
namespace ExactDerandomization
noncomputable section
open Finset Analysis
namespace Hie
abbrev Domain := Finset β
abbrev Vec := β β β
abbrev Mat := β β Vec
def mv (S : Domain) (C : Mat) (v : Vec) : Vec := fun x=> β yβS,C x y*v y
def mm (S : Domain) (C D : Mat) : Mat := fun x y=> β zβS,C x z * D z y
def wt (S : Domain) (C : Mat) x := β yβS, C x y
def nodes (S : Domain) (C : Mat) :=
S.filter fun x=> β yβS, 0<C x y β¨ 0<C y x
/-- support restricted to active set S' -/
structure SB (S S' : Domain) (f : β) (k : β) (C : Mat) : Prop where
nn : β x y, 0β€ C x y
key : β x y, C x yβ€k*f
row : β x, wt S C x β€ k
loc : β x y, 0<C x y β xβS' β§ yβS' β§ x<y
abbrev Step (S) (f) (C) := SB S S f 1 C
lemma wt_mm (S : Domain) (C D : Mat) (x) :
wt S (mm S C D) x = β yβS, C x y * wt S D y := by
simp_rw [wt,mm,mul_sum]
rw [sum_comm]
lemma mm_mv (S:Domain) (C D : Mat) (f : Vec) :
mv S (mm S C D) f=mv S C (mv S D f) := by
ext x; simp only [mv,mm,sum_mul,mul_sum,mul_assoc]; rw [sum_comm]
lemma restrict {S S' : Domain} {f k C} (h : SB S S f k C)
(e : nodes S C β S') : SB S S' f k C :=
β¨h.nn,h.key,h.row,fun x y hk=>by
obtain β¨hx,hy,hzβ© := h.loc x y hk
exact β¨e (mem_filter.2 β¨hx,y,hy,Or.inl hkβ©),
e (mem_filter.2 β¨hy,x,hx,Or.inr hkβ©),hzβ©β©
lemma relax {S S' S'' : Domain} {f k C} (h : SB S S' f k C) (e : S'β S'') :
SB S S'' f k C := by
refine β¨h.nn,h.key,h.row,fun x y hk=>?_β©
obtain β¨hx,hy,hzβ© := h.loc x y hk
exact β¨e hx,e hy,hzβ©
def zero : Mat := fun _ _=>0
lemma sb0 (S S' f) : SB S S' f 0 zero := by
constructor <;> simp [wt,zero]
def combine (S : Domain) (C F : Mat) := C+mm S F C
lemma data_bounds {S S' f k C F} (h : SB S S' f 1 C) (e : SB S S' f k F)
(hf:0<f) : SB S S' f (k+1) (combine S C F) := by
have h0 (x y) : 0β€ mm S F C x y :=
sum_nonneg (fun z _=> mul_nonneg (e.nn x z) (h.nn z y))
constructor
Β· intro x y; exact add_nonneg (h.nn x y) (h0 x y)
Β· intro x y
have hee : mm S F C x y β€ wt S F x *f := by
unfold wt mm; rw [sum_mul]
exact sum_le_sum fun z _=>mul_le_mul_of_nonneg_left
(by simpa using h.key z y) (e.nn x z)
have ht := e.row x
change C x y+mm S F C x yβ€_
nlinarith [h.key x y]
Β· intro x
have hz : wt S (mm S F C) x β€ wt S F x := by
rw [wt_mm]; unfold wt
apply sum_le_sum
intro y _
exact (mul_le_mul_of_nonneg_left (h.row y) (e.nn x y)).trans_eq (by simp)
simp only [wt,combine,Pi.add_apply, sum_add_distrib]
change wt S C x+wt S (mm S F C) x β€ _
linarith [h.row x,e.row x]
Β· intro x y he
by_cases hx: 0<C x y
Β· exact h.loc x y hx
change 0<C x y+_ at he
have hp : 0 < mm S F C x y := by linarith
obtain β¨z,hz,htβ© : β zβS, 0<F x z*C z y := by
by_contra! hn
exact (not_lt_of_ge (show mm S F C x yβ€0 from sum_nonpos hn)) hp
have hs := (mul_pos_iff.mp ht).resolve_right (by have:=h.nn z y; grind)
obtain β¨h1,h2,h3β© := h.loc z y hs.2