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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
use crate::data::FloatData;
use std::cmp::Ordering;
use std::collections::VecDeque;
use std::convert::TryInto;
const LANES: usize = 16;
pub fn fast_sum<T: FloatData<T>>(values: &[T]) -> T {
let chunks = values.chunks_exact(LANES);
let remainder = chunks.remainder();
let sum = chunks.fold([T::ZERO; LANES], |mut acc, chunk| {
let chunk: [T; LANES] = chunk.try_into().unwrap();
for i in 0..LANES {
acc[i] += chunk[i];
}
acc
});
let remainder: T = remainder.iter().copied().sum();
let mut reduced = T::ZERO;
for s in sum.iter().take(LANES) {
reduced += *s;
}
reduced + remainder
}
pub fn fast_f64_sum(values: &[f32]) -> f32 {
let chunks = values.chunks_exact(LANES);
let remainder = chunks.remainder();
let sum = chunks.fold([f64::ZERO; LANES], |mut acc, chunk| {
let chunk: [f32; LANES] = chunk.try_into().unwrap();
for i in 0..LANES {
acc[i] += f64::from(chunk[i]);
}
acc
});
let remainder: f64 = remainder
.iter()
.fold(f64::ZERO, |acc, b| acc + f64::from(*b));
let mut reduced: f64 = 0.;
for s in sum.iter().take(LANES) {
reduced += *s;
}
(reduced + remainder) as f32
}
pub fn naive_sum<T: FloatData<T>>(values: &[T]) -> T {
values.iter().copied().sum()
}
pub fn percentiles<T>(v: &[T], sample_weight: &[T], percentiles: &[T]) -> Vec<T>
where
T: FloatData<T>,
{
let mut idx: Vec<usize> = (0..v.len()).collect();
idx.sort_unstable_by(|a, b| v[*a].partial_cmp(&v[*b]).unwrap());
let mut pcts = VecDeque::from_iter(percentiles.iter());
let mut current_pct = *pcts.pop_front().expect("No percentiles were provided");
let mut p = Vec::new();
let mut cuml_pct = T::ZERO;
let mut current_value = v[idx[0]];
let total_values = fast_sum(sample_weight);
for i in idx.iter() {
if current_value != v[*i] {
current_value = v[*i];
}
cuml_pct += sample_weight[*i] / total_values;
if (current_pct == T::ZERO) || (cuml_pct >= current_pct) {
while cuml_pct >= current_pct {
p.push(current_value);
match pcts.pop_front() {
Some(p_) => current_pct = *p_,
None => return p,
}
}
} else if current_pct == T::ONE {
if let Some(i_) = idx.last() {
p.push(v[*i_]);
break;
}
}
}
p
}
pub fn map_bin<T: std::cmp::PartialOrd>(x: &[T], v: &T) -> Option<u16> {
let mut low = 0;
let mut high = x.len();
while low != high {
let mid = (low + high) / 2;
if x[mid] <= *v {
low = mid + 1;
} else {
high = mid;
}
}
u16::try_from(low).ok()
}
pub fn pivot_on_split(
index: &mut [usize],
feature: &[u16],
split_value: u16,
missing_right: bool,
) -> usize {
let mut low = 0;
let mut high = index.len() - 1;
let mad_idx = high;
while low < high {
while low < mad_idx {
let l = feature[index[low]];
match missing_compare(&split_value, l, missing_right) {
Ordering::Less | Ordering::Equal => break,
Ordering::Greater => low += 1,
}
}
while high > 0 {
let h = feature[index[high]];
match missing_compare(&split_value, h, missing_right) {
Ordering::Less | Ordering::Equal => high -= 1,
Ordering::Greater => break,
}
}
if low < high {
index.swap(high, low);
}
}
low
}
pub fn missing_compare(split_value: &u16, cmp_value: u16, missing_right: bool) -> Ordering {
if cmp_value == 0 {
if missing_right {
Ordering::Less
} else {
Ordering::Greater
}
} else {
split_value.cmp(&cmp_value)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_percentiles() {
let v = vec![4., 5., 6., 1., 2., 3., 7., 8., 9., 10.];
let w = vec![1.; v.len()];
let p = vec![0.3, 0.5, 0.75, 1.0];
let p = percentiles(&v, &w, &p);
assert_eq!(p, vec![3.0, 5.0, 8.0, 10.0]);
}
#[test]
fn test_percentiles_weighted() {
let v = vec![10., 8., 9., 1., 2., 3., 6., 7., 4., 5.];
let w = vec![1., 1., 1., 1., 1., 2., 1., 1., 5., 1.];
let p = vec![0.3, 0.5, 0.75, 1.0];
let p = percentiles(&v, &w, &p);
assert_eq!(p, vec![4.0, 4.0, 7.0, 10.0]);
}
#[test]
fn test_map_bin_or_equal() {
let v = vec![f64::MIN, 1., 4., 8., 9.];
assert_eq!(1, map_bin(&v, &0.).unwrap());
assert_eq!(2, map_bin(&v, &1.).unwrap());
assert_eq!(2, map_bin(&v, &2.).unwrap());
assert_eq!(3, map_bin(&v, &4.).unwrap());
assert_eq!(5, map_bin(&v, &9.).unwrap());
assert_eq!(5, map_bin(&v, &10.).unwrap());
assert_eq!(2, map_bin(&v, &1.).unwrap());
assert_eq!(0, map_bin(&v, &f64::NAN).unwrap());
}
#[test]
fn test_missing_compare() {
assert_eq!(missing_compare(&10, 0, true), Ordering::Less);
assert_eq!(missing_compare(&10, 0, false), Ordering::Greater);
assert_eq!(missing_compare(&10, 11, true), Ordering::Less);
assert_eq!(missing_compare(&10, 1, true), Ordering::Greater);
}
#[test]
fn test_pivot() {
let mut idx = vec![2, 6, 9, 5, 8, 13, 11, 7];
let f = vec![15, 10, 10, 11, 3, 18, 0, 9, 3, 5, 2, 6, 13, 19, 14];
let split_i = pivot_on_split(&mut idx, &f, 10, true);
for i in 0..split_i {
assert!((f[idx[i]] < 10) && f[idx[i]] != 0);
}
for i in idx[split_i..].iter() {
assert!((f[*i] >= 10) || (f[*i] == 0));
}
let mut idx = vec![2, 6, 9, 5, 8, 13, 11, 7];
let f = vec![15, 10, 10, 11, 3, 18, 0, 9, 3, 5, 2, 6, 13, 19, 14];
let split_i = pivot_on_split(&mut idx, &f, 10, false);
for i in 0..split_i {
assert!((f[idx[i]] < 10) || (f[idx[i]] == 0));
}
for i in idx[split_i..].iter() {
assert!((f[*i] >= 10) || (f[*i] != 0));
}
}
#[test]
fn test_fast_f64_sum() {
let records = 300000;
let vec = vec![0.23500371; records];
assert_ne!(vec.iter().sum::<f32>(), vec[0] * (records as f32));
assert_eq!(vec[0] * (records as f32), fast_f64_sum(&vec));
}
}