import Mathlib import OAI.Combinatorics.KServer.AllocationMovement import OAI.Combinatorics.KServer.Hierarchy import OAI.Combinatorics.KServer.PartitionBudget namespace OAI /-! The posterior counting measure is built from labeled hidden positions. Integrating adapted metric tests is exactly conditional expectation, and prefix quota domination is relative to this measure, not an extra premise. -/ noncomputable section open scoped BigOperators open Finset namespace KServer.PosteriorCounting attribute [local instance] Classical.propDecidable Classical.decEq open RankTracking PosteriorRanks HierarchyCounts variable {Ω Y A:Type} [Fintype Ω] [Fintype Y] [Fintype A] variable {w:Ω→ℝ} (hw:∀ ω,0≤w ω) {k:ℕ} def atoms (q:Fin k→Y) (y:Y):ℝ:=∑ l:Fin k,if q l=y then 1 else 0 def measure (H:Ω→ℕ) (q:Ω→Fin k→Y) (ω:Ω) (y:Y):ℝ:= posterior w H (fun ρ=>atoms (q ρ) y) ω omit [Fintype Y] in lemma atoms_nonneg (q:Fin k→Y) (y:Y):0≤atoms q y:=by exact sum_nonneg fun _ _=>by split_ifs <;> norm_num lemma atoms_total (q:Fin k→Y):(∑ y,atoms q y)=(k:ℝ):=by unfold atoms rw [sum_comm] simp include hw in omit [Fintype Y] in lemma measure_nonneg (H:Ω→ℕ) (q:Ω→Fin k→Y) (ω:Ω) (y:Y): 0 ≤ measure (w:=w) H q ω y:=posterior_nonneg hw H (fun _=>atoms_nonneg _ _) ω omit [Fintype Y] in lemma measure_adapted (H:Ω→ℕ) (q:Ω→Fin k→Y) (y:Y): Adapted H (fun ω=>measure (w:=w) H q ω y):=posterior_adapted w H _ include hw in lemma measure_total (H:Ω→ℕ) (q:Ω→Fin k→Y) (ω:Ω) (hω:0atoms_total _) (positive_mass hw H ω hω).ne' lemma atoms_integral (q:Fin k→Y) (f:Y→ℝ): (∑ y,atoms q y*f y)=∑ l:Fin k,f (q l):=by unfold atoms simp only [sum_mul,ite_mul,one_mul,zero_mul] rw [sum_comm] simp lemma conditional_integral (H:Ω→ℕ) (q:Ω→Fin k→Y) (ω:Ω) (F:Ω→Y→ℝ) (hF:∀ ρ,H ρ=H ω→F ρ=F ω): (∑ y,measure (w:=w) H q ω y*F ω y)= posterior w H (fun ρ=>∑ l:Fin k,F ρ (q ρ l)) ω:=by unfold measure simp_rw [←posterior_mul] rw [←posterior_sum] unfold posterior numerator congr 1 apply sum_congr rfl intro ρ hρ dsimp only rw [atoms_integral,hF ρ (mem_filter.mp hρ).2] lemma conditional_mean (H:Ω→ℕ) (f:Ω→ℝ) (ω:Ω): (∑ ρ,conditional w H ω ρ*f ρ)=posterior w H f ω:=by unfold conditional posterior numerator simp only [ite_mul,zero_mul,div_mul_eq_mul_div] rw [←sum_filter,←sum_div] rfl omit [Fintype A] in lemma prefix_mass (H:Ω→ℕ) (maps:Ω→ℕ→Y→A) (q:Ω→Fin k→Y) {d:ℕ} (word:Fin d→A) (ω:Ω) (hm:∀ ρ,H ρ=H ω→maps ρ=maps ω): (∑ y∈univ.filter (hit (maps ω) word),measure (w:=w) H q ω y)= posterior w H (fun ρ=>(count (maps ρ) word (q ρ):ℝ)) ω:=by unfold measure posterior numerator rw [←sum_div] congr 1 rw [sum_comm] apply sum_congr rfl intro ρ hρ rw [←mul_sum] congr 1 have he:=hm ρ (mem_filter.mp hρ).2 dsimp only rw [he] unfold atoms count rw [sum_comm] simp only [card_filter,Nat.cast_sum,Nat.cast_ite,Nat.cast_one,Nat.cast_zero] apply sum_congr rfl intro l _ simp [eq_comm] lemma size_prefix_bound (H:ℕ→Ω→ℕ) (hr:∀ t ω ρ,H (t+1) ω=H (t+1) ρ→H t ω=H t ρ) (maps:ℕ→Ω→ℕ→Y→A) (q:ℕ→Ω→Fin k→Y) {d:ℕ} (word:Fin d→A) (t:ℕ) (ω:Ω) (hω:0w ω.val omit [Fintype Ω] in lemma weight_pos (w:Ω→ℝ) (ω:Support w):00w ω*f ω) have hz:(∑ ω:{ω:Ω // ¬01)).trans (by simpa only [mul_one] using hw1) lemma mass_support {w:Ω→ℝ} (hw:∀ ω,0≤w ω) (H:Ω→ℕ) (h:ℕ): PosteriorRanks.mass (weight w) (fun ω=>H ω.val) h=PosteriorRanks.mass w H h:=by simpa only [PosteriorRanks.mass,PosteriorRanks.fiber,sum_filter,mul_ite,mul_one,mul_zero] using sum_support hw (fun ω=>if H ω=h then 1 else 0) lemma numerator_support {w:Ω→ℝ} (hw:∀ ω,0≤w ω) (H:Ω→ℕ) (f:Ω→ℝ) (h:ℕ): PosteriorRanks.numerator (weight w) (fun ω=>H ω.val) (fun ω=>f ω.val) h= PosteriorRanks.numerator w H f h:=by simpa only [PosteriorRanks.numerator,PosteriorRanks.fiber,sum_filter,mul_ite,mul_zero] using sum_support hw (fun ω=>if H ω=h then f ω else 0) lemma posterior_support {w:Ω→ℝ} (hw:∀ ω,0≤w ω) (H:Ω→ℕ) (f:Ω→ℝ) (ω:Support w): posterior (weight w) (fun ρ=>H ρ.val) (fun ρ=>f ρ.val) ω=posterior w H f ω.val:=by simp only [posterior,mass_support hw,numerator_support hw] lemma measure_support {Y:Type} [Fintype Y] {k:ℕ} {w:Ω→ℝ} (hw:∀ ω,0≤w ω) (H:Ω→ℕ) (q:Ω→Fin k→Y) (ω:Support w) (y:Y): PosteriorCounting.measure (w:=weight w) (fun ρ=>H ρ.val) (fun ρ=>q ρ.val) ω y= PosteriorCounting.measure (w:=w) H q ω.val y:= posterior_support hw H (fun ρ=>PosteriorCounting.atoms (q ρ) y) ω end KServer.PositiveExperiment end /-! The single positive finite joint request/tape experiment for every scale. All maps use precisely the request history and the independent finite tapes. -/ noncomputable section open scoped BigOperators open Finset namespace KServer.JointExperiment open FiniteExperiment PartitionProbabilities RankTracking PosteriorRanks attribute [local instance] Classical.propDecidable Classical.decEq variable {Y:Type} [MetricSpace Y] [Fintype Y] {k H L:ℕ} instance (priority := 2000) levelTapeFintype (r:ℝ):Fintype (LevelKeys.Tape Y k H r):=by letI (n:ℕ):DecidableEq (Fin n):=Classical.decEq _ infer_instance abbrev Tape (Y:Type) [MetricSpace Y] [Fintype Y] (k H L:ℕ) (r:Fin L→ℝ):= (d:Fin L)→LevelKeys.Tape Y k H (r d) def tapeWeight (r:Fin L→ℝ):Tape Y k H L r→ℝ:= productWeight (fun d=>LevelLaw.weight (Y:=Y) (k:=k) (H:=H) (r d)) lemma tapeWeight_sum {r:Fin L→ℝ} (hr:∀ d,0LevelLaw.weight (Y:=Y) (k:=k) (H:=H) (r d)) (fun d=>LevelLaw.weight_sum (Y:=Y) (k:=k) (H:=H) (hr d)) using 1 congr 1 ext x simp only [mem_univ] lemma tapeWeight_nonneg {r:Fin L→ℝ} (hr:∀ d,0LevelLaw.weight_nonneg (Y:=Y) (k:=k) (H:=H) (hr d) b) a def tapeLaw {r:Fin L→ℝ} (hr:∀ d,0f ω.val)=avg (rawWeight law hr) f:= PositiveExperiment.sum_support (raw_nonneg law hr) f lemma request_pos (ω:Atom (k:=k) law hr):0