You've probably seen this exact setup in a stats textbook. "Suppose that in a random selection of 100 colored candies, 22 are red, 18 are blue, 25 are green, 15 are yellow, and 20 are orange." Then comes the question: *Is this what we'd expect? In real terms, are the colors equally likely? Does the manufacturer's claim hold up?
It's a classic intro to statistical inference for a reason. That's why candy is tangible. Which means the numbers are small enough to grasp. And the logic underneath? That logic powers everything from clinical trials to A/B testing to quality control on a production line Simple, but easy to overlook. Worth knowing..
This changes depending on context. Keep that in mind.
Let's walk through what this problem is actually asking — and how to think about it without getting lost in formulas.
What Is This Problem Really About
At its core, this is a goodness-of-fit question. You have observed counts — what actually happened in your sample. And you have expected counts — what you'd predict if some hypothesis were true. Usually, that hypothesis is "all colors are equally likely" or "the proportions match what the company claims That's the part that actually makes a difference..
The statistical machinery — chi-square test, p-values, degrees of freedom — exists to answer one question: How surprised should we be by the difference between what we saw and what we expected?
That's it. The rest is notation.
The Null Hypothesis in Plain English
The null hypothesis (H₀) is the "nothing interesting is happening" scenario. For candies, it's usually: Each color has the same probability of being picked — 1/5 or 20%.
The alternative (H₁) isn't "the colors are rigged in some specific way." It's just: At least one color's probability differs from 20%.
Notice what the alternative doesn't say. It doesn't tell you which color. Which means it doesn't tell you how they differ. Now, it just says "not all equal. " That's intentional. The test is designed to detect any departure from equality — not a specific one you dreamed up after seeing the data.
Observed vs. Expected: The Numbers That Matter
With 100 candies and 5 colors under equal probability, expected count per color = 100 × 0.20 = 20.
| Color | Observed | Expected |
|---|---|---|
| Red | 22 | 20 |
| Blue | 18 | 20 |
| Green | 25 | 20 |
| Yellow | 15 | 20 |
| Orange | 20 | 20 |
Some are close. Yellow is -5. Is that "real" or just noise? Which means green is +5. That's what the test quantifies But it adds up..
Why It Matters / Why People Care
You might think: It's just candy. Who cares?
But swap "candy colors" for "defect types in a factory," "ad click-through rates by variant," "side effect frequencies in a drug trial," or "voting preferences by district." The structure is identical.
Real-World Stakes
A candy company claims their mix is 24% blue, 20% orange, 16% green, 14% yellow, 13% red, 13% brown. You buy a bag, count 100 pieces, and get different proportions. Do you:
- Assume your bag is a fluke?
- Tweet that the company lies?
- Run a proper test and then decide?
The chi-square goodness-of-fit test gives you a framework for that decision. It doesn't make the decision for you — it gives you a calibrated measure of evidence.
The Cost of Getting It Wrong
Two errors are possible:
- Type I (false alarm): You conclude the mix isn't as claimed, but it actually is. You damage a reputation, trigger an audit, waste money.
- Type II (missed signal): You conclude "looks fine" when the mix really has shifted. Customers notice. Quality slips. Brand erodes.
Statistical testing forces you to acknowledge both risks — and choose your tolerance (α, usually 0.05) before seeing data. That discipline matters.
How It Works: The Chi-Square Goodness-of-Fit Test
The test statistic measures total discrepancy between observed and expected, scaled by expected. Here's the formula:
χ² = Σ (Oᵢ − Eᵢ)² / Eᵢ
Where:
- Oᵢ = observed count for category i
- Eᵢ = expected count for category i
- Sum runs over all categories
Step-by-Step Calculation
Let's compute it for our candy data.
| Color | O | E | O−E | (O−E)² | (O−E)²/E |
|---|---|---|---|---|---|
| Red | 22 | 20 | 2 | 4 | 0.20 |
| Blue | 18 | 20 | -2 | 4 | 0.25 |
| Yellow | 15 | 20 | -5 | 25 | 1.25 |
| Orange | 20 | 20 | 0 | 0 | 0.20 |
| Green | 25 | 20 | 5 | 25 | 1.00 |
| Sum | **2. |
χ² = 2.90
Degrees of Freedom
df = (number of categories) − 1 = 5 − 1 = 4 Worth keeping that in mind. Less friction, more output..
Why minus one? Because the totals constrain you. If you know 4 category counts and the total, the 5th is determined. You lose one "free" piece of information.
The P-Value
Now we ask: If the null is true (equal probabilities), how often would we see a χ² value this large or larger just by chance?
With df = 4, χ² = 2.90 → p ≈ 0.57.
That's huge. In practice, a p-value of 0. 57 means: **If the colors were truly equally likely, you'd see a discrepancy this big (or bigger) 57% of the time purely by random sampling.
There's zero evidence against the null. The observed variation is completely consistent with random noise.
What If the Numbers Were Different?
Suppose instead you got: Red 30, Blue 10, Green 30, Yellow 10, Orange 20 Nothing fancy..
| Color | O | E | (O−E)²/E |
|---|---|---|---|
| Red | 30 | 20 | 5.Think about it: 00 |
| Blue | 10 | 20 | 5. 00 |
| Green | 30 | 20 | 5. |
| Color | O | E | (O−E)²/E |
|---|---|---|---|
| Yellow | 10 | 20 | 5.00 |
| Orange | 20 | 20 | 0.00 |
| Sum | **20. |
χ² = 20.00
With df = 4, a χ² value of 20.This means: If the colors were truly equally likely, you’d see a discrepancy this large (or larger) only 0.Think about it: 0006. 00 corresponds to a p-value of approximately **0.06% of the time by chance alone Not complicated — just consistent..
The evidence against the company’s claim is overwhelming. The observed distribution is statistically incompatible with the stated proportions The details matter here. No workaround needed..
What Should You Do?
In this case, the choice is clear:
- Assume a fluke? Unlikely. The p-value is orders of magnitude too low to dismiss as random variation.
- Tweet that the company lies? Premature. While the data suggests dishonesty, further investigation (e.g., checking production logs, supplier inputs) is needed to confirm systemic issues.
- Run a proper test? Already done. The chi-square test has provided a rigorous, objective basis for action.
The test doesn’t dictate your next step, but it quantifies the strength of the evidence. Here, the evidence is so strong that ignoring it risks reputational damage, legal liability, or customer backlash No workaround needed..
Conclusion
The chi-square goodness-of-fit test is a powerful tool for validating claims about proportions. It transforms subjective suspicion into objective evidence, forcing decisions to be grounded in data rather than intuition. In the first scenario (χ² = 2.90, p ≈ 0.57), the test reassures you that observed variation is normal. In the second (χ² = 20.00, p ≈ 0.0006), it exposes a critical deviation that demands attention.
Statistical testing doesn’t eliminate uncertainty—it manages it. By quantifying risks (Type I and Type II errors) and requiring predefined thresholds (α), it ensures that actions are taken only when the evidence is compelling. And in an era where data is both abundant and easily manipulated, such discipline is not just scientific—it’s ethical. Whether you’re auditing candy bags, clinical trials, or marketing claims, the chi-square test reminds us that the path to truth lies in rigor, not reaction.