Download data/lean/OAI/LinearAlgebra/MatrixFields/Construction/ExactStatisticWords.lean from introvoyz045/math: direct link, hf CLI and curl.
- Browser
- Download file 12.8 kB
-
https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/LinearAlgebra/MatrixFields/Construction/ExactStatisticWords.lean
- Command line
-
hf download hf://datasets/introvoyz045/math/data/lean/OAI/LinearAlgebra/MatrixFields/Construction/ExactStatisticWords.lean
-
curl -L -o ExactStatisticWords.lean https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/LinearAlgebra/MatrixFields/Construction/ExactStatisticWords.lean
12.8 kB
| import Mathlib | |
| import OAI.LinearAlgebra.MatrixFields.Histories.InheritedMaskMargins | |
| import OAI.LinearAlgebra.MatrixFields.Tensors.TerminalRelabel | |
| namespace OAI | |
| namespace MatrixAllFields | |
| open scoped BigOperators Topology Polynomial | |
| section | |
| namespace MatrixMultiplication.LeafVolumes | |
| open MatrixMultiplication.Foundation | |
| open scoped BigOperators | |
| variable {F U : Type*} [CommSemiring F] [DecidableEq U] | |
| def zeroXMatching : Tensor F Unit U U := fun _ y z => if y = z then 1 else 0 | |
| def zeroYMatching : Tensor F U Unit U := fun x _ z => if x = z then 1 else 0 | |
| def zeroZMatching : Tensor F U U Unit := fun x y _ => if x = y then 1 else 0 | |
| theorem zeroXMatching_matrixCoefficients : | |
| Tensor.pullback (fun _ : Unit Γ Unit => ()) | |
| (fun y : Unit Γ U => y.2) (fun z : U Γ Unit => z.1) | |
| (zeroXMatching (F := F) (U := U)) = | |
| Tensor.matrixCoefficients Unit Unit U := by | |
| funext x y z | |
| simp [Tensor.pullback, zeroXMatching, Tensor.matrixCoefficients] | |
| theorem zeroYMatching_matrixCoefficients : | |
| Tensor.pullback (fun x : U Γ Unit => x.1) | |
| (fun _ : Unit Γ Unit => ()) (fun z : Unit Γ U => z.2) | |
| (zeroYMatching (F := F) (U := U)) = | |
| Tensor.matrixCoefficients U Unit Unit := by | |
| funext x y z | |
| simp [Tensor.pullback, zeroYMatching, Tensor.matrixCoefficients, eq_comm] | |
| theorem zeroZMatching_matrixCoefficients : | |
| Tensor.pullback (fun x : Unit Γ U => x.2) | |
| (fun y : U Γ Unit => y.1) (fun _ : Unit Γ Unit => ()) | |
| (zeroZMatching (F := F) (U := U)) = | |
| Tensor.matrixCoefficients Unit U Unit := by | |
| funext x y z | |
| simp [Tensor.pullback, zeroZMatching, Tensor.matrixCoefficients] | |
| def complementaryMatching {V : Type*} [DecidableEq V] (e : U β V) : | |
| Tensor F Unit U V := fun _ y z => if e y = z then 1 else 0 | |
| theorem complementaryMatching_pullback {V : Type*} [DecidableEq V] (e : U β V) : | |
| Tensor.pullback id id e (complementaryMatching (F := F) e) = | |
| zeroXMatching (F := F) (U := U) := by | |
| funext x y z | |
| simp [Tensor.pullback, complementaryMatching, zeroXMatching] | |
| theorem zeroXMatching_subtype (p : U β Prop) [DecidablePred p] : | |
| Tensor.pullback id (Subtype.val : {u // p u} β U) Subtype.val | |
| (zeroXMatching (F := F) (U := U)) = | |
| zeroXMatching (F := F) (U := {u // p u}) := by | |
| funext x y z | |
| simp [Tensor.pullback, zeroXMatching, Subtype.ext_iff] | |
| theorem matchingPower_leaf {X Y Z : Type*} [DecidableEq Y] | |
| (T : Tensor F X Y Z) (xβ : X) (e : Y β Z) | |
| (hmatch : β y z, T xβ y (e z) = if y = z then 1 else 0) | |
| (n : β) (p : (Fin n β Y) β Prop) [DecidablePred p] : | |
| Tensor.pullback (fun _ : Unit => fun _ : Fin n => xβ) | |
| (Subtype.val : {w // p w} β (Fin n β Y)) | |
| (fun w : {w // p w} => fun i => e (w.val i)) (Tensor.power T n) = | |
| zeroXMatching (F := F) (U := {w // p w}) := by | |
| funext x y z | |
| simp only [Tensor.pullback, Tensor.power, hmatch, zeroXMatching, | |
| Fintype.prod_boole, β funext_iff, Subtype.ext_iff] | |
| theorem matchingPower_matrixCoefficients {X Y Z : Type*} [DecidableEq Y] | |
| (T : Tensor F X Y Z) (xβ : X) (e : Y β Z) | |
| (hmatch : β y z, T xβ y (e z) = if y = z then 1 else 0) | |
| (n : β) (p : (Fin n β Y) β Prop) [DecidablePred p] : | |
| Tensor.pullback (fun _ : Unit Γ Unit => fun _ : Fin n => xβ) | |
| (fun y : Unit Γ {w // p w} => y.2.val) | |
| (fun z : {w // p w} Γ Unit => fun i => e (z.1.val i)) | |
| (Tensor.power T n) = | |
| Tensor.matrixCoefficients Unit Unit {w // p w} := by | |
| have h := matchingPower_leaf T xβ e hmatch n p | |
| have hp := congrArg (Tensor.pullback (fun _ : Unit Γ Unit => ()) | |
| (fun y : Unit Γ {w // p w} => y.2) | |
| (fun z : {w // p w} Γ Unit => z.1)) h | |
| rw [zeroXMatching_matrixCoefficients] at hp | |
| exact hp | |
| theorem rankAtMost_matchingPower_leaf {X Y Z : Type*} [DecidableEq Y] | |
| (T : Tensor F X Y Z) (xβ : X) (e : Y β Z) | |
| (hmatch : β y z, T xβ y (e z) = if y = z then 1 else 0) | |
| (n : β) (p : (Fin n β Y) β Prop) [DecidablePred p] | |
| {r : β} (hT : Tensor.RankAtMost (Tensor.power T n) r) : | |
| Tensor.RankAtMost (Tensor.matrixCoefficients (K := F) Unit Unit {w // p w}) r := by | |
| rw [β matchingPower_matrixCoefficients T xβ e hmatch n p] | |
| exact hT.pullback _ _ _ | |
| section ExactLabeledWords | |
| variable {A : Type*} [Fintype A] [DecidableEq A] | |
| abbrev LabeledExactWords (counts b : A β β) := | |
| Ξ£ w : ExactWords counts, β i : Fin (β a, counts a), Fin (b (w.val i)) | |
| theorem labelings_card {I : Type*} [Fintype I] [DecidableEq I] | |
| (w : I β A) (b : A β β) : | |
| Fintype.card (β i, Fin (b (w i))) = | |
| β a, b a ^ wordPopulation w a := by | |
| classical | |
| simp only [Fintype.card_pi, Fintype.card_fin] | |
| calc | |
| (β i, b (w i)) = β s : Ξ£ a, {i // w i = a}, b s.1 := by | |
| rw [β (Equiv.sigmaFiberEquiv w).prod_comp (fun i => b (w i))] | |
| apply Finset.prod_congr rfl | |
| intro s _ | |
| exact congrArg b s.2.property | |
| _ = β a, b a ^ wordPopulation w a := by | |
| simp [Fintype.prod_sigma, wordPopulation] | |
| theorem labeledExactWords_card (counts b : A β β) : | |
| Fintype.card (LabeledExactWords counts b) = | |
| Nat.multinomial Finset.univ counts * β a, b a ^ counts a := by | |
| classical | |
| rw [Fintype.card_sigma] | |
| have hcard : β w : ExactWords counts, | |
| Fintype.card (β i : Fin (β a, counts a), Fin (b (w.val i))) = | |
| β a, b a ^ counts a := by | |
| intro w | |
| rw [labelings_card] | |
| simp only [w.property] | |
| simp only [hcard, Finset.sum_const, Finset.card_univ, smul_eq_mul, | |
| exactWords_card] | |
| theorem labeledExactWords_card_pos (counts b : A β β) | |
| (hb : β a, counts a β 0 β 0 < b a) : | |
| 0 < Fintype.card (LabeledExactWords counts b) := by | |
| classical | |
| rw [labeledExactWords_card] | |
| apply Nat.mul_pos (Nat.multinomial_pos Finset.univ counts) | |
| apply Finset.prod_pos | |
| intro a _ | |
| by_cases h : counts a = 0 | |
| Β· simp [h] | |
| Β· exact pow_pos (hb a h) _ | |
| end ExactLabeledWords | |
| structure Dimensions where | |
| rows : β | |
| inner : β | |
| cols : β | |
| deriving DecidableEq | |
| def Dimensions.volume (d : Dimensions) : β := d.rows * d.inner * d.cols | |
| def Dimensions.product (d e : Dimensions) : Dimensions := | |
| β¨d.rows * e.rows, d.inner * e.inner, d.cols * e.colsβ© | |
| theorem Dimensions.volume_product (d e : Dimensions) : | |
| (d.product e).volume = d.volume * e.volume := by | |
| simp only [Dimensions.product, Dimensions.volume] | |
| ring | |
| def historyDimensions {C : Type*} [Fintype C] (d : C β Dimensions) : Dimensions := | |
| β¨β c, (d c).rows, β c, (d c).inner, β c, (d c).colsβ© | |
| theorem historyDimensions_volume {C : Type*} [Fintype C] (d : C β Dimensions) : | |
| (historyDimensions d).volume = β c, (d c).volume := by | |
| simp [historyDimensions, Dimensions.volume, Finset.prod_mul_distrib] | |
| end MatrixMultiplication.LeafVolumes | |
| noncomputable section | |
| namespace MatrixMultiplication.ExactStatisticWords | |
| open MatrixMultiplication.Foundation | |
| open scoped BigOperators | |
| variable {A U : Type*} [Fintype A] [DecidableEq A] [Fintype U] [DecidableEq U] | |
| abbrev Words (statistic : U β A) (counts : A β β) := | |
| {w : Fin (β a, counts a) β U // | |
| β a, wordPopulation (statistic β w) a = counts a} | |
| def statisticWord (statistic : U β A) (counts : A β β) | |
| (w : Words statistic counts) : ExactWords counts := | |
| β¨statistic β w.val, w.propertyβ© | |
| omit [Fintype U] [DecidableEq U] in | |
| theorem empiricalLaw_eq (statistic : U β A) (counts : A β β) | |
| (w : Words statistic counts) : | |
| InheritedMasks.empiricalLaw (statistic β w.val) = | |
| fun a => (counts a : β) / (β a, counts a : β) := by | |
| funext a | |
| simp only [InheritedMasks.empiricalLaw, w.property, Fintype.card_fin] | |
| omit [Fintype U] [DecidableEq U] in | |
| theorem typeWindow_of_counts (statistic : U β A) (counts : A β β) | |
| (w : Words statistic counts) (Ξ½ : A β β) (Ξ· : β) | |
| (happrox : β a, |(counts a : β) / (β a, counts a : β) - Ξ½ a| β€ Ξ·) : | |
| InheritedMasks.typeWindow Ξ½ Ξ· (statistic β w.val) := by | |
| intro a | |
| rw [empiricalLaw_eq] | |
| exact happrox a | |
| omit [Fintype U] [DecidableEq U] in | |
| theorem sum_statistic_weight (statistic : U β A) (counts q : A β β) | |
| (w : Words statistic counts) : | |
| (β i, q (statistic (w.val i))) = β a, counts a * q a := by | |
| let e := Equiv.sigmaFiberEquiv (statistic β w.val) | |
| calc | |
| (β i, q (statistic (w.val i))) = | |
| β p : (Ξ£ a, {i // (statistic β w.val) i = a}), q p.1 := by | |
| rw [β Equiv.sum_comp e (fun i => q (statistic (w.val i)))] | |
| apply Finset.sum_congr rfl | |
| intro p _ | |
| exact congrArg q p.2.property | |
| _ = β a, counts a * q a := by | |
| rw [Fintype.sum_sigma] | |
| apply Finset.sum_congr rfl | |
| intro a _ | |
| simp only [Finset.sum_const, Finset.card_univ, smul_eq_mul] | |
| change wordPopulation (statistic β w.val) a * q a = counts a * q a | |
| rw [w.property] | |
| def fiberEquiv (statistic : U β A) (counts : A β β) (w : ExactWords counts) : | |
| {v : Words statistic counts // statisticWord statistic counts v = w} β | |
| {v : Fin (β a, counts a) β U // β i, statistic (v i) = w.val i} where | |
| toFun v := β¨v.val.val, fun i => congrFun (congrArg Subtype.val v.property) iβ© | |
| invFun v := by | |
| have heq : statistic β v.val = w.val := funext v.property | |
| let v' : Words statistic counts := β¨v.val, fun a => by | |
| rw [heq] | |
| exact w.property aβ© | |
| exact β¨v', Subtype.ext heqβ© | |
| left_inv v := by | |
| apply Subtype.ext | |
| apply Subtype.ext | |
| rfl | |
| right_inv v := by | |
| apply Subtype.ext | |
| rfl | |
| omit [DecidableEq U] in | |
| theorem card (statistic : U β A) (counts b : A β β) | |
| (hb : β a, Fintype.card {u : U // statistic u = a} = b a) : | |
| Fintype.card (Words statistic counts) = | |
| Nat.multinomial Finset.univ counts * β a, b a ^ counts a := by | |
| classical | |
| have hfiber (w : ExactWords counts) : | |
| Fintype.card {v : Words statistic counts // statisticWord statistic counts v = w} = | |
| β a, b a ^ counts a := by | |
| let e : {v : Fin (β a, counts a) β U // β i, statistic (v i) = w.val i} β | |
| (β i : Fin (β a, counts a), {u : U // statistic u = w.val i}) := | |
| { toFun := fun v i => β¨v.val i, v.property iβ© | |
| invFun := fun v => β¨fun i => (v i).val, fun i => (v i).propertyβ© | |
| left_inv := by intro v; rfl | |
| right_inv := by intro v; rfl } | |
| rw [Fintype.card_congr (fiberEquiv statistic counts w), | |
| Fintype.card_congr e, Fintype.card_pi] | |
| simp only [hb] | |
| have h := LeafVolumes.labelings_card w.val b | |
| simpa only [Fintype.card_pi, Fintype.card_fin, w.property] using h | |
| calc | |
| Fintype.card (Words statistic counts) = | |
| Fintype.card (Ξ£ w : ExactWords counts, | |
| {v : Words statistic counts // statisticWord statistic counts v = w}) := | |
| (Fintype.card_congr (Equiv.sigmaFiberEquiv (statisticWord statistic counts))).symm | |
| _ = Nat.multinomial Finset.univ counts * β a, b a ^ counts a := by | |
| simp only [Fintype.card_sigma, hfiber, Finset.sum_const, Finset.card_univ, | |
| smul_eq_mul, exactWords_card] | |
| omit [DecidableEq U] in | |
| theorem card_of_supported (statistic : U β A) (counts b : A β β) | |
| (hb : β a, 0 < counts a β Fintype.card {u : U // statistic u = a} = b a) : | |
| Fintype.card (Words statistic counts) = | |
| Nat.multinomial Finset.univ counts * β a, b a ^ counts a := by | |
| rw [card statistic counts (fun a => Fintype.card {u : U // statistic u = a}) | |
| (fun _ => rfl)] | |
| congr 1 | |
| apply Finset.prod_congr rfl | |
| intro a _ | |
| by_cases ha : counts a = 0 | |
| Β· simp [ha] | |
| Β· rw [hb a (Nat.pos_of_ne_zero ha)] | |
| omit [Fintype A] [DecidableEq U] in | |
| theorem card_fixedWeight_fiber (statistic : U β A) (weight : U β β) | |
| (statisticWeight : A β β) (hweight : β u, weight u = statisticWeight (statistic u)) | |
| (a : β) (s : A) (hs : statisticWeight s = a) : | |
| Fintype.card {u : {u : U // weight u = a} // statistic u.val = s} = | |
| Fintype.card {u : U // statistic u = s} := by | |
| apply Fintype.card_congr | |
| exact | |
| { toFun := fun u => β¨u.val.val, u.propertyβ© | |
| invFun := fun u => β¨β¨u.val, by rw [hweight, u.property, hs]β©, u.propertyβ© | |
| left_inv := by intro u; rfl | |
| right_inv := by intro u; rfl } | |
| omit [DecidableEq U] in | |
| theorem card_pos_of_supported (statistic : U β A) (counts b : A β β) | |
| (hb : β a, 0 < counts a β Fintype.card {u : U // statistic u = a} = b a) | |
| (hpos : β a, 0 < counts a β 0 < b a) : | |
| 0 < Fintype.card (Words statistic counts) := by | |
| rw [card_of_supported statistic counts b hb] | |
| apply Nat.mul_pos (Nat.multinomial_pos Finset.univ counts) | |
| apply Finset.prod_pos | |
| intro a _ | |
| by_cases ha : counts a = 0 | |
| Β· simp [ha] | |
| Β· exact pow_pos (hpos a (Nat.pos_of_ne_zero ha)) _ | |
| end MatrixMultiplication.ExactStatisticWords | |
| end | |
| end | |
| end MatrixAllFields | |
| end OAI | |