Debian Codegraph @

src/dr/inference/distribution/InverseGaussianDistributionModel.java

raw symbols SCIP
1 /*
2 * InverseGaussianDistributionModel.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.inferencexml.distribution.InverseGaussianDistributionModelParser;
33 import dr.math.UnivariateFunction;
34 import dr.math.distributions.InverseGaussianDistribution;
35 import org.w3c.dom.Document;
36 import org.w3c.dom.Element;
37
38 /**
39 * @author Wai Lok Sibon Li
40 * @version $Id: InverseGaussianDistributionModel.java,v 1.8 2009/03/30 20:25:59 rambaut Exp $
41 */
42
43 public class InverseGaussianDistributionModel extends AbstractModel implements ParametricDistributionModel {
44
45 /**
46 * @param meanParameter the mean, mu
47 * @param igParameter either the standard deviation parameter, sigma or the shape parameter, lamba
48 * @param offset offset of the distribution
49 * @param useShape whether shape or stdev is used
50 */
51 public InverseGaussianDistributionModel(Parameter meanParameter, Parameter igParameter, double offset, boolean useShape) {
52
53 super(InverseGaussianDistributionModelParser.INVERSEGAUSSIAN_DISTRIBUTION_MODEL);
54
55 if(useShape) {
56 this.shapeParameter = igParameter;
57 this.stdevParameter = null;
58 addVariable(shapeParameter);
59 this.shapeParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
60 }
61 else {
62 this.stdevParameter = igParameter;
63 this.shapeParameter = null;
64 addVariable(stdevParameter);
65 this.stdevParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
66 }
67
68 this.meanParameter = meanParameter;
69 addVariable(meanParameter);
70 this.offset = offset;
71 this.meanParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
72
73 }
74
75 public final double getS() {
76 if(stdevParameter==null) {
77 return Math.sqrt(InverseGaussianDistribution.variance(getM(), getShape()));
78 }
79 return stdevParameter.getParameterValue(0);
80 }
81
82 public final void setS(double S) {
83 if(stdevParameter==null) {
84 throw new RuntimeException("Standard deviation parameter is not being used");
85 }
86 else {
87 stdevParameter.setParameterValue(0, S);
88 }
89 }
90
91 public final Parameter getSParameter() {
92 if(stdevParameter==null) {
93 throw new RuntimeException("Standard deviation parameter is not being used");
94 }
95 return stdevParameter;
96 }
97
98 public final double getShape() {
99 if(shapeParameter == null) {
100 double shape = (getM() * getM() * getM()) / (getS() * getS());
101 return shape;
102 }
103 return shapeParameter.getParameterValue(0);
104 }
105
106 public final void setShape(double shape) {
107 if(shapeParameter==null) {
108 throw new RuntimeException("Shape parameter is not being used");
109
110 }
111 else {
112 shapeParameter.setParameterValue(0, shape);
113 }
114 }
115
116 public final Parameter getShapeParameter() {
117 if(shapeParameter==null) {
118 throw new RuntimeException("Shape parameter is not being used");
119 }
120 return shapeParameter;
121 }
122
123 /* Unused method */
124 //private double getStDev() {
125 //return Math.sqrt(InverseGaussianDistribution.variance(getM(), getShape()));//Math.sqrt((getM()*getM()*getM())/getShape());
126 //}
127
128 /**
129 * @return the mean
130 */
131 public final double getM() {
132 return meanParameter.getParameterValue(0);
133 }
134
135 public final void setM(double M) {
136 meanParameter.setParameterValue(0, M);
137 //double shape = (getM() * getM() * getM()) / (getS() * getS());
138 //setShape(shape);
139 }
140
141 public final Parameter getMParameter() {
142 return meanParameter;
143 }
144
145 // *****************************************************************
146 // Interface Distribution
147 // *****************************************************************
148
149 public double pdf(double x) {
150 if (x - offset <= 0.0) return 0.0;
151 return InverseGaussianDistribution.pdf(x - offset, getM(), getShape());
152 }
153
154 public double logPdf(double x) {
155 if (x - offset <= 0.0) return Double.NEGATIVE_INFINITY;
156 return InverseGaussianDistribution.logPdf(x - offset, getM(), getShape());
157 }
158
159 public double cdf(double x) {
160 if (x - offset <= 0.0) return 0.0;
161 return InverseGaussianDistribution.cdf(x - offset, getM(), getShape());
162 }
163
164 public double quantile(double y) {
165 return InverseGaussianDistribution.quantile(y, getM(), getShape()) + offset;
166 }
167
168 /**
169 * @return the mean of the distribution
170 */
171 public double mean() {
172 //return InverseGaussianDistribution.mean(getM(), getShape()) + offset;
173 return getM() + offset;
174 }
175
176 /**
177 * @return the variance of the distribution.
178 */
179 public double variance() {
180 //return InverseGaussianDistribution.variance(getM(), getShape());
181 return getS() * getS();
182 }
183
184 public final UnivariateFunction getProbabilityDensityFunction() {
185 return pdfFunction;
186 }
187
188 private final UnivariateFunction pdfFunction = new UnivariateFunction() {
189 public final double evaluate(double x) {
190 System.out.println("just checking if this ever gets used anyways... probably have to change the getLowerBound in LogNormalDistributionModel if it does");
191 return pdf(x);
192 }
193
194 public final double getLowerBound() {
195 return 0.0;
196 //return Double.NEGATIVE_INFINITY;
197 }
198
199 public final double getUpperBound() {
200 return Double.POSITIVE_INFINITY;
201 }
202 };
203
204 // *****************************************************************
205 // Interface DensityModel
206 // *****************************************************************
207
208 @Override
209 public double logPdf(double[] x) {
210 return logPdf(x[0]);
211 }
212
213 @Override
214 public Variable<Double> getLocationVariable() {
215 throw new UnsupportedOperationException("Not implemented");
216 }
217
218 // *****************************************************************
219 // Interface Model
220 // *****************************************************************
221
222 public void handleModelChangedEvent(Model model, Object object, int index) {
223 // no intermediates need to be recalculated...
224 }
225
226 public void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
227 // no intermediates need to be recalculated...
228 }
229
230 protected void storeState() {
231 } // no additional state needs storing
232
233 protected void restoreState() {
234 } // no additional state needs restoring
235
236 protected void acceptState() {
237 } // no additional state needs accepting
238
239 // **************************************************************
240 // XMLElement IMPLEMENTATION
241 // **************************************************************
242
243 public Element createElement(Document document) {
244 throw new RuntimeException("Not implemented!");
245 }
246
247 // **************************************************************
248 // Private instance variables
249 // **************************************************************
250
251 private final Parameter meanParameter;
252 private final Parameter stdevParameter;
253 private final Parameter shapeParameter;
254 private final double offset;
255
256 }