dist-rust
Zero-dependency PDF, CDF, and quantile functions for the normal and Student's t distributions in Rust.
Repository Health
Technical Analysis
distrs is a small, zero-dependency Rust crate providing probability density (PDF), cumulative distribution (CDF), and percent-point/quantile (PPF) functions for the normal and Student’s t distributions. It implements well-established numerical algorithms — Wichura’s AS 241 for normal quantiles and Hill’s Algorithms 395 and 396 for Student’s t — porting reference implementations faithfully rather than approximating them.
The crate supports no_std environments via an optional libm dependency, making it usable in embedded and other resource-constrained Rust targets alongside standard applications. It ships with an extensive inline test suite validating outputs against reference values from the original algorithm papers, and is dual-licensed under Unlicense OR MIT.
What You Get
- Normal distribution functions -
Normal::pdf,Normal::cdf, andNormal::ppffor the normal distribution given a mean and standard deviation. - Student’s t distribution functions -
StudentsT::pdf,StudentsT::cdf, andStudentsT::ppfaccepting integer or non-integer degrees of freedom. - no_std support - an optional
no_stdfeature swaps the system math library forlibm, so the crate runs in embedded and other allocation-constrained targets. - Zero required dependencies - the default build has no external crates at all;
libmis pulled in only whenno_stdis enabled.
Common Use Cases
- Statistical hypothesis testing - computing p-values and confidence intervals from normal or t-distributed test statistics without a full stats library.
- Embedded/no_std numerical code - firmware or constrained targets that need distribution functions but can’t depend on the standard library.
- Financial and scientific modeling - Monte Carlo simulations, quantile calculations, and probability modeling in Rust services.
- Porting statistical code from other ecosystems - drop-in equivalents for
scipy.stats.norm/scipy.stats.t-style PDF/CDF/PPF calls when migrating to Rust.
Under The Hood
Architecture
The crate is a flat, single-purpose module tree: lib.rs re-exports the Normal and StudentsT marker structs from normal.rs and students_t.rs, both of which route their floating-point primitives through a shared math module that swaps between native f64 methods/C library calls (erf, tgamma) and the libm crate depending on the no_std feature flag. There’s no dependency injection or runtime state — each distribution type exposes plain static pdf/cdf/ppf functions operating on scalar f64 inputs, so the math abstraction is the only real seam, and any change to its function set ripples identically into both distribution implementations.
Tech Stack
Pure Rust, 2018 edition, with a single optional dependency (libm 0.2, gated behind the no_std feature) and no dependencies at all in the default build. It uses core::f64::consts rather than std, builds via cargo, and is validated in CI across Ubuntu, macOS, and Windows runners, plus separate no_std and nightly-toolchain builds — there is no web, ORM, or application framework involved; this is a pure numerical primitives crate.
Code Quality
Each module carries an extensive inline #[cfg(test)] suite covering standard inputs, mean/std-dev variants, infinities, NaNs, and invalid parameters (zero or negative standard deviation, out-of-range probabilities), with expected values checked against reference data from the cited algorithm papers. doctest is explicitly disabled in Cargo.toml, so README code samples aren’t compiled as tests, and there’s no separate integration-test directory, but the CI matrix runs cargo test under default, no_std, and nightly feature combinations on three operating systems, giving strong practical coverage despite the crate’s small size.
API Design
The public surface is intentionally thin: two zero-sized marker structs, each exposing three static methods (pdf, cdf, ppf) with matching names and argument order across both distributions, so switching from Normal to StudentsT requires no relearning. StudentsT’s degrees-of-freedom parameter is generic over Into<f64>, letting callers pass an integer or a float without an explicit cast. Getting started needs exactly one use statement and no configuration, and the README documents the full API plus the academic references the algorithms are drawn from.