[−][src]Function rand::distributions::log_gamma::log_gamma
pub fn log_gamma(x: f64) -> f64
Calculates ln(gamma(x)) (natural logarithm of the gamma function) using the Lanczos approximation.
The approximation expresses the gamma function as:
gamma(z+1) = sqrt(2*pi)*(z+g+0.5)^(z+0.5)*exp(-z-g-0.5)*Ag(z)
g
is an arbitrary constant; we use the approximation with g=5
.
Noting that gamma(z+1) = z*gamma(z)
and applying ln
to both sides:
ln(gamma(z)) = (z+0.5)*ln(z+g+0.5)-(z+g+0.5) + ln(sqrt(2*pi)*Ag(z)/z)
Ag(z)
is an infinite series with coefficients that can be calculated
ahead of time - we use just the first 6 terms, which is good enough
for most purposes.