Debian Codegraph @

src/dr/math/distributions/InverseGammaDistribution.java

raw symbols SCIP
1 /*
2 * InverseGammaDistribution.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.GammaFunction;
29 import dr.math.UnivariateFunction;
30
31 /**
32 * inverse gamma distribution.
33 * <p/>
34 * (Parameters: shape, scale; mean: ??; variance: ??)
35 *
36 * @author Joseph Heled
37 * @version $Id$
38 */
39 public class InverseGammaDistribution implements Distribution {
40
41 private double shape, scale;
42
43 private final double factor;
44 private final double logFacor;
45
46 public InverseGammaDistribution(double shape, double scale) {
47 this.shape = shape;
48 this.scale = scale;
49 this.factor = Math.pow(scale, shape) / Math.exp(GammaFunction.lnGamma(shape));
50 this.logFacor = shape * Math.log(scale) - GammaFunction.lnGamma(shape);
51 }
52
53 public double getShape() {
54 return shape;
55 }
56
57 public void setShape(double value) {
58 shape = value;
59 }
60
61 public double getScale() {
62 return scale;
63 }
64
65 public void setScale(double value) {
66 scale = value;
67 }
68
69 public double pdf(double x) {
70 return pdf(x, shape, scale, factor);
71 }
72
73 public double logPdf(double x) {
74 return logPdf(x, shape, scale, logFacor);
75 }
76
77 public double cdf(double x) {
78 return cdf(x, shape, scale);
79 }
80
81 public double quantile(double y) {
82 return quantile(y, shape, scale);
83 }
84
85 public double mean() {
86 return mean(shape, scale);
87 }
88
89 public double variance() {
90 return variance(shape, scale);
91 }
92
93 public double nextInverseGamma() {
94 return nextInverseGamma(shape, scale);
95 }
96
97 public final UnivariateFunction getProbabilityDensityFunction() {
98 return pdfFunction;
99 }
100
101 private final UnivariateFunction pdfFunction = new UnivariateFunction() {
102 public final double evaluate(double x) {
103 return pdf(x);
104 }
105
106 public final double getLowerBound() {
107 return 0.0;
108 }
109
110 public final double getUpperBound() {
111 return Double.POSITIVE_INFINITY;
112 }
113 };
114
115 /**
116 * probability density function of the Gamma distribution
117 *
118 * @param x argument
119 * @param shape shape parameter
120 * @param scale scale parameter
121 * @return pdf value
122 */
123 public static double pdf(double x, double shape, double scale, double factor) {
124 if (x <= 0)
125 return 0.0;
126
127 final double a = Math.exp(shape*Math.log(scale) - (shape + 1)*Math.log(x) - (scale/x)
128 - GammaFunction.lnGamma(shape));
129
130 return factor * a;
131 }
132
133 /**
134 * the natural log of the probability density function of the distribution
135 *
136 * @param x argument
137 * @param shape shape parameter
138 * @param scale scale parameter
139 * @return log pdf value
140 */
141 public static double logPdf(double x, double shape, double scale, double factor) {
142 if (x <= 0)
143 return Double.NEGATIVE_INFINITY;
144
145 return factor + shape*Math.log(scale) - (shape + 1)*Math.log(x) - (scale/x) - GammaFunction.lnGamma(shape);
146 }
147
148 /**
149 * cumulative density function of the Gamma distribution
150 *
151 * @param x argument
152 * @param shape shape parameter
153 * @param scale scale parameter
154 * @return cdf value
155 */
156 public static double cdf(double x, double shape, double scale) {
157 if (x <= 0.0 || shape <= 0.0) {
158 return 0.0;
159 }
160 return GammaFunction.incompleteGammaQ(shape, scale/x);
161 }
162
163 /**
164 * quantile (inverse cumulative density function) of the Gamma distribution
165 *
166 * @param y argument
167 * @param shape shape parameter
168 * @param scale scale parameter
169 * @return icdf value
170 */
171 public static double quantile(double y, double shape, double scale) {
172 // this is what R thinks
173 final GammaDistribution g = new GammaDistribution(shape, scale);
174 return 1/g.quantile(1-y);
175 }
176
177 /**
178 * mean of the Gamma distribution
179 *
180 * @param shape shape parameter
181 * @param scale scale parameter
182 * @return mean
183 */
184 public static double mean(double shape, double scale) {
185 if( shape > 1 ) {
186 return scale / (shape - 1);
187 }
188 return Double.POSITIVE_INFINITY;
189 }
190
191 /**
192 * variance of the Gamma distribution.
193 *
194 * @param shape shape parameter
195 * @param scale scale parameter
196 * @return variance
197 */
198 public static double variance(double shape, double scale) {
199 if( shape > 2 ) {
200 return scale*scale / ((shape - 1)*(scale-1)*(scale-2));
201 }
202 return Double.POSITIVE_INFINITY;
203 }
204
205 /**
206 * sample from the Gamma distribution. This could be calculated using
207 * quantile, but current algorithm is faster.
208 *
209 * @param shape shape parameter
210 * @param scale scale parameter
211 * @return sample
212 */
213 public static double nextInverseGamma(double shape, double scale) {
214 return 1.0 / new GammaDistribution(shape, 1/scale).nextGamma();
215 }
216 }