Stats 0.2.0 kruskalwallis test - CyrilB1531/lodestar GitHub Wiki
Lodestar.Stats 0.2.0. This page is frozen at that release. Read the current documentation for what
mainsays now. A link to a decision or a migration page followsmain, and leaves the archive.
KruskalWallis.Test
Compares two or more groups by their ranks in the pooled sample.
public static TestResult Test(double[][] groups)
Parameters β groups are the samples to compare, at least two, each holding at least one
value β scipy.stats.kruskal takes its samples the same way, one array per group, which
groups is params for.
Returns β TestResult: the H statistic, and the upper-tail p-value.
Exceptions β ArgumentException when there are fewer than two groups, a group is empty, or
every value in the pooled sample is tied.
Example β the same three shifts OneWayAnova.Test compares.
using Lodestar.Stats;
double[] morning = [12.0, 14.0, 11.0, 13.0, 15.0];
double[] afternoon = [16.0, 15.0, 18.0, 17.0, 14.0];
double[] evening = [21.0, 19.0, 22.0, 20.0, 23.0];
TestResult result = KruskalWallis.Test(morning, afternoon, evening);
double h = Math.Round(result.Statistic, 4); // => 11.6215
double p = Math.Round(result.PValue, 6); // => 0.002995
Remarks β a fully tied pooled sample throws, where OneWayAnova.Test's
analogous input answers NaN.
using Lodestar.Stats;
string message = "nothing was thrown";
try
{
KruskalWallis.Test([5.0, 5.0], [5.0, 5.0]);
}
catch (ArgumentException error)
{
message = error.Message;
}
string what = message; // => Every value in the pooled sample is tiedβ¦
The tie correction this statistic divides by is 1 - (tΒ³ - t) / (nΒ³ - n) for a tie group
spanning t of the n pooled values; when every value is tied, t = n and the correction is
exactly 0 β not close to zero, not a value a tolerance would need to catch β so the division
that would follow is refused instead of silently producing an infinite or NaN statistic from
ranks that carry no information at all.
A NaN propagates. There is no nan_policy here: a NaN anywhere in any group makes the
statistic and the p-value NaN, checked before ranking β unguarded, Array.Sort sorts a NaN to
the front and it would take a finite rank like any other value, the same failure mode
MannWhitney.Test shares and guards against the same way.
Applies to β net10.0, netstandard2.0.
See also β OneWayAnova.Test for the parametric counterpart,
MannWhitney.Test, the Python equivalence table.