BasicsBayes TheoremGoogleMetaJane Street
Problem
Given prior P(A)= p_a, likelihood P(B∣A)= p_b_given_a, and marginal evidence P(B)= p_b > 0, implement bayes_posterior(p_a, p_b_given_a, p_b) returning P(A∣B)=P(B)P(B∣A)P(A).
Why It Matters: Bayes' Rule is the engine of spam filtering, medical diagnosis, Bayesian A/B testing, and probabilistic machine learning.
Function Signature
def bayes_posterior(p_a: float, p_b_given_a: float, p_b: float) -> float:
Sample Input & Output
Example 1: Disease Screening Posterior
Input:
p_a = 0.01, p_b_given_a = 0.90, p_b = 0.0585
Expected Output:
0.153846
Explanation: (0.90 * 0.01) / 0.0585 ≈ 0.153846 — even a 90%-sensitive test has ~15.4% posterior when base rate is 1%!
Example 2: Uninformative Evidence (P(B|A) = P(B))
Input:
p_a = 0.4, p_b_given_a = 0.25, p_b = 0.25
Expected Output:
0.4
Explanation: When evidence is equally likely under A and overall, the posterior equals the prior 0.4.
Constraints & Edge Cases
- Return posterior float (
p_b > 0). p_b > 0is guaranteed.- Return a Python
floatin[0.0, 1.0].
Constraints & Evaluation Criteria
- Return posterior float (`p_b > 0`).
Hints
Python 3⌘/Ctrl+Enter to Run · ⌘/Ctrl+Shift+Enter to Submit
Loading Code Editor...
+1 Hidden Submit Case
Input / Scenario
p_a=0.1, p_b_given_a=0.8, p_b=0.2
Expected Output / Criteria
0.4