File size: 17,193 Bytes
e5a97c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
import Mathlib
import OAI.Combinatorics.MatchingCount.Sampling.MetropolisChoice

namespace OAI

namespace MatchingFPRAS.Replica

open Finset

variable {I Ω : Type*} [Fintype I] [DecidableEq I] [Fintype Ω] [DecidableEq Ω]

variable (p : I → Ω → ℝ)

omit [Fintype I] [Fintype Ω] [DecidableEq Ω] in
lemma replacePair_at (x y : I) (hxy : x≠y) (s : I → Ω) (a : Ω×Ω) :
    ((replacePair x y s a) x,(replacePair x y s a) y)=a := by
  simp [replacePair,hxy]

omit [Fintype I] [Fintype Ω] [DecidableEq Ω] in
lemma replacePair_idem (x y : I) (s : I → Ω) (a b : Ω×Ω) :
    replacePair x y (replacePair x y s a) b = replacePair x y s b := by
  funext i
  by_cases hy : i=y
  · subst i; simp [replacePair]
  · by_cases hx : i=x
    · subst i; simp [replacePair,hy]
    · simp [replacePair,hx,hy]

noncomputable def pairStep (x y : I) (Q : PairCapacity p x y) (s t : I → Ω) : ℝ :=
  ∑ b : Ω×Ω, if replacePair x y s b=t then Q.q (s x,s y) b/(p x (s x)*p y (s y)) else 0

noncomputable def baseStep (i : I) (s t : I → Ω) : ℝ :=
  ∑ a : Ω, if Function.update s i a=t then p i a else 0

lemma local_pairFlow (hp : ∀ i a, 0<p i a) (hs : ∀ i,∑ a,p i a=1)
    (x y : I) (hxy : x≠y) (Q : PairCapacity p x y) (s t : I → Ω) :
    pairFlow p x y Q s t = density p s*pairStep p x y Q s t := by
  let f : (I → Ω) → ℝ := fun r => if r=s then pairStep p x y Q r t else 0
  have hf := slice_mean p hs x y f
  have hmean : mean p f=density p s*pairStep p x y Q s t := by
    simp only [mean,f,mul_ite,mul_zero,sum_ite_eq',mem_univ,ite_true]
  rw [hmean] at hf
  rw [←hf]
  unfold pairFlow pushedFlow mean
  rw [Fintype.sum_prod_type]
  apply sum_congr rfl
  intro r _
  rw [Fintype.sum_prod_type,mul_sum]
  apply sum_congr rfl
  intro a _
  have hpa : ((replacePair x y r a) x,(replacePair x y r a) y)=a := replacePair_at x y hxy r a
  rcases a with ⟨ax,ay⟩
  have hx : (replacePair x y r (ax,ay)) x=ax := congrArg Prod.fst hpa
  have hy : (replacePair x y r (ax,ay)) y=ay := congrArg Prod.snd hpa
  by_cases ha : replacePair x y r (ax,ay)=s
  · simp only [f,ha,ite_true]
    rw [←ha]
    simp only [pairStep,hx,hy,replacePair_idem,mul_sum]
    apply sum_congr rfl
    intro b _
    by_cases hb : replacePair x y r b=t
    · simp only [ha,hb,ite_true]
      field_simp [ne_of_gt (hp x ax),ne_of_gt (hp y ay)]
    · simp only [Prod.mk.injEq,ha,hb,true_and,ite_false,mul_zero]
  · simp only [f,ite_eq_right ha,mul_zero]
    apply sum_eq_zero
    intro b _
    simp only [Prod.mk.injEq,ha,false_and,ite_false]

lemma local_baseFlow (_hp : ∀ i a,0<p i a) (hs : ∀ i,∑ a,p i a=1)
    (i : I) (s t : I → Ω) : baseFlow p i s t=density p s*baseStep p i s t := by
  let f : (I → Ω) → ℝ := fun r => if r=s then baseStep p i r t else 0
  have hf := mean_average p hs i f
  have hmean : mean p f=density p s*baseStep p i s t := by
    simp only [mean,f,mul_ite,mul_zero,sum_ite_eq',mem_univ,ite_true]
  rw [hmean] at hf
  rw [←hf]
  unfold baseFlow pushedFlow mean
  rw [Fintype.sum_prod_type]
  simp only [average_apply]
  apply sum_congr rfl
  intro r _
  rw [Fintype.sum_prod_type,mul_sum]
  apply sum_congr rfl
  intro a _
  by_cases ha : Function.update r i a=s
  · simp only [f,ite_eq_left ha]
    rw [←ha]
    simp only [baseStep,Function.update_idem,mul_sum]
    apply sum_congr rfl
    intro b _
    by_cases hb : Function.update r i b=t
    · simp only [ha,hb,ite_true]
    · simp only [Prod.mk.injEq,ha,hb,true_and,ite_false,mul_zero]
  · simp only [f,ite_eq_right ha,mul_zero]
    apply sum_eq_zero
    intro b _
    simp only [Prod.mk.injEq,ha,false_and,ite_false]

noncomputable def localScan (guides : I → Finset I) (base : Finset I)
    (Q : ∀ x y,PairCapacity p x y) (s t : I → Ω) : ℝ :=
  (if s=t then (1:ℝ) else 0)/2+
    ((∑ x,∑ y ∈ guides x,pairStep p x y (Q x y) s t)+∑ i ∈ base,baseStep p i s t)/
      (2*(updateCount guides base:ℝ))

theorem localScan_kernel (hp : ∀ i a,0<p i a) (hs : ∀ i,∑ a,p i a=1)
    (guides : I → Finset I) (hself : ∀ x y,y∈guides x → x≠y) (base : Finset I)
    (Q : ∀ x y,PairCapacity p x y) (s t : I → Ω) :
    localScan p guides base Q s t=FiniteChain.kernel (density p) (scanFlow p guides base Q) s t := by
  have hpos : 0<density p s := prod_pos fun i _=>hp i (s i)
  have hpair : (∑ x,∑ y ∈ guides x,pairFlow p x y (Q x y) s t)=
      density p s*(∑ x,∑ y ∈ guides x,pairStep p x y (Q x y) s t) := by
    simp only [mul_sum]
    apply sum_congr rfl
    intro x _
    apply sum_congr rfl
    intro y hy
    exact local_pairFlow p hp hs x y (hself x y hy) (Q x y) s t
  have hbase : (∑ i ∈ base,baseFlow p i s t)=density p s*(∑ i ∈ base,baseStep p i s t) := by
    simp only [mul_sum]
    apply sum_congr rfl
    intro i hi
    exact local_baseFlow p hp hs i s t
  unfold FiniteChain.kernel scanFlow localScan
  rw [hpair,hbase]
  have hn := ne_of_gt hpos
  by_cases hst : s=t
  · subst t
    simp only [ite_true]
    field_simp [hn]
  · simp only [ite_eq_right hst,zero_div]
    field_simp [hn]
    ring

end MatchingFPRAS.Replica

namespace MatchingFPRAS.WeightedOptions

open Finset Variation

variable {J Ω B : Type*} [Fintype J] [DecidableEq J] [Fintype Ω] [DecidableEq Ω]

variable [Fintype B] [Nonempty B]

def drawThen (c : WeightedOptions J) (g : J → B → Ω) (k : ℕ)
    (bits : (Fin k → Bool) × B) : Ω := g (c.draw k bits.1) bits.2

omit [DecidableEq J] [Fintype Ω] [DecidableEq Ω] [Nonempty B] in
lemma drawThen_law [DecidableEq J] [Fintype Ω] [DecidableEq Ω] [Nonempty B]
    (c : WeightedOptions J) (g : J → B → Ω) (k : ℕ) :
    imageLaw (c.drawThen g k)=bind (imageLaw (c.draw k)) (fun j=>imageLaw (g j)) :=
  imageLaw_adaptive_pair (c.draw k) g

theorem drawThen_tv (c : WeightedOptions J) (g : J → B → Ω) (k : ℕ)
    (P : J → Ω → ℝ) (hn : ∀ j x,0≤P j x) (hs : ∀ j,∑ x,P j x=1)
    (e : ℝ) (he : ∀ j,tv (imageLaw (g j)) (P j)≤e) :
    tv (imageLaw (c.drawThen g k)) (bind (fun j=>(c.law j:ℝ)) P)≤
      2*c.size/(2:ℝ)^k+e := by
  rw [drawThen_law]
  calc
    _ ≤ tv (bind (imageLaw (c.draw k)) (fun j=>imageLaw (g j)))
        (bind (imageLaw (c.draw k)) P)+
        tv (bind (imageLaw (c.draw k)) P) (bind (fun j=>(c.law j:ℝ)) P) := tv_triangle _ _ _
    _ ≤ e+2*c.size/(2:ℝ)^k := add_le_add
      (bind_perturbation _ _ _ (imageLaw_nonneg _) (imageLaw_sum _) e he)
      ((bind_contraction _ _ P hn hs).trans (c.draw_tv k))
    _ = _ := add_comm _ _

end MatchingFPRAS.WeightedOptions

namespace MatchingFPRAS.Replica

open Finset Variation

variable {I Ω : Type*} [Fintype I] [DecidableEq I] [Fintype Ω] [DecidableEq Ω]

abbrev Update (I : Type*) := (I×I) ⊕ I

def updates (guides : I → Finset I) (base : Finset I) : Finset (Update I) :=
  ((univ : Finset (I×I)).filter (fun ij=>ij.2∈guides ij.1)).disjSum base

lemma updates_card (guides : I → Finset I) (base : Finset I) :
    (updates guides base).card=updateCount guides base := by
  simp only [updates,card_disjSum,updateCount]
  congr 1
  rw [card_eq_sum_ones,sum_filter,Fintype.sum_prod_type]
  apply sum_congr rfl
  intro x _
  simp

lemma updates_nonempty (guides : I → Finset I) (base : Finset I) (hb : base.Nonempty) :
    (updates guides base).Nonempty := by
  obtain ⟨i,hi⟩:=hb
  exact ⟨Sum.inr i,by simp [updates,hi]⟩

variable (p : I → Ω → ℝ)

noncomputable def updateKernel (Q : ∀ x y,PairCapacity p x y) (s : I → Ω) :
    Option (Update I) → (I → Ω) → ℝ
  | none,t => if s=t then 1 else 0
  | some (.inl ij),t => pairStep p ij.1 ij.2 (Q ij.1 ij.2) s t
  | some (.inr i),t => baseStep p i s t

lemma pairStep_nonneg (hp : ∀ i a,0<p i a) (x y : I) (Q : PairCapacity p x y) (s t : I → Ω) :
    0≤pairStep p x y Q s t := by
  apply sum_nonneg
  intro b _
  split_ifs
  · exact div_nonneg (Q.nonneg _ _) (mul_nonneg (hp _ _).le (hp _ _).le)
  · exact le_rfl

lemma pairStep_sum (hp : ∀ i a,0<p i a) (x y : I) (Q : PairCapacity p x y) (s : I → Ω) :
    ∑ t,pairStep p x y Q s t=1 := by
  unfold pairStep
  rw [sum_comm]
  simp only [sum_ite_eq,mem_univ,ite_true]
  rw [←sum_div,Q.row]
  exact div_self (mul_pos (hp _ _) (hp _ _)).ne'

lemma baseStep_nonneg (hp : ∀ i a,0≤p i a) (i : I) (s t : I → Ω) : 0≤baseStep p i s t := by
  apply sum_nonneg
  intro a _
  split_ifs
  · exact hp _ _
  · exact le_rfl

lemma baseStep_sum (hs : ∀ i,∑ a,p i a=1) (i : I) (s : I → Ω) :
    ∑ t,baseStep p i s t=1 := by
  unfold baseStep
  rw [sum_comm]
  simpa using hs i

lemma updateKernel_nonneg (hp : ∀ i a,0<p i a) (Q : ∀ x y,PairCapacity p x y)
    (s : I → Ω) (u : Option (Update I)) (t : I → Ω) : 0≤updateKernel p Q s u t := by
  cases u with
  | none => dsimp [updateKernel]; split_ifs <;> norm_num
  | some u => cases u with
    | inl ij => exact pairStep_nonneg p hp _ _ _ _ _
    | inr i => exact baseStep_nonneg p (fun i a=>(hp i a).le) _ _ _

lemma updateKernel_sum (hp : ∀ i a,0<p i a) (hs : ∀ i,∑ a,p i a=1)
    (Q : ∀ x y,PairCapacity p x y) (s : I → Ω) (u : Option (Update I)) :
    ∑ t,updateKernel p Q s u t=1 := by
  cases u with
  | none => simp [updateKernel]
  | some u => cases u with
    | inl ij => exact pairStep_sum p hp _ _ _ _
    | inr i => exact baseStep_sum p hs _ _

variable [LinearOrder (Update I)]

def scanChoice (guides : I → Finset I) (base : Finset I) (hb : base.Nonempty) :
    WeightedOptions (Option (Update I)) :=
  WeightedOptions.mix (1/2) (by norm_num) (by norm_num) (WeightedOptions.pure none)
    ((WeightedOptions.uniform (updates guides base) (updates_nonempty guides base hb)).map some)

lemma scanChoice_size (guides : I → Finset I) (base : Finset I) (hb : base.Nonempty) :
    (scanChoice guides base hb).size=1+updateCount guides base := by
  simp only [scanChoice,WeightedOptions.mix,WeightedOptions.map,WeightedOptions.pure,
    WeightedOptions.uniform,updates_card]

lemma scanChoice_law (guides : I → Finset I) (base : Finset I) (hb : base.Nonempty)
    (Q : ∀ x y,PairCapacity p x y) (s t : I → Ω) :
    bind (fun u=>((scanChoice guides base hb).law u:ℝ)) (updateKernel p Q s) t=
      localScan p guides base Q s t := by
  unfold Variation.bind
  rw [Fintype.sum_option]
  simp only [scanChoice,WeightedOptions.mix_law,WeightedOptions.pure_law,
    WeightedOptions.map_law]
  simp only [Option.some.injEq,reduceCtorEq,ite_false,sum_const_zero,mul_zero,zero_add]
  simp only [sum_ite_eq',mem_univ,ite_true,WeightedOptions.uniform_law]
  push_cast
  rw [updates_card]
  simp only [updateKernel,Fintype.sum_sum_type,Fintype.sum_prod_type,updates,
    inl_mem_disjSum,inr_mem_disjSum,
    mem_filter,mem_univ,true_and]
  unfold localScan
  simp only [apply_ite,Rat.cast_inv,Rat.cast_natCast,Rat.cast_zero,
    ite_mul,zero_mul,mul_zero,sum_ite_mem,univ_inter]
  simp only [←mul_sum]
  by_cases h : s=t <;> simp only [h,ite_true,ite_false] <;> ring



open Finset Variation

variable {I Ω B : Type*} [Fintype I] [DecidableEq I] [Fintype Ω] [DecidableEq Ω]

variable [Fintype B] [Nonempty B]

def localSampler (pair : I → I → (Ω×Ω) → B → Ω×Ω) (refresh : I → B → Ω)
    (s : I → Ω) : Option (Update I) → B → (I → Ω)
  | none,_ => s
  | some (.inl ij),b => replacePair ij.1 ij.2 s (pair ij.1 ij.2 (s ij.1,s ij.2) b)
  | some (.inr i),b => Function.update s i (refresh i b)

lemma localSampler_tv (p : I → Ω → ℝ) (Q : ∀ x y,PairCapacity p x y)
    (pair : I → I → (Ω×Ω) → B → Ω×Ω) (refresh : I → B → Ω) (e : ℝ) (he : 0≤e)
    (hp : ∀ x y a,tv (imageLaw (pair x y a)) (fun b=>(Q x y).q a b/(p x a.1*p y a.2))≤e)
    (hr : ∀ i,tv (imageLaw (refresh i)) (p i)≤e) (s : I → Ω) (u : Option (Update I)) :
    tv (imageLaw (localSampler pair refresh s u)) (updateKernel p Q s u)≤e := by
  cases u with
  | none =>
    have h : imageLaw (localSampler pair refresh s none)=updateKernel p Q s none := by
      funext t
      simp only [imageLaw,push,localSampler,updateKernel]
      by_cases hst : s=t
      · subst t
        simpa using uniform_sum (A:=B)
      · simp [hst]
    rw [h,tv_self]
    exact he
  | some u => cases u with
    | inl ij =>
      have hk : updateKernel p Q s (some (.inl ij))=
          push (fun b=>(Q ij.1 ij.2).q (s ij.1,s ij.2) b/(p ij.1 (s ij.1)*p ij.2 (s ij.2)))
            (replacePair ij.1 ij.2 s) := by
        funext t
        simp only [updateKernel,pairStep,push]
      rw [hk]
      change tv (imageLaw ((replacePair ij.1 ij.2 s) ∘ pair ij.1 ij.2 (s ij.1,s ij.2)))
        (push (fun b=>(Q ij.1 ij.2).q (s ij.1,s ij.2) b/(p ij.1 (s ij.1)*p ij.2 (s ij.2)))
          (replacePair ij.1 ij.2 s))≤e
      rw [imageLaw_comp]
      exact (push_contraction _ _ _).trans (hp _ _ _)
    | inr i =>
      have hk : updateKernel p Q s (some (.inr i))=push (p i) (Function.update s i) := by
        funext t
        simp only [updateKernel,baseStep,push]
        apply sum_congr rfl
        intro a _
        congr 1
      rw [hk]
      change tv (imageLaw ((Function.update s i) ∘ refresh i)) (push (p i) (Function.update s i))≤e
      rw [imageLaw_comp]
      exact (push_contraction _ _ _).trans (hr i)

variable [LinearOrder (Update I)]

def sampleScan (guides : I → Finset I) (base : Finset I) (hb : base.Nonempty)
    (pair : I → I → (Ω×Ω) → B → Ω×Ω) (refresh : I → B → Ω) (k : ℕ) (s : I → Ω) :
    ((Fin k → Bool) × B) → (I → Ω) :=
  (scanChoice guides base hb).drawThen (localSampler pair refresh s) k

theorem sampleScan_tv (p : I → Ω → ℝ) (hp : ∀ i a,0<p i a) (hs : ∀ i,∑ a,p i a=1)
    (guides : I → Finset I) (hself : ∀ x y,y∈guides x → x≠y)
    (base : Finset I) (hb : base.Nonempty) (Q : ∀ x y,PairCapacity p x y)
    (pair : I → I → (Ω×Ω) → B → Ω×Ω) (refresh : I → B → Ω) (e : ℝ) (he : 0≤e)
    (hpr : ∀ x y a,tv (imageLaw (pair x y a)) (fun b=>(Q x y).q a b/(p x a.1*p y a.2))≤e)
    (hr : ∀ i,tv (imageLaw (refresh i)) (p i)≤e) (k : ℕ) (s : I → Ω) :
    tv (imageLaw (sampleScan guides base hb pair refresh k s))
      (FiniteChain.kernel (density p) (scanFlow p guides base Q) s)≤
      2*(1+updateCount guides base)/(2:ℝ)^k+e := by
  have ht := WeightedOptions.drawThen_tv (scanChoice guides base hb) (localSampler pair refresh s)
    k (updateKernel p Q s) (updateKernel_nonneg p hp Q s) (updateKernel_sum p hp hs Q s)
    e (localSampler_tv p Q pair refresh e he hpr hr s)
  have hl : Variation.bind (fun u=>((scanChoice guides base hb).law u:ℝ)) (updateKernel p Q s)=
      FiniteChain.kernel (density p) (scanFlow p guides base Q) s := by
    funext t
    rw [scanChoice_law,localScan_kernel p hp hs guides hself base Q]
  rw [hl,scanChoice_size] at ht
  simpa only [Nat.cast_add,Nat.cast_one,sampleScan] using ht

end MatchingFPRAS.Replica

namespace MatchingFPRAS.Variation

open Finset

variable {Ω B : Type*} [Fintype Ω] [DecidableEq Ω] [Fintype B] [Nonempty B]

def runSample (g : Ω → B → Ω) (z : Ω) : (n : ℕ) → (Fin n → B) → Ω
  | 0,_ => z
  | n+1,bits => g (runSample g z n (Fin.init bits)) (bits (Fin.last n))

omit [DecidableEq Ω] [Nonempty B] in
lemma runSample_law [DecidableEq Ω] [Nonempty B] (g : Ω → B → Ω) (z : Ω) (n : ℕ) :
    imageLaw (runSample g z (n+1))=bind (imageLaw (runSample g z n)) (fun s=>imageLaw (g s)) := by
  let e : ((Fin n → B) × B) ≃ (Fin (n+1) → B) :=
    (Equiv.prodComm _ _).trans (Fin.snocEquiv (fun _=>B))
  have h : runSample g z (n+1) ∘ e=(fun u=>g (runSample g z n u.1) u.2) := by
    funext u
    change g (runSample g z n (Fin.init (Fin.snoc (α:=fun _ : Fin (n+1)=>B) u.1 u.2)))
      (Fin.snoc (α:=fun _ : Fin (n+1)=>B) u.1 u.2 (Fin.last n))=_
    simp only [Fin.init_snoc,Fin.snoc_last]
  rw [←imageLaw_comp_equiv e,h]
  exact imageLaw_adaptive_pair (runSample g z n) g

theorem runSample_walk_tv (π : Ω → ℝ) (C : Ω → Ω → ℝ)
    (hn : ∀ s t,0≤FiniteChain.kernel π C s t) (hs : ∀ s,∑ t,FiniteChain.kernel π C s t=1)
    (g : Ω → B → Ω) (d : ℝ)
    (hd : ∀ s,tv (imageLaw (g s)) (FiniteChain.kernel π C s)≤d) (z : Ω) (n : ℕ) :
    tv (imageLaw (runSample g z n)) (FiniteChain.walk π C z n)≤n*d := by
  induction n with
  | zero =>
    have h : imageLaw (runSample g z 0)=FiniteChain.walk π C z 0 := by
      funext b
      by_cases hb : b=z
      · subst b; simp [imageLaw,push,uniform,runSample,FiniteChain.walk]
      · simp [imageLaw,push,runSample,FiniteChain.walk,hb,Ne.symm hb]
    rw [h,tv_self]
    simp
  | succ n ih =>
    rw [runSample_law]
    change tv (bind _ _) (bind _ _)≤_
    calc
      _ ≤ tv (bind (imageLaw (runSample g z n)) (fun s=>imageLaw (g s)))
          (bind (imageLaw (runSample g z n)) (FiniteChain.kernel π C))+
          tv (bind (imageLaw (runSample g z n)) (FiniteChain.kernel π C))
            (bind (FiniteChain.walk π C z n) (FiniteChain.kernel π C)) := tv_triangle _ _ _
      _ ≤ d+n*d := add_le_add
        (bind_perturbation _ _ _ (imageLaw_nonneg _) (imageLaw_sum _) d hd)
        ((bind_contraction _ _ _ hn hs).trans ih)
      _ = _ := by push_cast; ring

end MatchingFPRAS.Variation

end OAI