Conditional Probability and Independence
New information changes probabilities. A positive test result changes the probability of a disease; a failed health check changes the probability that a server is down. Conditional probability makes this precise, and independence describes events that carry no information about each other. Both are covered in depth in the probability course (Conditional Probability, Bayes' Theorem); here are the essentials with a computer-science angle.
Definition
For events and with :
"The probability of given ": restrict the sample space to and renormalise. Rearranged, it is the product rule , which is what you multiply along the branches of a tree diagram.
Law of total probability. If partition the sample space,
Bayes' rule and the false-positive trap
A fraud detector flags 99% of fraudulent transactions and wrongly flags 2% of legitimate ones. Only 0.1% of transactions are fraudulent. Given a flag, how likely is fraud?
from fractions import Fraction as F
p_fraud = F(1, 1000)
p_flag_given_fraud = F(99, 100)
p_flag_given_ok = F(2, 100)
p_flag = p_flag_given_fraud * p_fraud + p_flag_given_ok * (1 - p_fraud)
p_fraud_given_flag = p_flag_given_fraud * p_fraud / p_flag
print("P(flag) =", p_flag, "=", float(p_flag))
print("P(fraud | flag) =", p_fraud_given_flag, "~", round(float(p_fraud_given_flag), 4))P(flag) = 2097/100000 = 0.02097
P(fraud | flag) = 11/233 ~ 0.0472
Fewer than 5% of flagged transactions are fraud. With a rare condition, even an accurate test produces mostly false positives, because the 2% of the huge legitimate majority outweighs the 99% of the tiny fraudulent minority. Ignoring the base rate is the base rate fallacy.
Independence
and are independent if , equivalently : knowing tells nothing about .
Events are mutually independent if the product rule holds for every subcollection. Pairwise independence (every pair) is weaker:
from itertools import product
from fractions import Fraction
S = list(product([0, 1], repeat=2)) # two fair coin flips
P = lambda E: Fraction(sum(1 for w in S if E(w)), len(S))
A = lambda w: w[0] == 1 # first is heads
B = lambda w: w[1] == 1 # second is heads
C = lambda w: w[0] == w[1] # both the same
both = lambda X, Y: (lambda w: X(w) and Y(w))
print("pairwise:", P(both(A, B)) == P(A) * P(B), P(both(A, C)) == P(A) * P(C), P(both(B, C)) == P(B) * P(C))
print("all three:", P(lambda w: A(w) and B(w) and C(w)), "vs product", P(A) * P(B) * P(C))pairwise: True True True
all three: 1/4 vs product 1/8
Any two of the events are independent, but knowing and determines . Hash function families with this property ("pairwise independent hashing") are cheap to build and are enough for many algorithms.
Simpson's paradox
Conditioning can reverse a comparison. Suppose treatment X has a higher success rate than Y for small cases and for large cases, but X is mostly used on large (hard) cases. Then overall X can look worse than Y. The overall rate is a weighted average, and the weights differ between the groups. Always ask what is being conditioned on.
In software
Bayes' rule is the core of spam filters (Naive Bayes), anomaly detection and A/B test interpretation. Monitoring alerts suffer from the base-rate trap: an alert that is "99% accurate" on a rare failure mode can still be mostly noise. Independence assumptions (failures of different disks, different data centres) drive reliability calculations, and correlated failures are where they break.
Exercises
Exercise 1: Cards
Two cards are drawn without replacement. What is the probability that both are aces? Given that the first is an ace, what is the probability the second is?
Answer
; .
Solution
. By the product rule, .
Exercise 2: Two children
A family has two children, and at least one is a girl. What is the probability that both are girls (assume each child is a girl with probability , independently)?
Answer
.
Solution
Outcomes GG, GB, BG, BB are equally likely. Conditioning on "at least one girl" leaves GG, GB, BG, so . (If instead you learn that the older child is a girl, the answer is : the conditioning event matters.)
Exercise 3: Independent or not? (code)
Roll two dice. Are "the sum is 7" and "the first die shows 4" independent? What about "the sum is 8" and "the first die shows 4"?
Answer
Sum 7: independent. Sum 8: not independent.
Solution
from fractions import Fraction
from itertools import product
S = list(product(range(1, 7), repeat=2))
P = lambda E: Fraction(sum(1 for w in S if E(w)), 36)
first4 = lambda w: w[0] == 4
for target in (7, 8):
s = lambda w: w[0] + w[1] == target
print(target, P(lambda w: s(w) and first4(w)), P(s) * P(first4))7 1/36 1/36
8 1/36 5/216
For sum 7, every first value leaves exactly one second value, so the first die carries no information. For sum 8, a first die of 1 makes 8 impossible, so the events are dependent.
Test yourself
is defined as…
A test for a rare condition (1 in 10,000) has a 1% false-positive rate. A positive result means the condition is…
- almost certain
- about 99% likely
- still unlikely
- impossible
Pairwise independence of three events implies mutual independence.
- Always
- Never
- Not in general
- Only for coins