Law of Total Probability: P(B)

Basics
BasicsTotal ProbabilityGoogleMetaJane Street

Problem

Implement total_probability(p_a, p_b_given_a, p_b_given_not_a) returning the marginal probability P(B)=P(B∣A)P(A)+P(B∣Ac)(1−P(A))P(B) = P(B \mid A) P(A) + P(B \mid A^c)(1 - P(A)).

Why It Matters: The Law of Total Probability computes the exact denominator P(B)P(B) needed inside Bayes' Rule by summing across mutually exclusive cases (AA and AcA^c).

Function Signature

def total_probability(p_a: float, p_b_given_a: float, p_b_given_not_a: float) -> float:

Sample Input & Output

Example 1: Two-Factory Defect Rate

Input:

p_a = 0.3, p_b_given_a = 0.02, p_b_given_not_a = 0.05

Expected Output:

0.041

Explanation: Factory A produces 30% of parts (2% defect) and Factory B produces 70% (5% defect) → 0.3*0.02 + 0.7*0.05 = 0.041.

Example 2: Equal Conditional Rates

Input:

p_a = 0.2, p_b_given_a = 0.6, p_b_given_not_a = 0.6

Expected Output:

0.6

Explanation: If P(B∣A)=P(B∣Ac)=0.6P(B \mid A) = P(B \mid A^c) = 0.6, then P(B)=0.6P(B) = 0.6 regardless of P(A)P(A).

Constraints & Edge Cases

  • Return P(B) as a float.
  • Guaranteed all input probabilities lie in [0.0, 1.0].
  • Return a Python float.

Constraints & Evaluation Criteria

  • Return `P(B)` as a float.

Hints

Python 3
Loading Code Editor...
+1 Hidden Submit Case
Input / Scenario
p_a=0.1, 0.9, 0.05
Expected Output / Criteria
0.135