Debian Codegraph @

src/dr/inference/distribution/GammaDistributionModel.java

raw symbols SCIP
1 /*
2 * GammaDistributionModel.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.inference.distribution;
27
28 import dr.inference.model.AbstractModel;
29 import dr.inference.model.Model;
30 import dr.inference.model.Parameter;
31 import dr.inference.model.Variable;
32 import dr.math.UnivariateFunction;
33 import dr.math.distributions.GammaDistribution;
34 import org.apache.commons.math.MathException;
35 import org.apache.commons.math.distribution.GammaDistributionImpl;
36 import org.w3c.dom.Document;
37 import org.w3c.dom.Element;
38
39 /**
40 * A class that acts as a model for gamma distributed data.
41 *
42 * @author Andrew Rambaut
43 * @author Alexei Drummond
44 * @version $Id: GammaDistributionModel.java,v 1.6 2005/05/24 20:25:59 rambaut Exp $
45 */
46
47 public class GammaDistributionModel extends AbstractModel implements ParametricDistributionModel {
48
49 public enum GammaParameterizationType {
50 ShapeScale,
51 ShapeRate,
52 ShapeMean,
53 OneParameter
54 }
55
56 public static final String GAMMA_DISTRIBUTION_MODEL = "gammaDistributionModel";
57 public static final String ONE_P_GAMMA_DISTRIBUTION_MODEL = "onePGammaDistributionModel";
58
59 /**
60 * Construct a gamma distribution model with a default shape scale parameterization.
61 */
62 public GammaDistributionModel(Variable<Double> shape, Variable<Double> scale) {
63 this(GammaParameterizationType.ShapeScale, shape, scale, 0.0);
64 }
65
66 /**
67 * Construct a one parameter gamma distribution model.
68 */
69 public GammaDistributionModel(Variable<Double> shape) {
70 this(GammaParameterizationType.OneParameter, shape, null, 0.0);
71 }
72
73
74 /**
75 * Construct a gamma distribution model.
76 */
77 public GammaDistributionModel(GammaParameterizationType parameterization, Variable<Double> shape, Variable<Double> parameter2, double offset) {
78
79 super(GAMMA_DISTRIBUTION_MODEL);
80
81 this.offset = offset;
82
83 this.parameterization = parameterization;
84 this.shape = shape;
85 addVariable(shape);
86 shape.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
87
88 switch (parameterization) {
89 case ShapeScale:
90 this.scale = parameter2;
91 addVariable(scale);
92 scale.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
93 rate = null;
94 mean = null;
95 break;
96 case ShapeRate:
97 this.rate = parameter2;
98 addVariable(rate);
99 rate.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
100 scale = null;
101 mean = null;
102 break;
103 case ShapeMean:
104 this.mean = parameter2;
105 addVariable(mean);
106 mean.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
107 scale = null;
108 rate = null;
109 break;
110 case OneParameter:
111 scale = null;
112 rate = null;
113 mean = null;
114 break;
115 default:
116 throw new IllegalArgumentException("Unknown parameterization type");
117 }
118 }
119
120 // *****************************************************************
121 // Interface Distribution
122 // *****************************************************************
123
124 public double pdf(double x) {
125 if (x < offset) return 0.0;
126 return GammaDistribution.pdf(x - offset, getShape(), getScale());
127 }
128
129 public double logPdf(double x) {
130 if (x < offset) return Double.NEGATIVE_INFINITY;
131 return GammaDistribution.logPdf(x - offset, getShape(), getScale());
132 }
133
134 public double cdf(double x) {
135 if (x < offset) return 0.0;
136 return GammaDistribution.cdf(x - offset, getShape(), getScale());
137 }
138
139 public double quantile(double y) {
140 try {
141 return (new GammaDistributionImpl(getShape(), getScale())).inverseCumulativeProbability(y) + offset;
142 } catch (MathException e) {
143 return Double.NaN;
144 }
145 }
146
147 public double mean() {
148 return GammaDistribution.mean(getShape(), getScale()) + offset;
149 }
150
151 public double variance() {
152 return GammaDistribution.variance(getShape(), getScale());
153 }
154
155 public final UnivariateFunction getProbabilityDensityFunction() {
156 return pdfFunction;
157 }
158
159 private final UnivariateFunction pdfFunction = new UnivariateFunction() {
160 public final double evaluate(double x) {
161 return pdf(x);
162 }
163
164 public final double getLowerBound() {
165 return offset;
166 }
167
168 public final double getUpperBound() {
169 return Double.POSITIVE_INFINITY;
170 }
171 };
172
173 // *****************************************************************
174 // Interface Model
175 // *****************************************************************
176
177 public void handleModelChangedEvent(Model model, Object object, int index) {
178 // no intermediates need to be recalculated...
179 }
180
181 protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
182 // no intermediates need to be recalculated...
183 }
184
185 protected void storeState() {
186 } // no additional state needs storing
187
188 protected void restoreState() {
189 } // no additional state needs restoring
190
191 protected void acceptState() {
192 } // no additional state needs accepting
193
194 // **************************************************************
195 // XMLElement IMPLEMENTATION
196 // **************************************************************
197
198 public Element createElement(Document document) {
199 throw new RuntimeException("Not implemented!");
200 }
201
202 public double getShape() {
203 return shape.getValue(0);
204 }
205
206 public double getScale() {
207 switch (parameterization) {
208 case ShapeScale:
209 return scale.getValue(0);
210 case ShapeRate:
211 return (1.0 / rate.getValue(0));
212 case ShapeMean:
213 return (mean.getValue(0) / getShape());
214 case OneParameter:
215 return (1.0 / getShape());
216 default:
217 throw new IllegalArgumentException("Unknown parameterization type");
218 }
219 }
220
221 // *****************************************************************
222 // Interface DensityModel
223 // *****************************************************************
224
225 @Override
226 public double logPdf(double[] x) {
227 return logPdf(x[0]);
228 }
229
230 @Override
231 public Variable<Double> getLocationVariable() {
232 throw new UnsupportedOperationException("Not implemented");
233 }
234
235 // **************************************************************
236 // Private instance variables
237 // **************************************************************
238
239 private final GammaParameterizationType parameterization;
240
241 private final Variable<Double> shape;
242 private final Variable<Double> scale;
243 private final Variable<Double> rate;
244 private final Variable<Double> mean;
245
246 private final double offset;
247
248 }
249