EProportions · Guide test 062
Barnard unconditional exact test
Compares independent binomial probabilities without conditioning on both margins.
Do it in CodeSplash
In the app, set it up in Analyze with this method:
- Two independent proportions (z, Fisher, Boschloo, Barnard, non-inferiority) — Does the share of an outcome differ between two independent groups?
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
Alternative
Your data
Edit the counts| Success | Failure | Total | |
|---|---|---|---|
| Row 1 | 9E = 5.5 | 3E = 6.5 | 12 |
| Row 2 | 2E = 5.5 | 10E = 6.5 | 12 |
| Total | 11 | 13 | 24 |
E is the count expected if rows and columns were unrelated.
If H₀ were true
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
With your numbers
- pooled (score) z for the difference in proportions
- 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 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
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.
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))
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
- SciPy Barnard exact testdocs.scipy.org
- scipy.stats.barnard_exactdocs.scipy.org
- exact.testsearch.r-project.org
- 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.