# Test whether two samples differ

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

**Few items**

```ts
import { mannWhitneyUTest } from 'knotkit/hypothesis-test'

// Page load times in milliseconds, before and after a change.
const before = [412, 398, 455, 430, 441, 420, 409, 437]
const after = [380, 372, 401, 366, 390, 384, 377, 395]

const result = mannWhitneyUTest(before, after)

result.pValue < 0.05 // true
result.pValue.toFixed(4) // '0.0003'
```

**Thousands of items**

```ts
import { logNormalDistribution } from 'knotkit/distribution'
import { mannWhitneyUTest } from 'knotkit/hypothesis-test'
import { splitmix32 } from 'knotkit/prng'

// Page load times in milliseconds, before and after a change, drawn from two seeded right-skewed models.
const before = logNormalDistribution({ logMean: Math.log(400), logStandardDeviation: 0.5 })
const after = logNormalDistribution({ logMean: Math.log(380), logStandardDeviation: 0.5 })
const beforeRandom = splitmix32(1)
const afterRandom = splitmix32(2)

const beforeSamples = Array.from({ length: 5_000 }, () => before.draw(beforeRandom))
const afterSamples = Array.from({ length: 5_000 }, () => after.draw(afterRandom))

const result = mannWhitneyUTest(beforeSamples, afterSamples)

const median = (samples: readonly number[]): number => {
  const sorted = samples.toSorted((left, right) => left - right)

  return (sorted[sorted.length / 2 - 1]! + sorted[sorted.length / 2]!) / 2
}

beforeSamples.length // 5000
result.pValue < 0.001 // true, the samples differ
Math.round(median(beforeSamples)) // 397
Math.round(median(afterSamples)) // 379, the medians say by how much
```

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`](/modules/hypothesis-test/) · [API reference](/reference/knotkit/hypothesis-test/)
