module public import SubentropyCertificates.ClassicalSSAKernel import Mathlib.Tactic.Linarith import Mathlib.Algebra.Order.BigOperators.Group.Finset import Mathlib.Tactic.Ring /-! The determinant deficit decreases under arbitrary row and column mergings. A map sends each original label to its merged label; it need not be surjective. -/ @[expose] public section noncomputable section open scoped BigOperators namespace SubentropyCertificates.ClassicalSSA variable {I J G : Type*} [Fintype I] [Fintype J] [Fintype G] def tableDeficit (t : ℝ) (p : I → J → ℝ) := cells t Finset.univ p + factor t (mass Finset.univ p) - rows t Finset.univ p - cols t Finset.univ p def mergeRows [DecidableEq G] (f : I → G) (p : I → J → ℝ) (g : G) (j : J) := ∑ i, if f i = g then p i j else 0 theorem table_deficit_nonnegative {t : ℝ} (ht : 0 ≤ t) (p : I → J → ℝ) (hp : ∀ i j, 0 ≤ p i j) : 0 ≤ tableDeficit t p := by unfold tableDeficit linarith only [table_subadditivity ht Finset.univ p hp] /-- Proposition classical-merging, for any map of row labels. -/ theorem merge_rows_deficit [DecidableEq G] (f : I → G) (p : I → J → ℝ) (hp : ∀ i j, 0 ≤ p i j) {t : ℝ} (ht : 0 ≤ t) : 0 ≤ tableDeficit t (mergeRows f p) ∧ tableDeficit t (mergeRows f p) ≤ tableDeficit t p := by classical let q := mergeRows f p have hq (g j) : 0 ≤ q g j := Finset.sum_nonneg (fun i _ => by split <;> simp [hp]) refine ⟨table_deficit_nonnegative ht q hq, ?_⟩ let w : I × G × J → ℝ := fun u => if f u.1 = u.2.1 then p u.1 u.2.2 else 0 have hw (u) : 0 ≤ w u := by dsimp [w]; split <;> simp [hp] have H := classical_ssa_kernel w hw ht have hAB : kernel t (marginalAB w) = rows t Finset.univ p := by simp [kernel, rows, marginalAB, rowMass, w, Fintype.prod_prod_type, apply_ite (factor t)] have hBC : kernel t (marginalBC w) = cells t Finset.univ q := by simp only [kernel, marginalBC, cells, Fintype.prod_prod_type, q, mergeRows, w] have hABC : kernel t w = cells t Finset.univ p := by simp [kernel, cells, w, Fintype.prod_prod_type, apply_ite (factor t)] have hB : marginalB w = rowMass q := by funext g exact Finset.sum_comm have hcol : colMass Finset.univ q = colMass Finset.univ p := by funext j simp only [colMass, q, mergeRows] rw [Finset.sum_comm] simp have hmass : mass Finset.univ q = mass Finset.univ p := by rw [← sum_colMass, hcol, sum_colMass] rw [hAB, hBC, hABC, hB] at H change rows t Finset.univ p + cells t Finset.univ q ≤ cells t Finset.univ p + rows t Finset.univ q at H change tableDeficit t q ≤ tableDeficit t p unfold tableDeficit cols rw [hmass, hcol] linarith only [H] private theorem deficit_transpose (t : ℝ) (p : I → J → ℝ) : tableDeficit t (fun j i => p i j) = tableDeficit t p := by simp only [tableDeficit, cells, kernel, mass, rows, cols, rowMass, colMass] rw [Finset.prod_comm (f := fun j i => factor t (p i j)), Finset.sum_comm (f := fun j i => p i j)] ring def mergeColumns [DecidableEq G] (f : J → G) (p : I → J → ℝ) (i : I) (g : G) := ∑ j, if f j = g then p i j else 0 theorem merge_columns_deficit [DecidableEq G] (f : J → G) (p : I → J → ℝ) (hp : ∀ i j, 0 ≤ p i j) {t : ℝ} (ht : 0 ≤ t) : 0 ≤ tableDeficit t (mergeColumns f p) ∧ tableDeficit t (mergeColumns f p) ≤ tableDeficit t p := by have h := merge_rows_deficit f (fun j i => p i j) (fun j i => hp i j) ht rw [deficit_transpose t p] at h rw [← deficit_transpose t (mergeColumns f p)] exact h end SubentropyCertificates.ClassicalSSA