Skip to content

Poisson returns -1 for small lambda #1284

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 2 commits into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions rand_distr/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Remove unused fields from `Gamma`, `NormalInverseGaussian` and `Zipf` distributions (#1184)
This breaks serialization compatibility with older versions.
- Upgrade Rand
- Fix Knuth's method so `Poisson` doesn't return -1.0 for small lambda

## [0.4.3] - 2021-12-30
- Fix `no_std` build (#1208)
Expand Down
17 changes: 11 additions & 6 deletions rand_distr/src/poisson.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ where F: Float + FloatConst, Standard: Distribution<F>

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

#[test]
fn test_poisson_avg() {
test_poisson_avg_gen::<f64>(10.0, 0.5);
test_poisson_avg_gen::<f64>(15.0, 0.5);
test_poisson_avg_gen::<f32>(10.0, 0.5);
test_poisson_avg_gen::<f32>(15.0, 0.5);
test_poisson_avg_gen::<f64>(10.0, 0.1);
test_poisson_avg_gen::<f64>(15.0, 0.1);

test_poisson_avg_gen::<f32>(10.0, 0.1);
test_poisson_avg_gen::<f32>(15.0, 0.1);

//Small lambda will use Knuth's method with exp_lambda == 1.0
test_poisson_avg_gen::<f32>(0.00000000000000005, 0.1);
test_poisson_avg_gen::<f64>(0.00000000000000005, 0.1);
}

#[test]
Expand Down