Debian Codegraph @

src/dr/inference/distribution/NormalDistributionModel.java

raw symbols SCIP
1 /*
2 * NormalDistributionModel.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.*;
29 import dr.inferencexml.distribution.NormalDistributionModelParser;
30 import dr.math.MathUtils;
31 import dr.math.UnivariateFunction;
32 import dr.math.distributions.GaussianProcessRandomGenerator;
33 import dr.math.distributions.NormalDistribution;
34 import org.w3c.dom.Document;
35 import org.w3c.dom.Element;
36
37 /**
38 * A class that acts as a model for normally distributed data.
39 *
40 * @author Alexei Drummond
41 * @version $Id: NormalDistributionModel.java,v 1.6 2005/05/24 20:25:59 rambaut Exp $
42 */
43
44 public class NormalDistributionModel extends AbstractModel implements ParametricDistributionModel,
45 GaussianProcessRandomGenerator, GradientProvider {
46 /**
47 * Constructor.
48 */
49 public NormalDistributionModel(Variable<Double> mean, Variable<Double> scale) {
50
51 super(NormalDistributionModelParser.NORMAL_DISTRIBUTION_MODEL);
52
53 this.mean = mean;
54 this.scale = scale;
55 addVariable(mean);
56 mean.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 1));
57 addVariable(scale);
58 scale.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
59 }
60
61 public NormalDistributionModel(Parameter meanParameter, Parameter scale, boolean isPrecision) {
62 super(NormalDistributionModelParser.NORMAL_DISTRIBUTION_MODEL);
63 this.hasPrecision = isPrecision;
64 this.mean = meanParameter;
65 addVariable(meanParameter);
66 meanParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 1));
67 if (isPrecision) {
68 this.precision = scale;
69 this.scale = null; // todo why not keep the name scale to avoid confusion?? For whom?
70 } else {
71 this.scale = scale;
72 }
73 addVariable(scale);
74 scale.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
75 }
76
77 public double getStdev() {
78 return getScale();
79 }
80
81 public double getScale() {
82 if (hasPrecision)
83 return 1.0 / Math.sqrt(precision.getValue(0));
84 return scale.getValue(0);
85 }
86
87 public Variable<Double> getMean() {
88 return mean;
89 }
90
91 public Variable<Double> getPrecision() {
92 if (hasPrecision)
93 return precision;
94 return null;
95 }
96
97 // *****************************************************************
98 // Interface Distribution
99 // *****************************************************************
100
101 public double pdf(double x) {
102 return NormalDistribution.pdf(x, mean(), getScale());
103 }
104
105 public double logPdf(double x) {
106 return NormalDistribution.logPdf(x, mean(), getScale());
107 }
108
109 public double cdf(double x) {
110 return NormalDistribution.cdf(x, mean(), getScale());
111 }
112
113 public double quantile(double y) {
114 return NormalDistribution.quantile(y, mean(), getScale());
115 }
116
117 public double mean() {
118 return mean.getValue(0);
119 }
120
121 public double variance() {
122 if (hasPrecision)
123 return 1.0 / precision.getValue(0);
124 double sd = scale.getValue(0);
125 return sd * sd;
126 }
127
128 public final UnivariateFunction getProbabilityDensityFunction() {
129 return pdfFunction;
130 }
131
132 private final UnivariateFunction pdfFunction = new UnivariateFunction() {
133 public final double evaluate(double x) {
134 return pdf(x);
135 }
136
137 public final double getLowerBound() {
138 return Double.NEGATIVE_INFINITY;
139 }
140
141 public final double getUpperBound() {
142 return Double.POSITIVE_INFINITY;
143 }
144 };
145
146 // *****************************************************************
147 // Interface Model
148 // *****************************************************************
149
150 public void handleModelChangedEvent(Model model, Object object, int index) {
151 // no intermediates need to be recalculated...
152 }
153
154 protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
155 // no intermediates need to be recalculated...
156 }
157
158 protected void storeState() {
159 } // no additional state needs storing
160
161 protected void restoreState() {
162 } // no additional state needs restoring
163
164 protected void acceptState() {
165 } // no additional state needs accepting
166
167 public Element createElement(Document document) {
168 throw new RuntimeException("Not implemented!");
169 }
170
171 // **************************************************************
172 // Private instance variables
173 // **************************************************************
174
175 private final Variable<Double> mean;
176 private final Variable<Double> scale;
177 private Variable<Double> precision;
178 private boolean hasPrecision = false;
179
180 public Object nextRandom() {
181 double eps = MathUtils.nextGaussian();
182 eps *= getScale();
183 eps += mean();
184 return eps;
185 }
186
187 public double logPdf(Object x) {
188 double v = (Double) x;
189 return logPdf(v);
190 }
191
192 @Override
193 public Likelihood getLikelihood() {
194 return null;
195 }
196
197 @Override
198 public int getDimension() { return 1; }
199
200 @Override
201 public double[] getGradientLogDensity(Object x) {
202 double[] result = new double[1];
203 result[0] = NormalDistribution.gradLogPdf((Double) x, mean(), getScale());
204 return result;
205 }
206
207 @Override
208 public double[][] getPrecisionMatrix() {
209 double p = hasPrecision ?
210 precision.getValue(0) :
211 scale.getValue(0) * scale.getValue(0);
212 return new double[][]{{p}};
213 }
214
215 // *****************************************************************
216 // Interface DensityModel
217 // *****************************************************************
218
219 @Override
220 public double logPdf(double[] x) {
221 return logPdf(x[0]);
222 }
223
224 @Override
225 public Variable<Double> getLocationVariable() {
226 return mean;
227 }
228
229 }