packages

stats::descriptive

On this page

Descriptive statistics of one set of values: location, spread and shape. Sums are pairwise and variances corrected two-pass, so the error grows like eps log n rather than eps n. Empty input (or fewer values than ddof asks for) gives NaN, a NaN among the values gives NaN, as NumPy does; stats::nan skips NaN instead.

Modules

  • order Order statistics: quantiles by the nine definitions of Hyndman and Fan (1996) plus NumPy's four index rules, the median, the mode, the extremes.

Structs

SummarySource

pub struct Summary

A summary of a set of values: count, mean, spread, quartiles and shape. std and var use ddof = 1, as describe in pandas.

  • pub n: usize§

    Source

    The number of values.

  • pub mean: f64§

    Source
  • pub var: f64§

    Source

    The sample variance (ddof = 1).

  • pub std: f64§

    Source

    The sample standard deviation (ddof = 1).

  • pub min: f64§

    Source
  • pub q1: f64§

    Source

    The first quartile (linear interpolation).

  • pub median: f64§

    Source
  • pub q3: f64§

    Source

    The third quartile.

  • pub max: f64§

    Source
  • pub skew: f64§

    Source

    The biased skewness (skew(x)).

  • pub kurtosis: f64§

    Source

    The biased excess kurtosis (kurtosis(x)).

Trait implementations 1
  • impl Display for Summary

Functions

sumSource

pub fn sum<X: Sample>(x: X) -> f64

The sum of the values, by pairwise summation (O(n), error O(eps log n) of the sum of magnitudes); 0 when empty.

nabi
assert(stats::sum([0.1, 0.2, 0.3]) == 0.6000000000000001);

meanSource

pub fn mean<X: Sample>(x: X) -> f64

The arithmetic mean, by pairwise summation (O(n)); NaN when empty.

nabi
assert(stats::mean([1.0, 2.0, 4.0, 5.0]) == 3.0);

wmeanSource

pub fn wmean<X: Sample, W: Sample>(x: X, w: W) throws (StatsError) -> f64

The weighted mean sum(w x) / sum(w). Throws ShapeMismatch when the lengths differ and InvalidArgument for a negative weight or weights that sum to zero.

nabi
assert(try stats::wmean([1.0, 2.0, 3.0], [3.0, 1.0, 0.0]) == 1.25);

varSource

pub fn var<X: Sample>(x: X, ddof: usize = 0) -> f64

The variance sum((x - mean)^2) / (n - ddof) (corrected two-pass, Chan, Golub and LeVeque 1983). ddof = 0 (the default, as NumPy) is the population variance and the MLE under normality; ddof = 1 the unbiased sample variance. NaN when n <= ddof.

nabi
let x = [2.0, 4.0, 4.0, 4.0, 5.0, 5.0, 7.0, 9.0];
assert(stats::var(x) == 4.0);
assert(stats::var(x, ddof: 1) == 32.0 / 7.0);

stdSource

pub fn std<X: Sample>(x: X, ddof: usize = 0) -> f64

The standard deviation, sqrt(var(x, ddof)).

nabi
assert(stats::std([2.0, 4.0, 4.0, 4.0, 5.0, 5.0, 7.0, 9.0]) == 2.0);

semSource

pub fn sem<X: Sample>(x: X, ddof: usize = 1) -> f64

The standard error of the mean, std(x, ddof) / sqrt(n), with ddof = 1 by default (SciPy's sem).

nabi
let se = stats::sem([1.0, 2.0, 3.0, 4.0]);
assert((se - 0.6454972243679028).abs() < 1e-15);

momentSource

pub fn moment<X: Sample>(x: X, k: u32) -> f64

The k-th central moment sum((x - mean)^k) / n (1 gives 0, 2 the population variance); NaN when empty.

nabi
assert(stats::moment([1.0, 2.0, 3.0, 4.0, 5.0], 3) == 0.0);
assert(stats::moment([1.0, 2.0, 3.0, 4.0, 5.0], 4) == 6.8);

skewSource

pub fn skew<X: Sample>(x: X, bias: bool = true) -> f64

The skewness m3 / m2^1.5 of the central moments; with bias: false the adjusted Fisher-Pearson coefficient G1 = g1 sqrt(n (n - 1)) / (n - 2) (SciPy's skew). 0 for constant data; NaN when empty, or for bias: false below 3 values.

nabi
let x = [1.0, 2.0, 3.0, 10.0];
assert((stats::skew(x) - 1.0182337649086284).abs() < 1e-15); // SciPy: skew(x)

kurtosisSource

pub fn kurtosis<X: Sample>(x: X, fisher: bool = true, bias: bool = true) -> f64

The kurtosis m4 / m2^2, minus 3 by default (fisher: true, the excess kurtosis, 0 for a normal sample in the limit); with bias: false the unbiased estimator G2 of the k-statistics (SciPy's kurtosis). NaN when empty or constant, or for bias: false below 4 values.

nabi
let x = [1.0, 2.0, 3.0, 4.0, 5.0];
assert((stats::kurtosis(x) + 1.3).abs() < 1e-14);
assert((stats::kurtosis(x, fisher: false) - 1.7).abs() < 1e-14);

zscoreSource

pub fn zscore<const N: usize>(a: Array<f64, N>, ddof: usize = 0) -> Array<f64, N>

The z-scores (x - mean) / std(x, ddof) of every element, in the shape of a. Constant data gives NaN (0 / 0), as SciPy does.

nabi
use core::array::Vector;

let a: Vector<f64> = [1, 2, 3];
let z = stats::zscore(a);
assert(z[0] == -z[2] && z[1] == 0.0);

describeSource

pub fn describe<X: Sample>(x: X) -> Summary

The Summary of the values: one copy and a sort. O(n log n).

nabi
let s = stats::describe([1.0, 2.0, 3.0, 4.0, 5.0]);
assert(s.n == 5 && s.median == 3.0 && s.q1 == 2.0 && s.var == 2.5);