| 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 |
|
| 29 |
import dr.inference.model.Model; |
| 30 |
import dr.inference.model.Variable; |
| 31 |
import dr.inference.model.Variable.ChangeType; |
| 32 |
import dr.inferencexml.distribution.PriorParsers; |
| 33 |
import dr.math.MathUtils; |
| 34 |
import dr.math.Poisson; |
| 35 |
import dr.math.distributions.PoissonDistribution; |
| 36 |
import dr.xml.*; |
| 37 |
import jebl.math.Binomial; |
| 38 |
|
| 39 |
public class PoissonPartitionLikelihood extends ARGPartitionLikelihood { |
| 40 |
|
| 41 |
public static final String POISSON_PARTITION_LIKELIHOOD = "poissonPartitionLikelihood"; |
| 42 |
|
| 43 |
PoissonDistribution pd; |
| 44 |
double mean; |
| 45 |
|
| 46 |
public PoissonPartitionLikelihood(String id, ARGModel arg, double mean) { |
| 47 |
super(id, arg); |
| 48 |
|
| 49 |
pd = new PoissonDistribution(mean); |
| 50 |
this.mean = mean; |
| 51 |
} |
| 52 |
|
| 53 |
public double getLogLikelihood(double[] partition) { |
| 54 |
if ((getNumberOfPartitionsMinusOne() + 1) % 2 == 0) { |
| 55 |
return getEvenLogLikelihood(partition); |
| 56 |
} |
| 57 |
|
| 58 |
return getOddLogLikelihood(partition); |
| 59 |
|
| 60 |
} |
| 61 |
|
| 62 |
private double getEvenLogLikelihood(double[] partition) { |
| 63 |
int numberOfZeros = 0; |
| 64 |
int numberOfOnes = 0; |
| 65 |
|
| 66 |
for (double d : partition) { |
| 67 |
assert d == 0.0 || d == 1.0; |
| 68 |
|
| 69 |
if (d == 0.0) |
| 70 |
numberOfZeros++; |
| 71 |
else |
| 72 |
numberOfOnes++; |
| 73 |
} |
| 74 |
|
| 75 |
double poissonValue = (double) Math.min(numberOfZeros, numberOfOnes); |
| 76 |
|
| 77 |
double logLike = pd.logPdf(poissonValue); |
| 78 |
|
| 79 |
if (poissonValue < partition.length / 2) { |
| 80 |
return logLike - Math.log(Binomial.choose(partition.length, poissonValue)); |
| 81 |
} else { |
| 82 |
return logLike - Math.log(Binomial.choose(partition.length - 1, poissonValue)); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
private double getOddLogLikelihood(double[] partition) { |
| 87 |
int numberOfZeros = 0; |
| 88 |
int numberOfOnes = 0; |
| 89 |
|
| 90 |
for (double d : partition) { |
| 91 |
assert d == 0.0 || d == 1.0; |
| 92 |
|
| 93 |
if (d == 0.0) |
| 94 |
numberOfZeros++; |
| 95 |
else |
| 96 |
numberOfOnes++; |
| 97 |
|
| 98 |
} |
| 99 |
|
| 100 |
double poissonValue = (double) Math.min(numberOfZeros, numberOfOnes); |
| 101 |
|
| 102 |
return pd.logPdf(poissonValue) - |
| 103 |
Math.log(Binomial.choose(partition.length, poissonValue)); |
| 104 |
} |
| 105 |
|
| 106 |
public double[] generatePartition() { |
| 107 |
int lengthDividedByTwo = (getNumberOfPartitionsMinusOne() + 1) / 2; |
| 108 |
|
| 109 |
int value = 0; |
| 110 |
|
| 111 |
while (value < 1 || value > lengthDividedByTwo) { |
| 112 |
value = Poisson.nextPoisson(mean); |
| 113 |
} |
| 114 |
|
| 115 |
int[] x = new int[getNumberOfPartitionsMinusOne() + 1]; |
| 116 |
|
| 117 |
for (int i = 0; i < value; i++) { |
| 118 |
x[i] = 1; |
| 119 |
} |
| 120 |
|
| 121 |
MathUtils.permute(x); |
| 122 |
|
| 123 |
if (x[0] == 1) { |
| 124 |
for (int i = 0; i < x.length; i++) { |
| 125 |
if (x[i] == 1) { |
| 126 |
x[i] = 0; |
| 127 |
} else { |
| 128 |
x[i] = 1; |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
double[] rValue = new double[x.length]; |
| 134 |
|
| 135 |
for (int i = 0; i < rValue.length; i++) { |
| 136 |
rValue[i] = x[i]; |
| 137 |
} |
| 138 |
|
| 139 |
return rValue; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 144 |
|
| 145 |
public String getParserDescription() { |
| 146 |
return null; |
| 147 |
} |
| 148 |
|
| 149 |
public Class getReturnType() { |
| 150 |
return PoissonPartitionLikelihood.class; |
| 151 |
} |
| 152 |
|
| 153 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 154 |
return new XMLSyntaxRule[]{ |
| 155 |
AttributeRule.newDoubleRule(PriorParsers.MEAN, false), |
| 156 |
new ElementRule(ARGModel.class, false), |
| 157 |
}; |
| 158 |
} |
| 159 |
|
| 160 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 161 |
String id = ""; |
| 162 |
if (xo.hasId()) |
| 163 |
id = xo.getId(); |
| 164 |
|
| 165 |
double mean = xo.getDoubleAttribute(PriorParsers.MEAN); |
| 166 |
ARGModel arg = (ARGModel) xo.getChild(ARGModel.class); |
| 167 |
|
| 168 |
if (arg.isRecombinationPartitionType()) { |
| 169 |
throw new XMLParseException(ARGModel.TREE_MODEL + " must be of type " + ARGModel.REASSORTMENT_PARTITION); |
| 170 |
} |
| 171 |
|
| 172 |
return new PoissonPartitionLikelihood(id, arg, mean); |
| 173 |
} |
| 174 |
|
| 175 |
public String getParserName() { |
| 176 |
return POISSON_PARTITION_LIKELIHOOD; |
| 177 |
} |
| 178 |
|
| 179 |
}; |
| 180 |
|
| 181 |
protected void acceptState() { |
| 182 |
|
| 183 |
} |
| 184 |
|
| 185 |
@Override |
| 186 |
protected void handleModelChangedEvent(Model model, Object object, int index) { |
| 187 |
|
| 188 |
} |
| 189 |
|
| 190 |
@Override |
| 191 |
protected void handleVariableChangedEvent(Variable variable, int index, |
| 192 |
ChangeType type) { |
| 193 |
|
| 194 |
} |
| 195 |
|
| 196 |
@Override |
| 197 |
protected void restoreState() { |
| 198 |
|
| 199 |
} |
| 200 |
|
| 201 |
@Override |
| 202 |
protected void storeState() { |
| 203 |
|
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
} |