Kruskal-Wallis Test (cross-cutting/numerics/kruskal-wallis-test)
Use when the task is a distribution-free comparison of three or more
independent groups, the rank-based counterpart of the parametric
one-way location comparison: merge all observations, rank them with
average ranks for ties, form the H statistic, correct it for ties, and
convert it to a p-value through the chi-square survival function. This
leaf implements the standard Kruskal-Wallis method in pure Python,
stdlib only, with its own regularized lower incomplete gamma pair for
the chi-square survival (no sibling imports). It pairs with
cross-cutting/numerics/rank-based-hypothesis-testing, which owns paired
and two independent-group comparisons, and with
cross-cutting/numerics/hypothesis-testing, which owns the parametric
location and scale tests this leaf does not need.
Domain quick reference
- Ranks: merge the N observations of all groups and rank the merged
sample; equal observations share the average of the rank positions
they occupy (average ranks for ties).
- H statistic: H = (12 / (N (N + 1))) sum_i(R_i^2 / n_i) - 3 (N + 1),
where R_i is the sum of the ranks of group i and n_i its size. H = 0
when all groups are identical.
- Ties correction: with tie runs of size t the denominator is C = 1 -
sum((t^3 - t) / (N^3 - N)); C = 1.0 when there are no ties, and the
corrected statistic is H' = H / C, so ties move H upward.
- Degrees of freedom: df = k - 1 for k groups.
- p-value: p = P(chi2_(k-1) > H'), the survival function evaluated as
the regularized upper incomplete gamma Q(df / 2, H' / 2), computed
with the standard series and continued-fraction pair.
- Verdict: reject when p <= alpha, fail to reject otherwise; alpha
defaults to 0.05 and must lie in (0, 1).
- All quantities are pure floats; the module is deterministic and
offline.
- NACA-TR-824 frames the statistical data-analysis context; the
relations above are standard engineering methodology, summary-only.
Workflow
- Collect the groups: assemble the k >= 3 independent samples of
finite measurements (group collection); every group needs at least
two observations, and the significance level alpha must lie in
(0, 1).
- Rank the merged sample: concatenate all groups and assign average
ranks for ties with rank_data (rank assignment step).
- Compute the H statistic with kruskal_wallis_h, which returns the
uncorrected H of the formula above (H statistic computation step).
- Apply the ties correction: read the denominator C from
ties_correction and form h_corrected = H / C (ties correction step;
C = 1.0 leaves H unchanged).
- Convert to significance: kruskal_wallis_p_value(h_corrected,
group_count) evaluates the chi-square survival with df =
group_count - 1 through the self-contained incomplete gamma pair
(significance conversion step).
- Read the verdict report: kruskal_wallis_test(groups, alpha) returns
the dict with keys h, h_corrected, df, p_value, verdict and
group_rank_sums, where verdict is reject when p_value <= alpha
(verdict report step).
- Confirm the deterministic checks with the contract test run:
python3 scripts/test_kruskal_wallis_test.py (contract test step).
Worked example
Three groups of three, x = [2.1, 2.2, 2.3], y = [2.9, 3.0, 3.1],
z = [3.7, 3.8, 3.9], alpha 0.05 (module outputs shown):
- Merged ranks: x takes ranks 1 to 3 (sum 6), y ranks 4 to 6 (sum 15),
z ranks 7 to 9 (sum 24); N = 9, no ties.
- H = (12 / 90) (36/3 + 225/3 + 576/3) - 30 = 7.200000000000003,
within 1e-9 of 7.2; ties_correction returns C = 1.0, so
h_corrected = 7.200000000000003.
- df = 2; p_value = 0.027323722447292528 from the chi-square survival
(matches exp(-3.6), the exact df-2 survival at H = 7.2).
- Verdict: reject at 0.05; group_rank_sums [6.0, 15.0, 24.0].
A tied comparison, groups [1, 2, 3], [1.5, 2.5, 3.5], [2, 3, 4]:
values 2 and 3 both recur, C = 0.9833333333333333 below 1, H =
1.4222222222222243, h_corrected = 1.4463276836158214 (H divided by C),
p_value = 0.4852146824247665, verdict fail to reject at 0.05.
Pitfalls
- Correcting ties the wrong direction: the model divides H by C =
1 - sum((t^3 - t) / (N^3 - N)) with C below 1 when ties exist, so
the corrected statistic rises above the raw H; multiplying by C
instead would understate the separation.
- Forgetting average ranks: tied observations must share the mean of
their rank block, not arbitrary distinct integers, or H and the
p-value drift.
- Using the leaf on designs it does not own: paired measurements and
two independent-group comparisons belong to
cross-cutting/numerics/rank-based-hypothesis-testing, and parametric
location and scale tests belong to
cross-cutting/numerics/hypothesis-testing.
- Reading a group rank sum as a mean: group_rank_sums lists the raw
sums of average ranks per group in input order, and the sums always
total N (N + 1) / 2 whether ties are present or not.
- Passing undersized input: fewer than 3 groups, a group with a single
observation, non-finite values, a negative corrected statistic, or
alpha outside (0, 1) all raise ValueError.
- Treating the p-value as exact for small samples: the p-value comes
from the chi-square survival approximation with df = k - 1, exact
for the worked example but asymptotic for very small group sizes.
Verification
- Confirm kruskal_wallis_h on the worked example returns
7.200000000000003, within 1e-9 of 7.2, and kruskal_wallis_test
returns df 2, p_value 0.027323722447292528 (within 1e-3 of 0.0273),
verdict reject at 0.05 and group_rank_sums [6.0, 15.0, 24.0].
- Confirm all-identical groups return H = 0.0, p_value 1.0 and verdict
fail to reject, with the zero correction denominator handled.
- Confirm the tied example gives C = 0.9833333333333333 below 1 and
h_corrected = h / C above the raw H.
- Confirm rank invariance: multiplying every observation by 10 leaves
H unchanged because only the rank assignment feeds the statistic.
- Confirm every undersized, non-finite and out-of-range input raises
ValueError, and that repeated calls return identical reports.
- Run the contract test offline: python3
scripts/test_kruskal_wallis_test.py (35 tests, deterministic).
Related leaves
- cross-cutting/numerics/rank-based-hypothesis-testing: paired and two
independent-group location tests, the k = 2 family this leaf does
not cover.
- cross-cutting/numerics/hypothesis-testing: the parametric one-way
location comparison and scale tests for normal data.
- cross-cutting/numerics/runs-test: randomness and sequence structure
checks on a single sample.
- cross-cutting/numerics/grubbs-outlier-test: single-outlier detection
on grouped or pooled measurements.
- cross-cutting/numerics/probability-distributions: the distributions
behind the survival-function context.
Behavior contract (gate 3)
Run the deterministic contract test (stdlib unittest, offline):
python3 scripts/test_kruskal_wallis_test.py
The test covers the worked-example contract (H within 1e-9 of 7.2,
df 2, p_value 0.027323722447292528 within 1e-3 of 0.0273, verdict
reject at 0.05, per-group rank sums 6, 15, 24), the chi-square survival
anchor exp(-3.6) at df 2, average-rank assignment for ties, the ties
correction denominator and the h_corrected = H / C identity,
all-identical groups with H = 0 and p = 1, rank invariance under
monotone transforms, the rank-sum total identity N (N + 1) / 2, report
dict keys and determinism, and ValueError rejection of two groups, a
single-observation group, non-finite values, negative corrected
statistics, and alpha outside (0, 1).
Compliance
- Standards referenced, not reproduced: NACA-TR-824 is a public-domain
NACA technical report; the Kruskal-Wallis relations above are
standard engineering methodology, summary-only per standards-map.yaml.
- compliance: STANDARDS-REF, gated: false.
1---2name: kruskal-wallis-test3description: Use when you must compare three or more independent groups without the normality assumption: run the Kruskal-Wallis test by merging all observations, assigning average ranks for ties, computing the H statistic, applying the ties correction denominator, evaluating the p-value from the chi-square survival function with k minus 1 degrees of freedom, and returning the verdict at the chosen significance level. Produces H, the ties-corrected H, degrees of freedom, p-value, per-group rank sums and the gate verdict for non-normal group data. Trigger: kruskal wallis test, h-statistic, distribution-free k sample significance, rank-based multi-group comparison, chi-square survival p-value, coating batch roughness, process temperature settings.4license: Apache-2.05---67# Kruskal-Wallis Test (cross-cutting/numerics/kruskal-wallis-test)89Use when the task is a distribution-free comparison of three or more10independent groups, the rank-based counterpart of the parametric11one-way location comparison: merge all observations, rank them with12average ranks for ties, form the H statistic, correct it for ties, and13convert it to a p-value through the chi-square survival function. This14leaf implements the standard Kruskal-Wallis method in pure Python,15stdlib only, with its own regularized lower incomplete gamma pair for16the chi-square survival (no sibling imports). It pairs with17cross-cutting/numerics/rank-based-hypothesis-testing, which owns paired18and two independent-group comparisons, and with19cross-cutting/numerics/hypothesis-testing, which owns the parametric20location and scale tests this leaf does not need.2122## Domain quick reference2324- Ranks: merge the N observations of all groups and rank the merged25 sample; equal observations share the average of the rank positions26 they occupy (average ranks for ties).27- H statistic: H = (12 / (N (N + 1))) sum_i(R_i^2 / n_i) - 3 (N + 1),28 where R_i is the sum of the ranks of group i and n_i its size. H = 029 when all groups are identical.30- Ties correction: with tie runs of size t the denominator is C = 1 -31 sum((t^3 - t) / (N^3 - N)); C = 1.0 when there are no ties, and the32 corrected statistic is H' = H / C, so ties move H upward.33- Degrees of freedom: df = k - 1 for k groups.34- p-value: p = P(chi2_(k-1) > H'), the survival function evaluated as35 the regularized upper incomplete gamma Q(df / 2, H' / 2), computed36 with the standard series and continued-fraction pair.37- Verdict: reject when p <= alpha, fail to reject otherwise; alpha38 defaults to 0.05 and must lie in (0, 1).39- All quantities are pure floats; the module is deterministic and40 offline.41- NACA-TR-824 frames the statistical data-analysis context; the42 relations above are standard engineering methodology, summary-only.4344## Workflow45461. Collect the groups: assemble the k >= 3 independent samples of47 finite measurements (group collection); every group needs at least48 two observations, and the significance level alpha must lie in49 (0, 1).502. Rank the merged sample: concatenate all groups and assign average51 ranks for ties with rank_data (rank assignment step).523. Compute the H statistic with kruskal_wallis_h, which returns the53 uncorrected H of the formula above (H statistic computation step).544. Apply the ties correction: read the denominator C from55 ties_correction and form h_corrected = H / C (ties correction step;56 C = 1.0 leaves H unchanged).575. Convert to significance: kruskal_wallis_p_value(h_corrected,58 group_count) evaluates the chi-square survival with df =59 group_count - 1 through the self-contained incomplete gamma pair60 (significance conversion step).616. Read the verdict report: kruskal_wallis_test(groups, alpha) returns62 the dict with keys h, h_corrected, df, p_value, verdict and63 group_rank_sums, where verdict is reject when p_value <= alpha64 (verdict report step).657. Confirm the deterministic checks with the contract test run:66 python3 scripts/test_kruskal_wallis_test.py (contract test step).6768## Worked example6970Three groups of three, x = [2.1, 2.2, 2.3], y = [2.9, 3.0, 3.1],71z = [3.7, 3.8, 3.9], alpha 0.05 (module outputs shown):7273- Merged ranks: x takes ranks 1 to 3 (sum 6), y ranks 4 to 6 (sum 15),74 z ranks 7 to 9 (sum 24); N = 9, no ties.75- H = (12 / 90) (36/3 + 225/3 + 576/3) - 30 = 7.200000000000003,76 within 1e-9 of 7.2; ties_correction returns C = 1.0, so77 h_corrected = 7.200000000000003.78- df = 2; p_value = 0.027323722447292528 from the chi-square survival79 (matches exp(-3.6), the exact df-2 survival at H = 7.2).80- Verdict: reject at 0.05; group_rank_sums [6.0, 15.0, 24.0].8182A tied comparison, groups [1, 2, 3], [1.5, 2.5, 3.5], [2, 3, 4]:83values 2 and 3 both recur, C = 0.9833333333333333 below 1, H =841.4222222222222243, h_corrected = 1.4463276836158214 (H divided by C),85p_value = 0.4852146824247665, verdict fail to reject at 0.05.8687## Pitfalls8889- Correcting ties the wrong direction: the model divides H by C =90 1 - sum((t^3 - t) / (N^3 - N)) with C below 1 when ties exist, so91 the corrected statistic rises above the raw H; multiplying by C92 instead would understate the separation.93- Forgetting average ranks: tied observations must share the mean of94 their rank block, not arbitrary distinct integers, or H and the95 p-value drift.96- Using the leaf on designs it does not own: paired measurements and97 two independent-group comparisons belong to98 cross-cutting/numerics/rank-based-hypothesis-testing, and parametric99 location and scale tests belong to100 cross-cutting/numerics/hypothesis-testing.101- Reading a group rank sum as a mean: group_rank_sums lists the raw102 sums of average ranks per group in input order, and the sums always103 total N (N + 1) / 2 whether ties are present or not.104- Passing undersized input: fewer than 3 groups, a group with a single105 observation, non-finite values, a negative corrected statistic, or106 alpha outside (0, 1) all raise ValueError.107- Treating the p-value as exact for small samples: the p-value comes108 from the chi-square survival approximation with df = k - 1, exact109 for the worked example but asymptotic for very small group sizes.110111## Verification112113- Confirm kruskal_wallis_h on the worked example returns114 7.200000000000003, within 1e-9 of 7.2, and kruskal_wallis_test115 returns df 2, p_value 0.027323722447292528 (within 1e-3 of 0.0273),116 verdict reject at 0.05 and group_rank_sums [6.0, 15.0, 24.0].117- Confirm all-identical groups return H = 0.0, p_value 1.0 and verdict118 fail to reject, with the zero correction denominator handled.119- Confirm the tied example gives C = 0.9833333333333333 below 1 and120 h_corrected = h / C above the raw H.121- Confirm rank invariance: multiplying every observation by 10 leaves122 H unchanged because only the rank assignment feeds the statistic.123- Confirm every undersized, non-finite and out-of-range input raises124 ValueError, and that repeated calls return identical reports.125- Run the contract test offline: python3126 scripts/test_kruskal_wallis_test.py (35 tests, deterministic).127128## Related leaves129130- cross-cutting/numerics/rank-based-hypothesis-testing: paired and two131 independent-group location tests, the k = 2 family this leaf does132 not cover.133- cross-cutting/numerics/hypothesis-testing: the parametric one-way134 location comparison and scale tests for normal data.135- cross-cutting/numerics/runs-test: randomness and sequence structure136 checks on a single sample.137- cross-cutting/numerics/grubbs-outlier-test: single-outlier detection138 on grouped or pooled measurements.139- cross-cutting/numerics/probability-distributions: the distributions140 behind the survival-function context.141142## Behavior contract (gate 3)143144Run the deterministic contract test (stdlib unittest, offline):145146 python3 scripts/test_kruskal_wallis_test.py147148The test covers the worked-example contract (H within 1e-9 of 7.2,149df 2, p_value 0.027323722447292528 within 1e-3 of 0.0273, verdict150reject at 0.05, per-group rank sums 6, 15, 24), the chi-square survival151anchor exp(-3.6) at df 2, average-rank assignment for ties, the ties152correction denominator and the h_corrected = H / C identity,153all-identical groups with H = 0 and p = 1, rank invariance under154monotone transforms, the rank-sum total identity N (N + 1) / 2, report155dict keys and determinism, and ValueError rejection of two groups, a156single-observation group, non-finite values, negative corrected157statistics, and alpha outside (0, 1).158159## Compliance160161- Standards referenced, not reproduced: NACA-TR-824 is a public-domain162 NACA technical report; the Kruskal-Wallis relations above are163 standard engineering methodology, summary-only per standards-map.yaml.164- compliance: STANDARDS-REF, gated: false.