rand/distr/bernoulli.rs
1// Copyright 2018 Developers of the Rand project.
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9//! The Bernoulli distribution `Bernoulli(p)`.
10
11use crate::distr::Distribution;
12use crate::{Rng, RngExt};
13use core::fmt;
14
15#[cfg(feature = "serde")]
16use serde::{Deserialize, Serialize};
17
18/// The [Bernoulli distribution](https://en.wikipedia.org/wiki/Bernoulli_distribution) `Bernoulli(p)`.
19///
20/// This distribution describes a single boolean random variable, which is true
21/// with probability `p` and false with probability `1 - p`.
22/// It is a special case of the Binomial distribution with `n = 1`.
23///
24/// # Plot
25///
26/// The following plot shows the Bernoulli distribution with `p = 0.1`,
27/// `p = 0.5`, and `p = 0.9`.
28///
29/// 
30///
31/// # Example
32///
33/// ```rust
34/// use rand::distr::{Bernoulli, Distribution};
35///
36/// let d = Bernoulli::new(0.3).unwrap();
37/// let v = d.sample(&mut rand::rng());
38/// println!("{} is from a Bernoulli distribution", v);
39/// ```
40///
41/// # Precision
42///
43/// This `Bernoulli` distribution uses 64 bits from the RNG (a `u64`),
44/// so only probabilities that are multiples of 2<sup>-64</sup> can be
45/// represented.
46#[derive(Clone, Copy, Debug, PartialEq)]
47#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
48pub struct Bernoulli {
49 /// Probability of success, relative to the maximal integer.
50 p_int: u64,
51}
52
53// To sample from the Bernoulli distribution we use a method that compares a
54// random `u64` value `v < (p * 2^64)`.
55//
56// If `p == 1.0`, the integer `v` to compare against can not represented as a
57// `u64`. We manually set it to `u64::MAX` instead (2^64 - 1 instead of 2^64).
58// Note that value of `p < 1.0` can never result in `u64::MAX`, because an
59// `f64` only has 53 bits of precision, and the next largest value of `p` will
60// result in `2^64 - 2048`.
61//
62// Also there is a 100% theoretical concern: if someone consistently wants to
63// generate `true` using the Bernoulli distribution (i.e. by using a probability
64// of `1.0`), just using `u64::MAX` is not enough. On average it would return
65// false once every 2^64 iterations. Some people apparently care about this
66// case.
67//
68// That is why we special-case `u64::MAX` to always return `true`, without using
69// the RNG, and pay the performance price for all uses that *are* reasonable.
70// Luckily, if `new()` and `sample` are close, the compiler can optimize out the
71// extra check.
72const ALWAYS_TRUE: u64 = u64::MAX;
73
74// This is just `2.0.powi(64)`, but written this way because it is not available
75// in `no_std` mode.
76const SCALE: f64 = 2.0 * (1u64 << 63) as f64;
77
78/// Error type returned from [`Bernoulli::new`].
79#[derive(Clone, Copy, Debug, PartialEq, Eq)]
80pub enum BernoulliError {
81 /// `p < 0` or `p > 1`.
82 InvalidProbability,
83}
84
85impl fmt::Display for BernoulliError {
86 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
87 f.write_str(match self {
88 BernoulliError::InvalidProbability => "p is outside [0, 1] in Bernoulli distribution",
89 })
90 }
91}
92
93impl core::error::Error for BernoulliError {}
94
95impl Bernoulli {
96 /// Construct a new `Bernoulli` with the given probability of success `p`.
97 ///
98 /// # Precision
99 ///
100 /// For `p = 1.0`, the resulting distribution will always generate true.
101 /// For `p = 0.0`, the resulting distribution will always generate false.
102 ///
103 /// This method is accurate for any input `p` in the range `[0, 1]` which is
104 /// a multiple of 2<sup>-64</sup>. (Note that not all multiples of
105 /// 2<sup>-64</sup> in `[0, 1]` can be represented as a `f64`.)
106 #[inline]
107 pub fn new(p: f64) -> Result<Bernoulli, BernoulliError> {
108 if !(0.0..1.0).contains(&p) {
109 if p == 1.0 {
110 return Ok(Bernoulli { p_int: ALWAYS_TRUE });
111 }
112 return Err(BernoulliError::InvalidProbability);
113 }
114 Ok(Bernoulli {
115 p_int: (p * SCALE) as u64,
116 })
117 }
118
119 /// Construct a new `Bernoulli` with the probability of success of
120 /// `numerator`-in-`denominator`. I.e. `new_ratio(2, 3)` will return
121 /// a `Bernoulli` with a 2-in-3 chance, or about 67%, of returning `true`.
122 ///
123 /// return `true`. If `numerator == 0` it will always return `false`.
124 /// For `numerator > denominator` and `denominator == 0`, this returns an
125 /// error. Otherwise, for `numerator == denominator`, samples are always
126 /// true; for `numerator == 0` samples are always false.
127 #[inline]
128 pub fn from_ratio(numerator: u32, denominator: u32) -> Result<Bernoulli, BernoulliError> {
129 if numerator > denominator || denominator == 0 {
130 return Err(BernoulliError::InvalidProbability);
131 }
132 if numerator == denominator {
133 return Ok(Bernoulli { p_int: ALWAYS_TRUE });
134 }
135 let p_int = ((f64::from(numerator) / f64::from(denominator)) * SCALE) as u64;
136 Ok(Bernoulli { p_int })
137 }
138
139 #[inline]
140 /// Returns the probability (`p`) of the distribution.
141 ///
142 /// This value may differ slightly from the input due to loss of precision.
143 pub fn p(&self) -> f64 {
144 if self.p_int == ALWAYS_TRUE {
145 1.0
146 } else {
147 (self.p_int as f64) / SCALE
148 }
149 }
150}
151
152impl Distribution<bool> for Bernoulli {
153 #[inline]
154 fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> bool {
155 // Make sure to always return true for p = 1.0.
156 if self.p_int == ALWAYS_TRUE {
157 return true;
158 }
159 let v: u64 = rng.random();
160 v < self.p_int
161 }
162}
163
164#[cfg(test)]
165mod test {
166 use super::Bernoulli;
167 use crate::RngExt;
168 use crate::distr::Distribution;
169
170 #[test]
171 #[cfg(feature = "serde")]
172 fn test_serializing_deserializing_bernoulli() {
173 let coin_flip = Bernoulli::new(0.5).unwrap();
174 let de_coin_flip: Bernoulli =
175 postcard::from_bytes(&postcard::to_allocvec(&coin_flip).unwrap()).unwrap();
176
177 assert_eq!(coin_flip.p_int, de_coin_flip.p_int);
178 }
179
180 #[test]
181 fn test_trivial() {
182 // We prefer to be explicit here.
183 #![allow(clippy::bool_assert_comparison)]
184
185 let mut r = crate::test::rng(1);
186 let always_false = Bernoulli::new(0.0).unwrap();
187 let always_true = Bernoulli::new(1.0).unwrap();
188 for _ in 0..5 {
189 assert_eq!(r.sample::<bool, _>(&always_false), false);
190 assert_eq!(r.sample::<bool, _>(&always_true), true);
191 assert_eq!(Distribution::<bool>::sample(&always_false, &mut r), false);
192 assert_eq!(Distribution::<bool>::sample(&always_true, &mut r), true);
193 }
194 }
195
196 #[test]
197 #[cfg_attr(miri, ignore)] // Miri is too slow
198 fn test_average() {
199 const P: f64 = 0.3;
200 const NUM: u32 = 3;
201 const DENOM: u32 = 10;
202 let d1 = Bernoulli::new(P).unwrap();
203 let d2 = Bernoulli::from_ratio(NUM, DENOM).unwrap();
204 const N: u32 = 100_000;
205
206 let mut sum1: u32 = 0;
207 let mut sum2: u32 = 0;
208 let mut rng = crate::test::rng(2);
209 for _ in 0..N {
210 if d1.sample(&mut rng) {
211 sum1 += 1;
212 }
213 if d2.sample(&mut rng) {
214 sum2 += 1;
215 }
216 }
217 let avg1 = (sum1 as f64) / (N as f64);
218 assert!((avg1 - P).abs() < 5e-3);
219
220 let avg2 = (sum2 as f64) / (N as f64);
221 assert!((avg2 - (NUM as f64) / (DENOM as f64)).abs() < 5e-3);
222 }
223
224 #[test]
225 fn value_stability() {
226 let mut rng = crate::test::rng(3);
227 let distr = Bernoulli::new(0.4532).unwrap();
228 let mut buf = [false; 10];
229 for x in &mut buf {
230 *x = rng.sample(distr);
231 }
232 assert_eq!(
233 buf,
234 [
235 true, false, false, true, false, false, true, true, true, true
236 ]
237 );
238 }
239
240 #[test]
241 fn bernoulli_distributions_can_be_compared() {
242 assert_eq!(Bernoulli::new(1.0), Bernoulli::new(1.0));
243 }
244}