Skip to content

Commit 3a3fa47

Browse files
committed
Address review comments
1 parent 629aa94 commit 3a3fa47

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

src/distributions/binomial.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ impl Binomial {
3838
/// Construct a new `Binomial` with the given shape parameters
3939
/// `n`, `p`. Panics if `p <= 0` or `p >= 1`.
4040
pub fn new(n: u64, p: f64) -> Binomial {
41-
assert!(p > 0.0, "Binomial::new called with `p` <= 0");
42-
assert!(p < 1.0, "Binomial::new called with `p` >= 1");
41+
assert!(p > 0.0, "Binomial::new called with p <= 0");
42+
assert!(p < 1.0, "Binomial::new called with p >= 1");
4343
Binomial { n: n, p: p }
4444
}
4545
}
4646

4747
impl Distribution<u64> for Binomial {
48-
fn sample<R: Rng>(&self, rng: &mut R) -> u64 {
48+
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> u64 {
4949
// binomial distribution is symmetrical with respect to p -> 1-p, k -> n-k
5050
// switch p so that it is less than 0.5 - this allows for lower expected values
5151
// we will just invert the result at the end

src/distributions/log_gamma.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,18 @@
99
// except according to those terms.
1010

1111
/// Calculates ln(gamma(x)) (natural logarithm of the gamma
12-
/// function) using the Lanczos approximation with g=5
12+
/// function) using the Lanczos approximation.
13+
///
14+
/// The approximation expresses the gamma function as:
15+
/// `gamma(z+1) = sqrt(2*pi)*(z+g+0.5)^(z+0.5)*exp(-z-g-0.5)*Ag(z)`
16+
/// `g` is an arbitrary constant; we use the approximation with `g=5`.
17+
///
18+
/// Noting that `gamma(z+1) = z*gamma(z)` and applying `ln` to both sides:
19+
/// `ln(gamma(z)) = (z+0.5)*ln(z+g+0.5)-(z+g+0.5) + ln(sqrt(2*pi)*Ag(z)/z)`
20+
///
21+
/// `Ag(z)` is an infinite series with coefficients that can be calculated
22+
/// ahead of time - we use just the first 6 terms, which is good enough
23+
/// for most purposes.
1324
pub fn log_gamma(x: f64) -> f64 {
1425
// precalculated 6 coefficients for the first 6 terms of the series
1526
let coefficients: [f64; 6] = [
@@ -21,11 +32,11 @@ pub fn log_gamma(x: f64) -> f64 {
2132
-0.5395239384953e-5,
2233
];
2334

24-
// ln((x+g+0.5)^(x+0.5)*exp(-(x+g+0.5)))
35+
// (x+0.5)*ln(x+g+0.5)-(x+g+0.5)
2536
let tmp = x + 5.5;
2637
let log = (x + 0.5) * tmp.ln() - tmp;
2738

28-
// the first few terms of the series
39+
// the first few terms of the series for Ag(x)
2940
let mut a = 1.000000000190015;
3041
let mut denom = x;
3142
for j in 0..6 {
@@ -34,6 +45,7 @@ pub fn log_gamma(x: f64) -> f64 {
3445
}
3546

3647
// get everything together
37-
// division by x is because the series is actually for gamma(x+1) = x*gamma(x)
48+
// a is Ag(x)
49+
// 2.5066... is sqrt(2pi)
3850
return log + (2.5066282746310005 * a / x).ln();
3951
}

src/distributions/poisson.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ use std::f64::consts::PI;
1717

1818
/// The Poisson distribution `Poisson(lambda)`.
1919
///
20-
/// This distribution has density function: `f(k) = lambda^k *
21-
/// exp(-lambda) / k!` for `k >= 0`.
20+
/// This distribution has a density function:
21+
/// `f(k) = lambda^k * exp(-lambda) / k!` for `k >= 0`.
2222
///
2323
/// # Example
2424
///
@@ -42,7 +42,7 @@ impl Poisson {
4242
/// Construct a new `Poisson` with the given shape parameter
4343
/// `lambda`. Panics if `lambda <= 0`.
4444
pub fn new(lambda: f64) -> Poisson {
45-
assert!(lambda > 0.0, "Poisson::new called with `lambda` <= 0");
45+
assert!(lambda > 0.0, "Poisson::new called with lambda <= 0");
4646
Poisson {
4747
lambda: lambda,
4848
exp_lambda: (-lambda).exp(),
@@ -53,7 +53,7 @@ impl Poisson {
5353
}
5454

5555
impl Distribution<u64> for Poisson {
56-
fn sample<R: Rng>(&self, rng: &mut R) -> u64 {
56+
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> u64 {
5757
// using the algorithm from Numerical Recipes in C
5858

5959
// for low expected values use the Knuth method

0 commit comments

Comments
 (0)