| 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.Likelihood; |
| 29 |
import dr.inference.model.Model; |
| 30 |
import dr.inference.model.Variable; |
| 31 |
import dr.inference.model.Variable.ChangeType; |
| 32 |
import dr.math.MathUtils; |
| 33 |
import dr.xml.*; |
| 34 |
|
| 35 |
|
| 36 |
public class UniformPartitionLikelihood extends ARGPartitionLikelihood implements Likelihood { |
| 37 |
|
| 38 |
public static final String UNIFORM_PARTITION_LIKELIHOOD = "uniformPartitionLikelihood"; |
| 39 |
private double logStateCount; |
| 40 |
private boolean isRecombination; |
| 41 |
|
| 42 |
|
| 43 |
public UniformPartitionLikelihood(String id, ARGModel arg) { |
| 44 |
super(id, arg); |
| 45 |
|
| 46 |
isRecombination = arg.isRecombinationPartitionType(); |
| 47 |
|
| 48 |
int numberOfPartitionsMinusOne = getNumberOfPartitionsMinusOne(); |
| 49 |
|
| 50 |
if (arg.isRecombinationPartitionType()) { |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
logStateCount = Math.log(numberOfPartitionsMinusOne); |
| 58 |
} else { |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
double rValue = Math.pow(2.0, numberOfPartitionsMinusOne) - 1; |
| 66 |
|
| 67 |
logStateCount = Math.log(rValue); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
public double[] generatePartition() { |
| 72 |
if (isRecombination) { |
| 73 |
return generateRecombinationPartition(); |
| 74 |
} |
| 75 |
return generateReassortmentPartition(); |
| 76 |
} |
| 77 |
|
| 78 |
private double[] generateRecombinationPartition() { |
| 79 |
int numberOfPartitionsMinusOne = getNumberOfPartitionsMinusOne(); |
| 80 |
|
| 81 |
int cut = MathUtils.nextInt(numberOfPartitionsMinusOne); |
| 82 |
|
| 83 |
int leftValue = 0; |
| 84 |
int rightValue = 1; |
| 85 |
|
| 86 |
double[] partition = new double[numberOfPartitionsMinusOne + 1]; |
| 87 |
|
| 88 |
for (int i = 0; i < cut + 1; i++) |
| 89 |
partition[i] = leftValue; |
| 90 |
for (int i = cut + 1; i < partition.length; i++) |
| 91 |
partition[i] = rightValue; |
| 92 |
|
| 93 |
return partition; |
| 94 |
} |
| 95 |
|
| 96 |
public static double arraySum(double[] x) { |
| 97 |
double a = 0; |
| 98 |
for (double b : x) |
| 99 |
a += b; |
| 100 |
return a; |
| 101 |
} |
| 102 |
|
| 103 |
private double[] generateReassortmentPartition() { |
| 104 |
int numberOfPartitions = getNumberOfPartitionsMinusOne() + 1; |
| 105 |
|
| 106 |
double[] partition = new double[numberOfPartitions]; |
| 107 |
|
| 108 |
while (arraySum(partition) == 0) { |
| 109 |
for (int i = 1; i < partition.length; i++) { |
| 110 |
if (MathUtils.nextBoolean()) { |
| 111 |
partition[i] = 1.0; |
| 112 |
} else { |
| 113 |
partition[i] = 0.0; |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
partition[0] = 0.0; |
| 119 |
|
| 120 |
return partition; |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
public double getLogLikelihood(double[] partition) { |
| 125 |
return -logStateCount; |
| 126 |
} |
| 127 |
|
| 128 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 129 |
|
| 130 |
public String getParserDescription() { |
| 131 |
return null; |
| 132 |
} |
| 133 |
|
| 134 |
public Class getReturnType() { |
| 135 |
return UniformPartitionLikelihood.class; |
| 136 |
} |
| 137 |
|
| 138 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 139 |
return new XMLSyntaxRule[]{ |
| 140 |
new ElementRule(ARGModel.class, false), |
| 141 |
}; |
| 142 |
} |
| 143 |
|
| 144 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 145 |
ARGModel arg = (ARGModel) xo.getChild(ARGModel.class); |
| 146 |
|
| 147 |
String id = ""; |
| 148 |
if (xo.hasId()) |
| 149 |
id = xo.getId(); |
| 150 |
|
| 151 |
return new UniformPartitionLikelihood(id, arg); |
| 152 |
} |
| 153 |
|
| 154 |
public String getParserName() { |
| 155 |
return UNIFORM_PARTITION_LIKELIHOOD; |
| 156 |
} |
| 157 |
|
| 158 |
}; |
| 159 |
|
| 160 |
protected void acceptState() { |
| 161 |
|
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
protected void handleModelChangedEvent(Model model, Object object, int index) { |
| 166 |
|
| 167 |
} |
| 168 |
|
| 169 |
protected void handleVariableChangedEvent(Variable variable, int index, |
| 170 |
ChangeType type) { |
| 171 |
|
| 172 |
} |
| 173 |
|
| 174 |
protected void restoreState() { |
| 175 |
|
| 176 |
} |
| 177 |
|
| 178 |
protected void storeState() { |
| 179 |
|
| 180 |
} |
| 181 |
|
| 182 |
} |