| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 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(); |
| 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 |
} |