1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use cast::{self, usize};
use 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)),
)
}
}
}