Module: Distribution::Gamma::Ruby_

Extended by:
Math
Defined in:
lib/distribution/gamma/ruby.rb

Constant Summary

Constants included from MathExtension

MathExtension::EULER, MathExtension::LN2, MathExtension::LNPI, MathExtension::LOG_FLOAT_MIN, MathExtension::ROOT3_FLOAT_EPSILON, MathExtension::ROOT3_FLOAT_MIN, MathExtension::ROOT4_FLOAT_EPSILON, MathExtension::ROOT4_FLOAT_MIN, MathExtension::ROOT5_FLOAT_EPSILON, MathExtension::ROOT5_FLOAT_MIN, MathExtension::ROOT6_FLOAT_EPSILON, MathExtension::ROOT6_FLOAT_MIN, MathExtension::SQRT2, MathExtension::SQRTPI

Class Method Summary collapse

Methods included from Math

beta, binomial_coefficient, binomial_coefficient_gamma, combinations, erfc_e, exact_regularized_beta, factorial, fast_factorial, gammp, gammq, incomplete_beta, incomplete_gamma, lbeta, logbeta, loggamma, permutations, regularized_beta, rising_factorial, unnormalized_incomplete_gamma

Methods included from MathExtension

#beta, #binomial_coefficient, #binomial_coefficient_gamma, #binomial_coefficient_multiplicative, #erfc_e, #exact_regularized_beta, #exp_err, #factorial, #fast_factorial, #gammq, #incomplete_beta, #incomplete_gamma, #lbeta, #logbeta, #loggamma, #permutations, #regularized_beta, #rising_factorial, #unnormalized_incomplete_gamma

Class Method Details

.cdf(x, a, b) ⇒ Object

Gamma cumulative distribution function



37
38
39
40
41
42
43
# File 'lib/distribution/gamma/ruby.rb', line 37

def cdf(x, a, b)
  return 0.0 if x <= 0.0

  y = x.quo(b)
  return (1 - Math::IncompleteGamma.q(a, y)) if y > a
  (Math::IncompleteGamma.p(a, y))
end

.pdf(x, a, b) ⇒ Object

Gamma distribution probability density function

If you're looking at Wikipedia's Gamma distribution page, the arguments for this pdf function correspond as follows:

  • +x+: same
  • +a+: alpha or k
  • +b+: theta or 1/beta

This is confusing! But we're trying to most closely mirror the GSL function for the gamma distribution (see references).

Adapted the function itself from GSL-1.9 in rng/gamma.c: gsl_ran_gamma_pdf

==References



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/distribution/gamma/ruby.rb', line 24

def pdf(x, a, b)
  return 0 if x < 0
  if x == 0
    return 1.quo(b) if a == 1
    return 0
  elsif a == 1
    Math.exp(-x.quo(b)).quo(b)
  else
    Math.exp((a - 1) * Math.log(x.quo(b)) - x.quo(b) - Math.lgamma(a).first).quo(b)
  end
end