Loading the problem
Compute a posterior probability from a prior and a likelihood.
P(A | B) = P(B | A) * P(A) / P(B)
P(B) = P(B | A) * P(A) + P(B | not A) * (1 - P(A))
The classic use is medical testing. A test with 99% sensitivity for a disease that affects 1 in 1000 people still gives mostly false positives, because the prior is so small. Working that out by hand is the fastest cure for base rate neglect.
If P(B) comes out as zero, return 0.0.
def bayes(prior, likelihood, false_positive):
Input
bayes(prior=0.001, likelihood=0.99, false_positive=0.01)
Output
0.09016393442622951
Reasoning
P(B) = 0.99 * 0.001 + 0.01 * 0.999 = 0.01098. The posterior is 0.00099 / 0.01098, about 0.09. Even with a good test, a positive result means only a 9% chance of disease, because the disease is rare.
0.0 when P(B) is 0.[0, 1].Check runs the visible cases against a fresh kernel. Submit runs the hidden ones too.
⇧↵run cell