Skip to content

Fisher's exact test

CatScorer.fisher(), CatScorer.fisher_pvalue() · symmetric · statistical

Fisher's exact test (Fisher, 1922) tests the independence of the two rows of a 2×2 table without the large-sample approximation of chi-square. It is the test to use when counts are small. fisher returns the sample odds ratio of the table and fisher_pvalue the two-sided p-value of the test, both from SciPy's fisher_exact.

Definition

For the table

\(y\) not \(y\)
\(x\) \(a\) \(b\)
not \(x\) \(c\) \(d\)

the odds ratio is

\[ \mathrm{OR} = \frac{a\,d}{b\,c}: \]

the odds of \(y\) when \(x\) is present, divided by the odds of \(y\) when it is not. \(\mathrm{OR} > 1\) means attraction and \(\mathrm{OR} < 1\) avoidance. The p-value is the probability, with the margins of the table fixed, of a table at least as unlikely as the one observed. Both are symmetric, so both tuple entries are equal.

Example

With the example data:

>>> from asymcat import CatScorer
>>> pairs = [("q", "k")] * 2 + [("c", "k")] * 6 + [("k", "k")] * 3 + [("c", "s")] * 4
>>> scorer = CatScorer(pairs)
>>> scorer.fisher()[("c", "s")], scorer.fisher()[("c", "k")]
((inf, inf), (0.0, 0.0))
>>> [round(p, 3) for p in scorer.fisher_pvalue()[("c", "s")]]
[0.231, 0.231]

Every /s/ is spelled c, so the table of c and /s/ has \(c = 0\) and an infinite odds ratio; the table of c and /k/ has \(d = 0\) and an odds ratio of 0. Even so, with 15 observations the association is not significant (\(p = 0.23\)).

Properties

  • Exact. The p-value does not rely on large counts, which makes it the safest of the statistical tests for sparse data.
  • Signed through the odds ratio, around 1 rather than 0. Its logarithm is symmetric around 0.
  • Infinite and zero odds ratios. Any empty cell gives \(\mathrm{OR} = 0\) or \(\infty\), which is common with categorical data. Such values break scale_scorer; rank by the p-value or use the smoothed conditional probability instead.
  • Many tests. See Test significance.

API

fisher

fisher() -> dict[tuple[Any, Any], tuple[float, float]]

Return a Fisher Exact Odds Ratio scorer.

For each pair, this is the sample odds ratio (a * d) / (b * c) of the 2x2 contingency table, as returned by scipy.stats.fisher_exact. It is the unconditional maximum-likelihood estimate; R's fisher.test reports the conditional one, which differs slightly. A table with an empty cell gives an odds ratio of 0 or inf. Both tuple entries hold the same value; the p-values are given by fisher_pvalue.

Returns:

Type Description
Dict[Tuple[Any, Any], Tuple[float, float]]

Dictionary mapping symbol pairs to (odds ratio, odds ratio) tuples.

fisher_pvalue

fisher_pvalue() -> dict[tuple[Any, Any], tuple[float, float]]

Return the p-values of Fisher's exact test.

This is the significance counterpart to fisher: for each pair it returns the (two-sided) p-value from scipy.stats.fisher_exact over the 2x2 contingency table. As with the other symmetric statistical tests, both tuple entries hold the same value.

Returns:

Type Description
Dict[Tuple[Any, Any], Tuple[float, float]]

Dictionary mapping symbol pairs to (p-value, p-value) tuples.

References