1-- Finite probability spaces and the probability of an event2-- Part of Cumulus, the Grace17 mathematics library.3-- Copyright (c) 2026 Grant Passmore.4-- Released under the Apache License 2.0. See LICENSE in the repository root.5-- Authors: Grant Passmore67module Grace.Cumulus.Probability.Space8import SetSum9import Real.AbsoluteValue1011-- A finite probability space: a finite sample space Ω with nonnegative12-- weights P(ω) that sum to one.13predicate ProbabilitySpace (Ω P : Set) :=14 Finite(Ω) ∧ FunctionBetween(P, Ω, ℝ) ∧15 (∀ ω ∈ Ω, (0:R) ≤ (P(ω):R)) ∧ realSetSum(Ω, P) = (1:R)1617-- The probability of an event A ⊆ Ω is the total weight of its points.18def prob (P A : Set) : Set:= realSetSum(A, P)1920theorem prob.value (P A : Set)21shows prob(P, A) = realSetSum(A, P)22proof23simp [prob]2425theorem ProbabilitySpace.intro (Ω P : Set)26requires finite : Finite(Ω)27requires function : P : Ω → ℝ28requires nonnegative : ∀ ω ∈ Ω, (0:R) ≤ (P(ω):R)29requires total : realSetSum(Ω, P) = (1:R)30shows ProbabilitySpace(Ω, P)31proof32unfold ProbabilitySpace33refine⟨finite, function, nonnegative, total⟩3435theorem ProbabilitySpace.finite (Ω P : Set)36requires space : ProbabilitySpace(Ω, P)37shows Finite(Ω)38proof39unfold ProbabilitySpace at space40exact space.left4142theorem ProbabilitySpace.function (Ω P : Set)43requires space : ProbabilitySpace(Ω, P)44shows P : Ω → ℝ45proof46unfold ProbabilitySpace at space47exact space.right.left4849theorem ProbabilitySpace.nonnegative (Ω P : Set)50requires space : ProbabilitySpace(Ω, P)51shows∀ ω ∈ Ω, (0:R) ≤ (P(ω):R)52proof53unfold ProbabilitySpace at space54exact space.right.right.left5556theorem ProbabilitySpace.total (Ω P : Set)57requires space : ProbabilitySpace(Ω, P)58shows realSetSum(Ω, P) = (1:R)59proof60unfold ProbabilitySpace at space61exact space.right.right.right6263theorem ProbabilitySpace.event_finite (Ω P A : Set)64requires space : ProbabilitySpace(Ω, P)65requires event : A ⊆ Ω66shows Finite(A)67proof68have finite := ProbabilitySpace.finite with space69exact Finite.subset with finite, event7071theorem ProbabilitySpace.point_real (Ω P ω : Set)72requires space : ProbabilitySpace(Ω, P)73requires point : ω ∈ Ω74showsReal(P(ω))75proof76have function := ProbabilitySpace.function with space77exact FunctionBetween.apply_closed with function, point7879theorem ProbabilitySpace.point_nonnegative (Ω P ω : Set)80requires space : ProbabilitySpace(Ω, P)81requires point : ω ∈ Ω82shows (0:R) ≤ (P(ω):R)83proof84have nonnegative := ProbabilitySpace.nonnegative with space85exact nonnegative with ω, point8687theorem prob.real (Ω P A : Set)88requires space : ProbabilitySpace(Ω, P)89requires event : A ⊆ Ω90showsReal(prob(P, A))91proof92have function := ProbabilitySpace.function with space93have finite := ProbabilitySpace.event_finite with space, event94have sum := realSetSum.real with finite, function, event95rw prob.value96exact sum9798theorem prob.empty (P : Set)99shows prob(P, ∅) = (0:R)100proof101rw prob.value102exact realSetSum.empty at P103104theorem prob.whole (Ω P : Set)105requires space : ProbabilitySpace(Ω, P)106shows prob(P, Ω) = (1:R)107proof108rw prob.value109exact ProbabilitySpace.total with space110111theorem prob.nonnegative (Ω P A : Set)112requires space : ProbabilitySpace(Ω, P)113requires event : A ⊆ Ω114shows (0:R) ≤ (prob(P, A):R)115proof116have function := ProbabilitySpace.function with space117have nonnegative := ProbabilitySpace.nonnegative with space118have finite := ProbabilitySpace.event_finite with space, event119have bounds : ∀ x ∈ A, (0:R) ≤ (P(x):R)120intros x hx121have member := event with x, hx122exact nonnegative with x, member123have sum := realSetSum.nonnegative with finite, function, event, bounds124rw prob.value125exact sum126127theorem prob.monotone (Ω P A B : Set)128requires space : ProbabilitySpace(Ω, P)129requires eventB : B ⊆ Ω130requires inner : A ⊆ B131shows (prob(P, A):R) ≤ (prob(P, B):R)132proof133have function := ProbabilitySpace.function with space134have nonnegative := ProbabilitySpace.nonnegative with space135have finite := ProbabilitySpace.event_finite with space, eventB136have bounds : ∀ x ∈ B, (0:R) ≤ (P(x):R)137intros x hx138have member := eventB with x, hx139exact nonnegative with x, member140have sum := realSetSum.subset_le with finite, function, eventB, inner, bounds141rw [prob.value, prob.value]142exact sum143144theorem prob.le_one (Ω P A : Set)145requires space : ProbabilitySpace(Ω, P)146requires event : A ⊆ Ω147shows (prob(P, A):R) ≤ (1:R)148proof149have whole := Subset.reflexive at Ω150have bound := prob.monotone with space, whole, event151have total := prob.whole with space152 rwa total at bound153154theorem prob.union_disjoint (Ω P A B : Set)155requires space : ProbabilitySpace(Ω, P)156requires eventA : A ⊆ Ω157requires eventB : B ⊆ Ω158requires disjoint : Disjoint(A, B)159shows prob(P, A ∪ B) = (prob(P, A):R) + (prob(P, B):R)160proof161have function := ProbabilitySpace.function with space162have finiteA := ProbabilitySpace.event_finite with space, eventA163have finiteB := ProbabilitySpace.event_finite with space, eventB164have sum := realSetSum.union_disjoint with finiteA, finiteB, function,165 eventA, eventB, disjoint166rw [prob.value, prob.value, prob.value]167exact sum168169theorem prob.difference_subset (Ω P A B : Set)170requires space : ProbabilitySpace(Ω, P)171requires eventA : A ⊆ Ω172requires inner : B ⊆ A173shows prob(P, A ∖ B) = (prob(P, A):R) - (prob(P, B):R)174proof175have function := ProbabilitySpace.function with space176have finiteA := ProbabilitySpace.event_finite with space, eventA177havesplit:= realSetSum.difference with finiteA, function, eventA, inner178have rest := difference.subset_left at A, B179have eventRest := Subset.transitive with rest, eventA180have eventB := Subset.transitive with inner, eventA181have finiteB := Finite.subset with finiteA, inner182have finiteRest := Finite.subset with finiteA, rest183have realA := realSetSum.real with finiteA, function, eventA184have realB := realSetSum.real with finiteB, function, eventB185have realRest := realSetSum.real with finiteRest, function, eventRest186rw [prob.value, prob.value, prob.value]187linarith188189theorem prob.complement (Ω P A : Set)190requires space : ProbabilitySpace(Ω, P)191requires event : A ⊆ Ω192shows prob(P, Ω ∖ A) = (1:R) - (prob(P, A):R)193proof194have whole := Subset.reflexive at Ω195have difference := prob.difference_subset with space, whole, event196have total := prob.whole with space197 rwa total at difference198199theorem prob.singleton (Ω P ω : Set)200requires space : ProbabilitySpace(Ω, P)201requires point : ω ∈ Ω202shows prob(P, {ω}) = P(ω)203proof204have function := ProbabilitySpace.function with space205rw prob.value206exact realSetSum.singleton with function, point207208theorem prob.zero_of_pointwise (Ω P A : Set)209requires space : ProbabilitySpace(Ω, P)210requires event : A ⊆ Ω211requires vanishes : ∀ ω ∈ A, P(ω) = (0:R)212shows prob(P, A) = (0:R)213proof214have function := ProbabilitySpace.function with space215have finite := ProbabilitySpace.event_finite with space, event216rw prob.value217exact realSetSum.zero with finite, function, event, vanishes218219theorem prob.insert (Ω P A ω : Set)220requires space : ProbabilitySpace(Ω, P)221requires event : A ⊆ Ω222requires point : ω ∈ Ω223requires fresh : ¬(ω ∈ A)224shows prob(P, insert(ω, A)) = (prob(P, A):R) + (P(ω):R)225proof226have function := ProbabilitySpace.function with space227have finite := ProbabilitySpace.event_finite with space, event228rw [prob.value, prob.value]229exact realSetSum.insert with finite, function, event, point, fresh230231theorem prob.remove (Ω P A ω : Set)232requires space : ProbabilitySpace(Ω, P)233requires event : A ⊆ Ω234requires point : ω ∈ A235shows prob(P, A) = (prob(P, A ∖ {ω}):R) + (P(ω):R)236proof237have function := ProbabilitySpace.function with space238have finite := ProbabilitySpace.event_finite with space, event239rw [prob.value, prob.value]240exact realSetSum.insert_member with finite, function, event, point241242theorem prob.inclusion_exclusion (Ω P A B : Set)243requires space : ProbabilitySpace(Ω, P)244requires eventA : A ⊆ Ω245requires eventB : B ⊆ Ω246shows (prob(P, A ∪ B):R) + (prob(P, A ∩ B):R) = (prob(P, A):R) + (prob(P,247 B):R)248proof249have restSubset := difference.subset_left at B, A250have meetSubset := intersection.subset_right at A, B251have meetLeft := intersection.subset_left at A, B252have eventRest := Subset.transitive with restSubset, eventB253have eventMeet := Subset.transitive with meetSubset, eventB254have disjointRest := Disjoint.difference_right at B, A255have disjointMeet := Disjoint.of_subset_left with disjointRest, meetLeft256have unionSplit : A ∪ (B ∖ A) = A ∪ B257ext y258simp259have meetSplit : (A ∩ B) ∪ (B ∖ A) = B260ext y261simp262have first := prob.union_disjoint with space, eventA, eventRest, disjointRest263have second := prob.union_disjoint with space, eventMeet, eventRest,264 disjointMeet265rw unionSplit at first266rw meetSplit at second267have realA := prob.real with space, eventA268have realB := prob.real with space, eventB269have realMeet := prob.real with space, eventMeet270have realRest := prob.real with space, eventRest271have realUnion : Real(prob(P, A ∪ B))272have eventUnion : A ∪ B ⊆ Ω bysimp273exact prob.real with space, eventUnion274linarith275276theorem prob.union (Ω P A B : Set)277requires space : ProbabilitySpace(Ω, P)278requires eventA : A ⊆ Ω279requires eventB : B ⊆ Ω280shows prob(P, A ∪ B) = ((prob(P, A):R) + (prob(P, B):R)) - (prob(P, A ∩ B):R)281proof282have identity := prob.inclusion_exclusion with space, eventA, eventB283have eventUnion : A ∪ B ⊆ Ω bysimp284have eventMeet : A ∩ B ⊆ Ω bysimp285have realA := prob.real with space, eventA286have realB := prob.real with space, eventB287have realMeet := prob.real with space, eventMeet288have realUnion := prob.real with space, eventUnion289linarith290291theorem prob.union_le (Ω P A B : Set)292requires space : ProbabilitySpace(Ω, P)293requires eventA : A ⊆ Ω294requires eventB : B ⊆ Ω295shows (prob(P, A ∪ B):R) ≤ (prob(P, A):R) + (prob(P, B):R)296proof297have identity := prob.inclusion_exclusion with space, eventA, eventB298have eventUnion : A ∪ B ⊆ Ω bysimp299have eventMeet : A ∩ B ⊆ Ω bysimp300have realA := prob.real with space, eventA301have realB := prob.real with space, eventB302have realMeet := prob.real with space, eventMeet303have realUnion := prob.real with space, eventUnion304have nonnegative := prob.nonnegative with space, eventMeet305linarith306307theorem prob.union_three_le (Ω P A B C : Set)308requires space : ProbabilitySpace(Ω, P)309requires eventA : A ⊆ Ω310requires eventB : B ⊆ Ω311requires eventC : C ⊆ Ω312shows (prob(P, A ∪ B ∪ C):R) ≤313 ((prob(P, A):R) + (prob(P, B):R)) + (prob(P, C):R)314proof315have eventUnion : A ∪ B ⊆ Ω bysimp316have first := prob.union_le with space, eventUnion, eventC317have second := prob.union_le with space, eventA, eventB318have eventAll : A ∪ B ∪ C ⊆ Ω bysimp319have realA := prob.real with space, eventA320have realB := prob.real with space, eventB321have realC := prob.real with space, eventC322have realUnion := prob.real with space, eventUnion323have realAll := prob.real with space, eventAll324linarith325326theorem prob.intersection_le_left (Ω P A B : Set)327requires space : ProbabilitySpace(Ω, P)328requires eventA : A ⊆ Ω329shows (prob(P, A ∩ B):R) ≤ (prob(P, A):R)330proof331have inner := intersection.subset_left at A, B332exact prob.monotone with space, eventA, inner333334theorem prob.intersection_le_right (Ω P A B : Set)335requires space : ProbabilitySpace(Ω, P)336requires eventB : B ⊆ Ω337shows (prob(P, A ∩ B):R) ≤ (prob(P, B):R)338proof339have inner := intersection.subset_right at A, B340exact prob.monotone with space, eventB, inner341342theorem prob.difference (Ω P A B : Set)343requires space : ProbabilitySpace(Ω, P)344requires eventA : A ⊆ Ω345shows prob(P, A ∖ B) = (prob(P, A):R) - (prob(P, A ∩ B):R)346proof347have inner := intersection.subset_left at A, B348havesplit:= prob.difference_subset with space, eventA, inner349have same := difference.of_intersection at A, B350 rwa same atsplit351352theorem prob.difference_le (Ω P A B : Set)353requires space : ProbabilitySpace(Ω, P)354requires eventA : A ⊆ Ω355shows (prob(P, A ∖ B):R) ≤ (prob(P, A):R)356proof357have inner := difference.subset_left at A, B358exact prob.monotone with space, eventA, inner359360theorem prob.point_le (Ω P A ω : Set)361requires space : ProbabilitySpace(Ω, P)362requires event : A ⊆ Ω363requires point : ω ∈ A364shows (P(ω):R) ≤ (prob(P, A):R)365proof366have member := event with ω, point367have inner : {ω} ⊆ A bysimp368have bound := prob.monotone with space, event, inner369have singleton := prob.singleton with space, member370 rwa singleton at bound371372theorem ProbabilitySpace.point_le_one (Ω P ω : Set)373requires space : ProbabilitySpace(Ω, P)374requires point : ω ∈ Ω375shows (P(ω):R) ≤ (1:R)376proof377have whole := Subset.reflexive at Ω378have bound := prob.point_le with space, whole, point379have total := prob.whole with space380 rwa total at bound381382theorem prob.disjoint_le_one (Ω P A B : Set)383requires space : ProbabilitySpace(Ω, P)384requires eventA : A ⊆ Ω385requires eventB : B ⊆ Ω386requires disjoint : Disjoint(A, B)387shows (prob(P, A):R) + (prob(P, B):R) ≤ (1:R)388proof389have union := prob.union_disjoint with space, eventA, eventB, disjoint390have eventUnion : A ∪ B ⊆ Ω bysimp391have bound := prob.le_one with space, eventUnion392 rwa union at bound393394theorem prob.complement_zero_iff (Ω P A : Set)395requires space : ProbabilitySpace(Ω, P)396requires event : A ⊆ Ω397shows prob(P, Ω ∖ A) = (0:R) ↔ prob(P, A) = (1:R)398proof399have complement := prob.complement with space, event400have realA := prob.real with space, event401have eventRest := difference.subset_left at Ω, A402have realRest := prob.real with space, eventRest403constructor404 . intro zero405linarith406 . intro one407linarith408409theorem prob.union_disjoint_three (Ω P A B C : Set)410requires space : ProbabilitySpace(Ω, P)411requires eventA : A ⊆ Ω412requires eventB : B ⊆ Ω413requires eventC : C ⊆ Ω414requires disjointAB : Disjoint(A, B)415requires disjointAC : Disjoint(A, C)416requires disjointBC : Disjoint(B, C)417shows prob(P, A ∪ B ∪ C) =418 ((prob(P, A):R) + (prob(P, B):R)) + (prob(P, C):R)419proof420have eventUnion : A ∪ B ⊆ Ω bysimp421have disjointUnion := Disjoint.union2_iff.backward at A, B, C with422⟨disjointAC, disjointBC⟩423have first := prob.union_disjoint with space, eventUnion, eventC,424 disjointUnion425have second := prob.union_disjoint with space, eventA, eventB, disjointAB426rw [first, second]427rfl428429theorem prob.subset_zero (Ω P A B : Set)430requires space : ProbabilitySpace(Ω, P)431requires eventA : A ⊆ Ω432requires inner : B ⊆ A433requires zero : prob(P, A) = (0:R)434shows prob(P, B) = (0:R)435proof436have eventB := Subset.transitive with inner, eventA437have realA := prob.real with space, eventA438have realB := prob.real with space, eventB439have upper := prob.monotone with space, eventA, inner440have lower := prob.nonnegative with space, eventB441linarith442443theorem prob.superset_one (Ω P A B : Set)444requires space : ProbabilitySpace(Ω, P)445requires eventB : B ⊆ Ω446requires inner : A ⊆ B447requires one : prob(P, A) = (1:R)448shows prob(P, B) = (1:R)449proof450have eventA := Subset.transitive with inner, eventB451have realA := prob.real with space, eventA452have realB := prob.real with space, eventB453have lower := prob.monotone with space, eventB, inner454have upper := prob.le_one with space, eventB455linarith456