1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
use crate::core::*;
use rand::Rng;
use spaces::{continuous::PositiveReals, Matrix};
use std::fmt;

#[derive(Debug, Clone, Copy)]
pub struct Exponential {
    pub lambda: f64,
}

impl Exponential {
    pub fn new(lambda: f64) -> Exponential {
        assert_positive_real!(lambda);

        Exponential { lambda }
    }

    pub fn mu(&self) -> f64 {
        1.0 / self.lambda
    }
}

impl Default for Exponential {
    fn default() -> Exponential {
        Exponential { lambda: 1.0 }
    }
}

impl Distribution for Exponential {
    type Support = PositiveReals;

    fn support(&self) -> PositiveReals {
        PositiveReals
    }

    fn cdf(&self, x: f64) -> Probability {
        (1.0 - (-self.lambda * x).exp()).into()
    }

    fn sample<R: Rng + ?Sized>(&self, _: &mut R) -> f64 {
        unimplemented!()
    }
}

impl ContinuousDistribution for Exponential {
    fn pdf(&self, x: f64) -> Probability {
        (self.lambda * (-self.lambda * x).exp()).into()
    }
}

impl UnivariateMoments for Exponential {
    fn mean(&self) -> f64 {
        1.0 / self.lambda
    }

    fn variance(&self) -> f64 {
        1.0 / self.lambda / self.lambda
    }

    fn skewness(&self) -> f64 {
        2.0
    }

    fn kurtosis(&self) -> f64 {
        3.0
    }

    fn excess_kurtosis(&self) -> f64 {
        6.0
    }
}

impl Quantiles for Exponential {
    fn quantile(&self, _: Probability) -> f64 {
        unimplemented!()
    }

    fn median(&self) -> f64 {
        self.mean() * 2.0f64.ln()
    }
}

impl Modes for Exponential {
    fn modes(&self) -> Vec<f64> {
        vec![0.0]
    }
}

impl Entropy for Exponential {
    fn entropy(&self) -> f64 {
        1.0 - self.lambda.ln()
    }
}

impl FisherInformation for Exponential {
    fn fisher_information(&self) -> Matrix {
        Matrix::from_elem((1, 1), self.lambda * self.lambda)
    }
}

impl fmt::Display for Exponential {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Exp({})", self.lambda)
    }
}