Skip to content

Commit

Permalink
Implementation & Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tessob authored and YeungOnion committed May 23, 2024
1 parent 9f2aa4f commit 2db6bdb
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/distribution/exponential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,19 @@ impl ContinuousCDF<f64, f64> for Exp {
(-self.rate * x).exp()
}
}

/// Calculates the inverse cumulative distribution function.
///
/// # Formula
///
/// ```ignore
/// -ln(1 - p) / λ
/// ```
///
/// where `p` is the probability and `λ` is the rate
fn inverse_cdf(&self, p: f64) -> f64 {
-(1.0 - p).ln() / self.rate
}
}

impl Min<f64> for Exp {
Expand Down Expand Up @@ -457,6 +470,27 @@ mod tests {
test_case(f64::INFINITY, 1.0, cdf(f64::INFINITY));
}

#[test]
fn test_inverse_cdf() {
let distribution = Exp::new(0.42).unwrap();
assert_eq!(distribution.median(), distribution.inverse_cdf(0.5));

let distribution = Exp::new(0.042).unwrap();
assert_eq!(distribution.median(), distribution.inverse_cdf(0.5));

let distribution = Exp::new(0.0042).unwrap();
assert_eq!(distribution.median(), distribution.inverse_cdf(0.5));

let distribution = Exp::new(0.33).unwrap();
assert_eq!(distribution.median(), distribution.inverse_cdf(0.5));

let distribution = Exp::new(0.033).unwrap();
assert_eq!(distribution.median(), distribution.inverse_cdf(0.5));

let distribution = Exp::new(0.0033).unwrap();
assert_eq!(distribution.median(), distribution.inverse_cdf(0.5));
}

#[test]
fn test_sf() {
let sf = |arg: f64| move |x: Exp| x.sf(arg);
Expand Down

0 comments on commit 2db6bdb

Please sign in to comment.