introvoyz045's picture
Migrated from GitHub
4a56cd6 verified
Raw History Blame Contribute Delete
4.5 kB
import OAI.Combinatorics.ContingencyTables.Dense.DenseTrialSemantics
namespace OAI
/-! # Enumerating tables through their free entries -/
namespace ContingencyTables.BoxCompletion
open DenseAffine DensePrograms
open scoped BigOperators Classical
noncomputable section
abbrev Box (m n N : β„•) := Fin (m*n) β†’ Fin (N+1)
def freeValues {m n N : β„•} (z : Box m n N) : Fin mΓ—Fin n β†’ β„• :=
fun ij => (z (finProdFinEquiv ij)).val
def decode {m n : β„•} (R : Option (Fin m) β†’ β„•) (P : Option (Fin n) β†’ β„•)
(htotal : βˆ‘ i,R i=βˆ‘ j,P j) (N : β„•) (z : Box m n N) : Option (Table R P) :=
if hz : βˆ€ i j,0 ≀ integerTable R P (freeValues z) i j then
some ⟨naturalTable R P (freeValues z),naturalTable_rows R P _ hz,
naturalTable_columns R P _ hz htotal⟩
else none
def encode {m n N : β„•} (R : Option (Fin m) β†’ β„•) (P : Option (Fin n) β†’ β„•)
(hN : (βˆ‘ i,R i)≀N) (X : Table R P) : Box m n N := fun k =>
⟨X.val (some (finProdFinEquiv.symm k).1) (some (finProdFinEquiv.symm k).2),
Nat.lt_succ_of_le ((entry_le_total X.property _ _).trans hN)⟩
lemma freeValues_encode {m n N : β„•} (R : Option (Fin m) β†’ β„•)
(P : Option (Fin n) β†’ β„•) (hN : (βˆ‘ i,R i)≀N) (X : Table R P) :
freeValues (encode R P hN X)=fun ij => X.val (some ij.1) (some ij.2) := by
funext ij
simp only [freeValues,encode,Equiv.symm_apply_apply]
lemma decode_encode {m n N : β„•} (R : Option (Fin m) β†’ β„•)
(P : Option (Fin n) β†’ β„•) (htotal : βˆ‘ i,R i=βˆ‘ j,P j)
(hN : (βˆ‘ i,R i)≀N) (X : Table R P) :
decode R P htotal N (encode R P hN X)=some X := by
have he := integerTable_recover R P X.val X.property.1 X.property.2
have hz : βˆ€ i j,0 ≀ integerTable R P (freeValues (encode R P hN X)) i j := by
rw [freeValues_encode,he]
exact fun _ _ => Int.natCast_nonneg _
rw [decode,dite_eq_left hz]
congr 1
apply Subtype.ext
funext i j
simp only [naturalTable,freeValues_encode,he,Int.toNat_natCast]
/-- Each feasible table occurs at exactly one enumerated free vector. -/
theorem decode_eq_some {m n N : β„•} (R : Option (Fin m) β†’ β„•)
(P : Option (Fin n) β†’ β„•) (htotal : βˆ‘ i,R i=βˆ‘ j,P j)
(hN : (βˆ‘ i,R i)≀N) (z : Box m n N) (X : Table R P) :
decode R P htotal N z=some X ↔ z=encode R P hN X := by
constructor
Β· intro h
unfold decode at h
split at h
next hz =>
have he := Option.some.inj h
funext k
apply Fin.ext
have hv := congrArg (fun Y : Table R P =>
Y.val (some (finProdFinEquiv.symm k).1) (some (finProdFinEquiv.symm k).2)) he
simpa only [naturalTable_free,freeValues,Prod.mk.eta,
Equiv.apply_symm_apply,encode] using hv
next hz => cases h
Β· rintro rfl
exact decode_encode R P htotal hN X
lemma decode_elim_sum {A : Type*} [Fintype A] (y : Option A) (w : A β†’ β„•) :
y.elim 0 w=βˆ‘ a,if y=some a then w a else 0 := by
cases y <;> simp
/-- Summing over candidate completions counts each feasible table once,
and gives zero weight to every rejected completion. -/
theorem sum_decode {m n N : β„•} (R : Option (Fin m) β†’ β„•)
(P : Option (Fin n) β†’ β„•) (htotal : βˆ‘ i,R i=βˆ‘ j,P j)
(hN : (βˆ‘ i,R i)≀N) (w : Table R P β†’ β„•) :
(βˆ‘ z : Box m n N,(decode R P htotal N z).elim 0 w)=βˆ‘ X,w X := by
simp_rw [decode_elim_sum]
rw [Finset.sum_comm]
apply Finset.sum_congr rfl
intro X _
simp_rw [decode_eq_some R P htotal hN]
simp
/-- The existing executable acceptance test returns the same optional
table as the finite enumeration used in the counting proof. -/
theorem acceptCompletion_eq {m n N : β„•} (R : Option (Fin m) β†’ β„•)
(P : Option (Fin n) β†’ β„•) (htotal : βˆ‘ i,R i=βˆ‘ j,P j) (z : Box m n N) :
acceptCompletion ((marginCode R,marginCode P),
matrixCode (fun i j => freeValues z (i,j)))=
(decode R P htotal N z).map tableOutput := by
by_cases hz : βˆ€ i j,0 ≀ integerTable R P (freeValues z) i j
Β· have hv := (completionValid_code R P (fun i j => freeValues z (i,j))).mpr hz
simp only [acceptCompletion,hv,ite_true,decode,dite_eq_left hz]
rw [completedMatrix_code]
rfl
Β· have hv : completionValid ((marginCode R,marginCode P),
matrixCode (fun i j => freeValues z (i,j)))β‰ true := by
intro h
exact hz ((completionValid_code R P _).mp h)
simp only [acceptCompletion,hv,Bool.false_eq_true,ite_false,decode,dite_eq_right hz,
Option.map_none]
end
end ContingencyTables.BoxCompletion
end OAI