File size: 26,107 Bytes
9b6e014
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
import Mathlib

namespace OAI

/-! Dyadic Laws. -/

noncomputable section



 

 

open scoped BigOperators

namespace Contingency

 

structure DyadicLaw (α : Type*) [Fintype α] where
  bits : ℕ
  count : α → ℕ
  total : ∑ z, count z = 2 ^ bits

namespace DyadicLaw

variable {α β γ : Type*} [Fintype α] [Fintype β] [Fintype γ]

 
def pure [DecidableEq α] (z : α) (R : ℕ := 0) : DyadicLaw α where
  bits := R
  count x := if x = z then 2 ^ R else 0
  total := by simp

 

def bind (p : DyadicLaw α) (q : α → DyadicLaw β) (S : ℕ)
    (hq : ∀ x, (q x).bits = S) : DyadicLaw β where
  bits := p.bits + S
  count y := ∑ x, p.count x * (q x).count y
  total := by
    rw [Finset.sum_comm]
    simp_rw [← Finset.mul_sum, DyadicLaw.total, hq]
    rw [← Finset.sum_mul, p.total, pow_add]

 
def map [DecidableEq β] (f : α → β) (p : DyadicLaw α) : DyadicLaw β :=
  p.bind (fun x => pure (f x)) 0 (fun _ => rfl)

@[simp] theorem map_bits [DecidableEq β] (f : α → β) (p : DyadicLaw α) :
    (p.map f).bits = p.bits := by simp [map, bind]

@[simp] theorem map_count [DecidableEq β] (f : α → β) (p : DyadicLaw α) (y : β) :
    (p.map f).count y = ∑ x, if f x = y then p.count x else 0 := by
  simp only [map, bind, pure, pow_zero, mul_ite, mul_one, mul_zero]
  apply Finset.sum_congr rfl
  intro x _
  by_cases h : f x = y
  · simp [h]
  · simp [h, Ne.symm h]

 
def walk [DecidableEq α] (q : α → DyadicLaw α) (S : ℕ)
    (hq : ∀ x, (q x).bits = S) (x₀ : α) : ℕ → DyadicLaw α
  | 0 => pure x₀
  | t + 1 => (walk q S hq x₀ t).bind q S hq

@[simp] theorem walk_bits [DecidableEq α] (q : α → DyadicLaw α) (S : ℕ)
    (hq : ∀ x, (q x).bits = S) (x₀ : α) (t : ℕ) :
    (walk q S hq x₀ t).bits = t * S := by
  induction t with
  | zero => simp [walk, pure]
  | succ t ih => simp [walk, bind, ih, Nat.succ_mul]

def probability (p : DyadicLaw α) (x : α) : ℚ := p.count x / 2 ^ p.bits

@[simp] theorem probability_nonneg (p : DyadicLaw α) (x : α) :
    0 ≤ p.probability x := by
  unfold probability
  positivity

@[simp] theorem probability_total (p : DyadicLaw α) :
    ∑ x, p.probability x = 1 := by
  unfold probability
  rw [← Finset.sum_div]
  norm_cast
  rw [p.total]
  norm_num

end DyadicLaw

 
def bitWordLaw (r : ℕ) : DyadicLaw (Fin (2 ^ r)) where
  bits := r
  count _ := 1
  total := by simp

 
def roundedOffset (w : ℕ) (hw : 0 < w) (r : ℕ) (z : Fin (2 ^ r)) : Fin w :=
  ⟨w * z.val / 2 ^ r, by
    apply (Nat.div_lt_iff_lt_mul (pow_pos (by omega) _)).mpr
    exact Nat.mul_lt_mul_of_pos_left z.isLt hw⟩

 
def offsetLaw (w : ℕ) (hw : 0 < w) (r : ℕ) : DyadicLaw (Fin w) :=
  (bitWordLaw r).map (roundedOffset w hw r)

 
def offsetNumerator (w r u : ℕ) : ℕ :=
  ((u + 1) * 2 ^ r) ⌈/⌉ w - (u * 2 ^ r) ⌈/⌉ w

theorem offset_preimage (w : ℕ) (hw : 0 < w) (r u : ℕ) (hu : u < w) :
    {z ∈ Finset.range (2 ^ r) | w * z / 2 ^ r = u} =
      Finset.Ico ((u * 2 ^ r) ⌈/⌉ w) (((u + 1) * 2 ^ r) ⌈/⌉ w) := by
  ext z
  have hpow : 0 < 2 ^ r := pow_pos (by omega) _
  have hlo : (u * 2 ^ r) ⌈/⌉ w ≤ z ↔ u ≤ w * z / 2 ^ r := by
    rw [ceilDiv_le_iff_le_mul hw, Nat.le_div_iff_mul_le hpow]
  have hhi : z < ((u + 1) * 2 ^ r) ⌈/⌉ w ↔ w * z / 2 ^ r < u + 1 := by
    rw [lt_iff_not_ge, ceilDiv_le_iff_le_mul hw, not_le,
      Nat.div_lt_iff_lt_mul hpow]
  simp only [Finset.mem_filter, Finset.mem_range, Finset.mem_Ico, hlo, hhi]
  constructor
  · rintro ⟨_, heq⟩
    omega
  · rintro ⟨h1, h2⟩
    have heq : w * z / 2 ^ r = u := by omega
    refine ⟨?_, heq⟩
    have hlt : w * z < w * 2 ^ r :=
      (Nat.div_lt_iff_lt_mul hpow).mp (by omega)
    exact (Nat.mul_lt_mul_left hw).mp hlt

theorem offsetLaw_count (w : ℕ) (hw : 0 < w) (r : ℕ) (u : Fin w) :
    (offsetLaw w hw r).count u = offsetNumerator w r u.val := by
  rw [offsetLaw, DyadicLaw.map_count]
  simp only [bitWordLaw, roundedOffset, Fin.ext_iff]
  rw [Fin.sum_univ_eq_sum_range (fun z : ℕ =>
    if w * z / 2 ^ r = u.val then (1 : ℕ) else 0) (2 ^ r)]
  rw [← Finset.card_filter, offset_preimage w hw r u u.isLt, Nat.card_Ico]
  rfl

 

def stoppedNumerator {α : Type*} [DecidableEq α] (R φ : ℕ) (a : α → ℕ)
    (fallback z : α) : ℕ → ℕ
  | 0 => if z = fallback then 1 else 0
  | J + 1 => a z * 2 ^ (R * J) + φ * stoppedNumerator R φ a fallback z J

theorem stoppedNumerator_total {α : Type*} [Fintype α] [DecidableEq α]
    (R φ : ℕ) (a : α → ℕ) (fallback : α) (ht : (∑ z, a z) + φ = 2 ^ R)
    (J : ℕ) : ∑ z, stoppedNumerator R φ a fallback z J = 2 ^ (R * J) := by
  induction J with
  | zero => simp [stoppedNumerator]
  | succ J ih =>
    simp only [stoppedNumerator, Finset.sum_add_distrib, ← Finset.sum_mul,
      ← Finset.mul_sum, ih]
    rw [← add_mul, ht, Nat.mul_succ, pow_add]
    ring

 

def stoppedLaw {α : Type*} [Fintype α] [DecidableEq α]
    (trial : DyadicLaw (Option α)) (fallback : α) (J : ℕ) : DyadicLaw α where
  bits := trial.bits * J
  count z := stoppedNumerator trial.bits (trial.count none)
    (fun z => trial.count (some z)) fallback z J
  total := stoppedNumerator_total _ _ _ _ (by
    have ht := trial.total
    rw [Fintype.sum_option] at ht
    omega) J

 
theorem stoppedNumerator_closed {α : Type*} [DecidableEq α]
    (R φ : ℕ) (a : α → ℕ) (fallback z : α) (J : ℕ) :
    stoppedNumerator R φ a fallback z J =
      (∑ j ∈ Finset.range J, φ ^ j * a z * 2 ^ (R * (J - j - 1))) +
        (if z = fallback then φ ^ J else 0) := by
  induction J with
  | zero => simp [stoppedNumerator]
  | succ J ih =>
    rw [stoppedNumerator, ih, Finset.sum_range_succ']
    simp only [pow_zero, one_mul, Nat.sub_zero, Nat.add_sub_cancel]
    have hs : (∑ j ∈ Finset.range J,
        φ ^ (j + 1) * a z * 2 ^ (R * (J + 1 - (j + 1) - 1))) =
        φ * ∑ j ∈ Finset.range J, φ ^ j * a z * 2 ^ (R * (J - j - 1)) := by
      rw [Finset.mul_sum]
      apply Finset.sum_congr rfl
      intro j hj
      rw [Nat.add_sub_add_right, pow_succ]
      ring
    rw [hs]
    by_cases hz : z = fallback <;> simp [hz, pow_succ] <;> ring

 
def residualNumerator (M R D a : ℕ) : ℤ :=
  (2 : ℤ) ^ (R + D) - M * ((2 : ℤ) ^ D - 1) * a

theorem residual_integers {α : Type*} [Fintype α] (R D : ℕ) (a : α → ℕ)
    (htotal : ∑ z, a z = 2 ^ R)
    (hdom : ∀ z, (Fintype.card α : ℤ) * ((2 : ℤ) ^ D - 1) * a z ≤
      (2 : ℤ) ^ (R + D)) :
    (∀ z, 0 ≤ residualNumerator (Fintype.card α) R D (a z)) ∧
    (∑ z, residualNumerator (Fintype.card α) R D (a z) =
      (Fintype.card α : ℤ) * 2 ^ R) := by
  constructor
  · intro z
    exact sub_nonneg.mpr (hdom z)
  · have ht : ∑ z, (a z : ℤ) = (2 : ℤ) ^ R := by exact_mod_cast htotal
    simp only [residualNumerator, Finset.sum_sub_distrib, Finset.sum_const,
      Finset.card_univ, nsmul_eq_mul, ← Finset.mul_sum]
    rw [ht, pow_add]
    ring

end Contingency

 

 

open scoped BigOperators
namespace Contingency
noncomputable section

structure Law (α : Type*) [Fintype α] where
  mass : α → ℝ
  nonneg : ∀ x,0 ≤ mass x
  total : ∑ x,mass x=1

namespace Law
variable {α β γ : Type*} [Fintype α] [Fintype β] [Fintype γ]

@[ext] theorem ext {p r : Law α} (h : p.mass=r.mass) : p=r := by cases p; cases r; cases h; rfl

def pure (x : α) : Law α := by
  classical
  exact ⟨fun y => if x=y then 1 else 0,fun y => by split_ifs <;> norm_num,by simp⟩

def bind (p : Law α) (k : α → Law β) : Law β where
  mass y := ∑ x,p.mass x*(k x).mass y
  nonneg y := Finset.sum_nonneg fun x _ => mul_nonneg (p.nonneg x) ((k x).nonneg y)
  total := by rw [Finset.sum_comm]; simp only [← Finset.mul_sum,Law.total,mul_one]

def map (f : α → β) (p : Law α) : Law β := p.bind (fun x => pure (f x))

def tv (p r : Law α) : ℝ := (∑ x,|p.mass x-r.mass x|)/2

theorem tv_nonneg (p r : Law α) : 0 ≤ p.tv r := by unfold tv; positivity
@[simp] theorem tv_self (p : Law α) : p.tv p=0 := by simp [tv]
theorem tv_symm (p r : Law α) : p.tv r=r.tv p := by simp only [tv,abs_sub_comm]

theorem tv_triangle (p r s : Law α) : p.tv s ≤ p.tv r+r.tv s := by
  unfold tv
  rw [← add_div,← Finset.sum_add_distrib]
  apply div_le_div_of_nonneg_right _ (by norm_num)
  exact Finset.sum_le_sum fun x _ => abs_sub_le _ _ _

theorem tv_le_one (p r : Law α) : p.tv r ≤ 1 := by
  have h : (∑ x,|p.mass x-r.mass x|) ≤ 2 := by
    calc
      _ ≤ ∑ x,(p.mass x+r.mass x) := Finset.sum_le_sum fun x _ => by
        rw [abs_le]; constructor <;> linarith [p.nonneg x,r.nonneg x]
      _ = 2 := by rw [Finset.sum_add_distrib,p.total,r.total]; norm_num
  unfold tv; linarith

theorem tv_bind_left (p r : Law α) (k : α → Law β) :
    (p.bind k).tv (r.bind k) ≤ p.tv r := by
  unfold tv
  apply div_le_div_of_nonneg_right _ (by norm_num)
  calc
    _ = ∑ y,|∑ x,(p.mass x-r.mass x)*(k x).mass y| := by
      simp only [bind,sub_mul,Finset.sum_sub_distrib]
    _ ≤ ∑ y,∑ x,|p.mass x-r.mass x| *(k x).mass y := by
      apply Finset.sum_le_sum; intro y _
      have hh := Finset.abs_sum_le_sum_abs (s:=Finset.univ) (f:=fun x => (p.mass x-r.mass x)*(k x).mass y)
      simpa only [abs_mul,abs_of_nonneg ((k _).nonneg _)] using hh
    _ = _ := by rw [Finset.sum_comm]; simp only [← Finset.mul_sum,Law.total,mul_one]

theorem tv_bind_right (p : Law α) (k l : α → Law β) :
    (p.bind k).tv (p.bind l) ≤ ∑ x,p.mass x*(k x).tv (l x) := by
  unfold tv
  rw [show (∑ x,p.mass x*((∑ y,|(k x).mass y-(l x).mass y|)/2))=
    (∑ x,∑ y,p.mass x*|(k x).mass y-(l x).mass y|)/2 by simp only [mul_div_assoc,Finset.mul_sum,Finset.sum_div]]
  apply div_le_div_of_nonneg_right _ (by norm_num)
  calc
    _ = ∑ y,|∑ x,p.mass x*((k x).mass y-(l x).mass y)| := by simp only [bind,mul_sub,Finset.sum_sub_distrib]
    _ ≤ ∑ y,∑ x,p.mass x*|(k x).mass y-(l x).mass y| := by
      apply Finset.sum_le_sum; intro y _
      have hh := Finset.abs_sum_le_sum_abs (s:=Finset.univ) (f:=fun x => p.mass x*((k x).mass y-(l x).mass y))
      simpa only [abs_mul,abs_of_nonneg (p.nonneg _)] using hh
    _ = _ := Finset.sum_comm

theorem tv_bind_right_bound (p : Law α) (k l : α → Law β) (δ : ℝ)
    (hδ : ∀ x,(k x).tv (l x) ≤ δ) : (p.bind k).tv (p.bind l) ≤ δ := by
  apply (tv_bind_right p k l).trans
  calc
    _ ≤ ∑ x,p.mass x*δ := Finset.sum_le_sum fun x _ => mul_le_mul_of_nonneg_left (hδ x) (p.nonneg x)
    _ = δ := by rw [← Finset.sum_mul,p.total,one_mul]

theorem tv_bind (p r : Law α) (k l : α → Law β) (δ : ℝ)
    (hδ : ∀ x,(k x).tv (l x) ≤ δ) : (p.bind k).tv (r.bind l) ≤ p.tv r+δ := by
  exact (tv_triangle _ (r.bind k) _).trans (add_le_add (tv_bind_left p r k) (tv_bind_right_bound r k l δ hδ))

theorem tv_map (f : α → β) (p r : Law α) : (p.map f).tv (r.map f) ≤ p.tv r :=
  tv_bind_left _ _ _

def first (p : Law (Option α)) (fallback : α) : ℕ → Law α
  | 0 => pure fallback
  | J+1 => p.bind (fun o => match o with | none => first p fallback J | some x => pure x)

 
theorem first_tv (p r : Law (Option α)) (fallback : α) (J : ℕ) :
    (first p fallback J).tv (first r fallback J) ≤ J*p.tv r := by
  induction J with
  | zero => simp [first]
  | succ J ih =>
    have h := tv_bind p r
      (fun o => match o with | none => first p fallback J | some x => pure x)
      (fun o => match o with | none => first r fallback J | some x => pure x)
      (J*p.tv r) (by intro o; cases o; exact ih; simpa only [tv_self] using mul_nonneg (Nat.cast_nonneg J) (tv_nonneg p r))
    simpa only [first,Nat.cast_add,Nat.cast_one,add_mul,one_mul,add_comm] using h

@[simp] theorem bind_pure_mass (p : Law α) (y : α) : (p.bind pure).mass y=p.mass y := by
  classical
  simp [bind,pure]

theorem first_mass (p : Law (Option α)) (fallback : α) (u : Law α)
    (h : ∀ x,p.mass (some x)=(1-p.mass none)*u.mass x) (J : ℕ) (x : α) :
    (first p fallback J).mass x=(1-p.mass none^J)*u.mass x+p.mass none^J*(pure fallback).mass x := by
  induction J with
  | zero => simp [first]
  | succ J ih =>
    change (∑ o : Option α,p.mass o*(match o with | none => first p fallback J | some y => pure y).mass x)=_
    rw [Fintype.sum_option]
    simp only [h]
    have he : (∑ y,(1-p.mass none)*u.mass y*(pure y).mass x)=(1-p.mass none)*u.mass x := by
      simp only [mul_assoc]
      rw [← Finset.mul_sum]
      change (1-p.mass none)*(u.bind pure).mass x=_
      rw [bind_pure_mass]
    rw [he,ih,pow_succ]
    ring

theorem first_ideal_tv (p : Law (Option α)) (fallback : α) (u : Law α)
    (h : ∀ x,p.mass (some x)=(1-p.mass none)*u.mass x) (J : ℕ) :
    (first p fallback J).tv u ≤ p.mass none^J := by
  have he : (first p fallback J).tv u=p.mass none^J*(pure fallback).tv u := by
    unfold tv
    simp only [first_mass p fallback u h J]
    have hh (x) : (1-p.mass none^J)*u.mass x+p.mass none^J*(pure fallback).mass x-u.mass x=
        p.mass none^J*((pure fallback).mass x-u.mass x) := by ring
    simp only [hh,abs_mul,abs_of_nonneg (pow_nonneg (p.nonneg none) J),← Finset.mul_sum]
    ring
  rw [he]
  have hh := mul_le_mul_of_nonneg_left (tv_le_one (pure fallback) u) (pow_nonneg (p.nonneg none) J)
  simpa only [mul_one] using hh

end Law
end
end Contingency

 

 

open scoped BigOperators
namespace Contingency
noncomputable section
namespace DyadicLaw
variable {α β : Type*} [Fintype α] [Fintype β]
def toLaw (p : DyadicLaw α) : Law α where
  mass x := (p.count x:ℝ)/2^p.bits
  nonneg x := by positivity
  total := by
    rw [← Finset.sum_div]
    have hh : (∑ x,(p.count x:ℝ))=(2:ℝ)^p.bits := by exact_mod_cast p.total
    rw [hh]
    exact div_self (by positivity)

theorem toLaw_bind (p : DyadicLaw α) (k : α → DyadicLaw β) (S : ℕ)
    (hk : ∀ x,(k x).bits=S) : (p.bind k S hk).toLaw=p.toLaw.bind (fun x => (k x).toLaw) := by
  apply Law.ext
  funext y
  simp only [toLaw,bind,Law.bind,Nat.cast_sum,Nat.cast_mul,pow_add,hk,Finset.sum_div]
  apply Finset.sum_congr rfl
  intro x _
  ring
end DyadicLaw

namespace Law

def uniform (α : Type*) [Fintype α] [Nonempty α] : Law α where
  mass _ := 1/Fintype.card α
  nonneg _ := by positivity
  total := by simp only [Finset.sum_const,Finset.card_univ,nsmul_eq_mul]; field_simp

end Law

theorem ceilDiv_error (a w : ℕ) (hw : 0<w) :
    (a:ℝ) ≤ w*(a⌈/⌉w:ℕ) ∧ w*(a⌈/⌉w:ℕ) ≤ (a:ℝ)+w := by
  constructor
  · exact_mod_cast (ceilDiv_le_iff_le_mul hw).mp (le_refl (a⌈/⌉w))
  · have hh : w*(a⌈/⌉w)≤a+w := by
      rw [Nat.ceilDiv_eq_add_pred_div,Nat.mul_comm]
      exact (Nat.div_mul_le_self (a+w-1) w).trans (Nat.sub_le _ _)
    exact_mod_cast hh

theorem offsetNumerator_error (w : ℕ) (hw : 0<w) (r u : ℕ) :
    |(w:ℝ)*offsetNumerator w r u-(2:ℝ)^r| ≤ w := by
  have hmono : (u*2^r)⌈/⌉w ≤ ((u+1)*2^r)⌈/⌉w :=
    (gc_mul_ceilDiv hw).monotone_l (Nat.mul_le_mul_right (2^r) (Nat.le_succ u))
  rw [offsetNumerator,Nat.cast_sub hmono,abs_le]
  have hlo := ceilDiv_error (u*2^r) w hw
  have hhi := ceilDiv_error ((u+1)*2^r) w hw
  push_cast at hlo hhi
  constructor <;> nlinarith only [hlo.1,hlo.2,hhi.1,hhi.2]

theorem offsetLaw_point_error (w : ℕ) (hw : 0<w) (r : ℕ) (u : Fin w) :
    |(offsetLaw w hw r).toLaw.mass u-1/(w:ℝ)| ≤ 1/(2:ℝ)^r := by
  have hw0 : (0:ℝ)<w := by exact_mod_cast hw
  have hQ : (0:ℝ)<2^r := by positivity
  change |((offsetLaw w hw r).count u:ℝ)/(2:ℝ)^((offsetLaw w hw r).bits)-1/(w:ℝ)|≤_
  rw [offsetLaw_count]
  change |(offsetNumerator w r u:ℝ)/(2:ℝ)^r-1/(w:ℝ)|≤_
  calc
    _ = |(w:ℝ)*offsetNumerator w r u-(2:ℝ)^r|/((w:ℝ)*(2:ℝ)^r) := by
      rw [← abs_of_pos (mul_pos hw0 hQ),← abs_div]
      congr 1
      field_simp
    _ ≤ (w:ℝ)/((w:ℝ)*(2:ℝ)^r) := div_le_div_of_nonneg_right (offsetNumerator_error w hw r u) (by positivity)
    _ = _ := by field_simp

theorem offsetLaw_tv (w : ℕ) (hw : 0<w) (r : ℕ) :
    letI : Nonempty (Fin w) := ⟨⟨0,hw⟩⟩
    (offsetLaw w hw r).toLaw.tv (Law.uniform (Fin w)) ≤ (w:ℝ)/(2:ℝ)^r := by
  unfold Law.tv Law.uniform
  simp only [Fintype.card_fin]
  have hh : (∑ u : Fin w,|(offsetLaw w hw r).toLaw.mass u-1/(w:ℝ)|) ≤ (w:ℝ)/(2:ℝ)^r := by
    calc
      _ ≤ ∑ _ : Fin w,1/(2:ℝ)^r := Finset.sum_le_sum fun u _ => offsetLaw_point_error w hw r u
      _ = _ := by simp [div_eq_mul_inv]
  have hn : (0:ℝ)≤(w:ℝ)/(2:ℝ)^r := by positivity
  linarith

 
theorem rounded_offset_accuracy (w h d : ℕ) (hw : 0<w) :
    letI : Nonempty (Fin w) := ⟨⟨0,hw⟩⟩
    (offsetLaw w hw (Nat.clog 2 (w+1)+4*(h+d))).toLaw.tv (Law.uniform (Fin w)) ≤
      (2:ℝ)^(-((4*(h+d):ℕ):ℤ)) := by
  apply (offsetLaw_tv w hw _).trans
  have hh : (w:ℝ) ≤ 2^Nat.clog 2 (w+1) := by
    exact_mod_cast ((Nat.le_succ w).trans (Nat.le_pow_clog (by norm_num) (w+1)))
  rw [pow_add]
  calc
    _ ≤ (2:ℝ)^Nat.clog 2 (w+1)/((2:ℝ)^Nat.clog 2 (w+1)*(2:ℝ)^(4*(h+d))) := by gcongr
    _ = _ := by rw [zpow_neg,zpow_natCast]; field_simp

end
end Contingency

 

 

open scoped BigOperators
namespace Contingency.Law
noncomputable section
variable {ι : Type*} [Fintype ι] [DecidableEq ι] {α : ι → Type*} [∀ i,Fintype (α i)]

def pi (p : ∀ i,Law (α i)) : Law (∀ i,α i) where
  mass x := ∏ i,(p i).mass (x i)
  nonneg x := Finset.prod_nonneg fun i _ => (p i).nonneg (x i)
  total := by
    rw [← Fintype.prod_sum]
    simp only [Law.total,Finset.prod_const_one]

theorem tv_overlap {β : Type*} [Fintype β] (p r : Law β) :
    p.tv r = 1-∑ x,min (p.mass x) (r.mass x) := by
  have hh : (∑ x,|p.mass x-r.mass x|)=2-2*(∑ x,min (p.mass x) (r.mass x)) := by
    calc
      _ = ∑ x,(p.mass x+r.mass x-2*min (p.mass x) (r.mass x)) := by
        apply Finset.sum_congr rfl; intro x _
        rcases le_total (p.mass x) (r.mass x) with h|h
        · rw [min_eq_left h,abs_of_nonpos (sub_nonpos.mpr h)]; ring
        · rw [min_eq_right h,abs_of_nonneg (sub_nonneg.mpr h)]; ring
      _ = _ := by rw [Finset.sum_sub_distrib,Finset.sum_add_distrib,← Finset.mul_sum,p.total,r.total]; norm_num
  unfold tv; rw [hh]; ring

omit [Fintype ι] in
theorem deficit_prod_le_sum (f : ι → ℝ) (hf : ∀ i,0≤f i ∧ f i≤1) (s : Finset ι) :
    1-∏ i∈s,f i ≤ ∑ i∈s,(1-f i) := by
  classical
  induction s using Finset.induction_on with
  | empty => simp
  | @insert a s ha ih =>
    rw [Finset.prod_insert ha,Finset.sum_insert ha]
    have hp : (∏ i∈s,f i)≤1 := Finset.prod_le_one₀ (fun index _ => (hf index).1) (fun index _ => (hf index).2)
    have hn := mul_nonneg (by linarith [(hf a).2] : 0≤1-f a) (by linarith : 0≤1-∏ i∈s,f i)
    nlinarith

 
theorem tv_pi (p r : ∀ i,Law (α i)) :
    (pi p).tv (pi r) ≤ ∑ i,(p i).tv (r i) := by
  classical
  have hs : (∏ i,∑ x,min ((p i).mass x) ((r i).mass x)) ≤
      ∑ x,min ((pi p).mass x) ((pi r).mass x) := by
    rw [Fintype.prod_sum]
    apply Finset.sum_le_sum
    intro x _
    apply le_min
    · exact Finset.prod_le_prod₀ (fun index _ => le_min ((p index).nonneg _) ((r index).nonneg _))
        (fun index _ => min_le_left _ _)
    · exact Finset.prod_le_prod₀ (fun index _ => le_min ((p index).nonneg _) ((r index).nonneg _))
        (fun index _ => min_le_right _ _)
  have he (i) : (∑ x,min ((p i).mass x) ((r i).mass x))=1-(p i).tv (r i) := by rw [tv_overlap]; ring
  simp only [he] at hs
  have hb := deficit_prod_le_sum (fun i => 1-(p i).tv (r i)) (fun i =>
    ⟨by linarith [tv_le_one (p i) (r i)],by linarith [tv_nonneg (p i) (r i)]⟩) Finset.univ
  simp only [sub_sub_cancel] at hb
  rw [tv_overlap]
  linarith

variable {β γ : Type*} [Fintype β] [Fintype γ]
def prod (p : Law β) (r : Law γ) : Law (β×γ) where
  mass x := p.mass x.1*r.mass x.2
  nonneg x := mul_nonneg (p.nonneg _) (r.nonneg _)
  total := by rw [Fintype.sum_prod_type]; simp only [← Finset.mul_sum,r.total,mul_one,p.total]

theorem prod_eq_bind (p : Law β) (r : Law γ) : p.prod r=p.bind (fun x => r.map (x,·)) := by
  classical
  apply Law.ext; funext y
  simp only [prod,bind,map,pure,Prod.ext_iff]
  have hh (x : β) : (∑ z : γ,r.mass z*(if x=y.1 ∧ z=y.2 then 1 else 0))=
      if x=y.1 then r.mass y.2 else 0 := by
    by_cases h : x=y.1 <;> simp [h]
  simp only [hh]
  simp

theorem tv_prod (p p' : Law β) (r r' : Law γ) :
    (p.prod r).tv (p'.prod r') ≤ p.tv p'+r.tv r' := by
  rw [prod_eq_bind,prod_eq_bind]
  exact tv_bind p p' _ _ _ (fun x => tv_map (x,·) r r')

end
end Contingency.Law

 

 

open scoped BigOperators
namespace Contingency
noncomputable section
namespace DyadicLaw
variable {α β : Type*} [Fintype α] [Fintype β]

theorem toLaw_pure [DecidableEq α] (x : α) (R : ℕ) : (pure x R).toLaw=Law.pure x := by
  apply Law.ext; funext y
  by_cases h : x=y
  · subst y; simp [toLaw,pure,Law.pure]
  · simp [toLaw,pure,Law.pure,h,Ne.symm h]

theorem toLaw_map [DecidableEq β] (f : α → β) (p : DyadicLaw α) :
    (p.map f).toLaw=p.toLaw.map f := by
  change (p.bind (fun x => pure (f x)) 0 (fun _ => rfl)).toLaw=p.toLaw.map f
  exact (toLaw_bind p (fun x => pure (f x)) 0 (fun _ => rfl)).trans (by
    simp only [toLaw_pure,Law.map])

def pad (p : DyadicLaw α) (R : ℕ) (hR : p.bits≤R) : DyadicLaw α where
  bits := R
  count x := p.count x*2^(R-p.bits)
  total := by rw [← Finset.sum_mul,p.total,← pow_add,Nat.add_sub_of_le hR]

theorem toLaw_pad (p : DyadicLaw α) (R : ℕ) (hR : p.bits≤R) : (p.pad R hR).toLaw=p.toLaw := by
  apply Law.ext; funext x
  change ((p.count x*2^(R-p.bits):ℕ):ℝ)/(2:ℝ)^R=(p.count x:ℝ)/(2:ℝ)^p.bits
  push_cast
  have he : (2:ℝ)^R=(2:ℝ)^p.bits*(2:ℝ)^(R-p.bits) := by rw [← pow_add,Nat.add_sub_of_le hR]
  rw [he]
  field_simp

variable {ι : Type*} [Fintype ι] [DecidableEq ι] {γ : ι → Type*} [∀ i,Fintype (γ i)]
def pi (p : ∀ i,DyadicLaw (γ i)) : DyadicLaw (∀ i,γ i) where
  bits := ∑ i,(p i).bits
  count x := ∏ i,(p i).count (x i)
  total := by rw [← Fintype.prod_sum]; simp only [DyadicLaw.total,Finset.prod_pow_eq_pow_sum]

theorem toLaw_pi (p : ∀ i,DyadicLaw (γ i)) : (pi p).toLaw=Law.pi (fun i => (p i).toLaw) := by
  apply Law.ext; funext x
  simp only [toLaw,pi,Law.pi,Nat.cast_prod,Finset.prod_div_distrib,Finset.prod_pow_eq_pow_sum]

variable [DecidableEq α]
theorem toLaw_walk (q : α → DyadicLaw α) (S : ℕ) (hq : ∀ x,(q x).bits=S) (x₀ : α) :
    ∀ t,(walk q S hq x₀ t).toLaw=Nat.rec (Law.pure x₀) (fun _ p => p.bind (fun x => (q x).toLaw)) t := by
  intro t
  induction t with
  | zero => simp [walk,toLaw_pure]
  | succ t ih => rw [walk,toLaw_bind,ih]

end DyadicLaw

 
theorem stoppedLaw_toLaw {α : Type*} [Fintype α] [DecidableEq α]
    (p : DyadicLaw (Option α)) (fallback : α) (J : ℕ) :
    (stoppedLaw p fallback J).toLaw=Law.first p.toLaw fallback J := by
  apply Law.ext; funext z
  induction J with
  | zero =>
    by_cases h : z=fallback
    · subst z; simp [stoppedLaw,stoppedNumerator,DyadicLaw.toLaw,Law.first,Law.pure]
    · simp [stoppedLaw,stoppedNumerator,DyadicLaw.toLaw,Law.first,Law.pure,h,Ne.symm h]
  | succ J ih =>
    have hr : (Law.first p.toLaw fallback (J+1)).mass z=
        p.toLaw.mass none*(Law.first p.toLaw fallback J).mass z+p.toLaw.mass (some z) := by
      change (∑ o : Option α,p.toLaw.mass o*(match o with | none => Law.first p.toLaw fallback J | some y => Law.pure y).mass z)=_
      rw [Fintype.sum_option]
      simp [Law.pure]
    rw [hr,← ih]
    change ((p.count (some z)*2^(p.bits*J)+p.count none*stoppedNumerator p.bits (p.count none) (fun z => p.count (some z)) fallback z J:ℕ):ℝ)/(2:ℝ)^(p.bits*(J+1))=_
    simp only [DyadicLaw.toLaw,stoppedLaw]
    push_cast
    rw [Nat.mul_succ,pow_add]
    field_simp
    ring

end
end Contingency

 

 

open scoped BigOperators
namespace Contingency
noncomputable section
namespace Law
variable {α : Type*} [Fintype α] {β : α → Type*} [∀ x,Fintype (β x)]

def sigma (p : Law α) (q : ∀ x, Law (β x)) : Law (Sigma β) where
  mass z := p.mass z.1 * (q z.1).mass z.2
  nonneg z := mul_nonneg (p.nonneg _) ((q _).nonneg _)
  total := by
    rw [Fintype.sum_sigma]
    simp_rw [← Finset.mul_sum, (q _).total, mul_one]
    exact p.total

theorem sigma_eq_bind (p : Law α) (q : ∀ x,Law (β x)) :
    p.sigma q=p.bind (fun x => (q x).map (Sigma.mk x)) := by
  classical
  let : DecidableEq (Sigma β) := fun _ _ => Classical.propDecidable _
  apply Law.ext; funext ⟨x,y⟩
  simp only [sigma,bind,map,pure]
  have hh (z : α) : (∑ w : β z, (q z).mass w * (if Sigma.mk z w=Sigma.mk x y then 1 else 0))=
      if h : z=x then (q x).mass y else 0 := by
    by_cases h : z=x
    · subst z; simp
    · have hn (w : β z) : Sigma.mk z w ≠ Sigma.mk x y := by intro he; exact h (congrArg Sigma.fst he)
      simp [h,hn]
  change p.mass x * (q x).mass y = ∑ z, p.mass z * (∑ w : β z, (q z).mass w * (if Sigma.mk z w = Sigma.mk x y then 1 else 0))
  simp_rw [hh]
  simp

theorem tv_sigma (p p' : Law α) (q q' : ∀ x,Law (β x)) (δ : ℝ)
    (hq : ∀ x,(q x).tv (q' x) ≤ δ) :
    (p.sigma q).tv (p'.sigma q') ≤ p.tv p'+δ := by
  rw [sigma_eq_bind,sigma_eq_bind]
  exact tv_bind p p' _ _ δ (fun x => (tv_map _ _ _).trans (hq x))

end Law
namespace DyadicLaw
variable {α : Type*} [Fintype α] {β : α → Type*} [∀ x,Fintype (β x)]

def sigma (p : DyadicLaw α) (q : ∀ x,DyadicLaw (β x)) (S : ℕ) (hS : ∀ x,(q x).bits=S) : DyadicLaw (Sigma β) where
  bits := p.bits+S
  count z := p.count z.1*(q z.1).count z.2
  total := by
    rw [Fintype.sum_sigma]
    simp_rw [← Finset.mul_sum, (q _).total, hS]
    rw [← Finset.sum_mul,p.total,pow_add]

theorem sigma_bits (p : DyadicLaw α) (q : ∀ x,DyadicLaw (β x)) (S : ℕ) (hS : ∀ x,(q x).bits=S) :
    (p.sigma q S hS).bits=p.bits+S := rfl

theorem toLaw_sigma (p : DyadicLaw α) (q : ∀ x,DyadicLaw (β x)) (S : ℕ) (hS : ∀ x,(q x).bits=S) :
    (p.sigma q S hS).toLaw=p.toLaw.sigma (fun x => (q x).toLaw) := by
  apply Law.ext; funext z
  simp only [sigma,toLaw,Law.sigma,Nat.cast_mul,pow_add,hS]
  ring

end DyadicLaw
end
end Contingency

end

end OAI