Download lean/OAI/Combinatorics/ContingencyTables/Rooted/RootedAffine.lean from roseking/openAI-math-dataset: direct link, hf CLI and curl.
- Browser
- Download file 7.29 kB
-
https://huggingface.co/datasets/roseking/openAI-math-dataset/resolve/main/lean/OAI/Combinatorics/ContingencyTables/Rooted/RootedAffine.lean
- Command line
-
hf download hf://datasets/roseking/openAI-math-dataset/lean/OAI/Combinatorics/ContingencyTables/Rooted/RootedAffine.lean
-
curl -L -o RootedAffine.lean https://huggingface.co/datasets/roseking/openAI-math-dataset/resolve/main/lean/OAI/Combinatorics/ContingencyTables/Rooted/RootedAffine.lean
7.29 kB
| import OAI.Combinatorics.ContingencyTables.Rooted.RootedCut | |
| import OAI.Combinatorics.ContingencyTables.Rooted.ForestAffineCoefficients | |
| namespace OAI | |
| /-! # Cut coefficients for an explicitly selected forest root | |
| The same signed cut calculation applies to the executed root selector. | |
| In particular, coefficient size and the maximum-forest capacity bound | |
| do not depend on a classical choice of cut representative. | |
| -/ | |
| namespace ContingencyTables.RootedAffine | |
| open scoped BigOperators Classical | |
| open CompanionFactorialApplication RootedCompletion RootedCut GraphCompletionCounts | |
| open ForestCapacity ForestCut MaximumForest | |
| variable {I J : Type*} [Fintype I] [Fintype J] | |
| noncomputable def coefficient (A : Instance I J) (S : Selection A.T) | |
| (t : TreeCell A.T) (a : I×J) : ℝ := | |
| if A.small a.1 a.2=false ∧ ¬A.T.Adj (.inl a.1) (.inr a.2) | |
| then -sign A.T S t a/sign A.T S t t.val else 0 | |
| lemma coefficient_abs_le_one (A : Instance I J) (S : Selection A.T) | |
| (t : TreeCell A.T) (a : I×J) : |coefficient A S t a|≤1 := by | |
| unfold coefficient | |
| split_ifs | |
| · rw [abs_div,abs_neg,sign_abs_self A.T S A.hT,div_one] | |
| exact sign_abs_le_one A.T S t a | |
| · norm_num | |
| lemma capacity_le_of_coefficient_ne_zero (A : Instance I J) (S : Selection A.T) | |
| (t : TreeCell A.T) (a : I×J) (ha : coefficient A S t a≠0) : | |
| A.b a.1 a.2≤A.b t.val.1 t.val.2 := by | |
| have hf : A.small a.1 a.2=false ∧ ¬A.T.Adj (.inl a.1) (.inr a.2) := by | |
| by_contra hn | |
| exact ha (by simp only [coefficient,hn,ite_false]) | |
| have hcross : treeCut A.T S t (.inl a.1)≠treeCut A.T S t (.inr a.2) := by | |
| intro he | |
| apply ha | |
| simp [coefficient,hf,sign,he] | |
| have h := crossing_free_edge_le (cellGraph A.small) A.T (capacity A.b) | |
| A.hTG A.hT A.hmax (s(Sum.inl t.val.1,Sum.inr t.val.2)) | |
| (by simpa using t.property) | |
| (away A.T (.inl t.val.1) (.inr t.val.2) (S.root (.inl t.val.1))) | |
| (show (cellGraph A.small).Adj (.inl a.1) (.inr a.2) from hf.1) hf.2 hcross | |
| simpa only [capacity_cell] using h | |
| theorem normalized_coefficient_bound (A : Instance I J) (S : Selection A.T) | |
| (t : TreeCell A.T) (a : I×J) (B : ℝ) (hB : 0<B) : | |
| |coefficient A S t a*((A.b a.1 a.2:ℝ)+1)/B/(A.b t.val.1 t.val.2:ℝ)|≤2/B := by | |
| have hb : (1:ℝ)≤A.b t.val.1 t.val.2 := by | |
| exact_mod_cast (A.hlarge _ _ (A.hTG t.property)).2 | |
| by_cases ha : coefficient A S t a=0 | |
| · simp only [ha,zero_mul,zero_div,abs_zero] | |
| positivity | |
| have hcap : (A.b a.1 a.2:ℝ)≤A.b t.val.1 t.val.2 := by | |
| exact_mod_cast capacity_le_of_coefficient_ne_zero A S t a ha | |
| rw [abs_div,abs_div,abs_mul,abs_of_pos hB, | |
| abs_of_pos (by linarith : (0:ℝ)<A.b t.val.1 t.val.2), | |
| abs_of_nonneg (by positivity : (0:ℝ)≤(A.b a.1 a.2:ℝ)+1)] | |
| apply (div_le_iff₀ (by linarith : (0:ℝ)<A.b t.val.1 t.val.2)).mpr | |
| rw [div_le_iff₀ hB] | |
| have hh := mul_le_mul_of_nonneg_right (coefficient_abs_le_one A S t a) | |
| (by positivity : (0:ℝ)≤(A.b a.1 a.2:ℝ)+1) | |
| field_simp [hB.ne'] | |
| nlinarith | |
| noncomputable def smallContribution (A : Instance I J) (S : Selection A.T) | |
| (t : TreeCell A.T) (x q : I → J → ℝ) : ℝ := | |
| ∑ a : I×J,if A.small a.1 a.2 then | |
| indicator (treeCut A.T S t (.inl a.1))*(x a.1 a.2-A.X a.1 a.2)- | |
| indicator (treeCut A.T S t (.inr a.2))*(q a.1 a.2-A.X a.1 a.2) else 0 | |
| noncomputable def offset (A : Instance I J) (S : Selection A.T) | |
| (t : TreeCell A.T) (x q : I → J → ℝ) : ℝ := | |
| A.X t.val.1 t.val.2-smallContribution A S t x q/sign A.T S t t.val- | |
| ∑ a : I×J,coefficient A S t a*(A.X a.1 a.2:ℝ) | |
| noncomputable def affineTree (A : Instance I J) (S : Selection A.T) | |
| (t : TreeCell A.T) (x q : I → J → ℝ) (z : I×J → ℝ) : ℝ := | |
| AffinePenalty.affineValue (offset A S t x q) (coefficient A S t) z | |
| theorem affine_of_nonroot_equations (A : Instance I J) (S : Selection A.T) | |
| (t : TreeCell A.T) (x q z : I → J → ℝ) | |
| (hr : ∀ i,Sum.inl i≠S.root (.inl i) → | |
| rowDifference A.small x z (fun i j => (A.X i j:ℝ)) i=0) | |
| (hc : ∀ j,Sum.inr j≠S.root (.inr j) → | |
| columnDifference A.small q z (fun i j => (A.X i j:ℝ)) j=0) : | |
| z t.val.1 t.val.2=affineTree A S t x q (fun a => z a.1 a.2) := by | |
| let f : I×J → ℝ := fun a => cutTerm A.small | |
| (fun i => treeCut A.T S t (.inl i)) (fun j => treeCut A.T S t (.inr j)) | |
| x q z (fun i j => (A.X i j:ℝ)) a.1 a.2 | |
| have hsum : (∑ a,f a)=0 := by | |
| rw [Fintype.sum_prod_type] | |
| exact cut_sum_zero A.small _ _ x q z _ | |
| (fun i hi => hr i (treeCut_nonroot A.T S A.hT t (.inl i) hi)) | |
| (fun j hj => hc j (treeCut_nonroot A.T S A.hT t (.inr j) hj)) | |
| have hterm (a : I×J) : f a= | |
| (if a=t.val then sign A.T S t t.val*(z a.1 a.2-A.X a.1 a.2) else 0)+ | |
| (if A.small a.1 a.2 then | |
| indicator (treeCut A.T S t (.inl a.1))*(x a.1 a.2-A.X a.1 a.2)- | |
| indicator (treeCut A.T S t (.inr a.2))*(q a.1 a.2-A.X a.1 a.2) else 0)- | |
| sign A.T S t t.val*coefficient A S t a*(z a.1 a.2-A.X a.1 a.2) := by | |
| by_cases he : a=t.val | |
| · subst a | |
| have hs : A.small t.val.1 t.val.2=false := A.hTG t.property | |
| simp only [f,cutTerm,hs,Bool.false_eq_true,ite_false,ite_true,coefficient,t.property, | |
| not_true_eq_false,and_false,sign,mul_zero,zero_mul,add_zero,sub_zero] | |
| · by_cases hs : A.small a.1 a.2=true | |
| · simp [f,cutTerm,hs,he,coefficient] | |
| · have hs' : A.small a.1 a.2=false := Bool.eq_false_iff.mpr hs | |
| by_cases ht : A.T.Adj (.inl a.1) (.inr a.2) | |
| · have hz := other_tree_sign_zero A.T S t a ht he | |
| simp only [f,cutTerm,hs',Bool.false_eq_true,ite_false,he,coefficient,ht, | |
| not_true_eq_false,and_false,zero_add,mul_zero,zero_mul,sub_zero] | |
| change sign A.T S t a*(z a.1 a.2-(A.X a.1 a.2:ℝ))=0 | |
| rw [hz,zero_mul] | |
| · simp only [f,cutTerm,hs',Bool.false_eq_true,ite_false,he,coefficient,ht, | |
| not_false_eq_true,and_self,ite_true,zero_add,zero_sub] | |
| change sign A.T S t a*(z a.1 a.2-(A.X a.1 a.2:ℝ))= | |
| -(sign A.T S t t.val*(-sign A.T S t a/sign A.T S t t.val)* | |
| (z a.1 a.2-A.X a.1 a.2)) | |
| field_simp [sign_self_ne_zero A.T S A.hT t] | |
| simp_rw [hterm] at hsum | |
| rw [Finset.sum_sub_distrib,Finset.sum_add_distrib] at hsum | |
| have hsingle : (∑ a : I×J, | |
| if a=t.val then sign A.T S t t.val*(z a.1 a.2-A.X a.1 a.2) else 0)= | |
| sign A.T S t t.val*(z t.val.1 t.val.2-A.X t.val.1 t.val.2) := by simp | |
| rw [hsingle] at hsum | |
| change sign A.T S t t.val*(z t.val.1 t.val.2-A.X t.val.1 t.val.2)+ | |
| smallContribution A S t x q- | |
| (∑ a : I×J,sign A.T S t t.val*coefficient A S t a*(z a.1 a.2-A.X a.1 a.2))=0 at hsum | |
| have hprod : (∑ a : I×J,sign A.T S t t.val*coefficient A S t a*(z a.1 a.2-A.X a.1 a.2))= | |
| sign A.T S t t.val*((∑ a : I×J,coefficient A S t a*z a.1 a.2)- | |
| ∑ a : I×J,coefficient A S t a*(A.X a.1 a.2:ℝ)) := by | |
| rw [mul_sub,Finset.mul_sum,Finset.mul_sum,←Finset.sum_sub_distrib] | |
| apply Finset.sum_congr rfl | |
| intro a _ | |
| ring | |
| rw [hprod] at hsum | |
| unfold affineTree AffinePenalty.affineValue offset | |
| apply mul_left_cancel₀ (sign_self_ne_zero A.T S A.hT t) | |
| rw [mul_add,mul_sub,mul_sub,←mul_div_assoc, | |
| mul_div_cancel_left₀ _ (sign_self_ne_zero A.T S A.hT t)] | |
| nlinarith [hsum] | |
| end ContingencyTables.RootedAffine | |
| end OAI | |