BCompare groups · nonparam.brunner_munzel
Brunner–Munzel test (stochastic equality)
Does one group tend to have larger values than the other, without assuming equal shapes or variances?
Tests whether P(X > Y) + ½P(X = Y) = ½ for two independent groups, like Mann–Whitney but valid when the groups differ in spread or shape (Brunner & Munzel 2000). Reports the probability of superiority with a t-based confidence interval.
A real result
Example “Toothgrowth ties”, recorded in both engines. In the app, “Try this example” loads it into Analyze.
t(54.68) = 1.90, p = .063, p̂ = .64
- t (Brunner–Munzel)
- 1.897
- df
- 54.677
- p
- .063
Effect size
- Probability of superiority p̂ 0.6394 95% CI [0.4921, 0.7868]
Plots
View data
| supp | len |
|---|---|
| VC | 4.200 |
| VC | 11.500 |
| VC | 7.300 |
| VC | 5.800 |
| VC | 6.400 |
| VC | 10 |
| VC | 11.200 |
| VC | 11.200 |
| VC | 5.200 |
| VC | 7 |
| VC | 16.500 |
| VC | 16.500 |
| VC | 15.200 |
| VC | 17.300 |
| VC | 22.500 |
| VC | 17.300 |
| VC | 13.600 |
| VC | 14.500 |
| VC | 18.800 |
| VC | 15.500 |
| VC | 23.600 |
| VC | 18.500 |
| VC | 33.900 |
| VC | 25.500 |
| VC | 26.400 |
| VC | 32.500 |
| VC | 26.700 |
| VC | 21.500 |
| VC | 23.300 |
| VC | 29.500 |
| OJ | 15.200 |
| OJ | 21.500 |
| OJ | 17.600 |
| OJ | 9.700 |
| OJ | 14.500 |
| OJ | 10 |
| OJ | 8.200 |
| OJ | 9.400 |
| OJ | 16.500 |
| OJ | 9.700 |
| OJ | 19.700 |
| OJ | 23.300 |
| OJ | 23.600 |
| OJ | 26.400 |
| OJ | 20 |
| OJ | 25.200 |
| OJ | 25.800 |
| OJ | 21.200 |
| OJ | 14.500 |
| OJ | 27.300 |
| OJ | 25.500 |
| OJ | 26.400 |
| OJ | 22.400 |
| OJ | 24.500 |
| OJ | 24.800 |
| OJ | 30.900 |
| OJ | 26.400 |
| OJ | 27.300 |
| OJ | 29.400 |
| OJ | 23 |
Assumptions
- Group sizes — 30 and 30 values.
- Independent observations — Each subject appears once, in one group.
Descriptives
| supp | n | Median | Mean rank (pooled) |
|---|---|---|---|
| OJ | 30 | 22.700 | 34.683 |
| VC | 30 | 16.500 | 26.317 |
- p̂ = P(OJ > VC) + ½P(tie); 0.5 means stochastic equality. Alternative: two-sided.
t(54.68) = 1.90, p = .063, p̂ = .64
- t (Brunner–Munzel)
- 1.897
- df
- 54.677
- p
- .063
Effect size
- Probability of superiority p̂ 0.6394 95% CI [0.4921, 0.7868]
To keep this page light, the plots are shown once, under the R tab.
Assumptions
- Group sizes — 30 and 30 values.
- Independent observations — Each subject appears once, in one group.
Descriptives
| supp | n | Median | Mean rank (pooled) |
|---|---|---|---|
| OJ | 30 | 22.700 | 34.683 |
| VC | 30 | 16.500 | 26.317 |
- p̂ = P(OJ > VC) + ½P(tie); 0.5 means stochastic equality. Alternative: two-sided.
Do it in CodeSplash Run it on your own data, in Python or R, with the code to prove it.
Worked examples in the app
- Toothgrowth tiestoothgrowth · group: supp · outcome: len
- Mtcars greatermtcars · group: am_f · outcome: mpg
- Scores smallscores · group: section · outcome: after
- Fligner policello tiestoothgrowth · group: supp · outcome: len
- Fligner policello lessmtcars · group: am_f · outcome: mpg
- Fligner policello separatedscores · group: section · outcome: after
- Degenerate single leveltoothgrowth · group: supp · outcome: len
In the guides
Tests from the statistics guide this method runs
Where it sits in the decision guides
- Two independent groups (continuous): Student / Welch t, Mann–Whitney U · Statistics Guide · B — Compare groups
- Robust, permutation and exact methods · Statistics Guide · Step 3 — If assumptions are violated
- Non-parametric alternatives · Statistics Guide · Additional considerations
What you need
- Outcomenumeric · 1 column
- The numeric variable you are analyzing.
- Groupscategorical / boolean · 1 column · 2–2 levels
- The column whose levels define the groups to compare.
Choices you make
hypothesis
- Testdefault brunner_munzel
Brunner–Munzel tests whether one group tends to be higher; Fligner–Policello is an alternative robust rank-order test.
- Brunner–Munzel (t approximation)(default) — Valid when spreads and shapes differ; reports p̂ with a t interval.
- Fligner–Policello (robust rank-order) — Normal-theory statistic for symmetric distributions with unequal spreads; compares medians.
- Alternative hypothesisdefault two-sided
Which direction counts as evidence. Keep two-sided unless you fixed the direction before seeing the data.
- Neither group tends higher (≠)(default) — Tests for one group tending higher in either direction; keep it unless you fixed a direction before seeing the data.
- First group tends higher (>) — Tests only for the first group tending higher; pick it only if you fixed this direction before seeing the data.
- First group tends lower (<) — Tests only for the first group tending lower; pick it only if you fixed this direction before seeing the data.
- Confidence leveldefault 0.95
How sure the intervals should be: 0.95 gives 95% confidence intervals. Higher confidence means wider intervals.
Checks and remedies
CodeSplash checks each testable assumption on every run and shows the result on the card; premises the data can’t test are marked. If a check fails, these are the ways forward.
Group sizes
The t approximation needs about 10 values per group; with fewer use a permutation test.
Independent observations
What it reports
- Tests: Stochastic difference between groups
- statistic
- One test of "every component null holds" from several independent tests' p-values.
- df
- How much independent information is left to estimate variability after fitting the model.
- p
- How surprising your data would be if the null hypothesis were true, as a probability.
- phat
- A value from one group is as likely to beat a value from the other as to lose to it; the relative effect equals ½.
- ci_low
- A range of effect values compatible with your data at the chosen confidence level.
- ci_high
- A range of effect values compatible with your data at the chosen confidence level.
- n_1
- How many independent units the analysis used; it sets precision and power.
- n_2
- How many independent units the analysis used; it sets precision and power.
- median_1
- The middle value when the data are sorted; half the values lie on each side.
- median_2
- The middle value when the data are sorted; half the values lie on each side.
- level_1
- Which group counts as first (or treated, or positive) decides the sign of differences and ratios on the card.
- level_2
- Which group counts as first (or treated, or positive) decides the sign of differences and ratios on the card.
- test
- The exact variant that ran (for example Welch vs classic F), shown so the result can be reported and reproduced.
- test_label
- The exact variant that ran (for example Welch vs classic F), shown so the result can be reported and reproduced.
- missing_share
- Values that were not recorded; how they went missing decides which fixes are safe.
Python and R
Python and R agree to 10 decimals (relative tolerance 1e-10). Python uses pandas, numpy, scipy, codesplash; R uses stats, nanoparquet, codesplashr.
Default traps between the two languages
- Twin implementation (codesplash.stats.robust.brunner_munzel ↔ cs_brunner_munzel); equals scipy.stats.brunnermunzel with the statistic's sign flipped so that positive means the first group tends higher.
- Fligner–Policello (codesplash.stats.rank_tests.fligner_policello ↔ cs_fligner_policello) counts ties ½ and refers the statistic to the standard normal; under complete separation it is ±∞ with p = 0. The p̂ interval shown with it is Brunner–Munzel's.
The exact script CodeSplash ran for example “Toothgrowth ties” · Python 3.13.5 · scipy 1.18.1 · pandas 3.1.0
# Brunner–Munzel test (stochastic equality)
# Generated by CodeSplash Analytics 1.0.0 on 2026-11-09 · method nonparam.brunner_munzel 1.1.1 · spec 02022c25e142a982
# Dataset: toothgrowth (version 1)
# Runs on its own: outside the app, cs.publish() writes $CS_RESULT_PATH or prints the result JSON.
# %% Setup
import numpy as np
import pandas as pd
from scipy import stats
import codesplash as cs
from codesplash.stats import rank_tests, robust
# %% Data
df = pd.read_parquet("tests/parity/fixtures/toothgrowth.parquet", columns=["len", "supp"])
n_input = len(df)
df["supp"] = pd.Categorical(df["supp"], categories=["OJ", "VC"]) # stored level order
df = df.dropna(subset=["len", "supp"]) # listwise deletion over the analysis columns
df["supp"] = df["supp"].cat.remove_unused_categories()
n_dropped = n_input - len(df)
warnings = []
if n_dropped:
warnings.append(cs.cs_warning("CS_W_ROWS_DROPPED", dropped=n_dropped, total=n_input))
if n_input and not len(df):
cs.cs_error("CS_E_NO_ROWS")
cs.check_finite_columns(df, ["len", "supp"]) # infinite values stop the run (registry/errors.yaml)
# %% Two samples (independent groups or paired measurements, from the bound roles)
y = "len"
g = "supp"
levels = [str(x) for x in df[g].cat.categories]
if len(levels) != 2:
cs.cs_error("CS_E_GROUP_COUNT", column=g, expected=2, k=len(levels))
x1 = df.loc[df[g] == df[g].cat.categories[0], y].to_numpy(dtype=float)
x2 = df.loc[df[g] == df[g].cat.categories[1], y].to_numpy(dtype=float)
design, label_1, label_2 = "independent", levels[0], levels[1]
# %% Test
conf = 0.95
alternative = "two-sided"
test = "brunner_munzel" or "brunner_munzel"
n1, n2 = len(x1), len(x2)
for lab, n_g in ((label_1, n1), (label_2, n2)): # the variance estimates need two values per group
if n_g < 2:
cs.cs_error("CS_E_SMALL_GROUP", min=2, group=f"{g} = {lab}" if design == "independent" else lab, n=n_g)
# Brunner–Munzel t test (scipy.stats.brunnermunzel; statistic sign: positive = first group higher)
r = robust.brunner_munzel(x1, x2, conf, alternative)
label = "Brunner–Munzel test"
if test == "fligner_policello":
# Fligner–Policello robust rank-order test (normal reference; ties ½); p̂ keeps the Brunner–Munzel interval
fp = rank_tests.fligner_policello(x1, x2, alternative)
r = dict(r, statistic=fp["statistic"], df=None, p=fp["p"])
label = "Fligner–Policello test"
# %% Assumptions
assumptions = [
{"key": "sample_size", "label": "Group sizes", "status": "warn" if min(n1, n2) < 10 else "pass",
"detail": f"{n1} and {n2} values" + ("; with fewer than 10 per group consider a permutation test." if min(n1, n2) < 10 else ".")},
{"key": "independence", "label": "Independent observations", "status": "info", "detail": "Each subject appears once, in one group."},
]
# %% Result
values = {"statistic": r["statistic"], "df": r["df"], "p": r["p"], "phat": r["phat"], "ci_low": r["ci_low"], "ci_high": r["ci_high"],
"n_1": n1, "n_2": n2, "median_1": float(np.median(x1)), "median_2": float(np.median(x2)), "level_1": label_1, "level_2": label_2,
"test": test, "test_label": label}
rk = stats.rankdata(np.concatenate([x1, x2]))
values["missing_share"] = n_dropped / n_input if n_input else 0.0 # consult-a-statistician input
chart_rows = np.unique(np.round(np.linspace(0, len(df) - 1, min(len(df), 5000))).astype(int))
cs.publish({
"method": "nonparam.brunner_munzel",
"methodVersion": "1.1.1",
"title": label,
"engine": cs.engine_info("pandas", "numpy", "scipy"),
"n": n1 + n2,
"summary": f"{label}: P({label_1} > {label_2}) + ½P(tie) = {r['phat']:.3f}, {conf:.0%} CI [{r['ci_low']:.3f}, {r['ci_high']:.3f}]; {cs.fmt_p(r['p'])}.",
"apa": (f"z = {cs.fmt_num(r['statistic'])}, {cs.fmt_p(r['p'])}, p̂ = {cs.fmt_num(r['phat'], leading_zero=False)}" if test == "fligner_policello"
else f"t({cs.fmt_num(r['df'])}) = {cs.fmt_num(r['statistic'])}, {cs.fmt_p(r['p'])}, p̂ = {cs.fmt_num(r['phat'], leading_zero=False)}"),
"statistics": ([{"key": "statistic", "label": "z (Fligner–Policello)", "value": r["statistic"]}] if test == "fligner_policello" else
[{"key": "statistic", "label": "t (Brunner–Munzel)", "value": r["statistic"]}, {"key": "df", "label": "df", "value": r["df"]}])
+ [{"key": "p", "label": "p", "value": r["p"], "format": "p"}],
"tables": [{"key": "descriptives", "title": "Descriptives", "columns": [
{"key": "group", "label": g, "type": "string"}, {"key": "n", "label": "n", "type": "integer"},
{"key": "median", "label": "Median", "type": "number"}, {"key": "mean_rank", "label": "Mean rank (pooled)", "type": "number"}],
"rows": [{"group": label_1, "n": n1, "median": values["median_1"], "mean_rank": float(rk[:n1].mean())},
{"group": label_2, "n": n2, "median": values["median_2"], "mean_rank": float(rk[n1:].mean())}]}],
"effectSizes": [{"key": "phat", "label": "Probability of superiority p̂", "value": r["phat"], "ciLow": r["ci_low"], "ciHigh": r["ci_high"],
"ciLevel": conf, "method": "P(X > Y) + ½P(X = Y), t interval (Brunner & Munzel 2000)"}],
"assumptions": assumptions,
"warnings": warnings,
"notes": [f"p̂ = P({label_1} > {label_2}) + ½P(tie); 0.5 means stochastic equality. Alternative: {alternative}."]
+ (["Fligner–Policello: placement counts with ties ½, standard normal reference; assumes symmetric distributions "
"(it then compares medians). The p̂ interval is Brunner–Munzel's."] if test == "fligner_policello" else []),
"values": values,
"charts": [cs.chart("distribution", "raincloud", f"{y} by {g}", {
"y": cs.col(y, df[y].to_numpy(dtype=float)[chart_rows]),
"group": cs.col(g, df[g].astype(str).to_numpy()[chart_rows], levels=levels)})],
})
The exact script CodeSplash ran for example “Toothgrowth ties” · R version 4.5.1 (2025-06-13)
# Brunner–Munzel test (stochastic equality)
# Generated by CodeSplash Analytics 1.0.0 on 2026-11-09 · method nonparam.brunner_munzel 1.1.1 · spec 02022c25e142a982
# Dataset: toothgrowth (version 1)
# Runs on its own: outside the app, cs_publish() writes $CS_RESULT_PATH or prints the result JSON.
# %% Setup
library(codesplashr)
# %% Data
df <- as.data.frame(nanoparquet::read_parquet("tests/parity/fixtures/toothgrowth.parquet", col_select = c("len", "supp")))
n_input <- nrow(df)
df[["supp"]] <- factor(df[["supp"]], levels = c("OJ", "VC")) # stored level order
df <- df[stats::complete.cases(df[c("len", "supp")]), , drop = FALSE] # listwise deletion
df[] <- lapply(df, function(x) if (is.factor(x)) droplevels(x) else x)
n_dropped <- n_input - nrow(df)
warnings <- list()
if (n_dropped > 0) {
warnings <- c(warnings, cs_warning("CS_W_ROWS_DROPPED", dropped = n_dropped, total = n_input))
}
if (n_input > 0 && nrow(df) == 0) cs_error("CS_E_NO_ROWS")
cs_check_finite_columns(df, c("len", "supp")) # infinite values stop the run (registry/errors.yaml)
# %% Two samples (independent groups or paired measurements, from the bound roles)
y <- "len"
g <- "supp"
levels_g <- levels(df[[g]])
if (length(levels_g) != 2) cs_error("CS_E_GROUP_COUNT", column = g, expected = 2, k = length(levels_g))
x1 <- df[[y]][df[[g]] == levels_g[1]]
x2 <- df[[y]][df[[g]] == levels_g[2]]
design <- "independent"
label_1 <- levels_g[1]
label_2 <- levels_g[2]
# %% Test
conf <- 0.95
alternative <- "two-sided"
test <- "brunner_munzel"
if (is.null(test)) test <- "brunner_munzel"
n1 <- length(x1)
n2 <- length(x2)
for (i in 1:2) { # the variance estimates need two values per group
lab <- c(label_1, label_2)[i]
if (c(n1, n2)[i] < 2) cs_error("CS_E_SMALL_GROUP", min = 2, group = if (design == "independent") paste0(g, " = ", lab) else lab, n = c(n1, n2)[i])
}
# Brunner-Munzel t test (brunnermunzel::brunnermunzel.test; statistic positive = first group higher)
r <- cs_brunner_munzel(x1, x2, conf, alternative)
label <- "Brunner–Munzel test"
if (test == "fligner_policello") {
# Fligner-Policello robust rank-order test (normal reference; ties 1/2); p-hat keeps the Brunner-Munzel interval
fp <- cs_fligner_policello(x1, x2, alternative)
r$statistic <- fp$statistic
r["df"] <- list(NULL)
r$p <- fp$p
label <- "Fligner–Policello test"
}
# %% Assumptions
assumptions <- list(
list(key = "sample_size", label = "Group sizes", status = if (min(n1, n2) < 10) "warn" else "pass",
detail = paste0(sprintf("%d and %d values", n1, n2), if (min(n1, n2) < 10) "; with fewer than 10 per group consider a permutation test." else ".")),
list(key = "independence", label = "Independent observations", status = "info", detail = "Each subject appears once, in one group.")
)
# %% Result
values <- list(statistic = r$statistic, df = r$df, p = r$p, phat = r$phat, ci_low = r$ci_low, ci_high = r$ci_high,
n_1 = n1, n_2 = n2, median_1 = median(x1), median_2 = median(x2), level_1 = label_1, level_2 = label_2,
test = test, test_label = label)
rk <- rank(c(x1, x2))
values$missing_share <- if (n_input > 0) n_dropped / n_input else 0 # consult-a-statistician input
chart_rows <- unique(round(seq(1, nrow(df), length.out = min(nrow(df), 5000))))
cs_publish(list(
method = "nonparam.brunner_munzel",
methodVersion = "1.1.1",
title = label,
engine = cs_engine_info("stats", "nanoparquet"),
n = n1 + n2,
summary = sprintf("%s: P(%s > %s) + ½P(tie) = %.3f, %.0f%% CI [%.3f, %.3f]; %s.", label, label_1, label_2, r$phat, conf * 100, r$ci_low, r$ci_high, cs_fmt_p(r$p)),
apa = if (test == "fligner_policello") {
sprintf("z = %s, %s, p̂ = %s", cs_fmt_num(r$statistic), cs_fmt_p(r$p), cs_fmt_num(r$phat, leading_zero = FALSE))
} else {
sprintf("t(%s) = %s, %s, p̂ = %s", cs_fmt_num(r$df), cs_fmt_num(r$statistic), cs_fmt_p(r$p), cs_fmt_num(r$phat, leading_zero = FALSE))
},
statistics = c(if (test == "fligner_policello") {
list(list(key = "statistic", label = "z (Fligner–Policello)", value = r$statistic))
} else {
list(list(key = "statistic", label = "t (Brunner–Munzel)", value = r$statistic), list(key = "df", label = "df", value = r$df))
}, list(list(key = "p", label = "p", value = r$p, format = "p"))),
tables = list(list(key = "descriptives", title = "Descriptives",
columns = list(list(key = "group", label = g, type = "string"), list(key = "n", label = "n", type = "integer"),
list(key = "median", label = "Median", type = "number"), list(key = "mean_rank", label = "Mean rank (pooled)", type = "number")),
rows = data.frame(group = c(label_1, label_2), n = c(n1, n2), median = c(median(x1), median(x2)),
mean_rank = c(mean(rk[seq_len(n1)]), mean(rk[n1 + seq_len(n2)])), stringsAsFactors = FALSE))),
effectSizes = list(list(key = "phat", label = "Probability of superiority p̂", value = r$phat, ciLow = r$ci_low, ciHigh = r$ci_high,
ciLevel = conf, method = "P(X > Y) + ½P(X = Y), t interval (Brunner & Munzel 2000)")),
assumptions = assumptions,
warnings = warnings,
notes = c(list(sprintf("p̂ = P(%s > %s) + ½P(tie); 0.5 means stochastic equality. Alternative: %s.", label_1, label_2, alternative)),
if (test == "fligner_policello") list(paste("Fligner–Policello: placement counts with ties ½, standard normal reference; assumes symmetric distributions",
"(it then compares medians). The p̂ interval is Brunner–Munzel's.")) else list()),
values = values,
charts = list(cs_chart("distribution", "raincloud", sprintf("%s by %s", y, g),
list(y = cs_col(y, df[[y]][chart_rows]), group = cs_col(g, as.character(df[[g]][chart_rows]), levels = levels_g))))
))
Sources
- Brunner, E., & Munzel, U. (2000). The nonparametric Behrens–Fisher problem: asymptotic theory and a small-sample approximation. Biometrical Journal, 42, 17–25.
- Neubert, K., & Brunner, E. (2007). A studentized permutation test for the non-parametric Behrens–Fisher problem. Computational Statistics & Data Analysis, 51, 5192–5204.
From the CodeSplash method registry · nonparam.brunner_munzel v1.1.1.