use rand_distr::Distribution as Distribution2;
use rand_distr::Exp as Exp2;
use crate::distributions::Distribution;
use rand::Rng;
pub struct Exponential {
rate: f64,
}
impl Exponential {
pub fn new(rate: f64) -> Result<Exponential, String> {
if rate <= 0.0 {
Err(format!(
"Exponential: illegal rate `{}` should be greater than 0",
rate
))
} else {
Ok(Exponential { rate })
}
}
}
impl<R: Rng + ?Sized> Distribution<R> for Exponential {
type Domain = f64;
fn sample(&self, rng: &mut R) -> f64 {
Exp2::new(self.rate).unwrap().sample(rng)
}
fn log_prob(&self, x: &f64) -> f64 {
if *x < 0.0 {
f64::NEG_INFINITY
} else {
self.rate.ln() - self.rate * x
}
}
fn is_discrete(&self) -> bool {
false
}
}
impl std::fmt::Display for Exponential {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Exponential {{ rate = {} }}", self.rate)
}
}
#[cfg(test)]
mod tests {
use super::*;
use rand::rngs::ThreadRng;
use rand::thread_rng;
#[test]
fn exponential_sample() {
let mut rng = thread_rng();
let rate = 2.0f64;
let dist = Exponential::new(rate).unwrap();
println!("dist = {}", dist);
let mut total = 0f64;
let trials = 10000;
for _ in 0..trials {
total += dist.sample(&mut rng);
}
let empirical_mean = total / (trials as f64);
let expected_mean = 1.0 / rate;
let expected_std = 1.0 / rate;
let err = 5.0 * expected_std / (trials as f64).sqrt();
assert!((empirical_mean - expected_mean).abs() < err);
}
#[test]
fn exponential_log_prob() {
let dist = Exponential::new(1.0).unwrap();
let lp = <Exponential as Distribution<ThreadRng>>::log_prob(&dist, &1.0);
assert!((lp - (-1.0f64)).abs() < 1e-10);
let lp_out = <Exponential as Distribution<ThreadRng>>::log_prob(&dist, &-0.1);
assert_eq!(lp_out, f64::NEG_INFINITY);
assert!(!<Exponential as Distribution<ThreadRng>>::is_discrete(
&dist
));
}
#[test]
#[should_panic]
fn exponential_zero_rate() {
Exponential::new(0.0).unwrap();
}
#[test]
#[should_panic]
fn exponential_negative_rate() {
Exponential::new(-1.0).unwrap();
}
}