File size: 15,981 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import OAI.Computability.PerfectCompleteness.Construction.SourcePhysicalChildExpectation
import OAI.Computability.PerfectCompleteness.Construction.SourceQuestionPhysicalHigh
import OAI.Computability.PerfectCompleteness.Repetition.CleanPhysicalHighEvent
import OAI.Computability.PerfectCompleteness.Repetition.CleanPhysicalRawChildren

namespace OAI

section

namespace PerfectCompleteness.CleanPhysicalHighAverage

noncomputable section

open scoped Classical
open RecursiveSpaces DescendantSpaces TreeSourceSpaces HierarchicalArrays
open UniqueGamesTheorem.Foundations.Games

private theorem mpr_heq {α β : Sort _} (h : α = β) (x : β) :
    HEq (Eq.mpr h x) x := by
  cases h
  rfl

variable {branch rows repeats : Nat → Nat} {n h t v m : Nat} [NeZero m]
  {upper : Nodes branch n} {d : HierarchicalFrozenTables.LowerNodes upper (h + 1)}
  (S : CleanDecoderRate.Setup branch rows repeats n h t v m upper d)

abbrev suffix := CleanRightDecoder.suffix upper (CleanPhysicalDecoderLaw.lower S)
  S.cut (CleanDecoderContext.lowerHeight upper d)

include S in
theorem suffix_proper : h + 1 < Nodes.height upper :=
  CleanRightDecoder.suffix_proper upper (CleanPhysicalDecoderLaw.lower S)
    S.cut (CleanDecoderContext.lowerHeight upper d)

variable (hbranch : ∀ k < n, 0 < branch k)
  (hnode : WholeArrayInteriorExterior.upperNode (CleanDecoderRate.path S) =
    HierarchicalLeftDecoder.LowerNode upper (h + 1) d)
  {Original : OriginalDecoderMark.SlotFamily branch n t → Type*}
  [∀ slots, Fintype (Original slots)]
  (F : OriginalDecoderMark.Family branch rows n t Original) (κ : ℝ)
  (σ : KeyStrategy.Strategy (TreeCanonical.locationCount branch n t))
  (r : Nat) (ρ : ℝ) (A : ManyGoodRows.RowMap (Block rows upper) r)
  (direction : BucketSampler.Direction (rows (h + 1))) (threshold : ℝ) (s : Nat)

abbrev nativeDirection :=
  (ProjectedFiberRawPair.direction (CleanDecoderRate.path S) upper (h + 1) d hnode direction).val

def tapeStatistic
    (left right : Slots branch (h + 1) → Fin t → MixedSupport.Slot)
    (projection : ∀ leaf k, MixedSupport.Projection (left leaf k) (right leaf k))
    (tape : WholeCutSampler.Tape rows repeats (CleanDecoderRate.path S)
      (CutSlotAssembly.fill (CleanDecoderRate.path S) S.outside right)) : ℝ :=
  LowerProjectedOriginalSuccess.kernelSuccess (repeats := repeats) (CleanDecoderRate.path S)
    (CutSlotAssembly.fill (CleanDecoderRate.path S) S.outside left)
    (CutSlotAssembly.fill (CleanDecoderRate.path S) S.outside right)
    (CutProjectionAssembly.fillProjection (CleanDecoderRate.path S) S.outside left right projection)
    upper (h + 1) hbranch d hnode direction A S.cut σ
    (OriginalDecoderMark.mark F upper (h + 1) κ
      (CutSlotAssembly.fill (CleanDecoderRate.path S) S.outside left)) ρ threshold s
    (WholeCutSubtree.knownBucketsAtNode rows repeats
      (CutSlotAssembly.fill (CleanDecoderRate.path S) S.outside right)
      upper (suffix S) (suffix_proper S) (LinearMap.ker A) tape)
    (WholeCutSampler.evaluate rows repeats (CleanDecoderRate.path S)
      (CutSlotAssembly.fill (CleanDecoderRate.path S) S.outside right) tape)

def childStatistic
    (left right : Slots branch (h + 1) → Fin t → MixedSupport.Slot)
    (projection : ∀ leaf k, MixedSupport.Projection (left leaf k) (right leaf k))
    (children : CutChildGrouping.Raw (C := CleanDecoderRate.Calls S) right rows) : ℝ :=
  tapeStatistic S hbranch hnode F κ σ r ρ A direction threshold s left right projection
    (CleanPhysicalReplay.physicalTape rows repeats (CleanDecoderRate.path S)
      S.outside S.placeholder right S.exterior children)

private theorem childStatistic_congr
    (left left' right : Slots branch (h + 1) → Fin t → MixedSupport.Slot)
    (hl : left = left')
    (projection : ∀ leaf k, MixedSupport.Projection (left leaf k) (right leaf k))
    (projection' : ∀ leaf k, MixedSupport.Projection (left' leaf k) (right leaf k))
    (hp : ∀ leaf k, HEq (projection leaf k) (projection' leaf k))
    (children : CutChildGrouping.Raw (C := CleanDecoderRate.Calls S) right rows) :
    childStatistic S hbranch hnode F κ σ r ρ A direction threshold s
        left right projection children =
      childStatistic S hbranch hnode F κ σ r ρ A direction threshold s
        left' right projection' children := by
  cases hl
  have he : projection = projection' :=
    funext (fun leaf => funext (fun k => eq_of_heq (hp leaf k)))
  cases he
  rfl

theorem rightInput_eq (x : CleanDecoderRate.Sample S) :
    CleanPhysicalDecoderLaw.rightInput S (LinearMap.ker A)
        (nativeDirection S hnode direction) x =
      LowerCutOwnInput.ofArrays (CleanNativeReplay.rightSlots S x) rows upper
        (CleanPhysicalDecoderLaw.lower S) (LinearMap.ker A)
        (nativeDirection S hnode direction)
        (WholeCutSubtree.knownBucketsAtNode rows repeats (CleanNativeReplay.rightSlots S x)
          upper (suffix S) (suffix_proper S) (LinearMap.ker A)
          (CleanNativeReplay.rightTape S x))
        (CleanPhysicalDecoderLaw.rightArrays S x) := by
  have hk := WholeCutSubtree.knownBuckets_eq_original rows repeats (Nodes.path upper)
    (suffix S) (suffix_proper S) (CleanNativeReplay.rightSlots S x)
    (LinearMap.ker A) (CleanNativeReplay.rightTape S x)
  exact congrArg
    (fun known : HiddenBucketBias.VisibleDirection (LinearMap.ker A) →
        OwnInputReference.UpperSpace (CleanNativeReplay.rightSlots S x) upper =>
      CleanPhysicalDecoderLaw.inputOfArrays (CleanNativeReplay.rightSlots S x)
        upper (CleanPhysicalDecoderLaw.lower S) (LinearMap.ker A)
        (nativeDirection S hnode direction) known (CleanPhysicalDecoderLaw.rightArrays S x))
    hk.symm

theorem physical_probability_eq (x : CleanDecoderRate.Sample S) :
    (CleanPhysicalDecoderLaw.pairKernel S F κ σ r ρ A (LinearMap.ker A)
      (nativeDirection S hnode direction) threshold x).probability
        (CleanPhysicalHighEvent.highEvent S hbranch s x) =
      childStatistic S hbranch hnode F κ σ r ρ A direction threshold s
        (CleanPhysicalReplay.leftInside rows repeats (CleanDecoderRate.path S)
          S.clauses S.designated x)
        (CleanPhysicalReplay.rightInside rows repeats (CleanDecoderRate.path S)
          S.clauses S.designated x)
        (SourceChildKernel.parentProjection rows S.clauses S.designated
          (fun i => ((x i).1, (x i).2.1)) (fun i => (x i).2.2))
        (CleanPhysicalReplay.rightBlocks rows repeats (CleanDecoderRate.path S)
          S.clauses S.designated x) := by
  have ha := CleanPhysicalCanonicalQuery.arrays_pullback rows repeats (CleanDecoderRate.path S)
    S.outside S.placeholder S.clauses S.designated x S.exterior
  change CleanPhysicalDecoderLaw.leftArrays S x = _ at ha
  unfold childStatistic tapeStatistic LowerProjectedOriginalSuccess.kernelSuccess
  unfold CleanPhysicalDecoderLaw.pairKernel CleanPhysicalDecoderLaw.leftLaw
    CleanPhysicalDecoderLaw.rightLaw
  rw [rightInput_eq S hnode r A direction x]
  unfold CleanPhysicalDecoderLaw.adviceLaw
  rw [ha]
  rfl

def sourceStatistic
    (sources : SourceChildKernel.Sources (m := m) (t := t) S.designated)
    (observed : Σ flags : SourceProjectedTag.Flags (branch := branch) (n := h),
      CutChildGrouping.Raw (C := CleanDecoderRate.Calls S)
        (SourcePhysicalTaggedChildren.slots S.clauses S.designated sources flags) rows) : ℝ :=
  childStatistic S hbranch hnode F κ σ r ρ A direction threshold s
    (SourceChildKernel.parentLeftSlots S.clauses S.designated sources)
    (SourcePhysicalTaggedChildren.slots S.clauses S.designated sources observed.1)
    (fun leaf k => SourceChildKernel.projection S.clauses S.designated leaf.1
      (sources leaf.1) (observed.1 leaf.1) leaf.2 k) observed.2

def questionStatistic
    (q : PreliminarySampler.Questions branch (h + 1) t m)
    (choices : SourceQuestionKernelJoint.ChoiceTuple (branch := branch) (n := h) (t := t))
    (flags : SourceProjectedTag.Flags (branch := branch) (n := h))
    (children : CutChildGrouping.Raw (C := CleanDecoderRate.Calls S)
      (SourceProjectedTag.mixedInside S.clauses S.designated q choices flags) rows) : ℝ :=
  childStatistic S hbranch hnode F κ σ r ρ A direction threshold s
    (SourceProjectedTag.originalSlots S.clauses q)
    (SourceProjectedTag.mixedInside S.clauses S.designated q choices flags)
    (SourceProjectedTag.projection S.clauses S.designated q choices flags) children

private theorem sourceStatistic_eq_question
    (q : PreliminarySampler.Questions branch (h + 1) t m)
    (choices : SourceQuestionKernelJoint.ChoiceTuple (branch := branch) (n := h) (t := t))
    (observed : Σ flags : SourceProjectedTag.Flags (branch := branch) (n := h),
      CutChildGrouping.Raw (C := CleanDecoderRate.Calls S)
        (SourceProjectedTag.mixedInside S.clauses S.designated q choices flags) rows) :
    sourceStatistic S hbranch hnode F κ σ r ρ A direction threshold s
        (SourceQuestionKernelJoint.sources S.designated q choices) observed =
      questionStatistic S hbranch hnode F κ σ r ρ A direction threshold s
        q choices observed.1 observed.2 := by
  apply childStatistic_congr S hbranch hnode F κ σ r ρ A direction threshold s
    _ _ _ (SourceProjectedTag.parentLeftSlots_sources S.clauses S.designated q choices)
  intro leaf k
  symm
  unfold SourceProjectedTag.projection
  dsimp only [id]
  exact mpr_heq _ _

theorem physical_probability_eq_observed (x : CleanDecoderRate.Sample S) :
    (CleanPhysicalDecoderLaw.pairKernel S F κ σ r ρ A (LinearMap.ker A)
      (nativeDirection S hnode direction) threshold x).probability
        (CleanPhysicalHighEvent.highEvent S hbranch s x) =
      let q := SourceQuestionRawSwap.insideQuestions rows S.clauses S.designated x
      let choices := SourceQuestionRawSwap.positions rows S.clauses S.designated x
      let observed := SourcePhysicalChildExpectation.observe rows S.clauses S.designated q choices
        (SourceQuestionRawSwap.raw rows S.clauses S.designated x)
      questionStatistic S hbranch hnode F κ σ r ρ A direction threshold s
        q choices observed.1 observed.2 := by
  let q := SourceQuestionRawSwap.insideQuestions rows S.clauses S.designated x
  let choices := SourceQuestionRawSwap.positions rows S.clauses S.designated x
  let observed := SourcePhysicalChildExpectation.observe rows S.clauses S.designated q choices
    (SourceQuestionRawSwap.raw rows S.clauses S.designated x)
  have hread := congrArg
    (fun sources : SourceChildKernel.Sources (m := m) (t := t) S.designated =>
      sourceStatistic S hbranch hnode F κ σ r ρ A direction threshold s sources
        (SourcePhysicalTaggedChildren.observe rows S.clauses S.designated sources
          (SourceQuestionRawSwap.raw rows S.clauses S.designated x)))
    (SourceQuestionRawSwap.sources_insideQuestions_positions rows S.clauses S.designated x)
  exact (physical_probability_eq S hbranch hnode F κ σ r ρ A direction threshold s x).trans
    (hread.symm.trans (sourceStatistic_eq_question S hbranch hnode F κ σ r ρ A
      direction threshold s q choices observed))

theorem questionStatistic_expectation
    (q : PreliminarySampler.Questions branch (h + 1) t m)
    (choices : SourceQuestionKernelJoint.ChoiceTuple (branch := branch) (n := h) (t := t))
    (flags : SourceProjectedTag.Flags (branch := branch) (n := h)) :
    (CutChildGrouping.rawLaw (C := CleanDecoderRate.Calls S)
      (SourceProjectedTag.mixedInside S.clauses S.designated q choices flags) rows).expectation
        (questionStatistic S hbranch hnode F κ σ r ρ A direction threshold s q choices flags) =
      SourceQuestionPhysicalHigh.success S.outside S.placeholder S.clauses S.designated
        q choices flags upper (suffix S) (suffix_proper S) (h + 1) hbranch d hnode A S.cut σ
        (OriginalDecoderMark.mark F upper (h + 1) κ
          (SourceQuestionPhysicalHigh.originalSlots (CleanDecoderRate.path S) S.outside S.clauses q))
        ρ threshold S.exterior direction s := by
  exact CleanPhysicalRawChildren.expectation_lowerTape rows repeats S.outside S.placeholder
    (SourceProjectedTag.mixedInside S.clauses S.designated q choices flags)
    upper (suffix S) S.exterior
    (tapeStatistic S hbranch hnode F κ σ r ρ A direction threshold s
      (SourceProjectedTag.originalSlots S.clauses q)
      (SourceProjectedTag.mixedInside S.clauses S.designated q choices flags)
      (SourceProjectedTag.projection S.clauses S.designated q choices flags))

theorem mean_probability_eq (cube : Nat) (hcube : 0 < cube) :
    (CleanDecoderPairLaw.pairLaw S σ (CleanPhysicalDecoderLaw.useful S F κ)
      r ρ A (LinearMap.ker A) (nativeDirection S hnode direction) threshold cube hcube).probability
        (fun z => DecoderRankSplit.highAgrees S hbranch s z.1 z.2) =
      (PreliminarySampler.questionsLaw
        (branch := branch) (n := h + 1) (t := t) (m := m)).expectation
        (fun q => (SourceProjectedTag.positionLaw
          (branch := branch) (n := h) (t := t)).expectation
          (fun choices => (SourceProjectedTag.flagLaw
            (fun _ : Fin (branch h) => ProjectedCleanRate.projectionFlag cube hcube)).expectation
            (fun flags => SourceQuestionPhysicalHigh.success
              S.outside S.placeholder S.clauses S.designated q choices flags
              upper (suffix S) (suffix_proper S) (h + 1) hbranch d hnode A S.cut σ
              (OriginalDecoderMark.mark F upper (h + 1) κ
                (SourceQuestionPhysicalHigh.originalSlots
                  (CleanDecoderRate.path S) S.outside S.clauses q))
              ρ threshold S.exterior direction s))) := by
  rw [← CleanPhysicalHighEvent.mean_probability_eq S hbranch F κ σ r ρ A
    (LinearMap.ker A) (nativeDirection S hnode direction) threshold cube hcube s]
  have hsample := FiniteDistribution.expectation_congr (CleanDecoderRate.rawLaw S cube hcube)
    (physical_probability_eq_observed S hbranch hnode F κ σ r ρ A direction threshold s)
  refine hsample.trans ?_
  rw [show CleanDecoderRate.rawLaw S cube hcube =
    SourceChildKernel.originalLaw (C := CleanDecoderRate.Calls S) (t := t)
      rows S.clauses S.designated
      (fun _ => ProjectedCleanRate.projectionFlag cube hcube) from rfl]
  rw [SourcePhysicalChildExpectation.originalLaw_expectation rows S.clauses S.designated
    (fun _ => ProjectedCleanRate.projectionFlag cube hcube)
    (questionStatistic S hbranch hnode F κ σ r ρ A direction threshold s)]
  apply FiniteDistribution.expectation_congr
  intro q
  apply FiniteDistribution.expectation_congr
  intro choices
  apply FiniteDistribution.expectation_congr
  intro flags
  exact questionStatistic_expectation S hbranch hnode F κ σ r ρ A direction threshold s q choices flags

theorem probability_eq_tag_average (cube : Nat) (hcube : 0 < cube) :
    (CleanDecoderPairLaw.pairLaw S σ (CleanPhysicalDecoderLaw.useful S F κ)
      r ρ A (LinearMap.ker A) (nativeDirection S hnode direction) threshold cube hcube).probability
        (fun z => DecoderRankSplit.highAgrees S hbranch s z.1 z.2) =
      (PreliminarySampler.questionsLaw
        (branch := branch) (n := h + 1) (t := t) (m := m)).expectation
        (fun q => (SourceProjectedTag.law (t := t)
          (fun _ : Fin (branch h) => ProjectedCleanRate.projectionFlag cube hcube)).expectation
          (fun tag => SourceQuestionPhysicalHigh.success
            S.outside S.placeholder S.clauses S.designated q tag.1 tag.2
            upper (suffix S) (suffix_proper S) (h + 1) hbranch d hnode A S.cut σ
            (OriginalDecoderMark.mark F upper (h + 1) κ
              (SourceQuestionPhysicalHigh.originalSlots
                (CleanDecoderRate.path S) S.outside S.clauses q))
            ρ threshold S.exterior direction s)) := by
  simpa only [SourceProjectedTag.law, FiniteDistribution.expectation_product] using
    mean_probability_eq S hbranch hnode F κ σ r ρ A direction threshold s cube hcube

end
end PerfectCompleteness.CleanPhysicalHighAverage

end

end OAI