File size: 7,991 Bytes
2f887db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import OAI.Computability.PerfectCompleteness.Reduction.PreliminaryAveraging

namespace OAI


namespace PerfectCompleteness.CandidateCoupling

open PreliminarySampler PreliminaryAveraging TreeSourceSpaces HierarchicalArrays
open UniqueGamesTheorem.Foundations.Games
open scoped BigOperators Classical

noncomputable section

section Selection

variable {I : Type*} [Fintype I] [DecidableEq I]
  {D : I → Type*} [∀ i, Fintype (D i)]

theorem selected_probability (μ : FiniteDistribution I)
    (ν : ∀ i, FiniteDistribution (D i)) (event : (Σ i, D i) → Bool) :
    (μ.product (FiniteProduct.law ν)).probability
        (fun z => event ⟨z.1, z.2 z.1⟩) =
      (CompletionSoundness.sigmaLaw μ ν).probability event := by
  rw [probability_product, CompletionSoundness.sigmaLaw_probability]
  apply FiniteDistribution.expectation_congr
  intro i
  have h := FiniteDistribution.probability_pushforward
    (FiniteProduct.law ν) (fun directions => directions i)
    (fun direction => event ⟨i, direction⟩)
  rw [FiniteProduct.eval_pushforward] at h
  exact h.symm

theorem selected_pushforward (μ : FiniteDistribution I)
    (ν : ∀ i, FiniteDistribution (D i)) :
    (μ.product (FiniteProduct.law ν)).pushforward
        (fun z => (⟨z.1, z.2 z.1⟩ : Σ i, D i)) =
      CompletionSoundness.sigmaLaw μ ν := by
  apply FiniteDistribution.eq_of_weight_eq
  intro choice
  rw [FiniteDistribution.weight_eq_probability_singleton,
    FiniteDistribution.weight_eq_probability_singleton,
    FiniteDistribution.probability_pushforward]
  exact selected_probability μ ν (fun c => decide (c = choice))

omit [DecidableEq I] in
theorem expectation_sigmaLaw (μ : FiniteDistribution I)
    (ν : ∀ i, FiniteDistribution (D i)) (f : (Σ i, D i) → ℝ) :
    (CompletionSoundness.sigmaLaw μ ν).expectation f =
      μ.expectation (fun i => (ν i).expectation (fun d => f ⟨i, d⟩)) := by
  simp only [FiniteDistribution.expectation, CompletionSoundness.sigmaLaw,
    Fintype.sum_sigma, Finset.mul_sum, mul_assoc]

end Selection

theorem probability_eq_expectation {X : Type*} [Fintype X]
    (μ : FiniteDistribution X) (event : X → Bool) :
    μ.probability event = μ.expectation (fun x => if event x then 1 else 0) := by
  simp only [FiniteDistribution.probability, FiniteDistribution.expectation,
    mul_ite, mul_one, mul_zero]

theorem expectation_div {X : Type*} [Fintype X]
    (μ : FiniteDistribution X) (f : X → ℝ) (c : ℝ) :
    μ.expectation (fun x => f x / c) = μ.expectation f / c := by
  simp only [FiniteDistribution.expectation, mul_div_assoc, Finset.sum_div]

variable {v m n t : Nat} {branch rows repeats : Nat → Nat}

abbrev DirectionTuple (rows : Nat → Nat) (leaf : RecursiveSpaces.Slots branch n) :=
  (level : Fin n) → BucketSampler.Direction
    (rows (Nodes.height (GeometricPath.nodeAtLevel leaf level)))

def directionsLaw (leaf : RecursiveSpaces.Slots branch n)
    (hrows : ∀ k, 0 < rows (k + 1)) : FiniteDistribution (DirectionTuple rows leaf) :=
  FiniteProduct.law (fun level => directionLaw leaf level hrows)

theorem directions_marginal (leaf : RecursiveSpaces.Slots branch n)
    (hrows : ∀ k, 0 < rows (k + 1)) (level : Fin n) :
    (directionsLaw leaf hrows).pushforward (fun directions => directions level) =
      directionLaw leaf level hrows :=
  FiniteProduct.eval_pushforward _ level

theorem directions_expectation_product (leaf : RecursiveSpaces.Slots branch n)
    (hrows : ∀ k, 0 < rows (k + 1))
    (f : (level : Fin n) → BucketSampler.Direction
      (rows (Nodes.height (GeometricPath.nodeAtLevel leaf level))) → ℝ) :
    (directionsLaw leaf hrows).expectation (fun directions => ∏ level, f level (directions level)) =
      ∏ level, (directionLaw leaf level hrows).expectation (f level) :=
  FiniteProduct.expectation_product _ f

def selectedTupleLaw (leaf : RecursiveSpaces.Slots branch n) (hn : 0 < n)
    (hrows : ∀ k, 0 < rows (k + 1)) :
    FiniteDistribution (Fin n × DirectionTuple rows leaf) := by
  letI : Nonempty (Fin n) := ⟨⟨0, hn⟩⟩
  exact (FiniteDistribution.uniform (Fin n)).product (directionsLaw leaf hrows)

theorem selectedTupleLaw_pushforward (leaf : RecursiveSpaces.Slots branch n)
    (hn : 0 < n) (hrows : ∀ k, 0 < rows (k + 1)) :
    (selectedTupleLaw leaf hn hrows).pushforward
        (fun z => (⟨z.1, z.2 z.1⟩ : Choice rows leaf)) = choiceLaw leaf hn hrows := by
  let : Nonempty (Fin n) := ⟨⟨0, hn⟩⟩
  exact selected_pushforward (FiniteDistribution.uniform (Fin n))
    (fun level => directionLaw leaf level hrows)

def candidateFraction (leaf : RecursiveSpaces.Slots branch n)
    (event : Choice rows leaf → Bool) (directions : DirectionTuple rows leaf) : ℝ :=
  (∑ level : Fin n, if event ⟨level, directions level⟩ then 1 else 0) / (n : ℝ)

theorem choice_probability_candidateFraction (leaf : RecursiveSpaces.Slots branch n)
    (hn : 0 < n) (hrows : ∀ k, 0 < rows (k + 1))
    (event : Choice rows leaf → Bool) :
    (choiceLaw leaf hn hrows).probability event =
      (directionsLaw leaf hrows).expectation (candidateFraction leaf event) := by
  rw [choice_probability]
  change (∑ level : Fin n, (directionLaw leaf level hrows).probability
      (fun direction => event ⟨level, direction⟩)) / (n : ℝ) =
    (directionsLaw leaf hrows).expectation
      (fun directions => (∑ level : Fin n,
        if event ⟨level, directions level⟩ then 1 else 0) / (n : ℝ))
  rw [expectation_div, SmallBias.expectation_sum]
  congr 1
  apply Finset.sum_congr rfl
  intro level _
  rw [probability_eq_expectation]
  exact (FiniteProduct.expectation_eval
    (fun level => directionLaw leaf level hrows) level
    (fun direction => if event ⟨level, direction⟩ then (1 : ℝ) else 0)).symm

abbrev Shared (clauses : Fin m → SourceClause.NormalizedClause v)
    (branch : Nat → Nat) (n t : Nat) (rows : Nat → Nat) :=
  (b : Base branch n t m) ×
    (Arrays (sourceSlots clauses (endpoints b.1)) rows × DirectionTuple rows b.2)

def conditionalSharedLaw (clauses : Fin m → SourceClause.NormalizedClause v)
    (rows repeats : Nat → Nat) (hrows : ∀ k, 0 < rows (k + 1))
    (b : Base branch n t m) :
    FiniteDistribution
      (Arrays (sourceSlots clauses (endpoints b.1)) rows × DirectionTuple rows b.2) :=
  (WholeArraySampler.law rows repeats (GeometricPath.leafPath b.2)
    (sourceSlots clauses (endpoints b.1))).product (directionsLaw b.2 hrows)

def sharedLaw [NeZero m] (clauses : Fin m → SourceClause.NormalizedClause v)
    (rows repeats : Nat → Nat) (hbranch : ∀ k < n, 0 < branch k)
    (hrows : ∀ k, 0 < rows (k + 1)) :
    FiniteDistribution (Shared clauses branch n t rows) :=
  CompletionSoundness.sigmaLaw (baseLaw hbranch)
    (conditionalSharedLaw clauses rows repeats hrows)

theorem candidate_average_shared [NeZero m]
    (clauses : Fin m → SourceClause.NormalizedClause v)
    (rows repeats : Nat → Nat) (hn : 0 < n)
    (hbranch : ∀ k < n, 0 < branch k) (hrows : ∀ k, 0 < rows (k + 1))
    (observe : ∀ b : Base branch n t m,
      Arrays (sourceSlots clauses (endpoints b.1)) rows → Choice rows b.2 → Bool) :
    (PreliminarySampler.law clauses rows repeats hn hbranch hrows).probability
        (fun e => observe e.1
          (WholeArraySampler.evaluate rows repeats (GeometricPath.leafPath e.1.2)
            (sourceSlots clauses (endpoints e.1.1)) e.2.1) e.2.2) =
      (sharedLaw clauses rows repeats hbranch hrows).expectation
        (fun e => candidateFraction e.1.2 (observe e.1 e.2.1) e.2.2) := by
  rw [array_observation_probability clauses rows repeats hn hbranch hrows observe,
    sharedLaw, expectation_sigmaLaw]
  apply FiniteDistribution.expectation_congr
  intro b
  rw [conditionalSharedLaw, FiniteDistribution.expectation_product]
  apply FiniteDistribution.expectation_congr
  intro arrays
  exact choice_probability_candidateFraction b.2 hn hrows (observe b arrays)

end
end PerfectCompleteness.CandidateCoupling

end OAI