introvoyz045's picture
Migrated from GitHub
4a56cd6 verified
Raw History Blame Contribute Delete
5.82 kB
import Mathlib
import OAI.Combinatorics.MatchingCount.Algorithm.RationalUpdate
import OAI.Combinatorics.MatchingCount.Algorithm.FiniteTrialLaw
namespace OAI
namespace MatchingFPRAS.FiniteTrial
open Finset Switching Transport Enlargement Adaptive Initializer
open scoped Classical
variable {G : GraphInput} [Nontrivial (Fin G.n)]
attribute [local instance] NumberedHierarchy.labelOrder sym2Order bagEdgeOrder bagEdgeFintype
attribute [local instance] UnitRefresh.Computable.allowedDec prodOrder sumOrder
noncomputable local instance (w : Fin G.n β†’ Fin G.n β†’ β„š) (K : β„•) :
DecidableEq (SourceLabel (fun x y=>(w x y:ℝ)) K) := Classical.decEq _
noncomputable local instance (w : Fin G.n β†’ Fin G.n β†’ β„š) (K : β„•) :
Fintype (BagEdge (sourceBags (fun x y=>(w x y:ℝ)) K)) := Fintype.ofFinite _
noncomputable local instance (w : Fin G.n β†’ Fin G.n β†’ β„š) (K : β„•) :
Fintype (SourceMarked (fun x y=>(w x y:ℝ)) K) := Fintype.ofFinite _
structure Layer (p : Trial.Parameters G) (j : β„•) where
Ξ© : Type
finite : Fintype Ξ©
law : @Law Ξ© finite
state : Ξ© β†’ ScaleState G j
ratios : Ξ© β†’ Fin j β†’ β„š
ratios_nonneg : βˆ€ x i,0 ≀ ratios x i
attribute [instance] Layer.finite
noncomputable def Layer.good (p : Trial.Parameters G) {j : β„•} (L : Layer p j) (x : L.Ξ©) : Prop :=
(L.state x).Balanced p.hn ∧ βˆ€ i : Fin j,
|(L.ratios x i:ℝ)/(partition (Trial.raw G (i.val+1)) βˆ…/partition (Trial.raw G i.val) βˆ…)-1| ≀ (p.epsilon:ℝ)/(100*p.K)
noncomputable def firstLayer (p : Trial.Parameters G) : Layer p 0 where
Ξ© := PUnit
finite := inferInstance
law := ⟨fun _=>1,fun _=>by norm_num,by simp⟩
state _ := initialScaleState G p.hn
ratios _ := Fin.elim0
ratios_nonneg _ i := Fin.elim0 i
noncomputable def Layer.append (p : Trial.Parameters G) {j : β„•} (hj : j ≀ p.K) (L : Layer p j) : Layer p (j+1) where
Ξ© := Ξ£ x : L.Ξ©, Fin p.S β†’ SourceMarked (fun a b=>((L.state x).activity a b:ℝ)) p.K
finite := inferInstance
law := L.law.sigma (fun x=>(L.state x).sampleLaw p hj)
state x := (L.state x.1).update p.hn (RationalSource.sourceMarkedEquiv (L.state x.1).activity p.K ∘ x.2)
ratios x := Fin.snoc (L.ratios x.1) ((L.state x.1).ratioEstimate (RationalSource.sourceMarkedEquiv (L.state x.1).activity p.K ∘ x.2))
ratios_nonneg x i := by
refine Fin.lastCases ?_ (fun i=>?_) i
Β· simpa using (L.state x.1).ratioEstimate_nonneg x.2
Β· simpa using L.ratios_nonneg x.1 i
theorem Layer.append_good (p : Trial.Parameters G) {j : β„•} (hj : j ≀ p.K) (L : Layer p j) (x : L.Ξ©)
(hx : L.good p x) (y : Fin p.S β†’ SourceMarked (fun a b=>((L.state x).activity a b:ℝ)) p.K)
(hy : StageGood (fun a b=>((L.state x).activity a b:ℝ)) (fun a b=>((L.state x).nextActivity a b:ℝ)) p.K p.sigma y) :
(L.append p hj).good p ⟨x,y⟩ := by
have h := successful_update p hj (L.state x) hx.1 y hy
refine ⟨h.1,?_⟩
intro i
refine Fin.lastCases ?_ (fun i=>?_) i
Β· simpa [Layer.append] using h.2
Β· simpa [Layer.append] using hx.2 i
theorem Layer.append_failure (p : Trial.Parameters G) {j : β„•} (hj : j ≀ p.K) (L : Layer p j) :
(L.append p hj).law.event (fun x=>Β¬(L.append p hj).good p x) ≀
L.law.event (fun x=>Β¬L.good p x)+13/(1000*p.K) := by
apply first_failure_sigma L.law (fun x=>(L.state x).sampleLaw p hj) (L.good p)
((L.append p hj).good p) _ (by positivity)
intro x hx
apply le_trans ((L.state x).sampleLaw p hj |>.event_mono ?_)
((L.state x).sampleLaw_budget p hj).le
intro y hy hgood
exact hy (L.append_good p hj x hx y hgood)
noncomputable def layers (p : Trial.Parameters G) : (j : β„•) β†’ j ≀ p.K β†’ Layer p j
| 0, _ => firstLayer p
| j+1, h => (layers p j (by omega)).append p (by omega)
theorem layers_failure (p : Trial.Parameters G) (j : β„•) (hj : j ≀ p.K) :
(layers p j hj).law.event (fun x=>Β¬(layers p j hj).good p x) ≀ j*(13/(1000*p.K)) := by
induction j with
| zero =>
have hg : βˆ€ x,(firstLayer p).good p x := by
intro x
constructor
Β· exact initial_balanced p.hn p.even
Β· intro i; exact Fin.elim0 i
simp [layers,Law.event,hg]
| succ j ih =>
refine le_trans ((layers p j (by omega)).append_failure p (by omega)) ?_
calc
_ ≀ (j:ℝ)*(13/(1000*p.K))+13/(1000*p.K) := by linarith only [ih (by omega)]
_ = _ := by push_cast; ring
noncomputable def output (p : Trial.Parameters G) (x : (layers p p.K le_rfl).Ξ©) : β„š :=
(Z (completeGraph G.n):β„š)*∏ i,(layers p p.K le_rfl).ratios x i
lemma output_nonneg (p : Trial.Parameters G) (x : (layers p p.K le_rfl).Ξ©) : 0 ≀ output p x := by
apply mul_nonneg (Nat.cast_nonneg _)
exact prod_nonneg fun i _=>(layers p p.K le_rfl).ratios_nonneg x i
def Accurate (p : Trial.Parameters G) (y : β„š) : Prop :=
(1-p.epsilon)*(Z G:β„š)≀y ∧ y≀(1+p.epsilon)*(Z G:β„š)
theorem good_output (p : Trial.Parameters G) (hZ : 0<Z G) (x : (layers p p.K le_rfl).Ξ©)
(hx : (layers p p.K le_rfl).good p x) : Accurate p (output p x) := by
have h:=Trial.final_trial_interval G p.even p.epsilon p.epsilon_pos p.epsilon_lt_one p.K p.schedule hZ
(fun i=>((layers p p.K le_rfl).ratios x i:ℝ)) hx.2
unfold Accurate output
dsimp only at h
exact_mod_cast h
theorem actual_finite_trial_failure (p : Trial.Parameters G) (hZ : 0<Z G) :
(layers p p.K le_rfl).law.event (fun x=>Β¬Accurate p (output p x)) ≀ 13/1000 := by
have hh := layers_failure p p.K le_rfl
have he : (p.K:ℝ)*(13/(1000*p.K))=13/1000 := by
have hk : (p.K:ℝ)β‰ 0 := by exact_mod_cast p.K_pos.ne'
rw [←mul_div_assoc,mul_comm (1000:ℝ),mul_div_mul_left _ _ hk]
rw [he] at hh
apply le_trans ((layers p p.K le_rfl).law.event_mono ?_) hh
intro x hx hg
exact hx (good_output p hZ x hg)
end MatchingFPRAS.FiniteTrial
end OAI