Debian Codegraph @

src/dr/evomodel/continuous/TruncatedWorkingDistribution.java

raw symbols SCIP
1 /*
2 * TruncatedWorkingDistribution.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.evomodel.continuous;
27
28 import dr.inference.distribution.AbstractDistributionLikelihood;
29 import dr.inference.distribution.DistributionLikelihood;
30 import dr.inference.model.Statistic;
31 import dr.math.distributions.NormalDistribution;
32 import dr.xml.*;
33
34 /**
35 * @author Marc A. Suchard
36 */
37 public class TruncatedWorkingDistribution extends AbstractDistributionLikelihood {
38
39 public static final String MEAN = "mean";
40 public static final String STDEV = "stdev";
41 public static final String WORKING_PRIOR = "truncatedWorkingPrior";
42
43 private DistributionLikelihood baseDistribution;
44 private LatentTruncation truncation;
45
46 public TruncatedWorkingDistribution(DistributionLikelihood baseDistribution,
47 LatentTruncation truncation) {
48 super(baseDistribution.getModel());
49 this.baseDistribution = baseDistribution;
50 this.truncation = truncation;
51 }
52
53 @Override
54 public double calculateLogLikelihood() {
55 double logLikelihood = truncation.getLogLikelihood(); // Returns log(0) or log(1)
56 if (logLikelihood != Double.NEGATIVE_INFINITY) {
57 logLikelihood += baseDistribution.getLogLikelihood() +
58 truncation.getNormalizationConstant(baseDistribution.getDistribution());
59 }
60 return logLikelihood;
61 }
62
63 public static XMLObjectParser WORKING_PRIOR_PARSER = new AbstractXMLObjectParser() {
64
65 public String getParserName() {
66 return WORKING_PRIOR;
67 }
68
69 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
70
71 double mean = xo.getDoubleAttribute(MEAN);
72 double stdev = xo.getDoubleAttribute(STDEV);
73
74 DistributionLikelihood likelihood = new DistributionLikelihood(new NormalDistribution(mean, stdev));
75 for (int j = 0; j < xo.getChildCount(); j++) {
76 if (xo.getChild(j) instanceof Statistic) {
77 likelihood.addData((Statistic) xo.getChild(j));
78 } else if (!(xo.getChild(j) instanceof LatentTruncation)) {
79 throw new XMLParseException("Illegal element in " + xo.getName() + " element");
80 }
81 }
82
83 LatentTruncation truncation = (LatentTruncation) xo.getChild(LatentTruncation.class);
84
85 return new TruncatedWorkingDistribution(likelihood, truncation);
86 }
87
88 public XMLSyntaxRule[] getSyntaxRules() {
89 return rules;
90 }
91
92 private final XMLSyntaxRule[] rules = {
93 AttributeRule.newDoubleRule(MEAN),
94 AttributeRule.newDoubleRule(STDEV),
95 new ElementRule(Statistic.class, 1, Integer.MAX_VALUE),
96 new ElementRule(LatentTruncation.class),
97 };
98
99 public String getParserDescription() {
100 return "Calculates the prior probability of some data under a latent truncated normal distribution.";
101 }
102
103 public Class getReturnType() {
104 return TruncatedWorkingDistribution.class;
105 }
106 };
107 }