Skip to content

knotkit/hypothesis-test

Computes t-test, ANOVA, rank-test, goodness-of-fit, and exact-test p-values for counts, samples, and contingency tables

import {
type Probability = number & {
readonly [probabilityBrand]: true;
}
const Probability: {
readonly make: (value: number) => Probability;
}

A number in the closed unit interval, checked by

Probability.make

.

Checked constructor for the opaque

Probability

scalar.

Probability
,
function binomialTest(successes: number, trials: number, options?: BinomialTestOptions): BinomialTestResult

Test the success probability of independent Bernoulli trials. One-sided tails include the observed count. The two-sided test sums all outcomes whose mass is at most that of the observed count, with a relative allowance of 1e-7. Uses O(trials) time and constant working storage.

@throws ― {HypothesisTestInvalidInputError} With INVALID_COUNT, SUCCESSES_EXCEED_TRIALS, or INVALID_OPTION.

@throws ― {DistributionInvalidInputError} With INVALID_PARAMETER outside the supported binomial parameter domain.

binomialTest
,
function chiSquaredGoodnessOfFitTest(observed: ArrayLike<number>, probabilities: ArrayLike<Probability>, options?: ChiSquaredGoodnessOfFitTestOptions): ChiSquaredGoodnessOfFitTestResult

Compare category counts against probabilities scaled by the observed total. The probability sum must lie within 1e-12 of one. The calculation preserves the supplied probabilities. It drops categories with two zero counts and returns a zero p-value when any positive observation has zero expectation.

Pooling works inward from both tails and merges any remaining low interior categories toward larger indices. The p-value is the upper tail of the asymptotic chi-squared distribution. Time and working storage are linear in the category count.

@throws ― {HypothesisTestInvalidInputError} With INVALID_COUNT for an invalid count or unsafe total, LENGTH_MISMATCH, TOO_FEW_CATEGORIES, INVALID_PROBABILITIES, ZERO_TOTAL_COUNT, INVALID_OPTION for an invalid pooling threshold, or INVALID_DEGREES_OF_FREEDOM.

@throws ― {DistributionInvalidInputError} With INVALID_PARAMETER when the degrees of freedom exceed the supported maximum.

chiSquaredGoodnessOfFitTest
,
function kruskalWallisTest(groups: ArrayLike<ArrayLike<number>>): KruskalWallisTestResult

Kruskal-Wallis H-test of whether k >= 2 independent groups come from the same distribution, matching scipy.stats.kruskal. The p-value is the upper tail of the chi-squared distribution with k - 1 degrees of freedom, so there is no alternative, and it is an approximation that is unreliable for groups of fewer than about five values. Uses O(N log N) time and O(N) space for N observations.

When every observation is equal the tie correction is zero. SciPy returns NaN there; this function throws.

@throws ― {HypothesisTestInvalidInputError} With TOO_FEW_GROUPS below two groups, EMPTY_SAMPLE, NON_FINITE_SAMPLE, or ZERO_VARIANCE.

@throws ― {DistributionInvalidInputError} With INVALID_PARAMETER above the supported number of groups.

kruskalWallisTest
,
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
,
function oneWayAnova(groups: ArrayLike<ArrayLike<number>>): OneWayAnovaResult

One-way analysis of variance of whether k >= 2 independent groups share a mean, matching scipy.stats.f_oneway. The p-value is the upper tail of the F distribution, so there is no alternative. Groups may have different sizes and a group may hold a single value, provided the total exceeds k. Uses O(N) time and constant working storage for N observations.

When every group is constant, the within-group mean square is zero. SciPy returns NaN or an infinite statistic there; this function throws.

@throws ― {HypothesisTestInvalidInputError} With TOO_FEW_GROUPS below two groups, EMPTY_SAMPLE, TOO_SMALL_SAMPLE when the observations do not exceed the group count, NON_FINITE_SAMPLE, or ZERO_VARIANCE.

@throws ― {DistributionInvalidInputError} With INVALID_PARAMETER above 1e5 degrees of freedom.

oneWayAnova
,
function welchTTest(first: ArrayLike<number>, second: ArrayLike<number>, options?: WelchTTestOptions): WelchTTestResult

Welch's unequal-variance t-test of whether two independent samples share a mean, matching scipy.stats.ttest_ind(first, second, equal_var=False). The statistic is (mean₁ - mean₂) / sqrt(s₁²/m + s₂²/n), and the degrees of freedom follow Welch–Satterthwaite, so they are usually fractional. greater tests a first mean above the second. Uses O(m + n) time and constant working storage.

Two constant samples have zero standard error. SciPy returns NaN or an infinite statistic there; this function throws. One constant sample alongside a varying one is accepted.

@throws ― {HypothesisTestInvalidInputError} With EMPTY_SAMPLE, TOO_SMALL_SAMPLE below two values in either sample, NON_FINITE_SAMPLE, ZERO_VARIANCE, or INVALID_OPTION.

@throws ― {DistributionInvalidInputError} With INVALID_PARAMETER above 1e5 degrees of freedom.

welchTTest
,
} from 'knotkit/hypothesis-test'
const
const fairCoin: BinomialTestResult
fairCoin
=
function binomialTest(successes: number, trials: number, options?: BinomialTestOptions): BinomialTestResult

Test the success probability of independent Bernoulli trials. One-sided tails include the observed count. The two-sided test sums all outcomes whose mass is at most that of the observed count, with a relative allowance of 1e-7. Uses O(trials) time and constant working storage.

@throws ― {HypothesisTestInvalidInputError} With INVALID_COUNT, SUCCESSES_EXCEED_TRIALS, or INVALID_OPTION.

@throws ― {DistributionInvalidInputError} With INVALID_PARAMETER outside the supported binomial parameter domain.

binomialTest
(63, 100)
const
const ratio: BinomialTestResult
ratio
=
function binomialTest(successes: number, trials: number, options?: BinomialTestOptions): BinomialTestResult

Test the success probability of independent Bernoulli trials. One-sided tails include the observed count. The two-sided test sums all outcomes whose mass is at most that of the observed count, with a relative allowance of 1e-7. Uses O(trials) time and constant working storage.

@throws ― {HypothesisTestInvalidInputError} With INVALID_COUNT, SUCCESSES_EXCEED_TRIALS, or INVALID_OPTION.

@throws ― {DistributionInvalidInputError} With INVALID_PARAMETER outside the supported binomial parameter domain.

binomialTest
(682, 925, {
BinomialTestOptions.successProbability?: Probability | undefined

Common success probability under the null. Defaults to one half.

successProbability
:
const Probability: {
readonly make: (value: number) => Probability;
}

A number in the closed unit interval, checked by

Probability.make

.

Checked constructor for the opaque

Probability

scalar.

Probability
.
make: (value: number) => Probability

@throws ― {DistributionInvalidInputError} With INVALID_PROBABILITY outside the closed unit interval.

make
(0.75),
})
const
const fit: ChiSquaredGoodnessOfFitTestResult
fit
=
function chiSquaredGoodnessOfFitTest(observed: ArrayLike<number>, probabilities: ArrayLike<Probability>, options?: ChiSquaredGoodnessOfFitTestOptions): ChiSquaredGoodnessOfFitTestResult

Compare category counts against probabilities scaled by the observed total. The probability sum must lie within 1e-12 of one. The calculation preserves the supplied probabilities. It drops categories with two zero counts and returns a zero p-value when any positive observation has zero expectation.

Pooling works inward from both tails and merges any remaining low interior categories toward larger indices. The p-value is the upper tail of the asymptotic chi-squared distribution. Time and working storage are linear in the category count.

@throws ― {HypothesisTestInvalidInputError} With INVALID_COUNT for an invalid count or unsafe total, LENGTH_MISMATCH, TOO_FEW_CATEGORIES, INVALID_PROBABILITIES, ZERO_TOTAL_COUNT, INVALID_OPTION for an invalid pooling threshold, or INVALID_DEGREES_OF_FREEDOM.

@throws ― {DistributionInvalidInputError} With INVALID_PARAMETER when the degrees of freedom exceed the supported maximum.

chiSquaredGoodnessOfFitTest
([23, 21, 30, 26], [0.25, 0.25, 0.25, 0.25].
Array<number>.map<Probability>(callbackfn: (value: number, index: number, array: number[]) => Probability, thisArg?: any): Probability[]

Calls a defined callback function on each element of an array, and returns an array that contains the results.

@param ― callbackfn A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array.

@param ― thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.

map
(
const Probability: {
readonly make: (value: number) => Probability;
}

A number in the closed unit interval, checked by

Probability.make

.

Checked constructor for the opaque

Probability

scalar.

Probability
.
make: (value: number) => Probability
make
))
const
const means: WelchTTestResult
means
=
function welchTTest(first: ArrayLike<number>, second: ArrayLike<number>, options?: WelchTTestOptions): WelchTTestResult

Welch's unequal-variance t-test of whether two independent samples share a mean, matching scipy.stats.ttest_ind(first, second, equal_var=False). The statistic is (mean₁ - mean₂) / sqrt(s₁²/m + s₂²/n), and the degrees of freedom follow Welch–Satterthwaite, so they are usually fractional. greater tests a first mean above the second. Uses O(m + n) time and constant working storage.

Two constant samples have zero standard error. SciPy returns NaN or an infinite statistic there; this function throws. One constant sample alongside a varying one is accepted.

@throws ― {HypothesisTestInvalidInputError} With EMPTY_SAMPLE, TOO_SMALL_SAMPLE below two values in either sample, NON_FINITE_SAMPLE, ZERO_VARIANCE, or INVALID_OPTION.

@throws ― {DistributionInvalidInputError} With INVALID_PARAMETER above 1e5 degrees of freedom.

welchTTest
([2.1, 3.4, 1.9, 5.6], [1.2, 2.9, 3.1, 0.8, 2.2], {
HypothesisTestAlternativeOptions.alternative?: "two-sided" | "less" | "greater" | undefined

Defaults to two-sided.

alternative
: 'greater',
})
const
const groups: OneWayAnovaResult
groups
=
function oneWayAnova(groups: ArrayLike<ArrayLike<number>>): OneWayAnovaResult

One-way analysis of variance of whether k >= 2 independent groups share a mean, matching scipy.stats.f_oneway. The p-value is the upper tail of the F distribution, so there is no alternative. Groups may have different sizes and a group may hold a single value, provided the total exceeds k. Uses O(N) time and constant working storage for N observations.

When every group is constant, the within-group mean square is zero. SciPy returns NaN or an infinite statistic there; this function throws.

@throws ― {HypothesisTestInvalidInputError} With TOO_FEW_GROUPS below two groups, EMPTY_SAMPLE, TOO_SMALL_SAMPLE when the observations do not exceed the group count, NON_FINITE_SAMPLE, or ZERO_VARIANCE.

@throws ― {DistributionInvalidInputError} With INVALID_PARAMETER above 1e5 degrees of freedom.

oneWayAnova
([
[6, 8, 4, 5],
[8, 12, 9, 11],
[13, 9, 11, 8],
])
const
const ranks: MannWhitneyUTestResult
ranks
=
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
([19, 22, 16, 29, 24], [20, 11, 17, 12])
const
const medians: KruskalWallisTestResult
medians
=
function kruskalWallisTest(groups: ArrayLike<ArrayLike<number>>): KruskalWallisTestResult

Kruskal-Wallis H-test of whether k >= 2 independent groups come from the same distribution, matching scipy.stats.kruskal. The p-value is the upper tail of the chi-squared distribution with k - 1 degrees of freedom, so there is no alternative, and it is an approximation that is unreliable for groups of fewer than about five values. Uses O(N log N) time and O(N) space for N observations.

When every observation is equal the tie correction is zero. SciPy returns NaN there; this function throws.

@throws ― {HypothesisTestInvalidInputError} With TOO_FEW_GROUPS below two groups, EMPTY_SAMPLE, NON_FINITE_SAMPLE, or ZERO_VARIANCE.

@throws ― {DistributionInvalidInputError} With INVALID_PARAMETER above the supported number of groups.

kruskalWallisTest
([
[2.9, 3.0, 2.5, 2.6],
[3.8, 2.7, 4.0, 2.4],
[2.8, 3.4, 3.7, 2.2],
])
hypothesis-test.ts
  • compute a chi-square p-value for observed counts against category probabilities
  • test whether draws fit a continuous distribution with Kolmogorov-Smirnov
  • compare the distributions of two samples
  • test one sample mean, two sample means, or paired measurements with a t-test, or three or more group means with a one-way ANOVA, or compare samples by rank with Mann-Whitney U, Wilcoxon signed-rank, or Kruskal-Wallis
  • test an observed success count using an exact binomial p-value
  • calculate a two-sided or directional Fisher exact p-value for a 2-by-2 table
{"v":1,"box":[53.2431,94.1597,1043,444.3555],"x":{"s":"linear","lim":[-0.045,1.045],"inv":false,"g":0,"t":"Observed value (dots mark the 8 observations)","tk":[[0,"0"],[0.2,"0.2"],[0.4,"0.4"],[0.6,"0.6"],[0.8,"0.8"],[1,"1"]]},"y":{"s":"linear","lim":[-0.045,1.045],"inv":false,"g":0,"tk":[[0,"0"],[0.2,"0.2"],[0.4,"0.4"],[0.6,"0.6"],[0.8,"0.8"],[1,"1"]]},"ti":"Clustered: D = 0.52, p = 0.015, reject uniform","a":[{"k":"line","c":"#fffff8","s":"x","d":[0,0,0.06,0,0.06,0.125,0.09,0.125,0.09,0.25,0.18,0.25,0.18,0.375,0.25,0.375,0.25,0.5,0.28,0.5,0.28,0.625,0.33,0.625,0.33,0.75,0.43,0.75,0.43,0.875,0.48,0.875,0.48,1,1,1],"p":[94.1,429.9,148.59,429.9,148.59,389.74,175.83,389.74,175.83,349.58,257.55,349.58,257.55,309.42,321.11,309.42,321.11,269.26,348.35,269.26,348.35,229.1,393.76,229.1,393.76,188.94,484.56,188.94,484.56,148.78,529.96,148.78,529.96,108.62,1002.14,108.62]},{"k":"line","c":"#1d5fa6","s":"x","d":[0,0,0.06,0,0.06,0.125,0.09,0.125,0.09,0.25,0.18,0.25,0.18,0.375,0.25,0.375,0.25,0.5,0.28,0.5,0.28,0.625,0.33,0.625,0.33,0.75,0.43,0.75,0.43,0.875,0.48,0.875,0.48,1,1,1],"p":[94.1,429.9,148.59,429.9,148.59,389.74,175.83,389.74,175.83,349.58,257.55,349.58,257.55,309.42,321.11,309.42,321.11,269.26,348.35,269.26,348.35,229.1,393.76,229.1,393.76,188.94,484.56,188.94,484.56,148.78,529.96,148.78,529.96,108.62,1002.14,108.62]},{"k":"line","c":"#fffff8","s":"x","d":[0,0,0.05,0.05,0.1,0.1,0.15,0.15,0.2,0.2,0.25,0.25,0.3,0.3,0.35,0.35,0.4,0.4,0.45,0.45,0.5,0.5,0.55,0.55,0.6,0.6,0.65,0.65,0.7,0.7,0.75,0.75,0.8,0.8,0.85,0.85,0.9,0.9,0.95,0.95,1,1],"p":[94.1,429.9,139.51,413.83,184.91,397.77,230.31,381.71,275.71,365.64,321.11,349.58,366.51,333.51,411.92,317.45,457.32,301.39,502.72,285.32,548.12,269.26,593.52,253.19,638.92,237.13,684.33,221.07,729.73,205,775.13,188.94,820.53,172.87,865.93,156.81,911.34,140.75,956.74,124.68,1002.14,108.62]},{"k":"line","c":"#5b6b7c","s":"x","d":[0,0,0.05,0.05,0.1,0.1,0.15,0.15,0.2,0.2,0.25,0.25,0.3,0.3,0.35,0.35,0.4,0.4,0.45,0.45,0.5,0.5,0.55,0.55,0.6,0.6,0.65,0.65,0.7,0.7,0.75,0.75,0.8,0.8,0.85,0.85,0.9,0.9,0.95,0.95,1,1],"p":[94.1,429.9,139.51,413.83,184.91,397.77,230.31,381.71,275.71,365.64,321.11,349.58,366.51,333.51,411.92,317.45,457.32,301.39,502.72,285.32,548.12,269.26,593.52,253.19,638.92,237.13,684.33,221.07,729.73,205,775.13,188.94,820.53,172.87,865.93,156.81,911.34,140.75,956.74,124.68,1002.14,108.62]},{"k":"line","c":"#fffff8","s":"x","d":[0.48,0.48,0.48,1],"p":[529.96,275.68,529.96,108.62]},{"k":"line","c":"#c4501b","s":"x","d":[0.48,0.48,0.48,1],"p":[529.96,275.68,529.96,108.62]},{"k":"scatter","c":"#1d5fa6","s":"xy","d":[0.06,0,0.09,0,0.18,0,0.25,0,0.28,0,0.33,0,0.43,0,0.48,0],"p":[148.59,429.9,175.83,429.9,257.55,429.9,321.11,429.9,348.35,429.9,393.76,429.9,484.56,429.9,529.96,429.9],"r":4.96},{"k":"segments","c":"#8c8c84"},{"k":"segments","c":"#8c8c84"},{"k":"annotation","c":"#2b2b28"},{"k":"annotation","c":"#2b2b28"},{"k":"annotation","c":"#2b2b28"},{"k":"annotation","c":"#5c5c57"}],"lg":[]} 0 0.2 0.4 0.6 0.8 1 Observed value (dots mark the 8 observations) 0 0.2 0.4 0.6 0.8 1 gap D = 0.52 empirical CDF uniform null CDF Cumulative probability Clustered: D = 0.52, p = 0.015, reject uniform {"v":1,"box":[53.2431,605.1597,1043,955.3555],"x":{"s":"linear","lim":[-0.045,1.045],"inv":false,"g":1,"t":"Observed value (dots mark the 8 observations)","tk":[[0,"0"],[0.2,"0.2"],[0.4,"0.4"],[0.6,"0.6"],[0.8,"0.8"],[1,"1"]]},"y":{"s":"linear","lim":[-0.045,1.045],"inv":false,"g":1,"tk":[[0,"0"],[0.2,"0.2"],[0.4,"0.4"],[0.6,"0.6"],[0.8,"0.8"],[1,"1"]]},"ti":"Spread: D = 0.08, p > 0.99, consistent with uniform","a":[{"k":"line","c":"#fffff8","s":"x","d":[0,0,0.06,0,0.06,0.125,0.18,0.125,0.18,0.25,0.33,0.25,0.33,0.375,0.45,0.375,0.45,0.5,0.57,0.5,0.57,0.625,0.68,0.625,0.68,0.75,0.82,0.75,0.82,0.875,0.95,0.875,0.95,1,1,1],"p":[94.1,940.9,148.59,940.9,148.59,900.74,257.55,900.74,257.55,860.58,393.76,860.58,393.76,820.42,502.72,820.42,502.72,780.26,611.68,780.26,611.68,740.1,711.57,740.1,711.57,699.94,838.69,699.94,838.69,659.78,956.74,659.78,956.74,619.62,1002.14,619.62]},{"k":"line","c":"#1d5fa6","s":"x","d":[0,0,0.06,0,0.06,0.125,0.18,0.125,0.18,0.25,0.33,0.25,0.33,0.375,0.45,0.375,0.45,0.5,0.57,0.5,0.57,0.625,0.68,0.625,0.68,0.75,0.82,0.75,0.82,0.875,0.95,0.875,0.95,1,1,1],"p":[94.1,940.9,148.59,940.9,148.59,900.74,257.55,900.74,257.55,860.58,393.76,860.58,393.76,820.42,502.72,820.42,502.72,780.26,611.68,780.26,611.68,740.1,711.57,740.1,711.57,699.94,838.69,699.94,838.69,659.78,956.74,659.78,956.74,619.62,1002.14,619.62]},{"k":"line","c":"#fffff8","s":"x","d":[0,0,0.05,0.05,0.1,0.1,0.15,0.15,0.2,0.2,0.25,0.25,0.3,0.3,0.35,0.35,0.4,0.4,0.45,0.45,0.5,0.5,0.55,0.55,0.6,0.6,0.65,0.65,0.7,0.7,0.75,0.75,0.8,0.8,0.85,0.85,0.9,0.9,0.95,0.95,1,1],"p":[94.1,940.9,139.51,924.83,184.91,908.77,230.31,892.71,275.71,876.64,321.11,860.58,366.51,844.51,411.92,828.45,457.32,812.39,502.72,796.32,548.12,780.26,593.52,764.19,638.92,748.13,684.33,732.07,729.73,716,775.13,699.94,820.53,683.87,865.93,667.81,911.34,651.75,956.74,635.68,1002.14,619.62]},{"k":"line","c":"#5b6b7c","s":"x","d":[0,0,0.05,0.05,0.1,0.1,0.15,0.15,0.2,0.2,0.25,0.25,0.3,0.3,0.35,0.35,0.4,0.4,0.45,0.45,0.5,0.5,0.55,0.55,0.6,0.6,0.65,0.65,0.7,0.7,0.75,0.75,0.8,0.8,0.85,0.85,0.9,0.9,0.95,0.95,1,1],"p":[94.1,940.9,139.51,924.83,184.91,908.77,230.31,892.71,275.71,876.64,321.11,860.58,366.51,844.51,411.92,828.45,457.32,812.39,502.72,796.32,548.12,780.26,593.52,764.19,638.92,748.13,684.33,732.07,729.73,716,775.13,699.94,820.53,683.87,865.93,667.81,911.34,651.75,956.74,635.68,1002.14,619.62]},{"k":"line","c":"#fffff8","s":"x","d":[0.33,0.33,0.33,0.25],"p":[393.76,834.88,393.76,860.58]},{"k":"line","c":"#c4501b","s":"x","d":[0.33,0.33,0.33,0.25],"p":[393.76,834.88,393.76,860.58]},{"k":"scatter","c":"#1d5fa6","s":"xy","d":[0.06,0,0.18,0,0.33,0,0.45,0,0.57,0,0.68,0,0.82,0,0.95,0],"p":[148.59,940.9,257.55,940.9,393.76,940.9,502.72,940.9,611.68,940.9,711.57,940.9,838.69,940.9,956.74,940.9],"r":4.96},{"k":"segments","c":"#8c8c84"},{"k":"segments","c":"#8c8c84"},{"k":"annotation","c":"#2b2b28"},{"k":"annotation","c":"#5c5c57"}],"lg":[]} 0 0.2 0.4 0.6 0.8 1 Observed value (dots mark the 8 observations) 0 0.2 0.4 0.6 0.8 1 gap D = 0.08 Cumulative probability Spread: D = 0.08, p > 0.99, consistent with uniform
{"v":1,"box":[53.2431,122.3921,595,444.3555],"x":{"s":"linear","lim":[-0.045,1.045],"inv":false,"g":0,"t":"Observed value (dots mark the 8 observations)","tk":[[0,"0"],[0.2,"0.2"],[0.4,"0.4"],[0.6,"0.6"],[0.8,"0.8"],[1,"1"]]},"y":{"s":"linear","lim":[-0.045,1.045],"inv":false,"g":0,"tk":[[0,"0"],[0.2,"0.2"],[0.4,"0.4"],[0.6,"0.6"],[0.8,"0.8"],[1,"1"]]},"ti":"Clustered: D = 0.52, p = 0.015, reject\nuniform","a":[{"k":"line","c":"#fffff8","s":"x","d":[0,0,0.06,0,0.06,0.125,0.09,0.125,0.09,0.25,0.18,0.25,0.18,0.375,0.25,0.375,0.25,0.5,0.28,0.5,0.28,0.625,0.33,0.625,0.33,0.75,0.43,0.75,0.43,0.875,0.48,0.875,0.48,1,1,1],"p":[75.61,431.06,105.43,431.06,105.43,394.14,120.34,394.14,120.34,357.22,165.07,357.22,165.07,320.3,199.87,320.3,199.87,283.37,214.78,283.37,214.78,246.45,239.63,246.45,239.63,209.53,289.33,209.53,289.33,172.61,314.18,172.61,314.18,135.68,572.63,135.68]},{"k":"line","c":"#1d5fa6","s":"x","d":[0,0,0.06,0,0.06,0.125,0.09,0.125,0.09,0.25,0.18,0.25,0.18,0.375,0.25,0.375,0.25,0.5,0.28,0.5,0.28,0.625,0.33,0.625,0.33,0.75,0.43,0.75,0.43,0.875,0.48,0.875,0.48,1,1,1],"p":[75.61,431.06,105.43,431.06,105.43,394.14,120.34,394.14,120.34,357.22,165.07,357.22,165.07,320.3,199.87,320.3,199.87,283.37,214.78,283.37,214.78,246.45,239.63,246.45,239.63,209.53,289.33,209.53,289.33,172.61,314.18,172.61,314.18,135.68,572.63,135.68]},{"k":"line","c":"#fffff8","s":"x","d":[0,0,0.05,0.05,0.1,0.1,0.15,0.15,0.2,0.2,0.25,0.25,0.3,0.3,0.35,0.35,0.4,0.4,0.45,0.45,0.5,0.5,0.55,0.55,0.6,0.6,0.65,0.65,0.7,0.7,0.75,0.75,0.8,0.8,0.85,0.85,0.9,0.9,0.95,0.95,1,1],"p":[75.61,431.06,100.46,416.29,125.31,401.53,150.16,386.76,175.01,371.99,199.87,357.22,224.72,342.45,249.57,327.68,274.42,312.91,299.27,298.14,324.12,283.37,348.97,268.6,373.82,253.84,398.68,239.07,423.53,224.3,448.38,209.53,473.23,194.76,498.08,179.99,522.93,165.22,547.78,150.45,572.63,135.68]},{"k":"line","c":"#5b6b7c","s":"x","d":[0,0,0.05,0.05,0.1,0.1,0.15,0.15,0.2,0.2,0.25,0.25,0.3,0.3,0.35,0.35,0.4,0.4,0.45,0.45,0.5,0.5,0.55,0.55,0.6,0.6,0.65,0.65,0.7,0.7,0.75,0.75,0.8,0.8,0.85,0.85,0.9,0.9,0.95,0.95,1,1],"p":[75.61,431.06,100.46,416.29,125.31,401.53,150.16,386.76,175.01,371.99,199.87,357.22,224.72,342.45,249.57,327.68,274.42,312.91,299.27,298.14,324.12,283.37,348.97,268.6,373.82,253.84,398.68,239.07,423.53,224.3,448.38,209.53,473.23,194.76,498.08,179.99,522.93,165.22,547.78,150.45,572.63,135.68]},{"k":"line","c":"#fffff8","s":"x","d":[0.48,0.48,0.48,1],"p":[314.18,289.28,314.18,135.68]},{"k":"line","c":"#c4501b","s":"x","d":[0.48,0.48,0.48,1],"p":[314.18,289.28,314.18,135.68]},{"k":"scatter","c":"#1d5fa6","s":"xy","d":[0.06,0,0.09,0,0.18,0,0.25,0,0.28,0,0.33,0,0.43,0,0.48,0],"p":[105.43,431.06,120.34,431.06,165.07,431.06,199.87,431.06,214.78,431.06,239.63,431.06,289.33,431.06,314.18,431.06],"r":4.96},{"k":"segments","c":"#8c8c84"},{"k":"segments","c":"#8c8c84"},{"k":"annotation","c":"#2b2b28"},{"k":"annotation","c":"#2b2b28"},{"k":"annotation","c":"#2b2b28"},{"k":"annotation","c":"#5c5c57"}],"lg":[]} 0 0.2 0.4 0.6 0.8 1 Observed value (dots mark the 8 observations) 0 0.2 0.4 0.6 0.8 1 gap D = 0.52 empirical CDF uniform null CDF Cumulative probability Clustered: D = 0.52, p = 0.015, reject uniform {"v":1,"box":[53.2431,633.3921,595,955.3555],"x":{"s":"linear","lim":[-0.045,1.045],"inv":false,"g":1,"t":"Observed value (dots mark the 8 observations)","tk":[[0,"0"],[0.2,"0.2"],[0.4,"0.4"],[0.6,"0.6"],[0.8,"0.8"],[1,"1"]]},"y":{"s":"linear","lim":[-0.045,1.045],"inv":false,"g":1,"tk":[[0,"0"],[0.2,"0.2"],[0.4,"0.4"],[0.6,"0.6"],[0.8,"0.8"],[1,"1"]]},"ti":"Spread: D = 0.08, p > 0.99, consistent\nwith uniform","a":[{"k":"line","c":"#fffff8","s":"x","d":[0,0,0.06,0,0.06,0.125,0.18,0.125,0.18,0.25,0.33,0.25,0.33,0.375,0.45,0.375,0.45,0.5,0.57,0.5,0.57,0.625,0.68,0.625,0.68,0.75,0.82,0.75,0.82,0.875,0.95,0.875,0.95,1,1,1],"p":[75.61,942.06,105.43,942.06,105.43,905.14,165.07,905.14,165.07,868.22,239.63,868.22,239.63,831.3,299.27,831.3,299.27,794.37,358.91,794.37,358.91,757.45,413.59,757.45,413.59,720.53,483.17,720.53,483.17,683.61,547.78,683.61,547.78,646.68,572.63,646.68]},{"k":"line","c":"#1d5fa6","s":"x","d":[0,0,0.06,0,0.06,0.125,0.18,0.125,0.18,0.25,0.33,0.25,0.33,0.375,0.45,0.375,0.45,0.5,0.57,0.5,0.57,0.625,0.68,0.625,0.68,0.75,0.82,0.75,0.82,0.875,0.95,0.875,0.95,1,1,1],"p":[75.61,942.06,105.43,942.06,105.43,905.14,165.07,905.14,165.07,868.22,239.63,868.22,239.63,831.3,299.27,831.3,299.27,794.37,358.91,794.37,358.91,757.45,413.59,757.45,413.59,720.53,483.17,720.53,483.17,683.61,547.78,683.61,547.78,646.68,572.63,646.68]},{"k":"line","c":"#fffff8","s":"x","d":[0,0,0.05,0.05,0.1,0.1,0.15,0.15,0.2,0.2,0.25,0.25,0.3,0.3,0.35,0.35,0.4,0.4,0.45,0.45,0.5,0.5,0.55,0.55,0.6,0.6,0.65,0.65,0.7,0.7,0.75,0.75,0.8,0.8,0.85,0.85,0.9,0.9,0.95,0.95,1,1],"p":[75.61,942.06,100.46,927.29,125.31,912.53,150.16,897.76,175.01,882.99,199.87,868.22,224.72,853.45,249.57,838.68,274.42,823.91,299.27,809.14,324.12,794.37,348.97,779.6,373.82,764.84,398.68,750.07,423.53,735.3,448.38,720.53,473.23,705.76,498.08,690.99,522.93,676.22,547.78,661.45,572.63,646.68]},{"k":"line","c":"#5b6b7c","s":"x","d":[0,0,0.05,0.05,0.1,0.1,0.15,0.15,0.2,0.2,0.25,0.25,0.3,0.3,0.35,0.35,0.4,0.4,0.45,0.45,0.5,0.5,0.55,0.55,0.6,0.6,0.65,0.65,0.7,0.7,0.75,0.75,0.8,0.8,0.85,0.85,0.9,0.9,0.95,0.95,1,1],"p":[75.61,942.06,100.46,927.29,125.31,912.53,150.16,897.76,175.01,882.99,199.87,868.22,224.72,853.45,249.57,838.68,274.42,823.91,299.27,809.14,324.12,794.37,348.97,779.6,373.82,764.84,398.68,750.07,423.53,735.3,448.38,720.53,473.23,705.76,498.08,690.99,522.93,676.22,547.78,661.45,572.63,646.68]},{"k":"line","c":"#fffff8","s":"x","d":[0.33,0.33,0.33,0.25],"p":[239.63,844.59,239.63,868.22]},{"k":"line","c":"#c4501b","s":"x","d":[0.33,0.33,0.33,0.25],"p":[239.63,844.59,239.63,868.22]},{"k":"scatter","c":"#1d5fa6","s":"xy","d":[0.06,0,0.18,0,0.33,0,0.45,0,0.57,0,0.68,0,0.82,0,0.95,0],"p":[105.43,942.06,165.07,942.06,239.63,942.06,299.27,942.06,358.91,942.06,413.59,942.06,483.17,942.06,547.78,942.06],"r":4.96},{"k":"segments","c":"#8c8c84"},{"k":"segments","c":"#8c8c84"},{"k":"annotation","c":"#2b2b28"},{"k":"annotation","c":"#5c5c57"}],"lg":[]} 0 0.2 0.4 0.6 0.8 1 Observed value (dots mark the 8 observations) 0 0.2 0.4 0.6 0.8 1 gap D = 0.08 Cumulative probability Spread: D = 0.08, p > 0.99, consistent with uniform
  • Compare 8 clustered observations against Uniform(0, 1).
  • Largest CDF gap D = 0.520. Two-sided p = 0.015 (exact).
  • Compare 8 spread observations against Uniform(0, 1).
  • Largest CDF gap D = 0.080. Two-sided p = 1.000 (exact).
  • Use a one-sample Kolmogorov-Smirnov test for a fully specified continuous null.

Hover or focus a chart for exact values. Each change reruns demo.ts against knotkit/hypothesis-test in your browser; the defaults reproduce the README figure.

ExportReturnsDoes
chiSquaredGoodnessOfFitTestChiSquaredGoodnessOfFitTestResultPearson goodness of fit with optional adjacent pooling. Construct category probabilities with Probability.make. Degrees of freedom above CHI_SQUARED_MAX_DEGREES_OF_FREEDOM surface DistributionInvalidInputError.
kolmogorovSmirnovTestKolmogorovSmirnovTestResultCompare a sample with a continuous distribution, using the exact null distribution at fewer than 100 observations without repeated values.
twoSampleKolmogorovSmirnovTestTwoSampleKolmogorovSmirnovTestResultCompare two samples, using the exact null distribution below 10,000 pairs without repeated pooled values.
oneSampleTTestOneSampleTTestResultStudent's t-test of a sample mean against an expected mean, as scipy.stats.ttest_1samp. A constant sample throws ZERO_VARIANCE where SciPy returns NaN.
twoSampleTTestTwoSampleTTestResultPooled-variance t-test of two independent samples, as scipy.stats.ttest_ind with equal_var=True. Choose welchTTest when the variances may differ.
welchTTestWelchTTestResultUnequal-variance t-test with Welch-Satterthwaite degrees of freedom, as scipy.stats.ttest_ind with equal_var=False.
pairedTTestPairedTTestResultt-test of paired measurements, as scipy.stats.ttest_rel: oneSampleTTest of the differences against zero.
oneWayAnovaOneWayAnovaResultOne-way ANOVA F-test of k >= 2 independent groups, as scipy.stats.f_oneway. The p-value is the upper tail, so it takes no alternative.
mannWhitneyUTestMannWhitneyUTestResultMann-Whitney U rank test of two independent samples, as scipy.stats.mannwhitneyu. Exact without ties when the smaller sample has at most 8 values, else a tie-corrected normal approximation. All-equal values throw ZERO_VARIANCE where SciPy returns NaN.
wilcoxonSignedRankTestWilcoxonSignedRankTestResultWilcoxon signed-rank test of paired differences, as scipy.stats.wilcoxon with zero_method wilcox: zeros are dropped and ties take average ranks. All-zero differences throw ZERO_VARIANCE.
kruskalWallisTestKruskalWallisTestResultTie-corrected Kruskal-Wallis H-test of k >= 2 independent groups, as scipy.stats.kruskal, with a chi-squared upper-tail p-value. It takes no alternative.
binomialTestBinomialTestResultTest a success count under a binomial null. Construct the null successProbability with Probability.make. Trial counts above BINOMIAL_MAX_TRIALS surface DistributionInvalidInputError.
fisherExactTestFisherExactTestResultTest the top-left count of a 2-by-2 table under its fixed margins. Tables above HYPERGEOMETRIC_MAX_POPULATION_SIZE surface DistributionInvalidInputError.

Every error this module throws is HypothesisTestInvalidInputError, with a code from a closed, exported list. Branch on the code; the message is for people.

EMPTY_SAMPLE NON_FINITE_SAMPLE INVALID_COUNT INVALID_PROBABILITIES LENGTH_MISMATCH TOO_FEW_CATEGORIES INVALID_TABLE SUCCESSES_EXCEED_TRIALS INVALID_DEGREES_OF_FREEDOM ZERO_TOTAL_COUNT INVALID_OPTION TOO_SMALL_SAMPLE TOO_FEW_GROUPS ZERO_VARIANCE
If you need to…Use instead
evaluate a probability or draw from a named distributionknotkit/distribution
analyze regression, multi-factor or repeated-measures ANOVA, or paired-group rank tests such as FriedmanSciPy or R
test chi-squared independence or Fisher exact on a larger contingency tableSciPy or R

Measured by the suite in packages/hypothesis-test/__tests__/bench. The numbers come from one machine, so run the suite on yours before deciding on speed alone.

Prompt · knotkit/hypothesis-test
Use knotkit/hypothesis-test for this task. If Knotkit is missing, follow https://knotkit.jkomyno.dev/getting-started.md#install-from-source. Import from 'knotkit/hypothesis-test'.

Exports:
- `chiSquaredGoodnessOfFitTest` → `ChiSquaredGoodnessOfFitTestResult`: Pearson goodness of fit with optional adjacent pooling. Construct category probabilities with Probability.make. Degrees of freedom above CHI_SQUARED_MAX_DEGREES_OF_FREEDOM surface DistributionInvalidInputError.
- `kolmogorovSmirnovTest` → `KolmogorovSmirnovTestResult`: Compare a sample with a continuous distribution, using the exact null distribution at fewer than 100 observations without repeated values.
- `twoSampleKolmogorovSmirnovTest` → `TwoSampleKolmogorovSmirnovTestResult`: Compare two samples, using the exact null distribution below 10,000 pairs without repeated pooled values.
- `oneSampleTTest` → `OneSampleTTestResult`: Student's t-test of a sample mean against an expected mean, as scipy.stats.ttest_1samp. A constant sample throws ZERO_VARIANCE where SciPy returns NaN.
- `twoSampleTTest` → `TwoSampleTTestResult`: Pooled-variance t-test of two independent samples, as scipy.stats.ttest_ind with equal_var=True. Choose welchTTest when the variances may differ.
- `welchTTest` → `WelchTTestResult`: Unequal-variance t-test with Welch-Satterthwaite degrees of freedom, as scipy.stats.ttest_ind with equal_var=False.
- `pairedTTest` → `PairedTTestResult`: t-test of paired measurements, as scipy.stats.ttest_rel: oneSampleTTest of the differences against zero.
- `oneWayAnova` → `OneWayAnovaResult`: One-way ANOVA F-test of k >= 2 independent groups, as scipy.stats.f_oneway. The p-value is the upper tail, so it takes no alternative.
- `mannWhitneyUTest` → `MannWhitneyUTestResult`: Mann-Whitney U rank test of two independent samples, as scipy.stats.mannwhitneyu. Exact without ties when the smaller sample has at most 8 values, else a tie-corrected normal approximation. All-equal values throw ZERO_VARIANCE where SciPy returns NaN.
- `wilcoxonSignedRankTest` → `WilcoxonSignedRankTestResult`: Wilcoxon signed-rank test of paired differences, as scipy.stats.wilcoxon with zero_method wilcox: zeros are dropped and ties take average ranks. All-zero differences throw ZERO_VARIANCE.
- `kruskalWallisTest` → `KruskalWallisTestResult`: Tie-corrected Kruskal-Wallis H-test of k >= 2 independent groups, as scipy.stats.kruskal, with a chi-squared upper-tail p-value. It takes no alternative.
- `binomialTest` → `BinomialTestResult`: Test a success count under a binomial null. Construct the null successProbability with Probability.make. Trial counts above BINOMIAL_MAX_TRIALS surface DistributionInvalidInputError.
- `fisherExactTest` → `FisherExactTestResult`: Test the top-left count of a 2-by-2 table under its fixed margins. Tables above HYPERGEOMETRIC_MAX_POPULATION_SIZE surface DistributionInvalidInputError.

Use something else when:
- evaluate a probability or draw from a named distribution → knotkit/distribution
- analyze regression, multi-factor or repeated-measures ANOVA, or paired-group rank tests such as Friedman → SciPy or R
- test chi-squared independence or Fisher exact on a larger contingency table → SciPy or R

Errors are HypothesisTestInvalidInputError with a `code` property. Branch on `error.code`, never on the message.

Full page: https://knotkit.jkomyno.dev/modules/hypothesis-test.md
Types: node_modules/knotkit/hypothesis-test/ and https://knotkit.jkomyno.dev/reference/knotkit/hypothesis-test/