Hypothesis Tests
One-sample t-test. Tests whether the mean of a sample differs from a specified population mean. Assumes normally distributed data.
Two-sample independent t-test (Welch's). Tests whether two independent samples have different means. Does not assume equal variances.
Paired t-test. Tests whether the mean difference between paired observations is zero. Use when samples are related (before/after).
One-way ANOVA (F-test). Tests whether the means of two or more groups differ. Extension of the t-test to multiple groups.
Chi-square goodness-of-fit test. Tests whether observed frequencies match expected frequencies. If expected is omitted, assumes uniform distribution.
Shapiro-Wilk test for normality. Tests whether a sample comes from a normal distribution. Works for sample sizes 3 to 5000.
D'Agostino-Pearson normality test. Tests whether a sample differs from a normal distribution using skewness and kurtosis.
Anderson-Darling test for normality. More powerful than Shapiro-Wilk for detecting departures in the tails of the distribution.
Kolmogorov-Smirnov test. Tests whether a sample matches a given theoretical distribution. Pass a CDF function or a distribution name string (e.g., 'norm').
Mann-Whitney U test. Non-parametric alternative to the two-sample t-test. Does not assume normality.
Wilcoxon signed-rank test. Non-parametric alternative to the paired t-test. Tests the symmetry of the difference distribution around zero.
Kruskal-Wallis H test. Non-parametric alternative to one-way ANOVA. Tests whether multiple samples come from the same distribution.
Friedman chi-square test. Non-parametric alternative to repeated measures ANOVA. Tests whether multiple related samples have the same distribution.
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's test for equality of variances. More powerful than Levene's but assumes normally distributed data.
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()