| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.math.distributions; |
| 27 |
|
| 28 |
import dr.math.UnivariateFunction; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public class GeneralizedIntegerGammaDistribution implements Distribution { |
| 35 |
|
| 36 |
private int shape1, shape2; |
| 37 |
private double rate1, rate2; |
| 38 |
|
| 39 |
private double[] A = null; |
| 40 |
private double[] B = null; |
| 41 |
|
| 42 |
public GeneralizedIntegerGammaDistribution(int shape1, int shape2, double rate1, double rate2) { |
| 43 |
this.shape1 = shape1; |
| 44 |
this.shape2 = shape2; |
| 45 |
this.rate1 = rate1; |
| 46 |
this.rate2 = rate2; |
| 47 |
} |
| 48 |
|
| 49 |
@Override |
| 50 |
public double logPdf(double x) { |
| 51 |
throw new RuntimeException("Not yet implemented"); |
| 52 |
} |
| 53 |
|
| 54 |
@Override |
| 55 |
public double cdf(double x) { |
| 56 |
throw new RuntimeException("Not yet implemented"); |
| 57 |
} |
| 58 |
|
| 59 |
@Override |
| 60 |
public double quantile(double y) { |
| 61 |
throw new RuntimeException("Not yet implemented"); |
| 62 |
} |
| 63 |
|
| 64 |
@Override |
| 65 |
public double mean() { |
| 66 |
throw new RuntimeException("Not yet implemented"); |
| 67 |
} |
| 68 |
|
| 69 |
@Override |
| 70 |
public double variance() { |
| 71 |
throw new RuntimeException("Not yet implemented"); |
| 72 |
} |
| 73 |
|
| 74 |
@Override |
| 75 |
public UnivariateFunction getProbabilityDensityFunction() { |
| 76 |
throw new RuntimeException("Not yet implemented"); |
| 77 |
} |
| 78 |
|
| 79 |
public double generatingFunction(double s) { |
| 80 |
return Math.pow(rate1 / (rate1 + s), shape1) * Math.pow(rate2 / (rate2 + s), shape2); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
public double generatingFunctionPartialFraction(double s) { |
| 85 |
if (A == null) { |
| 86 |
computeCoefficients(); |
| 87 |
} |
| 88 |
double sum = 0.0; |
| 89 |
|
| 90 |
for (int i = 1; i <= shape1; ++i) { |
| 91 |
sum += A[i] / Math.pow(rate1 + s, i); |
| 92 |
} |
| 93 |
|
| 94 |
for (int i = 1; i <= shape2; ++i) { |
| 95 |
sum += B[i] / Math.pow(rate2 + s, i); |
| 96 |
} |
| 97 |
|
| 98 |
return sum; |
| 99 |
} |
| 100 |
|
| 101 |
private void computeCoefficients() { |
| 102 |
A = new double[shape1 + 1]; |
| 103 |
B = new double[shape2 + 1]; |
| 104 |
|
| 105 |
final double lambdaFactor = Math.pow(rate1, shape1) * Math.pow(rate2, shape2); |
| 106 |
|
| 107 |
int sign = 1; |
| 108 |
double factorial = 1.0; |
| 109 |
for (int i = 1; i <= shape1; ++i) { |
| 110 |
if (i > 1 && (shape2 + i - 2) > 1) { |
| 111 |
factorial *= shape2 + i - 2; |
| 112 |
factorial /= i - 1; |
| 113 |
} |
| 114 |
|
| 115 |
A[shape1 - i + 1] = factorial * sign * lambdaFactor / Math.pow(rate2 - rate1, shape2 + i - 1); |
| 116 |
sign *= -1; |
| 117 |
} |
| 118 |
|
| 119 |
sign = 1; |
| 120 |
factorial = 1.0; |
| 121 |
for (int i = 1; i <= shape2; ++i) { |
| 122 |
if (i > 1 && (shape1 + i - 2) > 1) { |
| 123 |
factorial *= shape1 + i - 2; |
| 124 |
factorial /= i - 1; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
B[shape2 - i + 1] = factorial * sign * lambdaFactor / Math.pow(rate1 - rate2, shape1 + i - 1); |
| 129 |
sign *= -1; |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
public double pdf(double x) { |
| 134 |
if (A == null) { |
| 135 |
computeCoefficients(); |
| 136 |
} |
| 137 |
|
| 138 |
final double expRate1X = Math.exp(-rate1 * x); |
| 139 |
final double expRate2X = Math.exp(-rate2 * x); |
| 140 |
|
| 141 |
double sum = 0.0; |
| 142 |
double power = 1.0; |
| 143 |
int factorial = 1; |
| 144 |
|
| 145 |
for (int i = 1; i <= shape1; ++i) { |
| 146 |
sum += A[i] * power * expRate1X / factorial; |
| 147 |
|
| 148 |
power *= x; |
| 149 |
factorial *= i; |
| 150 |
} |
| 151 |
|
| 152 |
power = 1.0; |
| 153 |
factorial = 1; |
| 154 |
for (int i = 1; i <= shape2; ++i) { |
| 155 |
sum += B[i] * power * expRate2X / factorial; |
| 156 |
|
| 157 |
power *= x; |
| 158 |
factorial *= i; |
| 159 |
} |
| 160 |
|
| 161 |
return sum; |
| 162 |
} |
| 163 |
|
| 164 |
public static double pdf(double x, int shape1, int shape2, double rate1, double rate2) { |
| 165 |
return new GeneralizedIntegerGammaDistribution(shape1, shape2, rate1, rate2).pdf(x); |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
} |
| 182 |
|