Repository navigation
Expand file tree
/
Copy pathDistribution.lean
More file actions
2054 lines (1798 loc) · 91.8 KB
/
Copy pathDistribution.lean
File metadata and controls
2054 lines (1798 loc) · 91.8 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
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
module
public import Mathlib.Data.Finsupp.Basic
public import Mathlib.Data.Finsupp.Defs
public import Mathlib.Data.Finsupp.SMul
public import Mathlib.Data.Part
public import Mathlib.Basic.NNReal.Basic
public import Mathlib.Data.Fintype.Basic
public import Mathlib.Algebra.BigOperators.Group.Finset.Basic
public import Mathlib.Algebra.BigOperators.Ring.Finset
public import Mathlib.LinearAlgebra.Finsupp.LSum
public import Mathlib.Logic.Equiv.Fin.Basic
public import Mathlib.Algebra.BigOperators.Fin
/-!
# Finite distributions
A distribution on `A` is a finitely supported function `A →₀ ℝ`. A probability
distribution is a nonnegative distribution of weight `1` [1, Definitions 1, 2 and 4].
## Main definitions
* `Distribution A`, `weight`, `NonNeg`, `isProbDist`, `IsProbability`, `ProbDist`
* `mass X P`: the mass of the event `P`; `restrict`, `evalSet`, `evalPred`
* `uniform A`, `ProbDist.uniform A`, notation `𝒰[A]`: the uniform law, raw and normalized
* `marginal`, `marginalAt`: marginals of a joint distribution
* `fTransform f X`: the pushforward of `X` along `f`
* `RV`, `PMF`, `condPMF`: random variables, their laws and conditional laws,
with the notations `ℙ⟦X⟧` and `ℙ⟦X | Y⟧`
* `prod`, `iidPow`, `clonePow`, `pi`: independent and shared-coordinate products
* `ProbDist.pi`: independent samples from normalized coordinate laws
## Main results
* the mass calculus: additivity, complements, monotonicity, `mass_le_weight`;
`mass_sub_mass_le_of_le`, `abs_mass_sub_le_of_le`: laws above a common part differ by at most
the rest
* pushforward: `mass_fTransform`, `weight_fTransform`, `fTransform_comp`,
`fTransform_prod`, `fTransform_equiv_uniform`
* `uniform_congr_fintype`, `ProbDist.uniform_congr_fintype`: the uniform law does not depend on
the chosen `Fintype` enumeration
* products: `prod_apply`, `weight_prod`, `mass_prod_fst`, `mass_prod_snd`,
`mass_prod_eq_sum`, `weight_pi`, `marginalAt_pi`, `fTransform_pi_castLE` (the first `k'` of `k`
independent samples)
* probability is preserved by pushforward, products and finite mixtures:
`fTransform_isProbDist`, `prod_isProbDist`, `isProbDist_sum_smul`
* `ProbDist.mem_support_pi_iff`: independent table support is coordinatewise support
* `ProbDist.mass_eq_one_of_support`: a property of every supported draw has probability one
## 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.
-/
@[expose] public section
noncomputable section
open scoped BigOperators NNReal
namespace Probability
/-- A finite distribution over `A`: a finitely supported function `A → ℝ`.
Paper Definition 1: "A distribution X : A → ℝ≥0 with finite support."
We do NOT require weight = 1 (sub-distributions appear in Theorem 1 proof),
and the codomain is the signed reals: non-negativity is a predicate
(`Distribution.NonNeg`), not part of the carrier. -/
abbrev Distribution (A : Type*) := A →₀ ℝ
namespace Distribution
set_option linter.unusedSectionVars false
-- Decidability policy: these classical instances serve PROOFS only. Any
-- lemma whose STATEMENT contains a decidability-dependent term
-- (`Finset.filter`, `if`) must take the instance as an explicit binder
-- (`[DecidablePred p]` / `[DecidableEq B]`), so the statement instantiates
-- with the caller's ambient instance and `rw`/`simp` match in both classical
-- and `[DecidableEq]`-world files.
attribute [local instance] Classical.propDecidable
attribute [local instance] Classical.decEq
variable {A : Type*} {B : Type*} {Ω : Type*} {C : Type*}
variable [Fintype A] [Nonempty A] [Fintype B] [Nonempty B]
variable [Fintype Ω] [Nonempty Ω] [Fintype C] [Nonempty C]
/-- The weight (total mass) of a distribution.
Paper: `|X| := ∑_{a ∈ A} X(a)`. -/
def weight (X : Distribution A) : ℝ :=
X.sum fun _ w => w
/-- Pointwise non-negativity of a finite-support mass function. Over the
signed carrier `A →₀ ℝ` this is no longer structural; honest distributions
carry it as a predicate. -/
def NonNeg {A : Type*} (X : Distribution A) : Prop := ∀ a, 0 ≤ X a
omit [Fintype A] [Nonempty A] in
lemma NonNeg.weight_nonneg {X : Distribution A} (hX : X.NonNeg) : 0 ≤ X.weight :=
Finset.sum_nonneg fun a _ => hX a
omit [Nonempty A] in
/-- On a finite carrier, support-based weight is the ordinary sum over all
points. (Needs `Fintype A` but not `Nonempty A` — the latter is a section-variable leak.) -/
lemma weight_eq_sum (X : Distribution A) :
X.weight = ∑ a : A, X a := by
simpa [weight] using
(Finsupp.sum_fintype (f := X) (g := fun _ w => w) (h := by intro _; rfl))
/-- The weight is the sum over **any** finite set containing the support:
points outside the support carry no mass. This is the `Fintype`-free
companion of `weight_eq_sum`, and the form needed on carriers that are
genuinely infinite (transcript spaces, system laws). -/
lemma weight_eq_sum_of_support_subset {A : Type*} (X : Distribution A)
{s : Finset A} (hs : X.support ⊆ s) :
X.weight = ∑ a ∈ s, X a :=
Finsupp.sum_of_support_subset X hs (fun _ w => w) (fun _ _ => rfl)
omit [Nonempty A] in
/-- A mass function on a finite carrier as a finite-support distribution. -/
def ofFiniteMassFunction (law : A → ℝ) : Distribution A :=
Finsupp.onFinset Finset.univ law (by intro a _; exact Finset.mem_univ a)
omit [Nonempty A] in
@[simp]
lemma ofFiniteMassFunction_apply (law : A → ℝ) (a : A) :
ofFiniteMassFunction law a = law a := by
simp [ofFiniteMassFunction]
omit [Nonempty A] in
/-- The total weight of a finite mass function as a distribution is the sum of
its masses over the carrier. -/
lemma weight_ofFiniteMassFunction (law : A → ℝ) :
(ofFiniteMassFunction law).weight = ∑ a : A, law a := by
simp [weight_eq_sum, ofFiniteMassFunction]
/-- A distribution is a probability distribution if it is pointwise
non-negative and its weight is 1. (Over the `NNReal` carrier the first
conjunct was structural; over `ℝ` it is part of the definition, so the
`ProbDist` boundary keeps its meaning.) -/
def isProbDist (X : Distribution A) : Prop := X.NonNeg ∧ X.weight = 1
/-- Lanzenberger, Definition 2.1 (p. 11): “A probability distribution is a
distribution X with weight 1”. The signed carrier also requires nonnegativity. -/
class IsProbability (X : Distribution A) : Prop where
nonNeg : X.NonNeg
weight_eq_one : X.weight = 1
omit [Fintype A] [Nonempty A] in
/-- The probability capability supplies the underlying distribution predicate. -/
lemma IsProbability.isProbDist (X : Distribution A) [IsProbability X] : X.isProbDist :=
⟨IsProbability.nonNeg, IsProbability.weight_eq_one⟩
omit [Fintype A] [Nonempty A] in
lemma isProbDist.nonNeg {X : Distribution A} (hX : X.isProbDist) : X.NonNeg := hX.1
omit [Fintype A] [Nonempty A] in
lemma isProbDist.weight_eq {X : Distribution A} (hX : X.isProbDist) : X.weight = 1 :=
hX.2
/-- Probability distributions are distributions with total mass one. -/
abbrev ProbDist (A : Type*) := {X : Distribution A // X.isProbDist}
instance (X : ProbDist A) : IsProbability X.val := ⟨X.property.1, X.property.2⟩
/-- Probability distributions evaluate like their underlying mass functions. -/
instance : CoeFun (ProbDist A) (fun _ => A → ℝ) where
coe X := X.val
/-- Represent a probability law by its finite support subtype.
This is useful when a downstream side condition should range over exactly the
samples that can occur under the law, without requiring the whole ambient
carrier to be finite or relevant. -/
noncomputable def supportProbDist {A : Type*} (D : ProbDist A) :
ProbDist {a : A // a ∈ D.val.support} :=
⟨ofFiniteMassFunction (fun a : {a : A // a ∈ D.val.support} => D.val a.1), by
refine ⟨fun a => by simpa using D.property.nonNeg a.1, ?_⟩
rw [weight_ofFiniteMassFunction]
rw [← D.property.weight_eq]
unfold weight
rw [Finsupp.sum]
exact Finset.sum_attach D.val.support (fun a => D.val a)⟩
@[simp]
lemma supportProbDist_apply {A : Type*} (D : ProbDist A)
(a : {a : A // a ∈ D.val.support}) :
supportProbDist D a = D.val a.1 := by
rfl
omit [Fintype A] [Nonempty A] in
/-- A unit point mass is a probability distribution. -/
lemma isProbDist_single (a : A) :
isProbDist (Finsupp.single a (1 : ℝ) : Distribution A) := by
constructor
· intro b
rw [Finsupp.single_apply]
split <;> norm_num
· rw [weight, Finsupp.sum_single_index rfl]
omit [Fintype A] [Nonempty A] in
/-- The point mass at `a`. -/
noncomputable def ProbDist.single (a : A) : ProbDist A :=
⟨Finsupp.single a 1, isProbDist_single a⟩
omit [Fintype A] [Nonempty A] in
/-- A probability distribution has a sample. -/
lemma ProbDist.nonempty (P : ProbDist A) : Nonempty A := by
by_contra hA
rw [not_nonempty_iff] at hA
have hw := P.2.2
rw [weight, Finsupp.sum, Finset.eq_empty_of_isEmpty P.1.support] at hw
simp at hw
omit [Fintype A] [Nonempty A] in
/-- A unit point mass has the probability capability. -/
instance (a : A) : IsProbability (Finsupp.single a (1 : ℝ) : Distribution A) :=
⟨(isProbDist_single a).1, (isProbDist_single a).2⟩
/-- Evaluate a distribution on a subset.
Paper: `X(B) := ∑_{a ∈ B} X(a)`. -/
def evalSet (X : Distribution A) (B : Finset A) : ℝ :=
∑ a ∈ B, X a
/-- Event mass of a distribution, summed over its finite support.
This is the support-based form of CR18/LM20 Definition 1 notation
`X(A) := ∑_{a ∈ A} X(a)`. Unlike `evalPred`, it does not require
`Fintype A`; the finite support is already carried by `Distribution A = A →₀ ℝ`. -/
noncomputable def mass (X : Distribution A) (P : A → Prop) : ℝ :=
X.sum fun a w => if P a then w else 0
omit [Fintype A] [Nonempty A] in
/-- Event mass of a non-negative distribution is non-negative. -/
lemma NonNeg.mass_nonneg {X : Distribution A} (hX : X.NonNeg) (P : A → Prop) :
0 ≤ X.mass P :=
Finset.sum_nonneg fun a _ => by
by_cases h : P a <;> simp [h, hX a]
omit [Fintype A] [Nonempty A] in
/-- Event mass is additive in the distribution: `(X + Y)(P) = X(P) + Y(P)`.
Signed layer, no `Fintype` — the companion of `weight_smul`/`mass_smul`
(`Probability.Lift`) on the additive side. -/
lemma mass_add (X Y : Distribution A) (P : A → Prop) :
(X + Y).mass P = X.mass P + Y.mass P := by
classical
unfold mass
rw [Finsupp.sum_add_index' (fun a => by simp)
(fun a b₁ b₂ => by by_cases h : P a <;> simp [h])]
omit [Fintype A] [Nonempty A] in
/-- Event mass as a filtered sum over **any** finite set containing the
support: the summands vanish off the support, so the value does not depend on
which superset is chosen. This is the working form of `mass` on carriers that
are genuinely infinite, and the companion of
`Distribution.weight_eq_sum_of_support_subset` and
`Probability.statDist_eq_sum_of_support_subset`.
The decidability of `P` is an explicit binder, per this file's decidability
policy: the statement contains a `Finset.filter`, so it must instantiate with
the caller's ambient instance. -/
lemma mass_eq_sum_of_support_subset (X : Distribution A) {s : Finset A}
(hs : X.support ⊆ s) (P : A → Prop) [DecidablePred P] :
X.mass P = ∑ a ∈ s.filter P, X a := by
classical
rw [Finset.sum_filter, mass,
Finsupp.sum_of_support_subset X hs _ (fun a _ => by simp)]
refine Finset.sum_congr rfl fun a _ => ?_
by_cases h : P a <;> simp [h]
/-- Restrict a distribution to an event, dropping all mass outside it. -/
noncomputable def restrict {A : Type*} (X : Distribution A) (P : A → Prop) : Distribution A :=
Finsupp.filter P X
@[simp]
lemma restrict_apply {A : Type*} (X : Distribution A) (P : A → Prop) (a : A) :
X.restrict P a = if P a then X a else 0 := by
simp [restrict, Finsupp.filter_apply]
/-- Restriction preserves non-negativity. -/
lemma NonNeg.restrict {A : Type*} {X : Distribution A} (hX : X.NonNeg) (P : A → Prop) :
(X.restrict P).NonNeg := fun a => by
rw [restrict_apply]
by_cases h : P a <;> simp [h, hX a]
/-- The mass of `P` after restricting to `Q` is the mass of `P ∩ Q`. -/
lemma mass_restrict {A : Type*} (X : Distribution A) (P Q : A → Prop) :
(X.restrict Q).mass P = X.mass (fun a => P a ∧ Q a) := by
unfold restrict mass Finsupp.sum
simp only [Finsupp.support_filter, Finsupp.filter_apply]
rw [Finset.sum_filter]
apply Finset.sum_congr rfl
intro a _ha
by_cases hP : P a <;> by_cases hQ : Q a <;> simp [hP, hQ]
/-- Restricting keeps only mass that was there. -/
lemma support_restrict_subset {A : Type*} (μ : Distribution A) (P : A → Prop) :
(μ.restrict P).support ⊆ μ.support := fun a ha => by
-- The restriction at `a` is `μ a` or zero; it is nonzero, so it is `μ a`.
rw [Finsupp.mem_support_iff, restrict_apply] at ha
rw [Finsupp.mem_support_iff]
split_ifs at ha
· exact ha
· exact absurd rfl ha
/-- Restricting twice is restricting to both events. -/
lemma restrict_restrict {A : Type*} (μ : Distribution A) (P Q : A → Prop) :
(μ.restrict P).restrict Q = μ.restrict fun a => P a ∧ Q a := by
-- Pointwise: `μ a` exactly when both events hold.
ext a
simp only [restrict_apply]
by_cases hP : P a <;> by_cases hQ : Q a <;> simp [hP, hQ]
/-- Restriction depends only on the event up to equivalence. -/
lemma restrict_congr {A : Type*} (μ : Distribution A) {P Q : A → Prop} (h : ∀ a, P a ↔ Q a) :
μ.restrict P = μ.restrict Q := by
-- Pointwise, the two events are the same condition.
ext a
simp only [restrict_apply, h]
/-- Restricting to the impossible event leaves nothing. -/
lemma restrict_false {A : Type*} (μ : Distribution A) : μ.restrict (fun _ => False) = 0 := by
-- Pointwise, the condition never holds.
ext a
simp
/-- A point mass restricted to an event: itself or nothing. -/
lemma single_restrict {A : Type*} (a : A) (w : ℝ) (P : A → Prop) :
Distribution.restrict (Finsupp.single a w : Distribution A) P =
if P a then Finsupp.single a w else 0 := by
-- The only point with mass is `a`: kept if `P a`, dropped otherwise.
by_cases h : P a
· simp [restrict, Finsupp.filter_single_of_pos _ h, h]
· simp [restrict, Finsupp.filter_single_of_neg _ h, h]
lemma mass_true {A : Type*} (X : Distribution A) :
X.mass (fun _ => True) = X.weight := by
unfold mass weight
simp
/-- A pointwise-impossible event has zero mass. -/
lemma mass_eq_zero_of_forall_not {A : Type*} (X : Distribution A) {P : A → Prop}
(h : ∀ a, ¬ P a) : X.mass P = 0 := by
unfold mass Finsupp.sum
exact Finset.sum_eq_zero fun a _ => ite_eq_right (h a)
/-- Mass depends only on the event up to logical equivalence. -/
lemma mass_congr {A : Type*} (X : Distribution A) {P Q : A → Prop} (h : ∀ a, P a ↔ Q a) :
X.mass P = X.mass Q := by
unfold mass
congr 1
funext a w
simp only [h a]
/-- `mass_congr` with the equivalence required only on the support — mass sums
over the support, so that is all it reads. -/
lemma mass_congr_of_support {A : Type*} (X : Distribution A) {P Q : A → Prop}
(h : ∀ a ∈ X.support, (P a ↔ Q a)) : X.mass P = X.mass Q := by
unfold mass
refine Finsupp.sum_congr fun a ha => ?_
simp only [h a ha]
/-- A property holding on the support of a normalized law has probability one. -/
lemma ProbDist.mass_eq_one_of_support
-- `A` carries outcomes; `P` is their normalized law and `event` their tested property.
{A : Type*} (P : ProbDist A) (event : A → Prop)
-- `h` asserts `event` for every draw with nonzero mass under `P`.
(h : ∀ a ∈ P.1.support, event a) : P.1.mass event = 1 := by
-- The event agrees with `True` wherever `P` has mass, so its mass is the total weight.
rw [mass_congr_of_support P.1 (Q := fun _ => True) (fun a ha => iff_true_intro (h a ha)),
mass_true]
exact P.2.2
open scoped Classical in
/-- An event and its complement partition the total mass: `X(P) + X(¬P) = |X|`. -/
lemma mass_add_compl {A : Type*} (X : Distribution A) (P : A → Prop) :
X.mass P + X.mass (fun a => ¬ P a) = X.weight := by
unfold mass weight Finsupp.sum
rw [← Finset.sum_add_distrib]
exact Finset.sum_congr rfl fun a _ => by by_cases h : P a <;> simp [h]
/-- **Splitting one event by another**: `X(Q) = X(P ∧ Q) + X(¬P ∧ Q)`. The
relative form of `mass_add_compl`, which is the case `Q = True`. Signed layer:
the pointwise indicator identity, summed. -/
lemma mass_and_add_mass_not_and {A : Type*} (X : Distribution A) (P Q : A → Prop) :
X.mass (fun a => P a ∧ Q a) + X.mass (fun a => ¬ P a ∧ Q a) = X.mass Q := by
classical
simp only [Distribution.mass, Finsupp.sum]
rw [← Finset.sum_add_distrib]
refine Finset.sum_congr rfl fun a _ => ?_
by_cases hp : P a <;> by_cases hq : Q a <;> simp [hp, hq]
/-- The mass of an event never exceeds the total mass of a non-negative
distribution: `X(P) ≤ |X|`. -/
lemma mass_le_weight {A : Type*} {X : Distribution A} (hX : X.NonNeg) (P : A → Prop) :
X.mass P ≤ X.weight := by
rw [← mass_add_compl X P]
exact le_add_of_nonneg_right (hX.mass_nonneg _)
/-- Monotonicity of event mass: a weaker event carries at least as much mass. -/
lemma mass_mono {A : Type*} {X : Distribution A} (hX : X.NonNeg) {P Q : A → Prop}
(h : ∀ a, P a → Q a) : X.mass P ≤ X.mass Q := by
classical
simp only [Distribution.mass, Finsupp.sum]
refine Finset.sum_le_sum fun a _ => ?_
by_cases hP : P a
· simp [hP, h a hP]
· by_cases hQ : Q a <;> simp [hP, hQ, hX a]
/-- **A common part bounds the difference of masses**, one side: if `μ₁` has weight one and both
`μ₁` and `μ₂` lie above `ν`, then `μ₁` exceeds `μ₂` on every event by at most the weight `μ₁` does
not share with `ν`, `μ₁(P) − μ₂(P) ≤ 1 − |ν|`. -/
lemma mass_sub_mass_le_of_le {A : Type*} {μ₁ μ₂ ν : Distribution A} (h₁ : μ₁.weight = 1)
(hν₁ : ∀ a, ν a ≤ μ₁ a) (hν₂ : ∀ a, ν a ≤ μ₂ a) (P : A → Prop) :
μ₁.mass P - μ₂.mass P ≤ 1 - ν.weight := by
-- Plan. Each μ above ν splits as ν + (μ − ν), with μ − ν ≥ 0. So
-- μ₁(P) − μ₂(P) = (μ₁ − ν)(P) − (μ₂ − ν)(P) ≤ (μ₁ − ν)(P) ≤ |μ₁ − ν| = 1 − |ν|.
-- Mass is additive, ν + (μ − ν) = μ: so (μ − ν)(P) = μ(P) − ν(P).
have hsub : ∀ μ : Distribution A, (μ - ν).mass P = μ.mass P - ν.mass P := by
intro μ
have := mass_add ν (μ - ν) P
rw [add_sub_cancel] at this
linarith
-- μ − ν ≥ 0 pointwise, for μ above ν.
have hnn : ∀ μ : Distribution A, (∀ a, ν a ≤ μ a) → (μ - ν).NonNeg := fun μ hle a => by
simp only [Finsupp.coe_sub, Pi.sub_apply, sub_nonneg]
exact hle a
-- |μ₁ − ν| = |μ₁| − |ν| = 1 − |ν|: additivity on the sure event.
have hwt : (μ₁ - ν).weight = 1 - ν.weight := by
have := mass_add ν (μ₁ - ν) (fun _ => True)
rw [add_sub_cancel, mass_true, mass_true, mass_true, h₁] at this
linarith
-- (μ₂ − ν)(P) ≥ 0, and a mass of the nonnegative μ₁ − ν is at most its weight.
have hlo := (hnn μ₂ hν₂).mass_nonneg P
have hhi := mass_le_weight (hnn μ₁ hν₁) P
rw [hsub] at hlo hhi
linarith
/-- **A common part bounds the difference of masses**: two laws of weight one that both lie
above `ν` differ on every event by at most the weight they do not share with `ν`,
`|μ₁(P) − μ₂(P)| ≤ 1 − |ν|`. -/
lemma abs_mass_sub_le_of_le {A : Type*} {μ₁ μ₂ ν : Distribution A} (h₁ : μ₁.weight = 1)
(h₂ : μ₂.weight = 1) (hν₁ : ∀ a, ν a ≤ μ₁ a) (hν₂ : ∀ a, ν a ≤ μ₂ a) (P : A → Prop) :
|μ₁.mass P - μ₂.mass P| ≤ 1 - ν.weight :=
-- The one-sided bound, for μ₁ over μ₂ and for μ₂ over μ₁.
abs_sub_le_iff.mpr ⟨mass_sub_mass_le_of_le h₁ hν₁ hν₂ P, mass_sub_mass_le_of_le h₂ hν₂ hν₁ P⟩
/-- **A single atom is below the mass of any event it satisfies**: `X a ≤ X(P)`
when `P a`. Non-negativity is what makes the other summands harmless — the
signed-carrier form of `Finset.single_le_sum` for `Distribution.mass`.
A support atom witnessing an event forces that event to carry positive mass,
so an event of mass zero has no witness in the support. -/
lemma apply_le_mass {A : Type*} {X : Distribution A} (hX : X.NonNeg)
{P : A → Prop} {a : A} (ha : P a) : X a ≤ X.mass P := by
classical
by_cases hsupport : a ∈ X.support
· unfold Distribution.mass Finsupp.sum
calc X a = (if P a then X a else 0) := (ite_eq_left ha).symm
_ ≤ _ := Finset.single_le_sum
(f := fun a' => if P a' then X a' else 0)
(fun a' _ => by by_cases h : P a' <;> simp [h, hX a']) hsupport
· rw [Finsupp.notMem_support_iff.mp hsupport]
exact hX.mass_nonneg _
/-- `mass_mono` with the implication required only on the support — mass is
a support sum, so off-support behavior of the events is irrelevant. -/
lemma mass_mono_on_support {A : Type*} {X : Distribution A} (hX : X.NonNeg)
{P Q : A → Prop} (h : ∀ a ∈ X.support, P a → Q a) :
X.mass P ≤ X.mass Q := by
classical
simp only [Distribution.mass, Finsupp.sum]
refine Finset.sum_le_sum fun a ha => ?_
by_cases hP : P a
· simp [hP, h a ha hP]
· by_cases hQ : Q a <;> simp [hP, hQ, hX a]
/-- Subadditivity over a disjunction — the two-event union bound. -/
lemma mass_or_le {A : Type*} {X : Distribution A} (hX : X.NonNeg) (P Q : A → Prop) :
X.mass (fun a => P a ∨ Q a) ≤ X.mass P + X.mass Q := by
classical
simp only [Distribution.mass, Finsupp.sum]
rw [← Finset.sum_add_distrib]
refine Finset.sum_le_sum fun a _ => ?_
by_cases hpq : P a ∨ Q a
· rw [ite_eq_left hpq]
rcases hpq with hp | hq
· rw [ite_eq_left hp]
refine le_add_of_nonneg_right ?_
by_cases hq : Q a <;> simp [hq, hX a]
· rw [ite_eq_left hq]
refine le_add_of_nonneg_left ?_
by_cases hp : P a <;> simp [hp, hX a]
· rw [ite_eq_right hpq]
push Not at hpq
simp [hpq.1, hpq.2]
/-- The `Finset`-indexed union bound — `mass_or_le` iterated. -/
lemma mass_exists_le {A ι : Type*} {X : Distribution A} (hX : X.NonNeg) (s : Finset ι)
(P : ι → A → Prop) :
X.mass (fun a => ∃ i ∈ s, P i a) ≤ ∑ i ∈ s, X.mass (P i) := by
classical
induction s using Finset.induction with
| empty => simpa using le_of_eq (mass_eq_zero_of_forall_not X (by simp))
| insert a t ha ih =>
rw [Finset.sum_insert ha]
refine le_trans (le_trans (mass_mono hX (Q := fun x => P a x ∨ ∃ i ∈ t, P i x)
(by rintro x ⟨i, hi, hix⟩
rcases Finset.mem_insert.mp hi with rfl | hi'
· exact Or.inl hix
· exact Or.inr ⟨i, hi', hix⟩))
(mass_or_le hX _ _)) ?_
linarith [ih]
/-- **The union bound over unordered pairs.** For a nonnegative distribution `X`, a finite
index set `s`, a symmetric bad event (`bad i j → bad j i`) and `X.mass (bad i j) ≤ δ` for all
distinct `i, j ∈ s`,
X.mass (∃ i, j ∈ s, i ≠ j ∧ bad i j) ≤ C(|s|, 2) · δ .
Symmetry charges each pair once; `mass_exists_le` over ordered pairs gives `|s|(|s|-1) δ`.
`X` need not be normalized and `ι` need not be finite. No `0 ≤ δ` is assumed: for `|s| ≤ 1`
the event is empty and `C(|s|, 2) = 0`; otherwise `δ` bounds a mass. -/
lemma mass_exists_pair_le_choose {Ω ι : Type*} (X : Distribution Ω) (hX : X.NonNeg)
(s : Finset ι) (bad : ι → ι → Ω → Prop)
(hsym : ∀ i j ω, bad i j ω → bad j i ω) (δ : ℝ)
(hbad : ∀ i ∈ s, ∀ j ∈ s, i ≠ j → X.mass (bad i j) ≤ δ) :
X.mass (fun ω => ∃ i ∈ s, ∃ j ∈ s, i ≠ j ∧ bad i j ω) ≤
(s.card.choose 2 : ℝ) * δ := by
classical
-- Induct on `s`, adding one index `i` at a time; `hbad` must follow `s`.
revert hbad
induction s using Finset.induction_on with
| empty =>
intro _
-- `s = ∅`: no pair, mass `0 = C(0, 2) · δ`.
rw [mass_eq_zero_of_forall_not X (by
rintro ω ⟨i, hi, _⟩
exact Finset.notMem_empty i hi)]
simp only [Finset.card_empty, Nat.choose_zero_succ, Nat.cast_zero, zero_mul]
exact le_rfl
| @insert i s hi ih =>
intro hbad
-- A bad pair in `insert i s` lies in `s` or contains `i`; symmetry puts `i` first:
-- bad pair in `insert i s` → bad pair in `s` ∨ ∃ j ∈ s, bad i j.
have hcover (ω : Ω) :
(∃ a ∈ insert i s, ∃ b ∈ insert i s, a ≠ b ∧ bad a b ω) →
(∃ a ∈ s, ∃ b ∈ s, a ≠ b ∧ bad a b ω) ∨
∃ j ∈ s, bad i j ω := by
rintro ⟨a, ha, b, hb, hab, he⟩
rcases Finset.mem_insert.mp ha with hai | ha
· rcases Finset.mem_insert.mp hb with hbi | hb
-- `a = i = b` contradicts `a ≠ b`.
· exact (hab (hai.trans hbi.symm)).elim
-- `(i, b)`: already incident with `i` first.
· exact Or.inr ⟨b, hb, hai ▸ he⟩
· rcases Finset.mem_insert.mp hb with hbi | hb
-- `(a, i)`: symmetry gives `bad i a`.
· exact Or.inr ⟨a, ha, hsym a i ω (hbi ▸ he)⟩
-- `(a, b)` with both in `s`: an old pair.
· exact Or.inl ⟨a, ha, b, hb, hab, he⟩
-- Monotonicity and the two-event union bound: mass ≤ old + new.
refine (mass_mono hX hcover).trans ?_
refine (mass_or_le hX _ _).trans ?_
-- Old pairs: the induction hypothesis on `s`, `≤ C(|s|, 2) · δ`.
have hold := ih (fun a ha b hb => hbad a (Finset.mem_insert_of_mem ha)
b (Finset.mem_insert_of_mem hb))
-- New pairs `(i, j)`, `j ∈ s`: `j ≠ i` as `i ∉ s`, so each costs `δ`; total `|s| · δ`.
have hnew : X.mass (fun ω => ∃ j ∈ s, bad i j ω) ≤ s.card * δ := by
refine (mass_exists_le hX s _).trans ?_
calc
_ ≤ ∑ j ∈ s, δ := Finset.sum_le_sum fun j hj =>
hbad i (Finset.mem_insert_self i s) j (Finset.mem_insert_of_mem hj)
(fun he => hi (he ▸ hj))
_ = _ := by rw [Finset.sum_const, nsmul_eq_mul]
-- Pascal: `C(n, 2) + n = C(n + 1, 2)` with `n = |s|` and `|insert i s| = n + 1`.
refine (add_le_add hold hnew).trans_eq ?_
rw [Finset.card_insert_of_notMem hi, Nat.choose_succ_succ, Nat.choose_one_right,
Nat.cast_add]
ring
/-- Restricting to an event leaves exactly the mass of that event. -/
lemma weight_restrict {A : Type*} (X : Distribution A) (P : A → Prop) :
(X.restrict P).weight = X.mass P := by
rw [← mass_true (X.restrict P), mass_restrict]
simp
end Distribution
namespace CryptoNotation
/-- Event-mass notation:
- `Pr[φ(x) | x ←$ D]` expands to `Distribution.mass D (fun x => φ x)`.
This follows CR18/LM20 Definition 1: distributions are finite-support mass
functions, and event probability/mass is obtained by summing the weights in the
event over that support. -/
scoped syntax "Pr[" term " | " Lean.Parser.Term.funBinder " ←$ " term "]" : term
scoped macro_rules
| `(Pr[$body | $b:funBinder ←$ $D]) =>
`(Probability.Distribution.mass $D (fun $b => $body))
/-- Conditional event-mass notation:
- `Pr[φ(x) | x ←$ D, ψ(x)]` expands to
`Distribution.cond D (fun x => φ x) (fun x => ψ x)`.
The result is partial, undefined exactly when the conditioning event has mass
zero. -/
scoped syntax "Pr[" term " | " Lean.Parser.Term.funBinder " ←$ " term ", " term "]" : term
scoped macro_rules
| `(Pr[$body | $b:funBinder ←$ $D, $cond]) =>
`(Probability.Distribution.cond $D (fun $b => $body) (fun $b => $cond))
end CryptoNotation
namespace Distribution
set_option linter.unusedSectionVars false
-- Decidability policy: these classical instances serve PROOFS only. Any
-- lemma whose STATEMENT contains a decidability-dependent term
-- (`Finset.filter`, `if`) must take the instance as an explicit binder
-- (`[DecidablePred p]` / `[DecidableEq B]`), so the statement instantiates
-- with the caller's ambient instance and `rw`/`simp` match in both classical
-- and `[DecidableEq]`-world files.
attribute [local instance] Classical.propDecidable
attribute [local instance] Classical.decEq
variable {A : Type*} {B : Type*} {Ω : Type*} {C : Type*}
variable [Fintype A] [Nonempty A] [Fintype B] [Nonempty B]
variable [Fintype Ω] [Nonempty Ω] [Fintype C] [Nonempty C]
/-- Evaluate a distribution on a predicate: the total mass of elements satisfying `P`.
`evalPred X P = ∑_{a : P(a)} X(a)`
This finite-carrier predicate evaluator is equivalent to
`evalSet X (Finset.univ.filter P)`. Prefer `mass` when the finite support of
the distribution itself should supply the finiteness, as in CR18. -/
def evalPred (X : Distribution A) (P : A → Prop) : ℝ :=
∑ a ∈ Finset.univ.filter P, X a
@[simp]
lemma evalPred_eq_evalSet (X : Distribution A) (P : A → Prop) :
X.evalPred P = X.evalSet (Finset.univ.filter P) := by
rfl
omit [Fintype A] [Nonempty A] [Fintype B] [Nonempty B]
[Fintype Ω] [Nonempty Ω] [Fintype C] [Nonempty C] in
/-- The uniform distribution over a finite nonempty type.
`uniform A` assigns mass `1 / |A|` to each element. -/
def uniform (A : Type*) [Fintype A] [Nonempty A] : Distribution A :=
Finsupp.equivFunOnFinite.invFun (fun _ => (1 : ℝ) / (Fintype.card A : ℝ))
/-! ### Uniform distribution lemmas -/
/-- Pointwise evaluation of the uniform distribution. -/
lemma uniform_apply (a : A) :
(uniform A) a = 1 / (Fintype.card A : ℝ) := by
simp [uniform, Finsupp.equivFunOnFinite]
/-- The uniform distribution is pointwise non-negative. -/
lemma uniform_nonNeg : (uniform A).NonNeg := fun a => by
rw [uniform_apply]
positivity
/-- The uniform distribution has weight 1. -/
lemma weight_uniform :
(uniform A).weight = 1 := by
rw [weight_eq_sum]
have h_card_pos : (0 : ℝ) < (Fintype.card A : ℝ) :=
Nat.cast_pos.mpr Fintype.card_pos
rw [Finset.sum_congr rfl (fun a _ => uniform_apply a)]
rw [Finset.sum_const, Finset.card_univ, nsmul_eq_mul, mul_div_cancel₀]
exact_mod_cast h_card_pos.ne'
/-- The uniform distribution is a probability distribution. -/
lemma uniform_isProbDist :
(uniform A).isProbDist := ⟨uniform_nonNeg, weight_uniform⟩
instance : IsProbability (uniform A) := ⟨uniform_nonNeg, weight_uniform⟩
/-- The uniform law `uniform A` as a probability distribution, `𝒰[A].1 a = 1 / |A|`: the same
masses, together with the proof `uniform_isProbDist` that they are normalized. -/
noncomputable def ProbDist.uniform (A : Type*) [Fintype A] [Nonempty A] : ProbDist A :=
⟨Distribution.uniform A, uniform_isProbDist⟩
omit [Fintype A] [Nonempty A] [Fintype B] [Nonempty B]
[Fintype Ω] [Nonempty Ω] [Fintype C] [Nonempty C] in
/-- Any two `Fintype` enumerations of the same nonempty type `A` define the same uniform
distribution. -/
lemma uniform_congr_fintype
-- `A` is the sample carrier; `inst₁` and `inst₂` are any two finite enumerations of `A`.
{A : Type*} (inst₁ inst₂ : Fintype A) [Nonempty A] :
@uniform A inst₁ _ = @uniform A inst₂ _ := by
-- Every carrier has at most one `Fintype` structure (`Subsingleton.elim`).
cases Subsingleton.elim inst₁ inst₂
rfl
omit [Fintype A] [Nonempty A] [Fintype B] [Nonempty B]
[Fintype Ω] [Nonempty Ω] [Fintype C] [Nonempty C] in
/-- Any two `Fintype` enumerations of the same nonempty type `A` define the same normalized
uniform law. -/
lemma ProbDist.uniform_congr_fintype
-- `A` is the sample carrier; `inst₁` and `inst₂` are any two finite enumerations of `A`.
{A : Type*} (inst₁ inst₂ : Fintype A) [Nonempty A] :
@ProbDist.uniform A inst₁ _ = @ProbDist.uniform A inst₂ _ := by
-- Every carrier has at most one `Fintype` structure (`Subsingleton.elim`).
cases Subsingleton.elim inst₁ inst₂
rfl
/-- The unique probability distribution on a one-point sample space. -/
def unitProbDist : ProbDist PUnit :=
⟨uniform PUnit, uniform_isProbDist⟩
/-- The marginal distribution obtained by projecting onto the first component.
Paper Definition 2: Given a joint distribution X_{A×B}, the marginal X_A is
X_A(a) := ∑_{b ∈ B} X_{A×B}(a, b). -/
def marginal (X : Distribution (A × B)) : Distribution A :=
X.sum (fun ⟨a, _⟩ w => Finsupp.single a w)
/-- The marginal of a distribution over a finite product, projected to one
coordinate.
For a family of alphabets `X : ι → Type*`, a point of the product is a function
`x : (i : ι) → X i`. The marginal at `j : ι` is the pushforward by evaluation
at `j`, i.e. `x ↦ x j`. -/
def marginalAt {ι : Type*} {X : ι → Type*}
(F : Distribution ((i : ι) → X i)) (j : ι) : Distribution (X j) :=
F.sum (fun x w => Finsupp.single (x j) w)
/-- Pointwise form of `marginalAt`: the mass at `xj` is the mass of the fiber
of product points whose `j`-th coordinate equals `xj`. -/
lemma marginalAt_apply {ι : Type*} {X : ι → Type*}
(F : Distribution ((i : ι) → X i)) (j : ι) (xj : X j) :
marginalAt F j xj = F.mass (fun x => x j = xj) := by
unfold marginalAt mass
simp only [Finsupp.sum_apply, Finsupp.single_apply]
/-- The f-transformation of a distribution.
Paper Definition 4: Given a distribution X over A and f : A → B,
(f(X))(b) := ∑_{a : f(a) = b} X(a).
This is the pushforward measure in the discrete case. -/
def fTransform (f : A → B) (X : Distribution A) : Distribution B :=
X.sum (fun a w => Finsupp.single (f a) w)
/-- Pushforward composes. -/
lemma fTransform_fTransform {A B C : Type*}
(f : B → C) (g : A → B) (X : Distribution A) :
fTransform f (fTransform g X) = fTransform (f ∘ g) X := by
change Finsupp.mapDomain f (Finsupp.mapDomain g X) = Finsupp.mapDomain (f ∘ g) X
exact (Finsupp.mapDomain_comp (v := X)).symm
/-- Pointwise evaluation of a pushforward is the mass of the fiber. -/
lemma fTransform_apply_eq_mass {A B : Type*}
(f : A → B) (X : Distribution A) (b : B) :
fTransform f X b = X.mass (fun a => f a = b) := by
unfold fTransform mass
simp only [Finsupp.sum_apply, Finsupp.single_apply]
/-- Evaluating a pushforward at an image point of an injective map recovers the
original mass. -/
lemma fTransform_injective_apply {A B : Type*}
(X : Distribution A) (f : A → B) (hf : Function.Injective f) (a : A) :
fTransform f X (f a) = X a := by
simp only [fTransform, Finsupp.sum, Finsupp.coe_finsetSum, Finset.sum_apply,
Finsupp.single_apply]
rw [Finset.sum_eq_single a]
· simp
· intro a' _ hne
simp [hf.ne hne]
· intro ha
simp [Finsupp.notMem_support_iff.mp ha]
/-- Evaluating a pushforward away from the image of the map gives zero mass. -/
lemma fTransform_apply_of_forall_ne {A B : Type*}
(X : Distribution A) (f : A → B) (b : B) (h : ∀ a, f a ≠ b) :
fTransform f X b = 0 := by
simp only [fTransform, Finsupp.sum, Finsupp.coe_finsetSum, Finset.sum_apply,
Finsupp.single_apply]
apply Finset.sum_eq_zero
intro a _
simp [h a]
/-- Every support element of a pushforward has a support witness in the source
distribution. -/
lemma mem_support_fTransform {A B : Type*} (f : A → B) (X : Distribution A) {b : B}
(hb : b ∈ (fTransform f X).support) : ∃ a ∈ X.support, f a = b := by
classical
have hsub : (fTransform f X).support ⊆ X.support.image f := Finsupp.mapDomain_support
simpa [fTransform] using hsub hb
/-- A weighted sum over a pushforward pulls the summand back. -/
lemma sum_fTransform_mul {A B : Type*} (f : A → B) (X : Distribution A) (g : B → ℝ) :
(fTransform f X).sum (fun b w => w * g b) = X.sum (fun a w => w * g (f a)) := by
classical
unfold fTransform
exact Finsupp.sum_mapDomain_index (fun _ => zero_mul _) (fun _ _ _ => add_mul _ _ _)
/-- Event mass under a pushforward is event mass of the preimage. -/
lemma mass_fTransform {A B : Type*}
(f : A → B) (X : Distribution A) (P : B → Prop) :
(fTransform f X).mass P = X.mass (fun a => P (f a)) := by
unfold fTransform mass
change (Finsupp.mapDomain f X).sum (fun b w => if P b then w else 0) =
X.sum (fun a w => if P (f a) then w else 0)
rw [Finsupp.sum_mapDomain_index
(fun b => by by_cases hb : P b <;> simp [hb])
(fun b m₁ m₂ => by by_cases hb : P b <;> simp [hb])]
/-- The pushforward of a point mass. -/
lemma fTransform_single {A B : Type*} (f : A → B) (a : A) (w : ℝ) :
fTransform f (Finsupp.single a w) = Finsupp.single (f a) w :=
Finsupp.mapDomain_single
/-- Restricting a pushforward is pushing forward the restriction to the preimage. -/
lemma restrict_fTransform {A B : Type*} (f : A → B) (μ : Distribution A) (P : B → Prop) :
(fTransform f μ).restrict P = fTransform f (μ.restrict fun a => P (f a)) := by
-- At `b`, both sides are masses of fibres of `f`.
ext b
rw [restrict_apply, fTransform_apply_eq_mass, fTransform_apply_eq_mass, mass_restrict]
split_ifs with h
· -- `P b`: on the fibre of `b`, the condition `P (f a)` holds
exact mass_congr _ fun a => ⟨fun ha => ⟨ha, ha ▸ h⟩, fun ha => ha.1⟩
· -- not `P b`: the fibre of `b` meets no point with `P (f a)`
exact (mass_eq_zero_of_forall_not _ fun a (ha : f a = b ∧ P (f a)) => h (ha.1 ▸ ha.2)).symm
/-- Pushforward preserves total mass. -/
lemma weight_fTransform {A B : Type*}
(f : A → B) (X : Distribution A) :
(fTransform f X).weight = X.weight := by
unfold weight
change (Finsupp.mapDomain f X).sum (fun _ w => w) = X.sum (fun _ w => w)
rw [Finsupp.sum_mapDomain_index (fun _ => rfl) (fun _ _ _ => rfl)]
/-- Pushforward preserves non-negativity: the mass at each image point is a
sum of non-negative source masses. -/
lemma NonNeg.fTransform {A B : Type*} {X : Distribution A} (hX : X.NonNeg)
(f : A → B) : (Distribution.fTransform f X).NonNeg := fun b => by
rw [fTransform_apply_eq_mass]
exact hX.mass_nonneg _
/-- Pushforward preserves total probability mass. -/
lemma fTransform_isProbDist {A B : Type*} (f : A → B) {X : Distribution A}
(hX : X.isProbDist) : (fTransform f X).isProbDist := by
refine ⟨hX.nonNeg.fTransform f, ?_⟩
rw [weight_fTransform]
exact hX.weight_eq
instance {A B : Type*} (f : A → B) (X : Distribution A) [IsProbability X] :
IsProbability (fTransform f X) :=
⟨(fTransform_isProbDist f (IsProbability.isProbDist X)).1,
(fTransform_isProbDist f (IsProbability.isProbDist X)).2⟩
/-- `isProbDist` normalizes through pushforward — for a non-negative source
law. (Over the signed carrier the unconditional `↔` is false: a pushforward
can merge cancelling signed masses into a non-negative law.) -/
lemma isProbDist_fTransform {A B : Type*} (f : A → B) {X : Distribution A}
(hX : X.NonNeg) :
(fTransform f X).isProbDist ↔ X.isProbDist := by
constructor
· intro h
refine ⟨hX, ?_⟩
rw [← weight_fTransform f X]
exact h.weight_eq
· exact fTransform_isProbDist f
/-- An injective pushforward reflects non-negativity: every source mass shows
up unmerged at its image point. -/
lemma NonNeg.of_fTransform_injective {A B : Type*} {X : Distribution A} {f : A → B}
(hf : Function.Injective f) (h : (Distribution.fTransform f X).NonNeg) : X.NonNeg :=
fun a => by
have := h (f a)
rwa [fTransform_injective_apply X f hf] at this
/-- `isProbDist` normalizes through an **injective** pushforward with no
side condition: injectivity rules out merging cancelling signed masses. -/
@[simp]
lemma isProbDist_fTransform_of_injective {A B : Type*} {f : A → B}
(hf : Function.Injective f) (X : Distribution A) :
(fTransform f X).isProbDist ↔ X.isProbDist := by
constructor
· intro h
have hX : X.NonNeg := NonNeg.of_fTransform_injective hf h.nonNeg
exact (isProbDist_fTransform f hX).mp h
· exact fTransform_isProbDist f
/-- Dividing every mass by `c` divides the total mass by `c`. -/
lemma weight_mapRange_div {A : Type*} (X : Distribution A) (c : ℝ) :
weight (Finsupp.mapRange (fun w => w / c) (by simp) X : Distribution A) =
X.weight / c := by
unfold weight
rw [Finsupp.sum_mapRange_index]
· unfold Finsupp.sum
rw [div_eq_mul_inv, Finset.sum_mul]
apply Finset.sum_congr rfl
intro a _ha
exact div_eq_mul_inv (X a) c
· intro a
simp
/-! ### Random variables -/
/-- A random variable is a function from a sample space to a value space.
This is intentionally only an abbreviation, matching Mathlib's discipline:
random variables are functions; distributions over their values are obtained
by pushforward. -/
abbrev RV : Type _ :=
Ω → A
/-- The probability mass function induced by a random variable.
`PMF p X` is the low-level Lean form of the paper notation `P_X`: the
ambient random experiment has mass function `p` on outcomes `ω`, and
`P_X(a) = ∑_{ω : X ω = a} p(ω)`.
Paper-facing notation below suppresses `p`, matching Maurer's convention that
probabilities are taken in the current random experiment. -/
def PMF (p : ProbDist Ω) (X : RV (Ω := Ω) (A := A)) : ProbDist A :=
⟨fTransform X p.val, fTransform_isProbDist X p.property⟩
/-- Paper-facing PMF notation in the current random experiment.
The notation intentionally omits the low-level experiment mass `p`, following
CR18/Maurer notation. In Lean, it expands using the ambient variable named
`p : ProbDist Ω`. -/
syntax "ℙ⟦" term "⟧" : term
syntax "ℙ⟦" term "⟧[" term "]" : term
macro "ℙ⟦" X:term "⟧" : term => do
let p := Lean.mkIdent `p
`(Probability.Distribution.PMF $p $X)
macro "ℙ⟦" X:term "⟧[" x:term "]" : term => do
let p := Lean.mkIdent `p
`(Probability.Distribution.PMF $p $X $x)
/-! ### Function-valued random variables -/
/-- Conditional law of a random variable given an event on the sample space.
`condPMF p X E` is undefined exactly when `p(E) = 0`. When it is defined, its
mass at `a` is `Pr[X = a ∧ E] / Pr[E]`. -/
noncomputable def condPMF {Ω A : Type*} (p : ProbDist Ω)
(X : RV (Ω := Ω) (A := A)) (E : Ω → Prop) : Part (ProbDist A) :=
⟨p.val.mass E ≠ 0,
fun hE =>
⟨Finsupp.mapRange (fun w => w / p.val.mass E) (by simp)
(fTransform X (p.val.restrict E)), by
constructor
· intro a
rw [Finsupp.mapRange_apply]
exact div_nonneg
(((p.property.nonNeg.restrict E).fTransform X) a)
(p.property.nonNeg.mass_nonneg E)
· rw [weight_mapRange_div, weight_fTransform, weight_restrict]
exact div_self hE⟩⟩
lemma condPMF_apply {Ω A : Type*} (p : ProbDist Ω)
(X : RV (Ω := Ω) (A := A)) (E : Ω → Prop)
(hE : p.val.mass E ≠ 0) (a : A) :
(condPMF p X E).get hE a =
p.val.mass (fun ω => X ω = a ∧ E ω) / p.val.mass E := by
unfold condPMF
rw [Finsupp.mapRange_apply, fTransform_apply_eq_mass, mass_restrict]
/-- Paper-facing conditional PMF notation in the current random experiment.
`ℙ⟦X | Y⟧` is the partial function `(x, y) ↦ P_{X|Y}(x, y)`;
`ℙ⟦X | Y⟧[x, y]` is its partial value at `(x, y)`.
The notation intentionally omits the low-level experiment mass `p`, following
CR18/Maurer notation. In Lean, it expands using the ambient variable named
`p : ProbDist Ω`. -/
syntax "ℙ⟦" term " | " term "⟧" : term
syntax "ℙ⟦" term " | " term "⟧[" term ", " term "]" : term
macro "ℙ⟦" X:term " | " Y:term "⟧" : term => do
let p := Lean.mkIdent `p
`(Probability.Distribution.condPMFOf $p $X $Y)
macro "ℙ⟦" X:term " | " Y:term "⟧[" x:term ", " y:term "]" : term => do
let p := Lean.mkIdent `p
`(Probability.Distribution.condPMFOf $p $X $Y $x $y)
/-! ### Evaluating `fTransform` -/
omit [Nonempty A] [Fintype B] [Nonempty B] in
/-- Fiber-sum form of `fTransform` evaluation.
`(fTransform f X) b` is the total mass of all `a` such that `f a = b`.
The decidability of the fiber predicate is an explicit parameter rather than