Last week, a pull request discussion derailed our sprint review. An engineer and an analyst were evaluating conversion counts between two onboarding flows on a small cohort. The engineer ran a script calling scipy.stats.chi2_contingency, getting chi2 = 5.8741 and p = 0.01536. The analyst calculated uncorrected Pearson chi-square, getting chi2 = 8.0607 and p = 0.00452. Their Cramér's V metrics clashed too: 0.4947 versus 0.5795.
Both colleagues were looking at the exact same four counts in a 2x2 grid. Neither had made an arithmetic blunder. The discrepancy came down to a quiet behavioral default in SciPy that catches Python developers every single day: Frank Yates's continuity correction.
To see why the two scripts diverged, consider the contingency matrix from our test case. The table summarized twenty-four observations across two treatment groups:
from scipy.stats import chi2_contingency, fisher_exact
from scipy.stats.contingency import association
import numpy as np
tbl = np.array([[8, 2],
[3, 11]])
chi2_y, p_y = chi2_contingency(tbl)[:2]
chi2_p, p_p = chi2_contingency(tbl, correction=False)[:2]
v_y = association(tbl, method='cramer', correction=True)
v_p = association(tbl, method='cramer', correction=False)
print(f"Yates (correction=True): chi2 = {chi2_y:.4f}, p = {p_y:.5f}, Cramer's V = {v_y:.4f}")
print(f"Pearson (correction=False): chi2 = {chi2_p:.4f}, p = {p_p:.5f}, Cramer's V = {v_p:.4f}")
Yates (correction=True): chi2 = 5.8741, p = 0.01536, Cramer's V = 0.4947
Pearson (correction=False): chi2 = 8.0607, p = 0.00452, Cramer's V = 0.5795
When you call scipy.stats.chi2_contingency(tbl) without extra keywords, Python sets correction=True automatically. For any table where degrees of freedom equal one—meaning any 2x2 layout—SciPy applies Yates's continuity correction. Rather than computing standard Pearson squared deviations between observed and expected frequencies, the algorithm subtracts 0.5 from the absolute deviation before squaring: (|O - E| - 0.5)**2 / E.
Yates introduced this deduction in 1934 to adjust for approximating discrete binomial probabilities with a continuous curve. With small counts, discrete probability steps can cause continuous curves to underestimate tail probabilities. Subtracting half a unit pulls the statistic toward zero, reducing test magnitude and inflating the p-value.
In our 24-subject table, row totals are 10 and 14, while column totals are 11 and 13. Multiplying the smallest row and column totals and dividing by 24 gives a minimum expected cell frequency of 4.583. Because this frequency dips just below 5, the table sits right on classic rule-of-thumb boundaries. The 0.5 deduction shrinks the test statistic from 8.0607 to 5.8741, a 27 percent drop.
The second trap is that scipy.stats.contingency.association defaults to correction=False. Running default chi2_contingency(tbl) alongside default association(tbl, method='cramer') reports a Yates-corrected p-value next to an uncorrected Pearson effect size. The hypothesis test and effect size in the same dashboard come from conflicting assumptions.
When expected cell counts drop under five, developers often switch to Fisher's exact test. Evaluating our sample table with fisher_exact(tbl, alternative='two-sided') returns an unconditional sample odds ratio of 14.6667 and a two-sided p-value of 0.01109. That exact p-value sits directly between the uncorrected Pearson result (0.00452) and the Yates result (0.01536).
Seeing three distinct p-values (0.00452, 0.01109, and 0.01536) for a single twenty-four-row experiment raises a practical question: which test statistic actually protects nominal error rates without destroying statistical power?
To evaluate test calibration under the null hypothesis, I simulated 10,000 independent trials in Python 3.14.3 and SciPy 1.17.1 with seed 20261123. Each trial drew two cohorts (n1 = 15, n2 = 15) with baseline conversion p0 = 0.30, skipping degenerate all-zero or all-full tables. At nominal alpha = 0.05, a calibrated test should reject in 5.0 percent of trials.
from scipy.stats import chi2_contingency, fisher_exact
import numpy as np
rng = np.random.default_rng(20261123)
a1 = rng.binomial(15, 0.30, size=10_000)
a2 = rng.binomial(15, 0.30, size=10_000)
p_yates, p_pearson, p_fisher = [], [], []
for x, y in zip(a1, a2):
if (x == 0 and y == 0) or (x == 15 and y == 15):
p_yates.append(1.0); p_pearson.append(1.0); p_fisher.append(1.0)
continue
tb = np.array([[x, 15 - x], [y, 15 - y]])
p_yates.append(chi2_contingency(tb, correction=True).pvalue)
p_pearson.append(chi2_contingency(tb, correction=False).pvalue)
p_fisher.append(fisher_exact(tb).pvalue)
print(f"yates : {np.mean(np.array(p_yates) < 0.05):.4f}")
print(f"pearson: {np.mean(np.array(p_pearson) < 0.05):.4f}")
print(f"fisher : {np.mean(np.array(p_fisher) < 0.05):.4f}")
yates : 0.0157
pearson: 0.0549
fisher : 0.0175
The simulation highlights why Yates's correction is controversial. At nominal alpha = 0.05, the Yates-corrected test rejected in only 1.57 percent of trials, over-correcting by more than a factor of three. Fisher's exact test was similarly conservative at 1.75 percent due to discrete hypergeometric support. In contrast, uncorrected Pearson chi-square registered a 5.49 percent rejection rate, tracking the 5.0 percent threshold closely.
This conservatism has real consequences: running correction=True cuts your effective significance level below 0.02, discarding real feature wins. Methodologists have documented this for decades. A comparative evaluation by Lydersen, Fagerland, and Laake (2009, Statistics in Medicine) showed that Yates's continuity correction and standard two-sided Fisher's exact test are overly conservative compared with mid-p or N - 1 chi-square alternatives. Ian Campbell similarly showed that Pearson's chi-square with an N - 1 divisor preserves nominal Type I error far better than Yates.
We adopted three concrete rules for Python analytics:
First, make parameters explicit. Never call scipy.stats.chi2_contingency with bare arguments on 2x2 tables. Pass correction=False to retain Pearson's nominal power, or correction=True if your protocol mandates Yates.
Second, harmonize effect sizes. If you compute Cramér's V via scipy.stats.contingency.association, pass the exact same correction flag used in your chi-square call to prevent reporting an uncorrected effect size next to a corrected p-value.
Third, check theoretical expected cell counts directly. Compute E = np.outer(row_sums, col_sums) / N. If expected counts are healthy, uncorrected Pearson chi-square controls false positives accurately; if expected counts drop near zero, transition deliberately to Fisher's exact test rather than relying on a silent continuity deduction.
Top comments (0)