Conditional Probability P(A | B) From a 2×2 Count Table

Basics
BasicsConditional ProbabilityGoogleMetaJane Street

Problem

Given contingency counts count_a_and_b (both AA and BB occurred) and count_not_a_and_b (BB occurred without AA), implement conditional_from_counts(count_a_and_b, count_not_a_and_b) returning P(A∣B)=count(A∩B)count(B)P(A \mid B) = \frac{\text{count}(A \cap B)}{\text{count}(B)}.

Why It Matters: Conditioning shrinks the sample space to only the rows where BB happened—the exact calculation behind precision, positive predictive value, and funnel conversion.

Function Signature

def conditional_from_counts(count_a_and_b: int, count_not_a_and_b: int) -> float:

Sample Input & Output

Example 1: Medical Test Precision

Input:

count_a_and_b = 30, count_not_a_and_b = 70

Expected Output:

0.3

Explanation: Out of 30 + 70 = 100 positive tests, 30 truly have condition AA, so P(A∣B)=0.3P(A \mid B) = 0.3.

Example 2: Zero False Positives

Input:

count_a_and_b = 25, count_not_a_and_b = 0

Expected Output:

1.0

Explanation: Every single BB event also has AA, giving 25 / 25 = 1.0.

Constraints & Edge Cases

  • Return float conditional probability.
  • Denominator is count_a_and_b + count_not_a_and_b > 0.
  • Return a Python float in [0.0, 1.0].

Constraints & Evaluation Criteria

  • Return `float` conditional probability.

Hints

Python 3
Loading Code Editor...
+1 Hidden Submit Case
Input / Scenario
20, 60
Expected Output / Criteria
0.25