import Mathlib import OAI.Combinatorics.IndependentSets.Repetition.Rectangle2 import OAI.Combinatorics.IndependentSets.Repetition.Overlap namespace OAI namespace IndependentSetsGames.Foundations.Repetition open scoped BigOperators open Games noncomputable section universe u def TraceSeed (σ : Type u) : Nat → Type u | 0 => PUnit | n + 1 => σ × TraceSeed σ n instance traceSeedFintype {σ : Type*} [Fintype σ] (n : Nat) : Fintype (TraceSeed σ n) := by induction n with | zero => exact inferInstanceAs (Fintype PUnit) | succ n ih => exact inferInstanceAs (Fintype (σ × TraceSeed σ n)) instance traceSeedUniqueZero {σ : Type*} : Unique (TraceSeed σ 0) := inferInstanceAs (Unique PUnit) def traceList {σ : Type*} : (n : Nat) → TraceSeed σ n → List σ | 0, _ => [] | n + 1, seed => seed.1 :: traceList n seed.2 def traceSeedLaw {σ : Type*} [Fintype σ] (μ : FiniteDistribution σ) : (n : Nat) → FiniteDistribution (TraceSeed σ n) | 0 => { weight := fun _ => 1, nonnegative := by intro; norm_num, normalized := by simp } | n + 1 => μ.product (traceSeedLaw μ n) theorem traceSeedLaw_expectation {σ : Type*} [Fintype σ] (μ : FiniteDistribution σ) (n : Nat) (f : List σ → ℝ) : (traceSeedLaw μ n).expectation (fun seed => f (traceList n seed)) = CorrelatedSampling.traceAverage μ.weight n f := by induction n generalizing f with | zero => simp [traceSeedLaw, traceList, FiniteDistribution.expectation, CorrelatedSampling.traceAverage] | succ n ih => change (μ.product (traceSeedLaw μ n)).expectation (fun seed => f (seed.1 :: traceList n seed.2)) = _ rw [FiniteDistribution.expectation_product] simp only [CorrelatedSampling.traceAverage, FiniteDistribution.expectation] apply Finset.sum_congr rfl intro s _ congr 1 exact ih (fun xs => f (s :: xs)) def sharedOutputLaw {X Y S Seed : Type*} [Fintype X] [Fintype Y] [Fintype S] [Fintype Seed] [DecidableEq S] (μ : FiniteDistribution (X × Y)) (seedLaw : FiniteDistribution Seed) (left : Seed → X → S) (right : Seed → Y → S) : FiniteDistribution ((X × Y) × S × S) := (μ.product seedLaw).pushforward (fun z => (z.1, left z.2 z.1.1, right z.2 z.1.2)) theorem sharedOutputLaw_weight {X Y S Seed : Type*} [Fintype X] [Fintype Y] [Fintype S] [Fintype Seed] [DecidableEq S] (μ : FiniteDistribution (X × Y)) (seedLaw : FiniteDistribution Seed) (left : Seed → X → S) (right : Seed → Y → S) (x : X) (y : Y) (a b : S) : (sharedOutputLaw μ seedLaw left right).weight ((x,y),a,b) = μ.weight (x,y) * seedLaw.expectation (fun seed => if left seed x = a ∧ right seed y = b then 1 else 0) := by classical simp only [sharedOutputLaw, FiniteDistribution.pushforward, FiniteDistribution.product, Fintype.sum_prod_type, Prod.mk.injEq] simp only [and_assoc, ite_and] simp [FiniteDistribution.expectation, Finset.mul_sum, mul_ite] open CorrelatedSampling variable {X Y S : Type*} [Fintype X] [Fintype Y] [Fintype S] [Nonempty S] [DecidableEq S] def samplerLocal (thresholds : List ℝ) (profile : X → FiniteDistribution S) (fallback : S) (n : Nat) (seed : TraceSeed (RectangleSeed thresholds S) n) (x : X) : S := localSample (rectangleAccept thresholds (profile x).weight) Prod.fst fallback (traceList n seed) def samplerOutputLaw (thresholds : List ℝ) (μ : FiniteDistribution (X × Y)) (L : X → FiniteDistribution S) (R : Y → FiniteDistribution S) (fallback : S) (n : Nat) : FiniteDistribution ((X × Y) × S × S) := sharedOutputLaw μ (traceSeedLaw (rectangleDistribution thresholds) n) (samplerLocal thresholds L fallback n) (samplerLocal thresholds R fallback n) omit [Nonempty S] [DecidableEq S] in theorem probability_weight_le_one (μ : FiniteDistribution S) (s : S) : μ.weight s ≤ 1 := by calc μ.weight s ≤ ∑ t, μ.weight t := Finset.single_le_sum (fun t _ => μ.nonnegative t) (Finset.mem_univ s) _ = 1 := μ.normalized theorem samplerOutputLaw_diagonal (thresholds : List ℝ) (μ : FiniteDistribution (X × Y)) (L : X → FiniteDistribution S) (R : Y → FiniteDistribution S) (hL : ∀ x s, (L x).weight s ∈ thresholds) (hR : ∀ y s, (R y).weight s ∈ thresholds) (fallback : S) (n : Nat) (x : X) (y : Y) (a : S) : μ.weight (x,y) * min ((L x).weight a) ((R y).weight a) / (1 + Information.totalVariation (L x).weight (R y).weight) * (1 - eventMass (rectangleWeight thresholds) (fun s => !(rectangleAccept thresholds (L x).weight s || rectangleAccept thresholds (R y).weight s)) ^ n) ≤ (samplerOutputLaw thresholds μ L R fallback n).weight ((x,y),a,a) := by have h := rectangle_label_diagonal_bound thresholds (L x).weight (R y).weight fallback a (hL x) (hR y) (L x).nonnegative (R y).nonnegative (probability_weight_le_one (L x)) (probability_weight_le_one (R y)) (L x).normalized (R y).normalized n rw [samplerOutputLaw, sharedOutputLaw_weight] have he : (traceSeedLaw (rectangleDistribution (α := S) thresholds) n).expectation (fun seed => if samplerLocal thresholds L fallback n seed x = a ∧ samplerLocal thresholds R fallback n seed y = a then 1 else 0) = traceAverage (rectangleWeight thresholds) n (localDiagonal (rectangleAccept thresholds (L x).weight) (rectangleAccept thresholds (R y).weight) Prod.fst fallback a) := by exact traceSeedLaw_expectation (rectangleDistribution (α := S) thresholds) n (localDiagonal (rectangleAccept thresholds (L x).weight) (rectangleAccept thresholds (R y).weight) Prod.fst fallback a) rw [he] simp only [Information.totalVariation, CorrelatedSampling.totalVariation] at h ⊢ simpa only [mul_div_assoc, mul_assoc] using mul_le_mul_of_nonneg_left h (μ.nonnegative (x,y)) theorem samplerOutputLaw_uniform_diagonal (μ : FiniteDistribution (X × Y)) (L : X → FiniteDistribution S) (R : Y → FiniteDistribution S) (fallback : S) (n : Nat) (x : X) (y : Y) (a : S) : μ.weight (x,y) * min ((L x).weight a) ((R y).weight a) / (1 + Information.totalVariation (L x).weight (R y).weight) * (1 - uniformRejectionRate S ^ n) ≤ (samplerOutputLaw (sharedProfileThresholds L R) μ L R fallback n).weight ((x,y),a,a) := by have hp := pow_le_pow_left₀ (sharedProfileReject_nonnegative L R x y) (sharedProfile_reject_le_rate L R x y) n have hco : 0 ≤ μ.weight (x,y) * min ((L x).weight a) ((R y).weight a) / (1 + Information.totalVariation (L x).weight (R y).weight) := div_nonneg (mul_nonneg (μ.nonnegative (x,y)) (le_min ((L x).nonnegative a) ((R y).nonnegative a))) (by linarith [Information.totalVariation_nonneg (L x).weight (R y).weight]) have h := samplerOutputLaw_diagonal (sharedProfileThresholds L R) μ L R (left_mem_sharedProfileThresholds L R) (right_mem_sharedProfileThresholds L R) fallback n x y a exact (mul_le_mul_of_nonneg_left (sub_le_sub_left hp 1) hco).trans h theorem samplerOutputLaw_totalVariation (p : FiniteDistribution ((X × Y) × S)) (μ : FiniteDistribution (X × Y)) (L : X → FiniteDistribution S) (R : Y → FiniteDistribution S) (fallback : S) (n : Nat) : Information.totalVariation (samplerOutputLaw (sharedProfileThresholds L R) μ L R fallback n).weight (diagonalWeights p.weight) ≤ 2 * Information.totalVariation p.weight (fun z => μ.weight z.1 * (L z.1.1).weight z.2) + 2 * Information.totalVariation p.weight (fun z => μ.weight z.1 * (R z.1.2).weight z.2) + uniformRejectionRate S ^ n := by apply diagonal_sampler_totalVariation_le p.weight μ.weight (fun xy => (L xy.1).weight) (fun xy => (R xy.2).weight) _ (Information.gameLaw_isProbability p) (Information.gameLaw_isProbability μ) (fun xy => Information.gameLaw_isProbability (L xy.1)) (fun xy => Information.gameLaw_isProbability (R xy.2)) (Information.gameLaw_isProbability _) (pow_nonneg (uniformRejectionRate_nonnegative S) n) · exact pow_le_one₀ (uniformRejectionRate_nonnegative S) (uniformRejectionRate_lt_one S).le · intro xy a exact samplerOutputLaw_uniform_diagonal μ L R fallback n xy.1 xy.2 a theorem samplerOutputLaw_arbitrarily_close (p : FiniteDistribution ((X × Y) × S)) (μ : FiniteDistribution (X × Y)) (L : X → FiniteDistribution S) (R : Y → FiniteDistribution S) (fallback : S) (η : ℝ) (hη : 0 < η) : ∃ n : Nat, Information.totalVariation (samplerOutputLaw (sharedProfileThresholds L R) μ L R fallback n).weight (diagonalWeights p.weight) ≤ 2 * Information.totalVariation p.weight (fun z => μ.weight z.1 * (L z.1.1).weight z.2) + 2 * Information.totalVariation p.weight (fun z => μ.weight z.1 * (R z.1.2).weight z.2) + η := by obtain ⟨n,hn⟩ := exists_uniformRejectionRate_pow_lt S η hη exact ⟨n, (samplerOutputLaw_totalVariation p μ L R fallback n).trans (by linarith)⟩ end end IndependentSetsGames.Foundations.Repetition end OAI