anofox-statistics
A statistical hypothesis testing library for Rust, validated against R (VALIDATION).
This library provides a wide range of statistical tests commonly used in data analysis, all validated against R's implementations to ensure numerical accuracy.
Features
-
Math Primitives
- Mean, variance, standard deviation, median
- Numerically stable mean and variance (Welford's algorithm)
- Trimmed mean (robust to outliers)
- Skewness and kurtosis (Fisher's definition, matching R's e1071)
-
Parametric Tests
- T-tests (Welch, Student, Paired) with all alternatives
- Yuen's test (robust t-test using trimmed means)
- Brown-Forsythe test (homogeneity of variances)
-
Nonparametric Tests
- Ranking with average tie handling
- Mann-Whitney U test (Wilcoxon rank-sum)
- Wilcoxon signed-rank test (paired)
- Kruskal-Wallis test (k-sample)
- Brunner-Munzel test (robust rank-based test for stochastic equality)
-
Distributional Tests
- Shapiro-Wilk normality test (Royston AS R94)
- D'Agostino's K-squared test (omnibus normality test using skewness and kurtosis)
-
Resampling Methods
- Permutation engine with custom statistics
- Permutation t-test
- Stationary bootstrap (for dependent data)
- Circular block bootstrap
-
Modern Distribution Tests
- Energy distance test (univariate and multivariate)
- Maximum Mean Discrepancy (MMD) with multiple kernels (Gaussian, Linear, Polynomial, Laplacian)
-
Forecast Evaluation
- Diebold-Mariano test for comparing predictive accuracy
- Clark-West test for nested model comparison
- Superior Predictive Ability (SPA) test for multiple model comparison
- MSPE-Adjusted SPA test for multiple nested models (Clark-West + bootstrap)
- Model Confidence Set (Hansen, Lunde, & Nason, 2011)
Installation
Add to your Cargo.toml:
[]
= "0.2"
Examples
The library includes runnable examples demonstrating each major feature:
Quick Start
T-Tests
use ;
let group1 = vec!;
let group2 = vec!;
// Welch t-test (unequal variances), mu=0.0 tests if mean difference equals zero
let result = t_test
.expect;
println!;
println!;
println!;
// Student t-test (equal variances assumed)
let result = t_test?;
// Paired t-test
let result = t_test?;
// Test if mean difference equals 0.5 (non-zero null hypothesis)
let result = t_test?;
// T-test with 95% confidence interval
let result = t_test?;
if let Some = result.conf_int
Yuen's Robust T-Test
use ;
// 20% trimmed means (robust to outliers)
let result = yuen_test?;
println!;
println!;
Brown-Forsythe Test
use brown_forsythe;
let groups = vec!;
let result = brown_forsythe?;
println!;
println!;
Nonparametric Tests
use ;
// Ranking
let data = vec!;
let ranks = rank?;
// Mann-Whitney U test (two-sided, no continuity correction, normal approximation)
let result = mann_whitney_u?;
// With exact p-values (for small samples without ties)
let result = mann_whitney_u?;
// With confidence interval (Hodges-Lehmann estimate)
let result = mann_whitney_u?;
if let Some = result.conf_int
// Test if location shift equals 0.5 (non-zero null hypothesis)
let result = mann_whitney_u?;
// Wilcoxon signed-rank test (paired)
let result = wilcoxon_signed_rank?;
// Wilcoxon with non-zero null hypothesis (test if median difference equals 0.5)
let result = wilcoxon_signed_rank?;
// Kruskal-Wallis test
let result = kruskal_wallis?;
// Brunner-Munzel test (robust alternative to Mann-Whitney)
let result = brunner_munzel?;
println!;
// Brunner-Munzel with 95% confidence interval
let result = brunner_munzel?;
if let Some = result.conf_int
Normality Tests
use ;
let data = vec!;
// Shapiro-Wilk test
let result = shapiro_wilk?;
println!;
println!;
// D'Agostino's K-squared test (omnibus test using skewness and kurtosis)
let result = dagostino_k_squared?;
println!;
println!;
Resampling Methods
use ;
// Permutation t-test
let result = permutation_t_test?;
println!;
// Stationary bootstrap for time series
let bootstrap = new?;
let samples: = bootstrap.take.collect;
// Circular block bootstrap
let bootstrap = new?;
Modern Distribution Tests
use ;
// Energy distance test
let result = energy_distance_test?;
println!;
println!;
// Maximum Mean Discrepancy with Gaussian kernel
let result = mmd_test?;
println!;
println!;
// MMD with automatic bandwidth selection
let result = mmd_test?;
Forecast Evaluation
use ;
// Forecast errors from two competing models
let errors_model1 = vec!;
let errors_model2 = vec!;
// Diebold-Mariano test (two-sided)
let result = diebold_mariano?;
println!;
println!;
// Clark-West test for nested models (e.g., AR(1) vs AR(2))
let restricted_errors = vec!; // Benchmark/restricted model
let unrestricted_errors = vec!; // Alternative/unrestricted model
let result = clark_west?;
println!;
println!;
// Superior Predictive Ability test (compare benchmark vs multiple models)
let benchmark_losses = vec!;
let model_losses = vec!;
let result = spa_test?;
println!;
println!;
// MSPE-Adjusted SPA for multiple nested models
// Combines Clark-West adjustment with bootstrap for multiple testing
let benchmark_errors = vec!;
let nested_model_errors = vec!;
let result = mspe_adjusted_spa?;
println!;
println!;
// Model Confidence Set - identify the set of best models
let losses = vec!;
let result = model_confidence_set?;
println!;
println!;
Validation
This library is developed using Test-Driven Development (TDD) with R as the oracle (ground truth). All implementations are validated against R's statistical functions:
| Rust Function | R Equivalent | Package |
|---|---|---|
t_test() |
t.test() |
stats |
yuen_test() |
yuen() |
WRS2 |
brown_forsythe() |
leveneTest(center=median) |
car |
mann_whitney_u(), wilcoxon_signed_rank() |
wilcox.test() |
stats |
kruskal_wallis() |
kruskal.test() |
stats |
brunner_munzel() |
brunner.munzel.test() |
lawstat |
shapiro_wilk() |
shapiro.test() |
stats |
dagostino_k_squared() |
agostino.test(), anscombe.test() |
moments |
skewness(), kurtosis() |
skewness(), kurtosis() |
e1071 |
diebold_mariano() |
dm.test() |
forecast |
All 245 test cases ensure numerical agreement with R within appropriate tolerances (typically 1e-10, with documented exceptions for algorithm-dependent tests like Shapiro-Wilk).
For complete transparency on the validation process, see R/VALIDATION.md, which documents:
- All 76 reference data files and their R generation code
- Tolerance rationale for each test category
- Step-by-step reproduction instructions
- R package dependencies
Dependencies
- statrs - Statistical distributions
- thiserror - Error handling
- rand - Random number generation for resampling
License
MIT License