
Statistical Tests
Statistical tests is a Nim library for performing statistical tests.
All public procs and summary types are generic over SomeFloat (float32 / float64).
All test procs return Result[Summary, StatisticalTestsError] for robust error handling.
Use ? for propagation and .tryGet() to unwrap in tests.
Supported Statistical Tests
Parametric Tests
| Statistical Test |
Command |
| Binomial Test |
binomialTest[T](x, n: int, p: T, alternative, significanceAlpha, confidenceIntervalAlpha, ciMethod) |
| Chi² Test (GOF) |
chi2Test[T](observedValues: openArray[T], expectedValues: openArray[T], significanceAlpha, confidenceIntervalAlpha) |
| Chi² Test (uniform) |
chi2Test[T](observedValues: openArray[T], significanceAlpha, confidenceIntervalAlpha) |
| Chi² Contingency Test |
chi2ContingencyTest[T](observedValues: seq[seq[T]], significanceAlpha, yates: bool = false) |
| F Test |
fTest[T](x1, x2: openArray[T], alternative, significanceAlpha, confidenceIntervalAlpha) |
| One-sample t-test |
oneSampleTTest[T](x: openArray[T], expectedMean, alternative, significanceAlpha, confidenceIntervalAlpha) |
| Equal-variance t-test |
equalVarianceTTest[T](x1, x2: openArray[T], expectedMean, alternative, significanceAlpha, confidenceIntervalAlpha) |
| Unequal-variance t-test |
unequalVarianceTTest[T](x1, x2: openArray[T], expectedMean, alternative, significanceAlpha, confidenceIntervalAlpha) |
| Paired t-test |
pairedTTest[T](differences: openArray[T], expectedMeanDifference, alternative, significanceAlpha, confidenceIntervalAlpha) |
| One-way ANOVA |
oneWayAnova[T](groups: seq[seq[T]], significanceAlpha) |
| Simple linear regression |
simpleLinearRegression[T](x, y: openArray[T], alternative, significanceAlpha, confidenceIntervalAlpha) |
Non-parametric Tests
| Statistical Test |
Command |
| Mann-Whitney U |
mannWhitneyUTest[T](x1, x2: openArray[T], alternative, significanceAlpha, continuityCorrection) |
| Wilcoxon signed-rank |
wilcoxonSignedRankTest[T](differences: openArray[T], zeroMethod, alternative, significanceAlpha) |
| KS one-sample |
ksOneSampleTest[T](x: openArray[T], referenceCdf, alternative, significanceAlpha) |
| KS two-sample |
ksTwoSampleTest[T](x1, x2: openArray[T], alternative, significanceAlpha) |
| Fisher's exact |
fisherExactTest(a, b, c, d: int, alternative, significanceAlpha, confidenceIntervalAlpha) |
Correlation Tests
| Statistical Test |
Command |
| Pearson correlation |
pearsonCorrelation[T](x, y: openArray[T], alternative, significanceAlpha, confidenceIntervalAlpha) |
| Spearman correlation |
spearmanCorrelation[T](x, y: openArray[T], alternative, significanceAlpha, confidenceIntervalAlpha) |
| Kendall tau-b |
kendallCorrelation[T](x, y: openArray[T], alternative, significanceAlpha, tieCorrection) |
Normality Tests
| Statistical Test |
Command |
| Shapiro-Wilk |
shapiroWilkTest[T](x: openArray[T], significanceAlpha) |
| Anderson-Darling |
andersonDarlingTest[T](x: openArray[T], significanceAlpha) |
| Jarque-Bera |
jarqueBeraTest[T](x: openArray[T], significanceAlpha) |
Multiple-comparison Corrections
| Procedure |
Command |
| Bonferroni |
bonferroniCorrection[T](pValues: openArray[T], alpha: T) |
| Holm-Bonferroni |
holmBonferroniCorrection[T](pValues: openArray[T], alpha: T) |
| Benjamini-Hochberg |
benjaminiHochbergCorrection[T](pValues: openArray[T], q: T) |
Power / Sample-size
| Procedure |
Command |
| One-sample t power |
oneSampleTPower[T](effectSize: T, n: int, alpha, alternative) |
| One-sample t sample size |
oneSampleTSampleSize[T](effectSize: T, power, alpha, alternative) |
| Equal-var t power |
equalVarTPower[T](effectSize, n1, n2, alpha, alternative) |
| Equal-var t sample size |
equalVarTSampleSize[T](effectSize, power, alpha, alternative) |
| Unequal-var t power |
unequalVarTPower[T](effectSize, n1, n2, alpha, alternative) |
| Unequal-var t sample size |
unequalVarTSampleSize[T](effectSize, power, alpha, alternative) |
| F-test power |
fTestPower[T](varianceRatio, n1, n2, alpha, alternative) |
| F-test sample size |
fTestSampleSize[T](varianceRatio, power, alpha, alternative) |
| Chi² GOF power |
chi2TestPower[T](effectSize, n, df, alpha) |
| Chi² GOF sample size |
chi2TestSampleSize[T](effectSize, power, df, alpha) |
| One-way ANOVA power |
oneWayAnovaPower[T](effectSize, groups, nPerGroup, alpha) |
| One-way ANOVA sample size |
oneWayAnovaSampleSize[T](effectSize, power, groups, alpha) |
Features
- Alternative hypothesis: every test proc accepts
alternative: Alternative = twoSided (twoSided, left, right).
- Significance verdict: every summary includes
isSignificant: bool computed as pValue < significanceAlpha (strict <).
- Effect sizes: Cohen's d, Hedges' g, Cramer's V, phi, odds ratio, eta-squared, omega-squared, rank-biserial correlation, Pearson's r.
- Confidence intervals: available on parametric tests, correlations, and
Fisher's exact test as the named
ConfidenceInterval[T] type (.lower /
.upper); ciMethod parameter on binomialTest supports Clopper-Pearson
(default), Wilson, Jeffreys, and Agresti-Coull.
- Yates' continuity correction:
chi2ContingencyTest supports yates: bool = false for 2x2 tables.
- Error handling: all procs return
Result[T, StatisticalTestsError]; ?-friendly. No silent NaN/Inf.
NaN parameters are rejected first by every validator (MustNotBeNaN) before any other
constraint check, mirroring distributions 2.0.0.
- Pretty-print: every summary type has a
$ proc for RST-style output.
requires "results >= 0.5.0": Result envelope dependency.
Removed in 1.0.0
oneSampleZTest, equalVarianceZTest, unequalVarianceZTest and their summary types have been removed.
These procs used sample standard deviations in their statistics, which makes them
t-tests with the wrong reference distribution (normal instead of t). Use the
oneSampleTTest / equalVarianceTTest / unequalVarianceTTest procs instead.
Example
import results
import statistical_tests
let stats = oneSampleTTest([1.0, 2.0, 3.0, 4.0, 5.0, 6.0]).tryGet()
echo stats.numberObservations
echo stats.observedMean
echo stats.expectedMean
echo stats.standardError
echo stats.tStatistic
echo stats.pValue
echo stats.confidenceInterval
echo stats.isSignificant
echo stats.cohensD
# Error handling
let r = pearsonCorrelation(@[1.0, 2.0], @[1.0, 2.0])
if r.isErr:
echo $r.error # static message of the variant
# Diagnostics are recovered by matching on the variant:
if r.error == StatisticalTestsError.LengthMismatch:
echo "inputs had different lengths"
# `DistError`s from `distributions` surface losslessly as `Dist`-prefixed
# mirrors of `StatisticalTestsError` (generated by the merge-enum macro), so a
# rejected constructor keeps its exact upstream variant, e.g.
# `StatisticalTestsError.DistMustBePositive`.
# float32 support
let s32 = oneSampleTTest([1.0'f32, 2.0'f32, 3.0'f32]).tryGet()
echo s32.pValue
# Pretty-print
echo $stats
Accuracy
~1e-12 relative where convergent (float64); ~1e-5 (float32).
Upper-tail p-values (chi², F, t, normal) are computed directly via the
regularized incomplete gamma/beta functions (and erfc), so small p-values
are not subject to the catastrophic cancellation of 1 - cdf.
Non-central approximations (power module) use Johnson & Welch / Poisson-weighted series
with documented accuracy. Non-parametric asymptotic p-values cite their sources.
TODO
- Exact p-values for Mann-Whitney / Wilcoxon on small-n cases.
- Post-hoc tests for one-way ANOVA (Tukey HSD, Games-Howell, Dunnett).
- Native non-central distributions in
distributions for exact power calculations.
Performance, feature, and documentation PR's are always welcome.
Contact
I can be reached at aymanalbaz98@gmail.com