1-- Independent events2-- 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.Independence8import Conditional910-- Two events are independent when the probability of both is the product.11predicate IndependentEvents (P A B : Set) :=12 prob(P, A ∩ B) = (prob(P, A):R) * (prob(P, B):R)1314theorem IndependentEvents.product (P A B : Set)15requires independent : IndependentEvents(P, A, B)16shows prob(P, A ∩ B) = (prob(P, A):R) * (prob(P, B):R)17proof18unfold IndependentEvents at independent19exact independent2021theorem IndependentEvents.intro (P A B : Set)22requires product : prob(P, A ∩ B) = (prob(P, A):R) * (prob(P, B):R)23shows IndependentEvents(P, A, B)24proof25unfold IndependentEvents26exact product2728theorem IndependentEvents.symmetric (Ω P A B : Set)29requires space : ProbabilitySpace(Ω, P)30requires eventA : A ⊆ Ω31requires eventB : B ⊆ Ω32requires independent : IndependentEvents(P, A, B)33shows IndependentEvents(P, B, A)34proof35have product := IndependentEvents.product with independent36have realA := prob.real with space, eventA37have realB := prob.real with space, eventB38have commute := intersection.commutative at B, A39have swap := realMulValue.commutative at prob(P, B), prob(P, A)40unfold IndependentEvents41rw [commute, product]42 symm43exact swap4445theorem IndependentEvents.whole_left (Ω P A : Set)46requires space : ProbabilitySpace(Ω, P)47requires eventA : A ⊆ Ω48shows IndependentEvents(P, Ω, A)49proof50have realA := prob.real with space, eventA51have same := intersection.eq_right_of_subset at Ω, A with eventA52have total := prob.whole with space53unfold IndependentEvents54rw [same, total]55ring5657theorem IndependentEvents.whole_right (Ω P A : Set)58requires space : ProbabilitySpace(Ω, P)59requires eventA : A ⊆ Ω60shows IndependentEvents(P, A, Ω)61proof62have whole := Subset.reflexive at Ω63haveleft:= IndependentEvents.whole_left with space, eventA64exact IndependentEvents.symmetric with space, whole, eventA, left6566theorem IndependentEvents.empty_left (Ω P A : Set)67requires space : ProbabilitySpace(Ω, P)68requires eventA : A ⊆ Ω69shows IndependentEvents(P, ∅, A)70proof71have realA := prob.real with space, eventA72have same := intersection.left_empty at A73have empty := prob.empty at P74unfold IndependentEvents75rw [same, empty]76ring7778theorem IndependentEvents.empty_right (Ω P A : Set)79requires space : ProbabilitySpace(Ω, P)80requires eventA : A ⊆ Ω81shows IndependentEvents(P, A, ∅)82proof83have empty := empty.subset at Ω84haveleft:= IndependentEvents.empty_left with space, eventA85exact IndependentEvents.symmetric with space, empty, eventA, left8687theorem IndependentEvents.complement_right (Ω P A B : Set)88requires space : ProbabilitySpace(Ω, P)89requires eventA : A ⊆ Ω90requires eventB : B ⊆ Ω91requires independent : IndependentEvents(P, A, B)92shows IndependentEvents(P, A, Ω ∖ B)93proof94have product := IndependentEvents.product with independent95have realA := prob.real with space, eventA96have realB := prob.real with space, eventB97have same : A ∩ (Ω ∖ B) = A ∖ B98ext y99simp100havesplit:= prob.difference at Ω, P, A, B with space, eventA101have complement := prob.complement with space, eventB102unfold IndependentEvents103rw [same, split, product, complement]104ring105106theorem IndependentEvents.complement_left (Ω P A B : Set)107requires space : ProbabilitySpace(Ω, P)108requires eventA : A ⊆ Ω109requires eventB : B ⊆ Ω110requires independent : IndependentEvents(P, A, B)111shows IndependentEvents(P, Ω ∖ A, B)112proof113have eventRest : Ω ∖ A ⊆ Ω bysimp114have swapped := IndependentEvents.symmetric with space, eventA, eventB,115 independent116have complemented := IndependentEvents.complement_right with space, eventB,117 eventA, swapped118exact IndependentEvents.symmetric with space, eventB, eventRest, complemented119120theorem IndependentEvents.complement_both (Ω P A B : Set)121requires space : ProbabilitySpace(Ω, P)122requires eventA : A ⊆ Ω123requires eventB : B ⊆ Ω124requires independent : IndependentEvents(P, A, B)125shows IndependentEvents(P, Ω ∖ A, Ω ∖ B)126proof127have eventRest : Ω ∖ B ⊆ Ω bysimp128haveright:= IndependentEvents.complement_right with space, eventA, eventB,129 independent130exact IndependentEvents.complement_left with space, eventA, eventRest, right131132-- Independence means that conditioning on B does not change the odds of A.133theorem IndependentEvents.condProb (Ω P A B : Set)134requires space : ProbabilitySpace(Ω, P)135requires eventA : A ⊆ Ω136requires eventB : B ⊆ Ω137requires positive : (0:R) < (prob(P, B):R)138requires independent : IndependentEvents(P, A, B)139shows condProb(P, A, B) = prob(P, A)140proof141have product := IndependentEvents.product with independent142have realA := prob.real with space, eventA143have realB := prob.real with space, eventB144have nonzero := prob.ne_zero_of_positive with space, eventB, positive145have pull := realDiv.mul_numerator_left at prob(P, A), prob(P, B), prob(P, B)146haveself:= realDiv.self at prob(P, B) with nonzero147have one := realMulValue.one_right at prob(P, A)148rw [condProb.value, product, pull, self]149exact one150151theorem IndependentEvents.of_condProb (Ω P A B : Set)152requires space : ProbabilitySpace(Ω, P)153requires eventA : A ⊆ Ω154requires eventB : B ⊆ Ω155requires positive : (0:R) < (prob(P, B):R)156requires conditioned : condProb(P, A, B) = prob(P, A)157shows IndependentEvents(P, A, B)158proof159have product := condProb.multiplication with space, eventA, eventB, positive160rw conditioned at product161exact IndependentEvents.intro with product162163theorem IndependentEvents.of_prob_zero (Ω P A B : Set)164requires space : ProbabilitySpace(Ω, P)165requires eventA : A ⊆ Ω166requires eventB : B ⊆ Ω167requires zero : prob(P, A) = (0:R)168shows IndependentEvents(P, A, B)169proof170have eventMeet : A ∩ B ⊆ Ω bysimp171have realMeet := prob.real with space, eventMeet172have realA := prob.real with space, eventA173have realB := prob.real with space, eventB174have upper := prob.intersection_le_left at Ω, P, A, B with space, eventA175have lower := prob.nonnegative with space, eventMeet176have vanishes : prob(P, A ∩ B) = (0:R) bylinarith177unfold IndependentEvents178rw [vanishes, zero]179ring180181theorem IndependentEvents.of_prob_one (Ω P A B : Set)182requires space : ProbabilitySpace(Ω, P)183requires eventA : A ⊆ Ω184requires eventB : B ⊆ Ω185requires one : prob(P, A) = (1:R)186shows IndependentEvents(P, A, B)187proof188have eventMeet : A ∩ B ⊆ Ω bysimp189have eventRest : B ∖ A ⊆ Ω bysimp190have innerRest : B ∖ A ⊆ Ω ∖ A bysimp191have eventComplement : Ω ∖ A ⊆ Ω bysimp192have realA := prob.real with space, eventA193have realMeet := prob.real with space, eventMeet194have realRest := prob.real with space, eventRest195have realComplement := prob.real with space, eventComplement196have realB := prob.real with space, eventB197have complement := prob.complement with space, eventA198have bound := prob.monotone with space, eventComplement, innerRest199have lower := prob.nonnegative with space, eventRest200havesplit:= prob.difference at Ω, P, B, A with space, eventB201have commute := intersection.commutative at B, A202rw commute atsplit203have meetValue : prob(P, A ∩ B) = prob(P, B) bylinarith204unfold IndependentEvents205rw [meetValue, one]206ring207208theorem IndependentEvents.union_left (Ω P A C B : Set)209requires space : ProbabilitySpace(Ω, P)210requires eventA : A ⊆ Ω211requires eventC : C ⊆ Ω212requires eventB : B ⊆ Ω213requires disjoint : Disjoint(A, C)214requires independentA : IndependentEvents(P, A, B)215requires independentC : IndependentEvents(P, C, B)216shows IndependentEvents(P, A ∪ C, B)217proof218have productA := IndependentEvents.product with independentA219have productC := IndependentEvents.product with independentC220have realA := prob.real with space, eventA221have realC := prob.real with space, eventC222have realB := prob.real with space, eventB223have eventMeetA : A ∩ B ⊆ Ω bysimp224have eventMeetC : C ∩ B ⊆ Ω bysimp225havesplit : (A ∪ C) ∩ B = (A ∩ B) ∪ (C ∩ B)226ext y227simp228have innerC := intersection.subset_left at C, B229have innerA := intersection.subset_left at A, B230have disjointRight := Disjoint.of_subset_right with disjoint, innerC231have disjointMeet := Disjoint.of_subset_left with disjointRight, innerA232have union := prob.union_disjoint with space, eventMeetA, eventMeetC,233 disjointMeet234have wholeUnion := prob.union_disjoint with space, eventA, eventC, disjoint235unfold IndependentEvents236rw [split, union, productA, productC, wholeUnion]237ring238239-- Three events, pairwise and mutually.240predicate PairwiseIndependent (P A B C : Set) :=241 IndependentEvents(P, A, B) ∧ IndependentEvents(P, A,242 C) ∧ IndependentEvents(P, B, C)243244predicate MutuallyIndependent (P A B C : Set) :=245 PairwiseIndependent(P, A, B, C) ∧246 prob(P, A ∩ B ∩ C) = ((prob(P, A):R) * (prob(P, B):R)) * (prob(P, C):R)247248theorem PairwiseIndependent.first (P A B C : Set)249requires pairwise : PairwiseIndependent(P, A, B, C)250shows IndependentEvents(P, A, B)251proof252unfold PairwiseIndependent at pairwise253exact pairwise.left254255theorem PairwiseIndependent.second (P A B C : Set)256requires pairwise : PairwiseIndependent(P, A, B, C)257shows IndependentEvents(P, A, C)258proof259unfold PairwiseIndependent at pairwise260exact pairwise.right.left261262theorem PairwiseIndependent.third (P A B C : Set)263requires pairwise : PairwiseIndependent(P, A, B, C)264shows IndependentEvents(P, B, C)265proof266unfold PairwiseIndependent at pairwise267exact pairwise.right.right268269theorem PairwiseIndependent.swap (Ω P A B C : Set)270requires space : ProbabilitySpace(Ω, P)271requires eventA : A ⊆ Ω272requires eventB : B ⊆ Ω273requires pairwise : PairwiseIndependent(P, A, B, C)274shows PairwiseIndependent(P, B, A, C)275proof276have first := PairwiseIndependent.first with pairwise277have second := PairwiseIndependent.second with pairwise278have third := PairwiseIndependent.third with pairwise279have swapped := IndependentEvents.symmetric with space, eventA, eventB, first280unfold PairwiseIndependent281refine⟨swapped, third, second⟩282283theorem MutuallyIndependent.pairwise (P A B C : Set)284requires mutual : MutuallyIndependent(P, A, B, C)285shows PairwiseIndependent(P, A, B, C)286proof287unfold MutuallyIndependent at mutual288exact mutual.left289290theorem MutuallyIndependent.triple (P A B C : Set)291requires mutual : MutuallyIndependent(P, A, B, C)292shows prob(P, A ∩ B ∩ C) = ((prob(P, A):R) * (prob(P, B):R)) * (prob(P, C):R)293proof294unfold MutuallyIndependent at mutual295exact mutual.right296297-- Under mutual independence the intersection A ∩ B is independent of C.298theorem MutuallyIndependent.intersection_left (P A B C : Set)299requires mutual : MutuallyIndependent(P, A, B, C)300shows IndependentEvents(P, A ∩ B, C)301proof302have pairwise := MutuallyIndependent.pairwise with mutual303have first := PairwiseIndependent.first with pairwise304have productAB := IndependentEvents.product with first305have triple := MutuallyIndependent.triple with mutual306unfold IndependentEvents307rw [triple, productAB]308rfl309