Skip to content

Commit f2e21db

Browse files
authored
Poisson returns -1 for small lambda (#1284)
* Correct Knuth's method since not using do-while
1 parent 7d73990 commit f2e21db

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

rand_distr/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
- Remove unused fields from `Gamma`, `NormalInverseGaussian` and `Zipf` distributions (#1184)
99
This breaks serialization compatibility with older versions.
1010
- Upgrade Rand
11+
- Fix Knuth's method so `Poisson` doesn't return -1.0 for small lambda
1112

1213
## [0.4.3] - 2021-12-30
1314
- Fix `no_std` build (#1208)

rand_distr/src/poisson.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ where F: Float + FloatConst, Standard: Distribution<F>
8989

9090
// for low expected values use the Knuth method
9191
if self.lambda < F::from(12.0).unwrap() {
92-
let mut result = F::zero();
93-
let mut p = F::one();
92+
let mut result = F::one();
93+
let mut p = rng.gen::<F>();
9494
while p > self.exp_lambda {
9595
p = p*rng.gen::<F>();
9696
result = result + F::one();
@@ -161,10 +161,15 @@ mod test {
161161

162162
#[test]
163163
fn test_poisson_avg() {
164-
test_poisson_avg_gen::<f64>(10.0, 0.5);
165-
test_poisson_avg_gen::<f64>(15.0, 0.5);
166-
test_poisson_avg_gen::<f32>(10.0, 0.5);
167-
test_poisson_avg_gen::<f32>(15.0, 0.5);
164+
test_poisson_avg_gen::<f64>(10.0, 0.1);
165+
test_poisson_avg_gen::<f64>(15.0, 0.1);
166+
167+
test_poisson_avg_gen::<f32>(10.0, 0.1);
168+
test_poisson_avg_gen::<f32>(15.0, 0.1);
169+
170+
//Small lambda will use Knuth's method with exp_lambda == 1.0
171+
test_poisson_avg_gen::<f32>(0.00000000000000005, 0.1);
172+
test_poisson_avg_gen::<f64>(0.00000000000000005, 0.1);
168173
}
169174

170175
#[test]

0 commit comments

Comments
 (0)