Lesson 5 of 15

Independence

When Events Don't Influence Each Other

Events AA and BB are independent if knowing BB occurred tells you nothing about AA:

P(AB)=P(A)P(A \mid B) = P(A)

Equivalently (and more useful in practice):

P(AB)=P(A)P(B)P(A \cap B) = P(A) \cdot P(B)

# Two fair coin flips are independent
p_heads_1 = 0.5
p_heads_2 = 0.5
p_both = p_heads_1 * p_heads_2

print(p_both)  # 0.25 — P(HH)

Testing for Independence

Given P(A)P(A), P(B)P(B), and P(AB)P(A \cap B), events are independent iff:

P(AB)P(A)P(B)<ϵ|P(A \cap B) - P(A) \cdot P(B)| < \epsilon

Mutual Exclusivity ≠ Independence

Mutually exclusive events (where P(AB)=0P(A \cap B) = 0) are almost never independent (unless one has probability 0). If AA occurs, BB definitely cannot — that is dependence.

Your Task

Implement two functions:

  1. are_independent(p_a, p_b, p_ab) — returns True if P(AB)P(A)P(B)<109|P(A \cap B) - P(A) \cdot P(B)| < 10^{-9}
  2. joint_independent(p_a, p_b) — returns P(A)P(B)P(A) \cdot P(B), rounded to 4 decimal places

Print the independence check, then the joint probability.

Pyodide loading...
Loading...
Click "Run" to execute your code.