Chi-Square Test: Formula, Types, Examples, and Applications
TL;DR: The chi-square (χ²) test is a non-parametric method for analyzing categorical data. It answers two fundamental questions: Does this sample fit an expected distribution? And are these two variables related?

18 min read  |  For analysts, students, and researchers

This guide covers all three chi-square test types, their assumptions, and what to do when those assumptions are violated; full worked examples with Python code; Cramer’s V for effect size; and a decision table for choosing between chi-square and its alternatives.

What Is the Chi-Square Test?

The chi-square test is a non-parametric statistical test used exclusively with categorical data. It measures how much the observed frequencies differ from the frequencies you would expect if the null hypothesis were true. Because it does not assume the shape of the underlying population distribution, it is one of the most widely applicable tests in statistics.

The test statistic is calculated as the sum of squared, normalized differences between observed and expected counts across all categories:

Chi-Square Test Formula χ² = Σ (O − E)² / E

  • χ² = chi-square statistic
  • Σ = sum over all cells
  • O = observed frequency
  • E = expected frequency

Key Property: Chi-square measures discrepancy, not direction or strength of association. A value near zero indicates that the observed and expected frequencies are close. A large value signals a notable divergence. The raw statistic alone does not tell you which categories drive the difference; for that, examine the standardized residuals (see FAQ #5).

How the Chi-Square Test Works

Imagine you roll a standard die 60 times. If the die is fair, you’d expect each face to appear about 10 times. But you observe face-1 appearing 18 times and face-6 appearing only 4 times. Is the die biased, or is this normal random variation?

Chi-square quantifies exactly this question. You compute how far each observed count is from its expected count, square those differences (to make them positive and penalize large gaps more heavily), divide each by the expected count (to scale for the size of the category), and sum everything up. The bigger the result, the less likely the pattern arose by chance.

Key mental Model: Think of χ² as a “total surprise score.” If you observed exactly what you expected in every category, χ² = 0. Every discrepancy adds to the score. You then ask, "How surprised should I be?" The chi-square distribution tells you.

Data Analyst CourseExplore Program
Want to Become a Data Analyst? Learn From Experts!

Chi-Square Test vs T-Test vs ANOVA

This is one of the most searched comparisons in introductory statistics. The three tests address fundamentally different questions and apply to different data types. Choosing the wrong one is a common and consequential error.

Comparison Factor

Chi-Square Test

T-Test

ANOVA

Purpose

Test association or fit for categorical data

Compare means between 2 groups

Compare means across 3+ groups

Data type required

Categorical (nominal or ordinal)

Continuous (interval or ratio)

Continuous (interval or ratio)

Number of groups

2+ categories

Exactly 2 groups

3 or more groups

Outcome variable

Counts/frequencies

Means

Means

Parametric?

No (non-parametric)

Yes (assumes normality)

Yes (assumes normality)

Key assumption

Expected counts ≥5 per cell

Normality, equal variance

Normality, equal variance, independence

Effect size measure

Cramér’s V

Cohen’s d

Eta-squared (η²)

Null hypothesis

Variables are independent

Group means are equal

All group means are equal

Example use case

Is brand preference related to gender?

Do men and women differ in average salary?

Do 3 diets produce different average weight loss?

Which Statistical Test Should I Use? [Decision Flowchart]

Work through these five decision questions in order. Stop at the first row that produces your answer.

Chi Square Test Decision FlowChart

Quick Reference: Special Cases

  • Paired/repeated categorical data → McNemar’s Test
  • 2×2 table with small N (any expected count < 5) → Fisher’s Exact Test
  • 2×2 table with moderate N → Chi-square with Yates’ continuity correction
  • Ordinal outcome where rank order matters → Mann-Whitney U (2 groups) or Kruskal-Wallis (3+)
  • Continuous outcome, 2 groups → Independent samples t-test
  • Continuous outcome, 3+ groups → One-way ANOVA

Types of Chi-Square Tests

1. Goodness-of-Fit

  • One variable, one sample
  • Tests if the observed distribution matches a theoretical one
  • df = k − 1  (k = number of categories)

Example: Does the distribution of blood types in our sample match known population proportions?

2. Test of Independence

  • Two variables, one sample
  • Tests if the two variables are statistically independent
  • df = (rows − 1) × (columns − 1)

Example: Is political affiliation independent of education level, in a single survey sample?

3. Test of Homogeneity

  • One variable, multiple independent samples
  • Tests if different groups share the same distribution
  • df = (rows − 1) × (columns − 1) — same formula as independence

Example: Do patients in three separate hospitals show the same distribution of recovery outcomes?

Independence vs. Homogeneity: The Critical Distinction

These two tests use identical calculations but differ in their study designs and interpretations. In a test of independence, you draw one sample and measure two variables on each subject. In a test of homogeneity, you draw separate samples from different populations and measure a single variable in each.

Example

  • Independence: Survey 500 voters and record both age group and party preference. Are age and party preference related?
  • Homogeneity: Survey 200 voters in each of three cities and record party preference. Do voters across these cities have the same distribution of preferences?

McNemar’s Test: the Fourth Type

When subjects are measured twice (matched pairs, before/after a treatment), the standard chi-square test is inappropriate because the observations are not independent. McNemar’s test uses only the off-diagonal cells of a 2×2 table and has the form:

McNemar’s Test Representation χ² = (b − c)² / (b + c)

  • b, c = discordant cell counts
  • df = 1 always

Assumptions and What to Do When They Break

Key Assumptions

What to do when this breaks

  • 2×2 tables: Switch to Fisher’s Exact Test, or apply Yates’ continuity correction
  • Larger tables: Collapse sparse categories into an ‘Other’ group, collect more data, or use an exact chi-square test
  • Rule of thumb: No more than 20% of cells should have expected counts below 5, and none should be below 1

Data Analyst CourseExplore Program
Your Data Analytics Career is Around The Corner!

Step-by-Step Manual Calculation

Before applying the formula, let’s build the intuition by working through a complete calculation by hand. This is the best way to understand what the chi-square statistic actually measures.

Scenario: A university surveys 100 students and asks their degree type (Science vs Arts) and whether they prefer group or solo study. Is study preference independent of degree type?

Step 1: Record the Observed Contingency Table

Record the Observed Contingency Table

Step 2: Calculate Expected Frequencies

For each cell: E = (Row Total × Column Total) ÷ Grand Total

Calculate Expected Frequencies

Check: All expected counts are 22.5; above 5. The chi-square approximation is valid.

Step 3: Apply the Formula to Each Cell

For each cell, compute: (O − E)² ÷ E

Apply the Formula to Each Cell

Step 4: Calculate Degrees of Freedom

df = (rows − 1) × (columns − 1) = (2 − 1) × (2 − 1) = 1

Step 5: Look up the Critical Value and Interpret

At α = 0.05 with df = 1, the critical value is 3.841.

Result: Since χ² = 9.091 > 3.841, we reject H₀ (p ≈ 0.003).

Study preference is not independent of degree type. Science students prefer group study; Arts students prefer solo study.

Cramér’s V = √(9.091 / 100) = 0.301 — a large effect.

Enroll in the Data Analyst Course to learn over 17+ data analysis tools and skills, and gain real-world insights through live online classes led by industry experts.

Step-by-Step Calculation With Examples

Every chi-square test follows the same five-step process regardless of type:

  • State H₀ and H₁ before you touch the data
  • Choose significance level α (typically 0.05)
  • Calculate expected frequencies for each cell
  • Compute χ² and degrees of freedom
  • Interpret: compare p-value to α, then report Cramér’s V

Example 1: Goodness-of-Fit (Blood Type Distribution)

Scenario: A hospital pathologist samples 200 patients. Known population proportions are O: 44%, A: 42%, B: 10%, AB: 4%. Does the sample match these proportions?

  • H₀: The sample distribution matches the population proportions.
  • H₁: The sample distribution differs from the population proportions.
  • α = 0.05

Goodness of Fit

df = k − 1 = 4 − 1 = 3. Critical value at α = 0.05, df = 3: 7.815.

Result: Since χ² = 3.027 < 7.815, we fail to reject H₀ (p ≈ 0.387). The hospital sample is consistent with known population blood type proportions.

Example 2: Test of Independence (Gender and Product Preference)

Scenario: A retailer surveys 200 customers and records gender and product preference (A, B, or C). Is product preference independent of gender?

  • H₀: Gender and product preference are independent
  • H₁: There is an association between gender and product preference

Test of Independence

Expected counts: E = (row total × col total) / grand total

Test of Independence

df = (2−1)(3−1) = 2. Critical value at α = 0.05, df = 2: 5.991.

Result: Since χ² = 8.808 > 5.991, we reject H₀ (p ≈ 0.012). There is a statistically significant association between gender and product preference.

Example 3: Marketing A/B Test (3×3 Table)

Scenario: An e-commerce team tests three email subject line variants across three customer segments. They record clicks, ignores, and unsubscribes.

Marketing A/B Test

With N = 1050 and 9 cells, minimum expected count = (350×70)/1050 = 23.3 — well above 5. Applying the formula yields χ²(4) = 11.43, p = 0.022.

Actionable Interpretation: We reject H₀: click behavior differs across subject line variants. Variant B has the highest click rate (41.4% vs 34.3% and 25.7%). Cramér’s V = 0.074 indicates a small-to-medium effect — the lift is real but modest.

Effect Size: Cramér’s V

A significant p-value indicates that the association is unlikely to be due to chance. It does not tell you whether it matters in practice. With large samples (N > 1000), even trivially small differences produce significant p-values. Cramér’s V corrects for sample size and table dimensions to give a standardized effect size between 0 and 1:

Cramér’s V Representation: V = √ ( χ² / N · (min(r,c) − 1) )

  • N = total sample size
  • r = number of rows
  • c = number of columns

Cramer's V Representation

Worked example: From Example 2 (χ² = 8.808, N = 200, 2 rows, 3 cols):

V = √(8.808 / 200 × (2−1)) = √0.04404 = 0.210

  • Full interpretation: "χ²(2) = 8.808, p = 0.012. We reject H₀ — gender and product preference are not independent. The association is moderate (Cramér’s V = 0.21)."

Large Sample Warning: With N = 10,000, even V = 0.03 will produce p < 0.05. Always report both p-value and Cramér’s V. A significant but tiny effect should rarely drive business decisions.

Chi-Square Distribution Table and Critical Values

The chi-square distribution is the reference distribution used to convert a χ² statistic into a p-value. It is right-skewed (long right tail) and defined entirely by its degrees of freedom. As df increases, the distribution shifts right and becomes more symmetric.

Chi Square Distribution Table

How to calculate degrees of freedom:

  • Goodness-of-fit: df = k − 1  (k = number of categories)
  • Independence / Homogeneity: df = (rows − 1) × (columns − 1)
  • McNemar: df = 1  (always)

Chi-Square Test in Python (With Code Examples)

Python: Test of Independence (scipy)

import numpy as np
from scipy.stats import chi2_contingency
# Observed contingency table (rows = Gender, cols = Product A/B/C)
observed = np.array([
[45, 30, 25],  # Male
[25, 40, 35]   # Female
])
# Returns: statistic, p-value, df, expected frequencies
chi2, p, dof, expected = chi2_contingency(observed)
# Cramer's V
n = observed.sum()
min_dim = min(observed.shape) - 1
cramers_v = np.sqrt(chi2 / (n * min_dim))
print(f'Chi-square: {chi2:.3f}')
print(f'p-value:    {p:.4f}')
print(f'df:         {dof}')
print(f"Cramer's V: {cramers_v:.3f}")
# Check assumption: expected counts >= 5
if (expected < 5).any():
print('Warning: some expected counts < 5. Consider Fisher Exact Test.')
else:
print('All expected counts >= 5. Approximation is valid.')

Output:

Chi-square: 8.808

p-value: 0.0122

df: 2

Cramer's V: 0.210

All expected counts >= 5. Approximation is valid.

Python: Goodness-of-Fit (scipy)

from scipy.stats import chisquare
observed = [96, 84, 14, 6]
expected_props = [0.44, 0.42, 0.10, 0.04]
n = sum(observed)
expected = [p * n for p in expected_props]
chi2, p = chisquare(f_obs=observed, f_exp=expected)
print(f'chi2 = {chi2:.3f},  p = {p:.4f}')
# chi2 = 3.027,  p = 0.3870 -> Fail to reject H0

Python: 2×2 Table with Yates’ Correction

# Use correction=True (default) for 2x2 tables with moderate N
observed_2x2 = np.array([[8, 2], [1, 9]])
chi2_y, p_y, dof, exp = chi2_contingency(observed_2x2, correction=True)
chi2_n, p_n, _, _  = chi2_contingency(observed_2x2, correction=False)
print(f'With Yates correction:    chi2={chi2_y:.3f}, p={p_y:.4f}')
print(f'Without Yates correction: chi2={chi2_n:.3f}, p={p_n:.4f}')

Real-World Applications

1. Market Research: A/B Testing

Chi-square is the standard test for email and ad campaign A/B tests where the outcome is categorical (clicked/ignored/unsubscribed). It tells you whether engagement differences between variants are statistically significant, so you can confidently roll out the best performer.

2. Medical Research: Treatment Outcomes

A clinical trial recruits 300 patients, randomizing 150 to a new antibiotic and 150 to standard treatment. After two weeks, patients are classified as recovered, improved, or no change. Chi-square tests whether recovery outcomes are independent of treatment group.

3. Social Science: Education and Voting Behavior

A political scientist surveys 800 registered voters and cross-tabulates education level (high school, bachelor’s, postgraduate) against whether they voted in the last election. The chi-square test assesses whether turnout is independent of education level.

4. Quality Control: Defect Analysis

A manufacturer runs three production lines and classifies each unit as a pass, a minor defect, or a major defect. A chi-square test of homogeneity tests whether the defect distribution is the same across all three lines. A significant result indicates that defect rates differ across lines, triggering a root cause investigation.

5. Genetics: Mendel’s Pea Experiments

The goodness-of-fit test was essentially invented for this purpose. When Gregor Mendel crossed pea plants, Mendelian genetics predicted a 3:1 ratio of dominant to recessive traits. Observed counts were compared to this expected ratio. If the chi-square statistic is small, the data are consistent with Mendelian inheritance.

Data Analyst CourseExplore Program
Join The Ranks of Top-Notch Data Analysts!

Chi-Square for Data Analysts and Business Professionals

Chi-square is not just an academic tool. Here are four common professional contexts where it directly applies, including the exact question it answers and how to frame the result for a business audience.

1. Product Manager/Growth Analyst

Scenario: You run an A/B test on two checkout page designs. You record how many users in each group completed a purchase vs. abandoned the cart.

Question: Is conversion behavior independent of the design variant, or did one design genuinely outperform the other?

Answer: Run a 2×2 chi-square test of independence. Report: χ²(1) = X.X, p = 0.0X. If p < 0.05, present as: ‘Design B produced a statistically significant lift in conversion (Cramér’s V = 0.12, small-to-medium effect).’

2. HR/People Analytics

Scenario: Your company has three office locations. You want to know if voluntary turnover rates (stayed vs left) are the same across all three locations.

Question: Is turnover distribution homogeneous across offices, or does one location have a genuinely different retention problem?

Answer: Run a chi-square test of homogeneity (2 rows × 3 columns). A significant result means at least one location differs. Follow up with standardized residuals to identify which one.

3. Data Analyst/Marketing

Scenario: You have survey data on customer segment (New, Returning, VIP) and NPS category (Promoter, Passive, Detractor). You want to know if satisfaction differs by segment.

Question: Is the NPS category independent of the customer segment?

Answer: Run a 3×3 chi-square test of independence. If significant, use standardized residuals to identify which segment–NPS combination is over- or under-represented.

4. Business Intelligence/Operations

Scenario: A manufacturer receives components from four suppliers and classifies each batch as conforming or non-conforming. You want to flag underperforming suppliers.

Question: Is the non-conformance rate independent of the supplier?

Answer: Run a 2×4 chi-square test of homogeneity. A significant result means that at least one supplier’s defect rate differs from the others. Standardized residuals pinpoint which supplier to escalate.

Ready to turn data into career growth? This Data Analyst roadmap outlines the skills, tools, salary trends, and advancement opportunities to help you become a successful Data Analyst.

Limitations of the Chi-Square Test and Fixes

Here are the common problems with chi-square tests and the steps to fix them.

Limitations of the Chi Square Test

FAQs

1. What is the difference between the test of independence and the test of homogeneity?

They use the same formula and produce identical calculations, but their study designs and conclusions differ.

  • The test of independence uses a single sample and asks whether two measured variables are associated
  • The test of homogeneity uses multiple separate samples and asks whether those groups share the same distribution on a single variable

2. What does a chi-square value of 0 mean?

A χ² of exactly 0 means the observed frequencies are identical to the expected frequencies in every single cell. In practice, this rarely happens with real data. It means that the data perfectly match the null hypothesis.

3. Can chi-square be used for continuous data?

It cannot be used directly. You can bin continuous variables into categories first, but this loses information, and the results depend on your choice of cut-points. If your outcome is continuous, use a t-test (two groups) or ANOVA (three or more groups).

4. How do degrees of freedom affect the chi-square test?

Degrees of freedom determine the shape of the chi-square distribution used to compute your p-value. A higher df shifts the critical value to the right — meaning you need a larger χ² to achieve the same significance level. With df = 1, you need χ² > 3.841 for p < 0.05; with df = 10, you need χ² > 18.307.

5. What are standardized residuals, and how do I interpret them?

Standardized residuals are cell-level diagnostics calculated as (O − E) / √E. After a significant chi-square result, they tell you which specific cells contribute most to the overall discrepancy. A value above +2 indicates far more observations than expected; below −2 indicates far fewer. In R, access them via chisq.test()$stdres.

About the Author

Avijeet BiswalAvijeet Biswal

Avijeet is a Senior Research Analyst at Simplilearn. Passionate about Data Analytics, Machine Learning, and Deep Learning, Avijeet is also interested in politics, cricket, and football.

View More
  • Acknowledgement
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, OPM3 and the PMI ATP seal are the registered marks of the Project Management Institute, Inc.
  • *All trademarks are the property of their respective owners and their inclusion does not imply endorsement or affiliation.
  • Career Impact Results vary based on experience and numerous factors.