| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
use crate::distributions::Distribution; |
| 12 |
use crate::Rng; |
| 13 |
use core::{fmt, u64}; |
| 14 |
|
| 15 |
#[cfg(feature = "serde1")] |
| 16 |
use serde::{Serialize, Deserialize}; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
#[derive(Clone, Copy, Debug, PartialEq)] |
| 37 |
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))] |
| 38 |
pub struct Bernoulli { |
| 39 |
|
| 40 |
p_int: u64, |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
const ALWAYS_TRUE: u64 = u64::MAX; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
const SCALE: f64 = 2.0 * (1u64 << 63) as f64; |
| 67 |
|
| 68 |
|
| 69 |
#[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 70 |
pub enum BernoulliError { |
| 71 |
|
| 72 |
InvalidProbability, |
| 73 |
} |
| 74 |
|
| 75 |
impl fmt::Display for BernoulliError { |
| 76 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 77 |
f.write_str(match self { |
| 78 |
BernoulliError::InvalidProbability => "p is outside [0, 1] in Bernoulli distribution", |
| 79 |
}) |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
#[cfg(feature = "std")] |
| 84 |
impl ::std::error::Error for BernoulliError {} |
| 85 |
|
| 86 |
impl Bernoulli { |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
#[inline] |
| 98 |
pub fn new(p: f64) -> Result<Bernoulli, BernoulliError> { |
| 99 |
if !(0.0..1.0).contains(&p) { |
| 100 |
if p == 1.0 { |
| 101 |
return Ok(Bernoulli { p_int: ALWAYS_TRUE }); |
| 102 |
} |
| 103 |
return Err(BernoulliError::InvalidProbability); |
| 104 |
} |
| 105 |
Ok(Bernoulli { |
| 106 |
p_int: (p * SCALE) as u64, |
| 107 |
}) |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
#[inline] |
| 119 |
pub fn from_ratio(numerator: u32, denominator: u32) -> Result<Bernoulli, BernoulliError> { |
| 120 |
if numerator > denominator || denominator == 0 { |
| 121 |
return Err(BernoulliError::InvalidProbability); |
| 122 |
} |
| 123 |
if numerator == denominator { |
| 124 |
return Ok(Bernoulli { p_int: ALWAYS_TRUE }); |
| 125 |
} |
| 126 |
let p_int = ((f64::from(numerator) / f64::from(denominator)) * SCALE) as u64; |
| 127 |
Ok(Bernoulli { p_int }) |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
impl Distribution<bool> for Bernoulli { |
| 132 |
#[inline] |
| 133 |
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> bool { |
| 134 |
|
| 135 |
if self.p_int == ALWAYS_TRUE { |
| 136 |
return true; |
| 137 |
} |
| 138 |
let v: u64 = rng.gen(); |
| 139 |
v < self.p_int |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
#[cfg(test)] |
| 144 |
mod test { |
| 145 |
use super::Bernoulli; |
| 146 |
use crate::distributions::Distribution; |
| 147 |
use crate::Rng; |
| 148 |
|
| 149 |
#[test] |
| 150 |
#[cfg(feature="serde1")] |
| 151 |
fn test_serializing_deserializing_bernoulli() { |
| 152 |
let coin_flip = Bernoulli::new(0.5).unwrap(); |
| 153 |
let de_coin_flip : Bernoulli = bincode::deserialize(&bincode::serialize(&coin_flip).unwrap()).unwrap(); |
| 154 |
|
| 155 |
assert_eq!(coin_flip.p_int, de_coin_flip.p_int); |
| 156 |
} |
| 157 |
|
| 158 |
#[test] |
| 159 |
fn test_trivial() { |
| 160 |
|
| 161 |
#![allow(clippy::bool_assert_comparison)] |
| 162 |
|
| 163 |
let mut r = crate::test::rng(1); |
| 164 |
let always_false = Bernoulli::new(0.0).unwrap(); |
| 165 |
let always_true = Bernoulli::new(1.0).unwrap(); |
| 166 |
for _ in 0..5 { |
| 167 |
assert_eq!(r.sample::<bool, _>(&always_false), false); |
| 168 |
assert_eq!(r.sample::<bool, _>(&always_true), true); |
| 169 |
assert_eq!(Distribution::<bool>::sample(&always_false, &mut r), false); |
| 170 |
assert_eq!(Distribution::<bool>::sample(&always_true, &mut r), true); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
#[test] |
| 175 |
#[cfg_attr(miri, ignore)] |
| 176 |
fn test_average() { |
| 177 |
const P: f64 = 0.3; |
| 178 |
const NUM: u32 = 3; |
| 179 |
const DENOM: u32 = 10; |
| 180 |
let d1 = Bernoulli::new(P).unwrap(); |
| 181 |
let d2 = Bernoulli::from_ratio(NUM, DENOM).unwrap(); |
| 182 |
const N: u32 = 100_000; |
| 183 |
|
| 184 |
let mut sum1: u32 = 0; |
| 185 |
let mut sum2: u32 = 0; |
| 186 |
let mut rng = crate::test::rng(2); |
| 187 |
for _ in 0..N { |
| 188 |
if d1.sample(&mut rng) { |
| 189 |
sum1 += 1; |
| 190 |
} |
| 191 |
if d2.sample(&mut rng) { |
| 192 |
sum2 += 1; |
| 193 |
} |
| 194 |
} |
| 195 |
let avg1 = (sum1 as f64) / (N as f64); |
| 196 |
assert!((avg1 - P).abs() < 5e-3); |
| 197 |
|
| 198 |
let avg2 = (sum2 as f64) / (N as f64); |
| 199 |
assert!((avg2 - (NUM as f64) / (DENOM as f64)).abs() < 5e-3); |
| 200 |
} |
| 201 |
|
| 202 |
#[test] |
| 203 |
fn value_stability() { |
| 204 |
let mut rng = crate::test::rng(3); |
| 205 |
let distr = Bernoulli::new(0.4532).unwrap(); |
| 206 |
let mut buf = [false; 10]; |
| 207 |
for x in &mut buf { |
| 208 |
*x = rng.sample(&distr); |
| 209 |
} |
| 210 |
assert_eq!(buf, [ |
| •211 |
false, true, true, true, false, false, true, true, true, true |
| 212 |
]); |
| 213 |
} |
| 214 |
|
| 215 |
#[test] |
| 216 |
fn bernoulli_distributions_can_be_compared() { |
| 217 |
assert_eq!(Bernoulli::new(1.0), Bernoulli::new(1.0)); |
| 218 |
} |
| 219 |
} |