Repository navigation
Expand file tree
/
Copy pathStatisticalDistance.lean
More file actions
763 lines (681 loc) · 37.5 KB
/
Copy pathStatisticalDistance.lean
File metadata and controls
763 lines (681 loc) · 37.5 KB
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
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
module
public import Probability.Distribution
public import Probability.Expectation
public import Mathlib.Algebra.Order.Sub.Basic
/-!
# Statistical distance
The statistical distance `δ(X, Y) = ∑ₐ max (X(a) − Y(a), 0)` of finitely supported real mass
functions [1, Definition 3], its equivalent forms, the optimal-test identity and the ratio bounds
behind H-coefficient arguments. The carrier may be infinite; each law has finite support.
## Main definitions
* `statDist X Y`: the statistical distance `δ(X, Y)`
* `avgSuccessProb X Y g`: the success probability of the test `g` in telling
`X` from `Y` under a uniform prior
* `probBad D B`: the mass of a bad event, for H-coefficient bounds
## Main results
* `statDist_nonneg`, `statDist_self`, `statDist_symm_of_eq_weight`,
`statDist_triangle`, `statDist_le_weight`
* equivalent forms: `statDist_eq_weight_sub_sum_min`,
`statDist_eq_half_sum_abs_of_weight_eq`, `statDist_eq_mass_sub_mass_pos`
* event gaps: `mass_sub_mass_le_statDist`; expectation gaps:
`abs_expect_sub_expect_le_mul_statDist`
* the optimal-test identity `sSup_avgSuccessProb_eq_half_add_half_statDist`
* the expectation method `statDist_le_expect_of_ratio` [3, Lemma 2.2.1] and its good/bad
specialization `statDist_le_probBad_add_of_ratio_on_good` [3, Corollary 2.2.2]
* `statDist_fTransform_le_expect_of_ratio`: `δ(f_* P, f_* Q) ≤ 𝔼_Q[ε]`, data processing
followed by the expectation method; the root of the system-level ratio bounds
* `statDist_partition` [1, Lemma 2], `statDist_fTransform_le` (data processing,
[1, Lemma 3]), `statDist_fTransform_injective` (injective pushforward preserves statistical
distance), `statDist_sum_of_disjoint_support` [1, Lemma 2, p. 10]
## References
1. D. Lanzenberger and U. Maurer. Coupling of Random Systems. In *Theory of Cryptography
(TCC 2020)*, Springer, 2020. Cryptology ePrint Archive, Paper 2020/1187.
2. U. Maurer. *Cryptography Foundations*. Lecture notes, ETH Zurich, Spring 2018.
3. A. Jha. *Provable Security of Symmetric-key Cryptographic Schemes*. Doctoral thesis,
Indian Statistical Institute, 2020. https://digitalcommons.isical.ac.in/doctoral-theses/456/.
4. U. Maurer, K. Pietrzak and R. Renner. *Indistinguishability Amplification*. In CRYPTO 2007,
LNCS 4622, pp. 130–149, 2007.
5. C. Portmann and R. Renner. *Security in Quantum Cryptography*. arXiv:2102.00021v2, 2021.
-/
@[expose] public section
noncomputable section
open scoped BigOperators NNReal
namespace Probability
/-! ### Supremum helpers for advantage definitions -/
/-- If an index type is empty, then the image of `Set.univ` under any function
out of it is empty. -/
lemma image_univ_eq_empty_of_not_nonempty {ι : Type*} {α : Type*}
(f : ι → α) (hι : ¬ Nonempty ι) :
f '' Set.univ = (∅ : Set α) := by
ext x
constructor
· rintro ⟨i, _hi, rfl⟩
exact (hι ⟨i⟩).elim
· intro hx
simp at hx
/-- A pointwise upper bound on an `sSup` image over `Set.univ` also covers the
empty-index case when the upper bound is nonnegative. -/
lemma sSup_image_univ_le_of_forall {ι : Type*} (f : ι → ℝ) {a : ℝ}
(ha : 0 ≤ a) (h : ∀ i, f i ≤ a) :
sSup (f '' Set.univ) ≤ a := by
by_cases hι : Nonempty ι
· refine csSup_le ?nonempty ?upper
· rcases hι with ⟨i⟩
exact ⟨f i, ⟨i, Set.mem_univ i, rfl⟩⟩
· rintro b ⟨i, _hi, rfl⟩
exact h i
· rw [image_univ_eq_empty_of_not_nonempty f hι, Real.sSup_empty]
exact ha
/-! ### Definition and basic properties -/
/-- Statistical distance between two distributions.
[1, Definition 3]: `δ(X, Y) := ∑_{a} max(0, X(a) - Y(a))`, the one-sided excess, here on
signed laws.
`Distribution A = A →₀ ℝ` has finite support, and a summand `max (X a - Y a) 0` is nonzero only
on `(X - Y).support`. The sum therefore equals the sum over `Finset.univ` on a `Fintype`
(`statDist_eq_sum_univ`) and is defined on infinite carriers such as transcript spaces;
indexing by `(X - Y).support` needs no `DecidableEq`.
`δ` (`RandomSystem.lean`) sums over `μ.support` alone and agrees with this when `ν ≥ 0`. -/
noncomputable def statDist {A : Type*} (X Y : Distribution A) : ℝ :=
∑ a ∈ (X - Y).support, max (X a - Y a) 0
/-- On a `Fintype` carrier, `statDist` is the sum over all points. -/
lemma statDist_eq_sum_univ {A : Type*} [Fintype A] (X Y : Distribution A) :
statDist X Y = ∑ a : A, max (X a - Y a) 0 := by
classical
rw [statDist]
refine Finset.sum_subset (Finset.subset_univ _) fun a _ ha => ?_
rw [Finsupp.notMem_support_iff] at ha
have h : X a - Y a = 0 := by simpa using ha
rw [h, max_self]
/-- `statDist` is the same sum over **any** finite set containing the support
of the difference: the summand `max (X a - Y a) 0` vanishes wherever
`X a = Y a`. This is the `Fintype`-free companion of `statDist_eq_sum_univ`,
and the working form on carriers that are genuinely infinite — transcript
spaces `List (X × Option Y)`, system laws `Distribution (DDS X Y)`. Taking
`s = Finset.univ` recovers `statDist_eq_sum_univ`. -/
lemma statDist_eq_sum_of_support_subset {A : Type*} (X Y : Distribution A)
{s : Finset A} (hs : (X - Y).support ⊆ s) :
statDist X Y = ∑ a ∈ s, max (X a - Y a) 0 := by
rw [statDist]
refine Finset.sum_subset hs fun a _ ha => ?_
rw [Finsupp.notMem_support_iff] at ha
have h : X a - Y a = 0 := by simpa using ha
rw [h, max_self]
/-- Each summand of `statDist` is nonnegative, so the whole sum is. -/
lemma statDist_nonneg {A : Type*} (X Y : Distribution A) :
0 ≤ statDist X Y :=
Finset.sum_nonneg fun _ _ => le_max_right _ _
/-- `δ(X, X) = 0`. -/
lemma statDist_self {A : Type*} (X : Distribution A) :
statDist X X = 0 := by
simp [statDist]
/-- `δ(X, Y) = δ(Y, X)` when `|X| = |Y|`.
[1, Definition 3, p. 10]: for distributions of equal weight,
δ(X,Y) = (1/2) ∑_a |X(a) - Y(a)| = δ(Y,X).
Both sums run over the finite set `X.support ∪ Y.support`, which covers both differences. -/
lemma statDist_symm_of_eq_weight {A : Type*}
(X Y : Distribution A) (h : X.weight = Y.weight) :
statDist X Y = statDist Y X := by
have : DecidableEq A := Classical.decEq A
have hXY : (X - Y).support ⊆ X.support ∪ Y.support := Finsupp.support_sub
have hYX : (Y - X).support ⊆ X.support ∪ Y.support := by
intro a ha
rcases Finset.mem_union.mp (Finsupp.support_sub ha) with h' | h'
· exact Finset.mem_union_right _ h'
· exact Finset.mem_union_left _ h'
rw [statDist_eq_sum_of_support_subset X Y hXY,
statDist_eq_sum_of_support_subset Y X hYX]
have key : ∀ a : A, max (X a - Y a) 0 = max (Y a - X a) 0 + (X a - Y a) := by
intro a
rcases le_total (X a) (Y a) with hle | hle
· rw [max_eq_right (sub_nonpos.mpr hle), max_eq_left (sub_nonneg.mpr hle)]
ring
· rw [max_eq_left (sub_nonneg.mpr hle), max_eq_right (sub_nonpos.mpr hle)]
ring
rw [Finset.sum_congr rfl (fun a _ => key a), Finset.sum_add_distrib,
Finset.sum_sub_distrib,
← Distribution.weight_eq_sum_of_support_subset X Finset.subset_union_left,
← Distribution.weight_eq_sum_of_support_subset Y Finset.subset_union_right, h,
sub_self, add_zero]
/-! ### Alternative forms of statistical distance -/
/-- **Min form of statistical distance on an arbitrary carrier**
([1, Definition 3], second form; [4, equation (3), p. 140], used in the proof of
[4, Lemma 5]): `δ(P,Q) = |P| − ∑ min(P,Q)`, the sum taken over any finite set containing both
supports.
`Distribution` is `A →₀ ℝ`, so no `Fintype A` is needed; the `[Fintype]` form below is the
instance at `s = Finset.univ`. The general form serves carriers such as transcript spaces
`List (X × Option Y)` and system laws `Distribution (DDS X Y)`, like
`statDist_eq_sum_of_support_subset` and `Distribution.weight_eq_sum_of_support_subset`.
It uses only the pointwise lattice identity `max (x − y) 0 = x − min x y`, so signed laws of
unequal weight are allowed. The asymmetry [1] notes for unequal weight is visible: the
right-hand side carries `|P|`. -/
lemma statDist_eq_weight_sub_sum_min_of_support_subset {A : Type*}
(P Q : Distribution A) {s : Finset A} (hP : P.support ⊆ s)
(hQ : Q.support ⊆ s) :
statDist P Q = P.weight - ∑ a ∈ s, min (P a) (Q a) := by
have : DecidableEq A := Classical.decEq A
have hsub : (P - Q).support ⊆ s := fun a ha => by
rcases Finset.mem_union.mp (Finsupp.support_sub ha) with h | h
· exact hP h
· exact hQ h
rw [statDist_eq_sum_of_support_subset P Q hsub]
have key : ∀ a : A, max (P a - Q a) 0 = P a - min (P a) (Q a) := by
intro a
rcases le_total (P a) (Q a) with h | h
· rw [max_eq_right (sub_nonpos.mpr h), min_eq_left h, sub_self]
· rw [max_eq_left (sub_nonneg.mpr h), min_eq_right h]
rw [Finset.sum_congr rfl (fun a _ => key a), Finset.sum_sub_distrib,
← Distribution.weight_eq_sum_of_support_subset P hP]
/-- The `Fintype` instance of the min form: take `s = Finset.univ`. -/
lemma statDist_eq_weight_sub_sum_min {A : Type*} [Fintype A] (X Y : Distribution A) :
statDist X Y = X.weight - ∑ a : A, min (X a) (Y a) :=
statDist_eq_weight_sub_sum_min_of_support_subset X Y
(fun a _ => Finset.mem_univ a) (fun a _ => Finset.mem_univ a)
/-- Weight-one specialization of the min form ([4, equation (3)], used in the proof of
[4, Lemma 5]): `δ(X, Y) = 1 - ∑_a min(X(a), Y(a))`.
Only the *first* argument needs weight one — the min form charges `|X|`, so
`Y` may be an arbitrary signed distribution. -/
theorem statDist_eq_one_sub_sum_min {A : Type*} [Fintype A]
(X Y : Distribution A) (hX : X.weight = 1) :
statDist X Y = 1 - ∑ a : A, min (X a) (Y a) := by
rw [statDist_eq_weight_sub_sum_min, hX]
/-- **Half-`L1` form of statistical distance** ([1, Definition 3, remark];
[5, Definition 4]): for distributions of equal weight,
`δ(X, Y) = ½ ∑_a |X(a) - Y(a)|`.
Equal weight is exactly what is needed: pointwise
`max (x - y) 0 = ((x - y) + |x - y|) / 2`, and summing makes the linear term
contribute `(|X| - |Y|) / 2`, which vanishes precisely under the equal-weight
hypothesis (the library's idiom for symmetry, cf.
`statDist_symm_of_eq_weight`). -/
theorem statDist_eq_half_sum_abs_of_weight_eq {A : Type*} [Fintype A]
(X Y : Distribution A) (h : X.weight = Y.weight) :
statDist X Y = (∑ a : A, |X a - Y a|) / 2 := by
simp only [statDist_eq_sum_univ]
have key : ∀ a : A, max (X a - Y a) 0 = ((X a - Y a) + |X a - Y a|) / 2 := by
intro a
rcases le_total (X a) (Y a) with hle | hle
· rw [max_eq_right (sub_nonpos.mpr hle), abs_of_nonpos (sub_nonpos.mpr hle)]
ring
· rw [max_eq_left (sub_nonneg.mpr hle), abs_of_nonneg (sub_nonneg.mpr hle)]
ring
rw [Finset.sum_congr rfl (fun a _ => key a)]
simp_rw [div_eq_mul_inv]
rw [← Finset.sum_mul, Finset.sum_add_distrib, Finset.sum_sub_distrib,
← Distribution.weight_eq_sum X, ← Distribution.weight_eq_sum Y, h, sub_self, zero_add]
/-- Triangle inequality for statistical distance.
`δ(X, Z) ≤ δ(X, Y) + δ(Y, Z)`.
No `Fintype`: all three sums run over the union of the three difference
supports, and the estimate is the pointwise
`max (a - c) 0 ≤ max (a - b) 0 + max (b - c) 0`. -/
lemma statDist_triangle {A : Type*}
(X Y Z : Distribution A) :
statDist X Z ≤ statDist X Y + statDist Y Z := by
have : DecidableEq A := Classical.decEq A
rw [statDist_eq_sum_of_support_subset X Z
(Finset.subset_union_left :
(X - Z).support ⊆ (X - Z).support ∪ ((X - Y).support ∪ (Y - Z).support)),
statDist_eq_sum_of_support_subset X Y
(Finset.subset_union_left.trans Finset.subset_union_right),
statDist_eq_sum_of_support_subset Y Z
(Finset.subset_union_right.trans Finset.subset_union_right),
← Finset.sum_add_distrib]
apply Finset.sum_le_sum
intro a _
apply max_le
· calc X a - Z a = (X a - Y a) + (Y a - Z a) := by ring
_ ≤ max (X a - Y a) 0 + max (Y a - Z a) 0 :=
add_le_add (le_max_left _ _) (le_max_left _ _)
· exact add_nonneg (le_max_right _ _) (le_max_right _ _)
/-- Statistical distance is bounded by total weight (for non-negative laws).
`δ(X, Y) ≤ |X|`.
No `Fintype`: both sides are read on `X.support ∪ Y.support`, which already
covers the difference's support. The generality is what lets the bound reach
the carriers the random-systems layer uses, where no `Fintype` exists. -/
lemma statDist_le_weight {A : Type*}
{X Y : Distribution A} (hX : X.NonNeg) (hY : Y.NonNeg) :
statDist X Y ≤ X.weight := by
have : DecidableEq A := Classical.decEq A
rw [statDist_eq_sum_of_support_subset X Y Finsupp.support_sub,
Distribution.weight_eq_sum_of_support_subset X Finset.subset_union_left
(s := X.support ∪ Y.support)]
apply Finset.sum_le_sum
intro a _
exact max_le (sub_le_self _ (hY a)) (hX a)
/-- **Event-mass bound by statistical distance.**
For any event, the one-sided gap between its masses is bounded by statistical distance. -/
lemma mass_tsub_mass_le_statDist {A : Type*}
(X Y : Distribution A) (P : A → Prop) :
X.mass P - Y.mass P ≤ statDist X Y := by
classical
set s : Finset A := X.support ∪ Y.support ∪ (X - Y).support with hs
have hsub : (X - Y).support ⊆ s := Finset.subset_union_right
have hX : X.support ⊆ s := Finset.Subset.trans Finset.subset_union_left Finset.subset_union_left
have hY : Y.support ⊆ s := Finset.Subset.trans Finset.subset_union_right Finset.subset_union_left
rw [Distribution.mass_eq_sum_of_support_subset X hX P,
Distribution.mass_eq_sum_of_support_subset Y hY P,
statDist_eq_sum_of_support_subset X Y hsub, ← Finset.sum_sub_distrib]
have hfilter : ∑ a ∈ s.filter P, (X a - Y a) ≤ ∑ a ∈ s.filter P, max (X a - Y a) 0 :=
Finset.sum_le_sum fun a _ => le_max_left _ _
exact hfilter.trans (Finset.sum_le_sum_of_subset_of_nonneg (Finset.filter_subset _ _)
fun a _ _ => le_max_right _ _)
/-- **Real-valued event-mass bound by statistical distance.**
This is the real-valued form of `mass_tsub_mass_le_statDist`. -/
lemma mass_sub_mass_le_statDist {A : Type*}
(X Y : Distribution A) (P : A → Prop) :
((X.mass P : ℝ) - (Y.mass P : ℝ)) ≤ (statDist X Y : ℝ) :=
mass_tsub_mass_le_statDist X Y P
/-- **Two pushforwards of one law that agree off a bad event are within its
mass.** If `F` and `G` differ only on `P`, then the laws they induce from a
common non-negative `μ` are at statistical distance at most `μ(P)`.
This is the "identical-until-bad" step in its distributional form: the coupling
is the shared source `μ`, and the bound is the probability that the two
readings of one sampled atom part company. Both sides are pushforwards of the
*same* `μ` — that is what makes it an equality of couplings rather than a
triangle inequality, and it is why no weight or `Fintype` hypothesis appears. -/
lemma statDist_fTransform_le_mass_of_eq_off {A B : Type*} {μ : Distribution A}
(hμ : μ.NonNeg) (F G : A → B) (P : A → Prop)
(hoff : ∀ a ∈ μ.support, ¬ P a → F a = G a) :
statDist (Distribution.fTransform F μ) (Distribution.fTransform G μ)
≤ μ.mass P := by
classical
set s : Finset B := (μ.support.image F) ∪ (μ.support.image G) ∪
(Distribution.fTransform F μ - Distribution.fTransform G μ).support with hs
have hsub : (Distribution.fTransform F μ - Distribution.fTransform G μ).support ⊆ s :=
Finset.subset_union_right
have hcover : ∀ a ∈ μ.support, F a ∈ s := fun a ha =>
Finset.mem_union_left _ (Finset.mem_union_left _ (Finset.mem_image_of_mem F ha))
have hterm : ∀ b ∈ s,
max (Distribution.fTransform F μ b - Distribution.fTransform G μ b) 0
≤ μ.mass (fun a => P a ∧ F a = b) := by
intro b _
have hsplit : μ.mass (fun a => P a ∧ F a = b)
+ μ.mass (fun a => ¬ P a ∧ F a = b) = μ.mass (fun a => F a = b) :=
Distribution.mass_and_add_mass_not_and μ P (fun a => F a = b)
have hle : μ.mass (fun a => ¬ P a ∧ F a = b) ≤ μ.mass (fun a => G a = b) :=
Distribution.mass_mono_on_support hμ fun a ha hcase => by
rw [← hoff a ha hcase.1]; exact hcase.2
refine max_le ?_ (hμ.mass_nonneg _)
rw [Distribution.fTransform_apply_eq_mass, Distribution.fTransform_apply_eq_mass,
← hsplit]
linarith
calc statDist (Distribution.fTransform F μ) (Distribution.fTransform G μ)
= ∑ b ∈ s, max (Distribution.fTransform F μ b
- Distribution.fTransform G μ b) 0 :=
statDist_eq_sum_of_support_subset _ _ hsub
_ ≤ ∑ b ∈ s, μ.mass (fun a => P a ∧ F a = b) := Finset.sum_le_sum hterm
_ = μ.mass P := (Distribution.mass_eq_sum_mass_fiber μ P F s hcover).symm
/-! ### The distance/expectation bridge -/
/-- **[2, Exercise 4.4]**, one-sided: a functional taking values in `[m, M]`
moves by at most `(M − m) · δ(X, Y)`.
The proof is the exercise's: recentre `f` by the constant `m`, which is free
because equal weight makes `𝔼_X[m] − 𝔼_Y[m] = 0`, and then bound the recentred
functional pointwise by the one-sided excess. Signed layer plus `|X| = |Y|`;
no `Fintype`. -/
theorem expect_sub_expect_le_mul_statDist {A : Type*} (X Y : Distribution A) (f : A → ℝ)
{m M : ℝ} (hw : X.weight = Y.weight) (hm : ∀ a, m ≤ f a) (hM : ∀ a, f a ≤ M) :
X.expect f - Y.expect f ≤ (M - m) * statDist X Y := by
have hconst : (X - Y).expect (fun _ => m) = 0 := by
rw [Distribution.expect_sub_left, Distribution.expect_const, Distribution.expect_const, hw,
sub_self]
rw [← Distribution.expect_sub_left, ← sub_zero ((X - Y).expect f), ← hconst, statDist,
Finset.mul_sum, Distribution.expect, Distribution.expect, Finsupp.sum, Finsupp.sum,
← Finset.sum_sub_distrib]
refine Finset.sum_le_sum fun a _ => ?_
have hd : (X - Y) a = X a - Y a := by simp
have hmM : m ≤ M := (hm a).trans (hM a)
rw [hd]
rcases le_total 0 (X a - Y a) with h | h
· rw [max_eq_left h]
nlinarith [hM a]
· rw [max_eq_right h]
nlinarith [hm a]
/-- **[2, Exercise 4.4]**, the two-sided form the exercise asks to "state
formally": for distributions of equal weight, a functional taking values in
`[m, M]` satisfies `|𝔼_X[f] − 𝔼_Y[f]| ≤ (M − m) · δ(X, Y)`.
At `[m, M] = [-1, 1]`, the calibration [2, Definition 2.9] puts on a bit-guessing advantage,
this is the printed constant `2d`; at `[m, M] = [0, 1]` it is the `d` of [2, footnote 12].
Signed layer plus `|X| = |Y|`; no `Fintype`. -/
theorem abs_expect_sub_expect_le_mul_statDist {A : Type*}
(X Y : Distribution A) (f : A → ℝ) {m M : ℝ} (hw : X.weight = Y.weight)
(hm : ∀ a, m ≤ f a) (hM : ∀ a, f a ≤ M) :
|X.expect f - Y.expect f| ≤ (M - m) * statDist X Y := by
refine abs_sub_le_iff.mpr ⟨expect_sub_expect_le_mul_statDist X Y f hw hm hM, ?_⟩
rw [statDist_symm_of_eq_weight X Y hw]
exact expect_sub_expect_le_mul_statDist Y X f hw.symm hm hM
/-- Statistical distance is exactly the one-sided mass gap on the points where
the first distribution is heavier. This is the canonical binary hypothesis
test induced by the two laws. Finite supports suffice even when the sample
carrier itself is infinite. -/
lemma statDist_eq_mass_sub_mass_pos {A : Type*}
(X Y : Distribution A) :
(statDist X Y : ℝ) =
(X.mass (fun a => Y a < X a) : ℝ) -
(Y.mass (fun a => Y a < X a) : ℝ) := by
classical
rw [statDist_eq_sum_of_support_subset X Y Finsupp.support_sub,
Distribution.mass_eq_sum_of_support_subset X
(s := X.support ∪ Y.support) Finset.subset_union_left,
Distribution.mass_eq_sum_of_support_subset Y
(s := X.support ∪ Y.support) Finset.subset_union_right,
Finset.sum_filter, Finset.sum_filter, ← Finset.sum_sub_distrib]
apply Finset.sum_congr rfl
intro a _
by_cases h : Y a < X a
· rw [ite_eq_left h, ite_eq_left h, max_eq_left (sub_nonneg.mpr h.le)]
· rw [ite_eq_right h, ite_eq_right h, max_eq_right (sub_nonpos.mpr (not_lt.mp h))]
simp
/-! ### The optimal-test identity -/
/-- Success probability of the **binary hypothesis test** `g` at identifying
the source of a sample drawn from `X` or `Y` with equal prior ½: with
probability ½ the sample is drawn from `X` and the answer is correct iff `g`
answers `true`, with probability ½ it is drawn from `Y` and the answer is
correct iff `g` answers `false`. Meaningful as a probability when `X` and `Y`
are probability distributions; defined for arbitrary signed distributions.
This is hypothesis testing rather than value guessing: it
decides *which of two hypotheses* produced the sample; the different task of
recovering an unknown *value* from correlated side information is
`Distribution.guessProb` / `Distribution.condGuessProb` (`Probability/Entropy.lean`), the
quantities paired with min-entropy. In mathlib's decision-theory vocabulary
(`Mathlib/Probability/Decision/Risk/Defs.lean`) the quantity here is
`1 - avgRisk` at 0-1 loss under the uniform prior — prior-averaged over a
**given** rule `g`, which is what `avgRisk` names. `bayesRisk` there is the
*optimum* over rules, so the Bayes-flavoured statement is the supremum
`sSup_avgSuccessProb_eq_half_add_half_statDist` below, which is `1 - bayesRisk`
and hence the classical Bayes-error identity `bayesRisk = ½ − ½·δ(X, Y)`. -/
noncomputable def avgSuccessProb {A : Type*} (X Y : Distribution A) (g : A → Bool) : ℝ :=
(X.mass (fun a => g a = true) + Y.mass (fun a => g a = false)) / 2
/-- Structural form of `avgSuccessProb`: eliminating the complement event turns the
success probability into the signed mass gap of the acceptance set of `g`,
shifted by the total weight of `Y`. Unconditional. -/
lemma avgSuccessProb_eq_mass_sub_mass_add_weight_div_two {A : Type*}
(X Y : Distribution A) (g : A → Bool) :
avgSuccessProb X Y g =
(X.mass (fun a => g a = true) - Y.mass (fun a => g a = true) +
Y.weight) / 2 := by
have hcompl := Distribution.mass_add_compl Y (fun a => g a = true)
have hfalse : Y.mass (fun a => ¬ (g a = true)) =
Y.mass (fun a => g a = false) :=
Distribution.mass_congr Y (fun a => by simp)
unfold avgSuccessProb
rw [← hfalse]
linarith
/-- **No decision rule beats `½ + ½ δ(X, Y)`** (optimality half of [5, Theorem 8]).
Only `|Y| = 1` is needed: the acceptance-set mass gap is bounded by `δ(X, Y)` for arbitrary
signed `X`, `Y` (`mass_tsub_mass_le_statDist`). -/
theorem avgSuccessProb_le_half_add_half_statDist {A : Type*}
(X Y : Distribution A) (hY : Y.weight = 1) (g : A → Bool) :
avgSuccessProb X Y g ≤ 1 / 2 + statDist X Y / 2 := by
rw [avgSuccessProb_eq_mass_sub_mass_add_weight_div_two, hY]
have := mass_tsub_mass_le_statDist X Y (fun a => g a = true)
linarith
/-- **The Bayes rule attains `½ + ½ δ(X, Y)`** (attainment half of
[5, Theorem 8]): any rule that guesses `X` exactly on `{a | Y(a) < X(a)}` — the
Bayes-optimal rule for equal prior ½ — succeeds with probability exactly
`½ + ½ δ(X, Y)`. The rule is taken as an abstract `g` with a specification
hypothesis so the statement does not fix a `Decidable` instance. -/
theorem avgSuccessProb_eq_half_add_half_statDist_of_forall_eq_true_iff_lt
{A : Type*} (X Y : Distribution A) (hY : Y.weight = 1) (g : A → Bool)
(hg : ∀ a, g a = true ↔ Y a < X a) :
avgSuccessProb X Y g = 1 / 2 + statDist X Y / 2 := by
rw [avgSuccessProb_eq_mass_sub_mass_add_weight_div_two, hY,
Distribution.mass_congr X hg, Distribution.mass_congr Y hg,
← statDist_eq_mass_sub_mass_pos]
ring
/-- **Optimal-test identity** ([5, Theorem 8], classical case; the
`½ + ½p` computation of [4, Lemma 4]): the best success probability over
all decision rules `A → Bool` of deciding which of `X`, `Y` (equal prior ½) a
sample came from is exactly `½ + ½ δ(X, Y)`. In mathlib's decision-theory
vocabulary this is `1 - bayesRisk` at 0-1 loss and uniform prior.
Stated as a genuine supremum over all decision rules, in the library's
`sSup`-over-image idiom; `avgSuccessProb_le_half_add_half_statDist` gives the upper
bound for every rule and the Bayes rule attains it. Only `|Y| = 1` is
required, matching the min-form asymmetry of `δ`; for the intended reading
both `X` and `Y` are probability distributions. -/
theorem sSup_avgSuccessProb_eq_half_add_half_statDist {A : Type*}
(X Y : Distribution A) (hY : Y.weight = 1) :
sSup ((fun g : A → Bool => avgSuccessProb X Y g) '' Set.univ) =
1 / 2 + statDist X Y / 2 := by
have hbound : ∀ g : A → Bool, avgSuccessProb X Y g ≤ 1 / 2 + statDist X Y / 2 :=
avgSuccessProb_le_half_add_half_statDist X Y hY
have hbdd : BddAbove ((fun g : A → Bool => avgSuccessProb X Y g) '' Set.univ) := by
refine ⟨1 / 2 + statDist X Y / 2, ?_⟩
rintro x ⟨g, -, rfl⟩
exact hbound g
have hnonneg : (0 : ℝ) ≤ 1 / 2 + statDist X Y / 2 := by
have := statDist_nonneg X Y
linarith
refine le_antisymm (sSup_image_univ_le_of_forall _ hnonneg hbound) ?_
classical
calc 1 / 2 + statDist X Y / 2
= avgSuccessProb X Y (fun a => decide (Y a < X a)) :=
(avgSuccessProb_eq_half_add_half_statDist_of_forall_eq_true_iff_lt X Y hY _
(fun a => by simp)).symm
_ ≤ sSup ((fun g : A → Bool => avgSuccessProb X Y g) '' Set.univ) :=
le_csSup hbdd ⟨_, Set.mem_univ _, rfl⟩
/-- A one-sided ratio lower bound controls the pointwise deficit. -/
lemma sub_le_mul_of_one_sub_mul_le {a b eps : ℝ}
(h_lower : (1 - eps) * b ≤ a) :
b - a ≤ eps * b := by
nlinarith [h_lower]
/-- The expectation method for equal-weight mass functions:
`(1 − ε) · ideal ≤ real` gives `δ(real, ideal) ≤ 𝔼_ideal[ε]`.
[3, Lemma 2.2.1, p. 19]: “Adv_{O₁;O₀}(A) ≤ ϵ_bad + Ex[ϵ_ratio(Θ₀)]”. For probability laws,
setting `ε = 1` on the bad set and `ϵ_ratio` elsewhere gives the source's good/bad bound. -/
theorem statDist_le_expect_of_ratio
-- `A` carries outcomes; `real`, `ideal` are finitely supported real mass functions on `A`.
{A : Type*} (real ideal : Distribution A)
-- `ε a` is the scalar loss at outcome `a`; `hε` below requires it to be nonnegative.
(ε : A → ℝ)
-- `hweight` gives equal total mass; `hε` allows any nonnegative loss, including above one.
(hweight : real.weight = ideal.weight) (hε : ∀ a, 0 ≤ ε a)
-- `hideal` makes `ideal` nonnegative; `real` may be signed.
(hideal : ideal.NonNeg)
-- `ratio` bounds the ideal deficit at every `a`, including where `ideal a = 0`.
(ratio : ∀ a, (1 - ε a) * ideal a ≤ real a) :
statDist real ideal ≤ ideal.expect ε := by
classical
-- Equal weights make `δ` symmetric, so `δ(real, ideal)` is the ideal deficit
-- `∑_a max(ideal a − real a, 0)`, the sum “∑_{ω∈Ω} max{0, Pr[Θ0 = ω] − Pr[Θ1 = ω]}” of
-- [3, p. 20]. Sum it and `𝔼_ideal[ε]` over `ideal.support ∪ real.support`.
rw [statDist_symm_of_eq_weight _ _ hweight,
statDist_eq_sum_of_support_subset ideal real (s := ideal.support ∪ real.support)
(Finsupp.support_sub),
Distribution.expect_eq_sum_of_support_subset ideal ε Finset.subset_union_left]
-- Pointwise `max(ideal a − real a, 0) ≤ ε a · ideal a`: the ratio rearranges to
-- `ideal a − real a ≤ ε a · ideal a`, and `0 ≤ ε a · ideal a`.
refine Finset.sum_le_sum fun a _ => max_le ?_ (mul_nonneg (hideal a) (hε a))
have h := ratio a
rw [sub_mul, one_mul] at h
linarith [mul_comm (ε a) (ideal a)]
/-! ### H-coefficient bound -/
/-- The mass of the bad event `B` under distribution `D`. -/
noncomputable def probBad {A : Type*}
(D : Distribution A) (B : A → Prop) :
ℝ :=
D.mass B
/-- Adding deterministic terminal side information does
not change the mass of a bad event that ignores that terminal component. -/
@[simp]
lemma probBad_const_pair {A U : Type*}
(D : Distribution A) (B : A → Prop) (u : U) :
probBad (Distribution.fTransform (fun a : A => (a, u)) D)
(fun p : A × U => B p.1) =
probBad D B := by
unfold probBad
rw [Distribution.mass_fTransform]
/-- **The H-technique ratio bound on an arbitrary carrier.** If the
real/ideal density ratio is at least `1 - eps` on good points then
`δ(real, ideal) ≤ Pr_ideal[Bad] + eps`, for `real ≥ 0`, `ideal ≥ 0` of equal weight at most one:
the distribution form of [3, Corollary 2.2.2, p. 20], “a simple corollary of this result where
ϵratio is a constant function”. It is `statDist_le_expect_of_ratio` with error `1[Bad] + eps`.
`Distribution` is `A →₀ ℝ`: the sums run over
`ideal.support ∪ real.support`, so no finiteness of the carrier is
used. Transcript laws live on lists of query–reply pairs, a carrier that is
infinite even for finite alphabets. -/
theorem statDist_le_probBad_add_of_ratio_on_good {A : Type*}
(real ideal : Distribution A) (Bad : A → Prop) (eps : ℝ≥0)
(h_real_nonneg : real.NonNeg) (h_ideal_nonneg : ideal.NonNeg)
(h_weight : real.weight = ideal.weight)
(h_ideal_le : ideal.weight ≤ 1)
(h_ratio : ∀ a, ¬ Bad a → (1 - eps) * ideal a ≤ real a) :
statDist real ideal ≤ probBad ideal Bad + eps := by
classical
-- Use the expectation method with error `1[Bad] + eps`.
have h := statDist_le_expect_of_ratio real ideal
(fun a => (if Bad a then (1 : ℝ) else 0) + eps) h_weight
(fun a => add_nonneg (by split_ifs <;> norm_num) eps.coe_nonneg) h_ideal_nonneg (by
intro a
by_cases hb : Bad a
· -- On bad points the required lower bound is `-eps * ideal(a) ≤ real(a)`.
simp only [ite_eq_left hb]
nlinarith [h_real_nonneg a, mul_nonneg eps.coe_nonneg (h_ideal_nonneg a)]
· simpa only [ite_eq_right hb, zero_add] using h_ratio a hb)
calc
_ ≤ ideal.expect (fun a => (if Bad a then (1 : ℝ) else 0) + eps) := h
_ = probBad ideal Bad + (eps : ℝ) * ideal.weight := by
-- The indicator contributes the bad mass; the constant contributes `eps * weight`.
rw [Distribution.expect_add_right', Distribution.expect_indicator, Distribution.expect_const]
rfl
_ ≤ probBad ideal Bad + eps := by
-- A subprobability law has total weight at most one.
exact add_le_add le_rfl (by
simpa only [mul_one] using mul_le_mul_of_nonneg_left h_ideal_le eps.coe_nonneg)
/-! ### Partition and data processing -/
/-- Partition of statistical distance [1, Lemma 2].
For any partition {Aⱼ} of A:
δ(X, Y) = ∑_j δ(X_j, Y_j)
where X_j, Y_j are X, Y restricted to Aⱼ. -/
theorem statDist_partition {A : Type*} [Fintype A] {n : ℕ}
(X Y : Distribution A) (P : A → Fin n) :
statDist X Y =
∑ j : Fin n, ∑ a ∈ Finset.univ.filter (fun a => P a = j),
max (X a - Y a) 0 := by
simp only [statDist_eq_sum_univ]
exact (Finset.sum_fiberwise Finset.univ P _).symm
/-- Data processing inequality [1, Lemma 3, p. 11]: “δ(X, Y) ≥ δ(f(X), f(Y))”. For any
function `f : A → B`, applying `f` can only decrease statistical distance.
Proof of [1, Lemma 3] (Appendix A): δ(f(X), f(Y)) = ∑_b max(0, ∑_{f(a)=b} (X(a) - Y(a)))
≤ ∑_b ∑_{f(a)=b} max(0, X(a) - Y(a)) = ∑_a max(0, X(a) - Y(a)) = δ(X,Y).
The one step that is about the **signed** carrier is the fiber estimate
`max (0, ∑ᵢ dᵢ) ≤ ∑ᵢ max (0, dᵢ)`: a pushforward sums a fiber, and
cancellation inside a fiber can only shrink the one-sided excess.
The source sum runs over `X.support ∪ Y.support`; its image under `f` supports the
pushed-forward difference. -/
theorem statDist_fTransform_le {A B : Type*}
(X Y : Distribution A) (f : A → B) :
statDist (Distribution.fTransform f X) (Distribution.fTransform f Y) ≤ statDist X Y := by
have : DecidableEq A := Classical.decEq A
have : DecidableEq B := Classical.decEq B
-- A pushforward evaluates as the sum over the fiber, taken inside any finite
-- set covering the support.
have hval : ∀ (Z : Distribution A), Z.support ⊆ X.support ∪ Y.support →
∀ b : B, Distribution.fTransform f Z b =
∑ a ∈ (X.support ∪ Y.support).filter (fun a => f a = b), Z a := by
intro Z hZ b
rw [Distribution.fTransform_apply_eq_mass]
simp only [Distribution.mass]
rw [Finsupp.sum_of_support_subset Z hZ _ (fun i _ => by simp), Finset.sum_filter]
refine Finset.sum_congr rfl fun a _ => ?_
by_cases h : f a = b <;> simp [h]
have himg : ∀ (Z : Distribution A), Z.support ⊆ X.support ∪ Y.support →
(Distribution.fTransform f Z).support ⊆ (X.support ∪ Y.support).image f := by
intro Z hZ b hb
obtain ⟨a, ha, rfl⟩ := Distribution.mem_support_fTransform f Z hb
exact Finset.mem_image_of_mem f (hZ ha)
have hsupp :
(Distribution.fTransform f X - Distribution.fTransform f Y).support ⊆
(X.support ∪ Y.support).image f := by
intro b hb
rcases Finset.mem_union.mp (Finsupp.support_sub hb) with h | h
· exact himg X Finset.subset_union_left h
· exact himg Y Finset.subset_union_right h
rw [statDist_eq_sum_of_support_subset _ _ hsupp,
statDist_eq_sum_of_support_subset X Y Finsupp.support_sub,
← Finset.sum_fiberwise_of_maps_to
(fun a ha => Finset.mem_image_of_mem f ha) (fun a => max (X a - Y a) 0)]
refine Finset.sum_le_sum fun b _ => ?_
rw [hval X Finset.subset_union_left b, hval Y Finset.subset_union_right b]
refine max_le ?_ (Finset.sum_nonneg fun a _ => le_max_right _ _)
rw [← Finset.sum_sub_distrib]
exact Finset.sum_le_sum fun a _ => le_max_left _ _
/-- A ratio bound controls the distance after any common function:
`(1 − ε) · Q ≤ P` pointwise gives `δ(f_* P, f_* Q) ≤ 𝔼_Q[ε]`. -/
theorem statDist_fTransform_le_expect_of_ratio
-- `P`, `Q`: finitely supported real mass functions on an arbitrary type `A`;
-- `f : A → B` is any function to an arbitrary type `B`.
{A B : Type*} (P Q : Distribution A) (f : A → B)
-- `ε a`: the scalar ratio loss at `a`, a real number that may exceed one.
(ε : A → ℝ)
-- `hweight`: `|P| = |Q|`. `hQ`: `Q ≥ 0`; `P` may be signed.
(hweight : P.weight = Q.weight) (hQ : Q.NonNeg)
-- `hε`: `ε ≥ 0`. `ratio`: `(1 − ε a) · Q a ≤ P a` at every atom `a`, before `f`.
(hε : ∀ a, 0 ≤ ε a) (ratio : ∀ a, (1 - ε a) * Q a ≤ P a) :
statDist (Distribution.fTransform f P) (Distribution.fTransform f Q) ≤ Q.expect ε := by
calc
-- Data processing [1, Lemma 3, p. 11]: the common `f` cannot increase `δ`.
_ ≤ statDist P Q := statDist_fTransform_le P Q f
-- Expectation method [3, Lemma 2.2.1, p. 19]: equal weights and the ratio give `𝔼_Q[ε]`.
_ ≤ Q.expect ε := statDist_le_expect_of_ratio P Q ε hweight hε hQ ratio
/-! ### Injective pushforward -/
/-- Injective pushforward preserves statistical distance: for injective `f : A → B`,
`δ(f(X), f(Y)) = δ(X, Y)`.
The `≤` half is data processing [1, Lemma 3]; the `≥` half re-indexes the sum over
`(X − Y).support` along `f` (`Finset.sum_image`) inside a finite superset of the image law's
difference support. Both sums use finite supports on arbitrary carriers. -/
theorem statDist_fTransform_injective {A B : Type*}
(X Y : Distribution A) (f : A → B) (hf : Function.Injective f) :
statDist (Distribution.fTransform f X) (Distribution.fTransform f Y) = statDist X Y := by
classical
refine le_antisymm (statDist_fTransform_le X Y f) ?_
have himg : statDist X Y =
∑ b ∈ (X - Y).support.image f,
max (Distribution.fTransform f X b - Distribution.fTransform f Y b) 0 := by
rw [statDist, Finset.sum_image (fun a _ b _ h => hf h)]
exact Finset.sum_congr rfl fun a _ => by
rw [Distribution.fTransform_injective_apply X f hf,
Distribution.fTransform_injective_apply Y f hf]
rw [himg,
statDist_eq_sum_of_support_subset (Distribution.fTransform f X)
(Distribution.fTransform f Y)
(Finset.subset_union_left (s₂ := (X - Y).support.image f))]
exact Finset.sum_le_sum_of_subset_of_nonneg Finset.subset_union_right
fun b _ _ => le_max_right _ _
/-! ### Additivity across disjoint cells -/
/-- **[1, Lemma 2, p. 10], family form**: for families supported on
pairwise disjoint cells, statistical distance is additive,
`δ(∑ᵢ Xᵢ, ∑ᵢ Yᵢ) = ∑ᵢ δ(Xᵢ, Yᵢ)`. -/
theorem statDist_sum_of_disjoint_support {A ι : Type*} [DecidableEq A]
{t : Finset ι} (Xf Yf : ι → Distribution A)
(hdisj : (t : Set ι).PairwiseDisjoint
fun i => (Xf i).support ∪ (Yf i).support) :
statDist (∑ i ∈ t, Xf i) (∑ i ∈ t, Yf i) = ∑ i ∈ t, statDist (Xf i) (Yf i) := by
classical
-- Off the cells both sums vanish, so the whole distance lives on their union.
have hcell : ∀ (Zf : ι → Distribution A),
(∀ j, (Zf j).support ⊆ (Xf j).support ∪ (Yf j).support) →
∀ i ∈ t, ∀ a ∈ (Xf i).support ∪ (Yf i).support, (∑ j ∈ t, Zf j) a = Zf i a := by
intro Zf hZ i hi a ha
rw [Finsupp.finsetSum_apply]
refine Finset.sum_eq_single_of_mem i hi fun j hj hne => ?_
refine Finsupp.notMem_support_iff.mp fun hmem => ?_
exact Finset.disjoint_left.mp
(hdisj (Finset.mem_coe.mpr hi) (Finset.mem_coe.mpr hj) (Ne.symm hne))
ha (hZ j hmem)
have hsub : (∑ i ∈ t, Xf i - ∑ i ∈ t, Yf i).support
⊆ t.biUnion fun i => (Xf i).support ∪ (Yf i).support := by
intro a ha
rcases Finset.mem_union.mp (Finsupp.support_sub ha) with h | h
· obtain ⟨i, hi, hia⟩ := Finset.mem_biUnion.mp
(Finsupp.support_finsetSum h)
exact Finset.mem_biUnion.mpr ⟨i, hi, Finset.mem_union_left _ hia⟩
· obtain ⟨i, hi, hia⟩ := Finset.mem_biUnion.mp
(Finsupp.support_finsetSum h)
exact Finset.mem_biUnion.mpr ⟨i, hi, Finset.mem_union_right _ hia⟩
rw [statDist_eq_sum_of_support_subset _ _ hsub, Finset.sum_biUnion hdisj]
refine Finset.sum_congr rfl fun i hi => ?_
rw [statDist_eq_sum_of_support_subset (Xf i) (Yf i)
(Finsupp.support_sub.trans (Finset.union_subset_union
(Finset.Subset.refl _) (Finset.Subset.refl _)))]
exact Finset.sum_congr rfl fun a ha => by
rw [hcell Xf (fun _ => Finset.subset_union_left) i hi a ha,
hcell Yf (fun _ => Finset.subset_union_right) i hi a ha]
end Probability