Skip to content

Commit 8e9cec0

Browse files
committed
Address more comments
1 parent 3a3fa47 commit 8e9cec0

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

src/distributions/poisson.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub struct Poisson {
3535
// precalculated values
3636
exp_lambda: f64,
3737
log_lambda: f64,
38+
sqrt_2lambda: f64,
3839
magic_val: f64,
3940
}
4041

@@ -43,11 +44,13 @@ impl Poisson {
4344
/// `lambda`. Panics if `lambda <= 0`.
4445
pub fn new(lambda: f64) -> Poisson {
4546
assert!(lambda > 0.0, "Poisson::new called with lambda <= 0");
47+
let log_lambda = lambda.ln();
4648
Poisson {
4749
lambda: lambda,
4850
exp_lambda: (-lambda).exp(),
49-
log_lambda: lambda.ln(),
50-
magic_val: lambda * lambda.ln() - log_gamma(1.0 + lambda),
51+
log_lambda: log_lambda,
52+
sqrt_2lambda: (2.0 * lambda).sqrt(),
53+
magic_val: lambda * log_lambda - log_gamma(1.0 + lambda),
5154
}
5255
}
5356
}
@@ -68,8 +71,6 @@ impl Distribution<u64> for Poisson {
6871
}
6972
// high expected values - rejection method
7073
else {
71-
// some cached values
72-
let tmp = (2.0 * self.lambda).sqrt();
7374
let mut int_result: u64;
7475

7576
loop {
@@ -82,7 +83,7 @@ impl Distribution<u64> for Poisson {
8283
// draw from the lorentzian distribution
8384
comp_dev = (PI * rng.gen::<f64>()).tan();
8485
// shift the peak of the comparison ditribution
85-
result = tmp * comp_dev + self.lambda;
86+
result = self.sqrt_2lambda * comp_dev + self.lambda;
8687
// repeat the drawing until we are in the range of possible values
8788
if result >= 0.0 {
8889
break;

0 commit comments

Comments
 (0)