File size: 10,945 Bytes
5d5048e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import OAI.Combinatorics.LowSpaceSubsetSum.Representation.RepresentationBucketProbability

namespace OAI

/-! # Capacity of the actual designated parent buckets -/

noncomputable section

namespace LowSpaceSubsetSum

open scoped BigOperators

private theorem hashBucket_mod_mul_low {α : Type*} (X : Finset α) (v : α → ℤ)
    (x : α) (p q : ℤ) :
    hashBucket (X.filter fun e => v e % p = v x % p) v (fun w => w % (p * q)) x =
      hashBucket X v (fun w => w % (p * q)) x := by
  classical
  ext e
  simp only [hashBucket, Finset.mem_filter]
  constructor
  · exact fun h => ⟨h.1.1, h.2⟩
  · intro h
    exact ⟨⟨h.1, (show Int.ModEq (p * q) (v e) (v x) from h.2).of_dvd
      (show p ∣ p * q from ⟨q, rfl⟩)⟩, h.2⟩

namespace RepresentationSetup

variable {n : ℕ} (R : RepresentationSetup n)

/-- The low restriction removes no member of the full product-modulus bucket. -/
theorem chosenParentBucket_eq_hashBucket (h : R.PrimeHistory) (r : R.Residues h.2)
    (hn : 10000 ≤ n)
    (hlog : Real.logb 2 ((mixerSize n : ℝ) + 1) + 1 ≤ (n : ℝ) / 1000)
    (hr : R.ResidueSuccess (R.actualOffsets (R.allocatedChildAssignment hn hlog)) h r)
    (j : Fin 2) (q : ℕ) :
    R.chosenParentBucket h r hn hlog j ((prefixModulus h.2.val 0 : ℤ) * q) =
      hashBucket (R.chosenLowEntries h r hn hlog j) Entry.value
        (fun w => w % ((prefixModulus h.2.val 0 : ℤ) * q))
        (RepresentationUniverse.entry (R.signedWeights h.1.1.1)
          (R.chosenRawParent h r hn hlog j)) := by
  classical
  have hw := R.chosenRawParent_weight h r hn hlog hr j
  ext e
  simp only [chosenParentBucket, chosenLowEntries, hashBucket, Finset.mem_filter]
  change _ ↔ (_ ∧ _) ∧ e.value % ((prefixModulus h.2.val 0 : ℤ) * q) =
    RepresentationUniverse.parentWeight (R.signedWeights h.1.1.1) (R.chosenRawParent h r hn hlog j) %
      ((prefixModulus h.2.val 0 : ℤ) * q)
  rw [hw]
  constructor
  · intro he
    exact ⟨⟨he.1, (show Int.ModEq ((prefixModulus h.2.val 0 : ℤ) * q)
      e.value (weight (R.signedWeights h.1.1.1) (R.chosenNamedParent h r hn hlog j))
        from he.2).of_dvd ⟨q, rfl⟩⟩, he.2⟩
  · exact fun he => ⟨he.1.1, he.2⟩

/-- The unrefined left bucket fits its actual fixed-capacity dictionary. -/
theorem chosenLeftBucket_fits (h : R.PrimeHistory) (r : R.Residues h.2)
    (hn : 10000 ≤ n)
    (hlog : Real.logb 2 ((mixerSize n : ℝ) + 1) + 1 ≤ (n : ℝ) / 1000)
    (hr : R.ResidueSuccess (R.actualOffsets (R.allocatedChildAssignment hn hlog)) h r)
    (q : ℕ)
    (hq : ((R.chosenOtherBucket h r hn hlog 0 q).card : ℝ) ≤
      (compressionDictionaryCapacity n : ℝ) / 2) :
    (R.chosenParentBucket h r hn hlog 0 ((prefixModulus h.2.val 0 : ℤ) * q)).card ≤
      compressionDictionaryCapacity n := by
  rw [R.chosenParentBucket_eq_hashBucket h r hn hlog hr 0 q, hashBucket_card]
  have he := R.left_equalWeight_half_capacity h.1.1.1 h.1.1.2 h.1.2
    (R.allocatedBackground h.1.1.1 h.1.1.2 hn hlog) (by omega)
    (R.chosenLowEntries h r hn hlog 0) (R.chosenLowEntries_subset h r hn hlog 0)
    (RepresentationUniverse.entry (R.signedWeights h.1.1.1) (R.chosenRawParent h r hn hlog 0))
  have hs : ((sameWeightKeys (R.chosenLowEntries h r hn hlog 0) Entry.value
        (RepresentationUniverse.entry (R.signedWeights h.1.1.1) (R.chosenRawParent h r hn hlog 0))).card : ℝ) +
      (R.chosenOtherBucket h r hn hlog 0 q).card ≤ compressionDictionaryCapacity n := by
    linarith
  exact_mod_cast hs

/-- A full right-mask prefix has one possible equal-weight key, while its
unequal-weight keys are already counted in the unrefined collision bucket. -/
theorem chosenRightFullBucket_fits (h : R.PrimeHistory) (r : R.Residues h.2)
    (hn : 10000 ≤ n)
    (hlog : Real.logb 2 ((mixerSize n : ℝ) + 1) + 1 ≤ (n : ℝ) / 1000)
    (hr : R.ResidueSuccess (R.actualOffsets (R.allocatedChildAssignment hn hlog)) h r)
    (q : ℕ) (D : Finset (Fin n))
    (hq : ((R.chosenOtherBucket h r hn hlog 1 q).card : ℝ) ≤
      (compressionDictionaryCapacity n : ℝ) / 2) :
    ((R.chosenParentBucket h r hn hlog 1 ((prefixModulus h.2.val 0 : ℤ) * q)).filter
      (fun e => e.mask = D)).card ≤ compressionDictionaryCapacity n := by
  classical
  let X := R.chosenLowEntries h r hn hlog 1
  let x := RepresentationUniverse.entry (R.signedWeights h.1.1.1)
    (R.chosenRawParent h r hn hlog 1)
  let Y := X.filter fun e => e.mask = D
  have heq : ((sameWeightKeys Y Entry.value x).card : ℝ) ≤
      (compressionDictionaryCapacity n : ℝ) / 2 := by
    have hset : sameWeightKeys Y Entry.value x = X.filter (fun e => e.value = x.value ∧ e.mask = D) := by
      ext e
      simp only [sameWeightKeys, Y, Finset.mem_filter]
      tauto
    rw [hset]
    exact right_fullMask_half_capacity X x.value D (by omega)
  have hother : (otherCollisionKeys Y Entry.value
      (fun w => w % ((prefixModulus h.2.val 0 : ℤ) * q)) x).card ≤
        (R.chosenOtherBucket h r hn hlog 1 q).card := by
    apply Finset.card_le_card
    intro e he
    simp only [otherCollisionKeys, Y, Finset.mem_filter] at he
    exact Finset.mem_filter.mpr ⟨Finset.mem_filter.mpr ⟨he.1.1.1, he.1.2⟩, he.2⟩
  have hb : ((hashBucket Y Entry.value
      (fun w => w % ((prefixModulus h.2.val 0 : ℤ) * q)) x).card : ℝ) ≤
        compressionDictionaryCapacity n := by
    rw [hashBucket_card, Nat.cast_add]
    have ho : ((otherCollisionKeys Y Entry.value
      (fun w => w % ((prefixModulus h.2.val 0 : ℤ) * q)) x).card : ℝ) ≤
        (compressionDictionaryCapacity n : ℝ) / 2 :=
      le_trans (by exact_mod_cast hother) hq
    linarith
  have hset : (R.chosenParentBucket h r hn hlog 1
      ((prefixModulus h.2.val 0 : ℤ) * q)).filter (fun e => e.mask = D) =
      hashBucket Y Entry.value (fun w => w % ((prefixModulus h.2.val 0 : ℤ) * q)) x := by
    rw [R.chosenParentBucket_eq_hashBucket h r hn hlog hr 1 q]
    ext e
    simp only [hashBucket, Y, X, x, Finset.mem_filter]
    tauto
  rw [hset]
  exact_mod_cast hb

/-- Both actual bucket capacities hold for at least nine tenths of the extra
prime choices, with no independence assumption between the two counts. -/
theorem chosenBuckets_probability (h : R.PrimeHistory) (r : R.Residues h.2)
    (hn : 20000 ≤ n)
    (hlog : Real.logb 2 ((mixerSize n : ℝ) + 1) + 1 ≤ (n : ℝ) / 1000)
    (hr : R.ResidueSuccess (R.actualOffsets (R.allocatedChildAssignment (by omega) hlog)) h r)
    (hgood : R.AllocatedResourceGood hn hlog ⟨h, r⟩)
    (hs : mixerSpreadTest (R.signedWeights h.1.1.1) (R.orderedMixers h.1.1.1) n)
    (hscale : Real.logb 2 (inputScale R.input : ℝ) ≤ (n : ℝ) / 1000000000)
    (D : Finset (Fin n)) :
    (9 / 10 : ℝ) ≤
      ∑ q ∈ (primeRange (R.extraPrimeParameter h.1.1.1)).filter (fun q : ℕ =>
        (R.chosenParentBucket h r (by omega) hlog 0 ((prefixModulus h.2.val 0 : ℤ) * q)).card ≤
          compressionDictionaryCapacity n ∧
        ((R.chosenParentBucket h r (by omega) hlog 1 ((prefixModulus h.2.val 0 : ℤ) * q)).filter
          (fun e => e.mask = D)).card ≤ compressionDictionaryCapacity n),
        primeProbability (R.extraPrimeParameter h.1.1.1) q := by
  have hp := R.chosenOtherBuckets_probability h r hn hlog hr hgood
  have hloss := R.representation_bucket_loss h.1.1.1 h.1.1.2 hn hs hscale
  have hsmall : (9 / 10 : ℝ) ≤
      ∑ q ∈ (primeRange (R.extraPrimeParameter h.1.1.1)).filter (fun q : ℕ =>
        ((R.chosenOtherBucket h r (by omega) hlog 0 q).card : ℝ) ≤
          (compressionDictionaryCapacity n : ℝ) / 2 ∧
        ((R.chosenOtherBucket h r (by omega) hlog 1 q).card : ℝ) ≤
          (compressionDictionaryCapacity n : ℝ) / 2),
        primeProbability (R.extraPrimeParameter h.1.1.1) q := by linarith
  apply hsmall.trans
  apply Finset.sum_le_sum_of_subset_of_nonneg
  · intro q hq
    obtain ⟨hmem, hleft, hright⟩ := Finset.mem_filter.mp hq
    exact Finset.mem_filter.mpr ⟨hmem,
      R.chosenLeftBucket_fits h r (by omega) hlog hr q hleft,
      R.chosenRightFullBucket_fits h r (by omega) hlog hr q D hright⟩
  · intro _ _ _
    unfold primeProbability
    positivity

/-- The extra-prime draw is uniform on the concrete prime range. -/
abbrev ExtraPrimeSpace (z : Layout n) := ↥(primeRange (R.extraPrimeParameter z))

def extraPrimeLaw (z : Layout n) (hn : 100 ≤ n) : FiniteLaw (R.ExtraPrimeSpace z) := by
  let : Nonempty (R.ExtraPrimeSpace z) :=
    ⟨⟨(primeRange_nonempty (R.extraPrimeParameter_range z hn).1).choose,
      (primeRange_nonempty (R.extraPrimeParameter_range z hn).1).choose_spec⟩⟩
  exact FiniteLaw.uniform

theorem extraPrimeLaw_probability (z : Layout n) (hn : 100 ≤ n) (P : ℕ → Prop) [DecidablePred P] :
    (R.extraPrimeLaw z hn).probability (fun q => P q.val) =
      ∑ q ∈ (primeRange (R.extraPrimeParameter z)).filter P,
        primeProbability (R.extraPrimeParameter z) q := by
  classical
  let : Nonempty (R.ExtraPrimeSpace z) :=
    ⟨⟨(primeRange_nonempty (R.extraPrimeParameter_range z hn).1).choose,
      (primeRange_nonempty (R.extraPrimeParameter_range z hn).1).choose_spec⟩⟩
  rw [extraPrimeLaw, FiniteLaw.uniform_probability,
    finiteUniformProbability_finset (primeRange (R.extraPrimeParameter z)) P]
  simp [primeProbability, div_eq_mul_inv, mul_comm]

/-- Finite-kernel version of the two actual dictionary-capacity bounds. -/
theorem chosenBuckets_law_probability (h : R.PrimeHistory) (r : R.Residues h.2)
    (hn : 20000 ≤ n)
    (hlog : Real.logb 2 ((mixerSize n : ℝ) + 1) + 1 ≤ (n : ℝ) / 1000)
    (hr : R.ResidueSuccess (R.actualOffsets (R.allocatedChildAssignment (by omega) hlog)) h r)
    (hgood : R.AllocatedResourceGood hn hlog ⟨h, r⟩)
    (hs : mixerSpreadTest (R.signedWeights h.1.1.1) (R.orderedMixers h.1.1.1) n)
    (hscale : Real.logb 2 (inputScale R.input : ℝ) ≤ (n : ℝ) / 1000000000)
    (D : Finset (Fin n)) :
    (9 / 10 : ℝ) ≤ (R.extraPrimeLaw h.1.1.1 (by omega)).probability (fun q =>
        (R.chosenParentBucket h r (by omega) hlog 0 ((prefixModulus h.2.val 0 : ℤ) * q.val)).card ≤
          compressionDictionaryCapacity n ∧
        ((R.chosenParentBucket h r (by omega) hlog 1 ((prefixModulus h.2.val 0 : ℤ) * q.val)).filter
          (fun e => e.mask = D)).card ≤ compressionDictionaryCapacity n) := by
  classical
  let P : ℕ → Prop := fun q =>
    (R.chosenParentBucket h r (by omega) hlog 0 ((prefixModulus h.2.val 0 : ℤ) * q)).card ≤
      compressionDictionaryCapacity n ∧
    ((R.chosenParentBucket h r (by omega) hlog 1 ((prefixModulus h.2.val 0 : ℤ) * q)).filter
      (fun e => e.mask = D)).card ≤ compressionDictionaryCapacity n
  change (9 / 10 : ℝ) ≤ (R.extraPrimeLaw h.1.1.1 (by omega)).probability (fun q => P q.val)
  rw [R.extraPrimeLaw_probability h.1.1.1 (by omega) P]
  exact R.chosenBuckets_probability h r hn hlog hr hgood hs hscale D

end RepresentationSetup
end LowSpaceSubsetSum

end

end OAI