Bayes' Theorem Explained with Detailed Examples

Formula, derivation, and 4 fully solved real-world problems

Overview

Bayes' Theorem is a foundational result in probability theory for computing conditional probabilities in reverse. It lets us update the probability of a scenario after observing new evidence.

This page walks through the theorem's derivation, gives the working formula, and solves four real-world examples: colored balls, disease testing (the base-rate fallacy), factory defects, and radar detection.

Derivation of Bayes' Theorem

Start from the Law of Total Probability:

Partition of a sample space into mutually exclusive events E₁ through Eₙ, with event A intersected across them
Figure 1 — Event A partitioned across mutually exclusive, exhaustive events E₁, …, Eₙ.

For an event \( A \) and mutually exclusive, exhaustive events \( E_1, \dots, E_n \):

\[ P(A) = \sum_{i=1}^{n} P(A \mid E_i)\,P(E_i) \]

By the definition of conditional probability, the intersection can be written two ways:

\[ P(A)\,P(E_i \mid A) = P(E_i)\,P(A \mid E_i) \]

Solving for \( P(E_i \mid A) \):

\[ P(E_i \mid A) = \frac{P(E_i)\,P(A \mid E_i)}{P(A)} \]

Finally, substitute \( P(A) \) from the Law of Total Probability above:

\[ P(E_i \mid A) = \frac{P(E_i)\,P(A \mid E_i)}{\sum_{j=1}^{n} P(A \mid E_j)\,P(E_j)} \]

Bayes' Theorem Formula

Bayes' Theorem formula
Name Formula
Bayes' Theorem \[ P(E_i \mid A) = \frac{P(E_i)\,P(A \mid E_i)}{\sum_{j=1}^{n} P(A \mid E_j)\,P(E_j)} \]

Intuition. The numerator \( P(E_i)\,P(A \mid E_i) \) is the probability of the path "scenario \( E_i \) → evidence \( A \)." The denominator sums this over all possible scenarios. The quotient asks: what fraction of all paths to A went through \( E_i \)?

Solved Examples of Bayes' Theorem

Example 1 — Selecting balls from two boxes

Two boxes contain colored balls:

  • Box 1: 4 red, 2 green
  • Box 2: 4 green, 2 red

The boxes are selected with \( P(\text{Box 1}) = \tfrac{1}{3} \), \( P(\text{Box 2}) = \tfrac{2}{3} \), then a ball is drawn at random from the chosen box.

  1. Given the ball is red, what is the probability it came from Box 1?
  2. Given the ball is red, what is the probability it came from Box 2?
  3. Compare and interpret the two probabilities.
View solution

Let \( B_1 \) = select Box 1, \( B_2 \) = select Box 2, \( R \) = select red.

Probability tree for Example 1: Box 1 or Box 2 branch followed by red or green outcomes
Figure 2 — Probability tree for Example 1.

Given:

\[ P(B_1) = \tfrac{1}{3}, \quad P(B_2) = \tfrac{2}{3} \] \[ P(R \mid B_1) = \tfrac{4}{6} = \tfrac{2}{3}, \quad P(R \mid B_2) = \tfrac{2}{6} = \tfrac{1}{3} \]

So the total probability of red is:

\[ P(R) = \tfrac{2}{3}\cdot\tfrac{1}{3} + \tfrac{1}{3}\cdot\tfrac{2}{3} = \tfrac{4}{9} \]

(a) By Bayes' Theorem:

\[ P(B_1 \mid R) = \frac{P(R \mid B_1)\,P(B_1)}{P(R)} = \frac{\tfrac{2}{9}}{\tfrac{4}{9}} = \frac{1}{2} \]

(b) Similarly:

\[ P(B_2 \mid R) = \frac{P(R \mid B_2)\,P(B_2)}{P(R)} = \frac{\tfrac{2}{9}}{\tfrac{4}{9}} = \frac{1}{2} \]

(c) Interpretation. The posterior probabilities are equal (each \( \tfrac{1}{2} \)) despite Box 1 containing proportionally more red balls. Bayes' Theorem explains why: Box 2 is initially twice as likely, and that prior exactly balances Box 1's richer red ratio.

Example 2 — Disease testing and the base-rate fallacy

About 1% of a population has a particular disease. A test is:

  • 95% sensitive — \( P(\text{positive} \mid \text{disease}) = 0.95 \)
  • 98% specific — \( P(\text{negative} \mid \text{no disease}) = 0.98 \), so the false-positive rate is 2%

If a random person tests positive, what is the probability that they actually have the disease?

View solution

Let \( D \) = has disease, \( D^c \) = no disease, \( TP \) = tests positive.

Probability tree for the medical test: disease or no disease branch, followed by positive or negative test outcomes with the given rates
Figure 3 — Probability tree for Example 2.

Given:

  • \( P(D) = 0.01 \) (prior)
  • \( P(D^c) = 0.99 \)
  • \( P(TP \mid D) = 0.95 \) (sensitivity)
  • \( P(TP \mid D^c) = 0.02 \) (false-positive rate)

By Bayes' Theorem:

\[ P(D \mid TP) = \frac{P(TP \mid D)\,P(D)}{P(TP \mid D)\,P(D) + P(TP \mid D^c)\,P(D^c)} \] \[ = \frac{0.95 \times 0.01}{(0.95 \times 0.01) + (0.02 \times 0.99)} \] \[ = \frac{0.0095}{0.0095 + 0.0198} = \frac{0.0095}{0.0293} \approx 0.324 \]

Answer: only about 32.4% of people who test positive actually have the disease.

Interpretation — the base-rate fallacy. This surprisingly low value arises because the disease is rare: the massive healthy population produces far more false positives than the small diseased population produces true positives. Ignoring the prior \( P(D) \) — and focusing only on the "95% accuracy" — is exactly the base-rate fallacy.

Example 3 — Factory defect rates

Three factories produce light bulbs for a supplier:

  • Factory A: 20% of total production, 2% defective
  • Factory B: 50% of total production, 1% defective
  • Factory C: 30% of total production, 3% defective

A randomly purchased bulb is defective. What is the probability it was manufactured by Factory B?

View solution

Let \( A, B, C \) denote the factory of origin and \( D \) = the bulb is defective.

Given:

  • \( P(A) = 0.20, \quad P(B) = 0.50, \quad P(C) = 0.30 \)
  • \( P(D \mid A) = 0.02, \quad P(D \mid B) = 0.01, \quad P(D \mid C) = 0.03 \)

By Bayes' Theorem:

\[ P(B \mid D) = \frac{P(D \mid B)\,P(B)}{P(D \mid A)P(A) + P(D \mid B)P(B) + P(D \mid C)P(C)} \] \[ = \frac{0.01 \times 0.50}{(0.02 \times 0.20) + (0.01 \times 0.50) + (0.03 \times 0.30)} \] \[ = \frac{0.005}{0.004 + 0.005 + 0.009} = \frac{0.005}{0.018} = \frac{5}{18} \approx 0.278 \]

Answer: about 27.8%.

Interpretation. Even though Factory B produces half of all bulbs, it contributes only about 27.8% of the defective bulbs because its 1% defect rate is the lowest of the three.

Example 4 — Radar detection system

A radar detects an aircraft with 98% probability when one is present. When no aircraft is present, the radar falsely reports a detection 5% of the time. The background probability of an aircraft being present is 7%.

  1. Given a detection, what is the probability no aircraft is present?
  2. Given a detection, what is the probability an aircraft is present?
  3. Given no detection, what is the probability an aircraft is present?
  4. Given no detection, what is the probability no aircraft is present?
View solution

Let \( A \) = aircraft present, \( A^c \) = no aircraft, \( D \) = radar detects, \( D^c \) = no detection.

Given: \( P(A) = 0.07 \), \( P(A^c) = 0.93 \), \( P(D \mid A) = 0.98 \), \( P(D \mid A^c) = 0.05 \).

So \( P(D^c \mid A) = 0.02 \) and \( P(D^c \mid A^c) = 0.95 \).

Total probability of detection:

\[ P(D) = (0.98 \times 0.07) + (0.05 \times 0.93) = 0.0686 + 0.0465 = 0.1151 \]

(a) False alarm given detection:

Probability tree for the radar example: given a detection, split by aircraft present or absent
Figure 4 — Tree for parts (a) and (b).
\[ P(A^c \mid D) = \frac{P(D \mid A^c)\,P(A^c)}{P(D)} = \frac{0.0465}{0.1151} \approx 0.404 \]

(b) True aircraft given detection:

\[ P(A \mid D) = \frac{P(D \mid A)\,P(A)}{P(D)} = \frac{0.0686}{0.1151} \approx 0.596 \]

Check: \( 0.404 + 0.596 = 1 \) ✓

(c) Undetected aircraft given no detection:

Probability tree for the radar example: given no detection, split by aircraft present or absent
Figure 5 — Tree for parts (c) and (d).

Total probability of no detection:

\[ P(D^c) = (0.02 \times 0.07) + (0.95 \times 0.93) = 0.0014 + 0.8835 = 0.8849 \] \[ P(A \mid D^c) = \frac{P(D^c \mid A)\,P(A)}{P(D^c)} = \frac{0.0014}{0.8849} \approx 0.0016 \]

(d) Clear sky given no detection:

\[ P(A^c \mid D^c) = \frac{P(D^c \mid A^c)\,P(A^c)}{P(D^c)} = \frac{0.8835}{0.8849} \approx 0.9984 \]

Check: \( 0.0016 + 0.9984 = 1 \) ✓

Complete probability tree:

Complete probability tree for the radar example with all four leaf probabilities labeled
Figure 6 — Full probability tree for Example 4.

Practice Problems

Practice 1 — Two urns

Urn A has 3 white and 2 black marbles. Urn B has 1 white and 4 black marbles. An urn is chosen at random (equally likely), and a marble is drawn and turns out white. What is the probability the marble came from Urn A?

Answer

\( P(A \mid W) = \dfrac{(3/5)(1/2)}{(3/5)(1/2) + (1/5)(1/2)} = \dfrac{3/10}{4/10} = \dfrac{3}{4} \).

Practice 2 — Spam filter

40% of incoming emails are spam. The word "free" appears in 60% of spam and 5% of legitimate emails. An email contains "free." What is the probability it is spam?

Answer

\( P(\text{spam} \mid \text{free}) = \dfrac{0.60 \times 0.40}{(0.60 \times 0.40) + (0.05 \times 0.60)} = \dfrac{0.24}{0.24 + 0.03} = \dfrac{0.24}{0.27} = \dfrac{8}{9} \approx 0.889 \).

Practice 3 — Two-machine factory

Machine 1 produces 60% of a factory's output with a 3% defect rate; Machine 2 produces the remaining 40% with a 1% defect rate. A randomly selected item is defective. What is the probability it came from Machine 1?

Answer

\( P(\text{M1} \mid D) = \dfrac{0.03 \times 0.60}{(0.03 \times 0.60) + (0.01 \times 0.40)} = \dfrac{0.018}{0.018 + 0.004} = \dfrac{0.018}{0.022} = \dfrac{9}{11} \approx 0.818 \).

Frequently Asked Questions

What is Bayes' Theorem?

Bayes' Theorem relates a conditional probability \(P(E_i \mid A)\) to its reverse \(P(A \mid E_i)\): \(P(E_i \mid A) = \dfrac{P(E_i)\,P(A \mid E_i)}{\sum_j P(A \mid E_j)\,P(E_j)}\). It updates our belief about which scenario \(E_i\) caused the observed evidence \(A\).

What is the base rate fallacy?

The base rate fallacy is the tendency to overestimate \(P(D \mid TP)\) — the probability of a disease given a positive test — while ignoring the prior probability \(P(D)\). Even a highly accurate test can give a surprisingly low posterior probability when the disease is rare, because false positives from the large healthy population outweigh true positives from the small diseased population.

How is Bayes' Theorem derived from the Law of Total Probability?

Start with the symmetric form of conditional probability \(P(A)P(E_i \mid A) = P(E_i)P(A \mid E_i)\). Solve for \(P(E_i \mid A)\) to get \(P(E_i \mid A) = \dfrac{P(E_i)P(A \mid E_i)}{P(A)}\), then substitute \(P(A) = \sum_j P(A \mid E_j)P(E_j)\) from the Law of Total Probability.

When should I use Bayes' Theorem?

Use Bayes' Theorem when you know \(P(A \mid E_i)\) for each scenario \(E_i\) and the prior \(P(E_i)\), and you want to find \(P(E_i \mid A)\) — the probability of scenario \(E_i\) given observed evidence \(A\). Common applications: medical testing, spam filtering, fault diagnosis, and forensic evidence.

Related Probability Resources

Last updated: October 20, 2026