1 | **use** **crate**::stats::univariate::kde::kernel::Gaussian; |

2 | **use** **crate**::stats::univariate::kde::{Bandwidth, Kde}; |

3 | **use** **crate**::stats::univariate::Sample; |

4 | |

5 | **pub** **fn** sweep( |

6 | sample: &Sample<**f64**>, |

7 | npoints: **usize**, |

8 | range: Option<(**f64**, **f64**)>, |

9 | ) -> (Box<[**f64**]>, Box<[**f64**]>) { |

10 | **let** (xs, ys, _) = sweep_and_estimate(sample, npoints, range, sample[`0`]); |

11 | (xs, ys) |

12 | } |

13 | |

14 | **pub** **fn** sweep_and_estimate( |

15 | sample: &Sample<**f64**>, |

16 | npoints: **usize**, |

17 | range: Option<(**f64**, **f64**)>, |

18 | point_to_estimate: **f64**, |

19 | ) -> (Box<[**f64**]>, Box<[**f64**]>, **f64**) { |

20 | **let** x_min = sample.min(); |

21 | **let** x_max = sample.max(); |

22 | |

23 | **let** kde = Kde::new(sample, Gaussian, Bandwidth::Silverman); |

24 | **let** h = kde.bandwidth(); |

25 | |

26 | **let** (start, end) = **match** range { |

27 | Some((start, end)) => (start, end), |

28 | None => (x_min - `3.` * h, x_max + `3.` * h), |

29 | }; |

30 | |

31 | **let** **mut** xs: Vec<**f64**> = Vec::with_capacity(npoints); |

32 | **let** step_size = (end - start) / (npoints - `1`) **as** **f64**; |

33 | **for** n **in** `0`..npoints { |

34 | xs.push(start + (step_size * n **as** **f64**)); |

35 | } |

36 | |

37 | **let** ys = kde.map(&xs); |

38 | **let** point_estimate = kde.estimate(point_to_estimate); |

39 | |

40 | (xs.into_boxed_slice(), ys, point_estimate) |

41 | } |

42 | |