| 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.inference.distribution; |
| 27 |
|
| 28 |
import dr.evomodel.continuous.FullyConjugateMultivariateTraitLikelihood; |
| 29 |
import dr.evomodel.continuous.TreeTraitNormalDistributionModel; |
| 30 |
import dr.inference.model.*; |
| 31 |
import dr.inferencexml.distribution.DistributionLikelihoodParser; |
| 32 |
import dr.math.distributions.*; |
| 33 |
import dr.util.Attribute; |
| 34 |
import dr.util.Citable; |
| 35 |
import dr.util.Transform; |
| 36 |
import dr.xml.*; |
| 37 |
|
| 38 |
import java.util.logging.Logger; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public class MultivariateDistributionLikelihood extends AbstractDistributionLikelihood { |
| 46 |
|
| 47 |
public static final String MVN_PRIOR = "multivariateNormalPrior"; |
| 48 |
public static final String MVN_MEAN = "meanParameter"; |
| 49 |
public static final String MVN_PRECISION = "precisionParameter"; |
| 50 |
public static final String MVN_CV = "coefficientOfVariation"; |
| 51 |
public static final String WISHART_PRIOR = "multivariateWishartPrior"; |
| 52 |
public static final String INV_WISHART_PRIOR = "multivariateInverseWishartPrior"; |
| 53 |
public static final String DIRICHLET_PRIOR = "dirichletParameterPrior"; |
| 54 |
public static final String SUM_TO_NUMBER_OF_ELEMENTS = "sumToNumberOfElements"; |
| 55 |
public static final String DF = "df"; |
| 56 |
public static final String SCALE_MATRIX = "scaleMatrix"; |
| 57 |
public static final String MVGAMMA_PRIOR = "multivariateGammaPrior"; |
| 58 |
public static final String MVGAMMA_SHAPE = "shapeParameter"; |
| 59 |
public static final String MVGAMMA_SCALE = "scaleParameter"; |
| 60 |
public static final String COUNTS = "countsParameter"; |
| 61 |
public static final String NON_INFORMATIVE = "nonInformative"; |
| 62 |
public static final String MULTIVARIATE_LIKELIHOOD = "multivariateDistributionLikelihood"; |
| 63 |
public static final String DATA_AS_MATRIX = "dataAsMatrix"; |
| 64 |
|
| 65 |
public static final String TREE_TRAIT = "treeTraitNormalDistributionLikelihood"; |
| 66 |
public static final String TREE_TRAIT_NORMAL = "treeTraitNormalDistribution"; |
| 67 |
public static final String ROOT_VALUE = "rootValue"; |
| 68 |
public static final String CONDITION = "conditionOnRoot"; |
| 69 |
|
| 70 |
public static final String DATA = "data"; |
| 71 |
|
| 72 |
private final MultivariateDistribution distribution; |
| 73 |
private final Transform[] transforms; |
| 74 |
private Parameter parameter = null; |
| 75 |
|
| 76 |
public MultivariateDistributionLikelihood(String name, ParametricMultivariateDistributionModel model) { |
| 77 |
this(name, model, null); |
| 78 |
} |
| 79 |
|
| 80 |
public MultivariateDistributionLikelihood(String name, ParametricMultivariateDistributionModel model, |
| 81 |
Transform[] transforms) { |
| 82 |
super(model); |
| 83 |
this.distribution = model; |
| 84 |
this.transforms = transforms; |
| 85 |
} |
| 86 |
|
| 87 |
public MultivariateDistributionLikelihood(String name, MultivariateDistribution distribution) { |
| 88 |
this(name, distribution, null); |
| 89 |
} |
| 90 |
|
| 91 |
public MultivariateDistributionLikelihood(String name, MultivariateDistribution distribution, |
| 92 |
Transform[] transforms) { |
| 93 |
super(new DefaultModel(name)); |
| 94 |
this.distribution = distribution; |
| 95 |
this.transforms = transforms; |
| 96 |
} |
| 97 |
|
| 98 |
public MultivariateDistributionLikelihood(MultivariateDistribution distribution) { |
| 99 |
this(distribution, null); |
| 100 |
} |
| 101 |
|
| 102 |
public MultivariateDistributionLikelihood(MultivariateDistribution distribution, Transform[] transforms) { |
| 103 |
this(distribution.getType(), distribution, transforms); |
| 104 |
} |
| 105 |
|
| 106 |
public String toString() { |
| 107 |
return getClass().getName() + "(" + getLogLikelihood() + ")"; |
| 108 |
} |
| 109 |
|
| 110 |
public double calculateLogLikelihood() { |
| 111 |
double logL = 0.0; |
| 112 |
|
| 113 |
for (Attribute<double[]> data : dataList) { |
| 114 |
double[] x = |
| 115 |
|
| 116 |
|
| 117 |
data.getAttributeValue(); |
| 118 |
if (transforms != null) { |
| 119 |
double[] y = new double[x.length]; |
| 120 |
for (int i = 0; i < x.length; ++i) { |
| 121 |
logL += transforms[i].getLogJacobian(x[i]); |
| 122 |
y[i] = transforms[i].transform(x[i]); |
| 123 |
} |
| 124 |
logL += distribution.logPdf(y); |
| 125 |
} else { |
| 126 |
logL += distribution.logPdf(x); |
| 127 |
} |
| 128 |
} |
| 129 |
return logL; |
| 130 |
} |
| 131 |
|
| 132 |
public void addData(Parameter parameter) { |
| 133 |
this.parameter = parameter; |
| 134 |
|
| 135 |
addData((Attribute<double[]>)parameter); |
| 136 |
} |
| 137 |
|
| 138 |
public Parameter getDataParameter() { |
| 139 |
return parameter; |
| 140 |
} |
| 141 |
|
| 142 |
@Override |
| 143 |
public void addData(Attribute<double[]> data) { |
| 144 |
super.addData(data); |
| 145 |
|
| 146 |
if (data instanceof Variable && getModel() instanceof DefaultModel) { |
| 147 |
((DefaultModel) getModel()).addVariable((Variable) data); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
public MultivariateDistribution getDistribution() { |
| 152 |
return distribution; |
| 153 |
} |
| 154 |
|
| 155 |
public static Transform[] parseListOfTransforms(XMLObject xo, int maxDim) throws XMLParseException { |
| 156 |
Transform[] transforms = null; |
| 157 |
|
| 158 |
boolean anyTransforms = false; |
| 159 |
for (int i = 0; i < xo.getChildCount(); ++i) { |
| 160 |
if (xo.getChild(i) instanceof Transform.ParsedTransform) { |
| 161 |
Transform.ParsedTransform t = (Transform.ParsedTransform) xo.getChild(i); |
| 162 |
if (transforms == null) { |
| 163 |
transforms = Transform.Util.getListOfNoTransforms(maxDim); |
| 164 |
} |
| 165 |
|
| 166 |
t.end = Math.max(t.end, maxDim); |
| 167 |
if (t.start < 0 || t.end < 0 || t.start > t.end) { |
| 168 |
throw new XMLParseException("Invalid bounds for transform in " + xo.getId()); |
| 169 |
} |
| 170 |
for (int j = t.start; j < t.end; j += t.every) { |
| 171 |
transforms[j] = t.transform; |
| 172 |
anyTransforms = true; |
| 173 |
} |
| 174 |
} |
| 175 |
} |
| 176 |
if (anyTransforms) { |
| 177 |
StringBuilder sb = new StringBuilder("Using distributional transforms in " + xo.getId() + "\n"); |
| 178 |
for (int i = 0; i < transforms.length; ++i) { |
| 179 |
if (transforms[i] != Transform.NONE) { |
| 180 |
sb.append("\t").append(transforms[i].getTransformName()).append(" on index ") |
| 181 |
.append(i + 1).append("\n"); |
| 182 |
} |
| 183 |
} |
| 184 |
Logger.getLogger("dr.utils.Transform").info(sb.toString()); |
| 185 |
} |
| 186 |
return transforms; |
| 187 |
} |
| 188 |
|
| 189 |
public static XMLObjectParser DIRICHLET_PRIOR_PARSER = new AbstractXMLObjectParser() { |
| 190 |
|
| 191 |
public String getParserName() { |
| 192 |
return DIRICHLET_PRIOR; |
| 193 |
} |
| 194 |
|
| 195 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 196 |
|
| 197 |
boolean sumConstraint = false; |
| 198 |
if (xo.hasAttribute(SUM_TO_NUMBER_OF_ELEMENTS)) { |
| 199 |
sumConstraint = xo.getBooleanAttribute(SUM_TO_NUMBER_OF_ELEMENTS); |
| 200 |
} |
| 201 |
|
| 202 |
XMLObject cxo = xo.getChild(COUNTS); |
| 203 |
Parameter counts = (Parameter) cxo.getChild(Parameter.class); |
| 204 |
|
| 205 |
DirichletDistribution dirichlet = new DirichletDistribution(counts.getParameterValues(), sumConstraint); |
| 206 |
|
| 207 |
MultivariateDistributionLikelihood likelihood = new MultivariateDistributionLikelihood( |
| 208 |
dirichlet); |
| 209 |
|
| 210 |
cxo = xo.getChild(DATA); |
| 211 |
for (int j = 0; j < cxo.getChildCount(); j++) { |
| 212 |
if (cxo.getChild(j) instanceof Parameter) { |
| 213 |
likelihood.addData((Parameter) cxo.getChild(j)); |
| 214 |
} else { |
| 215 |
throw new XMLParseException("illegal element in " + xo.getName() + " element " + cxo.getName()); |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
return likelihood; |
| 220 |
} |
| 221 |
|
| 222 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 223 |
return rules; |
| 224 |
} |
| 225 |
|
| 226 |
private final XMLSyntaxRule[] rules = { |
| 227 |
AttributeRule.newBooleanRule(SUM_TO_NUMBER_OF_ELEMENTS, true), |
| 228 |
new ElementRule(COUNTS, |
| 229 |
new XMLSyntaxRule[]{new ElementRule(Parameter.class)}), |
| 230 |
new ElementRule(DATA, |
| 231 |
new XMLSyntaxRule[]{new ElementRule(Parameter.class)}, 1, Integer.MAX_VALUE), |
| 232 |
}; |
| 233 |
|
| 234 |
public String getParserDescription() { |
| 235 |
return "Calculates the likelihood of some data under a Dirichlet distribution."; |
| 236 |
} |
| 237 |
|
| 238 |
public Class getReturnType() { |
| 239 |
return Likelihood.class; |
| 240 |
} |
| 241 |
}; |
| 242 |
|
| 243 |
public static XMLObjectParser INV_WISHART_PRIOR_PARSER = new AbstractXMLObjectParser() { |
| 244 |
|
| 245 |
public String getParserName() { |
| 246 |
return INV_WISHART_PRIOR; |
| 247 |
} |
| 248 |
|
| 249 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 250 |
|
| 251 |
int df = xo.getIntegerAttribute(DF); |
| 252 |
|
| 253 |
XMLObject cxo = xo.getChild(SCALE_MATRIX); |
| 254 |
MatrixParameter scaleMatrix = (MatrixParameter) cxo.getChild(MatrixParameter.class); |
| 255 |
InverseWishartDistribution invWishart = new InverseWishartDistribution(df, scaleMatrix.getParameterAsMatrix()); |
| 256 |
|
| 257 |
MultivariateDistributionLikelihood likelihood = new MultivariateDistributionLikelihood( |
| 258 |
invWishart); |
| 259 |
|
| 260 |
cxo = xo.getChild(DATA); |
| 261 |
for (int j = 0; j < cxo.getChildCount(); j++) { |
| 262 |
if (cxo.getChild(j) instanceof MatrixParameter) { |
| 263 |
likelihood.addData((MatrixParameter) cxo.getChild(j)); |
| 264 |
} else { |
| 265 |
throw new XMLParseException("illegal element in " + xo.getName() + " element " + cxo.getName()); |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
return likelihood; |
| 270 |
} |
| 271 |
|
| 272 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 273 |
return rules; |
| 274 |
} |
| 275 |
|
| 276 |
private final XMLSyntaxRule[] rules = { |
| 277 |
AttributeRule.newDoubleRule(DF), |
| 278 |
new ElementRule(SCALE_MATRIX, |
| 279 |
new XMLSyntaxRule[]{new ElementRule(MatrixParameter.class)}), |
| 280 |
}; |
| 281 |
|
| 282 |
public String getParserDescription() { |
| 283 |
return "Calculates the likelihood of some data under an Inverse-Wishart distribution."; |
| 284 |
} |
| 285 |
|
| 286 |
public Class getReturnType() { |
| 287 |
return Likelihood.class; |
| 288 |
} |
| 289 |
}; |
| 290 |
|
| 291 |
public static XMLObjectParser WISHART_PRIOR_PARSER = new AbstractXMLObjectParser() { |
| 292 |
|
| 293 |
public String getParserName() { |
| 294 |
return WISHART_PRIOR; |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 299 |
|
| 300 |
MultivariateDistributionLikelihood likelihood; |
| 301 |
|
| 302 |
if (xo.hasAttribute(NON_INFORMATIVE) && xo.getBooleanAttribute(NON_INFORMATIVE)) { |
| 303 |
|
| 304 |
XMLObject cxo = xo.getChild(DATA); |
| 305 |
int dim = ((MatrixParameter) cxo.getChild(0)).getColumnDimension(); |
| 306 |
likelihood = new MultivariateDistributionLikelihood(new WishartDistribution(dim)); |
| 307 |
} else { |
| 308 |
if (!xo.hasAttribute(DF) || !xo.hasChildNamed(SCALE_MATRIX)) { |
| 309 |
throw new XMLParseException("Must specify both a df and scaleMatrix"); |
| 310 |
} |
| 311 |
|
| 312 |
double df = xo.getDoubleAttribute(DF); |
| 313 |
|
| 314 |
XMLObject cxo = xo.getChild(SCALE_MATRIX); |
| 315 |
MatrixParameter scaleMatrix = (MatrixParameter) cxo.getChild(MatrixParameter.class); |
| 316 |
|
| 317 |
likelihood = new MultivariateDistributionLikelihood( |
| 318 |
new WishartDistribution(df, scaleMatrix.getParameterAsMatrix()) |
| 319 |
); |
| 320 |
|
| 321 |
} |
| 322 |
|
| 323 |
XMLObject cxo = xo.getChild(DATA); |
| 324 |
for (int j = 0; j < cxo.getChildCount(); j++) { |
| 325 |
if (cxo.getChild(j) instanceof MatrixParameter) { |
| 326 |
likelihood.addData((MatrixParameter) cxo.getChild(j)); |
| 327 |
} else { |
| 328 |
throw new XMLParseException("illegal element in " + xo.getName() + " element " + cxo.getName()); |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
return likelihood; |
| 333 |
} |
| 334 |
|
| 335 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 336 |
return rules; |
| 337 |
} |
| 338 |
|
| 339 |
private final XMLSyntaxRule[] rules;{ |
| 340 |
rules = new XMLSyntaxRule[]{ |
| 341 |
AttributeRule.newBooleanRule(NON_INFORMATIVE, true), |
| 342 |
AttributeRule.newDoubleRule(DF, true), |
| 343 |
new ElementRule(SCALE_MATRIX, |
| 344 |
new XMLSyntaxRule[]{new ElementRule(MatrixParameter.class)}, true), |
| 345 |
new ElementRule(DATA, |
| 346 |
new XMLSyntaxRule[]{new ElementRule(MatrixParameter.class, 1, Integer.MAX_VALUE)} |
| 347 |
) |
| 348 |
}; |
| 349 |
} |
| 350 |
|
| 351 |
public String getParserDescription() { |
| 352 |
return "Calculates the likelihood of some data under a Wishart distribution."; |
| 353 |
} |
| 354 |
|
| 355 |
public Class getReturnType() { |
| 356 |
return Likelihood.class; |
| 357 |
} |
| 358 |
}; |
| 359 |
|
| 360 |
public static XMLObjectParser MULTIVARIATE_LIKELIHOOD_PARSER = new AbstractXMLObjectParser() { |
| 361 |
|
| 362 |
public String getParserName() { |
| 363 |
return MULTIVARIATE_LIKELIHOOD; |
| 364 |
} |
| 365 |
|
| 366 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 367 |
|
| 368 |
XMLObject cxo = xo.getChild(DistributionLikelihoodParser.DISTRIBUTION); |
| 369 |
ParametricMultivariateDistributionModel distribution = (ParametricMultivariateDistributionModel) |
| 370 |
cxo.getChild(ParametricMultivariateDistributionModel.class); |
| 371 |
|
| 372 |
|
| 373 |
int maxDim = distribution.getMean().length; |
| 374 |
Transform[] transforms = parseListOfTransforms(xo, maxDim); |
| 375 |
|
| 376 |
MultivariateDistributionLikelihood likelihood = new MultivariateDistributionLikelihood(xo.getId(), |
| 377 |
distribution, transforms); |
| 378 |
|
| 379 |
boolean dataAsMatrix = xo.getAttribute(DATA_AS_MATRIX, false); |
| 380 |
|
| 381 |
cxo = xo.getChild(DATA); |
| 382 |
if (cxo != null) { |
| 383 |
for (int j = 0; j < cxo.getChildCount(); j++) { |
| 384 |
if (cxo.getChild(j) instanceof Parameter) { |
| 385 |
Parameter data = (Parameter) cxo.getChild(j); |
| 386 |
if (data instanceof MatrixParameter) { |
| 387 |
MatrixParameter matrix = (MatrixParameter) data; |
| 388 |
if (dataAsMatrix) { |
| 389 |
likelihood.addData(matrix); |
| 390 |
} else { |
| 391 |
if (matrix.getParameter(0).getDimension() != distribution.getMean().length) |
| 392 |
throw new XMLParseException("dim(" + data.getStatisticName() + ") = " + matrix.getParameter(0).getDimension() |
| 393 |
+ " is not equal to dim(" + distribution.getType() + ") = " + distribution.getMean().length |
| 394 |
+ " in " + xo.getName() + "element"); |
| 395 |
|
| 396 |
for (int i = 0; i < matrix.getParameterCount(); i++) { |
| 397 |
likelihood.addData(matrix.getParameter(i)); |
| 398 |
} |
| 399 |
} |
| 400 |
} else { |
| 401 |
if (data.getDimension() != distribution.getMean().length) |
| 402 |
throw new XMLParseException("dim(" + data.getStatisticName() + ") = " + data.getDimension() |
| 403 |
+ " is not equal to dim(" + distribution.getType() + ") = " + distribution.getMean().length |
| 404 |
+ " in " + xo.getName() + "element"); |
| 405 |
likelihood.addData(data); |
| 406 |
} |
| 407 |
} else { |
| 408 |
throw new XMLParseException("illegal element in " + xo.getName() + " element"); |
| 409 |
} |
| 410 |
} |
| 411 |
} |
| 412 |
|
| 413 |
return likelihood; |
| 414 |
} |
| 415 |
|
| 416 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 417 |
return rules; |
| 418 |
} |
| 419 |
|
| 420 |
private final XMLSyntaxRule[] rules = { |
| 421 |
new ElementRule(DistributionLikelihoodParser.DISTRIBUTION, |
| 422 |
new XMLSyntaxRule[]{new ElementRule(ParametricMultivariateDistributionModel.class)} |
| 423 |
), |
| 424 |
AttributeRule.newBooleanRule(DATA_AS_MATRIX, true), |
| 425 |
new ElementRule(Transform.ParsedTransform.class, 0, Integer.MAX_VALUE), |
| 426 |
new ElementRule(DATA, |
| 427 |
new XMLSyntaxRule[]{new ElementRule(Parameter.class, 1, Integer.MAX_VALUE)}, true) |
| 428 |
}; |
| 429 |
|
| 430 |
public String getParserDescription() { |
| 431 |
return "Calculates the likelihood of some data under a given multivariate distribution."; |
| 432 |
} |
| 433 |
|
| 434 |
public Class getReturnType() { |
| 435 |
return MultivariateDistributionLikelihood.class; |
| 436 |
} |
| 437 |
}; |
| 438 |
|
| 439 |
|
| 440 |
public static XMLObjectParser MVN_PRIOR_PARSER = new AbstractXMLObjectParser() { |
| 441 |
|
| 442 |
public String getParserName() { |
| 443 |
return MVN_PRIOR; |
| 444 |
} |
| 445 |
|
| 446 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 447 |
|
| 448 |
XMLObject cxo = xo.getChild(MVN_MEAN); |
| 449 |
Parameter mean = (Parameter) cxo.getChild(Parameter.class); |
| 450 |
|
| 451 |
cxo = xo.getChild(MVN_PRECISION); |
| 452 |
MatrixParameter precision = (MatrixParameter) cxo.getChild(MatrixParameter.class); |
| 453 |
|
| 454 |
if (mean.getDimension() != precision.getRowDimension() || |
| 455 |
mean.getDimension() != precision.getColumnDimension()) |
| 456 |
throw new XMLParseException("Mean and precision have wrong dimensions in " + xo.getName() + " element"); |
| 457 |
|
| 458 |
Transform[] transforms = parseListOfTransforms(xo, mean.getDimension()); |
| 459 |
|
| 460 |
MultivariateDistributionLikelihood likelihood = |
| 461 |
new MultivariateDistributionLikelihood( |
| 462 |
new MultivariateNormalDistribution(mean.getParameterValues(), |
| 463 |
precision.getParameterAsMatrix()), transforms |
| 464 |
); |
| 465 |
cxo = xo.getChild(DATA); |
| 466 |
if (cxo != null) { |
| 467 |
for (int j = 0; j < cxo.getChildCount(); j++) { |
| 468 |
if (cxo.getChild(j) instanceof Parameter) { |
| 469 |
Parameter data = (Parameter) cxo.getChild(j); |
| 470 |
if (data instanceof MatrixParameter) { |
| 471 |
MatrixParameter matrix = (MatrixParameter) data; |
| 472 |
if (matrix.getParameter(0).getDimension() != mean.getDimension()) |
| 473 |
throw new XMLParseException("dim(" + data.getStatisticName() + ") = " + matrix.getParameter(0).getDimension() |
| 474 |
+ " is not equal to dim(" + mean.getStatisticName() + ") = " + mean.getDimension() |
| 475 |
+ " in " + xo.getName() + "element"); |
| 476 |
|
| 477 |
for (int i = 0; i < matrix.getParameterCount(); i++) { |
| 478 |
likelihood.addData(matrix.getParameter(i)); |
| 479 |
} |
| 480 |
} else { |
| 481 |
if (data.getDimension() != mean.getDimension()) |
| 482 |
throw new XMLParseException("dim(" + data.getStatisticName() + ") = " + data.getDimension() |
| 483 |
+ " is not equal to dim(" + mean.getStatisticName() + ") = " + mean.getDimension() |
| 484 |
+ " in " + xo.getName() + "element"); |
| 485 |
likelihood.addData(data); |
| 486 |
} |
| 487 |
} else { |
| 488 |
throw new XMLParseException("illegal element in " + xo.getName() + " element"); |
| 489 |
} |
| 490 |
} |
| 491 |
} |
| 492 |
|
| 493 |
return likelihood; |
| 494 |
} |
| 495 |
|
| 496 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 497 |
return rules; |
| 498 |
} |
| 499 |
|
| 500 |
private final XMLSyntaxRule[] rules = { |
| 501 |
new ElementRule(MVN_MEAN, |
| 502 |
new XMLSyntaxRule[]{new ElementRule(Parameter.class)}), |
| 503 |
new ElementRule(MVN_PRECISION, |
| 504 |
new XMLSyntaxRule[]{new ElementRule(MatrixParameter.class)}), |
| 505 |
new ElementRule(Transform.ParsedTransform.class, 0, Integer.MAX_VALUE), |
| 506 |
new ElementRule(DATA, |
| 507 |
new XMLSyntaxRule[]{new ElementRule(Parameter.class, 1, Integer.MAX_VALUE)}, true) |
| 508 |
}; |
| 509 |
|
| 510 |
public String getParserDescription() { |
| 511 |
return "Calculates the likelihood of some data under a given multivariate-normal distribution."; |
| 512 |
} |
| 513 |
|
| 514 |
public Class getReturnType() { |
| 515 |
return MultivariateDistributionLikelihood.class; |
| 516 |
} |
| 517 |
}; |
| 518 |
|
| 519 |
public static XMLObjectParser MVGAMMA_PRIOR_PARSER = new AbstractXMLObjectParser() { |
| 520 |
|
| 521 |
public String getParserName() { |
| 522 |
return MVGAMMA_PRIOR; |
| 523 |
} |
| 524 |
|
| 525 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 526 |
|
| 527 |
|
| 528 |
double[] shape; |
| 529 |
double[] scale; |
| 530 |
|
| 531 |
|
| 532 |
if (xo.hasChildNamed(MVGAMMA_SHAPE)) { |
| 533 |
|
| 534 |
XMLObject cxo = xo.getChild(MVGAMMA_SHAPE); |
| 535 |
shape = ((Parameter) cxo.getChild(Parameter.class)).getParameterValues(); |
| 536 |
|
| 537 |
cxo = xo.getChild(MVGAMMA_SCALE); |
| 538 |
scale = ((Parameter) cxo.getChild(Parameter.class)).getParameterValues(); |
| 539 |
|
| 540 |
if (shape.length != scale.length) |
| 541 |
throw new XMLParseException("Shape and scale have wrong dimensions in " + xo.getName() + " element"); |
| 542 |
|
| 543 |
} else { |
| 544 |
|
| 545 |
XMLObject cxo = xo.getChild(MVN_MEAN); |
| 546 |
double[] mean = ((Parameter) cxo.getChild(Parameter.class)).getParameterValues(); |
| 547 |
|
| 548 |
cxo = xo.getChild(MVN_CV); |
| 549 |
double[] cv = ((Parameter) cxo.getChild(Parameter.class)).getParameterValues(); |
| 550 |
|
| 551 |
if (mean.length != cv.length) |
| 552 |
throw new XMLParseException("Mean and CV have wrong dimensions in " + xo.getName() + " element"); |
| 553 |
|
| 554 |
final int dim = mean.length; |
| 555 |
shape = new double[dim]; |
| 556 |
scale = new double[dim]; |
| 557 |
|
| 558 |
for (int i = 0; i < dim; i++) { |
| 559 |
double c2 = cv[i] * cv[i]; |
| 560 |
shape[i] = 1.0 / c2; |
| 561 |
scale[i] = c2 * mean[i]; |
| 562 |
} |
| 563 |
} |
| 564 |
|
| 565 |
MultivariateDistributionLikelihood likelihood = |
| 566 |
new MultivariateDistributionLikelihood( |
| 567 |
new MultivariateGammaDistribution(shape, scale) |
| 568 |
); |
| 569 |
XMLObject cxo = xo.getChild(DATA); |
| 570 |
for (int j = 0; j < cxo.getChildCount(); j++) { |
| 571 |
if (cxo.getChild(j) instanceof Parameter) { |
| 572 |
Parameter data = (Parameter) cxo.getChild(j); |
| 573 |
likelihood.addData(data); |
| 574 |
if (data.getDimension() != shape.length) |
| 575 |
throw new XMLParseException("dim(" + data.getStatisticName() + ") != " + shape.length + " in " + xo.getName() + "element"); |
| 576 |
} else { |
| 577 |
throw new XMLParseException("illegal element in " + xo.getName() + " element"); |
| 578 |
} |
| 579 |
} |
| 580 |
return likelihood; |
| 581 |
} |
| 582 |
|
| 583 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 584 |
return rules; |
| 585 |
} |
| 586 |
|
| 587 |
private final XMLSyntaxRule[] rules = { |
| 588 |
|
| 589 |
new XORRule( |
| 590 |
new ElementRule(MVGAMMA_SHAPE, |
| 591 |
new XMLSyntaxRule[]{new ElementRule(Parameter.class)}), |
| 592 |
new ElementRule(MVN_MEAN, |
| 593 |
new XMLSyntaxRule[]{new ElementRule(Parameter.class)})), |
| 594 |
new XORRule( |
| 595 |
new ElementRule(MVGAMMA_SCALE, |
| 596 |
new XMLSyntaxRule[]{new ElementRule(Parameter.class)}), |
| 597 |
new ElementRule(MVN_CV, |
| 598 |
new XMLSyntaxRule[]{new ElementRule(Parameter.class)})), |
| 599 |
new ElementRule(DATA, |
| 600 |
new XMLSyntaxRule[]{new ElementRule(Parameter.class, 1, Integer.MAX_VALUE)}) |
| 601 |
}; |
| 602 |
|
| 603 |
public String getParserDescription() { |
| 604 |
return "Calculates the likelihood of some data under a given multivariate-gamma distribution."; |
| 605 |
} |
| 606 |
|
| 607 |
public Class getReturnType() { |
| 608 |
return MultivariateDistributionLikelihood.class; |
| 609 |
} |
| 610 |
}; |
| 611 |
|
| 612 |
public static XMLObjectParser TREE_TRAIT_MODEL = new AbstractXMLObjectParser() { |
| 613 |
|
| 614 |
public String getParserName() { |
| 615 |
return TREE_TRAIT_NORMAL; |
| 616 |
} |
| 617 |
|
| 618 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 619 |
|
| 620 |
boolean conditionOnRoot = xo.getAttribute(CONDITION, false); |
| 621 |
|
| 622 |
FullyConjugateMultivariateTraitLikelihood traitModel = (FullyConjugateMultivariateTraitLikelihood) |
| 623 |
xo.getChild(FullyConjugateMultivariateTraitLikelihood.class); |
| 624 |
|
| 625 |
TreeTraitNormalDistributionModel treeTraitModel; |
| 626 |
|
| 627 |
if (xo.getChild(ROOT_VALUE) != null) { |
| 628 |
XMLObject cxo = xo.getChild(ROOT_VALUE); |
| 629 |
Parameter rootValue = (Parameter) cxo.getChild(Parameter.class); |
| 630 |
treeTraitModel = new TreeTraitNormalDistributionModel(traitModel, rootValue, conditionOnRoot); |
| 631 |
} else { |
| 632 |
treeTraitModel = new TreeTraitNormalDistributionModel(traitModel, conditionOnRoot); |
| 633 |
} |
| 634 |
return treeTraitModel; |
| 635 |
} |
| 636 |
|
| 637 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 638 |
return rules; |
| 639 |
} |
| 640 |
|
| 641 |
private final XMLSyntaxRule[] rules = { |
| 642 |
AttributeRule.newBooleanRule(CONDITION, true), |
| 643 |
new ElementRule(FullyConjugateMultivariateTraitLikelihood.class) |
| 644 |
}; |
| 645 |
|
| 646 |
public String getParserDescription() { |
| 647 |
return "Parses TreeTraitNormalDistributionModel"; |
| 648 |
} |
| 649 |
|
| 650 |
public Class getReturnType() { |
| 651 |
return TreeTraitNormalDistributionModel.class; |
| 652 |
} |
| 653 |
}; |
| 654 |
|
| 655 |
|
| 656 |
public static XMLObjectParser TREE_TRAIT_DISTRIBUTION = new AbstractXMLObjectParser() { |
| 657 |
|
| 658 |
public String getParserName() { |
| 659 |
return TREE_TRAIT; |
| 660 |
} |
| 661 |
|
| 662 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
TreeTraitNormalDistributionModel treeTraitModel = (TreeTraitNormalDistributionModel) |
| 671 |
xo.getChild(TreeTraitNormalDistributionModel.class); |
| 672 |
|
| 673 |
MultivariateDistributionLikelihood likelihood = |
| 674 |
new MultivariateDistributionLikelihood( |
| 675 |
|
| 676 |
treeTraitModel |
| 677 |
); |
| 678 |
|
| 679 |
XMLObject cxo = xo.getChild(DATA); |
| 680 |
for (int j = 0; j < cxo.getChildCount(); j++) { |
| 681 |
if (cxo.getChild(j) instanceof Parameter) { |
| 682 |
likelihood.addData((Parameter) cxo.getChild(j)); |
| 683 |
} else { |
| 684 |
throw new XMLParseException("illegal element in " + xo.getName() + " element " + cxo.getName()); |
| 685 |
} |
| 686 |
} |
| 687 |
return likelihood; |
| 688 |
} |
| 689 |
|
| 690 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 691 |
return rules; |
| 692 |
} |
| 693 |
|
| 694 |
private final XMLSyntaxRule[] rules = { |
| 695 |
|
| 696 |
|
| 697 |
new ElementRule(TreeTraitNormalDistributionModel.class), |
| 698 |
new ElementRule(DATA, |
| 699 |
new XMLSyntaxRule[]{new ElementRule(Parameter.class, 1, Integer.MAX_VALUE)}) |
| 700 |
}; |
| 701 |
|
| 702 |
public String getParserDescription() { |
| 703 |
return "Calculates the likelihood of some data under a given multivariate-gamma distribution."; |
| 704 |
} |
| 705 |
|
| 706 |
public Class getReturnType() { |
| 707 |
return MultivariateDistributionLikelihood.class; |
| 708 |
} |
| 709 |
}; |
| 710 |
} |