File size: 4,764 Bytes
cdec422
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import OAI.LinearAlgebra.MatrixMultiplication.Completion.ColorEntropy
import OAI.LinearAlgebra.MatrixMultiplication.Completion.Entropy
import OAI.LinearAlgebra.MatrixMultiplication.Entropy.IndependentConditioning

namespace OAI

/-! Readable tensor completion and its finite arithmetic realization. -/

noncomputable section

namespace MatrixMultiplication.CompletionTerminationLaw

open MatrixMultiplication.Foundation CompletionLabels CompletionColorLaws CompletionLaws
open scoped BigOperators
attribute [local instance 10000] Classical.propDecidable Classical.decEq
attribute [local instance 11000] instDecidableEqFin

universe u
variable {X Y Z : Type u} [Fintype X] [Fintype Y] [Fintype Z]

theorem retained_mass_one (S : FlaggedTensor X Y Z) (first second : Color)
    (pf : FiniteLaw (ColorSlice S first)) (ps : FiniteLaw (ColorSlice S second))
    (a : ℝ) (ha : 0 ≤ a) (ha' : a ≤ 1) :
    eventMass (colorMix S first second pf ps a ha ha')
      (fun w => leafColor S w = first ∨ leafColor S w = second) = 1 := by
  rw [eventMass, sum_subtype_indicator]
  calc
    _ = ∑ w, (colorMix S first second pf ps a ha ha').mass w := by
      apply Finset.sum_congr rfl
      intro w _
      split_ifs with h
      · rfl
      · exact (colorMix_mass_zero S first second pf ps a ha ha' w
          (fun h' => h (Or.inl h')) (fun h' => h (Or.inr h'))).symm
    _ = _ := (colorMix S first second pf ps a ha ha').total

def retainedEquiv (S : FlaggedTensor X Y Z) (first second : Color) :
    {w : Leaf S // leafColor S w = first ∨ leafColor S w = second} ≃
      RetainedLeaf S first second := Equiv.refl _

theorem retained_sum (S : FlaggedTensor X Y Z) (first second : Color) (f : Leaf S → ℝ) :
    (∑ w : RetainedLeaf S first second, f w.val) =
      ∑ w : {w : Leaf S // leafColor S w = first ∨ leafColor S w = second}, f w.val := by
  exact ((retainedEquiv S first second).sum_comp
    (fun w : RetainedLeaf S first second => f w.val)).symm

def law (S : FlaggedTensor X Y Z) (first second : Color)
    (pf : FiniteLaw (ColorSlice S first)) (ps : FiniteLaw (ColorSlice S second))
    (a : ℝ) (ha : 0 ≤ a) (ha' : a ≤ 1) :
    FiniteLaw (RetainedLeaf S first second) where
  mass w := (colorMix S first second pf ps a ha ha').mass w.val
  nonneg w := (colorMix S first second pf ps a ha ha').nonneg w.val
  total := by
    rw [retained_sum]
    exact retained_mass_one S first second pf ps a ha ha'

theorem law_mass (S : FlaggedTensor X Y Z) (first second : Color)
    (pf : FiniteLaw (ColorSlice S first)) (ps : FiniteLaw (ColorSlice S second))
    (a : ℝ) (ha : 0 ≤ a) (ha' : a ≤ 1) (w : RetainedLeaf S first second) :
    (law S first second pf ps a ha ha').mass w =
      (colorMix S first second pf ps a ha ha').mass w.val := rfl

theorem law_map_mass {U : Type*} [Fintype U]
    (S : FlaggedTensor X Y Z) (first second : Color)
    (pf : FiniteLaw (ColorSlice S first)) (ps : FiniteLaw (ColorSlice S second))
    (a : ℝ) (ha : 0 ≤ a) (ha' : a ≤ 1) (f : Leaf S → U) (u : U) :
    ((law S first second pf ps a ha ha').map (fun w => f w.val)).mass u =
      ((colorMix S first second pf ps a ha ha').map f).mass u := by
  rw [FiniteLaw.map_mass]
  simp only [law_mass]
  rw [retained_sum S first second
    (fun w => if f w = u then (colorMix S first second pf ps a ha ha').mass w else 0)]
  rw [sum_subtype_indicator (fun w : Leaf S =>
    leafColor S w = first ∨ leafColor S w = second)
    (fun w => if f w = u then (colorMix S first second pf ps a ha ha').mass w else 0)]
  rw [FiniteLaw.map_mass]
  apply Finset.sum_congr rfl
  intro w _
  by_cases h : leafColor S w = first ∨ leafColor S w = second
  · simp [h]
  · have hz := colorMix_mass_zero S first second pf ps a ha ha' w
      (fun h' => h (Or.inl h')) (fun h' => h (Or.inr h'))
    simp [h, hz]

theorem law_entropy (S : FlaggedTensor X Y Z) (first second : Color)
    (hne : first ≠ second)
    (pf : FiniteLaw (ColorSlice S first)) (ps : FiniteLaw (ColorSlice S second))
    (a : ℝ) (ha : 0 ≤ a) (ha' : a ≤ 1) :
    finiteEntropy (law S first second pf ps a ha ha').mass =
      entropyTerm a + entropyTerm (1 - a) +
        a * finiteEntropy pf.mass + (1 - a) * finiteEntropy ps.mass := by
  have hmap : ((law S first second pf ps a ha ha').map Subtype.val).mass =
      (colorMix S first second pf ps a ha ha').mass := by
    funext w
    exact (law_map_mass S first second pf ps a ha ha' id w).trans
      ((colorMix S first second pf ps a ha ha').map_mass_apply id Function.injective_id w)
  rw [← (law S first second pf ps a ha ha').map_entropy_of_injective
    Subtype.val Subtype.val_injective, hmap]
  exact colorMix_entropy S first second hne pf ps a ha ha'

end MatrixMultiplication.CompletionTerminationLaw

end

end OAI