introvoyz045's picture
Migrated from GitHub
4a56cd6 verified
Raw History Blame Contribute Delete
11.5 kB
import Mathlib
import OAI.LinearAlgebra.MatrixFields.Histories.InheritedMaskMargins
namespace OAI
namespace MatrixAllFields
open scoped BigOperators Topology Polynomial
section
noncomputable section
namespace MatrixMultiplication.Subdivision
open MatrixMultiplication.Foundation InheritedMasks
open scoped BigOperators
attribute [local instance] Classical.propDecidable
variable {I : Type*} [Fintype I] [DecidableEq I]
(P : I β†’ Type*) [βˆ€ i, Fintype (P i)] [βˆ€ i, DecidableEq (P i)]
def concatenate {X : Type*} (w : βˆ€ i, P i β†’ X) : (Ξ£ i, P i) β†’ X :=
fun p => w p.1 p.2
def concatenateEquiv (X : Type*) : (βˆ€ i, P i β†’ X) ≃ ((Ξ£ i, P i) β†’ X) where
toFun := concatenate P
invFun := fun w i p => w ⟨i, p⟩
left_inv := by intro w; rfl
right_inv := by intro w; rfl
section Windows
variable {A : Type*} [Fintype A] [DecidableEq A]
omit [DecidableEq I] [βˆ€ index, DecidableEq (P index)] [Fintype A] in
theorem wordPopulation_concatenate (w : βˆ€ i, P i β†’ A) (a : A) :
wordPopulation (concatenate P w) a = βˆ‘ i, wordPopulation (w i) a := by
let e : {p : (Ξ£ i, P i) // concatenate P w p = a} ≃
(Ξ£ i, {p : P i // w i p = a}) :=
{ toFun := fun p => ⟨p.val.1, ⟨p.val.2, p.property⟩⟩
invFun := fun p => ⟨⟨p.1, p.2.val⟩, p.2.property⟩
left_inv := by intro p; rfl
right_inv := by intro p; rfl }
exact (Fintype.card_congr e).trans Fintype.card_sigma
omit [DecidableEq I] [βˆ€ index, DecidableEq (P index)] [Fintype A] in
theorem empiricalLaw_concatenate (w : βˆ€ i, P i β†’ A) (a : A) :
empiricalLaw (concatenate P w) a =
βˆ‘ i, ((Fintype.card (P i) : ℝ) / Fintype.card (Ξ£ i, P i)) *
empiricalLaw (w i) a := by
rw [empiricalLaw, wordPopulation_concatenate, Nat.cast_sum, Finset.sum_div]
apply Finset.sum_congr rfl
intro i _
by_cases hi : Fintype.card (P i) = 0
Β· have hp : wordPopulation (w i) a = 0 := by
have hle := Fintype.card_subtype_le (fun p : P i => w i p = a)
change wordPopulation (w i) a ≀ Fintype.card (P i) at hle
omega
simp [hi, hp]
Β· have hi' : (Fintype.card (P i) : ℝ) β‰  0 := Nat.cast_ne_zero.mpr hi
simp only [empiricalLaw, div_eq_mul_inv]
calc
(wordPopulation (w i) a : ℝ) * (Fintype.card (Ξ£ i, P i) : ℝ)⁻¹ =
(wordPopulation (w i) a : ℝ) * (Fintype.card (Ξ£ i, P i) : ℝ)⁻¹ *
((Fintype.card (P i) : ℝ) * (Fintype.card (P i) : ℝ)⁻¹) := by
rw [mul_inv_cancelβ‚€ hi', mul_one]
_ = (Fintype.card (P i) : ℝ) * (Fintype.card (Ξ£ i, P i) : ℝ)⁻¹ *
((wordPopulation (w i) a : ℝ) * (Fintype.card (P i) : ℝ)⁻¹) := by ring
def partWindows (Ξ½ : A β†’ ℝ) (Ξ· : I β†’ ℝ) (w : βˆ€ i, P i β†’ A) : Prop :=
βˆ€ i, 0 < Fintype.card (P i) β†’ typeWindow Ξ½ (Ξ· i) (w i)
omit [DecidableEq I] [βˆ€ index, DecidableEq (P index)] [Fintype A] in
theorem typeWindow_concatenate (Ξ½ : A β†’ ℝ) (Ξ· : ℝ) (Ξ·part : I β†’ ℝ)
(hΞ· : 0 ≀ Ξ·) (hwidth : βˆ€ i, Ξ·part i ≀ Ξ·)
(hsize : 0 < Fintype.card (Ξ£ i, P i)) (w : βˆ€ i, P i β†’ A)
(hw : partWindows P Ξ½ Ξ·part w) : typeWindow Ξ½ Ξ· (concatenate P w) := by
intro a
let mass : I β†’ ℝ := fun i => (Fintype.card (P i) : ℝ) / Fintype.card (Ξ£ i, P i)
let law : I β†’ ℝ := fun i =>
if Fintype.card (P i) = 0 then Ξ½ a else empiricalLaw (w i) a
have hm : βˆ€ i, 0 ≀ mass i := fun i => div_nonneg (Nat.cast_nonneg _) (Nat.cast_nonneg _)
have hsum : βˆ‘ i, mass i = 1 := by
dsimp [mass]
rw [← Finset.sum_div, ← Nat.cast_sum, ← Fintype.card_sigma]
exact div_self (by exact_mod_cast (Nat.ne_of_gt hsize))
have hl : βˆ€ i, |law i - Ξ½ a| ≀ Ξ· := by
intro i
by_cases hi : Fintype.card (P i) = 0
Β· simpa [law, hi] using hΞ·
Β· simpa only [law, ite_eq_right hi] using
(hw i (Nat.pos_of_ne_zero hi) a).trans (hwidth i)
have heq : empiricalLaw (concatenate P w) a = βˆ‘ i, mass i * law i := by
rw [empiricalLaw_concatenate]
apply Finset.sum_congr rfl
intro i _
by_cases hi : Fintype.card (P i) = 0 <;> simp [mass, law, hi]
rw [heq]
exact weighted_window mass law (Ξ½ a) Ξ· hm hsum hl
omit [DecidableEq I] [βˆ€ index, DecidableEq (P index)] [Fintype A] in
theorem statisticWindow_concatenate {X : Type*} (statistic : X β†’ A)
(Ξ½ : A β†’ ℝ) (Ξ· : ℝ) (Ξ·part : I β†’ ℝ)
(hΞ· : 0 ≀ Ξ·) (hwidth : βˆ€ i, Ξ·part i ≀ Ξ·)
(hsize : 0 < Fintype.card (Ξ£ i, P i)) (w : βˆ€ i, P i β†’ X)
(hw : partWindows P ν ηpart (fun i => statistic ∘ w i)) :
typeWindow ν η (statistic ∘ concatenate P w) := by
exact typeWindow_concatenate P Ξ½ Ξ· Ξ·part hΞ· hwidth hsize
(fun index => statistic ∘ w index) hw
end Windows
def positionsOfCounts (count : I β†’ β„•) (n : β„•) (hsum : βˆ‘ i, count i = n) :
Fin n ≃ (Ξ£ i, Fin (count i)) :=
Fintype.equivOfCardEq (by simp only [Fintype.card_fin, Fintype.card_sigma, hsum])
theorem typeWindow_reindex {A Q R : Type*}
[Fintype A] [DecidableEq A] [Fintype Q] [DecidableEq Q]
[Fintype R] [DecidableEq R]
(Ξ½ : A β†’ ℝ) (Ξ· : ℝ) (w : R β†’ A) (e : Q ≃ R) :
typeWindow Ξ½ Ξ· (w ∘ e) ↔ typeWindow Ξ½ Ξ· w := by
simp only [typeWindow, empiricalLaw, wordPopulation_reindex, Fintype.card_congr e]
omit [DecidableEq I] [βˆ€ index, DecidableEq (P index)] in
theorem statisticWindow_concatenate_at {A X Q : Type*}
[Fintype A] [DecidableEq A] [Fintype Q] [DecidableEq Q]
(e : Q ≃ (Ξ£ i, P i)) (statistic : X β†’ A)
(Ξ½ : A β†’ ℝ) (Ξ· : ℝ) (Ξ·part : I β†’ ℝ)
(hΞ· : 0 ≀ Ξ·) (hwidth : βˆ€ i, Ξ·part i ≀ Ξ·)
(hsize : 0 < Fintype.card (Ξ£ i, P i)) (w : βˆ€ i, P i β†’ X)
(hw : partWindows P ν ηpart (fun i => statistic ∘ w i)) :
typeWindow ν η (statistic ∘ (concatenate P w ∘ e)) := by
change typeWindow ν η ((statistic ∘ concatenate P w) ∘ e)
exact (typeWindow_reindex Ξ½ Ξ· _ e).mpr
(statisticWindow_concatenate P statistic Ξ½ Ξ· Ξ·part hΞ· hwidth hsize w hw)
section Coefficients
variable {F X Y Z : Type*} [CommSemiring F]
[Fintype X] [Fintype Y] [Fintype Z]
def repeated (Q : Type*) [Fintype Q] (T : Tensor F X Y Z) :
Tensor F (Q β†’ X) (Q β†’ Y) (Q β†’ Z) := fun x y z => ∏ p, T (x p) (y p) (z p)
def allParts {U : Type*} (keep : βˆ€ i, (P i β†’ U) β†’ Prop) (w : βˆ€ i, P i β†’ U) : Prop :=
βˆ€ i, keep i (w i)
def subdivisionMatrix {U : Type*} (keep : βˆ€ i, (P i β†’ U) β†’ Prop) :
(βˆ€ i, P i β†’ U) β†’ ((Ξ£ i, P i) β†’ U) β†’ F :=
fun w v => if v = concatenate P w ∧ allParts P keep w then 1 else 0
theorem subdivisionMatrix_apply (T : Tensor F ((Ξ£ i, P i) β†’ X)
((Ξ£ i, P i) β†’ Y) ((Ξ£ i, P i) β†’ Z))
(keepX : βˆ€ i, (P i β†’ X) β†’ Prop) (keepY : βˆ€ i, (P i β†’ Y) β†’ Prop)
(keepZ : βˆ€ i, (P i β†’ Z) β†’ Prop)
(x : βˆ€ i, P i β†’ X) (y : βˆ€ i, P i β†’ Y) (z : βˆ€ i, P i β†’ Z) :
Tensor.restrict (subdivisionMatrix P keepX) (subdivisionMatrix P keepY)
(subdivisionMatrix P keepZ) T x y z =
if allParts P keepX x ∧ allParts P keepY y ∧ allParts P keepZ z then
T (concatenate P x) (concatenate P y) (concatenate P z) else 0 := by
by_cases hx : allParts P keepX x <;>
by_cases hy : allParts P keepY y <;>
by_cases hz : allParts P keepZ z <;>
simp [Tensor.restrict, subdivisionMatrix, hx, hy, hz, ite_mul, mul_ite]
theorem restrict_received (T : Tensor F X Y Z)
(receivedX : ((Ξ£ i, P i) β†’ X) β†’ Prop)
(receivedY : ((Ξ£ i, P i) β†’ Y) β†’ Prop)
(receivedZ : ((Ξ£ i, P i) β†’ Z) β†’ Prop)
(keepX : βˆ€ i, (P i β†’ X) β†’ Prop) (keepY : βˆ€ i, (P i β†’ Y) β†’ Prop)
(keepZ : βˆ€ i, (P i β†’ Z) β†’ Prop)
(hx : βˆ€ x, allParts P keepX x β†’ receivedX (concatenate P x))
(hy : βˆ€ y, allParts P keepY y β†’ receivedY (concatenate P y))
(hz : βˆ€ z, allParts P keepZ z β†’ receivedZ (concatenate P z)) :
Tensor.restrict (subdivisionMatrix P keepX) (subdivisionMatrix P keepY)
(subdivisionMatrix P keepZ)
(ExactRecovery.delete (repeated (Ξ£ i, P i) T) receivedX receivedY receivedZ) =
fun x y z => ∏ i, ExactRecovery.delete (repeated (P i) T)
(keepX i) (keepY i) (keepZ i) (x i) (y i) (z i) := by
funext x y z
rw [subdivisionMatrix_apply]
simp only [ExactRecovery.delete, Fintype.prod_ite_zero, forall_and]
by_cases h : allParts P keepX x ∧ allParts P keepY y ∧ allParts P keepZ z
Β· have hX := hx x h.1
have hY := hy y h.2.1
have hZ := hz z h.2.2
have hp : (βˆ€ i, keepX i (x i)) ∧ (βˆ€ i, keepY i (y i)) ∧
(βˆ€ i, keepZ i (z i)) := h
simp only [ite_eq_left h, ite_eq_left hp, hX, hY, hZ, and_self, ite_true]
simp only [repeated, concatenate, Fintype.prod_sigma]
Β· simp only [allParts] at h
simp [allParts, h]
def subdivisionMatrixAt {U Q : Type*} (e : Q ≃ (Ξ£ i, P i))
(keep : βˆ€ i, (P i β†’ U) β†’ Prop) :
(βˆ€ i, P i β†’ U) β†’ (Q β†’ U) β†’ F :=
fun w v => if v = concatenate P w ∘ e ∧ allParts P keep w then 1 else 0
omit [DecidableEq I] [βˆ€ index, DecidableEq (P index)] in
theorem restrict_received_at {Q : Type*} [Fintype Q] [DecidableEq Q]
(e : Q ≃ (Ξ£ i, P i)) (T : Tensor F X Y Z)
(receivedX : (Q β†’ X) β†’ Prop) (receivedY : (Q β†’ Y) β†’ Prop)
(receivedZ : (Q β†’ Z) β†’ Prop)
(keepX : βˆ€ i, (P i β†’ X) β†’ Prop) (keepY : βˆ€ i, (P i β†’ Y) β†’ Prop)
(keepZ : βˆ€ i, (P i β†’ Z) β†’ Prop)
(hx : βˆ€ x, allParts P keepX x β†’ receivedX (concatenate P x ∘ e))
(hy : βˆ€ y, allParts P keepY y β†’ receivedY (concatenate P y ∘ e))
(hz : βˆ€ z, allParts P keepZ z β†’ receivedZ (concatenate P z ∘ e)) :
Tensor.restrict (subdivisionMatrixAt P e keepX) (subdivisionMatrixAt P e keepY)
(subdivisionMatrixAt P e keepZ)
(ExactRecovery.delete (repeated Q T) receivedX receivedY receivedZ) =
fun x y z => ∏ i, ExactRecovery.delete (repeated (P i) T)
(keepX i) (keepY i) (keepZ i) (x i) (y i) (z i) := by
funext x y z
have hcollapse : Tensor.restrict (subdivisionMatrixAt P e keepX)
(subdivisionMatrixAt P e keepY) (subdivisionMatrixAt P e keepZ)
(ExactRecovery.delete (repeated Q T) receivedX receivedY receivedZ) x y z =
if allParts P keepX x ∧ allParts P keepY y ∧ allParts P keepZ z then
ExactRecovery.delete (repeated Q T) receivedX receivedY receivedZ
(concatenate P x ∘ e) (concatenate P y ∘ e) (concatenate P z ∘ e) else 0 := by
by_cases hx : allParts P keepX x <;>
by_cases hy : allParts P keepY y <;>
by_cases hz : allParts P keepZ z <;>
simp [Tensor.restrict, subdivisionMatrixAt, hx, hy, hz, ite_mul, mul_ite]
rw [hcollapse]
simp only [ExactRecovery.delete, Fintype.prod_ite_zero, forall_and]
by_cases h : allParts P keepX x ∧ allParts P keepY y ∧ allParts P keepZ z
Β· have hX := hx x h.1
have hY := hy y h.2.1
have hZ := hz z h.2.2
have hp : (βˆ€ i, keepX i (x i)) ∧ (βˆ€ i, keepY i (y i)) ∧
(βˆ€ i, keepZ i (z i)) := h
simp only [ite_eq_left h, ite_eq_left hp, hX, hY, hZ, and_self, ite_true, repeated,
Function.comp_apply]
have hprod := Equiv.prod_comp e (fun p =>
T (concatenate P x p) (concatenate P y p) (concatenate P z p))
simpa only [concatenate, Fintype.prod_sigma] using hprod
Β· simp only [allParts] at h
simp [allParts, h]
end Coefficients
end MatrixMultiplication.Subdivision
end
end
end MatrixAllFields
end OAI