Two groups each run a fixed number of independent trials and their success proportions differ—can one conclude that A's success probability is lower than B's? Barnard's exact test answers that only after facing a point often overlooked: even if the two success probabilities are equal, what is that common probability? It might be 0.1, 0.5, or 0.9; different values change how likely each table is. Barnard's test considers those possibilities one by one and takes as the p-value the probability that makes the observed result least unusual.[1]

Fix the question, then enumerate possible tables

Consider a table constructed to illustrate the method. Groups A and B each have 10 trials; each trial is a success or failure; groups and trials are mutually independent; success probability is constant within a group; and both groups use the same definition of success. Write the success probabilities pA and pB, and before seeing the data specify the one-sided alternative H1: pA<pB with null H0: pA≥pB.

Outcome Group A Group B
Success 3 8
Failure 7 2
Total 10 10

Group A's success proportion is 0.3 and B's is 0.8. To judge how far the difference leans toward the alternative, first choose a ranking statistic. Here use the pooled-variance form: for any possible table with success counts xA and xB, set p̂=(xA+xB)/20 and compute T=(xA/10−xB/10)/√[p̂(1−p̂)(1/10+1/10)]. For the observed table T is about −2.2473; smaller T means a table more tilted toward a lower success probability in A.[1]

With group sizes fixed, xA and xB each have 11 possibilities from 0 through 10, so there are 121 possible tables. Rank them with the same statistic and put tables with T≤−2.2473 into the lower tail—21 tables in all. The lower-tail set is fixed by the observed table and the prechosen ranking; when the common probability later changes, what changes is each table's probability, not membership of these 21 tables.

Left grid marks the lower tail formed by the two groups' success counts; right curve shows how the tail probability varies with the common probability

Constructed table of two independent binomial samples: 10 trials per group with 3 and 8 successes. The left panel enumerates all 121 tables and marks the pre-specified lower tail; the right panel takes the maximum tail probability over the common success probability. Numbers computed by this article's script.

With an unknown common probability, the tail is not a single number

Start on the null boundary pA=pB=π. If π is temporarily given a value, the independent binomial model assigns a probability to every table. Summing the probabilities of the 21 lower-tail tables yields the tail function

L(π) = ∑<sub>T(xA,xB)≤Tobs</sub> C(10,xA) C(10,xB) π<sup>xA+xB</sup>(1−π)<sup>20−xA−xB</sup>.

Here C(10,x) is the number of ways to choose x successes in 10 trials. L(π) answers: if both groups share success probability exactly π, how probable is a result as tilted toward A—or more—than the observed table? Because π is not fixed by the null, the Barnard p-value is the maximum of L(π) over 0≤π≤1.[1]

The maximum makes the conclusion robust to any common probability the null allows. If a table's maximum tail probability never exceeds the significance level α, then for every boundary common probability the corresponding tail stays at most α. For the one-sided ranking fixed here, interior null points with pA>pB push results opposite the alternative; the worst case on the boundary therefore also protects H0: pA≥pB. The cost is that the result may be more conservative than a tail computed at one nominated π.

In this example the maximum occurs at π=0.5, with p≈0.0206947. The pooled success proportion in the data is 11/20=0.55; plugging only 0.55 into the tail function yields about 0.0201715, which is not the Barnard p-value here. The pooled proportion is used to build each table's ranking statistic; the common probability π is used to compute those tables' probabilities under the null boundary—different roles. Taking the maximum is also not a Bayesian average over π.

Computing with SciPy and reading the result

Table columns are the two groups; the first row is successes and the second failures. The code below fixes the one-sided direction and pooled-variance statistic, and contrasts more numerical optimization sample points:

from scipy.stats import barnard_exact, fisher_exact

table = [[3, 8], [7, 2]]
for points in (128, 512):
    result = barnard_exact(table, alternative="less",
                           pooled=True, n=points)
    print(points, result.statistic, result.pvalue)

fisher = fisher_exact(table, alternative="less")
print("Fisher p:", fisher.pvalue)

Approximate output:

128 -2.2473328748774732 0.020694732666015646
512 -2.2473328748774732 0.020694732666015646
Fisher p: 0.034889259347463675

If the significance level was set to 0.05 in advance, this example's Barnard p-value is below that level, so H0 is rejected and the result supports a lower success probability in A than in B. The p-value is not the probability that the null is true, nor does it by itself give the size of the success-probability difference or an uncertainty interval. The one-sided direction and the pooled option should be chosen from the question before analysis; pooled names the form of the ranking statistic here and does not require a preliminary equal-variance test.

A one-sided Fisher's exact test on the same table yields p≈0.0348893 and also rejects at level 0.05. Fisher's test further conditions on the observed total number of successes; Barnard's test keeps that total random and takes the maximum tail over the unknown common probability. Both calculations can be used for such independent binomial sampling, but they answer the question under different reference distributions.

The key is not to find a π that looks most like the truth, but to see how large a tail probability any π on the null boundary can produce. Fix sample sizes, direction, and ranking first, enumerate the tail, then maximize over the unknown common probability—that shows what this p-value protects.

References

  1. [1] SciPy community. “scipy.stats.barnard_exact.” SciPy Manual.