Guides
How to Calculate Fisher's Exact Test by Hand — Formula, Steps & Worked Example
How to calculate Fisher's exact test by hand in five steps: fix the margins, compute the observed table's probability, enumerate every same-margin table, and sum for the p-value.
To calculate Fisher’s exact test by hand, you fix the margins of your 2×2 table, compute the hypergeometric probability of the table you observed, list every other table those margins allow, and add up the probabilities of the tables the test counts. The one-sided p-value sums one tail; the two-sided p-value sums every table no more probable than yours. This guide carries one table — [[8, 2], [1, 9]] — through all five steps with every number shown, so you can reproduce the method on any small table and then check your answer in the Fisher exact test calculator.
Quick answer — the five steps
- Write the table and fix the margins. Add up each row and each column. Those totals stay fixed for everything that follows.
- Compute the probability of the observed table with the hypergeometric formula below.
- Enumerate every table with the same margins. Vary the top-left cell across its allowed range; each value gives one complete table.
- Sum for the one-sided p-value. Add the probabilities of your table and every table further out in the direction you specified in advance.
- Sum for the two-sided p-value. Add the probabilities of every table whose probability is less than or equal to your table’s probability. This is probability ordering, the definition R and SciPy use.
If you only wanted the number, the calculator does all five steps instantly. The rest of this guide is for showing the working.
What you need before you start
You need a 2×2 contingency table of counts — four whole numbers, not percentages, rates or means. Fisher’s exact test works on how many observations fell in each cell; a percentage has thrown away the sample size, and the sample size is exactly what decides how surprising a table is. You also need the null hypothesis stated plainly: the two classifications are independent, so any pattern in the table is the kind chance produces. And you need your tail and significance level, α, chosen before you see the p-value — one-sided or two-sided, and α = 0.05 unless you have a reason recorded in advance.
Two assumptions ride along. The observations must be independent of each other: one subject contributing twice, or matched pairs measured before and after, breaks the test — paired data belongs to McNemar’s test instead. And the counts must be actual counts of observations, in a table where each observation lands in exactly one cell. Small counts are not a problem; they are the point. Fisher’s exact test exists for tables too small for the chi-square approximation, which is why the comparison with chi-square turns on expected counts rather than on the total.
The formula
The probability of one specific table, with its margins fixed, is a hypergeometric probability:
P = C(a + b, a) · C(c + d, c) ÷ C(N, a + c)
where a, b, c and d are the four cells read left to right, top to bottom, and N is the grand total. The same probability written with factorials is:
P = (a + b)! (c + d)! (a + c)! (b + d)! ÷ (a! b! c! d! N!)
| Symbol | Meaning |
|---|---|
| a | top-left cell |
| b | top-right cell |
| c | bottom-left cell |
| d | bottom-right cell |
| a + b, c + d | the two row totals |
| a + c, b + d | the two column totals |
| N | grand total, a + b + c + d |
| C(n, k) | binomial coefficient — the number of ways to choose k from n |
Why hypergeometric? Fix the column totals: exactly a + c of the N observations fall in column 1. Under the null hypothesis, which of them land in row 1 is a draw without replacement — row 1 takes a + b draws from the N, and a of the column-1 observations end up there. Drawing without replacement from a fixed pool is precisely the hypergeometric distribution, and that is all the formula says. Every p-value in Fisher’s exact test is a sum of these table probabilities; there is no other machinery.
Step 1 — Write the table and fix the margins
Our worked example is a constructed teaching table: a treatment group and a control group, scored improved or not improved.
| Improved | Not improved | Row total | |
|---|---|---|---|
| Treatment | 8 | 2 | 10 |
| Control | 1 | 9 | 10 |
| Column total | 9 | 11 | N = 20 |
So a = 8, b = 2, c = 1, d = 9, with row totals 10 and 10, column totals 9 and 11. Fixing the margins means this: for the rest of the calculation, the totals 10, 10, 9, 11 and 20 are treated as given, and the only question is how the counts could have been arranged inside them. Conditioning on the margins is what makes the null distribution exact rather than approximate — the margins carry no information about association, so holding them fixed removes them from the problem.
One consequence does most of the work in this method. With both margins fixed, the whole table has one free cell: pick the top-left cell x, and the other three are forced — b = 10 − x, c = 9 − x, d = 1 + x. That is why enumeration in Step 3 is a single short list instead of a search.
Step 2 — Compute the probability of the observed table
Apply the formula to the observed table, a = 8:
P = C(10, 8) · C(10, 1) ÷ C(20, 9) = 45 · 10 ÷ 167,960 = 450 ÷ 167,960 = 0.002679
Read it literally: if treatment and improvement were independent, with these margins, the chance of this exact arrangement is about 0.00268 — roughly 27 in 10,000. That is the probability of the observed table itself. It is not yet a p-value. A p-value adds the probabilities of tables at least as extreme as yours, which is what the enumeration is for.
Step 3 — Enumerate every table with the same margins
The top-left cell x can range from max(0, 9 − 10) = 0 to min(10, 9) = 9, so there are exactly ten tables with these margins. Each one’s probability comes from the same formula, P(x) = C(10, x) · C(10, 9 − x) ÷ C(20, 9):
| x | Table | P(x) | Counted for two-sided p? |
|---|---|---|---|
| 0 | [[0, 10], [9, 1]] | 0.000060 | Yes |
| 1 | [[1, 9], [8, 2]] | 0.002679 | Yes |
| 2 | [[2, 8], [7, 3]] | 0.032151 | No |
| 3 | [[3, 7], [6, 4]] | 0.150036 | No |
| 4 | [[4, 6], [5, 5]] | 0.315075 | No |
| 5 | [[5, 5], [4, 6]] | 0.315075 | No |
| 6 | [[6, 4], [3, 7]] | 0.150036 | No |
| 7 | [[7, 3], [2, 8]] | 0.032151 | No |
| 8 | [[8, 2], [1, 9]] — observed | 0.002679 | Yes |
| 9 | [[9, 1], [0, 10]] | 0.000060 | Yes |
Add the probability column: 0.000060 + 0.002679 + 0.032151 + 0.150036 + 0.315075 + 0.315075 + 0.150036 + 0.032151 + 0.002679 + 0.000060 = 1.000000. The column summing to 1 is your self-check — these are all the tables there are, so their probabilities must exhaust the whole. If your column does not sum to 1, an arithmetic slip is hiding in it, and it is cheaper to find now than after the sums that follow.
Step 4 — Sum for the one-sided p-value
Suppose the direction was specified in advance: the treatment was expected to raise the improvement count, so the alternative is an odds ratio above 1, and the tail is the tables with x at or beyond the observed 8 in that direction. Sum those probabilities:
p (greater) = P(8) + P(9) = 0.002679 + 0.000060 = 0.002739
The other one-sided p-value sums the opposite tail, x ≤ 8, and comes to 0.999940 — as it must, since the observed table sits near the top of the range. Report a one-sided p-value only when the direction genuinely was fixed before the data were seen; choosing the tail that looks better after the fact quietly doubles the false-positive rate the test was supposed to control. Our interpretation guide covers that choice in full.
Step 5 — Sum for the two-sided p-value (probability ordering)
The two-sided question is not “both tails” in the sense of doubling. By probability ordering — the definition R’s fisher.test and SciPy use, and the one this site prints — a table counts toward the two-sided p-value if its probability is less than or equal to the observed table’s probability, 0.002679. From the enumeration, those are x = 0, 1, 8 and 9:
p (two-sided) = 0.000060 + 0.002679 + 0.002679 + 0.000060 = 0.005477, reported as 0.00548
Doubling the one-sided value instead would give 2 × 0.002739 = 0.005478 — here the two methods nearly agree, because this enumeration is almost symmetric. They do not always agree: on the table [[8, 2], [1, 5]], probability ordering gives 0.0350 while doubling gives 0.0490, a difference big enough to matter at α = 0.05. That gap is the most common reason two calculators disagree, and it is why the definition has to be named next to the number. The worked example is now complete: two-sided p = 0.00548, one-sided p = 0.002739, sample odds ratio = (8 × 9) ÷ (2 × 1) = 36.
Fisher’s exact test worked example
The lady tasting tea
The famous first example is small enough to finish entirely by hand, and it is a documented classic rather than a constructed one. In Fisher’s The Design of Experiments (1935), Muriel Bristol claimed she could tell whether milk or tea had been poured first. Eight cups were prepared, four each way, and in Fisher’s worked exposition she identified three of the four cups of each kind correctly:
| Called milk-first | Called tea-first | Row total | |
|---|---|---|---|
| Actually milk-first | 3 | 1 | 4 |
| Actually tea-first | 1 | 3 | 4 |
| Column total | 4 | 4 | N = 8 |
The margins are 4, 4, 4, 4, so x runs from 0 to 4 and C(8, 4) = 70. The five table probabilities are 1/70, 16/70, 36/70, 16/70 and 1/70 — that is 0.014286, 0.228571, 0.514286, 0.228571 and 0.014286. The observed table has x = 3, so P = 16/70 = 0.228571. The one-sided p-value is (16 + 1) ÷ 70 = 0.242857. The two-sided p-value counts every table with probability at most 0.228571 — all of them except the middle one — giving 34/70 = 0.485714, reported as 0.4857. Not significant, and honestly so: with eight cups, even a strong-looking performance is well within what guessing produces. (Salsburg later reported that Bristol in fact identified all eight cups correctly; that perfect table, [[4, 0], [0, 4]], has a two-sided p of 0.028571. The two accounts are different sources and are not merged here.)
Check your by-hand answer in the calculator
Both worked tables on this page are one click away from verification. The Fisher exact test calculator opens with the teaching table [[8, 2], [1, 5]], and its presets include the lady tasting tea table [[3, 1], [1, 3]] and the NIST worked example [[2, 5], [3, 2]]. Type the worked-example table [[8, 2], [1, 9]] into the grid and the calculator shows the same enumeration you built here — every same-margin table, its probability, and which ones the two-sided sum counts — alongside two-sided p = 0.00548, the sample odds ratio of 36, the conditional-MLE odds ratio of 27.33, and the exact 95% confidence interval [2.058, 1767.0]. If your hand sum and the calculator ever disagree, the enumeration table is where the difference will be visible.
How to do it in Excel, R and Python
Excel has no Fisher function, but it has the building block: HYPGEOM.DIST(x, row total, column-1 total, N, FALSE) returns one table’s probability. For the worked example, =HYPGEOM.DIST(8, 10, 9, 20, FALSE) returns 0.002679. Compute it for every x from 0 to 9 and sum the rows your alternative counts — the same enumeration as Step 3, in spreadsheet form.
R does the whole test in one call, and reports the conditional-MLE odds ratio with the Cornfield exact interval:
fisher.test(matrix(c(8, 1, 2, 9), nrow = 2))
# two-sided p = 0.005477, conditional-MLE odds ratio 27.33
Note that R fills the matrix down columns, so the vector is read as [[8, 2], [1, 9]].
Python with SciPy is equally direct, but reports the sample odds ratio instead:
from scipy.stats import fisher_exact
fisher_exact([[8, 2], [1, 9]])
# sample odds ratio 36.0, two-sided p = 0.005477
The two packages agree on the p-value because both use probability ordering. Their odds ratios differ because they estimate different quantities — ad/bc against the conditional maximum likelihood — and neither is wrong. Our methodology page states both definitions in full.
Common by-hand mistakes
- Reporting the observed table’s probability as the p-value. P = 0.002679 is one ingredient. The p-value is the sum over the counted tables, 0.005477.
- Doubling the one-sided p and calling it the two-sided p. Sometimes close, sometimes not — 0.0350 against 0.0490 on [[8, 2], [1, 5]]. Sum by probability ordering instead.
- Entering percentages. The test needs the four counts. Two tables with identical percentages and different totals have different p-values, correctly.
- Enumerating the wrong range. x runs from max(0, c₁ − r₂) to min(r₁, c₁), not from 0 to N. A probability column that does not sum to 1 is the tell.
- Choosing the tail after seeing the result. The direction is part of the design. Decided afterwards, it is not a one-sided test any more.
- Using Fisher on paired data. Before-and-after counts on the same subjects need McNemar’s test; Fisher assumes independent observations.
Frequently asked questions
How do I calculate Fisher’s exact test by hand?
Fix the margins of your 2×2 table, compute the observed table’s hypergeometric probability, enumerate every table with the same margins by varying the top-left cell, then sum: one tail for a one-sided p-value, or every table no more probable than yours for the two-sided p-value by probability ordering. The five steps above carry a full table through the whole method.
What is the formula for Fisher’s exact test?
The probability of one table is P = C(a + b, a) · C(c + d, c) ÷ C(N, a + c), equivalently (a + b)! (c + d)! (a + c)! (b + d)! ÷ (a! b! c! d! N!). Every p-value is a sum of these table probabilities over the tables the chosen alternative counts.
Why do you fix the margins in Fisher’s exact test?
Because the margins carry no information about association, conditioning on them removes two nuisance quantities and leaves a null distribution that can be computed exactly — the hypergeometric. It is also what makes the table have one free cell, so the enumeration is a short list rather than a search.
Why is the two-sided p-value not just double the one-sided one?
Because the enumeration is rarely symmetric. Probability ordering counts tables by how probable they are, not by distance in a tail; doubling assumes the far tail mirrors the near one. On [[8, 2], [1, 5]] the two methods give 0.0350 and 0.0490 — a material difference at α = 0.05.
What does ‘exact’ mean here?
That the null distribution is computed exactly, by enumeration, rather than approximated by a continuous distribution the way chi-square approximates it. Exact does not mean the result is exactly right for your study, or assumption-free — the test still assumes independent observations and counts rather than percentages.
How do I do Fisher’s exact test in Excel?
There is no Fisher function in Excel. Use HYPGEOM.DIST to get each same-margin table’s probability — HYPGEOM.DIST(x, row total, column-1 total, N, FALSE) — for every allowed x, then sum the probabilities your alternative counts, exactly as in Step 3 of this guide.
What if a cell is zero?
The test still works: enumerate as usual. What changes is the odds ratio — with b or c equal to zero the sample odds ratio is infinite or zero, so report the conditional-MLE odds ratio and the exact confidence interval instead, and never patch the table with a silent correction.
What is the conditional-MLE odds ratio, and why is it not ad/bc?
ad/bc is the sample odds ratio, computed straight from your four counts. The conditional-MLE odds ratio is the value of the odds-ratio parameter under which your observed table is the expected outcome of the noncentral hypergeometric distribution — it is the estimate R reports. They are different estimators of the same quantity, which is why this site prints both, labelled.
When do I use Fisher’s exact test instead of chi-square?
When the expected counts are small — judged by Cochran’s rule: all expected counts at least 1, and at least 80 percent of cells with an expected count of at least 5. If the rule fails, the chi-square approximation cannot be trusted and the exact test is the one to read. The decision is worked through in our Fisher vs chi-square guide.
Related calculators and pages
- Fisher exact test calculator — the five steps above, computed instantly, with the enumeration shown.
- Fisher’s exact test vs chi-square — which test your expected counts call for.
- Fisher’s exact test interpretation — what the result means and how to report it.
- Odds ratio calculator — the effect sizes for the same table, each with its interval.
- Freeman–Halton calculator — the exact test for tables larger than 2×2.
- Methodology — every definition on this site, stated in full.