The Kruskal–Wallis statistic is the analysis of variance of the joint ranks of $k$ groups, $H = \frac{12}{N(N+1)}\sum_i n_i\left(\bar{R}_i - \frac{N+1}{2}\right)^2$, divided by a tie factor $C$. Under the null hypothesis of identical populations it is approximately chi-squared with $k - 1$ degrees of freedom, a little conservatively: for four groups of 10, a test at 0.05 has actual level about 0.044. For two groups $H$ is the square of the rank-sum test's $z$. On four NHANES age groups of 10 adults, $H = 9.141$ and $p = 0.0275$ (permutation estimate 0.0223), and Dunn's comparisons separate only the youngest and oldest groups. The test detects a tendency of some groups to have larger values, which is a difference in means or medians only when the shapes agree. It does not repair unequal spreads.
One-Way Analysis of Variance compared the systolic blood pressures of 10 adults from each of four age groups in the NHANES survey of 2017 to 2018. It found $F = 5.716$ on 3 and 36 degrees of freedom, $p = 0.0026$. The $F$ test assumes that each group is a sample from a normal population and that the populations share a variance. Blood pressure in these age groups is right-skewed, and its spread nearly doubles from the youngest group to the oldest. The analysis of variance is fairly tolerant of the first problem and, with equal group sizes, of the second. Could we compare the four groups without needing either assumption?
William Kruskal and W. Allen Wallis (1952) proposed doing it with ranks. Given several samples, rank all the observations together from 1 to $N$ and add up the ranks in each sample. Then ask whether the rank sums differ more than random assignment of the ranks would make them. Their statistic, $H$, is to several groups what the Wilcoxon rank-sum test is to two, and they show that for two groups it is that test. The only assumptions they list for its use are that "the observations are all independent, that all those within a given sample come from a single population, and that the $C$ populations are of approximately the same form" (§1.3).
First we derive $H$ as the analysis of variance of the ranks, with its correction for ties, and work the age-group example by hand. Then we check the chi-squared approximation against exact counts and simulation, and show that for two groups $H$ is the square of the rank-sum test's $z$. Finally we examine what the test detects, how it behaves when the groups' spreads differ, how much power it has against the $F$ test, and how to find which groups differ. Four worked problems close the article. The article assumes One-Way Analysis of Variance and The Wilcoxon Rank-Sum Test.
Notation
There are $k$ groups (Kruskal and Wallis write $C$), of sizes $n_1, \dots, n_k$, with $N = \sum n_i$ observations in all. All $N$ observations are ranked together from 1 (smallest) to $N$ (largest), tied values receiving the mean of the ranks they span. For group $i$,
- $R_i$ is the sum of its ranks, and $\bar{R}_i = R_i/n_i$ its mean rank;
- the overall mean rank is $\bar{R} = (N+1)/2$, since the ranks $1, \dots, N$ add up to $N(N+1)/2$.
The null hypothesis is that the $k$ samples come from the same population, $H_0: F_1 = F_2 = \cdots = F_k$, where $F_i$ is the distribution of the measurement in population $i$. The alternative, as for the analysis of variance, is that they do not all agree. What kind of disagreement the test detects is taken up below.
The statistic
Here is the plan of the derivation. Under the null hypothesis the ranks are a known population, the integers 1 to $N$, so we know in advance how much a group's mean rank varies by chance. $H$ is the analysis of variance's between-groups sum of squares computed on the ranks, measured against that known variation instead of an estimate.
The ranks as a population
If $H_0$ holds and the measurements are continuous, all $N$ observations are independent draws from one distribution. Every assignment of the ranks $1, \dots, N$ to the $N$ positions is then equally likely, by the relabeling argument of the rank-sum article. The ranks of group $i$ are a random sample of $n_i$ drawn without replacement from the integers $1, \dots, N$, as for the rank-sum test. That population of integers has mean $(N+1)/2$ and variance
$$\sigma_r^2 = \frac{1}{N}\sum_{j=1}^N j^2 - \left(\frac{N+1}{2}\right)^2 = \frac{(N+1)(2N+1)}{6} - \frac{(N+1)^2}{4} = \frac{N^2 - 1}{12}.$$
The mean of a sample of $n_i$ drawn without replacement from a population of $N$ has the population mean as its mean. Its variance is $(\sigma_r^2/n_i)(N - n_i)/(N - 1)$, the usual variance of a mean reduced by the finite population correction, as The Sampling Distribution of the Mean proves. So
$$E(\bar{R}_i) = \frac{N+1}{2}, \qquad \operatorname{Var}(\bar{R}_i) = \frac{N^2 - 1}{12\,n_i}\cdot\frac{N - n_i}{N - 1} = \frac{(N+1)(N - n_i)}{12\,n_i},$$
which is the formula Kruskal and Wallis (1952, equation 3.1) give.
H as an analysis of variance of the ranks
The natural measure of how far the mean ranks are from what $H_0$ predicts is the between-groups sum of squares of the analysis of variance, computed on the ranks:
$$SSB_r = \sum_{i=1}^k n_i\left(\bar{R}_i - \frac{N+1}{2}\right)^2.$$
In the analysis of variance this sum is divided by an estimate of the variance computed from the data. Here no estimate is needed. The total variation of the ranks is known in advance, because the ranks are always the integers 1 to $N$. Their total sum of squares is
$$SST_r = \sum_{j=1}^N\left(j - \frac{N+1}{2}\right)^2 = N\sigma_r^2 = \frac{N(N^2-1)}{12},$$
and the Kruskal–Wallis statistic is
$$H = (N - 1)\,\frac{SSB_r}{SST_r} = \frac{12}{N(N+1)}\sum_{i=1}^k n_i\left(\bar{R}_i - \frac{N+1}{2}\right)^2.$$
It is $N - 1$ times the share of the variation in the ranks that lies between groups, the rank version of the $\eta^2$ of the analysis of variance. Expanding the square gives a form that is easier to compute. Since $n_i\bar{R}_i = R_i$ and $\sum R_i = N(N+1)/2$,
$$\sum_{i=1}^k n_i\left(\bar{R}_i - \frac{N+1}{2}\right)^2 = \sum_{i=1}^k \frac{R_i^2}{n_i} - (N+1)\sum_{i=1}^k R_i + \frac{N(N+1)^2}{4} = \sum_{i=1}^k \frac{R_i^2}{n_i} - \frac{N(N+1)^2}{4},$$
and multiplying by $12/(N(N+1))$ gives the computing formula of Kruskal and Wallis (1952, equation 1.2) and of the NIST/SEMATECH e-Handbook (§7.4.1):
$$H = \frac{12}{N(N+1)}\sum_{i=1}^k \frac{R_i^2}{n_i} - 3(N+1).$$
Large values of $H$ mean that the groups occupy different parts of the joint ranking, and only large values lead to rejection. The next subsection gives the reference distribution that says how large is large.
The chi-squared approximation
Here is the idea: $H$ is a weighted sum of squared standardized mean ranks, and such a sum is approximately chi-squared.
The formal version. Write $Z_i$ for the standardized mean rank of group $i$. By the formula for $\operatorname{Var}(\bar{R}_i)$,
$$Z_i = \frac{\bar{R}_i - (N+1)/2}{\sqrt{(N+1)(N-n_i)/(12\,n_i)}}, \qquad \left(1 - \frac{n_i}{N}\right)Z_i^2 = \frac{N - n_i}{N}\cdot\frac{12\,n_i\left(\bar{R}_i - \frac{N+1}{2}\right)^2}{(N+1)(N - n_i)} = \frac{12\,n_i\left(\bar{R}_i - \frac{N+1}{2}\right)^2}{N(N+1)}.$$
Adding over the groups gives an exact identity:
$$H = \sum_{i=1}^k \left(1 - \frac{n_i}{N}\right)Z_i^2.$$
Each $Z_i$ has mean 0 and variance 1 under $H_0$, so $E(Z_i^2) = 1$, and the mean of $H$ follows at once:
$$E(H) = \sum_{i=1}^k\left(1 - \frac{n_i}{N}\right) = k - \frac{N}{N} = k - 1,$$
exactly, for any group sizes. That is the mean of a chi-squared variable with $k - 1$ degrees of freedom. The weights $1 - n_i/N$ account for the constraint that the rank sums must add up to $N(N+1)/2$, which leaves only $k - 1$ of the mean ranks free.
The distribution itself is a limit theorem, cited here and not proved. As every $n_i$ grows, the mean ranks become jointly normal, and $H$ converges in distribution to chi-squared with $k - 1$ degrees of freedom. Kruskal and Wallis (1952) state the result and describe $H$ in these terms (their equation 1.5); the proof is in Kruskal (1952, §4), for group sizes whose shares of $N$ tend to positive limits. For two groups the limit follows from a cited result: the section on two groups proves that $H$ is the square of the rank-sum test's $z$, and Mann and Whitney (1947) proved that $z$ has a standard normal limit, so $H$ has a $\chi^2_1$ limit.
The approximation is the one used in practice: reject $H_0$ at level $\alpha$ when $H$ exceeds the $1-\alpha$ quantile of $\chi^2_{k-1}$. The NIST handbook suggests it once every group has more than four observations. The scipy documentation gives at least five per group as a typical rule.
Ties
Tied values receive midranks. This leaves every group's expected mean rank unchanged but lowers the total sum of squares of the ranks. As the signed-rank article derives, replacing $t$ consecutive ranks by their mean reduces the sum of squared deviations by $(t^3 - t)/12$. With ties, then,
$$SST_r = \frac{N(N^2 - 1)}{12} - \frac{1}{12}\sum_{\text{tie groups}}(t^3 - t) = \frac{N(N^2-1)}{12}\,C, \qquad C = 1 - \frac{\sum (t^3 - t)}{N^3 - N}.$$
The statistic is still $H = (N-1)\,SSB_r/SST_r$, with $SSB_r$ computed from the midranks. Substituting,
$$H = \frac{(N-1)\,SSB_r}{N(N^2-1)\,C/12} = \frac{1}{C}\cdot\frac{12}{N(N+1)}\,SSB_r,$$
which is the no-ties formula divided by $C$. This is Kruskal and Wallis's correction (their equations 1.3 and 1.4).
Dividing by $C$ is what keeps the null mean at $k - 1$. Given the observed midranks, each group's ranks are a sample drawn without replacement from them. Their variance is $\sigma_r^2 = SST_r/N$, so $\operatorname{Var}(\bar{R}_i) = (\sigma_r^2/n_i)(N - n_i)/(N-1)$ and
$$E(SSB_r) = \sum_{i=1}^k n_i \operatorname{Var}(\bar{R}_i) = \frac{\sigma_r^2}{N-1}\sum_{i=1}^k (N - n_i) = \frac{\sigma_r^2\,N(k-1)}{N-1} = \frac{(k-1)\,SST_r}{N-1}.$$
Hence $E(H) = (N-1)E(SSB_r)/SST_r = k - 1$, with ties or without.
Since $C < 1$, the correction always increases $H$. They note (§1.4) that it is usually small: with ten or fewer samples, and not more than a quarter of the observations tied, it does not change a chi-squared probability of 0.01 or more by more than ten percent.
Worked example: blood pressure in four age groups
Back to the four age groups. The sample is the one of the one-way ANOVA article: 10 adults drawn at random from each of four NHANES age groups (the script's seed 1). Each adult's value is the mean of up to three systolic readings. Ranking all 40 values together gives:
| Age group | Ranks | $R_i$ | $\bar{R}_i$ |
|---|---|---|---|
| 18–29 | 2, 5, 6, 13, 13, 15.5, 21, 21, 23, 28 | 147.5 | 14.75 |
| 30–44 | 1, 3, 4, 15.5, 18, 25, 26.5, 29, 32, 33 | 187.0 | 18.70 |
| 45–59 | 7, 8, 9, 10.5, 18, 18, 21, 26.5, 34, 36 | 188.0 | 18.80 |
| 60 and over | 10.5, 13, 24, 30, 31, 35, 37, 38, 39, 40 | 297.5 | 29.75 |
The rank sums add up to $820 = 40 \cdot 41/2$, as they must, and the overall mean rank is 20.5. The three younger groups have mean ranks a little below it and the oldest group one far above.
Example (ranking pulls in the highest values). The Kruskal–Wallis $p$-value for this sample is 0.0275, about ten times larger than the $F$ test's 0.0026, though still below 0.05. Ranking pulls in the oldest group's highest values, which the $F$ test counts at their full size. The three steps below compute it.
Step 1, the statistic. With $n_i = 10$,
$$\sum\frac{R_i^2}{n_i} = \frac{147.5^2 + 187.0^2 + 188.0^2 + 297.5^2}{10} = 18{,}057.55,$$
$$H = \frac{12}{40 \cdot 41}(18{,}057.55) - 3(41) = 132.128 - 123 = 9.128.$$
Step 2, ties. The 40 values contain six groups of ties, three pairs and three triples (each value is the mean of readings recorded in steps of 2 mm Hg, so coincidences are common), and
$$C = 1 - \frac{3(2^3 - 2) + 3(3^3 - 3)}{40^3 - 40} = 1 - \frac{90}{63{,}960} = 0.998593, \qquad H = \frac{9.128}{0.998593} = 9.141.$$
As Kruskal and Wallis predicted, the correction hardly matters. As a check on the derivation, the between-groups and total sums of squares of the ranks are $SSB_r = 1247.55$ and $SST_r = 5322.5$ (it would be $40(40^2 - 1)/12 = 5330$ without ties). Then $(N-1)SSB_r/SST_r = 39 \times 1247.55/5322.5 = 9.141$, as before.
Step 3, the $p$-value. The critical value of $\chi^2_3$ at $\alpha = 0.05$ is 7.815, so $H_0$ is rejected. The approximate $p$-value is $P(\chi^2_3 \ge 9.141) = 0.0275$.
The exact distribution of $H$ for these data is the distribution over all $40!/(10!)^4 \approx 4.7 \times 10^{21}$ ways to split the 40 ranks into four groups of ten. That is too many to list, but a random sample of them estimates it, as in the Monte Carlo tests of Permutation Tests. Over 30 seeds of 10,000 random splits, the proportion giving $H \ge 9.141$ had median 0.0223 (interquartile range 0.0210 to 0.0232). The chi-squared $p$-value is slightly larger than the permutation one, which anticipates the next section.
The classical $F$ test gave $p = 0.0026$ on the same data, ten times smaller. The ranks discard the information that the oldest group's highest values are far above the rest: 156 and 144.7 mm Hg count only as 40 and 39. In a sample that is not badly skewed, that information is genuine. The two tests agree that the groups differ.
The F test on the ranks
Since $H$ is a function of the between-groups share of the rank variation, so is the ordinary $F$ statistic computed on the ranks. From $H = (N-1)SSB_r/SST_r$ we have $SSB_r = H\,SST_r/(N-1)$, and so $SSW_r = SST_r - SSB_r = (N - 1 - H)\,SST_r/(N-1)$. The common factor $SST_r/(N-1)$ cancels in the ratio:
$$F_r = \frac{SSB_r/(k-1)}{SSW_r/(N-k)} = \frac{H/(k-1)}{(N - 1 - H)/(N - k)}.$$
For the example, $F_r = (9.141/3)/(29.859/36) = 3.674$, with $p = 0.0209$ from the $F_{3,36}$ distribution. Running the usual analysis of variance on the ranks is therefore a close relative of the Kruskal–Wallis test. It uses the same statistic, transformed, and refers it to a different approximate distribution.
The exact distribution and the approximation
The chi-squared distribution is a limit, and its accuracy in small samples can be measured. For small samples the exact null distribution of $H$ can be found by listing every assignment of the ranks to groups. Kruskal and Wallis (1952, §2.1) illustrated their test with artificial data on the daily output of three bottle-cap machines (samples of 5, 3 and 4 days), for which $H = 5.656$. They reported an exact probability of 0.049 for a value this large, against 0.059 from $\chi^2_2$.
The script listed all $12!/(5!\,3!\,4!) = 27{,}720$ assignments of the ranks 1 to 12 to groups of 5, 3 and 4. It found 1,348 with $H \ge 5.656$, and so reproduced their exact value, $1348/27{,}720 = 0.0486$. The chi-squared $p$-value is 0.0591, and the analysis of variance of the original numbers gives $p = 0.051$, as they also reported.
For larger designs simulation shows how good the approximation is. For four groups of 10 continuous observations from one population, 10,000 samples for each of 30 seeds gave:
- $P(H > 7.815) = 0.0439$ (interquartile range 0.0429 to 0.0457), against the nominal 0.05;
- a mean of $H$ of 2.996 over all 300,000 samples, matching both the exact null mean $k - 1 = 3$ and the chi-squared mean;
- a variance of 5.39, against 6 for $\chi^2_3$. Kruskal and Wallis (1952, equations 6.1 and 6.2) give formulas for the exact null mean and variance of $H$. For four groups of 10 the variance is 5.43, so the shortfall is a property of $H$, not of the simulation.
At these sample sizes the chi-squared approximation is somewhat conservative. Its $p$-values run a little high, and a test at the nominal 0.05 has an actual level of about 0.044. Software that can compute exact or permutation $p$-values removes the question. For three groups of up to five observations each, Kruskal and Wallis provided exact tables (their Table 6.1).
Two groups: the rank-sum test
For $k = 2$ the statistic reduces to the rank-sum test. Kruskal and Wallis (1952, §3.1) show that the standardized mean rank of one sample, squared, is $H$. Here is the computation. Write $d = \bar{R}_1 - (N+1)/2$. The two mean ranks are tied together by $n_1\bar{R}_1 + n_2\bar{R}_2 = N(N+1)/2$, which says that $n_1 d + n_2\left(\bar{R}_2 - (N+1)/2\right) = 0$. So the second group's deviation is $-n_1 d/n_2$, and
$$SSB_r = n_1 d^2 + n_2\left(\frac{n_1 d}{n_2}\right)^2 = n_1 d^2\left(1 + \frac{n_1}{n_2}\right) = \frac{n_1 N}{n_2}\,d^2.$$
With $W = R_1 = n_1\bar{R}_1$, the deviation is $d = \left(W - n_1(N+1)/2\right)/n_1$, so $SSB_r = N\left(W - n_1(N+1)/2\right)^2/(n_1 n_2)$ and
$$H = \frac{12}{N(N+1)}\,SSB_r = \frac{\left(W - n_1(N+1)/2\right)^2}{n_1 n_2 (N+1)/12} = z^2,$$
where $W$ is the Wilcoxon rank-sum statistic and $z$ its standardized value without continuity correction. With ties both sides change by the same factor. $H$ is divided by $C$, and the tie-corrected variance of $W$ is
$$\frac{n_1 n_2}{12}\left[(N+1) - \frac{\sum(t^3 - t)}{N(N-1)}\right] = \frac{n_1 n_2 (N+1)}{12}\left[1 - \frac{\sum(t^3 - t)}{N^3 - N}\right] = \frac{n_1 n_2 (N+1)}{12}\,C,$$
so $H = z^2$ still holds.
Example (20 men and 20 women). For the 20 men and 20 women of the two-sample t-test article, the rank-sum test without continuity correction gave $z = 0.609$ and $p = 0.543$, and the Kruskal–Wallis test on the same two groups gives $H = z^2 = 0.371$, with the same $p$. The script found $H = 0.37074$ (tie-corrected) and $z = 0.60888$, $z^2 = 0.37074$, with $p = 0.543$ from $\chi^2_1$. The relationship parallels $F = t^2$ for the analysis of variance of two groups.
What the test detects
The null hypothesis of the Kruskal–Wallis test is that all $k$ populations are identical. It is a valid test of that hypothesis whatever their common shape: under it, every assignment of ranks is equally likely, and that is all the exact distribution uses. When it rejects, it rejects because the groups' mean ranks differ. A group's mean rank depends on the probability that its observations exceed those of the other groups. In the analysis of variance's terms, the test responds to differences in location as measured by these probabilities, not by means.
Kruskal and Wallis listed among the assumptions for its use that the populations "are of approximately the same form". They described "detecting differences among the population means" as one of its most important applications. Under that assumption, the populations differ, if at all, by a shift. Their means, medians and every other measure of location then differ by the same amounts, and the test is a test of each of them. The NIST handbook (§7.4.1) accordingly describes the test as comparing populations "at least as far as their central tendencies, or medians, are concerned".
When the shapes differ, it is a test of neither means nor medians in general, as What the Rank-Sum Test Tests shows by example for two groups. The conclusion should then be stated in terms of the groups' tendency to have larger values.
When spreads differ
A rank test is sometimes recommended as a remedy for unequal variances. It is not one. The script repeated the experiment of the one-way ANOVA article: four normal populations with equal means and the standard deviations of the NHANES age groups (11.32, 15.36, 18.91 and 21.02 mm Hg). The population means and medians are equal, but the spreads are not.
Example (the smallest group the most variable). With group sizes 20, 15, 10 and 5, the smallest group is the most variable, and the Kruskal–Wallis test rejects too often, in 7.5% of samples: like the classical $F$ test, though by less. The proportion of samples in which each test rejected at $\alpha = 0.05$ (10,000 samples for each of 30 seeds, medians):
| Group sizes (from the least to the most variable group) | Classical $F$ | Welch's ANOVA | Kruskal–Wallis |
|---|---|---|---|
| 10, 10, 10, 10 | 0.057 | 0.051 | 0.049 |
| 20, 15, 10, 5 (smallest group most variable) | 0.116 | 0.059 | 0.075 |
| 5, 10, 15, 20 (largest group most variable) | 0.026 | 0.053 | 0.029 |
| 20, 15, 10, 5, all spreads equal | 0.049 | 0.057 | 0.044 |
With equal group sizes the Kruskal–Wallis test holds its level. With unequal sizes it fails in the same direction as the classical $F$ test, if by less: too often when the smallest group is the most variable, too rarely when the largest group is. Its null hypothesis, identical populations, is false here, since the spreads differ, and a test of identical populations is entitled to reject. But it is not a test that the centers differ. If that is the question, Welch's analysis of variance answers it better.
Power
Under a shift model, the asymptotic relative efficiency of the Kruskal–Wallis test to the $F$ test is the same expression as that of the rank-sum test to the $t$-test, a cited result stated in Asymptotic Relative Efficiency of Rank Tests: $3/\pi = 0.955$ for normal data, 1.9 for the $t$ distribution with 3 degrees of freedom and 3 for the exponential. The script compared the three tests at small sample sizes in two settings:
- four groups of 10 in a shift model, with group means $0, d, 2d, 3d$ standard deviations apart and $d = 0.493$ chosen to give the $F$ test power 0.80 for normal data;
- samples from the four NHANES age groups themselves.
| Populations | Classical $F$ | Welch's ANOVA | Kruskal–Wallis |
|---|---|---|---|
| Normal, shift model | 0.800 | 0.762 | 0.762 |
| $t$ with 3 df, shift model | 0.846 | 0.879 | 0.941 |
| Exponential, shift model | 0.805 | 0.818 | 0.924 |
| NHANES age groups, 10 per group | 0.770 | 0.765 | 0.760 |
| NHANES age groups, 5 per group | 0.390 | 0.326 | 0.349 |
For normal data the Kruskal–Wallis test loses about four percentage points of power at this size. Part of the loss comes from its chi-squared critical value being conservative: its actual level here is about 0.044, not 0.05. For heavy-tailed or strongly skewed data it gains about ten to twelve. For the NHANES age groups, moderately skewed with unequal spreads, the three tests have nearly the same power with 10 per group.
Which groups differ
A significant $H$, like a significant $F$, says only that the groups are not all alike. To find which ones differ, Dunn (1964) proposed comparing the groups' mean ranks from the joint ranking. Under $H_0$, the difference between the mean ranks of groups $i$ and $j$ has mean zero. Its variance comes from the variances and the covariance of two mean ranks drawn without replacement.
The formal version. Let $\sigma_r^2$ be the variance of the $N$ ranks or midranks. The variance of a mean rank is $\operatorname{Var}(\bar{R}_i) = (\sigma_r^2/n_i)(N - n_i)/(N-1)$, as above. For the covariance, The Sampling Distribution of the Mean proves that any two different draws without replacement have covariance $-\sigma_r^2/(N-1)$. The covariance of two means of different draws is the average of the covariances of their $n_i n_j$ pairs of draws, all equal, so $\operatorname{Cov}(\bar{R}_i, \bar{R}_j) = -\sigma_r^2/(N-1)$. Then
$$\operatorname{Var}(\bar{R}_i - \bar{R}_j) = \frac{\sigma_r^2}{N-1}\left[\frac{N - n_i}{n_i} + \frac{N - n_j}{n_j} + 2\right] = \frac{N\sigma_r^2}{N-1}\left(\frac{1}{n_i} + \frac{1}{n_j}\right),$$
because $(N - n_i)/n_i + 1 = N/n_i$. With ties, $N\sigma_r^2 = SST_r = \left[N(N^2-1) - \sum(t^3 - t)\right]/12$, and dividing by $N - 1$ gives
$$\operatorname{Var}(\bar{R}_i - \bar{R}_j) = \left[\frac{N(N+1)}{12} - \frac{\sum(t^3 - t)}{12(N-1)}\right]\left(\frac{1}{n_i} + \frac{1}{n_j}\right),$$
the bracket being $N/(N-1)$ times the variance of the ranks, reduced for ties, as in Dunn (1964). It equals $C \cdot N(N+1)/12$, with $C$ the tie factor of $H$. Each difference divided by its standard deviation is compared with the standard normal, as Dunn (1964) does; the approximation is meant for large groups. To keep the family-wise error rate at $\alpha$ over $p$ comparisons, Dunn used the quantile $z_{1-\alpha/(2p)}$: a Bonferroni correction. For the worked sample the standard deviation of a difference is $\sqrt{(40 \cdot 41/12)(0.998593)(0.2)} = 5.224$, and the six comparisons are:
| Comparison | Difference in mean ranks | $z$ | $p$ | Bonferroni ($\times 6$) |
|---|---|---|---|---|
| 18–29 vs 30–44 | 3.95 | 0.756 | 0.450 | 1 |
| 18–29 vs 45–59 | 4.05 | 0.775 | 0.438 | 1 |
| 18–29 vs 60+ | 15.00 | 2.871 | 0.0041 | 0.025 |
| 30–44 vs 45–59 | 0.10 | 0.019 | 0.985 | 1 |
| 30–44 vs 60+ | 11.05 | 2.115 | 0.034 | 0.207 |
| 45–59 vs 60+ | 10.95 | 2.096 | 0.036 | 0.217 |
Only the youngest and oldest groups are shown to differ. Holm's procedure, which the Bonferroni article describes, gives the same conclusion (adjusted $p$-values 0.025, then 0.172 for the next two).
Dunn also discussed the alternative of ranking each pair of groups separately, which amounts to a rank-sum test for each pair. A separate ranking uses only the two groups being compared, while the joint ranking places every group on the same scale. On the worked sample the separate exact rank-sum tests give:
- $p = 0.0047$ for the youngest against the oldest;
- 0.043 and 0.024 for the two middle groups against the oldest;
- 0.40, 0.47 and 0.87 for the rest.
After Holm's adjustment only the first, at 0.028, stays below 0.05. With either method, and with these ten adults per group, the evidence separates the oldest group from the youngest and cannot place the two middle groups.
Worked problems
The four problems are posed here; none is taken from the course text. The first uses the data of Kruskal and Wallis's (1952) own example, the second a design small enough to list by hand, and the last two the unit's four age groups. The script named under "In Python" checks every number.
Problem 1 (Kruskal and Wallis's bottle caps). Three machines made bottle caps, and the daily outputs on the days observed were $340,\ 345,\ 330,\ 342,\ 338$ for the first machine, $339,\ 333,\ 344$ for the second and $347,\ 343,\ 349,\ 355$ for the third (Kruskal and Wallis's artificial data). Compute $H$ and its chi-squared $p$-value, and compare with the exact $p$-value.
Solution. $H = 5.656$, with chi-squared $p = 0.059$ against the exact 0.049. The twelve values in order are $330, 333, 338, 339, 340, 342, 343, 344, 345, 347, 349, 355$, with ranks 1 to 12 and no ties. So the ranks are
| Machine | Ranks | $R_i$ | $n_i$ | $\bar{R}_i$ |
|---|---|---|---|---|
| 1 | 5, 9, 1, 6, 3 | 24 | 5 | 4.80 |
| 2 | 4, 2, 8 | 14 | 3 | 4.67 |
| 3 | 10, 7, 11, 12 | 40 | 4 | 10.00 |
The rank sums total $78 = 12 \cdot 13/2$. Then
$$\sum\frac{R_i^2}{n_i} = \frac{24^2}{5} + \frac{14^2}{3} + \frac{40^2}{4} = 115.2 + 65.333 + 400 = 580.533,$$
$$H = \frac{12}{12 \cdot 13}(580.533) - 3(13) = 44.656 - 39 = 5.656.$$
There are no ties, so no correction. With $k - 1 = 2$ degrees of freedom the chi-squared $p$-value is $P(\chi^2_2 \ge 5.656) = 0.0591$, and since the critical value at $\alpha = 0.05$ is 5.991 the chi-squared test does not reject. The exact distribution, from all 27,720 assignments of the twelve ranks to groups of 5, 3 and 4, gives $1348/27{,}720 = 0.0486$, as computed in the section on the exact distribution. So the exact test rejects at 0.05 and the approximation does not. The third machine's mean rank, 10.0, is far above the other two.
Problem 2 (the exact distribution for groups of 2, 1 and 1). Four untied observations fall in three groups of sizes 2, 1 and 1. List the exact null distribution of $H$, and find its mean.
Solution. $H$ takes the values 0.3, 1.8 and 2.7 with probabilities $\tfrac16$, $\tfrac13$ and $\tfrac12$, and its mean is exactly $2 = k - 1$.
With $N = 4$ the formula is $H = \tfrac{12}{20}\left[R_1^2/2 + R_2^2 + R_3^2\right] - 15$. The group of two receives one of the $\binom{4}{2} = 6$ pairs of ranks, and the other two ranks go one to each single group, in 2 ways, for $6 \times 2 = 12$ equally likely assignments. Swapping the two single groups does not change $R_2^2 + R_3^2$, so $H$ depends only on the pair:
| Ranks of the group of two | $R_1^2/2$ | $R_2^2 + R_3^2$ | Sum | $H$ |
|---|---|---|---|---|
| 1, 2 | 4.5 | $9 + 16 = 25$ | 29.5 | 2.7 |
| 1, 3 | 8 | $4 + 16 = 20$ | 28 | 1.8 |
| 1, 4 | 12.5 | $4 + 9 = 13$ | 25.5 | 0.3 |
| 2, 3 | 12.5 | $1 + 16 = 17$ | 29.5 | 2.7 |
| 2, 4 | 18 | $1 + 9 = 10$ | 28 | 1.8 |
| 3, 4 | 24.5 | $1 + 4 = 5$ | 29.5 | 2.7 |
For example, the first row gives $H = 0.6 \times 29.5 - 15 = 2.7$. Each pair has probability $\tfrac16$, so $P(H = 2.7) = \tfrac36$, $P(H = 1.8) = \tfrac26$ and $P(H = 0.3) = \tfrac16$. The mean is
$$\frac{3(2.7) + 2(1.8) + 0.3}{6} = \frac{12}{6} = 2,$$
as the general result $E(H) = k - 1$ requires. The largest possible value, 2.7, has probability one half, so with these sizes the test can never reject. The chi-squared distribution with 2 degrees of freedom, which puts probability 0.05 above 5.99, is no guide at all for a design this small.
Problem 3 (two groups: the youngest and the oldest). Apply the Kruskal–Wallis test to the youngest and oldest age groups alone, and compare with the rank-sum test of the same two groups.
Solution. $H = 7.434$ with $p = 0.0064$, and $H$ is the square of the rank-sum test's $z = 2.726$. The second worked problem of the rank-sum article ranks these twenty values. The rank sums are 141 for the oldest group and 69 for the youngest, and the tie groups have sizes 3 and 2. So
$$\sum\frac{R_i^2}{n_i} = \frac{141^2 + 69^2}{10} = 2464.2, \qquad \frac{12}{20 \cdot 21}(2464.2) - 3(21) = 70.406 - 63 = 7.406,$$
$$C = 1 - \frac{(3^3 - 3) + (2^3 - 2)}{20^3 - 20} = 1 - \frac{30}{7980} = 0.99624, \qquad H = \frac{7.406}{0.99624} = 7.434.$$
With one degree of freedom, $p = P(\chi^2_1 \ge 7.434) = 0.0064$. The rank-sum article found, without the continuity correction, $z = 36/13.204 = 2.726$, and $2.7265^2 = 7.434$ (carrying one more digit of $z$). The two-sided normal $p$-value of that $z$ is the same 0.0064, because $\chi^2_1$ is the distribution of the square of a standard normal variable.
Problem 4 (the three older groups). Leave out the youngest group, and test whether the three older groups (30 to 44, 45 to 59, and 60 and over) differ. Their values, to 0.1 mm Hg, are in the "In Python" section.
Solution. $H = 6.191$ on 2 degrees of freedom, $p = 0.045$: the three groups differ at the 0.05 level, though not by much. The 30 values must be ranked again from 1 to 30, since the ranks in the article's table come from the ranking of all 40. The new ranks:
| Age group | Ranks | $R_i$ | $\bar{R}_i$ |
|---|---|---|---|
| 30–44 | 1, 2, 3, 10, 12, 16, 17.5, 19, 22, 23 | 125.5 | 12.55 |
| 45–59 | 4, 5, 6, 7.5, 12, 12, 14, 17.5, 24, 26 | 128.0 | 12.80 |
| 60 and over | 7.5, 9, 15, 20, 21, 25, 27, 28, 29, 30 | 211.5 | 21.15 |
The rank sums total $465 = 30 \cdot 31/2$. Three values of 112.7 share ranks 11 to 13, two of 116.7 share ranks 17 and 18, and two of 108.7 share ranks 7 and 8. Then
$$\sum\frac{R_i^2}{n_i} = \frac{125.5^2 + 128.0^2 + 211.5^2}{10} = 7686.65, \qquad \frac{12}{30 \cdot 31}(7686.65) - 3(31) = 99.183 - 93 = 6.183,$$
$$C = 1 - \frac{(2^3 - 2) + (3^3 - 3) + (2^3 - 2)}{30^3 - 30} = 1 - \frac{36}{26{,}970} = 0.998665, \qquad H = \frac{6.183}{0.998665} = 6.191.$$
The critical value of $\chi^2_2$ at $\alpha = 0.05$ is 5.991, so $H_0$ is rejected, with $p = P(\chi^2_2 \ge 6.191) = 0.0453$. The two middle groups have nearly the same mean rank, 12.55 and 12.80, and the oldest group stands apart, as in the analysis of all four groups. The evidence is weaker than with four groups ($p = 0.0275$): the comparison no longer includes the youngest group, whose mean rank was the furthest from the oldest group's.
What we have learned
Ranking all the groups together and running the analysis of variance on the ranks gives Kruskal and Wallis's $H$; ranking within blocks instead gives Friedman's test, in Comparing Systems Across Many Tasks. Because the ranks are always the integers 1 to $N$, their total variation is known, and no variance needs estimating. $H$ is approximately chi-squared with $k - 1$ degrees of freedom, a little conservatively for four groups of 10. For two groups it is the square of the rank-sum test's $z$. On the four age groups it gave $H = 9.141$ and $p = 0.0275$, and Dunn's comparisons separated only the youngest and oldest groups. (The $t$-based procedures of the Bonferroni article placed the oldest group above both the 18 to 29 and the 30 to 44 groups.) Like the rank-sum test, it tests whether values in some groups tend to be larger, not whether the means or medians differ, and it does not repair unequal spreads.
The whole unit has rested on one device: under the null hypothesis, signs are fair coin tosses and ranks are a random arrangement, so null distributions come from counting. The sign test was the simplest case, a binomial count of plus signs with $p = \tfrac12$. The next unit, Inference on Proportions, takes up counts of yes-or-no outcomes with any probability. It begins with the normal approximation to the binomial, the same approximation the sign test used for large samples. Before it, Asymptotic Relative Efficiency of Rank Tests collects the efficiencies of all the unit's tests.
In Python
from scipy import stats
# the worked sample of the one-way ANOVA article: 10 adults from each NHANES age group (mm Hg)
g1 = [114.7, 110.7, 110.0, 114.0, 118.7, 114.0, 96.7, 100.0, 110.0, 94.0] # 18-29
g2 = [116.0, 116.7, 131.3, 110.7, 112.7, 88.7, 96.0, 121.3, 95.0, 127.3] # 30-44
g3 = [108.0, 112.7, 137.0, 103.3, 116.7, 114.0, 108.7, 112.7, 106.0, 133.3] # 45-59
g4 = [144.7, 156.0, 108.7, 134.0, 110.0, 122.7, 144.0, 126.0, 142.0, 115.3] # 60+
print(stats.kruskal(g1, g2, g3, g4)) # H = 9.141, p = 0.0275 (tie-corrected)
print(stats.mannwhitneyu(g1, g4)) # one pairwise follow-up: U = 14, p = 0.0072
Rounding the values to 0.1 mm Hg keeps the same ranks and ties, so kruskal reproduces $H = 9.141$. With ties present mannwhitneyu uses the normal approximation, which gives 0.0072 for the youngest against the oldest group; the exact conditional value in the text, 0.0047, comes from the script. Dunn's comparisons are not in scipy and take a few lines from the mean ranks and the variance above. The outputs shown were checked with scipy 1.17.1, and every number in this article is computed by tools/experiments/applied-statistics/kruskal-wallis-test.py in the site's repository.
References
- Dunn, O. J. (1964). Multiple comparisons using rank sums. Technometrics, 6(3), 241–252. record · 2 pages
- Kruskal, W. H. (1952). A nonparametric test for the several sample problem. The Annals of Mathematical Statistics, 23(4), 525–540. record
- Kruskal, W. H., and Wallis, W. A. (1952). Use of ranks in one-criterion variance analysis. Journal of the American Statistical Association, 47(260), 583–621. record · 4 pages
- Mann, H. B., and Whitney, D. R. (1947). On a test of whether one of two random variables is stochastically larger than the other. The Annals of Mathematical Statistics, 18(1), 50–60. record · 4 pages
- National Center for Health Statistics (2020). National Health and Nutrition Examination Survey 2017–2018: Demographic Variables and Sample Weights (DEMO_J) and Blood Pressure (BPX_J) data files. Centers for Disease Control and Prevention. https://wwwn.cdc.gov/nchs/nhanes/continuousnhanes/default.aspx?BeginYear=2017 record · 109 pages
- NIST/SEMATECH. e-Handbook of Statistical Methods, §7.4.1 (How can we compare several populations with unknown distributions (the Kruskal-Wallis test)?). National Institute of Standards and Technology. https://www.itl.nist.gov/div898/handbook/ record · 37 pages
- SciPy.
scipy.stats.kruskal. SciPy reference documentation. https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.kruskal.html record
Reviewed: verified, 16 Nov 2026 · checked against sources, 23 Nov 2026 · citations checked, 16 Nov 2026 · editorial review, 23 Nov 2026 · 11 revisions