GitHub
deepbox/stats

Hypothesis Tests

Statistical tests that evaluate hypotheses about population parameters. Each returns a TestResult with statistic and pvalue. Reject the null hypothesis when p-value < significance level (typically 0.05).
ttest_1samp
ttest_1samp(x: Tensor, popmean: number): TestResult

One-sample t-test. Tests whether the mean of a sample differs from a specified population mean. Assumes normally distributed data.

ttest_ind
ttest_ind(x: Tensor, y: Tensor): TestResult

Two-sample independent t-test (Welch's). Tests whether two independent samples have different means. Does not assume equal variances.

ttest_rel
ttest_rel(x: Tensor, y: Tensor): TestResult

Paired t-test. Tests whether the mean difference between paired observations is zero. Use when samples are related (before/after).

f_oneway
f_oneway(...groups: Tensor[]): TestResult

One-way ANOVA (F-test). Tests whether the means of two or more groups differ. Extension of the t-test to multiple groups.

chisquare
chisquare(observed: Tensor, expected?: Tensor): TestResult

Chi-square goodness-of-fit test. Tests whether observed frequencies match expected frequencies. If expected is omitted, assumes uniform distribution.

shapiro
shapiro(x: Tensor): TestResult

Shapiro-Wilk test for normality. Tests whether a sample comes from a normal distribution. Works for sample sizes 3 to 5000.

normaltest
normaltest(x: Tensor): TestResult

D'Agostino-Pearson normality test. Tests whether a sample differs from a normal distribution using skewness and kurtosis.

anderson
anderson(x: Tensor): TestResult

Anderson-Darling test for normality. More powerful than Shapiro-Wilk for detecting departures in the tails of the distribution.

kstest
kstest(x: Tensor, cdf: string | ((x: number) => number)): TestResult

Kolmogorov-Smirnov test. Tests whether a sample matches a given theoretical distribution. Pass a CDF function or a distribution name string (e.g., 'norm').

mannwhitneyu
mannwhitneyu(x: Tensor, y: Tensor): TestResult

Mann-Whitney U test. Non-parametric alternative to the two-sample t-test. Does not assume normality.

wilcoxon
wilcoxon(x: Tensor, y?: Tensor): TestResult

Wilcoxon signed-rank test. Non-parametric alternative to the paired t-test. Tests the symmetry of the difference distribution around zero.

kruskal
kruskal(...groups: Tensor[]): TestResult

Kruskal-Wallis H test. Non-parametric alternative to one-way ANOVA. Tests whether multiple samples come from the same distribution.

friedmanchisquare
friedmanchisquare(...groups: Tensor[]): TestResult

Friedman chi-square test. Non-parametric alternative to repeated measures ANOVA. Tests whether multiple related samples have the same distribution.

levene
levene(center: 'mean' | 'median' | 'trimmed', ...groups: Tensor[]): TestResult

Levene's test for equality of variances. Tests whether multiple samples have the same variance. Robust to departures from normality. The center parameter controls the centering method: 'median' (default recommendation, most robust), 'mean', or 'trimmed'.

bartlett
bartlett(...groups: Tensor[]): TestResult

Bartlett's test for equality of variances. More powerful than Levene's but assumes normally distributed data.

hypothesis-tests.ts
import { ttest_1samp, ttest_ind, f_oneway, shapiro, chisquare } from "deepbox/stats";import { tensor } from "deepbox/ndarray";// One-sample t-test: does the mean differ from 0?const sample = tensor([2.3, 1.9, 2.5, 2.1, 2.7]);const { statistic, pvalue } = ttest_1samp(sample, 0);if (pvalue < 0.05) console.log("Mean significantly differs from 0");// Two-sample t-test: do groups differ?const group1 = tensor([5.1, 4.9, 5.0, 5.2]);const group2 = tensor([4.5, 4.3, 4.6, 4.4]);const result = ttest_ind(group1, group2);// ANOVA: compare 3+ groupsconst a = tensor([23, 25, 27]);const b = tensor([30, 32, 29]);const c = tensor([35, 37, 36]);const fTest = f_oneway(a, b, c);// Normality testconst norm = shapiro(tensor([1.2, 2.3, 1.8, 2.1, 1.9, 2.5, 2.0]));if (norm.pvalue > 0.05) console.log("Cannot reject normality");

Choosing a Test

  • 1 sample, normal data → ttest_1samp()
  • 2 independent samples, normal → ttest_ind()
  • 2 paired samples, normal → ttest_rel()
  • 3+ groups, normal → f_oneway()
  • 2 independent samples, non-normal → mannwhitneyu()
  • 2 paired samples, non-normal → wilcoxon()
  • 3+ groups, non-normal → kruskal()
  • Check normality → shapiro() / normaltest() / anderson()
  • Check equal variances → levene() (robust) / bartlett() (normal data)
  • Categorical data → chisquare()
  • Match distribution → kstest()