/rust/registry/src/index.crates.io-1949cf8c6b5b557f/criterion-0.5.1/src/kde.rs
Line | Count | Source |
1 | | use crate::stats::univariate::kde::kernel::Gaussian; |
2 | | use crate::stats::univariate::kde::{Bandwidth, Kde}; |
3 | | use crate::stats::univariate::Sample; |
4 | | |
5 | 0 | pub fn sweep( |
6 | 0 | sample: &Sample<f64>, |
7 | 0 | npoints: usize, |
8 | 0 | range: Option<(f64, f64)>, |
9 | 0 | ) -> (Box<[f64]>, Box<[f64]>) { |
10 | 0 | let (xs, ys, _) = sweep_and_estimate(sample, npoints, range, sample[0]); |
11 | 0 | (xs, ys) |
12 | 0 | } |
13 | | |
14 | 0 | pub fn sweep_and_estimate( |
15 | 0 | sample: &Sample<f64>, |
16 | 0 | npoints: usize, |
17 | 0 | range: Option<(f64, f64)>, |
18 | 0 | point_to_estimate: f64, |
19 | 0 | ) -> (Box<[f64]>, Box<[f64]>, f64) { |
20 | 0 | let x_min = sample.min(); |
21 | 0 | let x_max = sample.max(); |
22 | | |
23 | 0 | let kde = Kde::new(sample, Gaussian, Bandwidth::Silverman); |
24 | 0 | let h = kde.bandwidth(); |
25 | | |
26 | 0 | let (start, end) = match range { |
27 | 0 | Some((start, end)) => (start, end), |
28 | 0 | None => (x_min - 3. * h, x_max + 3. * h), |
29 | | }; |
30 | | |
31 | 0 | let mut xs: Vec<f64> = Vec::with_capacity(npoints); |
32 | 0 | let step_size = (end - start) / (npoints - 1) as f64; |
33 | 0 | for n in 0..npoints { |
34 | 0 | xs.push(start + (step_size * n as f64)); |
35 | 0 | } |
36 | | |
37 | 0 | let ys = kde.map(&xs); |
38 | 0 | let point_estimate = kde.estimate(point_to_estimate); |
39 | | |
40 | 0 | (xs.into_boxed_slice(), ys, point_estimate) |
41 | 0 | } |