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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
use crate::core::*;
use rand;
use spaces::{Vector, Matrix, discrete::Binary};
use std::fmt;

#[derive(Debug, Clone, Copy)]
pub struct Bernoulli {
    pub p: Probability,
    q: Probability,

    variance: f64,
}

impl Bernoulli {
    pub fn new<P: Into<Probability>>(p: P) -> Bernoulli {
        let p = p.into();

        Bernoulli {
            p: p,
            q: !p,

            variance: (p * !p).into(),
        }
    }
}

impl Into<rand::distributions::Bernoulli> for Bernoulli {
    fn into(self) -> rand::distributions::Bernoulli {
        rand::distributions::Bernoulli::new(f64::from(self.p))
    }
}

impl Into<rand::distributions::Bernoulli> for &Bernoulli {
    fn into(self) -> rand::distributions::Bernoulli {
        rand::distributions::Bernoulli::new(f64::from(self.p))
    }
}

impl Distribution for Bernoulli {
    type Support = Binary;

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

    fn cdf(&self, k: bool) -> Probability {
        if k {
            1.0.into()
        } else {
            0.0.into()
        }
    }

    fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> bool {
        rng.gen_bool(self.p.into())
    }
}

impl DiscreteDistribution for Bernoulli {
    fn pmf(&self, k: bool) -> Probability {
        match k {
            true => self.p,
            false => self.q,
        }
    }
}

impl UnivariateMoments for Bernoulli {
    fn mean(&self) -> f64 { self.p.into() }

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

    fn skewness(&self) -> f64 {
        (1.0 - 2.0 * f64::from(self.p)) / self.variance.sqrt()
    }

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

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

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

    fn median(&self) -> f64 {
        match f64::from(self.p) {
            p if (p - 0.5).abs() < 1e-7 => 0.5,
            p if (p < 0.5) => 0.0,
            _ => 1.0,
        }
    }
}

impl Modes for Bernoulli {
    fn modes(&self) -> Vec<bool> {
        use std::cmp::Ordering::*;

        match self.p.partial_cmp(&self.q) {
            Some(Less) => vec![false],
            Some(Equal) => vec![false, true],
            Some(Greater) => vec![false],
            None => unreachable!(),
        }
    }
}

impl Entropy for Bernoulli {
    fn entropy(&self) -> f64 {
        let p: f64 = self.p.into();
        let q: f64 = self.q.into();

        if q.abs() < 1e-7 || (q - 1.0).abs() < 1e-7 {
            0.0
        } else {
            -q * q.ln() - p*p.ln()
        }
    }
}

impl FisherInformation for Bernoulli {
    fn fisher_information(&self) -> Matrix {
        Matrix::from_elem((1, 1), 1.0 / self.variance)
    }
}

impl MLE for Bernoulli {
    fn fit_mle(samples: Vector<bool>) -> Self {
        let n = samples.len() as f64;

        Bernoulli::new(samples.fold(0, |acc, v| acc + *v as u64) as f64 / n)
    }
}

impl fmt::Display for Bernoulli {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Ber({})", self.p)
    }
}