Fisher's Exact Test Calculator
Introduction: Fisher's Exact Test for Small 2x2 Tables
Fisher's exact test gives an exact probability calculation for contingency tables, unlike the more common test which relies on an approximation that is most reliable when expected counts are large enough. When the table is sparse, the chi-square approximation can give a p-value that is too loose for careful work. R.A. Fisher addressed that problem in 1935 by deriving an exact expression based on the hypergeometric distribution. The test considers all possible tables with fixed margins and calculates the probability of obtaining one as extreme as or more extreme than the observed configuration.
The computation rests on treating the row and column totals as fixed for the Fisher's exact test table. Suppose we observe the following layout. The rows might correspond to treatment and control groups, while the columns represent success and failure outcomes. Using the notation , , , and for the four cells, the margins are , , , and . Under the null hypothesis that the treatment and outcome are independent, the number appearing in the first cell follows a hypergeometric distribution with parameters , , and where .
| Outcome 1 | Outcome 2 | Row Totals | |
|---|---|---|---|
| Group 1 | a | b | a+b |
| Group 2 | c | d | c+d |
| Column Totals | a+c | b+d | n |
In this Fisher's exact test representation the probability of observing the particular table above, conditional on the margins, equals . This expression arises by counting the number of arrangements leading to the specified table and dividing by the total number of tables with the given margins. Because factorials of even modestly large integers explode in magnitude, the calculation in the script employs logarithms of factorials to avoid overflow, exponentiating only at the end to recover the probability.
To perform a two‑tailed Fisher's exact test, we compute the probability of the observed table and then enumerate all tables with the same margins, summing the probabilities of those whose probability is less than or equal to the observed value. This criterion mirrors Fisher's original "two‑sided" definition and ensures that extreme outcomes in either direction contribute to the p‑value. Alternative definitions of a two‑tailed Fisher test exist, but this method is common in statistical software and textbooks. The calculator reports the exact p‑value without invoking normal or chi‑square approximations.
Worked Example: Fisher's Exact Test for a Small Trial
As a worked Fisher's exact test example, consider a small clinical trial in which ten patients receive a new therapy and ten receive a placebo. Suppose three treated patients and one control patient experience symptom relief. The table therefore has and in the first row, and and in the second row. Plugging these counts into the formula above gives the probability of observing that table under the null hypothesis of no treatment effect. But to determine the p‑value we must also include tables with probabilities equal to or lower than that of the observed table. Those tables correspond to more lopsided outcomes such as four successes in the treated group and none in the control group. Summing their probabilities yields the exact p‑value.
In the example the observed configuration has probability approximately . Only one other table with the same margins is as or more extreme, the one with four successes in the treatment group. Its probability is . Therefore the two‑tailed p‑value is the sum . Although the treatment seems to have helped more patients, the p‑value suggests that such a difference could easily arise by chance when the sample size is this small. Larger studies are needed to draw firm conclusions.
Historical Context: Fisher's Exact Test and the Origins of Exact Inference
Fisher developed this exact test while working at the Rothamsted Experimental Station in England, a hub for agricultural research where small samples were common. His 1935 monograph "The Design of Experiments" introduced randomization and many other foundational ideas in statistics. The exact test arose from analyzing experiments with very small numbers of observations where existing approximate techniques failed. One famous anecdote describes a colleague claiming she could tell whether milk or tea was poured first into a cup. Fisher designed an eight‑cup test to evaluate her claim, leading to the formalization of the hypergeometric model. The logic of fixed margins and exact probability enumeration traces back to this playful experiment.
While conceived for agriculture and tea tasting, Fisher's exact test now permeates disciplines from medicine to ecology. Clinical trials with rare outcomes, genetics studies with small sample sizes, and laboratory experiments with limited resources all benefit from a method that remains valid regardless of cell counts. Software packages in R, Python, and other languages include built‑in implementations. This calculator offers the same capability in a lightweight, browser‑based form requiring no internet connection once loaded.
Formula: How the Fisher Exact Test Is Enumerated
This Fisher's exact test calculator uses a straightforward enumeration algorithm. After reading the four inputs it determines the possible range of the upper left cell consistent with the margins. Specifically, can vary between and . For each feasible it constructs the full table, computes its hypergeometric probability, and compares it to the probability of the observed table. Probabilities smaller than or equal to the observed one are accumulated into the p‑value. Because all computations occur in log space until the final exponentiation, the method remains stable even for moderately large totals such as 100 or 200.
The function logFact iteratively sums logarithms using JavaScript's Math.log. While this is less efficient than using a precomputed table or Lanczos approximation, it suffices for the small values typically entered by users. The algorithm terminates quickly, with a worst‑case complexity proportional to the range of . For most realistic inputs the calculation completes instantly on modern devices.
Interpretation and Practical Advice for Fisher's Exact Test
A small p-value from Fisher's exact test means that under the assumption of independence, the observed table or one more extreme would be unlikely. Many practitioners use a threshold such as to describe "statistical significance." However, p-values should not be interpreted mechanically. Consider the study design, potential biases, and the magnitude of the observed effect. The exact test does not measure effect size; it only gauges compatibility with the null hypothesis. To describe the association strength, analysts often compute the odds ratio or its logarithm, along with confidence intervals.
Another important nuance in Fisher's exact test is the choice between one‑ and two‑tailed results. If a research question predicts a specific direction of association—for instance, that the treatment can only increase the success rate—a one‑tailed test may be appropriate. The algorithm in this calculator uses the two‑tailed approach by default because it is more conservative and widely applicable. Researchers should match the tail specification to their hypotheses and justify the choice in reporting results.
Beyond 2x2 Tables and Fisher's Exact Test
Fisher's exact-test logic extends in principle to larger tables, but the computational burden escalates rapidly as the number of cells grows. For a table, the enumeration involves summing over many more configurations, and for an table it becomes impractical. Specialized algorithms such as network algorithms or Markov chain Monte Carlo methods are employed to approximate exact p-values in such settings. Nonetheless, the case remains the most common, especially in clinical research, making a dedicated calculator valuable.
Fisher's exact test holds an esteemed place in the history of statistics, illustrating how clever probability reasoning can circumvent limitations of asymptotic approximations. Its enduring popularity stems from its simplicity and reliability. Whether you are assessing the efficacy of a new drug, checking for a gender bias in small hiring datasets, or performing classroom demonstrations, this tool provides a transparent and precise method for evaluating independence in small samples.
How to use this Fisher's Exact Test Calculator
- Enter Cell a (row 1, column 1) using the count for the upper-left cell of your 2x2 table.
- Enter Cell b (row 1, column 2) using the count for the upper-right cell of the same table.
- Enter Cell c (row 2, column 1) using the count for the lower-left cell.
- Run the Fisher's exact test calculation, then compare the exact p-value with any alternate 2x2 table or grouping you are considering before you draw a conclusion.
Limitations and assumptions for Fisher's Exact Test
Fisher's exact test is exact for the counts you enter, but the answer still depends on correct cell values and on whether the rows and columns represent the comparison you intend. If your data are rounded, recoded, or built from mixed definitions, the p-value may be mathematically correct for the wrong table. This calculator follows the two-tailed convention described above, so make sure that matches your analysis plan before you report the result. It does not estimate effect size, adjust for covariates, or replace a fuller statistical review when the study question calls for one.
Arcade Mini-Game: Fisher's Exact Test Calculator Calibration Run
Use this quick arcade run to practice separating useful scenario inputs from common planning mistakes before you rely on the calculator output.
Start the game, then use your pointer or arrow keys to catch useful inputs and avoid bad assumptions.
