Chi-Square Calculator
Run a chi-square goodness-of-fit test to check whether observed category counts differ significantly from what you'd expect. Enter your data below, and see the formula, a worked example, and ready-to-use Python code underneath.
Enter your data
One number per category, separated by commas, spaces, or new lines — e.g. 18, 22, 20, 15, 25, 20
Please check your inputs — observed and expected lists must be the same length, and expected values must be greater than 0.
What the chi-square test tells you
The chi-square goodness-of-fit test compares observed category counts against the counts you'd expect under some assumption — often that all categories are equally likely — and tests whether the difference is bigger than you'd expect from random variation alone. It's commonly used to check whether a die or coin is fair, whether survey responses match an expected split, or whether observed data follows a claimed distribution.
A large chi-square statistic (and a correspondingly small p-value) suggests the observed counts don't fit the expected pattern well; a small chi-square statistic suggests they're consistent with it.
Chi-square formula
Python code
You can compute the statistic manually, or with SciPy's chisquare function:
# Method 1 — manual calculation observed = [18, 22, 20, 15, 25, 20] n_categories = len(observed) expected = [sum(observed) / n_categories] * n_categories chi_square = sum((o - e) ** 2 / e for o, e in zip(observed, expected)) df = n_categories - 1 print(chi_square, df) # 2.9 5 # Method 2 — SciPy from scipy.stats import chisquare chi_square, p_value = chisquare(observed) # assumes equal expected frequencies # chisquare(observed, f_exp=[...]) for custom expected frequencies
Worked example
A die is rolled 120 times, landing on each face this many times: 18, 22, 20, 15, 25, and 20. If the die is fair, each face should come up 20 times (120 / 6). Is the die significantly unfair, at α = 0.05?
| Face | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|
| Observed | 18 | 22 | 20 | 15 | 25 | 20 |
| Expected | 20 | 20 | 20 | 20 | 20 | 20 |
- 1Compute (O − E)² / E for each category.
(18−20)²/20 + (22−20)²/20 + (20−20)²/20 + (15−20)²/20 + (25−20)²/20 + (20−20)²/20 - 2Sum them up.
0.2 + 0.2 + 0 + 1.25 + 1.25 + 0 = 2.9 - 3Find the p-value with df = 6 − 1 = 5.
p = 0.7154 - 4Compare to α.
0.7154 > 0.05, so we fail to reject the null hypothesis.
There's no statistically significant evidence, at α = 0.05, that this die is unfair — the observed counts are consistent with random variation around a fair die.
Frequently asked questions
What is a chi-square goodness-of-fit test used for?
It tests whether observed category counts differ significantly from a set of expected counts, such as checking whether a die is fair, whether survey responses match an expected distribution, or whether observed proportions match a theoretical model.
What if I don't have specific expected values?
If you don't have a theoretical model in mind, the standard default is to assume all categories are equally likely, so each expected frequency is simply the total count divided by the number of categories. This calculator uses that default unless you provide your own expected values.
Why does the chi-square statistic only use squared differences?
Squaring the difference between each observed and expected count prevents positive and negative differences from cancelling out, and dividing by the expected count scales each category's contribution relative to how many observations it was expected to have.
How many degrees of freedom does a goodness-of-fit test have?
Degrees of freedom equal the number of categories minus 1. One degree of freedom is lost because, once you know the total count and all but one category's frequency, the last category's frequency is determined.
Can the chi-square statistic be negative?
No. It's a sum of squared, non-negative terms, so it's always zero or positive. A value of zero would mean every observed count exactly matched its expected count.