TExcelTAD
Home › Discrete Mathematics › Conditional Probability and Independence

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 AA and BB with Pr⁡[B]>0\Pr[B] > 0:

Pr⁡[A∣B]=Pr⁡[A∩B]Pr⁡[B]\Pr[A \mid B] = \frac{\Pr[A \cap B]}{\Pr[B]}

"The probability of AA given BB": restrict the sample space to BB and renormalise. Rearranged, it is the product rule Pr⁡[A∩B]=Pr⁡[B]⋅Pr⁡[A∣B]\Pr[A \cap B] = \Pr[B] \cdot \Pr[A \mid B], which is what you multiply along the branches of a tree diagram.

Law of total probability. If B1,…,BkB_1, \ldots, B_k partition the sample space,

Pr⁡[A]=∑iPr⁡[A∣Bi]Pr⁡[Bi]\Pr[A] = \sum_{i} \Pr[A \mid B_i] \Pr[B_i]

Bayes' rule and the false-positive trap

Pr⁡[B∣A]=Pr⁡[A∣B]Pr⁡[B]Pr⁡[A]\Pr[B \mid A] = \frac{\Pr[A \mid B] \Pr[B]}{\Pr[A]}

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?

python · try it yourself
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 Pr⁡[B]\Pr[B] is the base rate fallacy.

Independence

AA and BB are independent if Pr⁡[A∩B]=Pr⁡[A]Pr⁡[B]\Pr[A \cap B] = \Pr[A] \Pr[B], equivalently Pr⁡[A∣B]=Pr⁡[A]\Pr[A \mid B] = \Pr[A]: knowing BB tells nothing about AA.

Events A1,…,AnA_1, \ldots, A_n are mutually independent if the product rule holds for every subcollection. Pairwise independence (every pair) is weaker:

python · try it yourself
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 AA and BB determines CC. 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

1221\frac{1}{221}; 351=117\frac{3}{51} = \frac{1}{17}.

Solution

Pr⁡[2nd ace∣1st ace]=351\Pr[\text{2nd ace} \mid \text{1st ace}] = \frac{3}{51}. By the product rule, Pr⁡[both]=452⋅351=122652=1221\Pr[\text{both}] = \frac{4}{52} \cdot \frac{3}{51} = \frac{12}{2652} = \frac{1}{221}.

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 12\frac12, independently)?

Answer

13\frac{1}{3}.

Solution

Outcomes GG, GB, BG, BB are equally likely. Conditioning on "at least one girl" leaves GG, GB, BG, so Pr⁡=13\Pr = \frac{1}{3}. (If instead you learn that the older child is a girl, the answer is 12\frac{1}{2}: 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
python · try it yourself
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

Pr⁡[A∣B]\Pr[A \mid B] is defined as…

  1. Pr⁡[A]Pr⁡[B]\Pr[A]\Pr[B]
  2. Pr⁡[A∩B]/Pr⁡[B]\Pr[A \cap B] / \Pr[B]
  3. Pr⁡[B∣A]\Pr[B \mid A]
  4. Pr⁡[A]/Pr⁡[B]\Pr[A] / \Pr[B]

A test for a rare condition (1 in 10,000) has a 1% false-positive rate. A positive result means the condition is…

  1. almost certain
  2. about 99% likely
  3. still unlikely
  4. impossible

Pairwise independence of three events implies mutual independence.

  1. Always
  2. Never
  3. Not in general
  4. Only for coins