File size: 5,399 Bytes
f18e7de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import OAI.LinearAlgebra.MatrixMultiplication.JointExtraction.CoarseHashing
import OAI.LinearAlgebra.MatrixMultiplication.Separation.ComplexTypeCounting

namespace OAI

/-! Joint tensor extraction, compatibility and entropy estimates. -/

noncomputable section

namespace MatrixMultiplication.JointPopulation

open MatrixMultiplication.Foundation
open scoped BigOperators

attribute [local instance] Classical.propDecidable

structure HistoryKey where
  lot : ℕ
  initialShape : Fin 3 → ℕ
  physicalPlacement : Equiv.Perm (Fin 3)
  parentSplits : List ((Fin 3 → ℕ) × Bool)
  subdivisions : List ℕ

abbrev Shape := Fin 17 × Fin 17 × Fin 17

def shapeSide (s : Fin 3) (u : Shape) : Fin 17 :=
  if s = 0 then u.1 else if s = 1 then u.2.1 else u.2.2

variable {H : Type*} [Fintype H] [DecidableEq H]
    (counts : H → Shape → ℕ)

abbrev Positions (h : H) := Fin (∑ u, counts h u)
abbrev Target := ∀ h, ExactWords (counts h)

def canonicalTarget : Target counts :=
  fun h => Classical.choice (exactWords_nonempty (counts h))

theorem target_card : Fintype.card (Target counts) =
    ∏ h, Nat.multinomial Finset.univ (counts h) := by
  simp only [Target, Fintype.card_pi, exactWords_card]

omit [Fintype H] [DecidableEq H] in
theorem target_nonempty : Nonempty (Target counts) := ⟨canonicalTarget counts⟩

abbrev Position := Σ h, Positions counts h

def triple (e : Target counts) : JointCoarseHashing.Triple (Position counts) :=
  (fun p => (e p.1).val p.2 |>.1,
    fun p => (e p.1).val p.2 |>.2.1,
    fun p => (e p.1).val p.2 |>.2.2)

omit [Fintype H] [DecidableEq H] in
theorem triple_injective : Function.Injective (triple counts) := by
  intro e f h
  funext c
  apply Subtype.ext
  funext i
  apply Prod.ext
  · exact congrArg (fun t : JointCoarseHashing.Triple (Position counts) => t.1 ⟨c, i⟩) h
  · apply Prod.ext
    · exact congrArg (fun t : JointCoarseHashing.Triple (Position counts) => t.2.1 ⟨c, i⟩) h
    · exact congrArg (fun t : JointCoarseHashing.Triple (Position counts) => t.2.2 ⟨c, i⟩) h

def targetSet : Finset (JointCoarseHashing.Triple (Position counts)) :=
  Finset.univ.image (triple counts)

theorem targetSet_card : (targetSet counts).card =
    ∏ h, Nat.multinomial Finset.univ (counts h) := by
  rw [targetSet, Finset.card_image_of_injective _ (triple_injective counts)]
  simpa using target_card counts

abbrev Class (e : Target counts) (h : H) (u : Shape) :=
  {i : Positions counts h // (e h).val i = u}

omit [Fintype H] [DecidableEq H] in
theorem class_card (e : Target counts) (h : H) (u : Shape) :
    Fintype.card (Class counts e h u) = counts h u := (e h).property u

omit [Fintype H] [DecidableEq H] in
theorem symbol_count_pos (e : Target counts) (h : H) (i : Positions counts h) :
    0 < counts h ((e h).val i) := by
  rw [← class_card counts e h ((e h).val i)]
  exact Fintype.card_pos_iff.mpr ⟨⟨i, rfl⟩⟩

omit [Fintype H] [DecidableEq H] in
theorem triple_support (sum : H → ℕ)
    (hsupport : ∀ h u, 0 < counts h u →
      u.1.val + u.2.1.val + u.2.2.val = sum h) (e : Target counts) :
    JointCoarseHashing.HasSupportSum (fun p : Position counts => sum p.1) (triple counts e) := by
  intro p
  exact hsupport p.1 ((e p.1).val p.2) (symbol_count_pos counts e p.1 p.2)

omit [Fintype H] [DecidableEq H] in
theorem exists_position_permutation (e f : Target counts) :
    ∃ p : ∀ h, Equiv.Perm (Positions counts h), ∀ h, (e h).val ∘ p h = (f h).val := by
  have hp : ∀ h, ∃ p : Equiv.Perm (Positions counts h), (e h).val ∘ p = (f h).val := by
    intro h
    apply (samePopulation_iff_permutation _ _).mp
    intro u
    rw [(e h).property, (f h).property]
  choose p hp using hp
  exact ⟨p, hp⟩

def sideWord (s : Fin 3) (t : JointCoarseHashing.Triple (Position counts))
    (h : H) : Positions counts h → Fin 17 :=
  fun i => shapeSide s (t.1 ⟨h, i⟩, t.2.1 ⟨h, i⟩, t.2.2 ⟨h, i⟩)

omit [Fintype H] [DecidableEq H] in
theorem sideWord_triple (s : Fin 3) (e : Target counts) (h : H) :
    sideWord counts s (triple counts e) h = shapeSide s ∘ (e h).val := rfl

omit [Fintype H] [DecidableEq H] in
theorem marginal_population_eq (e f : Target counts) (h : H) (s : Fin 3) (a : Fin 17) :
    wordPopulation (sideWord counts s (triple counts e) h) a =
      wordPopulation (sideWord counts s (triple counts f) h) a := by
  obtain ⟨p, hp⟩ := exists_position_permutation counts e f
  rw [sideWord_triple, sideWord_triple, ← hp h]
  exact (wordPopulation_reindex (shapeSide s ∘ (e h).val) (p h) a).symm

def ambientSet (sum : H → ℕ) : Finset (JointCoarseHashing.Triple (Position counts)) :=
  Finset.univ.filter fun t =>
    JointCoarseHashing.HasSupportSum (fun p : Position counts => sum p.1) t ∧
    ∀ h s a, wordPopulation (sideWord counts s t h) a =
      wordPopulation (sideWord counts s (triple counts (canonicalTarget counts)) h) a

theorem targetSet_subset_ambient (sum : H → ℕ)
    (hsupport : ∀ h u, 0 < counts h u →
      u.1.val + u.2.1.val + u.2.2.val = sum h) :
    targetSet counts ⊆ ambientSet counts sum := by
  intro t ht
  obtain ⟨e, _, rfl⟩ := Finset.mem_image.mp ht
  apply Finset.mem_filter.mpr
  refine ⟨Finset.mem_univ _, triple_support counts sum hsupport e, ?_⟩
  intro h s a
  exact marginal_population_eq counts e (canonicalTarget counts) h s a

end MatrixMultiplication.JointPopulation

end

end OAI