induwara.lk
induwara.lkEducation · Statistics

Chi-Square Test Calculator — Goodness of Fit & Independence

Paste category counts or a contingency table and get the χ² statistic, degrees of freedom, an exact p-value, the critical value, the full expected-count matrix and a plain-English verdict — with every step of the working shown. Runs entirely in your browser.

By Induwara AshinsanaUpdated Jun 12, 2026
Chi-square test
NIST formulas · cross-checked
Contingency table (2 × 2)
Rows
Cols
Col 1Col 2
Row 1
Row 2

Enter whole-number counts (not percentages). Each row is one category of the first variable; each column one category of the second.

Try an example

Any value from 0.0001 to 0.5. 0.05 is the usual default.

χ²(1, N=100) = 16.67, p < 0.0001 — reject H₀ at α = 0.05. The two variables are associated (not independent).
χ² statistic
16.6667
Degrees of freedom
1
p-value
< 0.0001
≤ α (0.05)
Critical value (α=0.05)
3.8415
95% confidence

Observed

C1C2Σ
R1301040
R2204060
Σ5050100

Expected

C1C2
R12020
R23030

Cell contributions to χ²

CellObservedExpected(O−E)²/E% of χ²
R1·C13020530.0%
R1·C21020530.0%
R2·C120303.333320.0%
R2·C240303.333320.0%

Show working

  1. Eᵢⱼ = (rowᵢ total × colⱼ total) / N, N = 100
  2. χ² = Σ (Oᵢⱼ − Eᵢⱼ)² / Eᵢⱼ = 16.666667
  3. df = (r − 1)(c − 1) = (2 − 1)(2 − 1) = 1
  4. p = P(χ²(1) ≥ 16.6667) = Q(1/2, 16.6667/2) = < 0.0001

Cross-check: the p-value from the incomplete-gamma routine and from the closed form χ²(1): erfc(√(χ²/2)) agree to displayed precision.

Sources cited

How it works

Pearson's chi-square test measures the gap between the counts you observed and the counts you would expect if a hypothesis were true. The statistic is always the same shape — χ² = Σ (O − E)² / E summed over every category or cell — and the two tests below differ only in how the expected counts E and the degrees of freedom are found. The formulas follow the NIST/SEMATECH e-Handbook of Statistical Methods.

Goodness of fit. You have counts in k categories and a target distribution. With an equal (uniform) expectation, each expected count is E = N / k, where N is the total. With custom weights, the weights are normalised to sum to N, so 20, 20, 20 and 1, 1, 1 describe the same uniform shape. The degrees of freedom are df = k − 1 − m, where m is the number of distribution parameters you estimated from the same data — leave m at 0 when the expected distribution was fixed in advance (NIST §1.3.5.15).

Test of independence. You have an r×c table of counts cross-classifying two variables. Under the null hypothesis of independence the expected count in each cell is E_ij = (row i total × column j total) / N. Degrees of freedom are df = (r − 1)(c − 1). For a 2×2 table that is a single degree of freedom (NIST §7.4.3).

Yates' continuity correction is offered for 2×2 tables only. It replaces each |O − E| with max(0, |O − E| − 0.5) before squaring, which compensates for fitting discrete counts to the continuous χ² curve. It always lowers χ², so it is conservative; the tool shows both the corrected and uncorrected values.

The p-value is the upper-tail area of the χ² distribution with the right degrees of freedom: p = P(X ≥ χ²) = Q(df/2, χ²/2), where Q is the regularised upper incomplete gamma function. This tool evaluates Q by series expansion and continued fraction (Numerical Recipes §6.2) rather than reading a rounded printed table, so every χ² and df resolve exactly to display precision. The critical valueis the inverse: the χ² whose upper-tail area equals your significance level α, found by bisection on the same distribution. You reject the null hypothesis when χ² ≥ the critical value, equivalently when p ≤ α. For 1 and 2 degrees of freedom the p-value is cross-checked against an independent closed form, and the critical values match the NIST table to three decimals. Finally, the tool flags any expected count below 5, where the χ² approximation weakens and Fisher's exact test or combining categories is the better move.

Worked examples

Goodness of fit — is the die fair?

60 rolls, observed faces vs an equal expected 10 each

  1. Observed = {8, 12, 9, 11, 14, 6}, N = 60, k = 6, equal E = 60/6 = 10
  2. (8−10)²/10 + (12−10)²/10 + (9−10)²/10 = 0.4 + 0.4 + 0.1
  3. + (11−10)²/10 + (14−10)²/10 + (6−10)²/10 = 0.1 + 1.6 + 1.6
  4. χ² = 4.20, df = 6 − 1 = 5
  5. critical value at α = 0.05 = 11.070, and 4.20 < 11.070

p ≈ 0.521 → fail to reject H₀. The rolls are consistent with a fair die.

Independence — 2×2 association

Gender × class preference, N = 100

  1. Observed [[30,10],[20,40]]; row totals 40, 60; column totals 50, 50
  2. Expected = (row×col)/N → [[20,20],[30,30]]
  3. χ² = (30−20)²/20 + (10−20)²/20 + (20−30)²/30 + (40−30)²/30
  4. = 5 + 5 + 3.333 + 3.333 = 16.67, df = (2−1)(2−1) = 1
  5. critical value at α = 0.05 = 3.841, and 16.67 > 3.841

p ≈ 0.000045 → reject H₀. Gender and preference are associated.

Same table with Yates' correction

Continuity correction on the 2×2 above

  1. |O − E| = 10 in every cell → (10 − 0.5)² = 90.25
  2. χ²_Yates = 90.25/20 + 90.25/20 + 90.25/30 + 90.25/30
  3. = 4.5125 + 4.5125 + 3.0083 + 3.0083 = 15.04, df = 1
  4. Still far above the 3.841 critical value

p ≈ 0.0001 → reject H₀. The correction lowers χ² from 16.67 to 15.04 but the conclusion holds.

Frequently asked questions

Sources & references

Related tools

Rate this tool
Be the first to rate

Comments & feedback

Spotted a bug or want an improvement? Tell us — our team reviews every comment, and good ideas get built. Comments are public and anonymous.

Found a bug, edge case, or want to suggest an improvement?

Email me at [email protected] — most fixes ship within 24 hours.