By: Krischal Khanal
Coding the Bayesian update
def bayesian_update(prior, likelihood_given_true, likelihood_given_false):
"""
prior : P(H) — prior probability of hypothesis
likelihood_given_true : P(E|H) — probability of evidence if H is true
likelihood_given_false: P(E|¬H) — probability of evidence if H is false
"""
marginal = (prior * likelihood_given_true) + ((1 - prior) * likelihood_given_false)
posterior = (prior * likelihood_given_true) / marginal
return posterior
# The rare disease example
prior = 0.001 # 1 in 1000 people have the disease
p_e_given_h = 0.99 # test sensitivity
p_e_given_not_h = 0.05 # false positive rate
posterior = bayesian_update(prior, p_e_given_h, p_e_given_not_h)
print(f"Posterior probability of disease given positive test: {posterior:.4f}")
# Output: Posterior probability of disease given positive test: 0.0194Sequential updating
One elegant property of Bayesian updating is you can do it repeatedly. The posterior from one update becomes the prior for the next.
Let's say you took a second, independent test, and it also comes back positive:
# First positive test
posterior_1 = bayesian_update(0.001, 0.99, 0.05)
print(f"After first positive test: {posterior_1:.4f}")
# Second positive test — use posterior_1 as new prior
posterior_2 = bayesian_update(posterior_1, 0.99, 0.05)
print(f"After second positive test: {posterior_2:.4f}")After first positive test: 0.0194
After second positive test: 0.2818
After two independent tests, which came out positive both times, you now become 28% sure you have the disease. The evidences accumulate to form a more informed hypothesis.
Imagine you took a third test, and it came out positive too.
# Third positive test — use posterior_2 as new prior
posterior_3 = bayesian_update(posterior_2, 0.99, 0.05)
print(f"After third positive test: {posterior_3:.4f}")After third positive test: 0.8860
After third test that came out positive, you are now 88.6% certain that you have the disease.
But what if test result isn't always positive, but sometimes positive and sometimes negative, i.e. stochastic? We'll answer it on the upcoming blog in the blog series. But in order to understand it, we must understand the missing half of the Bayes' Theorem.
Code blocks are licensed under the MIT License. All other content is licensed under CC-BY 4.0 Copyright © 2026 Krischal Khanal.