forked from bheisler/criterion.rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kde.rs
41 lines (34 loc) · 1.13 KB
/
kde.rs
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
use crate::stats::univariate::kde::kernel::Gaussian;
use crate::stats::univariate::kde::{Bandwidth, Kde};
use crate::stats::univariate::Sample;
pub fn sweep(
sample: &Sample<f64>,
npoints: usize,
range: Option<(f64, f64)>,
) -> (Box<[f64]>, Box<[f64]>) {
let (xs, ys, _) = sweep_and_estimate(sample, npoints, range, sample[0]);
(xs, ys)
}
pub fn sweep_and_estimate(
sample: &Sample<f64>,
npoints: usize,
range: Option<(f64, f64)>,
point_to_estimate: f64,
) -> (Box<[f64]>, Box<[f64]>, f64) {
let x_min = sample.min();
let x_max = sample.max();
let kde = Kde::new(sample, Gaussian, Bandwidth::Silverman);
let h = kde.bandwidth();
let (start, end) = match range {
Some((start, end)) => (start, end),
None => (x_min - 3. * h, x_max + 3. * h),
};
let mut xs: Vec<f64> = Vec::with_capacity(npoints);
let step_size = (end - start) / (npoints - 1) as f64;
for n in 0..npoints {
xs.push(start + (step_size * n as f64));
}
let ys = kde.map(&xs);
let point_estimate = kde.estimate(point_to_estimate);
(xs.into_boxed_slice(), ys, point_estimate)
}