Fisher's exact test decides whether two yes/no variables are associated in a 2 x 2 table without relying on a large-sample approximation. It takes the row and column totals as fixed and asks how likely the observed arrangement, or a more extreme one, would be if the two variables were unrelated. Because it enumerates the possibilities directly, it is the standard choice when counts are small.
The logic in three steps:
The probability of one table. Label the table cells $a,b,c,d$ with row totals $r_1=a+b$, $r_2=c+d$, column totals $c_1=a+c$ and $c_2=b+d$, and grand total $N$. If rows and columns are unrelated, the number $a$ in the top-left cell follows a hypergeometric distribution, the same as drawing $c_1$ items without replacement from $N$ items of which $r_1$ are marked:
The p-value adds up the probabilities of all tables, with the same totals, that are no more probable than the observed one (two-sided), or at least as extreme in one direction (one-sided).
Worked example. Tool X ran $9$ wafers and tool Y ran $7$. Eight of the sixteen wafers failed: $7$ on X and only $1$ on Y, so the table is $\begin{pmatrix}7&2\\1&6\end{pmatrix}$. The probability of exactly this table is $\binom{9}{7}\binom{7}{1}/\binom{16}{8}=252/12870\approx0.0196$. The tables with $8$ failures on X have probability $0.0007$, so the one-sided p-value is $0.0196+0.0007=0.0203$. In the other direction, $1$ and $2$ failures on X have probabilities $0.0007$ and $0.0196$, so the two-sided p-value is $0.0007+0.0196+0.0196+0.0007\approx0.0406$. These match scipy's exact test, and the sample odds ratio is $(7\cdot6)/(2\cdot1)=21$.
Why not chi-square? The expected counts under independence here are $4.5,\,4.5,\,3.5,\,3.5$, all below the usual minimum of $5$. In that region the chi-square approximation is unreliable: the uncorrected test gives $p=0.0117$, which looks much stronger than the exact $0.0406$, and the Yates-corrected version gives $0.0438$. The exact test never overstates the evidence this way.
Things to know.
- Fixed margins: the test conditions on the row and column totals. This is natural when both are fixed by design, and it is still the standard approach when they are not.
- Conservative: because the distribution is discrete, the actual false-positive rate is often a bit below the nominal $5\%$. Mid-p variants reduce this at the cost of exactness.
- Two-sided choice: definitions differ. The common one sums all tables no more probable than the observed one, as above, but doubling the one-sided p-value can give a slightly different answer.
- Larger tables: the Freeman-Halton extension handles $r\times c$ tables, but the computation grows quickly, so software may use simulation.
- Report an effect size: give the odds ratio with an exact confidence interval, not just the p-value. A very small sample can produce a tiny p-value from a lopsided table by chance and still leave a wide interval.
- Independence: each wafer must appear once, and outcomes must be independent. Wafers from the same lot share conditions, so treat lot as a factor if it matters.
Read Fisher's exact test as an actual count rather than an approximation: of all the ways the failures could fall on the two tools, how rare is a split as lopsided as the one observed?