What is Chi-Square Test: When and How to Use It
Sep 04, 2026 4 Min Read 14 Views
(Last Updated)
When working with categorical data, you may want to know whether two variables are related or whether observed frequencies match an expected distribution. The Chi-Square Test is a statistical method designed to answer these questions.
For example, a data scientist might want to determine whether customer preferences differ across age groups or whether observed product categories follow an expected distribution. Instead of comparing numerical averages, the Chi-Square Test works primarily with counts or frequencies.
Table of contents
- TL;DR Summary
- What Is the Chi-Square Test?
- When Should You Use a Chi-Square Test?
- Types of Chi-Square Tests
- How Does the Chi-Square Test Work?
- Observed vs Expected Frequencies
- Chi-Square Test of Independence Example
- What Is the P-Value?
- Degrees of Freedom
- Assumptions of the Chi-Square Test
- Chi-Square Test in Python
- Key Takeaways
- Conclusion
- FAQs
- What is the Chi-Square Test used for?
- What are the two main types of Chi-Square Tests?
- What does a small p-value mean in a Chi-Square Test?
- Can the Chi-Square Test prove causation?
- What should I do if expected frequencies are too small?
TL;DR Summary
- The Chi-Square Test is used primarily with categorical data.
- It compares observed frequencies with expected frequencies.
- The two common forms are the Chi-Square Goodness-of-Fit Test and the Chi-Square Test of Independence.
- A small p-value can provide evidence against the null hypothesis.
- Expected frequencies and observations should satisfy the test’s assumptions.
What Is the Chi-Square Test?
The Chi-Square Test determines whether there is a statistically significant difference between observed and expected frequencies.
The basic idea is simple:
Observed frequencies → Expected frequencies → Compare the difference
If observed and expected counts are very different, the resulting Chi-Square statistic becomes larger.
The general formula is:
χ² = Σ (O − E)² / E
Where:
- O = observed frequency
- E = expected frequency
genui{“learning_viz”:{“type_id”:”CHI_SQUARE_GOODNESS_OF_FIT”}}
Read More: Feature Selection Techniques in Machine Learning
Strengthen your statistical and AI skills with HCL GUVI’s Artificial Intelligence & Machine Learning Course. Learn statistics, data analysis, and machine learning through practical projects.
When Should You Use a Chi-Square Test?
The Chi-Square Test is appropriate when your variables are categorical and your data can be represented as frequencies or counts.
Common applications include:
- Testing relationships between categorical variables.
- Comparing observed and expected category frequencies.
- Analyzing survey responses.
- Studying customer preferences.
- Examining demographic patterns.
- Testing whether categories are distributed as expected.
For example, a company could test whether product preference is associated with customer age group.
The Chi-Square Test was developed from work by Karl Pearson and is one of the most widely used methods for analyzing categorical data.
Types of Chi-Square Tests
There are two major forms of the test.
- Chi-Square Goodness-of-Fit Test
This test determines whether observed frequencies match an expected distribution.
For example, suppose a company expects four product categories to receive equal numbers of orders. After collecting data, it can test whether the observed order counts significantly differ from that expectation.
- Chi-Square Test of Independence
This test determines whether two categorical variables are associated.
For example:
Age Group × Product Preference
The test can determine whether product preference and age group appear to be related.
Pro Tip: Choose the test based on your research question. If you have one categorical variable and an expected distribution, consider goodness-of-fit. If you are examining the relationship between two categorical variables, consider the test of independence.
How Does the Chi-Square Test Work?
The test follows a basic sequence:
- Define the null and alternative hypotheses.
- Organize the categorical observations.
- Calculate expected frequencies.
- Calculate the Chi-Square statistic.
- Determine the degrees of freedom.
- Calculate or obtain the p-value.
- Interpret the result.
The null hypothesis generally states that there is no significant difference or association.
The alternative hypothesis states that a significant difference or association exists.
Observed vs Expected Frequencies
Understanding observed and expected frequencies is central to the test.
Suppose a survey records preferences for four products:
| Product | Observed | Expected |
| A | 30 | 25 |
| B | 20 | 25 |
| C | 28 | 25 |
| D | 22 | 25 |
The observed values represent what was actually recorded.
The expected values represent what would be expected if the null hypothesis were true.
The Chi-Square statistic summarizes the differences between these values.
Best Practice: Calculate expected frequencies based on the null hypothesis rather than using the observed frequencies as a substitute.
Chi-Square Test of Independence Example
Imagine a company surveys 200 customers to determine whether preferred payment method is associated with age group.
The data could be organized into a contingency table:
| Age Group | Card | UPI | Cash |
| 18–30 | 50 | 30 | 10 |
| 31–50 | 35 | 25 | 15 |
| 51+ | 15 | 10 | 10 |
The Chi-Square Test of Independence can evaluate whether payment preference and age group are statistically associated.
If the p-value is below the selected significance level, the analyst may reject the null hypothesis of independence.
However, statistical association does not automatically establish causation.
What Is the P-Value?
The p-value helps determine how compatible the observed data is with the null hypothesis.
A commonly used significance level is 0.05.
If:
p < 0.05
the result is commonly described as statistically significant, and the null hypothesis is rejected.
If:
p ≥ 0.05
there is insufficient evidence to reject the null hypothesis.
Warning: A p-value above 0.05 does not prove that there is no relationship. It means the data does not provide sufficient evidence to reject the null hypothesis under the chosen test and assumptions.
Degrees of Freedom
Degrees of freedom depend on the type of Chi-Square Test.
For a goodness-of-fit test:
df = k − 1
where k is the number of categories.
For a test of independence using a contingency table:
df = (r − 1)(c − 1)
where:
- r = number of rows
- c = number of columns
Degrees of freedom are used when determining the statistical significance of the calculated Chi-Square statistic.
Assumptions of the Chi-Square Test
Before applying the test, check whether its assumptions are appropriate.
- Categorical Data
The variables should generally represent categories rather than continuous numerical measurements.
- Independent Observations
Each observation should generally be independent of the others.
- Adequate Expected Frequencies
Expected cell counts should be sufficiently large for the approximation used by the test to be reliable.
If expected counts are too small, alternatives such as Fisher’s exact test may be more appropriate in some situations.
- Appropriate Sampling
The data should come from a sampling process appropriate for the research question.
Best Practice: Check expected cell counts before interpreting a Chi-Square result, particularly when working with small datasets or tables containing many categories.
Chi-Square Test in Python
Python’s SciPy library provides functions for performing Chi-Square tests.
For a goodness-of-fit test:
from scipy.stats import chisquare
observed = [30, 20, 28, 22]
expected = [25, 25, 25, 25]
result = chisquare(
observed,
f_exp=expected
)
print(result.statistic)
print(result.pvalue)
For a test of independence, you can use chi2_contingency():
from scipy.stats import chi2_contingency
table = [
[50, 30, 10],
[35, 25, 15],
[15, 10, 10]
]
chi2, p, dof, expected = chi2_contingency(table)
print("Chi-Square:", chi2)
print("p-value:", p)
print("Degrees of freedom:", dof)
This makes the test easy to incorporate into a data science workflow.
Key Takeaways
- The Chi-Square Test is primarily used for categorical frequency data.
- Goodness-of-fit tests compare observed and expected frequencies.
- Tests of independence examine relationships between categorical variables.
- The Chi-Square statistic increases as observed and expected frequencies differ.
- P-values help assess evidence against the null hypothesis.
- Expected frequencies and independence assumptions should be checked.
- Statistical association does not necessarily imply causation.
Strengthen your statistical and AI skills with HCL GUVI’s Artificial Intelligence & Machine Learning Course. Learn statistics, data analysis, and machine learning through practical projects.
Conclusion
The Chi-Square Test is a practical statistical technique for analyzing categorical data. It can determine whether observed frequencies differ from an expected distribution or whether two categorical variables appear to be associated.
For data scientists, understanding when the test is appropriate is just as important as knowing how to calculate it. By checking assumptions, interpreting p-values carefully, and considering practical significance alongside statistical significance, you can use Chi-Square analysis to draw more reliable conclusions from categorical datasets.
FAQs
What is the Chi-Square Test used for?
The Chi-Square Test is used primarily to analyze categorical frequency data and determine whether observed frequencies differ from expected frequencies or whether categorical variables are associated.
What are the two main types of Chi-Square Tests?
The two common types are the Chi-Square Goodness-of-Fit Test and the Chi-Square Test of Independence.
What does a small p-value mean in a Chi-Square Test?
A small p-value indicates that the observed data would be relatively unlikely under the null hypothesis, providing evidence against it.
Can the Chi-Square Test prove causation?
No. A statistically significant association between variables does not establish a causal relationship.
What should I do if expected frequencies are too small?
Depending on the table and research design, an alternative such as Fisher’s exact test may be more appropriate.



Did you enjoy this article?