src/dr/evomodel/treedatalikelihood/continuous/RepeatedMeasuresTraitLikelihood.java
raw symbols ◀ ▶ SCIP1 Debian patch touch this file
-
Migrating to libejml-java v0.41.
- Author
- Andrius Merkys <[email protected]>
| 1 | /* |
| 2 | * IntegratedFactorTraitDataModel.java |
| 3 | * |
| 4 | * Copyright (c) 2002-2017 Alexei Drummond, Andrew Rambaut and Marc Suchard |
| 5 | * |
| 6 | * This file is part of BEAST. |
| 7 | * See the NOTICE file distributed with this work for additional |
| 8 | * information regarding copyright ownership and licensing. |
| 9 | * |
| 10 | * BEAST is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU Lesser General Public License as |
| 12 | * published by the Free Software Foundation; either version 2 |
| 13 | * of the License, or (at your option) any later version. |
| 14 | * |
| 15 | * BEAST is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU Lesser General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU Lesser General Public |
| 21 | * License along with BEAST; if not, write to the |
| 22 | * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, |
| 23 | * Boston, MA 02110-1301 USA |
| 24 | */ |
| 25 | |
| 26 | package dr.evomodel.treedatalikelihood.continuous; |
| 27 | |
| 28 | import dr.evolution.tree.MutableTreeModel; |
| 29 | import dr.evolution.tree.Tree; |
| 30 | import dr.evomodel.tree.TreeModel; |
| 31 | import dr.evomodel.treedatalikelihood.continuous.cdi.PrecisionType; |
| 32 | import dr.evomodelxml.treelikelihood.TreeTraitParserUtilities; |
| 33 | import dr.inference.model.*; |
| 34 | import dr.math.KroneckerOperation; |
| 35 | import dr.math.distributions.MultivariateNormalDistribution; |
| 36 | import dr.math.matrixAlgebra.IllegalDimension; |
| 37 | import dr.math.matrixAlgebra.Matrix; |
| 38 | import dr.math.matrixAlgebra.Vector; |
| 39 | import dr.math.matrixAlgebra.WrappedVector; |
| 40 | import dr.math.matrixAlgebra.missingData.InversionResult; |
| 41 | import dr.xml.*; |
| •42 | import org.ejml.data.DMatrixRMaj; |
| 43 | |
| 44 | import java.util.ArrayList; |
| 45 | import java.util.Arrays; |
| 46 | import java.util.List; |
| 47 | |
| 48 | import static dr.math.matrixAlgebra.missingData.MissingOps.*; |
| 49 | |
| 50 | /** |
| 51 | * @author Marc A. Suchard |
| 52 | */ |
| 53 | |
| 54 | public class RepeatedMeasuresTraitLikelihood extends AbstractModelLikelihood |
| 55 | implements ContinuousTraitPartialsProvider, Reportable { |
| 56 | |
| 57 | public RepeatedMeasuresTraitLikelihood(String name, |
| 58 | CompoundParameter traitParameter, |
| 59 | List<Integer> missingIndices, |
| 60 | MatrixParameterInterface loadings, |
| 61 | MatrixParameterInterface traitPrecision, |
| 62 | double nuggetPrecision) { |
| 63 | super(name); |
| 64 | |
| 65 | this.traitParameter = traitParameter; |
| 66 | this.loadings = loadings; |
| 67 | this.traitPrecision = traitPrecision; // TODO Generalize for non-diagonal precision |
| 68 | // this.missingIndices = missingIndices; |
| 69 | |
| 70 | this.numTaxa = traitParameter.getParameterCount(); |
| 71 | this.dimTrait = traitParameter.getParameter(0).getDimension(); |
| 72 | assert(dimTrait == loadings.getRowDimension()); |
| 73 | |
| 74 | this.numFactors = loadings.getColumnDimension(); |
| 75 | this.dimPartial = numFactors + PrecisionType.FULL.getMatrixLength(numFactors); |
| 76 | |
| 77 | addVariable(traitParameter); |
| 78 | addVariable(loadings); |
| 79 | addVariable(traitPrecision); |
| 80 | |
| 81 | this.observedIndicators = setupObservedIndicators(missingIndices, numTaxa, dimTrait); |
| 82 | this.observedDimensions = setupObservedDimensions(observedIndicators); |
| 83 | |
| 84 | this.missingFactorIndices = new ArrayList<Integer>(); |
| 85 | for (int i = 0; i < numTaxa * dimTrait; ++i) { |
| 86 | missingFactorIndices.add(i); |
| 87 | } |
| 88 | |
| 89 | this.nuggetPrecision = nuggetPrecision; |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public boolean bufferTips() { |
| 94 | return true; |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public int getTraitCount() { |
| 99 | return 1; |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public int getTraitDimension() { |
| 104 | return numFactors; |
| 105 | } // Returns dimension of latent factors |
| 106 | |
| 107 | @Override |
| 108 | public PrecisionType getPrecisionType() { |
| 109 | return PrecisionType.FULL; |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public double[] getTipPartial(int taxonIndex, boolean fullyObserved) { |
| 114 | // if (fullyObserved) { |
| 115 | // throw new IllegalArgumentException("Wishart statistics are not implemented for the integrated factor model"); |
| 116 | // } |
| 117 | // |
| 118 | // checkStatistics(); |
| 119 | // |
| 120 | // double[] partial = new double[dimPartial]; |
| 121 | // System.arraycopy(partials, taxonIndex * dimPartial, partial, 0, dimPartial); |
| 122 | // return partial; |
| 123 | return null; |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public List<Integer> getMissingIndices() { |
| 128 | return missingFactorIndices; |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public CompoundParameter getParameter() { |
| 133 | return traitParameter; |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public Model getModel() { |
| 138 | return this; |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public double getLogLikelihood() { |
| 143 | if (!likelihoodKnown) { |
| 144 | logLikelihood = calculateLogLikelihood(); |
| 145 | likelihoodKnown = true; |
| 146 | } |
| 147 | return logLikelihood; |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public void makeDirty() { |
| 152 | likelihoodKnown = false; |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | protected void handleModelChangedEvent(Model model, Object object, int index) { |
| 157 | // No model dependencies |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | protected void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) { |
| 162 | // if (variable == loadings || variable == traitPrecision) { |
| 163 | // statisticsKnown = false; |
| 164 | // likelihoodKnown = false; |
| 165 | // fireModelChanged(this); |
| 166 | //// fireModelChanged(this, getTaxonIndex(index)); |
| 167 | // } else if (variable == traitParameter) { |
| 168 | // statisticsKnown = false; |
| 169 | // likelihoodKnown = false; |
| 170 | // fireModelChanged(this); |
| 171 | // } else { |
| 172 | // throw new RuntimeException("Unhandled parameter change type"); |
| 173 | // } |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | protected void storeState() { |
| 178 | storedLogLikelihood = logLikelihood; |
| 179 | storedLikelihoodKnow = likelihoodKnown; |
| 180 | storedStatisticsKnown = statisticsKnown; |
| 181 | |
| 182 | System.arraycopy(partials, 0, storedPartials, 0, partials.length); |
| 183 | System.arraycopy(normalizationConstants, 0, storeedNormalizationConstants, 0, normalizationConstants.length); |
| 184 | } |
| 185 | |
| 186 | @Override |
| 187 | protected void restoreState() { |
| 188 | logLikelihood = storedLogLikelihood; |
| 189 | likelihoodKnown = storedLikelihoodKnow; |
| 190 | statisticsKnown = storedStatisticsKnown; |
| 191 | |
| 192 | double[] tmp1 = partials; |
| 193 | partials = storedPartials; |
| 194 | storedPartials = tmp1; |
| 195 | |
| 196 | double[] tmp2 = normalizationConstants; |
| 197 | normalizationConstants = storeedNormalizationConstants; |
| 198 | storeedNormalizationConstants = tmp2; |
| 199 | } |
| 200 | |
| 201 | @Override |
| 202 | protected void acceptState() { |
| 203 | // Do nothing |
| 204 | } |
| 205 | |
| 206 | private double calculateLogLikelihood() { |
| 207 | |
| 208 | checkStatistics(); |
| 209 | |
| 210 | double logLikelihood = 0.0; |
| 211 | for (double r : normalizationConstants) { |
| 212 | logLikelihood += r; |
| 213 | } |
| 214 | return logLikelihood; |
| 215 | } |
| 216 | |
| 217 | private void setupStatistics() { |
| 218 | if (partials == null) { |
| 219 | partials = new double[numTaxa * dimPartial]; |
| 220 | storedPartials = new double[numTaxa * dimPartial]; |
| 221 | } |
| 222 | |
| 223 | if (normalizationConstants == null) { |
| 224 | normalizationConstants = new double[numTaxa]; |
| 225 | storeedNormalizationConstants = new double[numTaxa]; |
| 226 | } |
| 227 | |
| 228 | computePartialsAndRemainders(); |
| 229 | } |
| 230 | |
| 231 | private final double nuggetPrecision; |
| 232 | |
| •233 | // private void computePrecisionForTaxon(final DMatrixRMaj precision, final int taxon, |
| 234 | // final int numFactors) { |
| 235 | // |
| 236 | // final double[] observed = observedIndicators[taxon]; |
| 237 | // |
| 238 | // // Compute L D_i \Gamma D_i^t L^t // TODO Generalize for non-diagonal \Gamma |
| 239 | // for (int row = 0; row < numFactors; ++row) { |
| 240 | // for (int col = 0; col < numFactors; ++col) { |
| 241 | // double sum = 0; |
| 242 | // for (int k = 0; k < dimTrait; ++k) { |
| 243 | // double prec = (observed[k] == 1.0) ? traitPrecision.getParameterValue(k) : nuggetPrecision; |
| 244 | // sum += loadings.getParameterValue(k, row) * |
| 245 | // prec * |
| 246 | // loadings.getParameterValue(k, col); |
| 247 | // } |
| 248 | // precision.unsafe_set(row, col, sum); |
| 249 | // } |
| 250 | // } |
| 251 | // } |
| 252 | |
| •253 | // private InversionResult fillInMeanForTaxon(final WrappedVector output, final DMatrixRMaj precision, final int taxon) { |
| 254 | // |
| 255 | // final double[] observed = observedIndicators[taxon]; |
| 256 | // final Parameter Y = traitParameter.getParameter(taxon); |
| 257 | // |
| 258 | // // Solve for a value \mu_i s.t. P_i \mu_i = (L D_i Y_i) |
| 259 | // |
| 260 | // final double[] tmp = new double[numFactors]; |
| 261 | // final double[] tmp2 = new double[numFactors]; |
| 262 | // |
| 263 | // for (int row = 0; row < numFactors; ++row) { |
| 264 | // double sum = 0; |
| 265 | // for (int k = 0; k < dimTrait; ++k) { |
| 266 | // sum += loadings.getParameterValue(k, row) * |
| 267 | // observed[k] * traitPrecision.getParameterValue(k) * |
| 268 | // Y.getParameterValue(k); |
| 269 | // } |
| 270 | // tmp[row] = sum; |
| 271 | // } |
| 272 | // |
| •273 | // DMatrixRMaj B = DMatrixRMaj.wrap(numFactors, 1, tmp); |
| 274 | // DMatrixRMaj X = DMatrixRMaj.wrap(numFactors, 1, tmp2); |
| 275 | // |
| 276 | // InversionResult ci = safeSolve(precision, B, X, true); |
| 277 | // |
| 278 | // for (int row = 0; row < numFactors; ++row) { |
| 279 | // output.set(row, X.unsafe_get(row, 0)); |
| 280 | // } |
| 281 | // |
| 282 | // return ci; |
| 283 | // } |
| 284 | |
| 285 | // private double computeTraitInnerProduct(final int taxon) { |
| 286 | // final double[] observed = observedIndicators[taxon]; |
| 287 | // final Parameter Y = traitParameter.getParameter(taxon); |
| 288 | // |
| 289 | // // Compute Y_i^t D_i^t \Gamma D_i Y_i // TODO Generalize for non-diagonal \Gamma |
| 290 | // double sum = 0; |
| 291 | // for (int k = 0; k < dimTrait; ++k) { |
| 292 | // sum += Y.getParameterValue(k) * Y.getParameterValue(k) * |
| 293 | // observed[k] * traitPrecision.getParameterValue(k); |
| 294 | // } |
| 295 | // return sum; |
| 296 | // } |
| 297 | |
| •298 | // private double computeFactorInnerProduct(final WrappedVector mean, final DMatrixRMaj precision) { |
| 299 | // // Compute \mu_i^t P_i \mu^t |
| 300 | // double sum = 0; |
| 301 | // for (int row = 0; row < numFactors; ++row) { |
| 302 | // for (int col = 0; col < numFactors; ++col) { |
| 303 | // sum += mean.get(row) * precision.unsafe_get(row, col) * mean.get(col); |
| 304 | // } |
| 305 | // } |
| 306 | // return sum; |
| 307 | // } |
| 308 | |
| 309 | // private double getTraitDeterminant(final int taxon) { |
| 310 | // |
| 311 | // final double[] observed = observedIndicators[taxon]; |
| 312 | // |
| 313 | // // Compute det( D_i \Gamma D_i^t) // TODO Generalize for non-diagonal \Gamma |
| 314 | // double det = 1.0; |
| 315 | // for (int k = 0; k < dimTrait; ++k) { |
| 316 | // if (observed[k] == 1.0) { |
| 317 | // det *= traitPrecision.getParameterValue(k); |
| 318 | // } |
| 319 | // } |
| 320 | // return det; |
| 321 | // } |
| 322 | |
| •323 | // private void makeCompletedUnobserved(final DMatrixRMaj matrix, double diagonal) { |
| 324 | // for (int row = 0; row < numFactors; ++row) { |
| 325 | // for (int col = 0; col < numFactors; ++col) { |
| 326 | // double x = (row == col) ? diagonal : 0.0; |
| 327 | // matrix.unsafe_set(row, col, x); |
| 328 | // } |
| 329 | // } |
| 330 | // } |
| 331 | |
| 332 | private void computePartialsAndRemainders() { |
| 333 | |
| •334 | // final DMatrixRMaj precision = new DMatrixRMaj(numFactors, numFactors); |
| 335 | // final DMatrixRMaj variance = new DMatrixRMaj(numFactors, numFactors); |
| 336 | // |
| 337 | // int partialsOffset = 0; |
| 338 | // for (int taxon = 0; taxon < numTaxa; ++taxon) { |
| 339 | // |
| 340 | // // Work with mean in-place |
| 341 | // final WrappedVector mean = new WrappedVector.Raw(partials, partialsOffset, numFactors); |
| 342 | // |
| 343 | // computePrecisionForTaxon(precision, taxon, numFactors); |
| 344 | // InversionResult ci = fillInMeanForTaxon(mean, precision, taxon); |
| 345 | // |
| 346 | // if (DEBUG) { |
| 347 | // System.err.println("taxon " + taxon); |
| 348 | // System.err.println("\tprecision: " + precision); |
| 349 | // } |
| 350 | // |
| 351 | // double constant; |
| 352 | // double nuggetDensity = 0; |
| 353 | // |
| 354 | // if (observedDimensions[taxon] == 0) { |
| 355 | // |
| 356 | // makeCompletedUnobserved(precision, 0); |
| 357 | // makeCompletedUnobserved(variance, Double.POSITIVE_INFINITY); |
| 358 | // constant = 0.0; |
| 359 | // |
| 360 | // } else { |
| 361 | // |
| 362 | // |
| 363 | // if (DEBUG) { |
| 364 | // System.err.println("\tmean: " + mean); |
| 365 | // //System.err.println("\n"); |
| 366 | // } |
| 367 | // |
| 368 | // final double factorDeterminant = ci.getDeterminant(); |
| 369 | // double traitDeterminant = getTraitDeterminant(taxon); |
| 370 | // |
| 371 | // final double logDetChange = Math.log(traitDeterminant) - Math.log(factorDeterminant); |
| 372 | // |
| 373 | // final double factorInnerProduct = computeFactorInnerProduct(mean, precision); |
| 374 | // final double traitInnerProduct = computeTraitInnerProduct(taxon); |
| 375 | // final double innerProductChange = traitInnerProduct - factorInnerProduct; |
| 376 | // |
| 377 | // int dimensionChange = observedDimensions[taxon] - ci.getEffectiveDimension(); |
| 378 | // |
| 379 | // if (DEBUG) { |
| 380 | // System.err.println("fIP: " + factorInnerProduct); |
| 381 | // System.err.println("tIP: " + traitInnerProduct); |
| 382 | // System.err.println("fDet: " + factorDeterminant); |
| 383 | // System.err.println("tDet: " + traitDeterminant); |
| 384 | // System.err.println("deltaDim: " + dimensionChange + " deltaIP: " + innerProductChange + |
| 385 | // "\n\n"); |
| 386 | // } |
| 387 | // |
| 388 | // constant = 0.5 * (logDetChange - innerProductChange) - LOG_SQRT_2_PI * (dimensionChange) - |
| 389 | // nuggetDensity; |
| 390 | // |
| 391 | // } |
| 392 | // |
| 393 | // // store in precision, variance and normalization constant |
| 394 | // unwrap(precision, partials, partialsOffset + numFactors); // TODO PrecisionType should do this offset math |
| 395 | // |
| 396 | // if (STORE_VARIANCE) { // TODO Remove |
| 397 | // safeInvert(precision, variance, true); |
| 398 | // unwrap(variance, partials, partialsOffset + numFactors + numFactors * numFactors); |
| 399 | // } |
| 400 | // |
| 401 | // normalizationConstants[taxon] = constant; |
| 402 | // |
| 403 | // partialsOffset += dimPartial; |
| 404 | // } |
| 405 | } |
| 406 | |
| 407 | private static final boolean STORE_VARIANCE = false; |
| 408 | private static final boolean DEBUG = false; |
| 409 | |
| 410 | private void checkStatistics() { |
| 411 | if (!statisticsKnown) { |
| 412 | setupStatistics(); |
| 413 | statisticsKnown = true; |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | private static double[][] setupObservedIndicators(List<Integer> missingIndices, int nTaxa, int dimTrait) { |
| 418 | double[][] observed = new double[nTaxa][dimTrait]; |
| 419 | |
| 420 | // for (double[] v : observed) { |
| 421 | // Arrays.fill(v, 1.0); |
| 422 | // } |
| 423 | // |
| 424 | // if (missingIndices != null) { |
| 425 | // for (Integer idx : missingIndices) { |
| 426 | // int taxon = idx / dimTrait; |
| 427 | // int trait = idx % dimTrait; |
| 428 | // observed[taxon][trait] = 0.0; |
| 429 | // } |
| 430 | // } |
| 431 | |
| 432 | return observed; |
| 433 | } |
| 434 | |
| 435 | private static int[] setupObservedDimensions(double[][] observed) { |
| 436 | int length = observed.length; |
| 437 | int[] dimensions = new int[length]; |
| 438 | |
| 439 | for (int i = 0; i < length; ++i) { |
| 440 | double sum = 0; |
| 441 | for (double x : observed[i]) { |
| 442 | sum += x; |
| 443 | } |
| 444 | dimensions[i] = (int) sum; |
| 445 | } |
| 446 | |
| 447 | return dimensions; |
| 448 | } |
| 449 | |
| 450 | private boolean likelihoodKnown = false; |
| 451 | private boolean storedLikelihoodKnow; |
| 452 | private boolean statisticsKnown = false; |
| 453 | private boolean storedStatisticsKnown; |
| 454 | private double logLikelihood; |
| 455 | private double storedLogLikelihood; |
| 456 | |
| 457 | private double[] partials; |
| 458 | private double[] storedPartials; |
| 459 | |
| 460 | private double[] normalizationConstants; |
| 461 | private double[] storeedNormalizationConstants; |
| 462 | |
| 463 | private final int numTaxa; |
| 464 | private final int dimTrait; |
| 465 | private final int dimPartial; |
| 466 | private final int numFactors; |
| 467 | private final CompoundParameter traitParameter; |
| 468 | private final MatrixParameterInterface loadings; |
| 469 | private final MatrixParameterInterface traitPrecision; |
| 470 | private final List<Integer> missingFactorIndices; |
| 471 | |
| 472 | private final double[][] observedIndicators; |
| 473 | private final int[] observedDimensions; |
| 474 | |
| 475 | public static double LOG_SQRT_2_PI = 0.5 * Math.log(2 * Math.PI); |
| 476 | |
| 477 | // TODO Move remainder into separate class file |
| 478 | public static AbstractXMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 479 | @Override |
| 480 | public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 481 | |
| 482 | MutableTreeModel treeModel = (MutableTreeModel) xo.getChild(TreeModel.class); |
| 483 | TreeTraitParserUtilities utilities = new TreeTraitParserUtilities(); |
| 484 | |
| 485 | TreeTraitParserUtilities.TraitsAndMissingIndices returnValue = |
| 486 | utilities.parseTraitsFromTaxonAttributes(xo, TreeTraitParserUtilities.DEFAULT_TRAIT_NAME, |
| 487 | treeModel, true); |
| 488 | CompoundParameter traitParameter = returnValue.traitParameter; |
| 489 | List<Integer> missingIndices = returnValue.missingIndices; |
| 490 | |
| 491 | // MatrixParameterInterface loadings = (MatrixParameterInterface) xo.getElementFirstChild(LOADINGS); |
| 492 | Parameter traitPrecision = (Parameter) xo.getElementFirstChild(PRECISION); |
| 493 | |
| 494 | // double nugget = xo.getAttribute(NUGGET, 0.0); |
| 495 | |
| 496 | return new RepeatedMeasuresTraitLikelihood(xo.getId(), traitParameter, missingIndices, |
| 497 | null, (MatrixParameterInterface) traitPrecision, 0.0); |
| 498 | } |
| 499 | |
| 500 | @Override |
| 501 | public XMLSyntaxRule[] getSyntaxRules() { |
| 502 | return rules; |
| 503 | } |
| 504 | |
| 505 | @Override |
| 506 | public String getParserDescription() { |
| 507 | return null; |
| 508 | } |
| 509 | |
| 510 | @Override |
| 511 | public Class getReturnType() { |
| 512 | return RepeatedMeasuresTraitLikelihood.class; |
| 513 | } |
| 514 | |
| 515 | @Override |
| 516 | public String getParserName() { |
| 517 | return REPEATED_MEASURES_MODEL; |
| 518 | } |
| 519 | }; |
| 520 | |
| 521 | public static final String REPEATED_MEASURES_MODEL = "repeatedMeasuresModel"; |
| 522 | // public static final String LOADINGS = "loadings"; |
| 523 | public static final String PRECISION = "precision"; |
| 524 | // public static final String NUGGET = "nugget"; |
| 525 | |
| 526 | private final static XMLSyntaxRule[] rules = new XMLSyntaxRule[] { |
| 527 | // new ElementRule(LOADINGS, new XMLSyntaxRule[] { |
| 528 | // new ElementRule(MatrixParameterInterface.class), |
| 529 | // }), |
| 530 | new ElementRule(PRECISION, new XMLSyntaxRule[] { |
| 531 | new ElementRule(Parameter.class), |
| 532 | }), |
| 533 | // Tree trait parser |
| 534 | new ElementRule(MutableTreeModel.class), |
| 535 | AttributeRule.newStringRule(TreeTraitParserUtilities.TRAIT_NAME), |
| 536 | new ElementRule(TreeTraitParserUtilities.TRAIT_PARAMETER, new XMLSyntaxRule[]{ |
| 537 | new ElementRule(Parameter.class) |
| 538 | }), |
| 539 | new ElementRule(TreeTraitParserUtilities.MISSING, new XMLSyntaxRule[]{ |
| 540 | new ElementRule(Parameter.class) |
| 541 | }, true), |
| 542 | // AttributeRule.newDoubleRule(NUGGET, true), |
| 543 | |
| 544 | }; |
| 545 | |
| 546 | public void setLikelihoodDelegate(ContinuousDataLikelihoodDelegate delegate) { |
| 547 | this.delegate = delegate; |
| 548 | } |
| 549 | |
| 550 | private ContinuousDataLikelihoodDelegate delegate = null; |
| 551 | |
| 552 | // private static Matrix buildDiagonalMatrix(double[] diagonals) { |
| 553 | // Matrix mat = new Matrix(diagonals.length, diagonals.length); |
| 554 | // for (int i = 0; i < diagonals.length; ++i) { |
| 555 | // mat.set(i, i, diagonals[i]); |
| 556 | // } |
| 557 | // return mat; |
| 558 | // } |
| 559 | |
| 560 | @Override |
| 561 | public String getReport() { |
| 562 | |
| 563 | StringBuilder sb = new StringBuilder(); |
| 564 | |
| 565 | double logComponents = 0; |
| 566 | |
| 567 | if (delegate != null) { |
| 568 | |
| 569 | // final Tree tree = delegate.getCallbackLikelihood().getTree(); |
| 570 | // sb.append(tree.toString()); |
| 571 | // sb.append("\n\n"); |
| 572 | // |
| 573 | // final double normalization = delegate.getRateTransformation().getNormalization(); |
| 574 | // final double priorSampleSize = delegate.getRootProcessDelegate().getPseudoObservations(); |
| 575 | // |
| 576 | // double[][] treeStructure = MultivariateTraitDebugUtilities.getTreeVariance(tree, 1.0, Double.POSITIVE_INFINITY); |
| 577 | // sb.append("Tree structure:\n"); |
| 578 | // sb.append(new Matrix(treeStructure)); |
| 579 | // sb.append("\n\n"); |
| 580 | // |
| 581 | // double[][] treeVariance = MultivariateTraitDebugUtilities.getTreeVariance(tree, normalization, priorSampleSize); |
| 582 | // |
| 583 | // Matrix treeV = new Matrix(treeVariance); |
| 584 | // Matrix treeP = treeV.inverse(); |
| 585 | // |
| 586 | // sb.append("Tree variance:\n"); |
| 587 | // sb.append(treeV); |
| 588 | // sb.append("Tree precision:\n"); |
| 589 | // sb.append(treeP); |
| 590 | // sb.append("\n\n"); |
| 591 | // |
| 592 | // Matrix Lt = new Matrix(loadings.getParameterAsMatrix()); |
| 593 | // sb.append("Loadings:\n"); |
| 594 | // sb.append(Lt); |
| 595 | // sb.append("\n\n"); |
| 596 | // Matrix loadingsVariance = null; |
| 597 | // try { |
| 598 | // loadingsVariance = Lt.product(Lt.transpose()); |
| 599 | // } catch (IllegalDimension illegalDimension) { |
| 600 | // illegalDimension.printStackTrace(); |
| 601 | // } |
| 602 | // sb.append("Loadings variance:\n"); |
| 603 | // sb.append(loadingsVariance); |
| 604 | // sb.append("\n\n"); |
| 605 | // |
| 606 | // Matrix gamma = buildDiagonalMatrix(traitPrecision.getParameterValues()); |
| 607 | // sb.append("Trait precision:\n"); |
| 608 | // sb.append(gamma); |
| 609 | // sb.append("\n\n"); |
| 610 | // Matrix gammaVariance = gamma.inverse(); |
| 611 | // |
| 612 | // double[] tmp = new double[tree.getExternalNodeCount()]; |
| 613 | // Arrays.fill(tmp, 1.0); |
| 614 | // Matrix identity = buildDiagonalMatrix(tmp); |
| 615 | // Matrix loadingsFactorsVariance = new Matrix(KroneckerOperation.product(treeVariance, loadingsVariance.toComponents())); |
| 616 | // Matrix errorVariance = new Matrix(KroneckerOperation.product(identity.toComponents(), gammaVariance.toComponents())); |
| 617 | // |
| 618 | // sb.append("Loadings-factors variance:\n"); |
| 619 | // sb.append(loadingsFactorsVariance); |
| 620 | // sb.append("\n\n"); |
| 621 | // |
| 622 | // sb.append("Error variance\n"); |
| 623 | // sb.append(errorVariance); |
| 624 | // sb.append("\n\n"); |
| 625 | // |
| 626 | // Matrix totalVariance = null; |
| 627 | // try { |
| 628 | // totalVariance = loadingsFactorsVariance.add(errorVariance); |
| 629 | // } catch (IllegalDimension illegalDimension) { |
| 630 | // illegalDimension.printStackTrace(); |
| 631 | // } |
| 632 | // |
| 633 | // double[] allData = getParameter().getParameterValues(); |
| 634 | // |
| 635 | // List<Integer> notMissing = new ArrayList<Integer>(); |
| 636 | // for (int taxon = 0; taxon < numTaxa; ++taxon) { |
| 637 | // double[] observed = observedIndicators[taxon]; |
| 638 | // for (int trait = 0; trait < dimTrait; ++trait) { |
| 639 | // if (observed[trait] == 0.0) { |
| 640 | // System.err.println("Missing taxon " + taxon + " trait " + trait); |
| 641 | // } else { |
| 642 | // notMissing.add(taxon * dimTrait + trait); |
| 643 | // } |
| 644 | // } |
| 645 | // } |
| 646 | // |
| 647 | // int[] notMissingIndices = new int[notMissing.size()]; |
| 648 | // double[] data = new double[notMissing.size()]; |
| 649 | // for (int i = 0; i < notMissing.size(); ++i) { |
| 650 | // notMissingIndices[i] = notMissing.get(i); |
| 651 | // data[i] = allData[notMissing.get(i)]; |
| 652 | // } |
| 653 | // |
| 654 | // totalVariance = new Matrix(Matrix.gatherRowsAndColumns(totalVariance.toComponents(), notMissingIndices, notMissingIndices)); |
| 655 | // Matrix totalPrecision = totalVariance.inverse(); |
| 656 | // |
| 657 | // sb.append("Total variance:\n"); |
| 658 | // sb.append(totalVariance); |
| 659 | // sb.append("\n\n"); |
| 660 | // sb.append("Total precision:\n"); |
| 661 | // sb.append(totalPrecision); |
| 662 | // sb.append("\n\n"); |
| 663 | // |
| 664 | // sb.append("Data:\n"); |
| 665 | // sb.append(new Vector(data)); |
| 666 | // sb.append("\n\n"); |
| 667 | // |
| 668 | // MultivariateNormalDistribution mvn = new MultivariateNormalDistribution(new double[data.length], |
| 669 | // totalPrecision.toComponents()); |
| 670 | // |
| 671 | // double logDensity = mvn.logPdf(data); |
| 672 | // sb.append("logMultiVariateNormalDensity = " + logDensity + "\n\n"); |
| 673 | // |
| 674 | // double logInc = delegate.getCallbackLikelihood().getLogLikelihood(); |
| 675 | // sb.append("traitDataLikelihood = " + logInc + "\n"); |
| 676 | // logComponents += logInc; |
| 677 | } |
| 678 | |
| 679 | sb.append("logLikelihood = " + getLogLikelihood()+ "\n"); |
| 680 | |
| 681 | if (logComponents != 0.0) { |
| 682 | sb.append("total likelihood = " + (getLogLikelihood() + logComponents) + "\n"); |
| 683 | } |
| 684 | |
| 685 | return sb.toString(); |
| 686 | } |
| 687 | } |