Lesson 3 of 10 · Probability and Statistics

Bayes' theorem

Bayes' theorem reverses the direction of the condition: from the probability of the evidence given the hypothesis to the probability of the hypothesis given the evidence; it is the basis of Bayesian classifiers and Bayesian inference.

Mathematics for machine learning Difficulty: Intermediate Study time: 45 min
Lesson contents
  1. Introduction
  2. Why study this topic?
  3. Theoretical explanation
  4. Mathematical definitions
  5. The equations
  6. Worked example
  7. Application in AI
  8. Interactive lab
  9. Practice exercises
  10. Summary
  11. Short quiz
  12. Related lessons

1 Introduction

We often know how likely some evidence is if a hypothesis is true, such as the sensitivity of a medical test, but we want the reverse: what is the probability of disease if the test result is positive? Bayes' theorem answers this.

Learning objectives

  • Deriving Bayes' theorem from the definition of conditional probability
  • Telling apart the prior, the likelihood and the posterior
  • Solving diagnostic test problems
  • Understanding the principle of Naive Bayes classification

2 Why study this topic?

The Naive Bayes classifier for filtering messages, Bayesian inference for updating our beliefs about model parameters, and uncertainty estimation in Bayesian networks are all applications of this theorem. Understanding it protects against the base rate fallacy.

3 Theoretical explanation

Formula

\[P(H\mid E) = \frac{P(E\mid H)\,P(H)}{P(E)},\quad P(E) = P(E\mid H)P(H) + P(E\mid H^c)P(H^c)\]

TermNameMeaning
\(P(H)\)Prior probabilityOur belief before seeing the evidence
\(P(E\mid H)\)LikelihoodHow well the evidence fits the hypothesis
\(P(H\mid E)\)Posterior probabilityOur belief after the evidence
\(P(E)\)EvidenceA normalizing constant

The base rate

If a disease is rare, most positive results may be false even with an accurate test, because there are many more healthy people.

Bayesian classification

We choose the class that maximizes the posterior: \(\hat y = \arg\max_y P(\mathbf{x}\mid y)P(y)\), and we do not need \(P(\mathbf{x})\) because it is shared by all classes.

4 Mathematical definitions

Bayes' theorem
A formula that gives the posterior probability from the likelihood and the prior probability.
\[P(H\mid E) = \frac{P(E\mid H)P(H)}{P(E)}\]
Prior probability
The probability of a hypothesis before seeing the data.
\[P(H)\]
Posterior probability
The probability of a hypothesis after seeing the data.
\[P(H\mid E)\]
Bayes' theorem
A formula that computes the probability of a hypothesis after the evidence from the likelihood of the evidence and the prior probability of the hypothesis.
\[P(H\mid E)=\frac{P(E\mid H)P(H)}{P(E)}\]
Prior and posterior probability
The prior is our belief before the data, and the posterior is our belief after it according to Bayes' theorem.
\[p(\theta\mid\mathcal{D})\propto p(\mathcal{D}\mid\theta)p(\theta)\]

5 The equations

\[P(H\mid E) = \frac{P(E\mid H)P(H)}{P(E\mid H)P(H) + P(E\mid H^c)P(H^c)}\]
The expanded formula
\[\hat{y} = \arg\max_{y} P(y)\prod_{j} P(x_j\mid y)\]
The Naive Bayes classifier
\[p(\theta\mid\mathcal{D}) \propto p(\mathcal{D}\mid\theta)\,p(\theta)\]
Bayesian inference

6 Step-by-step worked example

A diagnostic test

Problem: A disease has a prevalence of 1%, the test has 95% sensitivity and 90% specificity (a 10% false-positive rate). What is the probability of disease if the result is positive?

  1. \(P(D) = 0.01\), \(P(+\mid D) = 0.95\), \(P(+\mid D^c) = 0.10\).
  2. \(P(+) = 0.95\cdot0.01 + 0.10\cdot0.99 = 0.0095 + 0.099 = 0.1085\).
  3. \(P(D\mid +) = 0.0095/0.1085 \approx 0.0876\).

Result: Only about 8.8%, because the disease is rare.

7 Practical application in AI

  • Spam filtering: Naive Bayes combines the evidence of the words to compute the probability that a message is spam.
  • Bayesian inference: We update the distribution of the model parameters after seeing the data, which gives an estimate of uncertainty instead of a single value.
  • AI-assisted diagnosis: Turning a classifier's output into a posterior probability requires knowing the disease prevalence in the target population.

8 Interactive lab linked to the lesson

A probability calculator, the normal and binomial distributions with plots, descriptive statistics (mean, median, mode, variance and standard deviation), correlation, random sampling, and a Bayes' theorem calculator.

Open the Probability & Statistics Lab

9 Practice exercises

Review questions for reflection

  • How does the posterior in the example change if the prevalence becomes 10%?
  • Why does the Bayesian classifier not need to compute \(P(\mathbf{x})\)?

Common mistakes

  • Equating \(P(H\mid E)\) with \(P(E\mid H)\).
  • Ignoring the prior probability (the base rate).
  • Forgetting the false-positive term in the denominator.

Practice 5 questions from the question bank for this lesson

10 Lesson summary

  • Posterior ∝ likelihood × prior.
  • The base rate has a large effect on the result.
  • Naive Bayes applies Bayes' theorem with the conditional independence assumption.

11 Short quiz

5 questions · 10 minutes · pass mark 60% — passing it completes the lesson.