import Mathlib.Tactic.Linarith import Mathlib.Tactic.Ring import OAI.Computability.BinPacking.Information.GamesAdapter namespace OAI namespace BinPackingGames.Foundations.Repetition.ProfileCorrection open scoped BigOperators open BinPackingGames.Foundations.Information noncomputable section variable {S X Y : Type*} [Fintype S] [Fintype X] [Fintype Y] def seedQuestionMarginal (p : X × (S × Y) → ℝ) : X × S → ℝ := firstMarginal (fun z : (X × S) × Y => p (z.1.1, (z.1.2, z.2))) theorem seedQuestionMarginal_isProbability (p : X × (S × Y) → ℝ) (hp : IsProbability p) : IsProbability (seedQuestionMarginal p) := by apply firstMarginal_isProbability constructor · intro z exact hp.1 _ · have h := hp.2 simp only [Fintype.sum_prod_type] at h ⊢ exact h omit [Fintype X] in theorem seedQuestionMarginal_first [Fintype X] (p : X × (S × Y) → ℝ) : firstMarginal (seedQuestionMarginal p) = firstMarginal p := by funext x simp only [seedQuestionMarginal, firstMarginal, Fintype.sum_prod_type] theorem totalVariation_triangle {Ω : Type*} [Fintype Ω] (p c d : Ω → ℝ) : totalVariation p d ≤ totalVariation p c + totalVariation c d := by unfold totalVariation have h := Finset.sum_le_sum (s := Finset.univ) (fun x _ => abs_sub_le (p x) (c x) (d x)) rw [Finset.sum_add_distrib] at h linarith theorem left_profile_correction (p : X × (S × Y) → ℝ) (μ : X × Y → ℝ) (fallbackS : S → ℝ) (fallbackY : Y → ℝ) (hp : IsProbability p) (hμ : IsProbability μ) (hS : IsProbability fallbackS) (hY : IsProbability fallbackY) : totalVariation p (fun z => μ (z.1, z.2.2) * conditionalKernel (seedQuestionMarginal p) fallbackS z.1 z.2.1) ≤ totalVariation p (fun z => seedQuestionMarginal p (z.1, z.2.1) * conditionalKernel μ fallbackY z.1 z.2.2) + totalVariation (firstMarginal p) (firstMarginal μ) := by let σ := seedQuestionMarginal p let L := conditionalKernel σ fallbackS let M := conditionalKernel μ fallbackY let k : X → S × Y → ℝ := fun x sy => L x sy.1 * M x sy.2 let c : X × (S × Y) → ℝ := fun z => σ (z.1,z.2.1) * M z.1 z.2.2 let d : X × (S × Y) → ℝ := fun z => μ (z.1,z.2.2) * L z.1 z.2.1 have hσ : IsProbability σ := seedQuestionMarginal_isProbability p hp have hk : ∀ x, IsProbability (k x) := by intro x exact kernelProduct_isProbability (L x) (fun _ : S => M x) (conditionalKernel_isProbability σ fallbackS hσ hS x) (fun _ => conditionalKernel_isProbability μ fallbackY hμ hY x) have hc : c = fun z => firstMarginal σ z.1 * k z.1 z.2 := by funext z change σ (z.1,z.2.1) * M z.1 z.2.2 = firstMarginal σ z.1 * (L z.1 z.2.1 * M z.1 z.2.2) rw [← mul_assoc, marginal_mul_conditionalKernel σ fallbackS hσ] have hd : d = fun z => firstMarginal μ z.1 * k z.1 z.2 := by funext z change μ (z.1,z.2.2) * L z.1 z.2.1 = firstMarginal μ z.1 * (L z.1 z.2.1 * M z.1 z.2.2) calc _ = (firstMarginal μ z.1 * M z.1 z.2.2) * L z.1 z.2.1 := by rw [marginal_mul_conditionalKernel μ fallbackY hμ] _ = _ := by ring have hmiddle : totalVariation c d = totalVariation (firstMarginal σ) (firstMarginal μ) := by rw [hc, hd] exact totalVariation_joint_common_kernel _ _ k hk have result := totalVariation_triangle p c d rw [hmiddle] at result have hmarg : firstMarginal σ = firstMarginal p := seedQuestionMarginal_first p rw [hmarg] at result exact result end end BinPackingGames.Foundations.Repetition.ProfileCorrection end OAI