Debian Codegraph @

src/dr/math/distributions/GeneralizedIntegerGammaDistribution.java

raw symbols SCIP
1 /*
2 * Hyperexponential.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.distributions;
27
28 import dr.math.UnivariateFunction;
29
30 /**
31 * GeneralizedIntegerGammaDistribution distribution.
32 * @author Marc A. Suchard
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 // http://www.ism.ac.jp/editsec/aism/pdf/034_3_0591.pdf
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 // System.err.println("A[" + (shape1 - i + 1) + "]: " + factorial);
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 // System.err.println("B[" + (shape2 - i + 1) + "]: " + factorial);
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; // x^{i - 1}
149 factorial *= i; // (i - 1)!
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; // x^{i - 1}
158 factorial *= i; // (i - 1)!
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 // https://en.wikipedia.org/wiki/Generalized_integer_gamma_distribution
170
171 // https://en.wikipedia.org/wiki/Generalized_integer_gamma_distribution
172
173 // http://arxiv.org/pdf/math/0408189v1.pdf
174
175 // http://www.math.utep.edu/Faculty/moschopoulos/Publications/1985-The_Distribution_of_the_Sum_of_Independent_Gamma_Random_Variables.pdf
176
177 // partial fractions, inverse Fourier transform
178 // http://stats.stackexchange.com/questions/72479/general-sum-of-gamma-distributions
179
180
181 }
182