Skip to main content

quantile

Function quantile 

Source
pub fn quantile(data: &[f64], p: f64) -> Option<f64>
Expand description

Computes the p-th quantile using the R-7 linear interpolation method.

This is the default quantile method in R, NumPy, and Excel.

§Algorithm

For sorted data x[0..n] and quantile p ∈ [0, 1]:

  1. Compute h = (n − 1) × p
  2. Let j = ⌊h⌋ and g = h − j
  3. Return (1 − g) × x[j] + g × x[j+1]

Reference: Hyndman & Fan (1996), The American Statistician 50(4), pp. 361–365.

§Complexity

Time: O(n log n) (dominated by sort), Space: O(n)

§Panics

Does not panic; returns None for invalid inputs.

§Returns

  • None if data is empty, p is outside [0, 1], or data contains NaN.

§Examples

use u_numflow::stats::quantile;
let data = [1.0, 2.0, 3.0, 4.0, 5.0];
assert_eq!(quantile(&data, 0.0), Some(1.0));
assert_eq!(quantile(&data, 1.0), Some(5.0));
assert_eq!(quantile(&data, 0.5), Some(3.0));