Debian Codegraph @

src/dr/inference/distribution/TruncatedNormalDistributionModel.java

raw symbols SCIP
1 /*
2 * TruncatedNormalDistributionModel.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 * NormalDistributionModel.java
28 *
29 * Copyright (C) 2002-2009 Alexei Drummond and Andrew Rambaut
30 *
31 * This file is part of BEAST.
32 * See the NOTICE file distributed with this work for additional
33 * information regarding copyright ownership and licensing.
34 *
35 * BEAST is free software; you can redistribute it and/or modify
36 * it under the terms of the GNU Lesser General Public License as
37 * published by the Free Software Foundation; either version 2
38 * of the License, or (at your option) any later version.
39 *
40 * BEAST is distributed in the hope that it will be useful,
41 * but WITHOUT ANY WARRANTY; without even the implied warranty of
42 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
43 * GNU Lesser General Public License for more details.
44 *
45 * You should have received a copy of the GNU Lesser General Public
46 * License along with BEAST; if not, write to the
47 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
48 * Boston, MA 02110-1301 USA
49 */
50
51 import dr.inference.model.AbstractModel;
52 import dr.inference.model.Model;
53 import dr.inference.model.Parameter;
54 import dr.inference.model.Variable;
55 import dr.inferencexml.distribution.NormalDistributionModelParser;
56 import dr.inferencexml.distribution.TruncatedNormalDistributionModelParser;
57 import dr.math.UnivariateFunction;
58 import dr.math.distributions.TruncatedNormalDistribution;
59 import org.w3c.dom.Document;
60 import org.w3c.dom.Element;
61
62 /**
63 * A class that acts as a model for normally distributed data.
64 *
65 * @author Alexei Drummond
66 * @version $Id: NormalDistributionModel.java,v 1.6 2005/05/24 20:25:59 rambaut Exp $
67 */
68
69 public class TruncatedNormalDistributionModel extends AbstractModel implements ParametricDistributionModel {
70 /**
71 * Constructor.
72 */
73 public TruncatedNormalDistributionModel(Variable<Double> mean, Variable<Double> stdev, Variable<Double> minimum,
74 Variable<Double> maximum) {
75 super(TruncatedNormalDistributionModelParser.TRUNCATED_NORMAL_DISTRIBUTION_MODEL);
76 this.mean = mean;
77 this.stdev = stdev;
78 this.minimum = minimum;
79 this.maximum = maximum;
80 this.
81 addVariable(mean);
82 mean.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 1));
83 addVariable(stdev);
84 stdev.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
85 addVariable(minimum);
86 minimum.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 1));
87 addVariable(maximum);
88 maximum.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 1));
89 recomputeTruncatedNormalDistribution();
90 }
91
92 public TruncatedNormalDistributionModel(Parameter meanParameter, Parameter scale, Parameter minParameter, Parameter
93 maxParameter, boolean isPrecision) {
94 super(NormalDistributionModelParser.NORMAL_DISTRIBUTION_MODEL);
95 this.minimum = minParameter;
96 this.maximum = maxParameter;
97 this.hasPrecision = isPrecision;
98 this.mean = meanParameter;
99 addVariable(meanParameter);
100 meanParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 1));
101 addVariable(maxParameter);
102 maxParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 1));
103 addVariable(minParameter);
104 minParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 1));
105 if (isPrecision) {
106 this.precision = scale;
107 this.stdev = null;
108 } else {
109 this.stdev = scale;
110 }
111 addVariable(scale);
112 scale.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
113 recomputeTruncatedNormalDistribution();
114 }
115
116 public double getStdev() {
117 if (hasPrecision)
118 return 1.0 / Math.sqrt(precision.getValue(0));
119 return stdev.getValue(0);
120 }
121
122 public Variable<Double> getMean() {
123 return mean;
124 }
125
126 public Variable<Double> getMaximum() {
127 return maximum;
128 }
129
130 public Variable<Double> getMinimum() {
131 return minimum;
132 }
133
134 public Variable<Double> getPrecision() {
135 if (hasPrecision)
136 return precision;
137 return null;
138 }
139
140 // *****************************************************************
141 // Interface Distribution
142 // *****************************************************************
143
144 public double pdf(double x) {
145 return distribution.pdf(x);
146 }
147
148 public double logPdf(double x) {
149 return distribution.logPdf(x);
150 }
151
152 public double cdf(double x) {
153 return distribution.cdf(x);
154 }
155
156 public double quantile(double y) {
157 return distribution.quantile(y);
158 }
159
160 public double mean() {
161 return mean.getValue(0);
162 }
163
164 public double minimum() {
165 return minimum.getValue(0);
166 }
167
168 public double maximum() {
169 return maximum.getValue(0);
170 }
171
172 public double variance() {
173 if (hasPrecision)
174 return 1.0 / precision.getValue(0);
175 double sd = stdev.getValue(0);
176 return sd * sd;
177 }
178
179 public final UnivariateFunction getProbabilityDensityFunction() {
180 return pdfFunction;
181 }
182
183 private void recomputeTruncatedNormalDistribution(){
184 distribution = new TruncatedNormalDistribution(mean.getValue(0), stdev.getValue(0), minimum.getValue(0),
185 maximum.getValue(0));
186 };
187
188 private final UnivariateFunction pdfFunction = new UnivariateFunction() {
189 public final double evaluate(double x) {
190 return pdf(x);
191 }
192
193 public final double getLowerBound() {
194 return Double.NEGATIVE_INFINITY;
195 }
196
197 public final double getUpperBound() {
198 return Double.POSITIVE_INFINITY;
199 }
200 };
201
202 // *****************************************************************
203 // Interface DensityModel
204 // *****************************************************************
205
206 @Override
207 public double logPdf(double[] x) {
208 return logPdf(x[0]);
209 }
210
211 @Override
212 public Variable<Double> getLocationVariable() {
213 return mean;
214 }
215
216 // *****************************************************************
217 // Interface Model
218 // *****************************************************************
219
220 public void handleModelChangedEvent(Model model, Object object, int index) {
221 // no intermediates need to be recalculated...
222 }
223
224 protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
225 recomputeTruncatedNormalDistribution();
226 }
227
228 protected void storeState() {
229 storedDistribution = distribution;
230 }
231
232 protected void restoreState() {
233 distribution = storedDistribution;
234 }
235
236 protected void acceptState() {
237 } // no additional state needs accepting
238
239 public Element createElement(Document document) {
240 throw new RuntimeException("Not implemented!");
241 }
242
243
244 // **************************************************************
245 // Private instance variables
246 // **************************************************************
247
248 private final Variable<Double> mean;
249 private final Variable<Double> stdev;
250 private final Variable<Double> minimum;
251 private final Variable<Double> maximum;
252 private Variable<Double> precision;
253 private boolean hasPrecision = false;
254 private TruncatedNormalDistribution distribution = null;
255 private TruncatedNormalDistribution storedDistribution = null;
256
257 }