Run inference where it has to live: inside a latency-critical Rust path, in-process and deterministic, with no Python round-trip and nothing to link. The standout is a classical hypothesis-test suite with scipy-grade p-values (exact and asymptotic modes) — the piece the Rust ecosystem otherwise lacks.
[]
= "0.2"
use ;
let z = NormalDistribution ;
assert!; // 1/sqrt(2*pi)
assert!;
assert!;
assert_eq!;
Why stats-claw
- Zero runtime dependencies. std-only. No BLAS, no LAPACK, no transitive supply chain — it compiles in seconds and drops into any Rust binary, including constrained targets.
- scipy-grade, and proven so. Every numeric is checked against committed golden fixtures generated from the reference Python libraries, within documented tolerances (down to 1e-12).
- Deterministic by construction. A hand-written
SplitMix64PRNG gives byte-identical uniform sampling and resampling across platforms — reproducible tests, reproducible science. (See Compatibility for the one documented exception.) - Formally verified where it counts. 68 Kani proof harnesses model-check panic-freedom and index bounds over complete scalar input domains — every RNG state, every hash word — rather than sampled ones. See VERIFICATION.md for what is proven and, just as importantly, what is not.
- The inference suite Rust was missing. Exact and asymptotic p-values for the classical tests (t, ANOVA, χ², Fisher, Mann–Whitney, Wilcoxon, Kruskal–Wallis, KS, Shapiro), with log-space tails for extreme significance.
Validated against reference libraries
| Area | Reference | Tolerance |
|---|---|---|
| Distributions (pdf/cdf/ppf/moments, log-space tails) | scipy.stats |
1e-9 – 1e-12 |
| Hypothesis tests (t/ANOVA, χ²/Fisher/McNemar, KS/Anderson/Shapiro, Mann–Whitney/Wilcoxon/Kruskal) | scipy.stats (incl. method="exact") |
≤ 1e-8 |
| Regression (OLS, ridge) | scikit-learn |
~1e-12 |
| Clustering / decomposition / change-point | scikit-learn, ruptures |
per-test |
| Association rules | mlxtend |
1e-12 |
What's in the box
Distributions — pdf/pmf, cdf, quantile, moments, log-space cdf/sf, and
seeded sampling for 14 families: normal, uniform, Cauchy, Laplace, exponential, gamma, beta,
Weibull, log-normal, chi-squared, Student's t, F, binomial, and Poisson.
Hypothesis tests — parametric (one-sample / independent / paired / Welch t-tests, ANOVA, variance), non-parametric (Mann–Whitney, Wilcoxon, Kruskal–Wallis, Friedman), categorical (χ², Fisher, McNemar, Cochran), goodness-of-fit (KS, Anderson–Darling, Shapiro–Wilk), and correlation — each returning the statistic, p-value (exact or asymptotic), degrees of freedom, and effect size where defined.
Resampling — bootstrap, jackknife, permutation, cross-validation, and percentile confidence / credible intervals, all driven by the deterministic PRNG.
Streaming — single-pass online estimators (RunningMoments via Welford, P2Quantile)
for latency-critical and unbounded-data workloads.
Also included — gradient and second-order optimizers (gradient descent, Adam, RMSProp,
AdaGrad, SGD, Newton, L-BFGS, conjugate-gradient) plus stochastic search (genetic, simulated
annealing); regression (OLS, ridge); clustering (k-means, DBSCAN, GMM, hierarchical, spectral,
mean-shift, affinity propagation); decomposition / embedding (PCA, ICA, factor analysis, t-SNE,
UMAP, LLE); and density, outlier, feature-selection, change-point (PELT), cardinality, and
association-rule utilities. These overlap mature crates like linfa
and smartcore; the inference suite above is the focus.
Usage
Distributions are plain parameter structs with Default; the behaviour lives in traits you
bring into scope (Pdf, Pmf, Cdf, Quantile, Moments, LogCdf, Sample).
Distributions
use ;
let coins = BinomialDistribution ;
assert_eq!;
assert!; // 5 heads is likelier than 3
Deterministic sampling
use ;
use SplitMix64;
let dist = NormalDistribution ;
let mut rng = new; // same seed => same stream, on every platform
let draws: = .map.collect;
assert_eq!;
Hypothesis testing
use ;
let control = ;
let treated = ;
// Returns a TestResult; `?` works in any function returning stats_claw::error::Result.
let r = t_test_ind?;
println!;
assert!; // significantly different means
assert_eq!; // n_a + n_b - 2
For ordinal or non-normal data, reach for the rank tests:
use ;
let a = ;
let b = ;
let r = mann_whitney_u?;
assert!;
assert!; // rank-biserial correlation
Bootstrap confidence intervals
use ;
use SplitMix64;
let sample = ;
let mean = ;
let boots = bootstrap_statistic?;
let = percentile_ci?; // 95% interval
assert!;
Streaming estimators
use RunningMoments;
let mut acc = new;
for x in
assert_eq!;
assert!;
assert!; // Bessel-corrected (n-1) sample variance
Regression
use ols;
let x = vec!; // one row per observation
let y = vec!; // y = 3x + 2
let model = ols?;
assert!;
assert!;
Performance
The hot paths are written for throughput: a SIMD ziggurat normal sampler (with a scalar
fallback and runtime feature detection), log-space tail evaluation to keep extreme p-values
accurate instead of underflowing to zero, and single-pass streaming estimators that hold O(1)
state. unsafe is confined to feature-gated SIMD intrinsics, each carrying a // SAFETY:
justification; everything else is safe Rust.
Verification
Beyond the test suite, the crate carries 68 Kani proof harnesses — model-checking properties over every input of a type rather than a sampled set. They establish panic-freedom, index-bounds, and structural invariants, for example:
next_f64()lands in[0, 1)for every one of the PRNG's 2^64 states.- Every index derived into the 128-entry ziggurat tables is in bounds, for every
u64drawn. - The HyperLogLog register index stays below
2^pfor every hash word at every supported precision. - The regression 2×2 solver is total:
OkorSingular, never a panic.
Two things they do not establish. They say nothing about numerical accuracy — Kani
over-approximates exp/ln, so accuracy remains the job of the golden-fixture
equivalence suite above. And because CBMC is a bounded model checker, properties
parameterised by a collection size (permutation bijectivity, k-fold partitioning) are
proven at small fixed sizes rather than for all N; VERIFICATION.md states the bound for
each. unsafe (the SIMD paths) is covered dynamically by Miri instead, since model
checkers cannot see through core::arch intrinsics.
The harnesses live behind #[cfg(kani)], so they are invisible to cargo build/test
and Kani is never a dependency — the zero-runtime-dependency guarantee is unchanged.
Full detail, including the documented limits, is in VERIFICATION.md.
Compatibility
- MSRV: Rust 1.93 (edition 2024). The minimum supported version is tested in CI.
- Dependencies: none at runtime (std-only).
- Determinism: the PRNG stream is byte-identical across operating systems and
architectures, and so is anything computed from it by plain arithmetic — Rust does not
reassociate floating-point, so even a 100k-term reduction over the uniform stream is
bit-identical on every target and at every optimisation level. The documented exception
is the normal sampler: Box–Muller evaluates
ln,sin, andcosthrough the platform math library, which is accurate to well under an ULP but is not correctly rounded. So individualstandard_normaldraws — and any statistic accumulated from them — can differ in the last ULP or two between targets, and even between optimisation levels of the same source. Compare those with a tolerance rather than bit-for-bit.
Status
Pre-stable (0.x): the public API is still being refined and may change before 1.0,
following semantic versioning. The validation harness and tolerances are part of the test
suite, so numeric behaviour is held stable even while names settle.
License
Dual-licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE)
- MIT license (LICENSE-MIT)
at your option. Contributions are accepted under the same dual license.