A medical test for a rare disease comes back positive. Should you be worried? Your gut says "the test is 99% accurate, so I'm almost certainly sick," but that's the wrong question, it ignores how rare the disease was in the first place. If the disease affects 1 in 100 people, and even a small fraction of HEALTHY people also test positive (a false positive rate), the healthy false-positives can vastly outnumber the sick true-positives in raw counts, even with a 99%-accurate test. The right question is: "of everyone who tests positive, healthy and sick combined, what fraction are actually sick?"
Bayes' theorem is the formula that answers exactly this: it takes a prior belief (how common is the disease, before any test), combines it with how the evidence behaves under each hypothesis (how likely is a positive test if you're sick, vs if you're not), and produces the correct, updated belief (how likely are you sick, given the positive test).
Theory gives the raw formula (posterior = likelihood * prior / evidence) and, for the common binary-hypothesis case, shows how to compute the evidence term yourself from the two conditional likelihoods and the prior.
Implement bayes_theorem(prior, likelihood, evidence) first, the direct formula, then posterior_binary(prior_h, likelihood_e_given_h, likelihood_e_given_not_h), which computes the evidence term and calls the first function.
[0, 1].posterior_binary must compute the evidence term itself (P(evidence), the total probability of seeing the evidence at all, summed over both the H-true and H-false cases), not take it as a separate argument.posterior_binary should call bayes_theorem rather than reimplementing the same division.Open one at a time. Each gives away a little more than the last.
bayes_theorem is one line: multiply, then divide.
The evidence term is a weighted average: P(evidence | H) * P(H) + P(evidence | not H) * P(not H), and P(not H) = 1 - P(H).
Click "Run Tests" to test your implementation