import OAI.Combinatorics.ContingencyTables.Rooted.ForestScaledPenalty import OAI.Combinatorics.ContingencyTables.Rooted.ForestPenaltyBound namespace OAI /-! # Integer free assignments use the original scaled affine tree values -/ namespace ContingencyTables.ForestScaledPenalty open scoped BigOperators Classical open CompanionFactorialApplication GraphCompletionCounts ForestAssignmentSplit open ForestAffineCoefficients ForestPhysicalCompletion ForestRootPenalty variable {I J : Type*} [Fintype I] [Fintype J] lemma free_coefficient_sum (A : Instance I J) (t : TreeCell A.T) (F : I×J → ℝ) : (∑ f : FreeCell A.small A.T,coefficient A t f.val*F f.val)= ∑ a : I×J,coefficient A t a*F a := by let p : I×J → Prop := fun a => A.small a.1 a.2=false ∧ ¬A.T.Adj (.inl a.1) (.inr a.2) calc _ = ∑ a ∈ Finset.univ.filter p,coefficient A t a*F a := (Finset.sum_subtype (p := p) (Finset.univ.filter p) (fun _ => by simp) (fun a => coefficient A t a*F a)).symm _ = _ := by apply Finset.sum_subset (Finset.filter_subset _ _) intro a _ ha have hp : ¬p a := by simpa only [Finset.mem_filter,Finset.mem_univ,true_and] using ha change ¬(A.small a.1 a.2=false ∧ ¬A.T.Adj (.inl a.1) (.inr a.2)) at hp simp only [coefficient,hp,ite_false,zero_mul] noncomputable def integerCube (A : Instance I J) (B : ℝ) (f : FreeIndex A.small A.T A.b) : FreeCell A.small A.T → ℝ := fun a => B*(f a).val/((A.b a.val.1 a.val.2:ℝ)+1) /-- The scaled affine value at each original integer free assignment is exactly the manuscript's solved tree value divided by its capacity. -/ theorem scaledTree_integer (A : Instance I J) (B : ℝ) (hB : 0 < B) (x q : I → J → ℤ) (f : FreeIndex A.small A.T A.b) (t : TreeCell A.T) : scaledTree A B x q (integerCube A B f) t= (solved A.small A.T A.r A.c x q (fun i j => (freeTable A.small A.T A.b f i j:ℤ)) t.val.1 t.val.2:ℝ)/(A.b t.val.1 t.val.2:ℝ) := by let F : I×J → ℝ := fun a => (freeTable A.small A.T A.b f a.1 a.2:ℝ) have hterm (a : FreeCell A.small A.T) : normalizedCoefficient A B t a*integerCube A B f a= coefficient A t a.val*F a.val/(A.b t.val.1 t.val.2:ℝ) := by have hN : (A.b a.val.1 a.val.2:ℝ)+1≠0 := by positivity have hbt : (A.b t.val.1 t.val.2:ℝ)≠0 := by exact_mod_cast (Nat.ne_of_gt (A.hlarge _ _ (A.hTG t.property)).2) have hF : F a.val=(f a).val := by simp [F,freeTable,a.property] rw [hF] unfold normalizedCoefficient integerCube field_simp [hB.ne',hN,hbt] unfold scaledTree AffinePenalty.affineValue normalizedOffset simp_rw [hterm] rw [← Finset.sum_div,free_coefficient_sum,← add_div] have hh := solved_eq_affine A t x q (fun i j => (freeTable A.small A.T A.b f i j:ℤ)) unfold affineTree AffinePenalty.affineValue at hh apply congrArg (fun z : ℝ => z/(A.b t.val.1 t.val.2:ℝ)) simpa only [F,Int.cast_natCast] using hh.symm lemma intervalCost_div (x b : ℝ) (hb : 0 < b) : intervalCost (x/b)=max 0 (max (-x) (x-b))/b := by unfold intervalCost have he : x/b-1=(x-b)/b := by field_simp [hb.ne'] rw [he,← neg_div, max_div_div_right hb.le] simpa only [zero_div] using max_div_div_right hb.le (0:ℝ) (max (-x) (x-b)) /-- The root residual in the continuous display is the original root residual on every integer free assignment; it is independent of those free values by the proved component-cut cancellation. -/ theorem rootConstant_eq_assignment (A : Instance I J) (x q free : I → J → ℤ) : rootConstant A x q= ∑ a,if ForestPenaltyApplication.rootFlag A.T a then |(signedResidual A.small A.T A.r A.c x q free a:ℝ)| else 0 := by have hroot := root_eq_large A.small A.b A.T A.hTG A.hT A.hmax (fun i j hs => (A.hlarge i j hs).2) have hh := signedResidual_independent A.small A.T A.hTG A.r A.c x q free 0 A.X A.hXr A.hXc hroot unfold rootConstant apply Finset.sum_congr rfl intro a _ rw [hh] lemma tree_sum_eq (A : Instance I J) (F : I×J → ℝ) : (∑ t : TreeCell A.T,F t.val)= ∑ i,∑ j,if A.T.Adj (.inl i) (.inr j) then F (i,j) else 0 := by let p : I×J → Prop := fun a => A.T.Adj (.inl a.1) (.inr a.2) calc _ = ∑ a ∈ Finset.univ.filter p,F a := (Finset.sum_subtype (p := p) (Finset.univ.filter p) (fun _ => by simp) F).symm _ = ∑ a : I×J,if p a then F a else 0 := Finset.sum_filter _ _ _ = _ := by rw [Fintype.sum_prod_type] /-- Thus the continuous display agrees term for term with the actual root/tree penalty in the original finite definition of h_j. -/ theorem penalty_at_integer (A : Instance I J) (C B : ℝ) (hB : 0 < B) (z : View A → ℕ) (f : FreeIndex A.small A.T A.b) : penalty A C B (fun i j => (ForestCountIdentification.smallX A.small z i j:ℤ)) (fun i j => ((A.b i j-ForestCountIdentification.smallY A.small z i j:ℕ):ℤ)) (integerCube A B f)=C*ForestPenaltyBound.penalty A z f := by let x := ForestCountIdentification.smallX A.small z let y := ForestCountIdentification.smallY A.small z let free := freeTable A.small A.T A.b f let tree := solved A.small A.T A.r A.c (fun i j => (x i j:ℤ)) (fun i j => ((A.b i j-y i j:ℕ):ℤ)) (fun i j => (free i j:ℤ)) have hroot : rootConstant A (fun i j => (x i j:ℤ)) (fun i j => ((A.b i j-y i j:ℕ):ℤ))= ∑ a,if ForestPenaltyApplication.rootFlag A.T a then |ForestLiteralCount.actualDeviation A.small A.T A.b x y free A.r A.c tree a| else 0 := by rw [rootConstant_eq_assignment A _ _ (fun i j => (free i j:ℤ))] apply Finset.sum_congr rfl intro a _ rw [ForestRootPenalty.abs_actualDeviation_solved] have htree : (∑ t : TreeCell A.T,intervalCost (scaledTree A B (fun i j => (x i j:ℤ)) (fun i j => ((A.b i j-y i j:ℕ):ℤ)) (integerCube A B f) t))= ∑ i,∑ j,if A.T.Adj (.inl i) (.inr j) then max 0 (max (-(tree i j:ℝ)) ((tree i j:ℝ)-A.b i j))/(A.b i j:ℝ) else 0 := by calc _ = ∑ t : TreeCell A.T, max 0 (max (-(tree t.val.1 t.val.2:ℝ)) ((tree t.val.1 t.val.2:ℝ)-A.b t.val.1 t.val.2))/ (A.b t.val.1 t.val.2:ℝ) := by apply Finset.sum_congr rfl intro t _ rw [scaledTree_integer A B hB] exact intervalCost_div _ _ (by exact_mod_cast (A.hlarge _ _ (A.hTG t.property)).2) _ = _ := tree_sum_eq A (fun a => max 0 (max (-(tree a.1 a.2:ℝ)) ((tree a.1 a.2:ℝ)-A.b a.1 a.2))/(A.b a.1 a.2:ℝ)) unfold penalty change C*(rootConstant A (fun i j => (x i j:ℤ)) (fun i j => ((A.b i j-y i j:ℕ):ℤ))+_)=_ rw [hroot,htree] rfl end ContingencyTables.ForestScaledPenalty end OAI