Bell Statistics

What is a chi-square test?

A chi-square test compares the counts observed in a table against the counts expected if the rows and columns were unrelated. The statistic adds up the squared gaps between them, so a large value means the pattern is further from independence than chance would explain.

Notation
χ²
Also called
chi-squared test, pearson's chi-square, test of independence, goodness-of-fit test
Allon Korem

Written by Allon Korem

Chief Executive Officer

Last updated

In plain English

A chi-square test works on counts arranged in a table — two variants against converted and not converted, or three plans against four support-ticket categories. It computes what each cell would contain if the two variables were completely unrelated, compares that against what actually landed there, and adds up the squared discrepancies scaled by how big each expected count was. A large total means the observed pattern is hard to reconcile with independence.

The expected counts are the part worth understanding, because they are where the null hypothesis lives. If 4.5% of all users converted overall, and one arm received 30% of the traffic, then independence predicts that arm holds 30% of the conversions. The test never asks whether that prediction is sensible; it only asks how far the data strayed from it. Everything the test knows about "no relationship" is encoded in that one multiplication of row total by column total.

For a 2 × 2 table this is the same test as a two-proportion z-test, exactly: χ² equals z², and the p-values match to the last decimal. The reason to reach for chi-square anyway is that it generalises. Four variants against converted-or-not is a 4 × 2 table that a z-test cannot handle, and the chi-square statistic answers "is anything going on across these four" in one number. What it will not tell you is *which* variant differs, and chasing that down means pairwise comparisons and a multiple comparisons correction.

Its most valuable everyday use in experimentation is not measuring an effect at all — it is the sample ratio mismatch check. Assign 50/50 and observe 49.2/50.8 across 200,000 users, and a chi-square goodness-of-fit test against the intended split says whether that gap is ordinary randomness or evidence that something upstream is dropping users non-randomly. It is a cheap test that invalidates an entire experiment when it fires, and it is skipped far more often than it is run.

The condition to respect is on expected counts rather than on how many users you have: every cell should expect at least five, and a table with 100,000 rows can still fail that if one category is rare. Below the threshold the chi-square approximation to the true distribution breaks down and the p-value is not trustworthy — Fisher's exact test computes the answer directly and is the right fallback.

The formula

One sum over every cell, and one rule for the expected counts it is compared against. Degrees of freedom come from the shape of the table rather than from the number of observations.

The statistic
χ² = Σ ( O − E )² / E

O is the observed count in a cell, E the expected one. Dividing by E is what stops large cells from dominating purely because they are large.

Expected count under independence
E_ij = ( row total_i × column total_j ) / grand total

The entire null hypothesis, in one line. Everything the test means by 'no relationship' is this prediction.

Degrees of freedom
df = ( rows − 1 ) · ( columns − 1 )

A 2 × 2 table gives 1, a 4 × 2 gives 3 — see degrees of freedom.

Relationship to the z-test
χ²(1 df) = z²

For a 2 × 2 table these are one test in two notations. 2.63² = 6.92, and both give p = 0.0085.

When it is safe
every expected cell count ≥ 5

About expectation, not sample size. Below it, use Fisher's exact test.

Worked example

An experiment is assigned 50/50 across four weeks. The platform reports 198,340 users in control and 201,660 in the variant — a 49.6/50.4 split. It looks close enough to ignore, and the team wants to know whether it is. This is a goodness-of-fit test against the intended allocation, not a test of the metric.

Observed
control 198,340; variant 201,660
Total
400,000
Expected under 50/50
200,000 each
Deviation
1,660 users, or 0.83%
Statistic
χ² = 1660²/200000 + 1660²/200000 = 27.56
Degrees of freedom
1

p = 0.00000015. A split this uneven would occur by chance roughly once in 6.7 million identically run experiments.

This is the check earning its keep. A 49.6/50.4 split looks like nothing — well under one percent — and at this traffic it is overwhelming evidence that assignment is not doing what it claims. The cause is usually mundane and always serious: a redirect that fails more often on one variant, a bot filter applied after assignment, an event that fires late on a slower page. Whatever it is, users are being dropped non-randomly, the two arms are no longer comparable, and every metric computed from this experiment is suspect regardless of how significant it looks. The right response is to stop reading the results and find the leak. Note how the sample size drives this: the same 0.83% deviation on 4,000 users gives χ² = 0.28 and p = 0.60, which is genuinely unremarkable.

Common misconceptions

A chi-square test needs at least five observations in each cell.
The condition is on EXPECTED counts, not observed ones, and the distinction matters. A cell can legitimately observe zero while expecting twelve — that is exactly the kind of departure the test is built to detect. What breaks the approximation is a small expectation, because the statistic's reference distribution assumes each cell has enough mass to behave smoothly.
A significant chi-square on a multi-variant test tells you which variant won.
It tells you the table as a whole is inconsistent with independence, and nothing about where. Identifying the responsible variant requires pairwise comparisons, and running all of them at 5% reintroduces the multiple comparisons problem the omnibus test was avoiding. Use a correction, or nominate the comparison of interest before the test starts.
A large chi-square statistic means a large effect.
It means a statistically detectable departure from independence, and the statistic grows with sample size for a fixed effect. At 400,000 users a 0.4-percentage-point skew produces χ² = 27.6; the same skew at 4,000 users produces 0.28. For magnitude, look at Cramér's V or the difference in rates itself — the statistic alone cannot distinguish a big effect from a big sample.

Frequently asked questions

What happens if a cell has fewer than five expected observations?
The chi-square approximation stops being reliable and the p-value should not be trusted — typically it comes back smaller than it should, so false positives become more likely than the number suggests. Fisher's exact test is the standard alternative because it enumerates the possible tables directly rather than approximating. For larger tables, combining sparse categories into a sensible grouping is often better than either.
How do I use a chi-square test to check for sample ratio mismatch?
Compare the observed user counts per arm against the counts your intended split predicts, using a goodness-of-fit test with one degree of freedom for two arms. Treat anything below about p = 0.001 as a genuine problem rather than the usual 0.05 — this check runs on every experiment, so a looser threshold produces constant false alarms. A failure means investigating the assignment pipeline, not adjusting the analysis.
Should I apply Yates' continuity correction?
Usually not. It was designed to compensate for approximating discrete counts with a continuous distribution in small 2 × 2 tables, and it is well known to overcorrect — producing p-values that are too conservative and costing real power. At the sample sizes online experiments run at it changes almost nothing anyway. If the table is small enough for the correction to matter, that is the signal to use Fisher's exact test instead.
Can I run a chi-square test on revenue or session duration?
Not directly — it works on counts in categories, and revenue is a measured quantity. Bucketing it into ranges to force a table technically works but throws away most of the information and makes the answer depend on where you drew the bucket boundaries. A t-test on the means, or a Mann-Whitney U test if the distribution is badly skewed, uses the data as it actually is.

Related terms

  • ANOVA

    One test for three or more variants — and the reason it deliberately refuses to say which one won.

  • Mann-Whitney U test

    Compares by rank instead of by mean, so one whale cannot move the result — and answers a subtly different question.

  • P-value

    How surprising your data would be if there were no effect — and the four things it is constantly mistaken for.

  • Paired t-test

    When every observation has a partner, pairing removes the differences between units and can cut the sample you need by an order of magnitude.

  • Z-test

    The test behind every conversion-rate readout — and the one condition that decides whether it is safe to use.

  • Fisher's exact test

    Counts the possible tables instead of approximating them — the right test when a cell expects fewer than five.

  • Log-rank test

    For questions about when rather than whether — and it uses the people who have not converted yet instead of discarding them.

  • McNemar's test

    The paired test for yes-or-no outcomes — and it throws away every subject who did not change their mind.

Calculate it

  • Chi-square test

    Test a contingency table of counts for association — any number of rows and columns.

  • Fisher's exact test

    The right test for a 2×2 table of small counts — exact p-values, no normal approximation.

Knowing the term is the easy part

Applying it to a live measurement problem is the part that goes wrong. If you are designing an experiment, reading a result you do not trust, or trying to work out what your marketing actually caused, that is the work we do.

References