What this calculator does
The chi-square (χ²) test compares the counts you observed with the counts you would expect if nothing interesting were going on. The test of independence asks whether two categorical variables are related: if they were unrelated, each cell of the table would simply reflect its row and column totals. The goodness of fit test asks whether one categorical variable follows a split you specify, such as equal shares or last year’s mix.
Besides χ² and the p-value, you get the expected counts, Cramér’s V (or Cohen’s w for goodness of fit) as an effect size, adjusted residuals that show which cells drive the result, the likelihood-ratio G-test, and for 2×2 tables Yates’ correction and Fisher’s exact test.
The formulas
χ² = Σ (O − E)² / E, df = (rows − 1)(columns − 1) · goodness of fit: df = k − 1
Cramér’s V = √( χ² / (N × (min(rows, columns) − 1)) ) · Cohen’s w = √(χ² / N)
Adjusted residual = (O − E) / √( E × (1 − row share) × (1 − column share) )
The p-value is the upper tail of the chi-square distribution with those degrees of freedom.
Worked example: payment method by city tier
Press Load example. An online store took 1,300 recent orders and recorded the customer’s city tier and how they paid: UPI, card or cash on delivery (COD).
- Result: χ²(4) = 74.63, p < 0.0001. Payment method and city tier are clearly related.
- Effect size: Cramér’s V = 0.169, a small association for a 3×3 table (medium starts near 0.21). Tier matters, but it is far from the whole story.
- Where it comes from: Tier 3 uses COD much more than independence predicts (39.7% of its orders against 25.7% overall, adjusted residual +6.6), while Tier 1 uses COD less (−6.6) and cards more (+5.7). UPI is broadly similar everywhere.
The business reading: COD costs and return risk concentrate in Tier 3, so that is where a prepaid incentive would pay off. Card-specific offers mainly reach Tier 1 customers.
Worked example: are tickets spread evenly across the week?
Switch to Goodness of fit and load the example: 800 support tickets by day of week, tested against an equal split (114.3 expected per day). The result is χ²(6) = 33.56, p < 0.0001. Sunday alone contributes 15.65 of the 33.56, with only 72 tickets against 114 expected, so weekend staffing can be lighter than weekday staffing.
Assumptions
- Counts, not percentages or averages. Each record is counted in exactly one cell.
- Independent records. The same customer should not appear in several cells (for repeated yes/no answers from the same people, use McNemar’s test).
- Large enough expected counts: none below 1 and at most 20% below 5. The calculator checks this for you.
Getting the same result in Excel and Python
In Excel, build the expected table with =row_total*column_total/grand_total, then
=CHISQ.TEST(observed_range, expected_range) returns the p-value. In Python:
from scipy import stats
import numpy as np
observed = np.array([[312, 148, 90], # Tier 1: UPI, Card, COD
[241, 72, 117], # Tier 2
[158, 35, 127]]) # Tier 3
chi2, p, dof, expected = stats.chi2_contingency(observed, correction=False)
print(chi2, p, dof) # 74.63, 2.4e-15, 4
print(stats.contingency.association(observed, method="cramer")) # 0.169
print(stats.chisquare([142, 128, 121, 119, 131, 87, 72])) # goodness of fit: 33.56, p = 8.2e-06
Common mistakes
- Running the test on percentages. The result then depends on nothing but the percentages, and is meaningless.
- Reading significance as strength. With thousands of records, a trivial association can be significant. Check Cramér’s V.
- Stopping at “they are related”. The residuals tell you which categories differ and in which direction, which is usually the actual answer.
- Including a totals row or column in the pasted table. The calculator drops rows and columns labelled “Total” automatically and tells you.