File size: 5,023 Bytes
4a56cd6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import OAI.LinearAlgebra.MatrixMultiplication.JointExtraction.CompatibilityControls
import OAI.LinearAlgebra.MatrixMultiplication.JointExtraction.CompatibilityScaling

namespace OAI

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

noncomputable section

namespace MatrixMultiplication.JointPairMixtureControls

open JointCompatibilityControls JointCompatibilityScaling
open scoped BigOperators

attribute [local instance] Classical.propDecidable

variable {C : Type*} [Fintype C] [DecidableEq C]
  {Shape A B : C β†’ Type*}
  [βˆ€ c, Fintype (Shape c)] [βˆ€ c, DecidableEq (Shape c)]
  [βˆ€ c, Fintype (A c)] [βˆ€ c, DecidableEq (A c)]
  [βˆ€ c, Fintype (B c)] [βˆ€ c, DecidableEq (B c)]

def pairMixture (base : βˆ€ c, Shape c β†’ β„•)
    (left : βˆ€ c, Shape c β†’ A c β†’ ℝ) (right : βˆ€ c, Shape c β†’ B c β†’ ℝ)
    (c : C) (ab : A c Γ— B c) : ℝ :=
  (βˆ‘ u, (base c u : ℝ) * left c u ab.1 * right c u ab.2) / baseSize base c

omit [Fintype C] [DecidableEq C] [βˆ€ c, DecidableEq (B c)] in
theorem sum_first_fiber (c : C) (q : A c Γ— B c β†’ ℝ) (a : A c) :
    (βˆ‘ ab : {ab : A c Γ— B c // ab.1 = a}, q ab.val) = βˆ‘ b, q (a, b) := by
  let e : B c ≃ {ab : A c Γ— B c // ab.1 = a} :=
    { toFun := fun b => ⟨(a, b), rfl⟩
      invFun := fun ab => ab.val.2
      left_inv := fun _ => rfl
      right_inv := fun ab => by apply Subtype.ext; exact Prod.ext ab.property.symm rfl }
  exact (e.sum_comp (fun ab => q ab.val)).symm

omit [Fintype C] [DecidableEq C] [βˆ€ c, DecidableEq (A c)] in
theorem sum_second_fiber (c : C) (q : A c Γ— B c β†’ ℝ) (b : B c) :
    (βˆ‘ ab : {ab : A c Γ— B c // ab.2 = b}, q ab.val) = βˆ‘ a, q (a, b) := by
  let e : A c ≃ {ab : A c Γ— B c // ab.2 = b} :=
    { toFun := fun a => ⟨(a, b), rfl⟩
      invFun := fun ab => ab.val.1
      left_inv := fun _ => rfl
      right_inv := fun ab => by apply Subtype.ext; exact Prod.ext rfl ab.property.symm }
  exact (e.sum_comp (fun ab => q ab.val)).symm

omit [Fintype C] [DecidableEq C] [βˆ€ c, DecidableEq (Shape c)] in
theorem base_eq_zero_of_size_eq_zero (base : βˆ€ c, Shape c β†’ β„•)
    (c : C) (hz : baseSize base c = 0) (u : Shape c) : base c u = 0 :=
  Nat.eq_zero_of_le_zero ((Finset.single_le_sum (fun _ _ => Nat.zero_le _)
    (Finset.mem_univ u)).trans_eq hz)

omit [Fintype C] [DecidableEq C] [βˆ€ c, DecidableEq (Shape c)]
  [βˆ€ c, Fintype (A c)] [βˆ€ c, DecidableEq (A c)] in
theorem totalCenterCount_eq_zero_of_size_eq_zero (base : βˆ€ c, Shape c β†’ β„•)
    (law : βˆ€ c, Shape c β†’ A c β†’ ℝ) (c : C) (hz : baseSize base c = 0) (a : A c) :
    totalCenterCount base law c a = 0 := by
  simp only [totalCenterCount, base_eq_zero_of_size_eq_zero base c hz,
    Nat.cast_zero, zero_mul, Finset.sum_const_zero]

omit [Fintype C] [DecidableEq C] [βˆ€ c, DecidableEq (Shape c)]
  [βˆ€ c, DecidableEq (B c)] in
theorem pairMixture_left_balance (base : βˆ€ c, Shape c β†’ β„•)
    (left : βˆ€ c, Shape c β†’ A c β†’ ℝ) (right : βˆ€ c, Shape c β†’ B c β†’ ℝ)
    (hnormalized : βˆ€ c u, 0 < base c u β†’ βˆ‘ b, right c u b = 1)
    (c : C) (a : A c) :
    (baseSize base c : ℝ) *
      (βˆ‘ ab : {ab : A c Γ— B c // ab.1 = a}, pairMixture base left right c ab.val) =
      totalCenterCount base left c a := by
  rw [sum_first_fiber]
  simp only [pairMixture, ← Finset.sum_div]
  have hnum : (βˆ‘ b : B c, βˆ‘ u : Shape c,
      (base c u : ℝ) * left c u a * right c u b) = totalCenterCount base left c a := by
    rw [Finset.sum_comm]
    apply Finset.sum_congr rfl
    intro u _
    by_cases hu : base c u = 0
    Β· simp [hu]
    Β· rw [← Finset.mul_sum, hnormalized c u (Nat.pos_of_ne_zero hu), mul_one]
  rw [hnum]
  by_cases hz : baseSize base c = 0
  Β· rw [totalCenterCount_eq_zero_of_size_eq_zero base left c hz a]
    simp
  Β· exact mul_div_cancelβ‚€ _ (Nat.cast_ne_zero.mpr hz)

omit [Fintype C] [DecidableEq C] [βˆ€ c, DecidableEq (Shape c)]
  [βˆ€ c, DecidableEq (A c)] in
theorem pairMixture_right_balance (base : βˆ€ c, Shape c β†’ β„•)
    (left : βˆ€ c, Shape c β†’ A c β†’ ℝ) (right : βˆ€ c, Shape c β†’ B c β†’ ℝ)
    (hnormalized : βˆ€ c u, 0 < base c u β†’ βˆ‘ a, left c u a = 1)
    (c : C) (b : B c) :
    (baseSize base c : ℝ) *
      (βˆ‘ ab : {ab : A c Γ— B c // ab.2 = b}, pairMixture base left right c ab.val) =
      totalCenterCount base right c b := by
  rw [sum_second_fiber]
  simp only [pairMixture, ← Finset.sum_div]
  have hnum : (βˆ‘ a : A c, βˆ‘ u : Shape c,
      (base c u : ℝ) * left c u a * right c u b) = totalCenterCount base right c b := by
    rw [Finset.sum_comm]
    apply Finset.sum_congr rfl
    intro u _
    by_cases hu : base c u = 0
    Β· simp [hu]
    Β· rw [← Finset.sum_mul, ← Finset.mul_sum,
        hnormalized c u (Nat.pos_of_ne_zero hu), mul_one]
  rw [hnum]
  by_cases hz : baseSize base c = 0
  Β· rw [totalCenterCount_eq_zero_of_size_eq_zero base right c hz b]
    simp
  Β· exact mul_div_cancelβ‚€ _ (Nat.cast_ne_zero.mpr hz)

end MatrixMultiplication.JointPairMixtureControls

end

end OAI