EProportions · Guide test 060
Fisher exact test
Tests association in a contingency table conditional on its margins; the familiar case is a two by two table.
Do it in CodeSplash
In the app, set it up in Analyze with one of these methods:
- χ² test of independence — Are two categorical variables associated?
- Two independent proportions (z, Fisher, Boschloo, Barnard, non-inferiority) — Does the share of an outcome differ between two independent groups?
Do it in CodeSplash Run this test on your own data, in Python or R.
Bench
Change the design, drag the data, move α. The statistic, its reference distribution and the verdict follow.
Bench · Fisher's exact test
Alternative
Your data
Edit the counts| Success | Failure | Total | |
|---|---|---|---|
| Row 1 | 9E = 6.0 | 3E = 6.0 | 12 |
| Row 2 | 3E = 6.0 | 9E = 6.0 | 12 |
| Total | 12 | 12 | 24 |
E is the count expected if rows and columns were unrelated.
If H₀ were true
- The reference: how a varies across samples when H₀ is true.
- Your statistic, from the data above.
- The p-value: the total probability of outcomes no more likely than yours (p = .039).
- The hatched rejection region at α = .050: statistics landing here reject H₀.
The formula
With your numbers
- the four cell counts (a is top-left)
- the total
- hypergeometric probability of a with all margins fixed
The verdict
a = 9, p = .039, OR = 8.05
p ≤ α = .050: reject H₀
Effect size: conditional MLE odds ratio OR = 8.052
Conditioning on the margins leaves one free cell; its hypergeometric distribution gives an exact p-value at any sample size.
- Statistic
- 9.000
- p-value
- 0.0391
Fisher's exact test on a 2 × 2 table of 24 counts: a = 9, p = .039, OR = 8.05. The p-value is below α = .050, so the data are hard to reconcile with H₀.
Question and hypotheses
H0: the odds ratio in a 2×2 table equals 1, conditional on margins.
Assumptions
Independent units; fixed margins or conditioning on observed margins justified by the design.
Formula
Parameters and notation
a is the first cell; r1 is its row total and c1 its column total. Feasible a values preserve all margins.
Structure of the method
Condition on the row and column margins of a 2-by-2 table, enumerate the feasible first-cell counts, and calculate hypergeometric probabilities. A tail rule converts these probabilities into a p-value; the probability-ordering two-sided rule is not the same as doubling every one-sided tail.
Estimated and nuisance parameters
Conditioning removes marginal probability nuisance parameters under the odds-ratio-one null. The observed sample odds ratio and the conditional maximum-likelihood odds ratio are different estimators; Python and R may report different estimates while giving matching conditional p-values.
Settings and design choices
Prespecify the odds-ratio null, alternative direction, two-sided ordering, confidence level, and confidence-interval convention. Do not choose Fisher versus an unconditional procedure by whichever yields significance. Table orientation must keep the success category and reference group consistent.
Fitting and adaptation
For a new independent table, recompute the conditional distribution from its new margins. There is no model fine-tuning. Inverting noncentral hypergeometric tests yields conditional odds-ratio intervals; verify compatibility between the chosen test ordering and interval procedure.
Sensitivity and robustness
Discrete p-values can be conservative and confidence bounds may be infinite when cells are zero. Exactness is conditional on the stated model, not a guarantee against dependence or misclassification. Tiny tables often cannot exclude clinically meaningful effects despite nonsignificance.
How to use and calibrate
Sum conditional hypergeometric probabilities in the chosen tail; specify the two sided convention. Noncentral hypergeometric inference tests other odds ratios.
Example application
Analyze a small 2×2 response table.
Interpretation and pitfalls
Watch out
Conditional exactness does not mean a uniquely defined two sided p value, nor does it repair clustered data.
What changed in edition 2
- New sections: structure of the method, estimated and nuisance parameters, settings and design choices, fitting and adaptation, sensitivity and robustness.
Python and R
For McNemar the table represents paired before/after categories; other entries use independent observations. The same numbers illustrate different sampling designs.
Validated — Executed successfully in the validation environment · needs numpy, scipy
import numpy as np
from scipy import stats
rng = np.random.default_rng(2026)
tab = np.array([[18,12],[9,21]])
print(stats.fisher_exact(tab))
Output
SignificanceResult(statistic=np.float64(3.5), pvalue=np.float64(0.03699239594051111))
Validated — Executed successfully in the validation environment · needs Base/standard library
tab <- matrix(c(18,12,9,21),nrow=2,byrow=TRUE)
print(fisher.test(tab))
Output
Fisher's Exact Test for Count Data data: tab p-value = 0.03699 alternative hypothesis: true odds ratio is not equal to 1 95 percent confidence interval: 1.066515 11.747297 sample estimates: odds ratio 3.423016
Sources
- SciPy — SciPy · fisher_exactdocs.scipy.org
- scipy.stats.fisher_exactdocs.scipy.org
- Statistics guide v4 (historical):
- R: Executed successfully in the validation environment
- Reference's record:
- Edition 2 review: Python: executed · R: not executed (Rscript unavailable). Python: Ran in the reference's review, on this exact code. R: Reviewed but not run: R wasn't available to the reference's authors.
From the CodeSplash statistics guide (v4), entry 060, updated from the reference's edition 2 (reviewed 14 November 2026) as STAT060.