Methodology — How Every Number on This Site Is Computed
The full engine specification in public form: the hypergeometric enumeration, the named two-sided definition, both odds ratios, the exact interval, the Freeman–Halton limits, and the canonical figures every calculator must reproduce.
The 2×2 engine — hypergeometric enumeration
For a 2×2 table [[a, b], [c, d]] the row totals are r1 = a + b and r2 = c + d. The column totals are c1 = a + c and c2 = b + d, and N is the grand total. The test conditions on the observed margins. With the margins fixed, the top-left cell x determines the whole table, and under the null hypothesis of independence x follows a hypergeometric distribution: P(x) = C(r1, x) · C(r2, c1 − x) ÷ C(N, c1), for x from max(0, c1 − r2) to min(r1, c1). Equivalently, the probability of the observed table is (a + b)! (c + d)! (a + c)! (b + d)! ÷ (a! b! c! d! N!).
The engine enumerates every allowed value of x, computes each table's probability from that formula, and sums the probabilities the chosen alternative counts. The one-sided p-value for the greater direction is the sum of P(x) over x ≥ a; for the less direction, the sum over x ≤ a. The probabilities of all enumerated tables sum to 1, and the enumeration table on the homepage shows them so the sum can be audited. A table with a zero margin allows only one table, so no test exists for it; the calculator says so in words rather than printing p = 1 as if it were a result.
Display rounding follows one rule everywhere: headline p-values are shown to 4 decimal places, enumeration probabilities to 6, expected counts to 2, and chi-square and G statistics to 3. Odds ratios and interval bounds are shown at 4 significant figures in a result block and 3 in prose. A p-value below 0.0001 is shown in scientific notation, or as p < .0001 in an APA sentence — never as p = .000. Numbers are computed at full precision and rounded only for display; a rounded display value is never fed back into a later calculation.
The two-sided definition — probability ordering
A two-sided p-value needs a rule for which tables count as at least as extreme as the observed one, and more than one rule exists. This site uses probability ordering: sum the probabilities of every table whose probability is less than or equal to the probability of the observed table. That is the definition used by R's fisher.test and by SciPy's fisher_exact, and it is the definition named next to every two-sided p-value we print. Borderline tables are counted the way R counts them, using the same relative tolerance of 1 + 1e-7 on the observed table's probability.
The main alternative, doubling the smaller one-sided tail, is never used here as a result. It is named only where we explain why two calculators can disagree. On the teaching table [[8, 2], [1, 5]], probability ordering gives a two-sided p of 0.0350 while doubling gives 0.0490. That difference crosses the α = 0.05 line. It is why the definition has to be named rather than assumed. The optional mid-p value is a separate, less conservative variant: the corresponding p-value minus half the observed table's probability. It is labelled mid-p wherever it appears and is never the headline number.
Odds ratios and the exact confidence interval
Two odds ratios exist for a 2×2 table and this site prints both, labelled. The sample odds ratio is the cross-product, ad ÷ bc. The conditional-MLE odds ratio is the value θ that maximises the noncentral hypergeometric likelihood — equivalently, the θ under which the expected value of the top-left cell equals the observed a, where each table's probability is weighted by θx. The conditional-MLE estimate is the one R reports. When a is the smallest possible value of x the conditional MLE is 0; when a is the largest, it is infinite; the result says so rather than printing a placeholder.
The exact confidence interval is computed by Cornfield inversion. The lower bound is the θ at which Pθ(X ≥ a) equals α ÷ 2. The upper bound is the θ at which Pθ(X ≤ a) equals α ÷ 2. At a boundary the bound is one-sided, as R computes them. Only this interval is called exact. The Woolf interval — the log odds ratio plus or minus z times the square root of (1/a + 1/b + 1/c + 1/d), exponentiated — is shown only where it is labelled approximate (Woolf), and only when all four cells are above zero.
Zero cells are handled in the open. If b or c is zero, the sample odds ratio is infinite or zero and the result leads with the conditional-MLE estimate and the exact interval, saying in words why. No silent correction is ever applied: an odds ratio computed with the Haldane–Anscombe correction of adding 0.5 to each cell appears only as an explicitly labelled extra. On the odds ratio calculator, relative risk is [a ÷ (a + b)] ÷ [c ÷ (c + d)]. Risk difference is the difference of the two risks. NNT is the reciprocal of the absolute risk difference. It is always rounded up to the next whole person, and reported as NNH when the risk difference is negative. For a case-control table those risk-based measures are withheld, with the reason printed: a case-control design fixes the case-to-control ratio by sampling, so risks cannot be estimated from it.
Cochran's rule and the comparison statistics
Expected counts are computed as E = (row total × column total) ÷ N for each cell. Whether the chi-square approximation can be trusted is judged by Cochran's rule in its full two-part form (Cochran, 1954): all expected counts at least 1, and at least 80 percent of cells with an expected count of at least 5. For a 2×2 table that second clause means all four cells. On the teaching table [[8, 2], [1, 5]] the expected counts are 5.625, 4.375, 3.375 and 2.625. So only 1 of the 4 cells reaches 5. The rule is not met, and the exact test — not chi-square — is the result to trust for that table. Every calculator on this site runs the check on the user's own expected counts and prints the verdict with the result.
The comparison statistics shown beside the exact p-value are labelled approximations. Pearson's chi-square is the sum of (O − E)² ÷ E over the cells, with 1 degree of freedom for a 2×2 table. The Yates-corrected form subtracts 0.5 from each |O − E| before squaring. The G statistic is 2 times the sum of O · ln(O ÷ E), with terms where O = 0 contributing 0. Their p-values come from the chi-square distribution with the appropriate degrees of freedom — for larger tables, (rows − 1) × (columns − 1). Cramér's V, phi and adjusted residuals on the table battery pages are computed by their standard formulas and named on those pages.
Freeman–Halton limits and the Monte Carlo labelling policy
For tables larger than 2×2, the Freeman–Halton calculator generalises the same idea: enumerate every table with the observed row and column margins, compute each table's multivariate hypergeometric probability, and sum the probabilities of the tables no more probable than the observed one. The enumeration follows the network algorithm of Mehta and Patel (1983), the standard method for making this enumeration feasible, and the extension itself is Freeman and Halton's (1951). No single odds ratio is computed for an r×c table, because none exists.
Exactness has stated limits, printed on the tool itself: tables up to 5×5 with a grand total N of at most 200 are enumerated exactly. Beyond those limits the tool offers a Monte Carlo estimate. The result is labelled in the headline itself — Simulated p-value (Monte Carlo, 10,000 replicates). The word exact never appears on a simulated result, and the replicate count is always part of the result. A reader can test the label: an exact p-value is identical on every run, while a simulated one moves slightly between runs. The same labelling rule binds every page and every guide on this site, without exception.
McNemar — the exact binomial definition
McNemar's test (McNemar, 1947) is the paired-data counterpart, and it uses only the discordant pairs: b pairs that changed one way and c pairs that changed the other. The concordant pairs carry no information about change and enter none of the statistics. The chi-square form without correction is (b − c)² ÷ (b + c); with the Edwards continuity correction it is (|b − c| − 1)² ÷ (b + c); both are printed, as a choice, never as a silent default. The exact form is a two-sided binomial test of min(b, c) against a binomial distribution with b + c trials and probability 0.5. The paired odds ratio is b ÷ c. As conventional guidance — stated as guidance, not law — the exact form is the one to report when b + c is below 25, and the McNemar calculator names the form that fits the user's own discordant total.
Verification — the canonical check figures
These are the contract figures. The engine was checked against SciPy's fisher_exact and against an independent exact enumeration. It must reproduce every value in this table. If the engine and this table ever disagree, the table is right and the engine gets fixed. Two-sided p-values are by probability ordering throughout.
| Drug trial (teaching example) [[8, 2], [1, 5]] | 0.0350 | 20.00 | 15.47 | [1.009, 1059.6] |
|---|---|---|---|---|
| Lady tasting tea (Fisher, 1935) [[3, 1], [1, 3]] | 0.4857 | 9.00 | 6.408 | [0.212, 626.2] |
| NIST worked example WE3 [[2, 5], [3, 2]] | 0.5581 | 0.2667 | 0.3006 | [0.0132, 4.923] |
| Worked example [[8, 2], [1, 9]] | 0.00548 | 36 | 27.33 | [2.058, 1767.0] |
| Freeman–Halton 2×3 [[4, 4, 2], [1, 4, 5]] — 42 tables | 0.2892 | — | — | — |
| McNemar check case (b = 25, c = 8) — exact binomial p | 0.0046 | — | 3.125 (paired OR) | — |
Sources
Fisher, R. A. (1922), on the interpretation of chi-square from contingency tables; Fisher, R. A. (1935), The Design of Experiments — the lady tasting tea. Freeman, G. H. and Halton, J. H. (1951), the extension to larger tables. Mehta, C. R. and Patel, N. R. (1983), the network algorithm for exact enumeration. Cornfield, J., the exact confidence interval by inversion. Cochran, W. G. (1954), the expected-count rule. McNemar, Q. (1947), the paired test. Yates, F. (1934), the continuity correction. The NIST/SEMATECH e-Handbook of Statistical Methods (worked example WE3), itl.nist.gov/div898/handbook. Software behaviour is documented in the R reference for fisher.test and the SciPy reference for scipy.stats.fisher_exact.