criterion/stats/univariate/
percentiles.rsuse cast::{self, usize};
use stats::float::Float;
pub struct Percentiles<A>(Box<[A]>)
where
A: Float;
impl<A> Percentiles<A>
where
A: Float,
usize: cast::From<A, Output = Result<usize, cast::Error>>,
{
unsafe fn at_unchecked(&self, p: A) -> A {
let _100 = A::cast(100);
debug_assert!(p >= A::cast(0) && p <= _100);
debug_assert!(self.0.len() > 0);
let len = self.0.len() - 1;
if p == _100 {
self.0[len]
} else {
let rank = (p / _100) * A::cast(len);
let integer = rank.floor();
let fraction = rank - integer;
let n = usize(integer).unwrap();
let &floor = self.0.get_unchecked(n);
let &ceiling = self.0.get_unchecked(n + 1);
floor + (ceiling - floor) * fraction
}
}
pub fn at(&self, p: A) -> A {
let _0 = A::cast(0);
let _100 = A::cast(100);
assert!(p >= _0 && p <= _100);
assert!(self.0.len() > 0);
unsafe { self.at_unchecked(p) }
}
pub fn iqr(&self) -> A {
unsafe {
let q1 = self.at_unchecked(A::cast(25));
let q3 = self.at_unchecked(A::cast(75));
q3 - q1
}
}
pub fn median(&self) -> A {
unsafe { self.at_unchecked(A::cast(50)) }
}
pub fn quartiles(&self) -> (A, A, A) {
unsafe {
(
self.at_unchecked(A::cast(25)),
self.at_unchecked(A::cast(50)),
self.at_unchecked(A::cast(75)),
)
}
}
}