Skip to content

Commit

Permalink
refactor: Rewrite Empirical::cdf and ::sf
Browse files Browse the repository at this point in the history
  • Loading branch information
FreezyLemon authored and YeungOnion committed Dec 3, 2024
1 parent 7456947 commit af648be
Showing 1 changed file with 9 additions and 14 deletions.
23 changes: 9 additions & 14 deletions src/distribution/empirical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::distribution::ContinuousCDF;
use crate::statistics::*;
use non_nan::NonNan;
use std::collections::BTreeMap;
use std::ops::Bound;

mod non_nan {
use core::cmp::Ordering;
Expand Down Expand Up @@ -249,24 +250,18 @@ impl Distribution<f64> for Empirical {

impl ContinuousCDF<f64, f64> for Empirical {
fn cdf(&self, x: f64) -> f64 {
let mut sum = 0;
for (keys, values) in &self.data {
if keys.get() > x {
return sum as f64 / self.sum as f64;
}
sum += values;
}
let start = Bound::Unbounded;
let end = Bound::Included(NonNan::new(x).expect("x must not be NaN"));

let sum: u64 = self.data.range((start, end)).map(|(_, v)| v).sum();
sum as f64 / self.sum as f64
}

fn sf(&self, x: f64) -> f64 {
let mut sum = 0;
for (keys, values) in self.data.iter().rev() {
if keys.get() <= x {
return sum as f64 / self.sum as f64;
}
sum += values;
}
let start = Bound::Excluded(NonNan::new(x).expect("x must not be NaN"));
let end = Bound::Unbounded;

let sum: u64 = self.data.range((start, end)).map(|(_, v)| v).sum();
sum as f64 / self.sum as f64
}

Expand Down

0 comments on commit af648be

Please sign in to comment.