Module: Distribution::MathExtension18
- Included in:
- Math
- Defined in:
- lib/distribution/math_extension.rb
Overview
Extension for Ruby18 Includes gamma and lgamma
Constant Summary collapse
- LOG_2PI =
log(2PI)
Math.log(2 * Math::PI)
- N =
8
- B0 =
1.0
- B1 =
-1.0 / 2.0
- B2 =
1.0 / 6.0
- B4 =
-1.0 / 30.0
- B6 =
1.0 / 42.0
- B8 =
-1.0 / 30.0
- B10 =
5.0 / 66.0
- B12 =
-691.0 / 2730.0
- B14 =
7.0 / 6.0
- B16 =
-3617.0 / 510.0
Instance Method Summary collapse
-
#gamma(x) ⇒ Object
Gamma function.
-
#loggamma(x) ⇒ Object
From statistics2.
Instance Method Details
#gamma(x) ⇒ Object
Gamma function. From statistics2
55 56 57 58 59 60 |
# File 'lib/distribution/math_extension.rb', line 55 def gamma(x) if (x < 0.0) return Math::PI / (Math.sin(Math.PI * x) * Math.exp(loggamma(1 - x))) #/ end Math.exp(loggamma(x)) end |
#loggamma(x) ⇒ Object
From statistics2
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/distribution/math_extension.rb', line 34 def loggamma(x) v = 1.0 while (x < N) v *= x x += 1.0 end w = 1.0 / (x * x) ret = B16 / (16 * 15) ret = ret * w + B14 / (14 * 13) ret = ret * w + B12 / (12 * 11) ret = ret * w + B10 / (10 * 9) ret = ret * w + B8 / ( 8 * 7) ret = ret * w + B6 / ( 6 * 5) ret = ret * w + B4 / ( 4 * 3) ret = ret * w + B2 / ( 2 * 1) ret = ret / x + 0.5 * LOG_2PI - Math.log(v) - x + (x - 0.5) * Math.log(x) ret end |