Stats 0.3.0 chisquare goodnessoffit - CyrilB1531/lodestar GitHub Wiki
Lodestar.Stats 0.3.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.
Tests observed counts against an expected distribution.
public static TestResult GoodnessOfFit(ReadOnlySpan<double> observed, ReadOnlySpan<double> expected = default)Parameters — observed are the observed counts, at least two categories; the span is read,
never modified. expected are the expected counts, which must sum to the observed total; omit
them for a uniform expectation across every category, which is what scipy.stats.chisquare does
with f_exp=None.
Returns — TestResult: the statistic, and the upper-tail p-value.
Exceptions — ArgumentException when there are fewer than two categories, observed and
expected differ in length, an expectation is not positive, or the expectations do not sum to
the observations.
Example — six faces of a die, rolled 88 times.
using Lodestar.Stats;
double[] rolls = [16.0, 18.0, 16.0, 14.0, 12.0, 12.0];
TestResult result = ChiSquare.GoodnessOfFit(rolls);
double statistic = result.Statistic; // => 2
double p = Math.Round(result.PValue, 6); // => 0.849145Remarks — a p-value this large says the rolls are entirely consistent with a fair die; the
uniform expectation here is 88 / 6 in every category, since expected was omitted. Passing an
explicit expected answers a different question — not "is this uniform?" but "does this match
this distribution?" — and it must sum to within 1e-8 of the observed total, relative to that
total, or the p-value would be comparing tables of different sizes.
A NaN or an infinity propagates. There is no nan_policy here: a NaN or an infinite value
anywhere in observed drives statistic itself to NaN (an inf - inf, then an inf / inf,
inside the loop above), and the p-value follows it rather than throwing the
ArgumentOutOfRangeException calling the incomplete gamma function on a NaN would otherwise
raise. Compare ChiSquare.Contingency, which raises
ArgumentException on a NaN cell instead, unchanged by this rule.
Applies to — net10.0, netstandard2.0.
See also — ChiSquare.Contingency, FisherExact.Test
for a 2×2 table at any sample size, the Python equivalence table.