FA-74676 / Experiment statistics / Open access
Rank-sum test with ties: Variance ignores ties · case 01
z-scores are too small on bucketed metrics with many ties.
ROOT CAUSE
The variance uses (N + 1) without subtracting the tie term.
VERIFIED REPAIR
Subtract sum(t^3 - t) / (N(N - 1)) inside the variance.
Unsuccessful approach: Using t^2 - t understates the correction for large tie groups.
Case contract
Pool a (control) and b (treatment), assign mid-ranks to ties (1-based). U = R_b - n_b(n_b + 1)/2. Tie-corrected variance = n_a n_b / 12 * ((N + 1) - sum(t^3 - t) / (N(N - 1))); z = (U - n_a n_b / 2) / sqrt(variance) without continuity correction; zero variance gives z = 0. Empty arm -> None. Return [U, round(z, 6)].
Why this case matters
Rank tests are used for heavy-tailed metrics such as latency; ties are common in bucketed data.
1 / The failure
Exit 1"""Failure Map reference implementation. Python standard library only."""
import json
import math
N = 1
observations = []
def solve(a, b):
na, nb = len(a), len(b)
if na == 0 or nb == 0:
return None
pooled = sorted([(v, 0) for v in a] + [(v, 1) for v in b])
N = na + nb
ranks = [0.0] * N
ties = 0
i = 0
while i < N:
j = i
while j + 1 < N and pooled[j + 1][0] == pooled[i][0]:
j += 1
mid = (i + j) / 2 + 1
for k in range(i, j + 1):
ranks[k] = mid
t = j - i + 1
ties += t ** 3 - t
i = j + 1
rb = sum(r for r, (v, g) in zip(ranks, pooled) if g == 1)
u = rb - nb * (nb + 1) / 2
var = na * nb / 12 * (N + 1)
if var <= 0:
return [u, 0.0]
return [u, round((u - na * nb / 2) / math.sqrt(var), 6)]
def check(label, actual, expected):
observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('ties get average ranks', [[1, 2, 2], [2, 3, 4]], [8.0, 1.623086]),
('two-way tie across arms', [[1, 2], [2, 3]], [3.5, 1.224745]),
('three-way tie with treatment', [[5, 5], [5, 6]], [3.0, 1.0]),
('no ties', [[1, 3, 5], [2, 4, 6, 8]], [9.0, 1.06066]),
('complete separation', [[1, 2], [3, 4, 5]], [6.0, 1.732051]),
('unequal arm sizes', [[0, 1, 1, 4], [1, 2]], [5.0, 0.491869]),
('rank sample 1', [[1, 3, 2], [7]], [3.0, 1.341641]),
('rank sample 2', [[3, 1, 2, 2, 4], [7, 3, 1, 1, 5, 3]], [18.0, 0.559282])],
[('two-way tie across arms', [[1, 2], [2, 3]], [3.5, 1.224745]),
('three-way tie with treatment', [[5, 5], [5, 6]], [3.0, 1.0]),
('no ties', [[1, 3, 5], [2, 4, 6, 8]], [9.0, 1.06066]),
('complete separation', [[1, 2], [3, 4, 5]], [6.0, 1.732051]),
('all values tied', [[2, 2], [2, 2, 2]], [3.0, 0.0]),
('unequal arm sizes', [[0, 1, 1, 4], [1, 2]], [5.0, 0.491869]),
('rank sample 5', [[3, 4, 0], [5, 3, 3]], [6.0, 0.695608]),
('rank sample 6', [[0, 3], [2]], [1.0, 0.0])],
[('three-way tie with treatment', [[5, 5], [5, 6]], [3.0, 1.0]),
('no ties', [[1, 3, 5], [2, 4, 6, 8]], [9.0, 1.06066]),
('complete separation', [[1, 2], [3, 4, 5]], [6.0, 1.732051]),
('all values tied', [[2, 2], [2, 2, 2]], [3.0, 0.0]),
('unequal arm sizes', [[0, 1, 1, 4], [1, 2]], [5.0, 0.491869]),
('rank sample 11', [[1, 1, 2, 3, 4], [2]], [2.5, 0.0]),
('rank sample 12', [[2, 1, 4], [6, 0, 3, 7]], [8.0, 0.707107]),
('rank sample 16', [[4, 0, 3, 3, 1], [4, 1]], [6.0, 0.398109])],
[('ties get average ranks', [[1, 2, 2], [2, 3, 4]], [8.0, 1.623086]),
('no ties', [[1, 3, 5], [2, 4, 6, 8]], [9.0, 1.06066]),
('complete separation', [[1, 2], [3, 4, 5]], [6.0, 1.732051]),
('all values tied', [[2, 2], [2, 2, 2]], [3.0, 0.0]),
('unequal arm sizes', [[0, 1, 1, 4], [1, 2]], [5.0, 0.491869]),
('rank sample 16', [[4, 0, 3, 3, 1], [4, 1]], [6.0, 0.398109]),
('rank sample 17', [[3, 5, 0], [0, 6, 2, 4, 4, 6]], [11.5, 0.65372]),
('rank sample 25', [[5, 0], [3, 2, 0, 2, 1]], [4.5, -0.197203])],
[('ties get average ranks', [[1, 2, 2], [2, 3, 4]], [8.0, 1.623086]),
('two-way tie across arms', [[1, 2], [2, 3]], [3.5, 1.224745]),
('complete separation', [[1, 2], [3, 4, 5]], [6.0, 1.732051]),
('all values tied', [[2, 2], [2, 2, 2]], [3.0, 0.0]),
('unequal arm sizes', [[0, 1, 1, 4], [1, 2]], [5.0, 0.491869]),
('rank sample 21', [[4, 4, 3, 1, 2], [4, 7, 7, 7, 6, 5]], [29.0, 2.603819]),
('rank sample 22', [[3], [6]], [1.0, 1.0]),
('rank sample 33', [[2, 0, 5, 1, 3], [3]], [3.5, 0.594089])]]
for label, args, expected in fixtures[N - 1]:
check(label, solve(*args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)
| Boundary fixture | Actual | Expected | Outcome |
|---|---|---|---|
| ties get average ranks | [8.0, 1.527525] | [8.0, 1.623086] | Failed |
| two-way tie across arms | [3.5, 1.161895] | [3.5, 1.224745] | Failed |
| three-way tie with treatment | [3.0, 0.774597] | [3.0, 1.0] | Failed |
| no ties | [9.0, 1.06066] | [9.0, 1.06066] | Passed |
| complete separation | [6.0, 1.732051] | [6.0, 1.732051] | Passed |
| unequal arm sizes | [5.0, 0.46291] | [5.0, 0.491869] | Failed |
| rank sample 1 | [3.0, 1.341641] | [3.0, 1.341641] | Passed |
| rank sample 2 | [18.0, 0.547723] | [18.0, 0.559282] | Failed |
SHA-256 / 0b30cffe708eaf4b5dc5b2aabe2312247d4fa69a2d1d196032a9b1e0faedcfcd
2 / The unsuccessful fix
Exit 1"""Failure Map reference implementation. Python standard library only."""
import json
import math
N = 1
observations = []
def solve(a, b):
na, nb = len(a), len(b)
if na == 0 or nb == 0:
return None
pooled = sorted([(v, 0) for v in a] + [(v, 1) for v in b])
N = na + nb
ranks = [0.0] * N
ties = 0
i = 0
while i < N:
j = i
while j + 1 < N and pooled[j + 1][0] == pooled[i][0]:
j += 1
mid = (i + j) / 2 + 1
for k in range(i, j + 1):
ranks[k] = mid
t = j - i + 1
ties += t ** 2 - t
i = j + 1
rb = sum(r for r, (v, g) in zip(ranks, pooled) if g == 1)
u = rb - nb * (nb + 1) / 2
var = na * nb / 12 * ((N + 1) - ties / (N * (N - 1))) if N > 1 else 0.0
if var <= 0:
return [u, 0.0]
return [u, round((u - na * nb / 2) / math.sqrt(var), 6)]
def check(label, actual, expected):
observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('ties get average ranks', [[1, 2, 2], [2, 3, 4]], [8.0, 1.623086]),
('two-way tie across arms', [[1, 2], [2, 3]], [3.5, 1.224745]),
('three-way tie with treatment', [[5, 5], [5, 6]], [3.0, 1.0]),
('no ties', [[1, 3, 5], [2, 4, 6, 8]], [9.0, 1.06066]),
('complete separation', [[1, 2], [3, 4, 5]], [6.0, 1.732051]),
('unequal arm sizes', [[0, 1, 1, 4], [1, 2]], [5.0, 0.491869]),
('rank sample 1', [[1, 3, 2], [7]], [3.0, 1.341641]),
('rank sample 2', [[3, 1, 2, 2, 4], [7, 3, 1, 1, 5, 3]], [18.0, 0.559282])],
[('two-way tie across arms', [[1, 2], [2, 3]], [3.5, 1.224745]),
('three-way tie with treatment', [[5, 5], [5, 6]], [3.0, 1.0]),
('no ties', [[1, 3, 5], [2, 4, 6, 8]], [9.0, 1.06066]),
('complete separation', [[1, 2], [3, 4, 5]], [6.0, 1.732051]),
('all values tied', [[2, 2], [2, 2, 2]], [3.0, 0.0]),
('unequal arm sizes', [[0, 1, 1, 4], [1, 2]], [5.0, 0.491869]),
('rank sample 5', [[3, 4, 0], [5, 3, 3]], [6.0, 0.695608]),
('rank sample 6', [[0, 3], [2]], [1.0, 0.0])],
[('three-way tie with treatment', [[5, 5], [5, 6]], [3.0, 1.0]),
('no ties', [[1, 3, 5], [2, 4, 6, 8]], [9.0, 1.06066]),
('complete separation', [[1, 2], [3, 4, 5]], [6.0, 1.732051]),
('all values tied', [[2, 2], [2, 2, 2]], [3.0, 0.0]),
('unequal arm sizes', [[0, 1, 1, 4], [1, 2]], [5.0, 0.491869]),
('rank sample 11', [[1, 1, 2, 3, 4], [2]], [2.5, 0.0]),
('rank sample 12', [[2, 1, 4], [6, 0, 3, 7]], [8.0, 0.707107]),
('rank sample 16', [[4, 0, 3, 3, 1], [4, 1]], [6.0, 0.398109])],
[('ties get average ranks', [[1, 2, 2], [2, 3, 4]], [8.0, 1.623086]),
('no ties', [[1, 3, 5], [2, 4, 6, 8]], [9.0, 1.06066]),
('complete separation', [[1, 2], [3, 4, 5]], [6.0, 1.732051]),
('all values tied', [[2, 2], [2, 2, 2]], [3.0, 0.0]),
('unequal arm sizes', [[0, 1, 1, 4], [1, 2]], [5.0, 0.491869]),
('rank sample 16', [[4, 0, 3, 3, 1], [4, 1]], [6.0, 0.398109]),
('rank sample 17', [[3, 5, 0], [0, 6, 2, 4, 4, 6]], [11.5, 0.65372]),
('rank sample 25', [[5, 0], [3, 2, 0, 2, 1]], [4.5, -0.197203])],
[('ties get average ranks', [[1, 2, 2], [2, 3, 4]], [8.0, 1.623086]),
('two-way tie across arms', [[1, 2], [2, 3]], [3.5, 1.224745]),
('complete separation', [[1, 2], [3, 4, 5]], [6.0, 1.732051]),
('all values tied', [[2, 2], [2, 2, 2]], [3.0, 0.0]),
('unequal arm sizes', [[0, 1, 1, 4], [1, 2]], [5.0, 0.491869]),
('rank sample 21', [[4, 4, 3, 1, 2], [4, 7, 7, 7, 6, 5]], [29.0, 2.603819]),
('rank sample 22', [[3], [6]], [1.0, 1.0]),
('rank sample 33', [[2, 0, 5, 1, 3], [3]], [3.5, 0.594089])]]
for label, args, expected in fixtures[N - 1]:
check(label, solve(*args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)
| Boundary fixture | Actual | Expected | Outcome |
|---|---|---|---|
| ties get average ranks | [8.0, 1.549826] | [8.0, 1.623086] | Failed |
| two-way tie across arms | [3.5, 1.181758] | [3.5, 1.224745] | Failed |
| three-way tie with treatment | [3.0, 0.816497] | [3.0, 1.0] | Failed |
| no ties | [9.0, 1.06066] | [9.0, 1.06066] | Passed |
| complete separation | [6.0, 1.732051] | [6.0, 1.732051] | Passed |
| unequal arm sizes | [5.0, 0.469668] | [5.0, 0.491869] | Failed |
| rank sample 1 | [3.0, 1.341641] | [3.0, 1.341641] | Passed |
| rank sample 2 | [18.0, 0.55065] | [18.0, 0.559282] | Failed |
SHA-256 / 7589cefdb821e0200a885094a2b1cfc48a9159dbd1f28801b984f3898fbe1a52
3 / The verified repair
Exit 0"""Failure Map reference implementation. Python standard library only."""
import json
import math
N = 1
observations = []
def solve(a, b):
na, nb = len(a), len(b)
if na == 0 or nb == 0:
return None
pooled = sorted([(v, 0) for v in a] + [(v, 1) for v in b])
N = na + nb
ranks = [0.0] * N
ties = 0
i = 0
while i < N:
j = i
while j + 1 < N and pooled[j + 1][0] == pooled[i][0]:
j += 1
mid = (i + j) / 2 + 1
for k in range(i, j + 1):
ranks[k] = mid
t = j - i + 1
ties += t ** 3 - t
i = j + 1
rb = sum(r for r, (v, g) in zip(ranks, pooled) if g == 1)
u = rb - nb * (nb + 1) / 2
var = na * nb / 12 * ((N + 1) - ties / (N * (N - 1))) if N > 1 else 0.0
if var <= 0:
return [u, 0.0]
return [u, round((u - na * nb / 2) / math.sqrt(var), 6)]
def check(label, actual, expected):
observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('ties get average ranks', [[1, 2, 2], [2, 3, 4]], [8.0, 1.623086]),
('two-way tie across arms', [[1, 2], [2, 3]], [3.5, 1.224745]),
('three-way tie with treatment', [[5, 5], [5, 6]], [3.0, 1.0]),
('no ties', [[1, 3, 5], [2, 4, 6, 8]], [9.0, 1.06066]),
('complete separation', [[1, 2], [3, 4, 5]], [6.0, 1.732051]),
('unequal arm sizes', [[0, 1, 1, 4], [1, 2]], [5.0, 0.491869]),
('rank sample 1', [[1, 3, 2], [7]], [3.0, 1.341641]),
('rank sample 2', [[3, 1, 2, 2, 4], [7, 3, 1, 1, 5, 3]], [18.0, 0.559282])],
[('two-way tie across arms', [[1, 2], [2, 3]], [3.5, 1.224745]),
('three-way tie with treatment', [[5, 5], [5, 6]], [3.0, 1.0]),
('no ties', [[1, 3, 5], [2, 4, 6, 8]], [9.0, 1.06066]),
('complete separation', [[1, 2], [3, 4, 5]], [6.0, 1.732051]),
('all values tied', [[2, 2], [2, 2, 2]], [3.0, 0.0]),
('unequal arm sizes', [[0, 1, 1, 4], [1, 2]], [5.0, 0.491869]),
('rank sample 5', [[3, 4, 0], [5, 3, 3]], [6.0, 0.695608]),
('rank sample 6', [[0, 3], [2]], [1.0, 0.0])],
[('three-way tie with treatment', [[5, 5], [5, 6]], [3.0, 1.0]),
('no ties', [[1, 3, 5], [2, 4, 6, 8]], [9.0, 1.06066]),
('complete separation', [[1, 2], [3, 4, 5]], [6.0, 1.732051]),
('all values tied', [[2, 2], [2, 2, 2]], [3.0, 0.0]),
('unequal arm sizes', [[0, 1, 1, 4], [1, 2]], [5.0, 0.491869]),
('rank sample 11', [[1, 1, 2, 3, 4], [2]], [2.5, 0.0]),
('rank sample 12', [[2, 1, 4], [6, 0, 3, 7]], [8.0, 0.707107]),
('rank sample 16', [[4, 0, 3, 3, 1], [4, 1]], [6.0, 0.398109])],
[('ties get average ranks', [[1, 2, 2], [2, 3, 4]], [8.0, 1.623086]),
('no ties', [[1, 3, 5], [2, 4, 6, 8]], [9.0, 1.06066]),
('complete separation', [[1, 2], [3, 4, 5]], [6.0, 1.732051]),
('all values tied', [[2, 2], [2, 2, 2]], [3.0, 0.0]),
('unequal arm sizes', [[0, 1, 1, 4], [1, 2]], [5.0, 0.491869]),
('rank sample 16', [[4, 0, 3, 3, 1], [4, 1]], [6.0, 0.398109]),
('rank sample 17', [[3, 5, 0], [0, 6, 2, 4, 4, 6]], [11.5, 0.65372]),
('rank sample 25', [[5, 0], [3, 2, 0, 2, 1]], [4.5, -0.197203])],
[('ties get average ranks', [[1, 2, 2], [2, 3, 4]], [8.0, 1.623086]),
('two-way tie across arms', [[1, 2], [2, 3]], [3.5, 1.224745]),
('complete separation', [[1, 2], [3, 4, 5]], [6.0, 1.732051]),
('all values tied', [[2, 2], [2, 2, 2]], [3.0, 0.0]),
('unequal arm sizes', [[0, 1, 1, 4], [1, 2]], [5.0, 0.491869]),
('rank sample 21', [[4, 4, 3, 1, 2], [4, 7, 7, 7, 6, 5]], [29.0, 2.603819]),
('rank sample 22', [[3], [6]], [1.0, 1.0]),
('rank sample 33', [[2, 0, 5, 1, 3], [3]], [3.5, 0.594089])]]
for label, args, expected in fixtures[N - 1]:
check(label, solve(*args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)
| Boundary fixture | Actual | Expected | Outcome |
|---|---|---|---|
| ties get average ranks | [8.0, 1.623086] | [8.0, 1.623086] | Passed |
| two-way tie across arms | [3.5, 1.224745] | [3.5, 1.224745] | Passed |
| three-way tie with treatment | [3.0, 1.0] | [3.0, 1.0] | Passed |
| no ties | [9.0, 1.06066] | [9.0, 1.06066] | Passed |
| complete separation | [6.0, 1.732051] | [6.0, 1.732051] | Passed |
| unequal arm sizes | [5.0, 0.491869] | [5.0, 0.491869] | Passed |
| rank sample 1 | [3.0, 1.341641] | [3.0, 1.341641] | Passed |
| rank sample 2 | [18.0, 0.559282] | [18.0, 0.559282] | Passed |
SHA-256 / 029fa2d65d21ca65f4388fee08fdc5e2ed824346f1c0839f9a1456857c3131ae
Verification & scope
A deterministic toy experiment-analysis model with a stipulated contract; results are rounded and are not a substitute for a validated statistics package. This reproducer isolates one failure mechanism. Results cover the supplied fixtures. Variants within a family share a test contract and should remain grouped when constructing evaluation splits. Related mechanisms with a shared evaluation_group must also remain together; these controlled models are not independent production incidents.
Observations recorded using Python 3.12.14 at 2026-09-29T14:48:59.031846+00:00.
Case digest / 1e10948e1be449066584d0fcf5b440bb2a2dc5ae1e2f44676dc63bb14411173a