Debian Codegraph @

src/dr/inference/distribution/BetaDistributionModel.java

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