Stats kruskalwallis test - CyrilB1531/lodestar GitHub Wiki

Development build. This page describes main, not a released package. The latest published Lodestar.Stats is 0.4.0 β€” read its documentation.

Home β€Ί Stats β€Ί Hypothesis tests

KruskalWallis.Test

Compares two or more groups by their ranks in the pooled sample.

public static TestResult Test(double[][] groups)
public static TestResult Test(NanPolicy nanPolicy, double[][] groups)

The policy comes first because the groups are a params array and C# allows no parameter after one β€” the shape string.Join uses, for the same reason.

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. nanPolicy says what to do with a NaN; scipy's nan_policy, defaulting to NanPolicy.Propagate.

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.

Under NanPolicy.Propagate, a NaN reaches the statistic and the p-value. The check runs 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.

Omission runs before this test's own requirements, which are unchanged by it β€” a sample left too short, or a pool left fully tied, is refused exactly as it would be if passed directly.

Applies to β€” net10.0, netstandard2.0.

See also β€” OneWayAnova.Test for the parametric counterpart, MannWhitney.Test, the Python equivalence table.