Debian Codegraph @

src/dr/inference/distribution/InverseGammaDistributionModel.java

raw symbols SCIP
1 /*
2 * InverseGammaDistributionModel.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.InverseGammaDistribution;
34 import org.w3c.dom.Document;
35 import org.w3c.dom.Element;
36
37 /**
38 * A class that acts as a model for inverse gamma distributed data.
39 *
40 * @author Matthew Hall
41 */
42
43 public class InverseGammaDistributionModel extends AbstractModel implements ParametricDistributionModel {
44
45 public static final String INVERSE_GAMMA_DISTRIBUTION_MODEL = "inverseGammaDistributionModel";
46
47 /**
48 * Construct a constant mutation rate model.
49 */
50 public InverseGammaDistributionModel(Variable<Double> shape, Variable<Double> scale) {
51
52 super(INVERSE_GAMMA_DISTRIBUTION_MODEL);
53
54 this.shape = shape;
55 this.scale = scale;
56 addVariable(shape);
57 shape.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
58 addVariable(scale);
59 scale.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
60 }
61
62 /**
63 * Construct a constant mutation rate model.
64 */
65 public InverseGammaDistributionModel(Variable<Double> shape) {
66
67 super(INVERSE_GAMMA_DISTRIBUTION_MODEL);
68
69 this.shape = shape;
70 addVariable(shape);
71 shape.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
72 }
73
74 // *****************************************************************
75 // Interface Distribution
76 // *****************************************************************
77
78 public double pdf(double x) {
79 return InverseGammaDistribution.pdf(x, getShape(), getScale(), 1.0);
80 }
81
82 public double logPdf(double x) {
83 return InverseGammaDistribution.logPdf(x, getShape(), getScale(), 1);
84 }
85
86 public double cdf(double x) {
87 return InverseGammaDistribution.cdf(x, getShape(), getScale());
88 }
89
90 public double quantile(double y) {
91 return (InverseGammaDistribution.quantile(y, getShape(), getScale()));
92 }
93
94 public double mean() {
95 return InverseGammaDistribution.mean(getShape(), getScale());
96 }
97
98 public double variance() {
99 return InverseGammaDistribution.variance(getShape(), getScale());
100 }
101
102 public final UnivariateFunction getProbabilityDensityFunction() {
103 return pdfFunction;
104 }
105
106 private final UnivariateFunction pdfFunction = new UnivariateFunction() {
107 public final double evaluate(double x) {
108 return pdf(x);
109 }
110
111 public final double getLowerBound() {
112 return 0.0;
113 }
114
115 public final double getUpperBound() {
116 return Double.POSITIVE_INFINITY;
117 }
118 };
119
120 // *****************************************************************
121 // Interface DensityModel
122 // *****************************************************************
123
124 @Override
125 public double logPdf(double[] x) {
126 return logPdf(x[0]);
127 }
128
129 @Override
130 public Variable<Double> getLocationVariable() {
131 throw new UnsupportedOperationException("Not implemented");
132 }
133
134 // *****************************************************************
135 // Interface Model
136 // *****************************************************************
137
138 public void handleModelChangedEvent(Model model, Object object, int index) {
139 // no intermediates need to be recalculated...
140 }
141
142 protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
143 // no intermediates need to be recalculated...
144 }
145
146 protected void storeState() {
147 } // no additional state needs storing
148
149 protected void restoreState() {
150 } // no additional state needs restoring
151
152 protected void acceptState() {
153 } // no additional state needs accepting
154
155 // **************************************************************
156 // XMLElement IMPLEMENTATION
157 // **************************************************************
158
159 public Element createElement(Document document) {
160 throw new RuntimeException("Not implemented!");
161 }
162
163 public double getShape() {
164 return shape.getValue(0);
165 }
166
167 public double getScale() {
168 return scale.getValue(0);
169 }
170
171 // **************************************************************
172 // Private instance variables
173 // **************************************************************
174
175 private Variable<Double> shape = null;
176 private Variable<Double> scale = null;
177 }
178