Understanding the Chi-Square Distribution: A Beginner’s Guide #DS12
Imagine you own a candy store and want to find out if your customers have specific preferences for certain candy flavors, or if their…
Understanding the Chi-Square Distribution: A Beginner’s Guide #DS12

Imagine you own a candy store and want to find out if your customers have specific preferences for certain candy flavors, or if their choices are random. How can you determine this using data? This is where the Chi-Square Distribution comes into play! It is a statistical tool that allows us to compare the actual data we observe (like candy sales) to what we would expect to see if there were no particular preferences among customers. In this article, we will break down the concept step-by-step using our candy store as an example and include a Python implementations along the way.

Source: secretsanfrancisco.com
Table of Contents
· What’s the Chi-Square Distribution All About? · The Null Hypothesis: Our Starting Point · The Chi-Square Distribution: Making Sense of the Number · Putting It to Work: A Python Example · Real-World Uses: Beyond Candy · What Does It All Mean? · Wrapping Up
What’s the Chi-Square Distribution All About?
The Chi-Square Distribution is a statistical tool that helps us measure how much our real-world data differs from what we would expect if everything were random or “normal”. It’s particularly useful when dealing with categorical data where things that can be counted and grouped, such as “how many people bought chocolate versus gummy bears.”
Imagine this scenario: You expect your candy store to sell equal amounts of chocolate, gummy bears, and lollipops (let’s say, 100 of each). However, when you check your sales, you find that you’ve sold 120 chocolates, 90 gummy bears, and 90 lollipops. Is this difference simply due to random chance, or do people genuinely prefer chocolate? The Chi-Square test helps you answer that question!
The Null Hypothesis: Our Starting Point
Before going into the math behind everything, let’s clarify the concept of the null hypothesis.
While it sounds complex, it’s quite straightforward. According to Google, a hypothesis is,
“a supposition or proposed explanation made on the basis of limited evidence as a starting point for further investigation.”
In essence, it represents our initial belief that “nothing interesting is happening”. In statistics, this serves as our default assumption until the data suggests otherwise.
In the context of our candy store example, the null hypothesis would be: “Customers do not prefer any flavor — they buy chocolate, gummy bears, and lollipops in equal amounts.” If we find evidence to reject this hypothesis, it indicates that there is a preference among customers. A hypothesis is simply an educated guess that we can test with data. The null hypothesis asserts that there is no pattern or relationship, essentially, that candy flavor does not impact sales. The Chi-Square test evaluates whether the actual data contradicts this notion of “no difference.”
Chi-Square Statistic: Measuring the Difference
How do we test our hypothesis? Using something called the Chi-Square statistic, a number that tells us how far our observed data (actual sales) deviates from our expected data (equal sales). Here’s the formula

Source: Investopedia
Where:
- O is the observed value,
- E is the expected value.
- c is the Degrees of Freedom
Let’s calculate it for our candy store:
- Total sales = 120 (Chocolate) + 90 (Gummy Bears) + 90 (Lollipops) = 300 candies.
- Expected sales per flavor = 300 ÷ 3 = 100 each.
Plugging in the numbers:
- Chocolate: (120–100)² ÷ 100 = 400 ÷ 100 = 4
- Gummy bears: (90–100)² ÷ 100 = 100 ÷ 100 = 1
- Lollipops: (90–100)² ÷ 100 = 100 ÷ 100 = 1
- Chi-Square = 4 + 1 + 1 = 6
A larger Chi-Square value suggests a greater difference between what we observed and what we expected. But is 6 significant enough to conclude that customers prefer chocolate? We will find out soon!
The Chi-Square Distribution: Making Sense of the Number
We need something else! The Chi-Square statistic by itself doesn’t provide much insight; it needs context. This is where the Chi-Square Distribution comes in. It serves as a guideline that indicates what Chi-Square values are considered “normal” under the null hypothesis, based on degrees of freedom (df).
Degrees of freedom are determined by the number of categories. In our case, we have: 3 flavors (chocolate, gummy bears, lollipops) — 1 = 2 df.
Why subtract 1? Because knowing the total sales (300) and two categories determines the third.
The Chi-Square Distribution changes shape based on the degrees of freedom. With more categories, it becomes wider. Statisticians use this distribution to establish a cutoff: if your Chi-Square value (like our 6) exceeds the expected threshold for 2 df, it suggests that the null hypothesis may not be valid.

Source: ar.inspiredpencil.com
Putting It to Work: A Python Example
Let’s see the Chi-Square test in action using Python! We’ll utilize a library called scipy to analyze our candy sales data.
from scipy.stats import chisquare
# Observed sales
observed = [120, 90, 90]
# Expected sales (equal distribution)
expected = [100, 100, 100]
# Run the Chi-Square test
chi_stat, p_value = chisquare(observed, expected)
print(f"Chi-Square Statistic: {chi_stat}")
print(f"P-value: {p_value}")
Chi-Square Statistic: 6.0
P-value: 0.04978706836786395
The Chi-Square Statistic is 6, which aligns with our earlier calculations. The p-value indicates the probability of obtaining this result (or one more extreme) if the null hypothesis is true. A p-value below 0.05 (5%) typically suggests that we should reject the null hypothesis.
So, with a p-value of 0.0498, we’d say there’s evidence (just barely!)that customers do not buy candy flavors equally. This may suggest a preference for chocolate.
Real-World Uses: Beyond Candy
The Chi-Square test is applicable in various fields beyond candy sales! Here are some examples:
- Marketing: In an A/B/C test, do three website designs lead to different click rates?
- Health: Do men and women have the same recovery rates from a treatment?
- Science: Does the outcome of a dice roll conform to an expected even distribution, or is the dice biased?
The Chi-Square test is also utilized in goodness-of-fit tests to determine if data aligns with a specific theory — such as checking if your candy sales reflect a predicted pattern from last year.
What Does It All Mean?
- Low Chi-Square: Your data aligns with expectations — there’s nothing surprising (the null hypothesis holds).
- High Chi-Square: Something’s up! The data deviates from expectations, suggesting a potential pattern or relationship.
In our candy store example, a Chi-Square value of 6 and a p-value just under 0.05 indicate that there may be flavor preferences among customers. It’s time to consider stocking up on chocolate!
Wrapping Up
The Chi-Square Distribution is a great statistical tool for data analysis. It helps identify when results are not random, whether regarding candy sales, website clicks, or dice rolls. Start with a null hypothesis, calculate your Chi-Square statistic, and use the distribution to make informed decisions.
What do you think? Are you ready to test your own data?
References - Bruce, P., Bruce, A., & Gedeck, P. (2020). Practical Statistics for Data Scientists: 50+ Essential Concepts Using R and Python. O’Reilly Media.
메타데이터
- post_id
- b4e8a04f40e3
- slug
- understanding-the-chi-square-distribution-a-beginners-guide-b4e8a04f40e3
- url
- https://medium.com/@vishalgimhan/understanding-the-chi-square-distribution-a-beginners-guide-b4e8a04f40e3
- canonical_url
- https://medium.com/@vishalgimhan/understanding-the-chi-square-distribution-a-beginners-guide-b4e8a04f40e3
- author_url
- https://medium.com/@vishalgimhan
- status
- ok
- fetched_at
- 2026-08-30 06:12:24