Skip to content

EProportions · Guide test 062

Barnard unconditional exact test

Compares independent binomial probabilities without conditioning on both margins.

Runs in CodeSplashPython example validatedR example not run

Do it in CodeSplash

In the app, set it up in Analyze with this method:

In Two independent proportions (z, Fisher, Boschloo, Barnard, non-inferiority), choose Test = Barnard's exact test (unconditional).

Do it in CodeSplash Run this test on your own data, in Python or R.

Bench

Change the design, drag the data, move α. The statistic, its reference distribution and the verdict follow.

Bench · Barnard unconditional exact test

12
12
0.60
0.25
0.050

Alternative

Your data

Edit the counts
SuccessFailureTotal
Row 1
9E = 5.5
3E = 6.5
12
Row 2
2E = 5.5
10E = 6.5
12
Total111324

E is the count expected if rows and columns were unrelated.

If H₀ were true

p = sup = .007

Shaded: the 42 outcomes whose z is at least as extreme as yours (the outlined cell is your table). Only the group sizes are fixed, so the probability of that region depends on the unknown common proportion π; the test takes its largest value over π, which can never be smaller than the p at the true π.

The formula

p=sup⁡π ∑(x1,x2): ∣z∣≥∣zobs∣(n1x1)(n2x2)πx1+x2(1−π)N−x1−x2p=\sup_{\pi}\ \sum_{(x_1,x_2):\,|z|\ge|z_{obs}|}\binom{n_1}{x_1}\binom{n_2}{x_2}\pi^{x_1+x_2}(1-\pi)^{N-x_1-x_2}

With your numbers

z=2.87,π^sup⁡=0.50,p=0.0066z = 2.87,\qquad \hat\pi_{\sup} = 0.50,\qquad p = 0.0066
zz
pooled (score) z for the difference in proportions
π\pi
the unknown common proportion, maximized over

The verdict

z = 2.87, p = .007

p ≤ α = .050: reject H₀

Fixes only the group sizes, not the total successes, so it is usually more powerful than Fisher's test. The price is a maximization over the nuisance proportion.

Statistic
2.868
p-value
0.0066
Barnard unconditional exact test on a 2 × 2 table of 24 counts: z = 2.87, p = .007. The p-value is below α = .050, so the data are hard to reconcile with H0.

Barnard unconditional exact test on a 2 × 2 table of 24 counts: z = 2.87, p = .007. The p-value is below α = .050, so the data are hard to reconcile with H₀.

Question and hypotheses

H0: two independent binomial success probabilities are equal.

Assumptions

Two fixed binomial sample sizes; independent observations. Margins of total successes need not be fixed.

Formula

p=sup⁡0≤p0≤1∑(a,b)∈E(n1a)(n2b)p0a+b(1−p0)n1+n2−a−bp=\sup_{0\le p_0\le1}\sum_{(a,b)\in\mathcal E}\binom{n_1}a\binom{n_2}b p_0^{a+b}(1-p_0)^{n_1+n_2-a-b}

Parameters and notation

E contains tables at least as extreme as observed using a specified statistic, often the pooled score z. The supremum removes the nuisance common probability.

Structure of the method

Enumerate the two independent binomial counts at their fixed group sizes, classify each outcome by the chosen pooled or unpooled statistic, and maximize the resulting null tail probability over the common success probability. The nuisance supremum is central to the unconditional construction.

Estimated and nuisance parameters

The observed group proportions determine the ordering statistic. The unknown common null probability is not replaced only by its pooled estimate: it is varied over its allowed interval when computing the p-value. Group sizes and table orientation remain fixed.

Settings and design choices

Choose the ordering statistic, one- or two-sided alternative, nuisance search precision, and numerical sampling settings. In SciPy, binomial experiments occupy columns, explaining the transpose in the example. Pooling affects the statistic and can change the rejection region.

Fitting and adaptation

Recompute the ordering and nuisance maximization for each new table. Increase numerical search resolution to check p-value stability near a decision threshold. A simulation audit should cover a range of null probabilities, because checking only the observed pooled estimate misses the defining worst-case requirement.

Sensitivity and robustness

The name covers several statistic orderings, so agreement requires more than matching the label Barnard. Edge probabilities and zero cells need valid statistic conventions. Computation grows with both sample sizes. Independence of trials is still essential even though the calculation is exact.

How to use and calibrate

Compute the unconditional tail over all binomial outcomes and maximize over p0; report statistic variant and alternative.

Example application

Compare two small independent success counts when unconditional exact inference is desired.

Interpretation and pitfalls

Watch out

Barnard denotes a family of orderings. Numerical optimization precision and pooled versus unpooled statistics can affect results.

What changed in edition 2

  • New sections: structure of the method, estimated and nuisance parameters, settings and design choices, fitting and adaptation, sensitivity and robustness.

Python and R

Rows are groups and columns success/failure; SciPy unconditional tests take the two binomial samples in columns, hence transpose.

Python · native

Validated — Executed successfully in the validation environment · needs numpy, scipy

import numpy as np
from scipy import stats
rng = np.random.default_rng(2026)
tab = np.array([[18,12],[9,21]])
print(stats.barnard_exact(tab.T,pooled=True))
Output
BarnardExactResult(statistic=np.float64(2.3354968324845684), pvalue=np.float64(0.022756823315570367))
R

Not run — Syntax checked; runtime not verified in the final environment · needs Exact

tab <- matrix(c(18,12,9,21),nrow=2,byrow=TRUE)
print(Exact::exact.test(tab,method="z-pooled",model="Binomial",cond.row=TRUE,to.plot=FALSE))

Sources

Statistics guide v4 (historical):
R: Syntax checked; runtime not verified in the final environment
Reference's record:
Edition 2 review: Python: executed · R: not executed (Rscript unavailable). Python: Ran in the reference's review, on this exact code. R: Reviewed but not run: R wasn't available to the reference's authors.

From the CodeSplash statistics guide (v4), entry 062, updated from the reference's edition 2 (reviewed 14 November 2026) as STAT062.