Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement FromIterator<f64> for Empirical and remove from_vec #305

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions src/distribution/empirical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl<T: PartialOrd> Ord for NonNan<T> {
///
/// let samples = vec![0.0, 5.0, 10.0];
///
/// let empirical = Empirical::from_vec(samples);
/// let empirical = Empirical::from_iter(samples);
/// assert_eq!(empirical.mean().unwrap(), 5.0);
/// ```
#[derive(Clone, PartialEq, Debug)]
Expand Down Expand Up @@ -65,14 +65,6 @@ impl Empirical {
})
}

pub fn from_vec(src: Vec<f64>) -> Empirical {
let mut empirical = Empirical::new().unwrap();
for elt in src.into_iter() {
empirical.add(elt);
}
empirical
}

pub fn add(&mut self, data_point: f64) {
if !data_point.is_nan() {
self.sum += 1.;
Expand Down Expand Up @@ -174,6 +166,16 @@ impl std::fmt::Display for Empirical {
}
}

impl FromIterator<f64> for Empirical {
fn from_iter<T: IntoIterator<Item = f64>>(iter: T) -> Self {
let mut empirical = Self::new().unwrap();
for elt in iter {
empirical.add(elt);
}
empirical
}
}

#[cfg(feature = "rand")]
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
impl ::rand::distributions::Distribution<f64> for Empirical {
Expand Down Expand Up @@ -243,7 +245,7 @@ mod tests {
#[test]
fn test_cdf() {
let samples = vec![5.0, 10.0];
let mut empirical = Empirical::from_vec(samples);
let mut empirical = Empirical::from_iter(samples);
assert_eq!(empirical.cdf(0.0), 0.0);
assert_eq!(empirical.cdf(5.0), 0.5);
assert_eq!(empirical.cdf(5.5), 0.5);
Expand Down Expand Up @@ -271,7 +273,7 @@ mod tests {
#[test]
fn test_sf() {
let samples = vec![5.0, 10.0];
let mut empirical = Empirical::from_vec(samples);
let mut empirical = Empirical::from_iter(samples);
assert_eq!(empirical.sf(0.0), 1.0);
assert_eq!(empirical.sf(5.0), 0.5);
assert_eq!(empirical.sf(5.5), 0.5);
Expand Down