Debian Codegraph @

src/dr/inference/distribution/LogNormalDistributionModel.java

raw symbols SCIP
1 /*
2 * LogNormalDistributionModel.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.LogNormalDistributionModelParser;
33 import dr.math.UnivariateFunction;
34 import dr.math.distributions.NormalDistribution;
35 import org.w3c.dom.Document;
36 import org.w3c.dom.Element;
37
38 /**
39 * A class that acts as a model for log-normally distributed data.
40 *
41 * @author Alexei Drummond
42 * @author Andrew Rambaut
43 * @version $Id: LogNormalDistributionModel.java,v 1.8 2005/05/24 20:25:59 rambaut Exp $
44 */
45
46 public class LogNormalDistributionModel extends AbstractModel implements ParametricDistributionModel {
47
48 public enum Parameterization {
49 MU_SIGMA,
50 MU_PRECISION,
51 MEAN_STDEV
52 }
53
54 /**
55 * Constructor.
56 * This is the old constructor left for backwards compatibility
57 */
58 public LogNormalDistributionModel(Parameter meanParameter, Parameter stdevParameter, double offset, boolean parametersInRealSpace) {
59
60 super(LogNormalDistributionModelParser.LOGNORMAL_DISTRIBUTION_MODEL);
61
62 this.offset = offset;
63
64 if (parametersInRealSpace) {
65 this.meanParameter = meanParameter;
66 this.muParameter = null;
67 addVariable(this.meanParameter);
68 this.meanParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
69
70 this.stdevParameter = stdevParameter;
71 this.sigmaParameter = null;
72 addVariable(this.stdevParameter);
73 this.stdevParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
74
75 this.parameterization = Parameterization.MEAN_STDEV;
76 } else {
77 this.muParameter = meanParameter;
78 this.meanParameter = null;
79 addVariable(this.muParameter);
80 this.muParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 1));
81
82 this.sigmaParameter = stdevParameter;
83 this.stdevParameter = null;
84 addVariable(this.sigmaParameter);
85 this.sigmaParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 1));
86
87 this.parameterization = Parameterization.MU_SIGMA;
88 }
89
90 precisionParameter = null;
91 }
92
93 /**
94 * Constructor that allows different parameterizations. Generally we would expect either mu and sigma or mean and
95 * stdev to be provided (the others should be null). But the only restriction is one of mean or mu and one of stdev
96 * and sigma.
97 * @param parameterization
98 * @param parameter1
99 * @param parameter2
100 * @param offset
101 */
102 public LogNormalDistributionModel(Parameterization parameterization, Parameter parameter1, Parameter parameter2, double offset) {
103
104 super(LogNormalDistributionModelParser.LOGNORMAL_DISTRIBUTION_MODEL);
105
106 switch (parameterization) {
107 case MU_SIGMA:
108 this.muParameter = parameter1;
109 this.sigmaParameter = parameter2;
110 this.meanParameter = null;
111 this.stdevParameter = null;
112 this.precisionParameter = null;
113 this.parameterization = Parameterization.MU_SIGMA;
114 break;
115 case MU_PRECISION:
116 this.muParameter = parameter1;
117 this.precisionParameter = parameter2;
118 this.meanParameter = null;
119 this.stdevParameter = null;
120 this.sigmaParameter = null;
121 this.parameterization = Parameterization.MU_PRECISION;
122 break;
123 case MEAN_STDEV:
124 this.meanParameter = parameter1;
125 this.stdevParameter = parameter2;
126 this.muParameter = null;
127 this.sigmaParameter = null;
128 this.precisionParameter = null;
129 this.parameterization = Parameterization.MEAN_STDEV;
130 break;
131 default:
132 throw new IllegalArgumentException("Unknow parameterization type");
133 }
134
135
136 this.offset = offset;
137
138 if (this.muParameter != null) {
139 addVariable(this.muParameter);
140 this.muParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 1));
141 }
142
143 if (meanParameter != null) {
144 addVariable(this.meanParameter);
145 this.meanParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
146 }
147
148 if (sigmaParameter != null) {
149 addVariable(this.sigmaParameter);
150 this.sigmaParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
151 }
152
153 if (stdevParameter != null) {
154 addVariable(this.stdevParameter);
155 this.stdevParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
156 }
157
158 if (precisionParameter != null) {
159 addVariable(this.precisionParameter);
160 this.precisionParameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
161 }
162 }
163
164 public Parameter getMeanParameter() {
165 return meanParameter;
166 }
167
168 public Parameter getStdevParameter() {
169 return stdevParameter;
170 }
171
172 public Parameter getMuParameter() {
173 return muParameter;
174 }
175
176 public Parameter getSigmaParameter() {
177 return sigmaParameter;
178 }
179
180 public Parameter getPrecisionParameter() {
181 return precisionParameter;
182 }
183
184 public Parameterization getParameterization() {
185 return parameterization;
186 }
187
188 public final double getMu() {
189 if (muParameter != null) {
190 return muParameter.getValue(0);
191 } else {
192 return calculateMu(meanParameter.getValue(0), stdevParameter.getValue(0));
193 }
194 }
195
196 public final double getMean() {
197 if (meanParameter != null) {
198 return meanParameter.getValue(0);
199 } else {
200 return calculateMean(muParameter.getValue(0), getSigma());
201 }
202 }
203
204 public final double getSigma() {
205 if (sigmaParameter != null) {
206 return sigmaParameter.getValue(0);
207 } else if (precisionParameter != null) {
208 return Math.sqrt(1.0 / precisionParameter.getValue(0));
209 } else{
210 return calculateSigma(meanParameter.getValue(0), stdevParameter.getValue(0));
211 }
212
213 }
214
215 public final double getStdev() {
216 if (stdevParameter != null) {
217 return stdevParameter.getValue(0);
218 } else if (precisionParameter != null) {
219 double sigma = Math.sqrt(1.0 / precisionParameter.getValue(0));
220 return calculateStdev(muParameter.getValue(0), sigma);
221 } else {
222 return calculateStdev(muParameter.getValue(0), sigmaParameter.getValue(0));
223 }
224 }
225
226 public final double getPrecision() {
227 if (precisionParameter != null) {
228 return precisionParameter.getValue(0);
229 } else {
230 double sigma = getSigma();
231 return 1.0 / (sigma * sigma);
232 }
233 }
234
235 private double calculateMu(double mean, double stdev) {
236 return Math.log(mean/Math.sqrt(1 + (stdev * stdev) / (mean * mean)));
237 }
238
239 private double calculateSigma(double mean, double stdev) {
240 return Math.sqrt(Math.log(1 + (stdev * stdev) / (mean * mean)));
241 }
242
243 private double calculateMean(double mu, double sigma) {
244 return Math.exp(mu + 0.5 * sigma * sigma);
245 }
246
247 private double calculateStdev(double mu, double sigma) {
248 return Math.sqrt( (Math.exp(sigma * sigma)-1) * Math.exp(2.0 * mu + (sigma * sigma)) );
249 }
250
251 // public final double getS() {
252 // //System.out.println(isStdevInRealSpace+"\t" + isMeanInRealSpace + "\t" + Math.sqrt(Math.log(1 + scaleParameter.getParameterValue(0)/Math.pow(meanParameter.getParameterValue(0), 2))) + "\t" + scaleParameter.getParameterValue(0));
253 // if(isStdevInRealSpace) {
254 //
255 // if(isMeanInRealSpace) {
256 // return Math.sqrt(Math.log(1 + scaleParameter.getParameterValue(0)/Math.pow(meanParameter.getParameterValue(0), 2)));
257 // }
258 // else {
259 // throw new RuntimeException("S can not be computed with M and stdev");
260 // }
261 // }
262 // return scaleParameter.getParameterValue(0);
263 // }
264 //
265 // public final void setS(double S) {
266 // scaleParameter.setParameterValue(0, S);
267 // }
268 //
269 // public final Parameter getSParameter() {
270 // return scaleParameter;
271 // }
272 //
273 // /* StDev in this class is actually incorrectly named the S parameter */
274 // private double getStDev() {
275 // return usesStDev ? getS() : Math.sqrt(1.0 / getS());
276 // }
277 //
278 // /**
279 // * @return the mean (always in log space)
280 // */
281 // public final double getM() {
282 // if (isMeanInRealSpace) {
283 // double stDev = getStDev();
284 // return Math.log(meanParameter.getParameterValue(0)) - (0.5 * stDev * stDev);
285 // } else {
286 // return meanParameter.getParameterValue(0);
287 //
288 // }
289 // }
290 //
291 // public final void setM(double M) {
292 // if (isMeanInRealSpace) {
293 // double stDev = getStDev();
294 // meanParameter.setParameterValue(0, Math.exp(M + (0.5 * stDev * stDev)));
295 // } else {
296 // meanParameter.setParameterValue(0, M);
297 // }
298 // }
299 //
300 // public final Parameter getMeanParameter() {
301 // return meanParameter;
302 // }
303 //
304 // public Parameter getPrecisionParameter() {
305 // if (!usesStDev)
306 // return scaleParameter;
307 // return null;
308 // }
309
310 // *****************************************************************
311 // Interface Distribution
312 // *****************************************************************
313
314 public double pdf(double x) {
315 if (x - offset <= 0.0) return 0.0;
316 return NormalDistribution.pdf(Math.log(x - offset), getMu(), getSigma()) / (x - offset);
317 }
318
319
320 public double logPdf(double x) {
321 if (x - offset <= 0.0) return Double.NEGATIVE_INFINITY;
322 return NormalDistribution.logPdf(Math.log(x - offset), getMu(), getSigma()) - Math.log(x - offset);
323 }
324
325 public double cdf(double x) {
326 if (x - offset <= 0.0) return 0.0;
327 return NormalDistribution.cdf(Math.log(x - offset), getMu(), getSigma());
328 }
329
330 public double quantile(double y) {
331 return Math.exp(NormalDistribution.quantile(y, getMu(), getSigma())) + offset;
332 }
333
334 /**
335 * @return the mean of the distribution
336 */
337 public double mean() {
338 return getMean();
339 }
340
341 /**
342 * @return the variance of the log normal distribution. Not really the variance of the lognormal but the S^2
343 * parameter
344 */
345 public double variance() {
346 return getStdev() * getStdev();
347 }
348
349 public final UnivariateFunction getProbabilityDensityFunction() {
350 return pdfFunction;
351 }
352
353 private final UnivariateFunction pdfFunction = new UnivariateFunction() {
354 public final double evaluate(double x) {
355 return pdf(Math.log(x));
356 }
357
358 public final double getLowerBound() {
359 return Double.NEGATIVE_INFINITY;
360 }
361
362 public final double getUpperBound() {
363 return Double.POSITIVE_INFINITY;
364 }
365 };
366
367 // *****************************************************************
368 // Interface DensityModel
369 // *****************************************************************
370
371 @Override
372 public double logPdf(double[] x) {
373 return logPdf(x[0]);
374 }
375
376 @Override
377 public Variable<Double> getLocationVariable() {
378 return meanParameter;
379 }
380
381 // *****************************************************************
382 // Interface Model
383 // *****************************************************************
384
385 public void handleModelChangedEvent(Model model, Object object, int index) {
386 // no intermediates need to be recalculated...
387 }
388
389 public void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
390 // no intermediates need to be recalculated...
391 }
392
393 protected void storeState() {
394 } // no additional state needs storing
395
396 protected void restoreState() {
397 } // no additional state needs restoring
398
399 protected void acceptState() {
400 } // no additional state needs accepting
401
402 // **************************************************************
403 // XMLElement IMPLEMENTATION
404 // **************************************************************
405
406 public Element createElement(Document document) {
407 throw new RuntimeException("Not implemented!");
408 }
409
410 // **************************************************************
411 // Private instance variables
412 // **************************************************************
413
414 // Can be parameterized as the mean and stdev of the lognormal or the mean and stdev of the underlying normal (mu & sigma)
415 private final Parameter meanParameter;
416 private final Parameter stdevParameter;
417 private final Parameter muParameter;
418 private final Parameter sigmaParameter;
419 private final Parameter precisionParameter;
420 private final double offset;
421
422 private Parameterization parameterization;
423
424 public static void main(String[] argv) {
425 Parameter meanParameter = new Parameter.Default(1.0);
426 Parameter stdevParameter = new Parameter.Default(5.0);
427 Parameter muParameter = new Parameter.Default(-1.629048);
428 Parameter sigmaParameter = new Parameter.Default(1.80502);
429
430 LogNormalDistributionModel ln1 = new LogNormalDistributionModel(Parameterization.MEAN_STDEV, meanParameter, stdevParameter, 0);
431 System.out.println("Lognormal mean = 1.0, stdev = 5.0");
432 System.out.println(" mu = " + ln1.getMu() + " (correct = -1.629048)");
433 System.out.println(" sigma = " + ln1.getSigma() + " (correct = 1.80502)");
434 System.out.println(" quantile(2.5) = " + ln1.quantile(0.025) + " (correct = 0.005702663)");
435 System.out.println(" quantile(97.5) = " + ln1.quantile(0.975) + " (correct = 6.744487892)");
436
437 LogNormalDistributionModel ln2 = new LogNormalDistributionModel(Parameterization.MU_SIGMA, muParameter, sigmaParameter, 0);
438 System.out.println("Lognormal mu = -1.629048, sigma = 1.80502");
439 System.out.println(" mean = " + ln2.getMean() + " (correct = 1.0)");
440 System.out.println(" sigma = " + ln2.getStdev() + " (correct = 5.0)");
441 System.out.println(" quantile(2.5) = " + ln2.quantile(0.025) + " (correct = 0.005702663)");
442 System.out.println(" quantile(97.5) = " + ln2.quantile(0.975) + " (correct = 6.744487892)");
443
444 meanParameter = new Parameter.Default(0.001);
445 stdevParameter = new Parameter.Default(0.0005);
446 LogNormalDistributionModel ln3 = new LogNormalDistributionModel(Parameterization.MEAN_STDEV, meanParameter, stdevParameter, 0);
447 System.out.println("Lognormal mean = 0.001, stdev = 0.0005");
448 System.out.println(" mu = " + ln3.getMu());
449 System.out.println(" sigma = " + ln3.getSigma());
450 for (int i = 1; i <= 12; i++) {
451 double y = ((double)i) / 13.0;
452 System.out.println(i + "\t" + y + "\t" + ln3.quantile(y));
453 }
454
455
456 }
457 }