ndarray-stats
Statistical routines for ndarray's ArrayBase — quantiles, correlation, entropy, and histograms as native extension traits.
Repository Health
Technical Analysis
ndarray-stats extends the n-dimensional arrays from ndarray with a suite of statistical routines that mirror what NumPy and Julia’s StatsBase offer, implemented as extension traits so they attach directly onto ArrayBase. Order statistics, summary statistics, correlation analysis, information-theoretic measures, deviation functions, and full histogram construction are all available without wrapping arrays in a new type or leaving the ndarray ecosystem.
Because every routine is exposed as a trait method (QuantileExt, SummaryStatisticsExt, CorrelationExt, EntropyExt, DeviationExt, HistogramExt), calling code reads like idiomatic Rust — array.quantile_mut(…), array.mean(), array.pearson_correlation() — while internals stay hidden behind a sealed-trait pattern that lets the crate add new methods without breaking downstream implementations.
What You Get
- Order statistics via QuantileExt — argmin/argmax (with NaN-skipping variants), and quantile/quantiles_mut for single and bulk quantile computation
- Summary statistics via SummaryStatisticsExt — mean, harmonic/geometric mean, variance, skewness, kurtosis, and central moments, including weighted variants
- Correlation analysis via CorrelationExt — covariance matrices and Pearson correlation coefficients across array axes
- Information-theoretic measures via EntropyExt — entropy, cross-entropy, and Kullback-Leibler divergence
- Deviation functions via DeviationExt — count-equal/unequal, L1/L2/L-infinity distances, (root) mean squared error, and peak signal-to-noise ratio
- Histogram construction via HistogramExt, Grid, GridBuilder, Bins, and Edges — configurable binning strategies for building n-dimensional histograms
Common Use Cases
- Data preprocessing pipelines - normalizing or summarizing numeric arrays before feeding them into a machine learning model built on ndarray/linfa
- Scientific computing - computing summary and order statistics on simulation or measurement data without leaving the ndarray ecosystem
- Signal and image comparison - using deviation functions (MSE, PSNR, L2 distance) to quantify differences between arrays
- Exploratory data analysis - building histograms and grids over multi-dimensional datasets to inspect distributions
Under The Hood
Architecture
The crate is organized as one extension trait per statistical domain — QuantileExt (quantile/mod.rs), SummaryStatisticsExt (summary_statistics/mod.rs + means.rs), CorrelationExt (correlation.rs), EntropyExt (entropy.rs), DeviationExt (deviation.rs), and HistogramExt (histogram/mod.rs, split further into bins.rs, grid.rs, histograms.rs, and strategies.rs) — all re-exported from a thin lib.rs. Every public trait is sealed via a private_decl!/private_impl! macro pair defined in a private module, so external crates can call the methods but never implement the traits themselves, which lets maintainers add new trait methods without a breaking change. Shared error types live in a dedicated errors.rs (EmptyInput, MinMaxError, MultiInputError, QuantileError) with From conversions between them, and interpolation strategies for quantiles are factored into their own quantile/interpolate.rs module implementing an Interpolate trait. Nothing in the core would break from swapping ndarray’s storage backend, since the crate only depends on ndarray’s public array/view APIs.