File size: 13,446 Bytes
135052a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import OAI.Probability.SATVariance.RootDisintegration

namespace OAI

noncomputable section

open MeasureTheory ProbabilityTheory

namespace RandomKSAT

open scoped Classical ENNReal

lemma favg_sigma.{u_1, u_2} {ι : Type u_1} [Fintype ι] {α : ι → Type u_2}
    [∀ i, Fintype (α i)] [∀ i, Nonempty (α i)] (f : (i : ι) × α i → ℝ) :
    favg f = ∑ i, ((Fintype.card (α i) : ℝ) / Fintype.card ((i : ι) × α i)) *
      favg (fun a => f ⟨i,a⟩) := by
  unfold favg
  rw [Fintype.sum_sigma, Finset.sum_div]
  apply Finset.sum_congr rfl
  intro i _
  have hc : (Fintype.card (α i) : ℝ) ≠ 0 := by
    exact_mod_cast (Fintype.card_ne_zero : Fintype.card (α i) ≠ 0)
  field_simp

lemma favg_pi_product.{u_1, u_2} {ι : Type u_1} {α : Type u_2} [Fintype ι] [DecidableEq ι] [Fintype α]
    (f : ι → α → ℝ) :
    favg (fun x : ι → α => ∏ i, f i (x i)) = ∏ i, favg (f i) := by
  classical
  unfold favg
  rw [← Fintype.prod_sum, Finset.prod_div_distrib]
  simp

lemma favg_pi_eval.{u_1, u_2} {ι : Type u_1} {α : Type u_2} [Fintype ι] [DecidableEq ι] [Fintype α] [Nonempty α]
    (i : ι) (f : α → ℝ) : favg (fun x : ι → α => f (x i)) = favg f := by
  classical
  have he := favg_equiv (Equiv.funSplitAt i α).symm (fun x : ι → α => f (x i))
  rw [← he, favg_prod]
  simp only [Equiv.funSplitAt_symm_apply]
  simp only [dite_true]
  simp_rw [favg_const]

lemma favg_pi_restrict.{u_1, u_2} {ι : Type u_1} {α : Type u_2} [Fintype ι] [DecidableEq ι] [Fintype α] [Nonempty α]
    (p : ι → Prop) [DecidablePred p] (f : ({i // p i} → α) → ℝ) :
    favg (fun x : ι → α => f (fun i => x i)) = favg f := by
  classical
  let e := Equiv.piEquivPiSubtypeProd p (fun _ : ι => α)
  have he := favg_equiv e.symm (fun x : ι → α => f (fun i : {i // p i} => x i))
  rw [← he, favg_prod]
  have hh : (fun a : {i // p i} → α =>
      favg (fun b : {i // ¬p i} → α => f (fun i => e.symm (a,b) i))) =
      fun a => f a := by
    funext a
    have hi : ∀ b : {i // ¬p i} → α, (fun i : {i // p i} => e.symm (a,b) i) = a := by
      intro b
      funext i
      simp [e, Equiv.piEquivPiSubtypeProd, i.property]
    simp_rw [hi]
    exact favg_const _
  rw [hh]

def clauseCastEquiv {u s t : ℕ} (h : s = t) : Clause u s ≃ Clause u t :=
  Equiv.cast (congrArg (Clause u) h)

lemma clauseRead_cast {u s t : ℕ} (h : s = t) (c : Clause u s) (v : Fin u) :
    clauseRead (clauseCastEquiv h c) v = clauseRead c v := by
  subst t
  rfl

abbrev RefreshNoise (u k : ℕ) := Clause u k × (Fin k → Clause u (k-1))

def trueRoot {k : ℕ} (t : RootType k) (a : Assignment k) (h : RootSatisfies t a) : Fin k :=
  h.choose

lemma trueRoot_spec {k : ℕ} (t : RootType k) (a : Assignment k) (h : RootSatisfies t a) :
    t (trueRoot t a h) = some (a (trueRoot t a h)) := h.choose_spec

def refreshPart {u k : ℕ} (t : RootType k) (a : Assignment k)
    (c : Clause u (k-rootSize t)) (z : RefreshNoise u k) : Clause u (k-rootSize t) :=
  if h : rootSize t = 0 then clauseCastEquiv (by omega) z.1
  else if h' : rootSize t = 1 ∧ RootSatisfies t a then
    clauseCastEquiv (by omega) (z.2 (trueRoot t a h'.2))
  else c

lemma favg_refreshPart {u k : ℕ} (hku : k ≤ u) (t : RootType k) (a : Assignment k)
    (f : Clause u (k-rootSize t) → ℝ) :
    favg (fun c => favg (fun z : RefreshNoise u k => f (refreshPart t a c z))) = favg f := by
  let := clause_nonempty u k hku
  let := clause_nonempty u (k-1) (by omega)
  let := clause_nonempty u (k-rootSize t) (by omega)
  by_cases h : rootSize t = 0
  · simp only [refreshPart, dite_eq_left h]
    have he :
        favg (fun z : RefreshNoise u k => f (clauseCastEquiv (by omega) z.1)) = favg f := by
      rw [favg_prod]
      dsimp only
      simp_rw [favg_const]
      exact favg_equiv (clauseCastEquiv (by omega)) f
    simp_rw [he]
    exact favg_const _
  · simp only [refreshPart, dite_eq_right h]
    by_cases h' : rootSize t = 1 ∧ RootSatisfies t a
    · simp only [dite_eq_left h']
      rw [favg_const]
      rw [favg_prod]
      dsimp only
      have hp := favg_pi_eval (trueRoot t a h'.2)
        (fun c : Clause u (k-1) => f (clauseCastEquiv (show k-1 = k-rootSize t by omega) c))
      rw [favg_const]
      exact hp.trans (favg_equiv (clauseCastEquiv (u := u) (s := k-1) (t := k-rootSize t) (by omega)) f)
    · simp only [dite_eq_right h']
      simp_rw [favg_const]

lemma favg_refresh_stationary {n k u : ℕ} (hku : k ≤ u)
    (e : Fin k ⊕ Fin u ≃ Fin n) (a : Assignment k) (f : Clause n k → ℝ) :
    favg (fun p : (t : RootType k) × Clause u (k-rootSize t) =>
      favg (fun z : RefreshNoise u k => f (mergeClause e p.1 (refreshPart p.1 a p.2 z)))) =
      favg f := by
  let (t : RootType k) : Nonempty (Clause u (k-rootSize t)) := clause_nonempty _ _ (by omega)
  trans favg (fun p : (t : RootType k) × Clause u (k-rootSize t) => mergeClause e p.1 p.2 |> f)
  · rw [favg_sigma, favg_sigma]
    apply Finset.sum_congr rfl
    intro t _
    congr 1
    exact favg_refreshPart hku t a (fun c => f (mergeClause e t c))
  · exact favg_equiv (splitClauseEquiv e) f

def refreshClause {n k u : ℕ} (e : Fin k ⊕ Fin u ≃ Fin n) (a : Assignment k)
    (c : Clause n k) (z : RefreshNoise u k) : Clause n k :=
  let p := (splitClauseEquiv e).symm c
  mergeClause e p.1 (refreshPart p.1 a p.2 z)

lemma favg_refreshClause {n k u : ℕ} (hku : k ≤ u)
    (e : Fin k ⊕ Fin u ≃ Fin n) (a : Assignment k) (f : Clause n k → ℝ) :
    favg (fun c => favg (fun z : RefreshNoise u k => f (refreshClause e a c z))) = favg f := by
  calc
    _ = favg (fun p : (t : RootType k) × Clause u (k-rootSize t) =>
        favg (fun z : RefreshNoise u k => f (refreshClause e a (splitClauseEquiv e p) z))) :=
      (favg_equiv (splitClauseEquiv e) _).symm
    _ = favg (fun p : (t : RootType k) × Clause u (k-rootSize t) =>
        favg (fun z : RefreshNoise u k => f (mergeClause e p.1 (refreshPart p.1 a p.2 z)))) := by
      congr 1
      funext p
      congr 1
      funext z
      exact congrArg f (congrArg
        (fun q : (t : RootType k) × Clause u (k-rootSize t) =>
          mergeClause e q.1 (refreshPart q.1 a q.2 z))
        ((splitClauseEquiv e).symm_apply_apply p))
    _ = favg f := favg_refresh_stationary hku e a f

lemma favg_pointwise_refresh.{u_1, u_2} {α : Type u_1} {β : Type u_2} [Fintype α] [Fintype β]
    (R : α → β → α)
    (hR : ∀ f : α → ℝ, favg (fun a => favg (fun b => f (R a b))) = favg f)
    (m : ℕ) (f : (Fin m → α) → ℝ) :
    favg (fun x : Fin m → α => favg (fun z : Fin m → β => f (fun i => R (x i) (z i)))) =
      favg f := by
  induction m with
  | zero =>
    simp only [favg, Fintype.sum_unique, Fintype.card_unique, Nat.cast_one, div_one]
    congr 1
    funext i
    exact i.elim0
  | succ m ih =>
    rw [favg_cons]
    simp_rw [favg_cons (α := β)]
    have he (x : Fin m → α) (a : α) (z : Fin m → β) (b : β) :
        (fun i : Fin (m+1) => R ((Fin.cons a x : Fin (m+1) → α) i) ((Fin.cons b z : Fin (m+1) → β) i)) =
        Fin.cons (R a b) (fun i => R (x i) (z i)) := by
      funext i
      refine Fin.cases ?_ (fun j => ?_) i <;> rfl
    have hf (x : Fin m → α) (a : α) (z : Fin m → β) (b : β) :
        f (fun i => R ((Fin.cons a x : Fin (m+1) → α) i) ((Fin.cons b z : Fin (m+1) → β) i)) =
        f (Fin.cons (R a b) (fun i => R (x i) (z i))) := congrArg f (he x a z b)
    simp_rw [hf]
    conv_lhs => arg 1; intro x; rw [favg_comm]
    have hsingle (x : Fin m → α) (z : Fin m → β) :
        favg (fun a => favg (fun b => f (Fin.cons (R a b) (fun i => R (x i) (z i))))) =
        favg (fun c => f (Fin.cons c (fun i => R (x i) (z i)))) :=
      hR (fun c : α => f (Fin.cons c (fun i : Fin m => R (x i) (z i))))
    simp_rw [hsingle]
    conv_lhs => arg 1; intro x; rw [favg_comm]
    rw [favg_comm]
    have htail (c : α) :
        favg (fun x : Fin m → α => favg (fun z : Fin m → β =>
          f (Fin.cons c (fun i => R (x i) (z i))))) =
        favg (fun x : Fin m → α => f (Fin.cons c x)) :=
      ih (fun x : Fin m → α => f (Fin.cons c x))
    simp_rw [htail]
    rw [favg_cons, favg_comm]

def pathReward.{u_1, u_2} {X : Type u_1} {α : Type u_2} (step : ℕ → α → X → X) (f : X → ℝ) :
    (m : ℕ) → (Fin m → α) → X → ℝ
  | 0, _, _ => 0
  | m+1, z, x => f x + pathReward (fun i => step (i+1)) f m (Fin.tail z) (step 0 (z 0) x)

def uniformStep.{u_1, u_2} {X : Type u_1} {α : Type u_2} [Fintype α] (step : ℕ → α → X → X)
    (i : ℕ) (f : X → ℝ) (x : X) : ℝ := favg (fun a => f (step i a x))

lemma uniformStep_mono.{u_1, u_2} {X : Type u_1} {α : Type u_2} [Fintype α] (step : ℕ → α → X → X) (i : ℕ) :
    Monotone (uniformStep step i) := by
  intro f g h x
  exact favg_mono fun _ => h _

lemma uniformStep_add.{u_1, u_2} {X : Type u_1} {α : Type u_2} [Fintype α] (step : ℕ → α → X → X)
    (i : ℕ) (f g : X → ℝ) :
    uniformStep step i (fun x => f x+g x) = fun x => uniformStep step i f x+uniformStep step i g x := by
  funext x
  exact favg_add _ _

lemma uniformStep_mul.{u_1, u_2} {X : Type u_1} {α : Type u_2} [Fintype α] (step : ℕ → α → X → X)
    (i : ℕ) (c : ℝ) (f : X → ℝ) :
    uniformStep step i (fun x => c*f x) = fun x => c*uniformStep step i f x := by
  funext x
  exact favg_mul _ _

lemma uniformStep_const.{u_1, u_2} {X : Type u_1} {α : Type u_2} [Fintype α] [Nonempty α]
    (step : ℕ → α → X → X) (i : ℕ) (c : ℝ) :
    uniformStep step i (fun _ => c) = fun _ => c := by
  funext x
  exact favg_const _

lemma uniformStep_add_const.{u_1, u_2} {X : Type u_1} {α : Type u_2} [Fintype α] [Nonempty α]
    (step : ℕ → α → X → X) (i : ℕ) (f : X → ℝ) (c : ℝ) :
    uniformStep step i (fun x => f x+c) = fun x => uniformStep step i f x+c := by
  rw [uniformStep_add, uniformStep_const]

lemma pathReward_avg.{u_1, u_2} {X : Type u_1} {α : Type u_2} [Fintype α] [Nonempty α]
    (step : ℕ → α → X → X) (f : X → ℝ) (m : ℕ) (x : X) :
    favg (fun z : Fin m → α => pathReward step f m z x) =
      markovSum (uniformStep step) f m x := by
  induction m generalizing step x with
  | zero => simp [pathReward, markovSum, favg_const]
  | succ m ih =>
    rw [favg_cons, favg_comm]
    simp only [pathReward, Fin.cons_zero, Fin.tail_cons, favg_add, favg_const]
    simp_rw [ih]
    rfl

lemma markovSum_mul.{u_1} {X : Type u_1} (P : ℕ → (X → ℝ) → X → ℝ)
    (hP : ∀ i c f, P i (fun x => c*f x) = fun x => c*P i f x)
    (c : ℝ) (f : X → ℝ) (m : ℕ) :
    markovSum P (fun x => c*f x) m = fun x => c*markovSum P f m x := by
  induction m generalizing P with
  | zero => funext x; simp [markovSum]
  | succ m ih =>
    rw [markovSum, ih _ (fun i => hP (i+1)), hP]
    funext x
    simp only [markovSum]
    ring

lemma pathReward_sq_potential.{u_1, u_2} {X : Type u_1} {α : Type u_2} [Fintype α] [Nonempty α]
    (step : ℕ → α → X → X) (f τ : X → ℝ)
    (hf : ∀ x, 0 ≤ f x) (hτ : ∀ x, 0 ≤ τ x) (z : ℕ → ℝ)
    (hz : ∀ i, 0 ≤ z i)
    (hdrift : ∀ i x, f x + uniformStep step i τ x ≤ τ x+z i)
    {Z : ℝ} (m : ℕ) (hZ : (∑ i ∈ Finset.range m, z i) ≤ Z) (x : X) :
    favg (fun w : Fin m → α => (pathReward step f m w x)^2) ≤
      2 * markovSum (uniformStep step) (fun y => f y*(τ y+Z)) m x := by
  induction m generalizing step z x with
  | zero => simp [pathReward, markovSum, favg_const]
  | succ m ih =>
    have hzt : (∑ i ∈ Finset.range m, z (i+1)) ≤ Z := by
      rw [Finset.sum_range_succ'] at hZ
      linarith [hz 0]
    have hfuture : uniformStep step 0
        (markovSum (uniformStep (fun i => step (i+1))) f m) x ≤
        τ x+Z-f x := by
      have hh := uniformStep_mono step 0 (fun y =>
        markovSum_potential (uniformStep (fun i => step (i+1)))
          (uniformStep_mono _) (uniformStep_add_const _) f τ hτ (fun i => z (i+1))
          (fun i => hdrift (i+1)) m y) x
      rw [uniformStep_add_const] at hh
      rw [Finset.sum_range_succ'] at hZ
      linarith [hdrift 0 x]
    have hi := favg_mono (fun a : α =>
      ih (fun i => step (i+1)) (fun i => z (i+1)) (fun i => hz (i+1))
        (fun i => hdrift (i+1)) hzt (step 0 a x))
    rw [favg_mul] at hi
    rw [favg_cons, favg_comm]
    have hs (a : α) : favg (fun w : Fin m → α =>
        (pathReward step f (m+1) (Fin.cons a w) x)^2) =
        (f x)^2 + 2*f x * markovSum (uniformStep (fun i => step (i+1))) f m (step 0 a x) +
          favg (fun w : Fin m → α =>
            (pathReward (fun i => step (i+1)) f m w (step 0 a x))^2) := by
      simp only [pathReward, Fin.cons_zero, Fin.tail_cons, add_sq]
      rw [favg_add, favg_add, favg_const]
      rw [favg_mul, pathReward_avg]
    simp_rw [hs]
    rw [favg_add, favg_add, favg_const, favg_mul]
    change _ ≤ 2*(f x*(τ x+Z)+_)
    change favg _ ≤ 2*uniformStep step 0
      (markovSum (uniformStep (fun i => step (i+1))) (fun y => f y*(τ y+Z)) m) x at hi
    change f x^2 + 2*f x * uniformStep step 0
      (markovSum (uniformStep (fun i => step (i+1))) f m) x + _ ≤
      2*(f x*(τ x+Z)+uniformStep step 0
        (markovSum (uniformStep (fun i => step (i+1))) (fun y => f y*(τ y+Z)) m) x)
    nlinarith [mul_le_mul_of_nonneg_left hfuture (mul_nonneg (by norm_num : (0 : ℝ) ≤ 2) (hf x)),
      sq_nonneg (f x)]

end RandomKSAT

end

end OAI