Skip to content

Test whether two samples differ

The Mann–Whitney U test compares two independent samples without assuming they are normal.

import {
function mannWhitneyUTest(first: ArrayLike<number>, second: ArrayLike<number>, options?: MannWhitneyUTestOptions): MannWhitneyUTestResult

Mann-Whitney U rank test of whether two independent samples come from the same distribution, matching scipy.stats.mannwhitneyu. greater tests that values of first tend to exceed values of second. The statistic is the U of first.

auto uses the exact null distribution when there are no tied values and the smaller sample has at most 8 values, which is SciPy's rule, provided the distribution costs at most 2e7 steps to build; otherwise it uses the normal approximation with tie correction. exact with tied values truncates the statistic as SciPy does. Building the exact distribution takes O(m² n) time and O(m n) space for sizes m <= n.

When every value is equal the variance is zero. SciPy returns NaN there; this function throws.

@throws ― {HypothesisTestInvalidInputError} With EMPTY_SAMPLE, NON_FINITE_SAMPLE, INVALID_OPTION (also for an exact method beyond the work limit), or ZERO_VARIANCE.

mannWhitneyUTest
} from 'knotkit/hypothesis-test'
// Page load times in milliseconds, before and after a change.
const
const before: number[]
before
= [412, 398, 455, 430, 441, 420, 409, 437]
const
const after: number[]
after
= [380, 372, 401, 366, 390, 384, 377, 395]
const
const result: MannWhitneyUTestResult
result
=
function mannWhitneyUTest(first: ArrayLike<number>, second: ArrayLike<number>, options?: MannWhitneyUTestOptions): MannWhitneyUTestResult

Mann-Whitney U rank test of whether two independent samples come from the same distribution, matching scipy.stats.mannwhitneyu. greater tests that values of first tend to exceed values of second. The statistic is the U of first.

auto uses the exact null distribution when there are no tied values and the smaller sample has at most 8 values, which is SciPy's rule, provided the distribution costs at most 2e7 steps to build; otherwise it uses the normal approximation with tie correction. exact with tied values truncates the statistic as SciPy does. Building the exact distribution takes O(m² n) time and O(m n) space for sizes m <= n.

When every value is equal the variance is zero. SciPy returns NaN there; this function throws.

@throws ― {HypothesisTestInvalidInputError} With EMPTY_SAMPLE, NON_FINITE_SAMPLE, INVALID_OPTION (also for an exact method beyond the work limit), or ZERO_VARIANCE.

mannWhitneyUTest
(
const before: number[]
before
,
const after: number[]
after
)
const result: MannWhitneyUTestResult
result
.
HypothesisTestResult.pValue: Probability

Null probability of a result at least as extreme as the observed one.

pValue
< 0.05 // true
const result: MannWhitneyUTestResult
result
.
HypothesisTestResult.pValue: Probability

Null probability of a result at least as extreme as the observed one.

pValue
.
Number.toFixed(fractionDigits?: number): string

Returns a string representing a number in fixed-point notation.

@param ― fractionDigits Number of digits after the decimal point. Must be in the range 0 - 20, inclusive.

toFixed
(4) // '0.0003'

A small p-value says the samples differ, not by how much, so report a difference such as the medians beside it. Its p-values agree with SciPy to 1e-9. For a difference in means with roughly normal data, use welchTTest. For paired measurements, use pairedTTest.

Modules: knotkit/hypothesis-test · API reference