| 1 | /* |
| 2 | * GammaFunction.java |
| 3 | * |
| 4 | * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard |
| 5 | * |
| 6 | * This file is part of BEAST. |
| 7 | * See the NOTICE file distributed with this work for additional |
| 8 | * information regarding copyright ownership and licensing. |
| 9 | * |
| 10 | * BEAST is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU Lesser General Public License as |
| 12 | * published by the Free Software Foundation; either version 2 |
| 13 | * of the License, or (at your option) any later version. |
| 14 | * |
| 15 | * BEAST is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU Lesser General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU Lesser General Public |
| 21 | * License along with BEAST; if not, write to the |
| 22 | * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, |
| 23 | * Boston, MA 02110-1301 USA |
| 24 | */ |
| 25 | |
| 26 | package dr.math; |
| 27 | |
| 28 | |
| 29 | /** |
| 30 | * gamma function |
| 31 | * |
| 32 | * @author Korbinian Strimmer |
| 33 | * @version $Id: GammaFunction.java,v 1.3 2005/05/24 20:26:01 rambaut Exp $ |
| 34 | */ |
| 35 | public class GammaFunction { |
| 36 | // |
| 37 | // Public stuff |
| 38 | // |
| 39 | |
| 40 | // Gamma function |
| 41 | |
| 42 | /** |
| 43 | * log Gamma function: ln(gamma(alpha)) for alpha>0, accurate to 10 decimal places |
| 44 | * |
| 45 | * @param alpha argument |
| 46 | * @return the log of the gamma function of the given alpha |
| 47 | */ |
| 48 | public static double lnGamma(double alpha) { |
| 49 | // Pike MC & Hill ID (1966) Algorithm 291: Logarithm of the gamma function. |
| 50 | // Communications of the Association for Computing Machinery, 9:684 |
| 51 | |
| 52 | double x = alpha, f = 0.0, z; |
| 53 | |
| 54 | if (x < 7) { |
| 55 | f = 1; |
| 56 | z = x - 1; |
| 57 | while (++z < 7) { |
| 58 | f *= z; |
| 59 | } |
| 60 | x = z; |
| 61 | f = -Math.log(f); |
| 62 | } |
| 63 | z = 1 / (x * x); |
| 64 | |
| 65 | return |
| 66 | f + (x - 0.5) * Math.log(x) - x + 0.918938533204673 + |
| 67 | (((-0.000595238095238 * z + 0.000793650793651) * |
| 68 | z - 0.002777777777778) * z + 0.083333333333333) / x; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Incomplete Gamma function Q(a,x) |
| 73 | * (a cleanroom implementation of Numerical Recipes gammq(a,x); |
| 74 | * in Mathematica this function is called GammaRegularized) |
| 75 | * |
| 76 | * @param a parameter |
| 77 | * @param x argument |
| 78 | * @return function value |
| 79 | */ |
| 80 | public static double incompleteGammaQ(double a, double x) { |
| 81 | return 1.0 - incompleteGamma(x, a, lnGamma(a)); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Incomplete Gamma function P(a,x) = 1-Q(a,x) |
| 86 | * (a cleanroom implementation of Numerical Recipes gammp(a,x); |
| 87 | * in Mathematica this function is 1-GammaRegularized) |
| 88 | * |
| 89 | * @param a parameter |
| 90 | * @param x argument |
| 91 | * @return function value |
| 92 | */ |
| 93 | public static double incompleteGammaP(double a, double x) { |
| 94 | return incompleteGamma(x, a, lnGamma(a)); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Incomplete Gamma function P(a,x) = 1-Q(a,x) |
| 99 | * (a cleanroom implementation of Numerical Recipes gammp(a,x); |
| 100 | * in Mathematica this function is 1-GammaRegularized) |
| 101 | * |
| 102 | * @param a parameter |
| 103 | * @param x argument |
| 104 | * @param lnGammaA precomputed lnGamma(a) |
| 105 | * @return function value |
| 106 | */ |
| 107 | public static double incompleteGammaP(double a, double x, double lnGammaA) { |
| 108 | return incompleteGamma(x, a, lnGammaA); |
| 109 | } |
| 110 | |
| 111 | |
| 112 | /** |
| 113 | * Returns the incomplete gamma ratio I(x,alpha) where x is the upper |
| 114 | * limit of the integration and alpha is the shape parameter. |
| 115 | * |
| 116 | * @param x upper limit of integration |
| 117 | * @param alpha shape parameter |
| 118 | * @param ln_gamma_alpha the log gamma function for alpha |
| 119 | * @return the incomplete gamma ratio |
| 120 | */ |
| 121 | private static double incompleteGamma(double x, double alpha, double ln_gamma_alpha) { |
| 122 | // (1) series expansion if (alpha>x || x<=1) |
| 123 | // (2) continued fraction otherwise |
| 124 | // RATNEST FORTRAN by |
| 125 | // Bhattacharjee GP (1970) The incomplete gamma integral. Applied Statistics, |
| 126 | // 19: 285-287 (AS32) |
| 127 | |
| 128 | double accurate = 1e-8, overflow = 1e30; |
| 129 | double factor, gin, rn, a, b, an, dif, term; |
| 130 | double pn0, pn1, pn2, pn3, pn4, pn5; |
| 131 | |
| 132 | if (x == 0.0) { |
| 133 | return 0.0; |
| 134 | } |
| 135 | if (x < 0.0 || alpha <= 0.0) { |
| 136 | throw new IllegalArgumentException("Arguments out of bounds"); |
| 137 | } |
| 138 | |
| 139 | factor = Math.exp(alpha * Math.log(x) - x - ln_gamma_alpha); |
| 140 | |
| 141 | if (x > 1 && x >= alpha) { |
| 142 | // continued fraction |
| 143 | a = 1 - alpha; |
| 144 | b = a + x + 1; |
| 145 | term = 0; |
| 146 | pn0 = 1; |
| 147 | pn1 = x; |
| 148 | pn2 = x + 1; |
| 149 | pn3 = x * b; |
| 150 | gin = pn2 / pn3; |
| 151 | |
| 152 | do { |
| 153 | a++; |
| 154 | b += 2; |
| 155 | term++; |
| 156 | an = a * term; |
| 157 | pn4 = b * pn2 - an * pn0; |
| 158 | pn5 = b * pn3 - an * pn1; |
| 159 | |
| 160 | if (pn5 != 0) { |
| 161 | rn = pn4 / pn5; |
| 162 | dif = Math.abs(gin - rn); |
| 163 | if (dif <= accurate) { |
| 164 | if (dif <= accurate * rn) { |
| 165 | break; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | gin = rn; |
| 170 | } |
| 171 | pn0 = pn2; |
| 172 | pn1 = pn3; |
| 173 | pn2 = pn4; |
| 174 | pn3 = pn5; |
| 175 | if (Math.abs(pn4) >= overflow) { |
| 176 | pn0 /= overflow; |
| 177 | pn1 /= overflow; |
| 178 | pn2 /= overflow; |
| 179 | pn3 /= overflow; |
| 180 | } |
| 181 | } while (true); |
| 182 | gin = 1 - factor * gin; |
| 183 | } else { |
| 184 | // series expansion |
| 185 | gin = 1; |
| 186 | term = 1; |
| 187 | rn = alpha; |
| 188 | do { |
| 189 | rn++; |
| 190 | term *= x / rn; |
| 191 | gin += term; |
| 192 | } |
| 193 | while (term > accurate); |
| 194 | gin *= factor / alpha; |
| 195 | } |
| 196 | return gin; |
| 197 | } |
| 198 | |
| 199 | } |