stats::descriptive::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. One quantile is found by introselect on a copy (O(n) expected); several at once sort the copy (O(n log n)).
Enums
QuantileMethodSource
pub enum QuantileMethodHow a quantile between two order statistics is chosen. The names
and results are NumPy's (numpy.quantile(method=...)); the
Hyndman-Fan type is given for each. Linear (type 7) is the
default of NumPy, R and Excel.
use stats::QuantileMethod;
let x = [1.0, 2.0, 3.0, 4.0];
assert(try stats::quantile(x, 0.5) == 2.5);
assert(try stats::quantile(x, 0.5, QuantileMethod::Lower) == 2.0);
assert(try stats::quantile(x, 0.5, QuantileMethod::InvertedCdf) == 2.0);
Type 1: the inverse of the empirical CDF.
Type 2: type 1, averaging at the jumps.
Type 3: the nearest order statistic, ties to the even one (SAS).
Type 4: linear interpolation of the empirical CDF.
Type 5: piecewise linear with knots at the midpoints.
Type 6:
p (n + 1)(Minitab, SPSS).Type 7:
1 + p (n - 1)(the default).Type 8: about median-unbiased whatever the distribution (recommended by Hyndman and Fan).
Type 9: about unbiased for normal data.
The order statistic below the type 7 position.
The order statistic above the type 7 position.
The nearer of
LowerandHigher, ties to the even position.The mean of
LowerandHigher.
Functions
quantileSource
pub fn quantile<X: Sample>(x: X, q: f64, method: QuantileMethod = QuantileMethod::Linear) throws (StatsError) -> f64 The q-quantile, 0 <= q <= 1, by method (default Linear).
NaN when empty or when a value is NaN. Throws InvalidArgument for
q outside [0, 1]. O(n) expected (introselect on a copy).
let x = [7.0, 1.0, 3.0, 5.0];
assert(try stats::quantile(x, 0.25) == 2.5);
assert(try stats::quantile(x, 1.0) == 7.0);
quantilesSource
pub fn quantiles<X: Sample, Q: Sample>(x: X, qs: Q, method: QuantileMethod = QuantileMethod::Linear) throws (StatsError) -> Vector<f64> The quantiles at each of qs, by one sort (O(n log n)). Throws
InvalidArgument for a q outside [0, 1].
let q = try stats::quantiles([1.0, 2.0, 3.0, 4.0, 5.0], [0.0, 0.5, 1.0]);
assert(q[1] == 3.0 && q[2] == 5.0);
percentileSource
pub fn percentile<X: Sample>(x: X, p: f64, method: QuantileMethod = QuantileMethod::Linear) throws (StatsError) -> f64 The p-th percentile, 0 <= p <= 100: quantile(x, p / 100).
assert(try stats::percentile([1.0, 2.0, 3.0, 4.0, 5.0], 90.0) == 4.6);
medianSource
pub fn median<X: Sample>(x: X) -> f64 The median (the mean of the middle two for an even count); NaN when empty or a value is NaN. O(n) expected.
assert(stats::median([3.0, 1.0, 2.0]) == 2.0);
assert(stats::median([4.0, 1.0, 3.0, 2.0]) == 2.5);
iqrSource
pub fn iqr<X: Sample>(x: X) -> f64 The interquartile range q(0.75) - q(0.25) (type 7).
assert(stats::iqr([1.0, 2.0, 3.0, 4.0, 5.0]) == 2.0);
modeSource
pub fn mode<X: Sample>(x: X) -> (f64, usize) The most frequent value and its count; the smallest of several
equally frequent ones (SciPy's mode). (NaN, 0) when empty or a
value is NaN. O(n log n).
let (m, c) = stats::mode([1.0, 3.0, 3.0, 2.0, 2.0]);
assert(m == 2.0 && c == 2);
minSource
pub fn min<X: Sample>(x: X) -> f64 The smallest value; NaN when empty or a value is NaN.
assert(stats::min([3.0, -1.0, 2.0]) == -1.0);
maxSource
pub fn max<X: Sample>(x: X) -> f64 The largest value; NaN when empty or a value is NaN.
assert(stats::max([3.0, -1.0, 2.0]) == 3.0);
rangeSource
pub fn range<X: Sample>(x: X) -> f64 max - min (NumPy's ptp); NaN when empty or a value is NaN.
assert(stats::range([3.0, -1.0, 2.0]) == 4.0);