| 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.inferencexml.model; |
| 27 |
|
| 28 |
import dr.inference.model.Likelihood; |
| 29 |
import dr.inference.model.ThreadedCompoundLikelihood; |
| 30 |
import dr.xml.*; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public class ThreadedCompoundLikelihoodParser extends AbstractXMLObjectParser { |
| 36 |
|
| 37 |
public static final String THREADED_COMPOUND_LIKELIHOOD = "threadedCompoundLikelihood"; |
| 38 |
public static final String WEIGHT = "robustWeight"; |
| 39 |
|
| 40 |
public String getParserName() { |
| 41 |
return THREADED_COMPOUND_LIKELIHOOD; |
| 42 |
} |
| 43 |
|
| 44 |
public String[] getParserNames() { |
| 45 |
return new String[]{getParserName()}; |
| 46 |
} |
| 47 |
|
| 48 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 49 |
|
| 50 |
ThreadedCompoundLikelihood compoundLikelihood = new ThreadedCompoundLikelihood(); |
| 51 |
|
| 52 |
for (int i = 0; i < xo.getChildCount(); i++) { |
| 53 |
if (xo.getChild(i) instanceof Likelihood) { |
| 54 |
compoundLikelihood.addLikelihood((Likelihood) xo.getChild(i)); |
| 55 |
} else { |
| 56 |
|
| 57 |
Object rogueElement = xo.getChild(i); |
| 58 |
|
| 59 |
throw new XMLParseException("An element (" + rogueElement + ") which is not a likelihood has been added to a " + THREADED_COMPOUND_LIKELIHOOD + " element"); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
double weight = xo.getAttribute(WEIGHT, 0.0); |
| 64 |
if (weight < 0) |
| 65 |
throw new XMLParseException("Robust weight must be non-negative."); |
| 66 |
compoundLikelihood.setWeightFactor(Math.exp(-weight)); |
| 67 |
|
| 68 |
return compoundLikelihood; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public String getParserDescription() { |
| 76 |
return "A likelihood function which is simply the product of its component likelihood functions."; |
| 77 |
} |
| 78 |
|
| 79 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 80 |
return rules; |
| 81 |
} |
| 82 |
|
| 83 |
private final XMLSyntaxRule[] rules = { |
| 84 |
new ElementRule(Likelihood.class, 1, Integer.MAX_VALUE), |
| 85 |
AttributeRule.newDoubleRule(WEIGHT, true), |
| 86 |
}; |
| 87 |
|
| 88 |
public Class getReturnType() { |
| 89 |
return ThreadedCompoundLikelihood.class; |
| 90 |
} |
| 91 |
} |