Skip to content

Commit eff4459

Browse files
feat: port Probability.CondCount (#3889)
Co-authored-by: int-y1 <jason_yuen2007@hotmail.com>
1 parent 9d9de69 commit eff4459

File tree

2 files changed

+208
-0
lines changed

2 files changed

+208
-0
lines changed

Mathlib.lean

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1719,6 +1719,7 @@ import Mathlib.Order.WellFoundedSet
17191719
import Mathlib.Order.WithBot
17201720
import Mathlib.Order.Zorn
17211721
import Mathlib.Order.ZornAtoms
1722+
import Mathlib.Probability.CondCount
17221723
import Mathlib.Probability.ConditionalProbability
17231724
import Mathlib.Probability.ProbabilityMassFunction.Basic
17241725
import Mathlib.Probability.ProbabilityMassFunction.Monad

Mathlib/Probability/CondCount.lean

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
/-
2+
Copyright (c) 2022 Kexing Ying. All rights reserved.
3+
Released under Apache 2.0 license as described in the file LICENSE.
4+
Authors: Kexing Ying, Bhavik Mehta
5+
6+
! This file was ported from Lean 3 source module probability.cond_count
7+
! leanprover-community/mathlib commit 117e93f82b5f959f8193857370109935291f0cc4
8+
! Please do not edit these lines, except to modify the commit id
9+
! if you have ported upstream changes.
10+
-/
11+
import Mathlib.Probability.ConditionalProbability
12+
13+
/-!
14+
# Classical probability
15+
16+
The classical formulation of probability states that the probability of an event occurring in a
17+
finite probability space is the ratio of that event to all possible events.
18+
This notion can be expressed with measure theory using
19+
the counting measure. In particular, given the sets `s` and `t`, we define the probability of `t`
20+
occuring in `s` to be `|s|⁻¹ * |s ∩ t|`. With this definition, we recover the the probability over
21+
the entire sample space when `s = Set.univ`.
22+
23+
Classical probability is often used in combinatorics and we prove some useful lemmas in this file
24+
for that purpose.
25+
26+
## Main definition
27+
28+
* `ProbabilityTheory.condCount`: given a set `s`, `condCount s` is the counting measure
29+
conditioned on `s`. This is a probability measure when `s` is finite and nonempty.
30+
31+
## Notes
32+
33+
The original aim of this file is to provide a measure theoretic method of describing the
34+
probability an element of a set `s` satisfies some predicate `P`. Our current formulation still
35+
allow us to describe this by abusing the definitional equality of sets and predicates by simply
36+
writing `condCount s P`. We should avoid this however as none of the lemmas are written for
37+
predicates.
38+
-/
39+
40+
41+
noncomputable section
42+
43+
open ProbabilityTheory
44+
45+
open MeasureTheory MeasurableSpace
46+
47+
namespace ProbabilityTheory
48+
49+
variable {Ω : Type _} [MeasurableSpace Ω]
50+
51+
/-- Given a set `s`, `condCount s` is the counting measure conditioned on `s`. In particular,
52+
`condCount s t` is the proportion of `s` that is contained in `t`.
53+
54+
This is a probability measure when `s` is finite and nonempty and is given by
55+
`ProbabilityTheory.condCount_probabilityMeasure`. -/
56+
def condCount (s : Set Ω) : Measure Ω :=
57+
Measure.count[|s]
58+
#align probability_theory.cond_count ProbabilityTheory.condCount
59+
60+
@[simp]
61+
theorem condCount_empty_meas : (condCount ∅ : Measure Ω) = 0 := by simp [condCount]
62+
#align probability_theory.cond_count_empty_meas ProbabilityTheory.condCount_empty_meas
63+
64+
theorem condCount_empty {s : Set Ω} : condCount s ∅ = 0 := by simp
65+
#align probability_theory.cond_count_empty ProbabilityTheory.condCount_empty
66+
67+
theorem finite_of_condCount_ne_zero {s t : Set Ω} (h : condCount s t ≠ 0) : s.Finite := by
68+
by_contra hs'
69+
simp [condCount, cond, Measure.count_apply_infinite hs'] at h
70+
#align probability_theory.finite_of_cond_count_ne_zero ProbabilityTheory.finite_of_condCount_ne_zero
71+
72+
theorem condCount_univ [Fintype Ω] {s : Set Ω} :
73+
condCount Set.univ s = Measure.count s / Fintype.card Ω := by
74+
rw [condCount, cond_apply _ MeasurableSet.univ, ← ENNReal.div_eq_inv_mul, Set.univ_inter]
75+
congr
76+
rw [← Finset.coe_univ, Measure.count_apply, Finset.univ.tsum_subtype' fun _ => (1 : ENNReal)]
77+
· simp [Finset.card_univ]
78+
· exact (@Finset.coe_univ Ω _).symm ▸ MeasurableSet.univ
79+
#align probability_theory.cond_count_univ ProbabilityTheory.condCount_univ
80+
81+
variable [MeasurableSingletonClass Ω]
82+
83+
theorem condCount_probabilityMeasure {s : Set Ω} (hs : s.Finite) (hs' : s.Nonempty) :
84+
ProbabilityMeasure (condCount s) :=
85+
{ measure_univ := by
86+
rw [condCount, cond_apply _ hs.measurableSet, Set.inter_univ, ENNReal.inv_mul_cancel]
87+
· exact fun h => hs'.ne_empty <| Measure.empty_of_count_eq_zero h
88+
· exact (Measure.count_apply_lt_top.2 hs).ne }
89+
#align probability_theory.cond_count_is_probability_measure ProbabilityTheory.condCount_probabilityMeasure
90+
91+
theorem condCount_singleton (ω : Ω) (t : Set Ω) [Decidable (ω ∈ t)] :
92+
condCount {ω} t = if ω ∈ t then 1 else 0 := by
93+
rw [condCount, cond_apply _ (measurableSet_singleton ω), Measure.count_singleton, inv_one,
94+
one_mul]
95+
split_ifs
96+
· rw [(by simpa : ({ω} : Set Ω) ∩ t = {ω}), Measure.count_singleton]
97+
· rw [(by simpa : ({ω} : Set Ω) ∩ t = ∅), Measure.count_empty]
98+
#align probability_theory.cond_count_singleton ProbabilityTheory.condCount_singleton
99+
100+
variable {s t u : Set Ω}
101+
102+
theorem condCount_inter_self (hs : s.Finite) : condCount s (s ∩ t) = condCount s t := by
103+
rw [condCount, cond_inter_self _ hs.measurableSet]
104+
#align probability_theory.cond_count_inter_self ProbabilityTheory.condCount_inter_self
105+
106+
theorem condCount_self (hs : s.Finite) (hs' : s.Nonempty) : condCount s s = 1 := by
107+
rw [condCount, cond_apply _ hs.measurableSet, Set.inter_self, ENNReal.inv_mul_cancel]
108+
· exact fun h => hs'.ne_empty <| Measure.empty_of_count_eq_zero h
109+
· exact (Measure.count_apply_lt_top.2 hs).ne
110+
#align probability_theory.cond_count_self ProbabilityTheory.condCount_self
111+
112+
theorem condCount_eq_one_of (hs : s.Finite) (hs' : s.Nonempty) (ht : s ⊆ t) :
113+
condCount s t = 1 := by
114+
haveI := condCount_probabilityMeasure hs hs'
115+
refine' eq_of_le_of_not_lt prob_le_one _
116+
rw [not_lt, ← condCount_self hs hs']
117+
exact measure_mono ht
118+
#align probability_theory.cond_count_eq_one_of ProbabilityTheory.condCount_eq_one_of
119+
120+
theorem pred_true_of_condCount_eq_one (h : condCount s t = 1) : s ⊆ t := by
121+
have hsf := finite_of_condCount_ne_zero (by rw [h]; exact one_ne_zero)
122+
rw [condCount, cond_apply _ hsf.measurableSet, mul_comm] at h
123+
replace h := ENNReal.eq_inv_of_mul_eq_one_left h
124+
rw [inv_inv, Measure.count_apply_finite _ hsf, Measure.count_apply_finite _ (hsf.inter_of_left _),
125+
Nat.cast_inj] at h
126+
suffices s ∩ t = s by exact this ▸ fun x hx => hx.2
127+
rw [← @Set.Finite.toFinset_inj _ _ _ (hsf.inter_of_left _) hsf]
128+
exact Finset.eq_of_subset_of_card_le (Set.Finite.toFinset_mono <| s.inter_subset_left t) h.ge
129+
#align probability_theory.pred_true_of_cond_count_eq_one ProbabilityTheory.pred_true_of_condCount_eq_one
130+
131+
theorem condCount_eq_zero_iff (hs : s.Finite) : condCount s t = 0 ↔ s ∩ t = ∅ := by
132+
simp [condCount, cond_apply _ hs.measurableSet, Measure.count_apply_eq_top, Set.not_infinite.2 hs,
133+
Measure.count_apply_finite _ (hs.inter_of_left _)]
134+
#align probability_theory.cond_count_eq_zero_iff ProbabilityTheory.condCount_eq_zero_iff
135+
136+
theorem condCount_of_univ (hs : s.Finite) (hs' : s.Nonempty) : condCount s Set.univ = 1 :=
137+
condCount_eq_one_of hs hs' s.subset_univ
138+
#align probability_theory.cond_count_of_univ ProbabilityTheory.condCount_of_univ
139+
140+
theorem condCount_inter (hs : s.Finite) :
141+
condCount s (t ∩ u) = condCount (s ∩ t) u * condCount s t := by
142+
by_cases hst : s ∩ t = ∅
143+
· rw [hst, condCount_empty_meas, Measure.coe_zero, Pi.zero_apply, MulZeroClass.zero_mul,
144+
condCount_eq_zero_iff hs, ← Set.inter_assoc, hst, Set.empty_inter]
145+
rw [condCount, condCount, cond_apply _ hs.measurableSet, cond_apply _ hs.measurableSet,
146+
cond_apply _ (hs.inter_of_left _).measurableSet, mul_comm _ (Measure.count (s ∩ t)),
147+
← mul_assoc, mul_comm _ (Measure.count (s ∩ t)), ← mul_assoc, ENNReal.mul_inv_cancel, one_mul,
148+
mul_comm, Set.inter_assoc]
149+
· rwa [← Measure.count_eq_zero_iff] at hst
150+
· exact (Measure.count_apply_lt_top.2 <| hs.inter_of_left _).ne
151+
#align probability_theory.cond_count_inter ProbabilityTheory.condCount_inter
152+
153+
theorem condCount_inter' (hs : s.Finite) :
154+
condCount s (t ∩ u) = condCount (s ∩ u) t * condCount s u := by
155+
rw [← Set.inter_comm]
156+
exact condCount_inter hs
157+
#align probability_theory.cond_count_inter' ProbabilityTheory.condCount_inter'
158+
159+
theorem condCount_union (hs : s.Finite) (htu : Disjoint t u) :
160+
condCount s (t ∪ u) = condCount s t + condCount s u := by
161+
rw [condCount, cond_apply _ hs.measurableSet, cond_apply _ hs.measurableSet,
162+
cond_apply _ hs.measurableSet, Set.inter_union_distrib_left, measure_union, mul_add]
163+
exacts[htu.mono inf_le_right inf_le_right, (hs.inter_of_left _).measurableSet]
164+
#align probability_theory.cond_count_union ProbabilityTheory.condCount_union
165+
166+
theorem condCount_compl (t : Set Ω) (hs : s.Finite) (hs' : s.Nonempty) :
167+
condCount s t + condCount s (tᶜ) = 1 := by
168+
rw [← condCount_union hs disjoint_compl_right, Set.union_compl_self,
169+
(condCount_probabilityMeasure hs hs').measure_univ]
170+
#align probability_theory.cond_count_compl ProbabilityTheory.condCount_compl
171+
172+
theorem condCount_disjoint_union (hs : s.Finite) (ht : t.Finite) (hst : Disjoint s t) :
173+
condCount s u * condCount (s ∪ t) s + condCount t u * condCount (s ∪ t) t =
174+
condCount (s ∪ t) u := by
175+
rcases s.eq_empty_or_nonempty with (rfl | hs') <;> rcases t.eq_empty_or_nonempty with (rfl | ht')
176+
· simp
177+
· simp [condCount_self ht ht']
178+
· simp [condCount_self hs hs']
179+
rw [condCount, condCount, condCount, cond_apply _ hs.measurableSet,
180+
cond_apply _ ht.measurableSet, cond_apply _ (hs.union ht).measurableSet,
181+
cond_apply _ (hs.union ht).measurableSet, cond_apply _ (hs.union ht).measurableSet]
182+
conv_lhs =>
183+
rw [Set.union_inter_cancel_left, Set.union_inter_cancel_right,
184+
mul_comm (Measure.count (s ∪ t))⁻¹, mul_comm (Measure.count (s ∪ t))⁻¹, ← mul_assoc,
185+
← mul_assoc, mul_comm _ (Measure.count s), mul_comm _ (Measure.count t), ← mul_assoc,
186+
← mul_assoc]
187+
rw [ENNReal.mul_inv_cancel, ENNReal.mul_inv_cancel, one_mul, one_mul, ← add_mul, ← measure_union,
188+
Set.union_inter_distrib_right, mul_comm]
189+
exacts[hst.mono inf_le_left inf_le_left, (ht.inter_of_left _).measurableSet,
190+
Measure.count_ne_zero ht', (Measure.count_apply_lt_top.2 ht).ne, Measure.count_ne_zero hs',
191+
(Measure.count_apply_lt_top.2 hs).ne]
192+
#align probability_theory.cond_count_disjoint_union ProbabilityTheory.condCount_disjoint_union
193+
194+
/-- A version of the law of total probability for counting probabilites. -/
195+
theorem condCount_add_compl_eq (u t : Set Ω) (hs : s.Finite) :
196+
condCount (s ∩ u) t * condCount s u + condCount (s ∩ uᶜ) t * condCount s (uᶜ) =
197+
condCount s t := by
198+
-- Porting note: The original proof used `conv_rhs`. However, that tactic timed out.
199+
have : condCount s t = (condCount (s ∩ u) t * condCount (s ∩ u ∪ s ∩ uᶜ) (s ∩ u) +
200+
condCount (s ∩ uᶜ) t * condCount (s ∩ u ∪ s ∩ uᶜ) (s ∩ uᶜ)) := by
201+
rw [condCount_disjoint_union (hs.inter_of_left _) (hs.inter_of_left _)
202+
(disjoint_compl_right.mono inf_le_right inf_le_right), Set.inter_union_compl]
203+
rw [this]
204+
simp [condCount_inter_self hs]
205+
#align probability_theory.cond_count_add_compl_eq ProbabilityTheory.condCount_add_compl_eq
206+
207+
end ProbabilityTheory

0 commit comments

Comments
 (0)