Biostatistics · Intermediate

The M&M problem

Written by SignalsLab

Early edition: these lessons are being reviewed.

Content

Summary

This lesson applies Bayesian inference to the M&M problem, demonstrating how to update beliefs about bag origins using color distributions. By defining a mutually exclusive and collectively exhaustive hypothesis suite, we calculate posterior probabilities that reveal the likelihood of each bag's production year based on observed candy colors.

Intuition

Imagine you have two bags of M&Ms, one from 1994 and one from 1996, but you don't know which is which. You draw one yellow M&M from Bag 1 and one green M&M from Bag 2. To solve this, we define two hypotheses: Hypothesis A (Bag 1 is 1994, Bag 2 is 1996) and Hypothesis B (Bag 1 is 1996, Bag 2 is 1994). Since we have no prior reason to prefer one arrangement, we assign equal prior probabilities of 1/2 to both. The key insight is that the 1994 mix has a higher proportion of yellow candies (20%) than the 1996 mix (14%), while both have similar green proportions. Therefore, observing a yellow candy in Bag 1 strongly supports Hypothesis A. We calculate the likelihood of the data under each hypothesis, multiply by the priors to get unnormalized posteriors, and then normalize by dividing by the sum of these values. This process updates our belief, showing that Hypothesis A is significantly more probable than Hypothesis B.

Key equations

p(H|D)=p(D|H)⋅p(H)p(D)
(1)

Bayes's theorem relating posterior probability to likelihood, prior, and normalizing constant.

p(D)=∑ip(D|Hi)⋅p(Hi)
(2)

Law of total probability used to calculate the normalizing constant.

Derivation

  1. Define the hypothesis suite: Hypothesis A (Bag 1=1994, Bag 2=1996) and Hypothesis B (Bag 1=1996, Bag 2=1994). These are mutually exclusive and collectively exhaustive.
  2. Assign prior probabilities: p(A) = 1/2 and p(B) = 1/2 due to lack of distinguishing information.
  3. Identify likelihood parameters: 1994 mix has 20% Yellow, 10% Green; 1996 mix has 14% Yellow, 20% Green.
  4. Calculate likelihood for Hypothesis A: p(D|A) = P(Yellow from 1994) * P(Green from 1996) = 0.20 * 0.20 = 0.04.
  5. Calculate likelihood for Hypothesis B: p(D|B) = P(Yellow from 1996) * P(Green from 1994) = 0.14 * 0.10 = 0.014.
  6. Compute unnormalized posteriors: For A, 0.04 * 0.5 = 0.02. For B, 0.014 * 0.5 = 0.007.
  7. Calculate normalizing constant: p(D) = 0.02 + 0.007 = 0.027.
  8. Compute final posteriors: p(A|D) = 0.02 / 0.027 ≈ 0.74 and p(B|D) = 0.007 / 0.027 ≈ 0.26.

Worked example

Problem

Given the M&M color distributions (1994: 20% Yellow, 10% Green; 1996: 14% Yellow, 20% Green) and equal priors, calculate the posterior probability that Bag 1 is from 1994 given a Yellow draw from Bag 1 and a Green draw from Bag 2.

  1. Hypothesis A: Bag 1 is 1994, Bag 2 is 1996. Likelihood = 0.20 * 0.20 = 0.04. Unnormalized Posterior = 0.04 * 0.5 = 0.02.
  2. Hypothesis B: Bag 1 is 1996, Bag 2 is 1994. Likelihood = 0.14 * 0.10 = 0.014. Unnormalized Posterior = 0.014 * 0.5 = 0.007.
  3. Normalizing Constant = 0.02 + 0.007 = 0.027.
  4. Posterior p(A|D) = 0.02 / 0.027 ≈ 0.74.

Answer 0.74

Common mistakes

  • Failing to ensure the hypothesis suite is mutually exclusive and collectively exhaustive, which breaks the normalization step.
  • Incorrectly multiplying dependent probabilities instead of independent ones for the likelihood calculation.
  • Forgetting to normalize the posteriors, resulting in values that do not sum to 1.
  • Confusing the direction of the hypothesis assignment (e.g., assigning 1994 to Bag 2 in Hypothesis A).

Animation

Animation coming soon

The animated version of this lesson is being prepared for the web. In the Signals AI app, lessons are drawn step by step.

Code

This is a text lesson: it has no animation code yet.

ccBYNC© 2026 SignalsLab. Lesson text shared as is under CC BY-NC 4.0.