math / data /lean /OAI /Combinatorics /ContingencyTables /Rooted /ForestLiteralCount.lean
introvoyz045's picture
Migrated from GitHub
4a56cd6 verified
Raw History Blame Contribute Delete
15.9 kB
import OAI.Combinatorics.ContingencyTables.Rooted.ForestPenaltyApplication
namespace OAI
/-!
# Literal doubled-coordinate profiles for companion completion counts
Small views are retained separately. Every large pair is balanced, with
its physical tree or free-cell value represented in the prescribed finite
interval. These identities connect the concrete laminar factor family to
the actual graph completion sum.
-/
namespace ContingencyTables.ForestLiteralCount
open scoped BigOperators Classical
open SimpleGraph
open GraphIntegralCompletion GraphCompletionCounts ForestCapacity
variable {I J : Type*} [Fintype I] [Fintype J]
noncomputable def width (T : SimpleGraph (IβŠ•J)) (d : β„•) (b : I β†’ J β†’ β„•) (i : I) (j : J) : β„• :=
if T.Adj (.inl i) (.inr j) then 20*d*b i j else b i j
noncomputable def lower (T : SimpleGraph (IβŠ•J)) (d : β„•) (b : I β†’ J β†’ β„•) (i : I) (j : J) : β„€ :=
if T.Adj (.inl i) (.inr j) then -((10*d*b i j:β„•):β„€) else 0
noncomputable def cellCount (T : SimpleGraph (IβŠ•J)) (d : β„•) (b free : I β†’ J β†’ β„•)
(a : TreeIndex T d b) (i : I) (j : J) : β„• :=
if h : T.Adj (.inl i) (.inr j) then (a ⟨(i,j),h⟩).val else free i j
noncomputable def profile (small : I β†’ J β†’ Bool) (T : SimpleGraph (IβŠ•J))
(d : β„•) (b x y free : I β†’ J β†’ β„•) (a : TreeIndex T d b) (u : (IΓ—J)Γ—Bool) : β„• :=
if small u.1.1 u.1.2 then (if u.2 then y u.1.1 u.1.2 else x u.1.1 u.1.2)
else if u.2 then width T d b u.1.1 u.1.2-cellCount T d b free a u.1.1 u.1.2
else cellCount T d b free a u.1.1 u.1.2
noncomputable def largeValue (T : SimpleGraph (IβŠ•J)) (tree : I β†’ J β†’ β„€)
(free : I β†’ J β†’ β„•) (i : I) (j : J) : β„€ :=
if T.Adj (.inl i) (.inr j) then tree i j else free i j
omit [Fintype I] [Fintype J] in
lemma cellCount_le (small : I β†’ J β†’ Bool) (T : SimpleGraph (IβŠ•J))
(d : β„•) (b free : I β†’ J β†’ β„•) (a : TreeIndex T d b)
(hf : βˆ€ i j, small i j=false β†’ Β¬T.Adj (.inl i) (.inr j) β†’ free i j≀b i j)
(i : I) (j : J) (hs : small i j=false) : cellCount T d b free a i j≀width T d b i j := by
by_cases ht : T.Adj (.inl i) (.inr j)
Β· simp only [cellCount,ht,dite_eq_left,width,ite_true]
have h := (a ⟨(i,j),ht⟩).isLt
change (a ⟨(i,j),ht⟩).val <20*d*b i j+1 at h
omega
Β· simp only [cellCount,ht,not_false_eq_true,dite_eq_right,width,ite_false]
exact hf i j hs ht
omit [Fintype I] [Fintype J] in
lemma row_profile (small : I β†’ J β†’ Bool) (T : SimpleGraph (IβŠ•J))
(hTG : T≀cellGraph small) (d : β„•) (b x y free : I β†’ J β†’ β„•) (a : TreeIndex T d b)
(i : I) (j : J) :
ForestPenaltyApplication.physicalRow (lower T d b) (profile small T d b x y free a) i j=
(if small i j then (x i j:β„€) else largeValue T (physical T d b a) free i j : β„€) := by
by_cases ht : T.Adj (.inl i) (.inr j)
Β· have hs : small i j=false := hTG ht
simp only [ForestPenaltyApplication.physicalRow,lower,profile,hs,Bool.false_eq_true,
ite_false,ht,ite_true,cellCount,dite_eq_left,largeValue,physical]
push_cast
ring
Β· simp only [ForestPenaltyApplication.physicalRow,lower,ht,ite_false,profile,
Bool.false_eq_true,cellCount,not_false_eq_true,dite_eq_right,largeValue]
split_ifs <;> norm_num
omit [Fintype I] [Fintype J] in
lemma column_profile (small : I β†’ J β†’ Bool) (T : SimpleGraph (IβŠ•J))
(hTG : T≀cellGraph small) (d : β„•) (b x y free : I β†’ J β†’ β„•) (a : TreeIndex T d b)
(hy : βˆ€ i j, small i j=true β†’ y i j≀b i j)
(hf : βˆ€ i j, small i j=false β†’ Β¬T.Adj (.inl i) (.inr j) β†’ free i j≀b i j)
(i : I) (j : J) :
ForestPenaltyApplication.physicalColumn (lower T d b) (width T d b)
(profile small T d b x y free a) i j=
(if small i j then ((b i j-y i j:β„•):β„€)
else largeValue T (physical T d b a) free i j : β„€) := by
by_cases hs : small i j=true
Β· have ht : Β¬T.Adj (.inl i) (.inr j) := by
intro h
have hh : small i j=false := hTG h
exact Bool.false_ne_true (hh.symm.trans hs)
simp only [ForestPenaltyApplication.physicalColumn,lower,width,ht,ite_false,profile,
hs,ite_true,Int.cast_zero,zero_add]
push_cast
rw [Nat.cast_sub (hy i j hs)]
Β· have hs' : small i j=false := Bool.eq_false_iff.mpr hs
have hbound := cellCount_le small T d b free a hf i j hs'
have hcol : ForestPenaltyApplication.physicalColumn (lower T d b) (width T d b)
(profile small T d b x y free a) i j=
ForestPenaltyApplication.physicalRow (lower T d b) (profile small T d b x y free a) i j := by
simp only [ForestPenaltyApplication.physicalColumn,ForestPenaltyApplication.physicalRow,
profile,hs',Bool.false_eq_true,ite_false,ite_true,Nat.cast_sub hbound]
ring
rw [hcol,row_profile small T hTG d b x y free a i j]
simp only [hs',Bool.false_eq_true,ite_false]
noncomputable def actualDeviation (small : I β†’ J β†’ Bool) (T : SimpleGraph (IβŠ•J))
(b x y free : I β†’ J β†’ β„•) (r : I β†’ β„€) (c : J β†’ β„€) (tree : I β†’ J β†’ β„€) : IβŠ•J β†’ ℝ :=
Sum.elim
(fun i => ((βˆ‘ j, if small i j then (x i j:β„€) else largeValue T tree free i j):β„€)-r i)
(fun j => ((βˆ‘ i, if small i j then ((b i j-y i j:β„•):β„€)
else largeValue T tree free i j):β„€)-c j)
noncomputable def actualPenalty (small : I β†’ J β†’ Bool) (T : SimpleGraph (IβŠ•J))
(b x y free : I β†’ J β†’ β„•) (r : I β†’ β„€) (c : J β†’ β„€) (tree : I β†’ J β†’ β„€) : ℝ :=
(βˆ‘ v, if ForestPenaltyApplication.rootFlag T v then
|actualDeviation small T b x y free r c tree v| else 0)+
βˆ‘ i, βˆ‘ j, if T.Adj (.inl i) (.inr j) then
max 0 (max (-(tree i j:ℝ)) ((tree i j:ℝ)-b i j))/(b i j:ℝ) else 0
lemma deviation_profile (small : I β†’ J β†’ Bool) (T : SimpleGraph (IβŠ•J))
(hTG : T≀cellGraph small) (d : β„•) (b x y free : I β†’ J β†’ β„•) (a : TreeIndex T d b)
(r : I β†’ β„€) (c : J β†’ β„€)
(hy : βˆ€ i j, small i j=true β†’ y i j≀b i j)
(hf : βˆ€ i j, small i j=false β†’ Β¬T.Adj (.inl i) (.inr j) β†’ free i j≀b i j) :
ForestPenaltyApplication.deviation (lower T d b) (width T d b) r c
(profile small T d b x y free a)=actualDeviation small T b x y free r c (physical T d b a) := by
funext g
cases g with
| inl i =>
simp only [ForestPenaltyApplication.deviation,actualDeviation,Sum.elim_inl]
push_cast
congr 1
apply Finset.sum_congr rfl
intro j _
simpa only [Int.cast_ite,Int.cast_natCast] using row_profile small T hTG d b x y free a i j
| inr j =>
simp only [ForestPenaltyApplication.deviation,actualDeviation,Sum.elim_inr]
push_cast
congr 1
apply Finset.sum_congr rfl
intro i _
simpa only [Int.cast_ite,Int.cast_natCast] using column_profile small T hTG d b x y free a hy hf i j
lemma penalty_profile (small : I β†’ J β†’ Bool) (T : SimpleGraph (IβŠ•J))
(hTG : T≀cellGraph small) (d : β„•) (b x y free : I β†’ J β†’ β„•) (a : TreeIndex T d b)
(r : I β†’ β„€) (c : J β†’ β„€)
(hy : βˆ€ i j, small i j=true β†’ y i j≀b i j)
(hf : βˆ€ i j, small i j=false β†’ Β¬T.Adj (.inl i) (.inr j) β†’ free i j≀b i j) :
ForestPenaltyApplication.penalty T b (lower T d b) (width T d b) r c
(profile small T d b x y free a)=actualPenalty small T b x y free r c (physical T d b a) := by
unfold ForestPenaltyApplication.penalty ForestPenaltyApplication.rootPenalty actualPenalty
rw [deviation_profile small T hTG d b x y free a r c hy hf]
congr 1
unfold ForestPenaltyApplication.treePenalty
apply Finset.sum_congr rfl
intro i _
apply Finset.sum_congr rfl
intro j _
by_cases ht : T.Adj (.inl i) (.inr j)
Β· have hs : small i j=false := hTG ht
have hrow := row_profile small T hTG d b x y free a i j
simp only [hs,Bool.false_eq_true,ite_false,largeValue,ht,ite_true] at hrow
simp only [ht,ite_true,EqualitySoftening.affineDistance,mul_one]
rw [show (lower T d b i j:ℝ)+(profile small T d b x y free a ((i,j),false):ℝ)=
(physical T d b a i j:ℝ) from hrow]
Β· simp only [ht,ite_false]
omit [Fintype I] [Fintype J] in
lemma row_pointwise (small : I β†’ J β†’ Bool) (T : SimpleGraph (IβŠ•J))
(hTG : T≀cellGraph small) (x free : I β†’ J β†’ β„•) (tree : I β†’ J β†’ β„€)
(hsupport : βˆ€ i j, Β¬T.Adj (.inl i) (.inr j) β†’ tree i j=0) (i : I) (j : J) :
(if small i j then (x i j:β„€) else largeValue T tree free i j)=tree i j+
ForestPhysicalCompletion.fixedRow small T (fun i j => (x i j:β„€))
(fun i j => (free i j:β„€)) i j := by
by_cases ht : T.Adj (.inl i) (.inr j)
Β· have hs : small i j=false := hTG ht
simp only [hs,Bool.false_eq_true,ite_false,largeValue,ht,ite_true,
ForestPhysicalCompletion.fixedRow,add_zero]
Β· simp only [largeValue,ht,ite_false,hsupport i j ht,zero_add,
ForestPhysicalCompletion.fixedRow]
omit [Fintype I] [Fintype J] in
lemma column_pointwise (small : I β†’ J β†’ Bool) (T : SimpleGraph (IβŠ•J))
(hTG : T≀cellGraph small) (b y free : I β†’ J β†’ β„•) (tree : I β†’ J β†’ β„€)
(hsupport : βˆ€ i j, Β¬T.Adj (.inl i) (.inr j) β†’ tree i j=0) (i : I) (j : J) :
(if small i j then ((b i j-y i j:β„•):β„€) else largeValue T tree free i j)=tree i j+
ForestPhysicalCompletion.fixedColumn small T (fun i j => ((b i j-y i j:β„•):β„€))
(fun i j => (free i j:β„€)) i j := by
by_cases ht : T.Adj (.inl i) (.inr j)
Β· have hs : small i j=false := hTG ht
simp only [hs,Bool.false_eq_true,ite_false,largeValue,ht,ite_true,
ForestPhysicalCompletion.fixedColumn,add_zero]
Β· simp only [largeValue,ht,ite_false,hsupport i j ht,zero_add,
ForestPhysicalCompletion.fixedColumn]
lemma nonroot_iff (small : I β†’ J β†’ Bool) (T : SimpleGraph (IβŠ•J))
(hTG : T≀cellGraph small) (b x y free : I β†’ J β†’ β„•) (r : I β†’ β„€) (c : J β†’ β„€)
(tree : I β†’ J β†’ β„€) (hsupport : βˆ€ i j, Β¬T.Adj (.inl i) (.inr j) β†’ tree i j=0) :
(βˆ€ g, ForestPenaltyApplication.rootFlag T g=false β†’
actualDeviation small T b x y free r c tree g=0) ↔
NonrootMargins T
(ForestPhysicalCompletion.rowResidual small T r (fun i j => (x i j:β„€))
(fun i j => (free i j:β„€)))
(ForestPhysicalCompletion.columnResidual small T c (fun i j => ((b i j-y i j:β„•):β„€))
(fun i j => (free i j:β„€))) tree := by
constructor
Β· intro h
constructor
Β· intro i hi
have hflag : ForestPenaltyApplication.rootFlag T (.inl i)=false := by
simpa only [ForestPenaltyApplication.rootFlag,decide_eq_false_iff_not] using hi
have hraw := h (.inl i) hflag
simp only [actualDeviation,Sum.elim_inl] at hraw
have hsum : (βˆ‘ j, if small i j then (x i j:β„€) else largeValue T tree free i j)=r i := by
exact_mod_cast sub_eq_zero.mp hraw
simp_rw [row_pointwise small T hTG x free tree hsupport] at hsum
rw [Finset.sum_add_distrib] at hsum
unfold ForestPhysicalCompletion.rowResidual
omega
Β· intro j hj
have hflag : ForestPenaltyApplication.rootFlag T (.inr j)=false := by
simpa only [ForestPenaltyApplication.rootFlag,decide_eq_false_iff_not] using hj
have hraw := h (.inr j) hflag
simp only [actualDeviation,Sum.elim_inr] at hraw
have hsum : (βˆ‘ i, if small i j then ((b i j-y i j:β„•):β„€)
else largeValue T tree free i j)=c j := by exact_mod_cast sub_eq_zero.mp hraw
simp_rw [column_pointwise small T hTG b y free tree hsupport] at hsum
rw [Finset.sum_add_distrib] at hsum
unfold ForestPhysicalCompletion.columnResidual
omega
Β· intro h g hg
cases g with
| inl i =>
have hi : Sum.inl i≠componentRoot T (.inl i) := by
simpa only [ForestPenaltyApplication.rootFlag,decide_eq_false_iff_not] using hg
have hsum : (βˆ‘ j, if small i j then (x i j:β„€) else largeValue T tree free i j)=r i := by
simp_rw [row_pointwise small T hTG x free tree hsupport]
rw [Finset.sum_add_distrib,h.1 i hi]
exact sub_add_cancel _ _
simp only [actualDeviation,Sum.elim_inl]
have hreal : (((βˆ‘ j, if small i j then (x i j:β„€)
else largeValue T tree free i j):β„€):ℝ)=(r i:ℝ) := by exact_mod_cast hsum
rw [hreal,sub_self]
| inr j =>
have hj : Sum.inr j≠componentRoot T (.inr j) := by
simpa only [ForestPenaltyApplication.rootFlag,decide_eq_false_iff_not] using hg
have hsum : (βˆ‘ i, if small i j then ((b i j-y i j:β„•):β„€)
else largeValue T tree free i j)=c j := by
simp_rw [column_pointwise small T hTG b y free tree hsupport]
rw [Finset.sum_add_distrib,h.2 j hj]
exact sub_add_cancel _ _
simp only [actualDeviation,Sum.elim_inr]
have hreal : (((βˆ‘ i, if small i j then ((b i j-y i j:β„•):β„€)
else largeValue T tree free i j):β„€):ℝ)=(c j:ℝ) := by exact_mod_cast hsum
rw [hreal,sub_self]
/-- For each actual small profile and free assignment, the concrete
laminar factor sum retains exactly the source's root/tree penalty at
the unique integral completion, with its tree values in the prescribed
completion intervals. -/
theorem literal_tree_sum (small : I β†’ J β†’ Bool) (b : I β†’ J β†’ β„•)
(T : SimpleGraph (IβŠ•J)) (hTG : T≀cellGraph small) (hT : T.IsAcyclic)
(hmax : βˆ€ F : SimpleGraph (IβŠ•J), F≀cellGraph small β†’ F.IsAcyclic β†’
MaximumForest.weight F (capacity b)≀MaximumForest.weight T (capacity b))
(r : I β†’ β„€) (c : J β†’ β„€) (x y free : I β†’ J β†’ β„•) (X : I β†’ J β†’ β„€)
(d W : β„•) (alpha : ℝ) (hd : 1≀d) (hcard : Fintype.card I*Fintype.card J≀d)
(hlarge : βˆ€ i j, small i j=false β†’ W≀b i j ∧ 1≀b i j)
(hsmall : βˆ€ i j, small i j=true β†’ b i j<W ∧ x i j≀b i j ∧ y i j≀b i j)
(hf : βˆ€ i j, small i j=false β†’ Β¬T.Adj (.inl i) (.inr j) β†’ free i j≀b i j)
(hX : βˆ€ i j, 0≀X i j ∧ X i j≀b i j)
(hXr : βˆ€ i, (βˆ‘ j, X i j)=r i) (hXc : βˆ€ j, (βˆ‘ i, X i j)=c j) :
(βˆ‘ a : TreeIndex T d b, ForestPenaltyApplication.physicalWeight T b
(lower T d b) (width T d b) r c alpha (profile small T d b x y free a))=
(2:ℝ)^(-alpha*actualPenalty small T b x y free r c
(ForestPhysicalCompletion.solved small T r c (fun i j => (x i j:β„€))
(fun i j => ((b i j-y i j:β„•):β„€)) (fun i j => (free i j:β„€)))) := by
have hy : βˆ€ i j, small i j=true β†’ y i j≀b i j := fun i j hs => (hsmall i j hs).2.2
have hsum := ForestPhysicalCompletion.tree_indicator_sum small b T hTG hT hmax r c
(fun i j => (x i j:β„€)) (fun i j => ((b i j-y i j:β„•):β„€))
(fun i j => (free i j:β„€)) X d W hd hcard hlarge
(by
intro i j hs
refine ⟨(hsmall i j hs).1,⟨Nat.cast_nonneg _,?_⟩,⟨Nat.cast_nonneg _,?_⟩⟩
Β· exact_mod_cast (hsmall i j hs).2.1
Β· exact_mod_cast Nat.sub_le (b i j) (y i j))
(by
intro i j hs ht
exact ⟨Nat.cast_nonneg _,by exact_mod_cast hf i j hs ht⟩)
hX hXr hXc (fun tree => (2:ℝ)^(-alpha*actualPenalty small T b x y free r c tree))
calc
_=(βˆ‘ a : TreeIndex T d b,
if NonrootMargins T
(ForestPhysicalCompletion.rowResidual small T r (fun i j => (x i j:β„€))
(fun i j => (free i j:β„€)))
(ForestPhysicalCompletion.columnResidual small T c (fun i j => ((b i j-y i j:β„•):β„€))
(fun i j => (free i j:β„€))) (physical T d b a)
then (2:ℝ)^(-alpha*actualPenalty small T b x y free r c (physical T d b a)) else 0) := by
apply Finset.sum_congr rfl
intro a _
rw [ForestPenaltyApplication.exact_eq,deviation_profile small T hTG d b x y free a r c hy hf,
penalty_profile small T hTG d b x y free a r c hy hf]
simp only [nonroot_iff small T hTG b x y free r c (physical T d b a) (physical_supported T d b a)]
_=_ := hsum
end ContingencyTables.ForestLiteralCount
end OAI