Bayes' Theorem: Updating Beliefs with Evidence

A deep dive into Bayes’ theorem — from its 18th-century origins to modern applications in spam filters, medical diagnosis, and machine learning.

Your ability to do good science depends on your ability to update your beliefs in the face of evidence.

— E. T. Jaynes [1]

The Problem of Inverse Probability

Suppose you test positive for a rare disease. The test is 99% accurate. Should you panic?The intuitive answer is “yes” — but as we’ll see, the correct answer depends critically on how rare the disease is.

This is a question of inverse probability: given an observed effect (positive test), what is the probability of the underlying cause (having the disease)? For centuries, this kind of reasoning was considered impossible or meaningless. It took a Presbyterian minister named Thomas Bayes to crack the problem.

Bayes’ Theorem

Bayes’ theorem relates the conditional probability of a hypothesis 𝐻 given evidence 𝐸 to the reverse:

𝑃(𝐻|𝐸)=𝑃(𝐸|𝐻)⋅𝑃(𝐻)𝑃(𝐸)

(1)

where:

A Simple Example

Consider a medical test for a disease that affects 1 in 1,000 peopleThis base rate is crucial. Many real-world errors in reasoning come from ignoring it — a phenomenon called base rate neglect.:

What is 𝑃(disease|positive)?

First, compute the marginal probability of testing positive:

𝑃(positive)=𝑃(positive|disease)⋅𝑃(disease)+𝑃(positive|no disease)⋅𝑃(no disease)

(2)

=0.99×0.001+0.05×0.999=0.05094

(3)

Then apply Equation 1:

𝑃(disease|positive)=0.99×0.0010.05094≈0.0194

(4)

The result is shocking: despite the test being 99% accurate, a positive result only means a  2% chance of actually having the disease. This counterintuitive result arises because the false positives from the large healthy population (5%) vastly outnumber the true positives from the tiny sick population (0.1%).

Visualizing the Update

The shift from prior to posterior is best seen visually. Figure 1 shows how the distribution concentrates around the true value as evidence accumulates:

The following table tracks how our belief updates through each piece of evidence:

Table 1: Sequential Bayesian updates. Each independent positive test dramatically shifts the probability.
StageP(disease)Odds
Prior (before test)0.0011 : 999
After positive test0.01941 : 50.5
After second positive test0.2841 : 2.5
After third positive test0.8676.5 : 1

This is why doctors order confirmatory tests. A single positive is weak evidence; multiple independent positives compound into strong evidence.

The Odds Form

Bayes’ theorem has an elegant equivalent in odds form that makes sequential updating trivial:

odds(𝐻|𝐸)=LR×odds(𝐻)

(5)

where the likelihood ratio LR=𝑃(𝐸|𝐻)/𝑃(𝐸|¬𝐻) and odds(𝐻)=𝑃(𝐻)/(1−𝑃(𝐻)).

This is the form used in practice The odds form avoids computing the marginal probability 𝑃(𝐸) entirely — the likelihood ratio handles it implicitly. because it factors out 𝑃(𝐸) and lets us chain updates:

odds𝑛=LR1×LR2×…×LR𝑛×odds0

(6)

Figure 2 illustrates the likelihood ratio concept: when the evidence is much more likely under the hypothesis than under the alternative, the LR is large and the update is strong.

Figure 2: The likelihood ratio compares how probable the evidence is under competing hypotheses. A large LR means strong evidence for the hypothesis.

Implementation

The code is straightforward. Here’s a Python implementation that handles both single and sequential updates:

class BayesUpdater:
    """Bayesian belief updater using the odds form."""

    def __init__(self, prior: float):
        self.odds = prior / (1 - prior)
        self.history = [prior]

    def update(self, lr: float) -> float:
        """Update belief with a new likelihood ratio."""
        self.odds *= lr
        p = self.odds / (1 + self.odds)
        self.history.append(p)
        return p

    @property
    def probability(self) -> float:
        return self.odds / (1 + self.odds)

# Medical diagnosis example
belief = BayesUpdater(prior=0.001)

# First positive test (LR = sensitivity / (1 - specificity))
belief.update(lr=0.99 / 0.05)   # P ≈ 0.0194
print(f"After 1st positive: {belief.probability:.4f}")

# Second independent positive test
belief.update(lr=0.99 / 0.05)   # P ≈ 0.284
print(f"After 2nd positive: {belief.probability:.4f}")

# Third independent positive test
belief.update(lr=0.99 / 0.05)   # P ≈ 0.867
print(f"After 3rd positive: {belief.probability:.4f}")

Output:

After 1st positive: 0.0194
After 2nd positive: 0.2841
After 3rd positive: 0.8673

Visualizing the sequence: Figure 3 shows how the probability climbs with each independent positive test — starting from near zero and crossing 50% after just three tests.

Figure 3: Sequential Bayesian updates in the medical diagnosis example. Each positive test dramatically shifts the posterior probability.

Applications

Spam Filtering

Email spam filters were one of the first killer apps of Bayesian reasoning. Paul Graham’s 2002 essay A Plan for Spam [2] showed that a simple Bayesian classifier could achieve >99.5% accuracy.

The idea: each word has a spam probability. Given the words in an email, combine them using Bayes’ theorem:

𝑃(spam|𝑤1,𝑤2,…,𝑤𝑛)=11+∏𝑖=1𝑛1−𝑝𝑖𝑝𝑖⋅1−𝑠𝑠

(7)

where 𝑝𝑖=𝑃(spam|𝑤𝑖) and 𝑠 is the prior spam rate.

Modern spam filters (Gmail, etc.) use more sophisticated models, but the core insight remains Bayesian.

A/B Testing

In product development, Bayesian A/B testing provides a principled way to decide whether variant A or variant B is better:

𝑃(B > A|data)=∫01∫01𝟙[𝑝𝐵>𝑝𝐴]⋅𝑃(𝑝𝐴|data)⋅𝑃(𝑝𝐵|data)𝑑𝑝𝐴𝑑𝑝𝐵

(8)

Unlike frequentist hypothesis testing, this directly answers the question product managers actually care about: “what is the probability that B is better?”

Bayesian Neural Networks

Deep learning has embraced Bayesian ideas. Instead of learning a single weight vector 𝑤, a Bayesian neural network learns a distribution over weights 𝑃(𝑤|data), enabling principled uncertainty estimates Uncertainty quantification is critical for safety-sensitive applications like autonomous driving and medical diagnosis..

Figure 4 compares traditional and Bayesian approaches to neural network weights:

Figure 4: Traditional neural networks learn a single point estimate (left). Bayesian networks learn a distribution over weights (right), capturing uncertainty.
𝑃(𝑦|𝑥,data)=∫𝑃(𝑦|𝑥,𝑤)⋅𝑃(𝑤|data)𝑑𝑤

(9)

The Philosophy of Priors

The choice of prior is the most controversial aspect of Bayesian reasoning. Critics argue it introduces subjectivity; proponents counter that all reasoning involves assumptions, and Bayes just makes them explicit.

There are several schools of thought:

The pragmatic view: the prior matters most with little data. With enough evidence, all reasonable priors converge This is a consequence of the “washing out of priors” — Bayesian posteriors are asymptotically dominated by the likelihood..

Conclusion

Bayes’ theorem is not just a formula — it is a framework for rational thought. It tells us how to update our beliefs when new evidence arrives, how much weight to give that evidence, and when to be skeptical of our own intuitions.

From spam filters to medical diagnosis, from A/B testing to artificial intelligence, Bayesian reasoning is everywhere — often hiding in plain sight. The next time you encounter surprising evidence, ask yourself: what is the likelihood ratio, and what was my prior?

Bibliography

  • [1] E. T. Jaynes, Probability Theory: The Logic of Science. Cambridge University Press, 2003.
  • [2] P. Graham, “A Plan for Spam,” 2002, [Online]. Available: http://www.paulgraham.com/spam.html
  • [3] B. Efron, Large-Scale Inference: Empirical Bayes Methods for Estimation, Testing, and Prediction. Cambridge University Press, 2010.