BIOSTATISTICS • CATEGORICAL DATA ANALYSIS

Fisher's Exact Test

An exact method for testing association in 2×2 contingency tables when sample sizes are small.

Historical Context & Motivation

In the early decades of the twentieth century, statisticians increasingly confronted the problem of analyzing categorical data arranged in contingency tables. The chi-squared test, introduced by Karl Pearson in 1900, provided an asymptotic framework for evaluating the independence of two categorical variables. However, the chi-squared approximation becomes unreliable when expected cell frequencies are small — a situation that arises routinely in clinical trials, genetics studies, and epidemiological investigations where sample sizes are limited. This shortcoming motivated the search for a method that would yield exact probabilities without relying on large-sample approximations.

The solution arrived through the work of Sir Ronald A. Fisher, one of the most influential statisticians of the twentieth century. Fisher devised an elegant test that computes the probability of obtaining any particular arrangement of cell counts in a 2×2 table, conditional on the fixed marginal totals. The famous illustration of this method involved a colleague, Dr. Muriel Bristol, who claimed she could distinguish whether milk or tea had been poured into the cup first — an anecdote that has become one of the most celebrated thought experiments in the history of statistics.

1900
Pearson's Chi-Squared Test
Karl Pearson introduces the chi-squared goodness-of-fit test, laying the foundation for asymptotic categorical data analysis. The test relies on large-sample theory for its distributional approximation.
1922
Fisher's Foundational Papers
R.A. Fisher publishes seminal works on the mathematical foundations of statistics, including maximum likelihood estimation, which frame the theoretical landscape for exact inference methods.
1935
The Lady Tasting Tea
Fisher publishes The Design of Experiments, presenting the exact test through the famous tea-tasting experiment. This work formalizes the hypergeometric probability model for 2×2 tables with fixed margins.
1970s
Computational Feasibility
With the advent of digital computing, Fisher's exact test becomes practical for routine use. Software implementations eliminate the need for manual combinatorial calculations, democratizing the method for applied researchers.
2000s–Present
Modern Extensions
Generalizations to r×c tables and network-based algorithms extend exact testing beyond the 2×2 case. Fisher's exact test remains a standard tool in genomics, clinical trials, and any setting with sparse categorical data.

The central question Fisher addressed is deceptively simple: given the observed marginal totals of a 2×2 contingency table, how likely is it to observe the specific arrangement of cell counts — or one more extreme — if the row and column variables are truly statistically independent? By computing this probability exactly rather than relying on an asymptotic χ² distribution, Fisher's test provides valid inference even when cells contain very few observations, making it indispensable in modern biostatistical practice.

Core Principles & Definitions

Fisher's exact test rests upon a set of interlocking principles that distinguish it from approximate methods like the chi-squared test. Understanding these principles is essential before engaging with the mathematical machinery, because the test's validity hinges on the specific probabilistic model it invokes — namely, the hypergeometric distribution applied to the cell counts of a 2×2 contingency table with fixed marginal totals.

1

Conditioning on Margins

Fisher's test treats the row and column marginal totals as fixed. Under this conditioning, only one cell is free to vary in a 2×2 table, and the probability of each possible table configuration is computed exactly via the hypergeometric distribution.
2

Exact Probability Calculation

Unlike the chi-squared test, which approximates the sampling distribution, Fisher's test calculates the exact probability of observing the data (or more extreme configurations) under the null hypothesis of independence. No minimum expected cell count is required.
3

Null Hypothesis of Independence

The null hypothesis (H₀) posits that the two categorical variables are statistically independent — knowing one variable provides no information about the other. The alternative hypothesis (H₁) asserts that an association exists.
4

The Hypergeometric Model

When margins are fixed, the distribution of any single cell count follows the hypergeometric distribution. This is analogous to drawing colored balls from an urn without replacement — the exact model underlying the test's combinatorial logic.
5

One-Tailed vs. Two-Tailed

A one-tailed test evaluates association in a specific direction, while a two-tailed test assesses association in either direction by summing probabilities of all tables at least as extreme as the observed one, regardless of the direction of deviation.
KEY TAKEAWAY
Think of Fisher's exact test like counting all possible ways to deal cards into a specific layout. Imagine you have a fixed number of red and black cards (row totals) and two piles of fixed size (column totals). Under the null hypothesis, the cards are randomly assigned to piles. Fisher's test enumerates every possible deal and computes the exact fraction of deals that are as extreme as — or more extreme than — the one you actually observed. If that fraction is very small, the arrangement is unlikely to have arisen by chance alone, and you reject independence.

Visual Explanation

The following diagram illustrates the structure of a 2×2 contingency table and the relationship between the cell counts, marginal totals, and the grand total. Because the marginals are treated as fixed, only one cell value (here, cell a) is free to vary; the remaining three cells are determined by subtraction. The diagram also shows how the hypergeometric probability is constructed from the factorial terms associated with each margin and cell.

A 2×2 contingency table with cell counts a, b, c, d. The row totals (a + b and c + d) and column totals (a + c and b + d) are treated as fixed. The grand total is n. Only one cell (marked FREE) is free to vary under the constraint.

As the diagram makes clear, once the four marginal totals are fixed, specifying any single cell — say, cell a — automatically determines the other three by subtraction: b = (a + b) − a, c = (a + c) − a, and d = n − a − b − c. This means there is only one degree of freedom in the table, and the test evaluates all possible values of a under H₀ to compute the p-value.

Mathematical Framework

The mathematical foundation of Fisher's exact test is the hypergeometric probability of observing a particular cell count a given fixed marginal totals. We denote the row totals as R₁ = a + b and R₂ = c + d, the column totals as C₁ = a + c and C₂ = b + d, and the grand total as n. The probability of the observed table — or equivalently, the probability that cell a takes a particular value — is given by the following expression.

HYPERGEOMETRIC PROBABILITY
P(a) = C(R₁, a) × C(R₂, C₁ − a) / C(n, C₁)
where C(n, k) = n! / (k!(n − k)!) is the binomial coefficient, R₁ = a + b (row 1 total), R₂ = c + d (row 2 total), C₁ = a + c (column 1 total), and n is the grand total.

An equivalent formulation, often seen in textbooks, expresses the probability directly in terms of factorials of the margins and cells.

FACTORIAL FORM
P = (R₁! × R₂! × C₁! × C₂!) / (n! × a! × b! × c! × d!)
This is algebraically equivalent to the binomial-coefficient form and is often more convenient for manual computation. The numerator contains the product of the four marginal factorials, and the denominator contains the grand total factorial multiplied by the four cell factorials.

To compute the p-value, one sums the hypergeometric probabilities of all table configurations that are as extreme as or more extreme than the observed table. For a one-tailed test in the direction of a positive association, this means summing over all values of a from the observed value up to min(R₁, C₁). For a two-tailed test, one also includes tables in the opposite tail whose individual probabilities are less than or equal to the probability of the observed table.

ONE-TAILED P-VALUE (UPPER TAIL)
p = Σ P(k) for k = a_obs, a_obs + 1, ..., min(R₁, C₁)
The summation runs over all values of k from the observed cell count aobs to the maximum possible value of a given the marginal constraints.
RANGE OF CELL a
max(0, C₁ − R₂) ≤ a ≤ min(R₁, C₁)
The lower bound ensures non-negative counts in all cells; the upper bound ensures that a does not exceed either the row 1 total or the column 1 total. Enumerating all values of a in this range produces every possible table under the given marginals.

Probability Landscape of All Possible Tables

To build intuition for how the p-value is computed, consider a concrete scenario. Suppose we observe a 2×2 table with marginals R₁ = 5, R₂ = 7, C₁ = 4, and C₂ = 8 (n = 12). The free cell a can range from max(0, 4 − 7) = 0 to min(5, 4) = 4, giving five possible table configurations. The diagram below plots the hypergeometric probability of each configuration and shows which tables are included in one-tailed and two-tailed p-value calculations when the observed value is a = 4.

Hypergeometric probability distribution for a table with marginals R₁ = 5, R₂ = 7, C₁ = 4, C₂ = 8. Each bar represents the probability of that value of cell a under H₀. If aobs = 4, the one-tailed p-value sums P(3) + P(4) = 0.088 + 0.008 = 0.096.

The bar chart reveals that under independence, the most probable table configurations are a = 1 and a = 2, which together account for over 76% of the probability mass. The observed value a = 4 lies in the extreme right tail with a probability of only 0.008. The one-tailed p-value (testing whether a is unusually large) sums P(a ≥ 3) = 0.088 + 0.008 = 0.096. Since this exceeds the conventional α = 0.05 threshold, we would fail to reject H₀ at the 5% level in this example, though the result would be significant at α = 0.10.

Worked Example: Drug Efficacy Trial

Consider a small clinical trial in which 12 patients are randomized to either a new drug (n = 5) or placebo (n = 7). The primary outcome is binary: improvement (yes/no). Suppose 3 of the 5 drug patients and 1 of the 7 placebo patients improve. We wish to test whether the drug is associated with improvement using Fisher's exact test at the α = 0.05 significance level (one-tailed, since we are testing whether the drug improves outcomes).

Observed 2×2 contingency table for drug efficacy trial
ImprovedNot ImprovedTotal
Drug3 (a)2 (b)5
Placebo1 (c)6 (d)7
Total4812
Fisher's Exact Test: Drug vs. Placebo
1
Step 1 — Identify Margins and Cell RangeThe marginals are R₁ = 5, R₂ = 7, C₁ = 4, C₂ = 8, and n = 12. The free cell a (Drug × Improved) ranges from max(0, 4 − 7) = 0 to min(5, 4) = 4. The observed value is a = 3.
a ∈ {0, 1, 2, 3, 4}, aobs = 3
2
Step 2 — Compute P(a = 3) Using the Factorial FormulaP(a = 3) = (R₁! × R₂! × C₁! × C₂!) / (n! × a! × b! × c! × d!) = (5! × 7! × 4! × 8!) / (12! × 3! × 2! × 1! × 6!). Computing the numerator: 120 × 5040 × 24 × 40320 = 585,252,864,000. Computing the denominator: 479,001,600 × 6 × 2 × 1 × 720 = 4,137,533,952,000. Therefore P(3) = 585,252,864,000 / 4,137,533,952,000.
P(a = 3) ≈ 0.1414
3
Step 3 — Compute P(a = 4) for the More Extreme TableP(a = 4) = (5! × 7! × 4! × 8!) / (12! × 4! × 1! × 0! × 7!) = 585,252,864,000 / (479,001,600 × 24 × 1 × 1 × 5040). The denominator = 57,915,973,017,600. Wait — let us recompute carefully. Denominator = 479,001,600 × 24 × 1 × 1 × 5040 = 57,915,973,017,600. This is incorrect; let's use the ratio approach. P(4)/P(3) = [b × c] / [(a+1)(d+1)] = [2 × 1] / [4 × 7] = 2/28 = 1/14 ≈ 0.0714. So P(4) = 0.1414 × 0.0714 ≈ 0.0101.
P(a = 4) ≈ 0.0101
4
Step 4 — Sum Probabilities for One-Tailed P-ValueThe one-tailed p-value (testing for a positive association between drug and improvement) is the sum of probabilities for tables at least as extreme as the observed table in the direction of interest: p = P(a = 3) + P(a = 4) = 0.1414 + 0.0101.
p = 0.1515
5
Step 5 — Decision and InterpretationSince p = 0.1515 > α = 0.05, we fail to reject the null hypothesis of independence. Although a higher proportion of drug patients improved (60% vs. 14%), the sample size is too small to conclude that this difference is statistically significant at the 5% level. The result suggests the trial may be underpowered, and a larger sample size could be warranted.
Fail to reject H₀ at α = 0.05
💡 Computational Tip
For manual calculations, the recurrence relation P(a+1) = P(a) × [b × c] / [(a+1)(d+1)] allows you to compute successive probabilities from any starting value without recomputing all factorials. Most statistical software (R's fisher.test(), Python's scipy.stats.fisher_exact()) handles this automatically.

Fisher's Exact Test vs. Chi-Squared Test

A natural question arises: when should one use Fisher's exact test rather than the more familiar Pearson's chi-squared test? The answer depends on sample size, expected cell counts, and the specific inferential context. The table below summarizes the key distinctions between these two approaches to testing independence in 2×2 contingency tables.

Comparison of Fisher's exact test and Pearson's chi-squared test for 2×2 tables
FeatureFisher's Exact TestChi-Squared Test
Probability modelHypergeometric (exact)χ² approximation (asymptotic)
Sample size requirementNone — valid for any sample sizeAll expected counts ≥ 5 (rule of thumb)
Computational costHigher for large n (combinatorial enumeration)Low — single formula
Type I error controlConservative (actual α ≤ nominal α)Liberal when expected counts are small
MarginalsConditions on fixed marginsDoes not require fixed margins
ExtensionsGeneralizable to r×c tables (computationally expensive)Naturally extends to r×c and multi-way tables
When to preferSmall samples, sparse cells, regulatory submissionsLarge samples, exploratory analysis
KEY TAKEAWAY
Fisher's exact test is analogous to counting every possible arrangement of items in labeled boxes, while the chi-squared test is like estimating the distribution of those arrangements using a smooth continuous curve. When there are enough items (large n), the smooth approximation works well and is computationally efficient. But when items are few, the smooth curve oversimplifies the discrete, lumpy reality, and only the exact enumeration can be trusted. As a general rule, favor Fisher's exact test whenever any expected cell count falls below 5, or whenever precise p-values are required (e.g., regulatory submissions to the FDA).

Connections to Advanced Methods

Fisher's exact test for 2×2 tables is the simplest member of a broader family of exact conditional tests. Understanding its relationship to more advanced methods helps situate the test within the larger statistical ecosystem and prepares the student for extensions encountered in advanced biostatistics coursework.

Fisher's exact test in the landscape of categorical data methods
FeatureFisher's Exact Test (2×2)Advanced Extensions
Table dimension2×2 onlyFisher–Freeman–Halton test extends to r×c tables
StratificationSingle 2×2 tableCochran–Mantel–Haenszel test pools evidence across K strata
Effect estimationProvides p-value only (no direct effect estimate)Exact logistic regression provides conditional maximum likelihood estimates and exact confidence intervals
Multiple testingSingle comparisonPermutation tests and exact methods in genomics handle thousands of simultaneous comparisons
Bayesian analogFrequentist exact inferenceBayesian analysis of contingency tables with Dirichlet priors provides posterior probability of association

A particularly important connection is to exact logistic regression, which can be understood as a multivariate generalization of Fisher's exact test. Where Fisher's test conditions on the marginals of a single 2×2 table, exact logistic regression conditions on the sufficient statistics of the logistic model to obtain exact conditional distributions for the regression coefficients. This method is invaluable when modeling binary outcomes with small samples and multiple predictors — a scenario where standard maximum likelihood logistic regression may fail to converge.

Additionally, the Barnard's test offers an unconditional alternative to Fisher's test. Rather than fixing both sets of margins, Barnard's test fixes only one margin and maximizes the p-value over nuisance parameters. Although Barnard's test can be more powerful than Fisher's in certain configurations, it is computationally more demanding and less widely implemented in standard software, which explains Fisher's test's continued dominance in practice.

Practice Problems

PROBLEM 1CONCEPTUAL
Explain why Fisher's exact test conditions on the marginal totals being fixed. What probabilistic model does this conditioning invoke, and why does it matter for the test's validity with small samples?
PROBLEM 2BASIC CALCULATION
Given a 2×2 table with a = 4, b = 1, c = 2, d = 8, compute the exact probability of observing this particular table configuration using the factorial formula P = (R₁! × R₂! × C₁! × C₂!) / (n! × a! × b! × c! × d!).
PROBLEM 3INTERMEDIATE
A researcher studies the association between a genetic variant (present/absent) and disease status (case/control) in 20 participants. The observed table is: variant-present cases = 6, variant-present controls = 2, variant-absent cases = 4, variant-absent controls = 8. Enumerate all possible tables under the observed marginals and compute the two-tailed p-value for Fisher's exact test.
PROBLEM 4APPLIED
In a pilot study for a new antibiotic, 8 patients with antibiotic-resistant infections receive the new drug and 6 receive the standard treatment. Bacterial clearance is observed in 5 of the new drug patients and 1 of the standard treatment patients. Perform a one-tailed Fisher's exact test (testing whether the new drug has a higher clearance rate) at α = 0.05. Should the researchers proceed to a Phase II trial based on this result?
PROBLEM 5CRITICAL THINKING
Fisher's exact test is known to be conservative — its actual Type I error rate is often below the nominal α. Explain the source of this conservatism mathematically, and discuss how the mid-p correction addresses this issue. Under what circumstances might a biostatistician prefer the standard (conservative) Fisher's test despite the availability of the mid-p approach?

Summary & Review

Fisher's exact test is a non-parametric statistical test that evaluates the independence of two categorical variables in a 2×2 contingency table by computing exact probabilities under the hypergeometric distribution. Unlike the chi-squared test, which relies on large-sample approximations, Fisher's test is valid for any sample size and does not require a minimum expected cell count. The test conditions on the fixed marginal totals of the table, reducing the problem to a single free cell whose distribution under the null hypothesis is fully specified.

The p-value is computed by summing the probabilities of all tables as extreme as or more extreme than the observed table. The test may be conducted as one-tailed or two-tailed depending on the research hypothesis. Although Fisher's test is conservative due to the discreteness of the hypergeometric distribution, the mid-p correction can bring the actual Type I error rate closer to the nominal level. The test remains a cornerstone of biostatistical practice, indispensable in clinical trials, genetics, and epidemiology whenever sample sizes are small or data are sparse.

Varsity Tutors • Biostatistics • Fisher's Exact Test