Chi-Square Tests
Statistics & ProbabilityWhere this fitsContinues from P-values and T-tests. Z-tests and t-tests work on numerical data. The chi-square test works on categorical data. It is a non-parametric test (it makes no assumption about the data’s distribution) and is very common in feature selection.
The chi-square distribution
A distribution built from the standard normal.
- If you take a standard normal variable (mean 0, sd 1) and square it, you get a chi-square distribution with 1 degree of freedom.
- Sum independent squared standard normals and you get a chi-square distribution with degrees of freedom:
Key properties:
- It is a continuous probability distribution.
- Values are never negative (they are sums of squares), starting at 0.
- It is right-skewed for small degrees of freedom, and approaches the normal shape as degrees of freedom grow.
- Its mean equals its degrees of freedom .

The test statistic we compute in a chi-square test follows this distribution, which is why the test is named after it.
Two chi-square tests
Both are based on the chi-square distribution but answer different questions.
| Test | Needs | Question |
|---|---|---|
| Goodness of fit | one categorical column | Does the observed distribution match an expected/theoretical distribution? |
| Test for independence | two categorical columns | Are the two categorical variables related, or independent? |
The common formula for both:
where is the observed frequency and is the expected frequency.
Test 1: goodness of fit
Checks whether one categorical column’s observed counts match some theoretical distribution (uniform, binomial, etc.).
Steps
- State (data follows the theoretical distribution) and (it does not).
- Compute the expected count for each category under .
- Compute the test statistic .
- Degrees of freedom .
- Get the p-value from the chi-square distribution and compare to .
Is a die fair? (uniform)Roll a die 60 times. If fair, each face is expected 10 times. Suppose observed counts are 12, 8, 11, 9, 5, 15.
- : outcomes are uniform (die is fair). : not uniform.
- some value.
- Degrees of freedom . If the p-value < 0.05, reject (the die is not fair).
Boys/girls per family (binomial)Out of 800 families with 4 children, count how many have 0, 1, 2, 3, 4 boys. Under , the number of boys follows a binomial distribution with , so expected counts are , giving 50, 200, 300, 200, 50. Compare observed vs these expected counts with the chi-square statistic (degrees of freedom ).
NoteThe theoretical distribution can be uniform, binomial, Poisson, etc. You compute the expected counts from whatever distribution the null hypothesis assumes.
Test 2: test for independence
Checks whether two categorical variables are related or independent.
Steps
- Build a contingency table of observed counts (rows = categories of one variable, columns = categories of the other).
- State (the two variables are independent) and (they are associated).
- Compute an expected count for each cell, assuming independence.
- Compute over all cells.
- Degrees of freedom .
- Get the p-value and compare to .
The key step: expected counts
Under independence, the probability of landing in a cell is the product of its row and column probabilities (just like independent events multiply). Multiplying by the total gives the expected count:
Education vs exercise preferenceA contingency table of education level (high school, bachelors, PhD) vs preferred exercise (yoga, running, swimming). For the “high school & yoga” cell: Do this for every cell, compute , use degrees of freedom , and read the p-value.
Titanic (Pclass vs Survived)Cross-tabulate
Pclass(1, 2, 3) againstSurvived(0, 1). Usingscipy.stats.chi2_contingency(table), the observed and expected counts differ a lot, giving a p-value ≈ 0. So we reject independence: survival is associated with passenger class. This is exactly whyPclassis a valuable feature for predicting survival.
Assumptions for the independence testObservations should be independent, and every expected cell count should be greater than 5 for the chi-square approximation to hold.
Sample vs population caution
WarningA chi-square test on a sample infers about the population. Even if a table “obviously” shows a pattern (e.g. more Pclass-3 passengers), the formal test is what lets you generalize from your limited sample to the whole population. Eyeballing the sample is not proof.
Where chi-square is used in ML
| Use case | How |
|---|---|
| Feature selection | rank categorical features by their association with the target; drop irrelevant ones (a filter method) |
| Evaluating classifiers | compare observed vs expected class frequencies in a confusion matrix |
| Analyzing relationships | test associations between categorical features in EDA |
| Discretizing continuous variables | choose good bin boundaries |
| Decision trees | some algorithms use chi-square to pick the best split |
Summary
- The chi-square distribution is a sum of squared standard normals; it is non-negative, right-skewed, and its mean equals its degrees of freedom.
- Both chi-square tests use on categorical data.
- Goodness of fit (one column): does the observed distribution match a theoretical one? df .
- Test for independence (two columns): are the variables related? Build a contingency table, compute expected counts , and use df .
- Small p-value → reject (not a good fit / variables are associated).
- Chi-square is widely used for feature selection in machine learning.