| 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.arg; |
| 27 |
|
| 28 |
import dr.inference.model.Model; |
| 29 |
import dr.inference.model.Parameter; |
| 30 |
import dr.inference.model.Variable; |
| 31 |
import dr.inference.model.Variable.ChangeType; |
| 32 |
import dr.math.MathUtils; |
| 33 |
import dr.xml.*; |
| 34 |
|
| 35 |
public class HierarchicalPartitionLikelihood extends ARGPartitionLikelihood { |
| 36 |
|
| 37 |
public static final String HIERARCHICAL_PARTITION_LIKELIHOOD = "hierarchicalPartitionLikelihood"; |
| 38 |
|
| 39 |
private Parameter probabilities; |
| 40 |
|
| 41 |
public HierarchicalPartitionLikelihood(String id, ARGModel arg, Parameter probs) { |
| 42 |
super(id, arg); |
| 43 |
|
| 44 |
this.probabilities = probs; |
| 45 |
|
| 46 |
addVariable(probs); |
| 47 |
addModel(arg); |
| 48 |
} |
| 49 |
|
| 50 |
public double[] generatePartition() { |
| 51 |
double[] partition = new double[getNumberOfPartitionsMinusOne() + 1]; |
| 52 |
|
| 53 |
partition[0] = 0.0; |
| 54 |
|
| 55 |
for (int i = 0; i < partition.length; i++) |
| 56 |
partition[i] = 0.0; |
| 57 |
|
| 58 |
while (UniformPartitionLikelihood.arraySum(partition) == 0.0) { |
| 59 |
for (int i = 1; i < partition.length; i++) { |
| 60 |
if (MathUtils.nextDouble() < probabilities.getParameterValue(i - 1)) { |
| 61 |
partition[i] = 1.0; |
| 62 |
} else { |
| 63 |
partition[i] = 0.0; |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
return partition; |
| 70 |
} |
| 71 |
|
| 72 |
public double getLogLikelihood(double[] partition) { |
| 73 |
double logLike = 0; |
| 74 |
|
| 75 |
for (int i = 1; i < partition.length; i++) { |
| 76 |
if (partition[i] == 1.0) { |
| 77 |
logLike += Math.log(probabilities.getParameterValue(i - 1)); |
| 78 |
} else { |
| 79 |
logLike += Math.log(1 - probabilities.getParameterValue(i - 1)); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
return logLike; |
| 86 |
} |
| 87 |
|
| 88 |
protected void acceptState() { |
| 89 |
|
| 90 |
} |
| 91 |
|
| 92 |
protected void handleModelChangedEvent(Model model, Object object, int index) { |
| 93 |
|
| 94 |
|
| 95 |
} |
| 96 |
|
| 97 |
protected void handleVariableChangedEvent(Variable variable, int index, |
| 98 |
ChangeType type) { |
| 99 |
|
| 100 |
} |
| 101 |
|
| 102 |
protected void restoreState() { |
| 103 |
|
| 104 |
} |
| 105 |
|
| 106 |
@Override |
| 107 |
protected void storeState() { |
| 108 |
|
| 109 |
|
| 110 |
} |
| 111 |
|
| 112 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 113 |
|
| 114 |
public String getParserDescription() { |
| 115 |
return null; |
| 116 |
} |
| 117 |
|
| 118 |
public Class getReturnType() { |
| 119 |
return PoissonPartitionLikelihood.class; |
| 120 |
} |
| 121 |
|
| 122 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 123 |
return new XMLSyntaxRule[]{ |
| 124 |
|
| 125 |
new ElementRule(ARGModel.class, false), |
| 126 |
}; |
| 127 |
} |
| 128 |
|
| 129 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 130 |
String id = ""; |
| 131 |
if (xo.hasId()) |
| 132 |
id = xo.getId(); |
| 133 |
|
| 134 |
ARGModel arg = (ARGModel) xo.getChild(ARGModel.class); |
| 135 |
|
| 136 |
Parameter values = (Parameter) xo.getChild(Parameter.class); |
| 137 |
|
| 138 |
if (values.getDimension() != arg.getNumberOfPartitions() - 1) { |
| 139 |
throw new XMLParseException("The dimension of the parameter must equal the number of partitions minus 1 "); |
| 140 |
} |
| 141 |
|
| 142 |
if (arg.isRecombinationPartitionType()) { |
| 143 |
throw new XMLParseException(ARGModel.TREE_MODEL + " must be of type " + ARGModel.REASSORTMENT_PARTITION); |
| 144 |
} |
| 145 |
|
| 146 |
return new HierarchicalPartitionLikelihood(id, arg, values); |
| 147 |
} |
| 148 |
|
| 149 |
public String getParserName() { |
| 150 |
return HIERARCHICAL_PARTITION_LIKELIHOOD; |
| 151 |
} |
| 152 |
|
| 153 |
}; |
| 154 |
|
| 155 |
} |