Fisher's Exact Test is a nonparametric significance test that determines whether two categorical variables are associated within a 2×2 contingency table. Unlike the Pearson chi-square approximation, this method calculates exact probabilities directly from the hypergeometric distribution, making it the gold-standard procedure when sample sizes are small or when expected cell frequencies violate asymptotic assumptions.
Originally devised by Sir Ronald A. Fisher in the 1920s, the test remains a cornerstone of biomedical research, clinical trials, epidemiology, and quality assurance. This calculator performs the full exact computation — returning the P-value, odds ratio (OR), relative risk (RR), phi coefficient (ϕ), and per-group proportions — while employing a log-gamma (Lanczos approximation) engine that prevents factorial overflow even when the total sample $N$ reaches into the hundreds or thousands.
Required Project Parameters
- Hypothesis Type (Categorical) — The directionality of the statistical test. A Two-Tailed hypothesis tests for any association regardless of direction. A Left-Tailed hypothesis tests whether Group 1 exhibits a significantly lower proportion of Outcome 1 relative to Group 2. A Right-Tailed hypothesis tests for a significantly higher proportion.
- Cell A (Count) — The observed frequency of Outcome 1 in Group 1. Together with Cells B, C, and D, these four counts fully define the contingency matrix.
- Cell B (Count) — The observed frequency of Outcome 2 in Group 1.
- Cell C (Count) — The observed frequency of Outcome 1 in Group 2.
- Cell D (Count) — The observed frequency of Outcome 2 in Group 2.
- Significance Level $\alpha$ (Probability) — The threshold (commonly 0.05, 0.01, or 0.10) below which the null hypothesis of independence is rejected.
The Hypergeometric Foundation of Exact Categorical Inference
Anatomy of the 2×2 Contingency Matrix
Every Fisher's Exact Test begins with a 2×2 cross-classification of observed frequencies. The four cells — conventionally labeled $a$, $b$, $c$, $d$ — represent the joint distribution of two binary variables across two independent groups.
From these four counts, four marginal totals are derived:
- Row 1 total: $R_1 = a + b$
- Row 2 total: $R_2 = c + d$
- Column 1 total: $C_1 = a + c$
- Column 2 total: $C_2 = b + d$
- Grand total: $N = a + b + c + d$
Under the null hypothesis of independence, the expected frequency of Cell A equals $E_a = \frac{R_1 \cdot C_1}{N}$. The critical statistical rule is that Fisher's Exact Test is strictly required when more than 20% of expected cell frequencies fall below 5, or when any single expected frequency drops below 1. In these situations the chi-square asymptotic approximation becomes unreliable, and only exact enumeration of probabilities guarantees valid inference.
The Core Probability Function
The probability of observing a specific cell configuration — given fixed marginal totals — follows the hypergeometric distribution:
$$P(a) = \frac{\binom{R_1}{a} \binom{R_2}{C_1 - a}}{\binom{N}{C_1}}$$
Expanding the binomial coefficients into factorials yields the classic computational form:
$$P(a) = \frac{R_1!; R_2!; C_1!; C_2!}{a!; b!; c!; d!; N!}$$
This formula gives the exact probability of the observed table under the null hypothesis. The key insight is that all marginals ($R_1$, $R_2$, $C_1$, $C_2$) are treated as fixed constants, and the test evaluates only the single remaining degree of freedom — the value of cell $a$.
Logarithmic Computation and the Lanczos Approximation
A direct factorial calculation of $P(a)$ encounters numerical overflow for any realistic sample size. Standard 64-bit floating-point arithmetic cannot represent $171!$ or larger because the result exceeds approximately $1.7 \times 10^{308}$. Most basic web-based tools silently fail or return errors above $N \approx 170$.
This calculator circumvents the limitation by computing in logarithmic space:
$$\ln P(a) = \ln(R_1!) + \ln(R_2!) + \ln(C_1!) + \ln(C_2!) - \ln(a!) - \ln(b!) - \ln(c!) - \ln(d!) - \ln(N!)$$
Each $\ln(k!)$ is evaluated via the log-gamma function $\ln\Gamma(k+1)$, implemented through the Lanczos approximation with six high-precision coefficients (e.g., 76.18009…, −86.50532…). This approach produces results accurate to roughly 15 significant digits — matching the full precision of IEEE 754 double-precision arithmetic — regardless of whether $N$ is 30 or 30,000.
The final probability is recovered as $P(a) = e^{\ln P(a)}$, ensuring that all intermediate products remain within representable floating-point range.
Summation Strategies for One-Tailed and Two-Tailed Tests
The observed table probability $P_{\text{obs}}$ alone is not the P-value. The P-value is the cumulative probability of all tables at least as extreme as the observed one, given fixed marginals. The iteration bounds for cell $a$ are:
$$a_{\min} = \max(0,; R_1 + C_1 - N) \qquad a_{\max} = \min(R_1,; C_1)$$
For a Right-Tailed test, the P-value sums all $P(x)$ for $x \geq a_{\text{obs}}$. For a Left-Tailed test, it sums all $P(x)$ for $x \leq a_{\text{obs}}$.
The Two-Tailed case is more nuanced. Unlike the symmetric normal distribution, the hypergeometric distribution of a contingency table can be highly skewed. Therefore, the two-tailed P-value is computed by summing the probabilities of all tables whose individual exact probability satisfies $P(x) \leq P_{\text{obs}}$. A floating-point epsilon tolerance of $1 \times 10^{-10}$ is applied during this comparison ($P(x) \leq P_{\text{obs}} + \varepsilon$) to prevent IEEE 754 rounding artifacts from incorrectly excluding tables whose probability is functionally equal to the observed one.
This methodology means the two-tailed P-value is generally not simply double the one-tailed value — a common misconception rooted in experience with symmetric distributions.
Decision Criteria and Comparative Method Reference
Fisher's Exact Test versus Pearson's Chi-Square: Selection Guide
| Criterion | Fisher's Exact Test | Pearson's Chi-Square ($\chi^2$) | Yates-Corrected Chi-Square |
|---|---|---|---|
| Statistical basis | Exact hypergeometric probability | Asymptotic $\chi^2$ approximation | Continuity-corrected approximation |
| Minimum expected cell freq. | No minimum required | All cells $\geq 5$ recommended | All cells $\geq 5$ recommended |
| Mandatory use condition | >20% of expected cells < 5 or any cell < 1 | All expected cells ≥ 5 | All expected cells ≥ 5; $N < 40$ |
| Sample size behavior | Exact at any $N$ | Increasingly accurate as $N \to \infty$ | Conservative at small $N$ |
| Computational cost | Higher (full enumeration) | Minimal (single formula) | Minimal (single formula) |
| Two-tailed P-value symmetry | Asymmetric (skewed distribution) | Symmetric by construction | Symmetric by construction |
Effect Size Metrics: Odds Ratio, Relative Risk, and Phi Coefficient
| Metric | Formula | Valid Study Designs | Interpretation Benchmark |
|---|---|---|---|
| Odds Ratio (OR) | $\frac{a \cdot d}{b \cdot c}$ | Case-control, cross-sectional, cohort, RCT | OR = 1 → no association; OR > 1 → positive; OR < 1 → inverse |
| Relative Risk (RR) | $\frac{a / R_1}{c / R_2}$ | Cohort studies, RCTs only | RR = 1 → no difference; RR > 1 → increased risk in Group 1 |
| Phi Coefficient (ϕ) | $\frac{ad - bc}{\sqrt{R_1 \cdot R_2 \cdot C_1 \cdot C_2}}$ | Any 2×2 design | |ϕ| < 0.1 → negligible; 0.1–0.3 → small; 0.3–0.5 → moderate; > 0.5 → large |
| Risk Difference (RD) | $\frac{a}{R_1} - \frac{c}{R_2}$ | Cohort studies, RCTs | RD = 0 → no absolute difference; clinically interpretable as percentage-point change |
Common Significance Thresholds in Practice
| Domain | Typical $\alpha$ | Rationale | Common Adjustment |
|---|---|---|---|
| Clinical trials (Phase III) | 0.05 | Regulatory convention (FDA, EMA) | Bonferroni for multiple endpoints |
| Genomics / GWAS | $5 \times 10^{-8}$ | ~1 million independent tests genome-wide | Family-wise error rate control |
| Exploratory biomarker studies | 0.10 | Higher sensitivity for hypothesis generation | False Discovery Rate (FDR) |
| Manufacturing quality control | 0.01 | Cost of false positives is high | Sequential testing plans |
Interpreting Effect Sizes Across Study Architectures
Odds Ratio: The Universal Association Metric
The Odds Ratio $\text{OR} = \frac{a \cdot d}{b \cdot c}$ quantifies the strength of association between exposure and outcome. Its defining advantage is mathematical symmetry — the OR remains valid regardless of whether the sampling scheme conditions on exposure (cohort design) or on outcome (case-control design).
In retrospective case-control studies, the investigator selects subjects based on disease status, which means the marginal totals for outcome groups are fixed by design. Under these conditions, the population prevalence of disease is unknown, and Relative Risk cannot be validly estimated. The Odds Ratio is the only measure that remains mathematically correct because its value is invariant to the direction of conditioning.
When the outcome is rare (roughly < 10% prevalence in both groups), the OR closely approximates the RR — a relationship known as the rare-disease assumption. For common outcomes, the OR will systematically overestimate the RR when OR > 1 and underestimate it when OR < 1.
Relative Risk: Direct Probability Comparison
The Relative Risk $\text{RR} = \frac{a/R_1}{c/R_2}$ divides the probability of the outcome in Group 1 by the probability in Group 2. This metric is more clinically intuitive than the OR because it directly answers the question: "How many times more likely is the outcome in the exposed group?"
However, RR is only valid in prospective cohort studies and randomized controlled trials (RCTs) where the row totals ($R_1$, $R_2$) represent naturally sampled or randomized groups. Attempting to calculate RR from a case-control design produces a biased estimate that depends on the arbitrary sampling ratio of cases to controls.
Phi Coefficient: Standardized Correlation for Binary Data
The phi coefficient $\phi = \frac{ad - bc}{\sqrt{R_1 \cdot R_2 \cdot C_1 \cdot C_2}}$ is the Pearson product-moment correlation coefficient applied to two dichotomous variables. It ranges from $-1$ to $+1$ and provides a standardized effect size that is independent of sample size.
This metric complements the P-value by answering a distinct question: the P-value indicates whether an association exists, while $\phi$ indicates how strong that association is. A highly significant P-value with a near-zero $\phi$ suggests a statistically detectable but practically negligible relationship — a scenario that becomes increasingly common as $N$ grows large.
Frequently Asked Questions
The formal criterion is based on expected cell frequencies, not raw sample size. Fisher's Exact Test is required whenever more than 20% of expected cell frequencies are below 5, or any single expected cell frequency falls below 1. These expected values are calculated as $E_{ij} = \frac{R_i \cdot C_j}{N}$, meaning they depend on the distribution of observations across cells, not just the total count.
A study with $N = 200$ can still require Fisher's test if the marginals are highly imbalanced — for example, a 190/10 split across one variable would produce expected cells well below 5 in the sparse category. Conversely, a balanced study with $N = 20$ (five observations per cell) meets the chi-square assumptions adequately. The practical recommendation is to always compute expected values first, then select the appropriate test.
The "double the one-tailed value" shortcut assumes a symmetric probability distribution, which holds for the normal and $t$-distributions but generally fails for the hypergeometric distribution underlying Fisher's test. The hypergeometric distribution over a 2×2 table is discrete and can be markedly skewed when the marginal totals are unequal.
The correct two-tailed procedure sums the probabilities of all possible tables whose exact probability is less than or equal to the observed table's probability ($P(x) \leq P_{\text{obs}}$), regardless of which tail they fall in. Because the distribution may have a long right tail and a short left tail (or vice versa), the extreme tables on opposite sides carry different probability masses. The resulting two-tailed P-value is therefore computed as a sum over a non-contiguous set of table configurations, which typically does not equal twice any single tail.
Standard factorial computation fails at $N \approx 170$ because $171!$ exceeds $1.7976 \times 10^{308}$, the maximum value of a 64-bit IEEE 754 double-precision float. Any direct multiplication of factorials beyond this threshold returns infinity, rendering the exact probability incalculable in most browser-based or spreadsheet-based environments.
The log-gamma strategy converts all multiplications into additions in logarithmic space. Each factorial $k!$ is replaced by $\ln\Gamma(k+1)$, computed via the Lanczos approximation — a rational function series that estimates the gamma function to full double-precision accuracy using a small set of precomputed coefficients. The final probability is obtained by exponentiating the summed log-terms. Because logarithms compress the dynamic range (e.g., $\ln(1000!) \approx 5912.1$ rather than a number with 2568 digits), all intermediate values remain well within floating-point limits. This allows stable exact-test computation for tables where $N$ extends into the thousands or beyond.
Precision as the Foundation of Categorical Inference
Fisher's Exact Test occupies a unique position in the statistical toolkit: it is one of the few hypothesis tests that requires no distributional approximations whatsoever. The P-value it returns is not an estimate — it is the exact probability of observing data at least as extreme as the recorded table, given fixed marginal totals.
Performing this computation by hand is feasible only for the smallest tables. Even a modest $N = 50$ demands evaluating dozens of hypergeometric probabilities, each involving factorials that quickly exceed manual calculation capacity. Automated computation via log-gamma transforms eliminates both the arithmetic burden and the numerical overflow risk, delivering results that are accurate to machine precision regardless of sample dimensions.
The combination of exact P-values, odds ratios for universal applicability, relative risk for prospective designs, and the phi coefficient for standardized effect size provides a comprehensive analytical profile from a single 2×2 table. This integrated output enables researchers and analysts to move directly from raw contingency data to defensible statistical conclusions — without the methodological compromises inherent in asymptotic approximation methods.