Stats 0.1.0 fisherexact test - CyrilB1531/lodestar GitHub Wiki

Lodestar.Stats 0.1.0. This page is frozen at that release. Read the current documentation for what main says now. A link to a decision or a migration page follows main, and leaves the archive.

FisherExact.Test

Tests a 2x2 table for association.

public static TestResult Test(int[][] table, Alternative alternative = Alternative.TwoSided)

Parameterstable is the counts, as two rows of two, every count non-negative. alternative says which tail the p-value covers.

ReturnsTestResult: the conditional odds ratio — PositiveInfinity when the second diagonal is zero, NaN when both diagonals are — and the p-value.

ExceptionsArgumentException when table is not 2x2. ArgumentOutOfRangeException when a count is negative, or the table's total exceeds 1,000,000.

Example — Fisher's own tea-tasting table: four cups poured each way, and the taster placed three of each correctly.

using Lodestar.Stats;

int[][] table = [3, 1], [1, 3](/CyrilB1531/lodestar/wiki/3,-1],-[1,-3);

TestResult result = FisherExact.Test(table);

double oddsRatio = result.Statistic;               // => 9
double p = Math.Round(result.PValue, 6);           // => 0.485714

Remarks — the one-sided p-value on the same table is smaller, Math.Round(FisherExact.Test(table, Alternative.Greater).PValue, 6) giving 0.242857: the two-sided p-value sums every table at least as extreme in either direction, so it is never smaller than the one-sided sum on its own tail.

The exact enumeration has a cost proportional to the table's total. Every table sharing the observed margins is walked, so a table summing past 1,000,000 is refused with ArgumentOutOfRangeException rather than run for however long that takes. ChiSquare.Contingency is the asymptotic alternative at that scale — right at large samples, where this test's exactness stops mattering and its cost starts.

Applies to — net10.0, netstandard2.0.

See alsoChiSquare.Contingency, Alternative, the Python equivalence table.