Have a go at it. The editor and the docs panel are open, and your code is saved as you type. Running it needs a free account — you’ll come back to exactly what you wrote.
Bayes' Rule
Bayes' rule turns a test result into a belief:
The classic result: a disease affects 1 in 100 people, and a test catches 99% of cases with a 5% false positive rate. You test positive. Your chance of having it is not 99%:
P(E) = 0.99*0.01 + 0.05*0.99 = 0.0594 P(H|E) = 0.99*0.01 / 0.0594 = 0.1667
About 17%. The prior dominates because the disease is rare, and no amount of intuition gets you there without doing the arithmetic. This is also exactly why accuracy is a bad metric on imbalanced data.
Your task:
Implement posterior(prior, likelihood, false_positive_rate) returning P(H∣E) as a float.
false_positive_rate is P(E∣¬H).
Example Tests
The rare-disease case: a positive test still leaves you probably fine
Input: {"prior":0.01,"likelihood":0.99,"false_positive_rate":0.05}
Expected: 0.16667
A perfect test with no false positives confirms the hypothesis outright
Input: {"prior":0.3,"likelihood":0.9,"false_positive_rate":0}
Expected: 1
Evidence that is equally likely either way leaves the prior untouched
Input: {"prior":0.4,"likelihood":0.5,"false_positive_rate":0.5}
Expected: 0.4
def posterior(prior, likelihood, false_positive_rate):
"""
Bayes' rule for a binary hypothesis.
Args:
prior: P(H)
likelihood: P(E | H)
false_positive_rate: P(E | not H)
Returns:
float: P(H | E)
"""
# YOUR CODE HERE
pass