Chi-Square Test
The chi-square test is a way to check whether a pattern you see in counts is likely to be “real” or could easily happen by random chance. It’s especially handy when your data comes as categories—like clicks vs. no clicks, device type, or diagnosis groups.
What it is checking
A Chi-Square Test compares what you observed in a table of counts to what you would expect if a simple assumption (the null hypothesis) were true. The test statistic is based on how far each cell’s observed count is from its expected count, scaled by the expected count. Larger mismatches produce a larger chi-square value, which leads to a smaller p-value.
Common versions you’ll see
- Chi-square test of independence: Are two categorical variables related? (e.g., “device type” and “purchase yes/no”).
- Chi-square goodness-of-fit: Does one categorical variable match a claimed distribution? (e.g., ad traffic split 50/50 across two variants).
Typical assumptions include independent observations and sufficiently large expected counts in cells (a common rule of thumb is at least ~5).
Practical example
Suppose you track 1,000 visitors and tabulate purchases by device (mobile/desktop). If purchases are much more common on desktop than expected under “no relationship,” the chi-square test of independence can flag that association.
Why it matters in AI/ML
In ML workflows, chi-square tests often appear in:
- Feature selection for classification with categorical or binned features (e.g.,
sklearn.feature_selection.chi2). - Data validation: detecting distribution shifts in categorical inputs between training and production.
- A/B testing for conversion rates when outcomes are counted events.
Used well, it helps you avoid treating noisy category differences as meaningful signals.
Chi-Square Test is a hypothesis test for categorical data that compares observed counts to expected counts under a null model, using a chi-square statistic and reference distribution. It is used to test independence in contingency tables or goodness-of-fit to a specified distribution. In AI/ML, it supports feature selection and data validation by detecting associations between categorical features and labels. Example: testing whether click-through rate depends on device type.
Imagine you run a small shop and you expect customers to buy coffee and tea in about equal amounts. After a week, you count what people actually bought and compare it to what you expected. A Chi-Square Test does this kind of “expected vs. observed” check for categories.
In statistics and machine learning, the Chi-Square Test helps answer questions like: “Are two categories related?” (for example, whether clicking an ad is related to a user’s device type) or “Does this data match a claimed pattern?” If the difference between expected and observed counts is too large, it suggests the pattern or relationship is real, not just random chance.