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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
use crate::distribution::{Binomial, Discrete, Univariate}; use rand::distributions::Distribution; use rand::Rng; use crate::statistics::*; use crate::Result; /// Implements the /// [Bernoulli](https://en.wikipedia.org/wiki/Bernoulli_distribution) /// distribution which is a special case of the /// [Binomial](https://en.wikipedia.org/wiki/Binomial_distribution) /// distribution where `n = 1` (referenced [Here](./struct.Binomial.html)) /// /// # Examples /// /// ``` /// use statrs::distribution::{Bernoulli, Discrete}; /// use statrs::statistics::Mean; /// /// let n = Bernoulli::new(0.5).unwrap(); /// assert_eq!(n.mean(), 0.5); /// assert_eq!(n.pmf(0), 0.5); /// assert_eq!(n.pmf(1), 0.5); /// ``` #[derive(Debug, Copy, Clone, PartialEq)] pub struct Bernoulli { b: Binomial, } impl Bernoulli { /// Constructs a new bernoulli distribution with /// the given `p` probability of success. /// /// # Errors /// /// Returns an error if `p` is `NaN`, less than `0.0` /// or greater than `1.0` /// /// # Examples /// /// ``` /// use statrs::distribution::Bernoulli; /// /// let mut result = Bernoulli::new(0.5); /// assert!(result.is_ok()); /// /// result = Bernoulli::new(-0.5); /// assert!(result.is_err()); /// ``` pub fn new(p: f64) -> Result<Bernoulli> { Binomial::new(p, 1).map(|b| Bernoulli { b: b }) } /// Returns the probability of success `p` of the /// bernoulli distribution. /// /// # Examples /// /// ``` /// use statrs::distribution::Bernoulli; /// /// let n = Bernoulli::new(0.5).unwrap(); /// assert_eq!(n.p(), 0.5); /// ``` pub fn p(&self) -> f64 { self.b.p() } /// Returns the number of trials `n` of the /// bernoulli distribution. Will always be `1.0`. /// /// # Examples /// /// ``` /// use statrs::distribution::Bernoulli; /// /// let n = Bernoulli::new(0.5).unwrap(); /// assert_eq!(n.n(), 1); /// ``` pub fn n(&self) -> u64 { 1 } } impl Distribution<f64> for Bernoulli { fn sample<R: Rng + ?Sized>(&self, r: &mut R) -> f64 { r.gen_bool(self.p()) as u8 as f64 } } impl Univariate<u64, f64> for Bernoulli { /// Calculates the cumulative distribution /// function for the bernoulli distribution at `x`. /// /// # Formula /// /// ```ignore /// if x < 0 { 0 } /// else if x >= 1 { 1 } /// else { 1 - p } /// ``` fn cdf(&self, x: f64) -> f64 { self.b.cdf(x) } } impl Min<u64> for Bernoulli { /// Returns the minimum value in the domain of the /// bernoulli distribution representable by a 64- /// bit integer /// /// # Formula /// /// ```ignore /// 0 /// ``` fn min(&self) -> u64 { 0 } } impl Max<u64> for Bernoulli { /// Returns the maximum value in the domain of the /// bernoulli distribution representable by a 64- /// bit integer /// /// # Formula /// /// ```ignore /// 1 /// ``` fn max(&self) -> u64 { 1 } } impl Mean<f64> for Bernoulli { /// Returns the mean of the bernoulli /// distribution /// /// # Formula /// /// ```ignore /// p /// ``` fn mean(&self) -> f64 { self.b.mean() } } impl Variance<f64> for Bernoulli { /// Returns the variance of the bernoulli /// distribution /// /// # Formula /// /// ```ignore /// p * (1 - p) /// ``` fn variance(&self) -> f64 { self.b.variance() } /// Returns the standard deviation of the bernoulli /// distribution /// /// # Formula /// /// ```ignore /// sqrt(p * (1 - p)) /// ``` fn std_dev(&self) -> f64 { self.b.std_dev() } } impl Entropy<f64> for Bernoulli { /// Returns the entropy of the bernoulli /// distribution /// /// # Formula /// /// ```ignore /// q = (1 - p) /// -q * ln(q) - p * ln(p) /// ``` fn entropy(&self) -> f64 { self.b.entropy() } } impl Skewness<f64> for Bernoulli { /// Returns the skewness of the bernoulli /// distribution /// /// # Formula /// /// ```ignore /// q = (1 - p) /// (1 - 2p) / sqrt(p * q) /// ``` fn skewness(&self) -> f64 { self.b.skewness() } } impl Median<f64> for Bernoulli { /// Returns the median of the bernoulli /// distribution /// /// # Formula /// /// ```ignore /// if p < 0.5 { 0 } /// else if p > 0.5 { 1 } /// else { 0.5 } /// ``` fn median(&self) -> f64 { self.b.median() } } impl Mode<u64> for Bernoulli { /// Returns the mode of the bernoulli distribution /// /// # Formula /// /// ```ignore /// if p < 0.5 { 0 } /// else { 1 } /// ``` fn mode(&self) -> u64 { self.b.mode() } } impl Discrete<u64, f64> for Bernoulli { /// Calculates the probability mass function for the /// bernoulli distribution at `x`. /// /// # Formula /// /// ```ignore /// if x == 0 { 1 - p } /// else { p } /// ``` fn pmf(&self, x: u64) -> f64 { self.b.pmf(x) } /// Calculates the log probability mass function for the /// bernoulli distribution at `x`. /// /// # Formula /// /// ```ignore /// else if x == 0 { ln(1 - p) } /// else { ln(p) } /// ``` fn ln_pmf(&self, x: u64) -> f64 { self.b.ln_pmf(x) } }