| 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.treedatalikelihood.continuous; |
| 27 |
|
| 28 |
import dr.evolution.tree.MutableTreeModel; |
| 29 |
import dr.evolution.tree.Tree; |
| 30 |
import dr.evomodel.treedatalikelihood.continuous.cdi.PrecisionType; |
| 31 |
import dr.evomodelxml.treelikelihood.TreeTraitParserUtilities; |
| 32 |
import dr.inference.model.*; |
| 33 |
import dr.math.KroneckerOperation; |
| 34 |
import dr.math.distributions.MultivariateNormalDistribution; |
| 35 |
import dr.math.matrixAlgebra.IllegalDimension; |
| 36 |
import dr.math.matrixAlgebra.Matrix; |
| 37 |
import dr.math.matrixAlgebra.Vector; |
| 38 |
import dr.math.matrixAlgebra.WrappedVector; |
| 39 |
import dr.math.matrixAlgebra.missingData.InversionResult; |
| 40 |
import dr.xml.*; |
| •41 |
import org.ejml.data.DMatrixRMaj; |
| 42 |
|
| 43 |
import java.util.ArrayList; |
| 44 |
import java.util.Arrays; |
| 45 |
import java.util.List; |
| 46 |
|
| 47 |
import static dr.math.matrixAlgebra.missingData.MissingOps.safeInvert; |
| 48 |
import static dr.math.matrixAlgebra.missingData.MissingOps.safeSolve; |
| 49 |
import static dr.math.matrixAlgebra.missingData.MissingOps.unwrap; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public class IntegratedFactorAnalysisLikelihood extends AbstractModelLikelihood |
| 56 |
implements ContinuousTraitPartialsProvider, Reportable { |
| 57 |
|
| 58 |
public IntegratedFactorAnalysisLikelihood(String name, |
| 59 |
CompoundParameter traitParameter, |
| 60 |
List<Integer> missingIndices, |
| 61 |
MatrixParameterInterface loadings, |
| 62 |
Parameter traitPrecision, |
| 63 |
double nuggetPrecision) { |
| 64 |
super(name); |
| 65 |
|
| 66 |
this.traitParameter = traitParameter; |
| 67 |
this.loadings = loadings; |
| 68 |
this.traitPrecision = traitPrecision; |
| 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 |
} |
| 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 |
} |
| 124 |
|
| 125 |
@Override |
| 126 |
public List<Integer> getMissingIndices() { |
| 127 |
return missingFactorIndices; |
| 128 |
} |
| 129 |
|
| 130 |
@Override |
| 131 |
public CompoundParameter getParameter() { |
| 132 |
return traitParameter; |
| 133 |
} |
| 134 |
|
| 135 |
@Override |
| 136 |
public Model getModel() { |
| 137 |
return this; |
| 138 |
} |
| 139 |
|
| 140 |
@Override |
| 141 |
public double getLogLikelihood() { |
| 142 |
if (!likelihoodKnown) { |
| 143 |
logLikelihood = calculateLogLikelihood(); |
| 144 |
likelihoodKnown = true; |
| 145 |
} |
| 146 |
return logLikelihood; |
| 147 |
} |
| 148 |
|
| 149 |
@Override |
| 150 |
public void makeDirty() { |
| 151 |
likelihoodKnown = false; |
| 152 |
} |
| 153 |
|
| 154 |
@Override |
| 155 |
protected void handleModelChangedEvent(Model model, Object object, int index) { |
| 156 |
|
| 157 |
} |
| 158 |
|
| 159 |
@Override |
| 160 |
protected void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) { |
| 161 |
if (variable == loadings || variable == traitPrecision) { |
| 162 |
statisticsKnown = false; |
| 163 |
likelihoodKnown = false; |
| 164 |
fireModelChanged(this); |
| 165 |
|
| 166 |
} else if (variable == traitParameter) { |
| 167 |
statisticsKnown = false; |
| 168 |
likelihoodKnown = false; |
| 169 |
fireModelChanged(this); |
| 170 |
} else { |
| 171 |
throw new RuntimeException("Unhandled parameter change type"); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
@Override |
| 176 |
protected void storeState() { |
| 177 |
storedLogLikelihood = logLikelihood; |
| 178 |
storedLikelihoodKnow = likelihoodKnown; |
| 179 |
storedStatisticsKnown = statisticsKnown; |
| 180 |
|
| 181 |
System.arraycopy(partials, 0, storedPartials, 0, partials.length); |
| 182 |
System.arraycopy(normalizationConstants, 0, storedNormalizationConstants, 0, normalizationConstants.length); |
| 183 |
} |
| 184 |
|
| 185 |
@Override |
| 186 |
protected void restoreState() { |
| 187 |
logLikelihood = storedLogLikelihood; |
| 188 |
likelihoodKnown = storedLikelihoodKnow; |
| 189 |
statisticsKnown = storedStatisticsKnown; |
| 190 |
|
| 191 |
double[] tmp1 = partials; |
| 192 |
partials = storedPartials; |
| 193 |
storedPartials = tmp1; |
| 194 |
|
| 195 |
double[] tmp2 = normalizationConstants; |
| 196 |
normalizationConstants = storedNormalizationConstants; |
| 197 |
storedNormalizationConstants = tmp2; |
| 198 |
} |
| 199 |
|
| 200 |
@Override |
| 201 |
protected void acceptState() { |
| 202 |
|
| 203 |
} |
| 204 |
|
| 205 |
private double calculateLogLikelihood() { |
| 206 |
|
| 207 |
checkStatistics(); |
| 208 |
|
| 209 |
double logLikelihood = 0.0; |
| 210 |
for (double r : normalizationConstants) { |
| 211 |
logLikelihood += r; |
| 212 |
} |
| 213 |
return logLikelihood; |
| 214 |
} |
| 215 |
|
| 216 |
private void setupStatistics() { |
| 217 |
if (partials == null) { |
| 218 |
partials = new double[numTaxa * dimPartial]; |
| 219 |
storedPartials = new double[numTaxa * dimPartial]; |
| 220 |
} |
| 221 |
|
| 222 |
if (normalizationConstants == null) { |
| 223 |
normalizationConstants = new double[numTaxa]; |
| 224 |
storedNormalizationConstants = new double[numTaxa]; |
| 225 |
} |
| 226 |
|
| 227 |
computePartialsAndRemainders(); |
| 228 |
} |
| 229 |
|
| 230 |
private final double nuggetPrecision; |
| 231 |
|
| •232 |
private void computePrecisionForTaxon(final DMatrixRMaj precision, final int taxon, |
| 233 |
final int numFactors) { |
| 234 |
|
| 235 |
final double[] observed = observedIndicators[taxon]; |
| 236 |
|
| 237 |
|
| 238 |
for (int row = 0; row < numFactors; ++row) { |
| 239 |
for (int col = 0; col < numFactors; ++col) { |
| 240 |
double sum = 0; |
| 241 |
for (int k = 0; k < dimTrait; ++k) { |
| 242 |
double thisPrecision = (observed[k] == 1.0) ? traitPrecision.getParameterValue(k) : nuggetPrecision; |
| 243 |
sum += loadings.getParameterValue(k, row) * |
| 244 |
thisPrecision * |
| 245 |
loadings.getParameterValue(k, col); |
| 246 |
} |
| 247 |
precision.unsafe_set(row, col, sum); |
| 248 |
} |
| 249 |
} |
| 250 |
} |
| 251 |
|
| •252 |
private InversionResult fillInMeanForTaxon(final WrappedVector output, final DMatrixRMaj precision, final int taxon) { |
| 253 |
|
| 254 |
final double[] observed = observedIndicators[taxon]; |
| 255 |
final Parameter Y = traitParameter.getParameter(taxon); |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
final double[] tmp = new double[numFactors]; |
| 260 |
final double[] tmp2 = new double[numFactors]; |
| 261 |
|
| 262 |
for (int row = 0; row < numFactors; ++row) { |
| 263 |
double sum = 0; |
| 264 |
for (int k = 0; k < dimTrait; ++k) { |
| 265 |
sum += loadings.getParameterValue(k, row) * |
| 266 |
observed[k] * traitPrecision.getParameterValue(k) * |
| 267 |
Y.getParameterValue(k); |
| 268 |
} |
| 269 |
tmp[row] = sum; |
| 270 |
} |
| 271 |
|
| •272 |
DMatrixRMaj B = DMatrixRMaj.wrap(numFactors, 1, tmp); |
| 273 |
DMatrixRMaj X = DMatrixRMaj.wrap(numFactors, 1, tmp2); |
| 274 |
|
| 275 |
InversionResult ci = safeSolve(precision, B, X, true); |
| 276 |
|
| 277 |
for (int row = 0; row < numFactors; ++row) { |
| 278 |
output.set(row, X.unsafe_get(row, 0)); |
| 279 |
} |
| 280 |
|
| 281 |
return ci; |
| 282 |
} |
| 283 |
|
| 284 |
private double computeTraitInnerProduct(final int taxon) { |
| 285 |
final double[] observed = observedIndicators[taxon]; |
| 286 |
final Parameter Y = traitParameter.getParameter(taxon); |
| 287 |
|
| 288 |
|
| 289 |
double sum = 0; |
| 290 |
for (int k = 0; k < dimTrait; ++k) { |
| 291 |
sum += Y.getParameterValue(k) * Y.getParameterValue(k) * |
| 292 |
observed[k] * traitPrecision.getParameterValue(k); |
| 293 |
} |
| 294 |
return sum; |
| 295 |
} |
| 296 |
|
| •297 |
private double computeFactorInnerProduct(final WrappedVector mean, final DMatrixRMaj precision) { |
| 298 |
|
| 299 |
double sum = 0; |
| 300 |
for (int row = 0; row < numFactors; ++row) { |
| 301 |
for (int col = 0; col < numFactors; ++col) { |
| 302 |
sum += mean.get(row) * precision.unsafe_get(row, col) * mean.get(col); |
| 303 |
} |
| 304 |
} |
| 305 |
return sum; |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
private double getTraitLogDeterminant(final int taxon) { |
| 324 |
|
| 325 |
final double[] observed = observedIndicators[taxon]; |
| 326 |
|
| 327 |
|
| 328 |
double logDet = 0.0; |
| 329 |
for (int k = 0; k < dimTrait; ++k) { |
| 330 |
if (observed[k] == 1.0) { |
| 331 |
logDet += Math.log(traitPrecision.getParameterValue(k)); |
| 332 |
} |
| 333 |
} |
| 334 |
return logDet; |
| 335 |
} |
| 336 |
|
| •337 |
private void makeCompletedUnobserved(final DMatrixRMaj matrix, double diagonal) { |
| 338 |
for (int row = 0; row < numFactors; ++row) { |
| 339 |
for (int col = 0; col < numFactors; ++col) { |
| 340 |
double x = (row == col) ? diagonal : 0.0; |
| 341 |
matrix.unsafe_set(row, col, x); |
| 342 |
} |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
private void computePartialsAndRemainders() { |
| 347 |
|
| •348 |
final DMatrixRMaj precision = new DMatrixRMaj(numFactors, numFactors); |
| 349 |
final DMatrixRMaj variance = new DMatrixRMaj(numFactors, numFactors); |
| 350 |
|
| 351 |
int partialsOffset = 0; |
| 352 |
for (int taxon = 0; taxon < numTaxa; ++taxon) { |
| 353 |
|
| 354 |
|
| 355 |
final WrappedVector mean = new WrappedVector.Raw(partials, partialsOffset, numFactors); |
| 356 |
|
| 357 |
computePrecisionForTaxon(precision, taxon, numFactors); |
| 358 |
InversionResult ci = fillInMeanForTaxon(mean, precision, taxon); |
| 359 |
|
| 360 |
if (DEBUG) { |
| 361 |
System.err.println("taxon " + taxon); |
| 362 |
System.err.println("\tprecision: " + precision); |
| 363 |
} |
| 364 |
|
| 365 |
double constant; |
| 366 |
double nuggetDensity = 0; |
| 367 |
|
| 368 |
if (observedDimensions[taxon] == 0) { |
| 369 |
|
| 370 |
makeCompletedUnobserved(precision, 0); |
| 371 |
makeCompletedUnobserved(variance, Double.POSITIVE_INFINITY); |
| 372 |
constant = 0.0; |
| 373 |
|
| 374 |
} else { |
| 375 |
|
| 376 |
|
| 377 |
if (DEBUG) { |
| 378 |
System.err.println("\tmean: " + mean); |
| 379 |
|
| 380 |
} |
| 381 |
|
| 382 |
final double factorLogDeterminant = Math.log(ci.getDeterminant()); |
| 383 |
double traitLogDeterminant = getTraitLogDeterminant(taxon); |
| 384 |
|
| 385 |
|
| 386 |
final double logDetChange = traitLogDeterminant - factorLogDeterminant; |
| 387 |
|
| 388 |
final double factorInnerProduct = computeFactorInnerProduct(mean, precision); |
| 389 |
final double traitInnerProduct = computeTraitInnerProduct(taxon); |
| 390 |
final double innerProductChange = traitInnerProduct - factorInnerProduct; |
| 391 |
|
| 392 |
int dimensionChange = observedDimensions[taxon] - ci.getEffectiveDimension(); |
| 393 |
|
| 394 |
if (DEBUG) { |
| 395 |
System.err.println("fIP: " + factorInnerProduct); |
| 396 |
System.err.println("tIP: " + traitInnerProduct); |
| 397 |
System.err.println("fDet: " + factorLogDeterminant); |
| 398 |
System.err.println("tDet: " + traitLogDeterminant); |
| 399 |
System.err.println("deltaDim: " + dimensionChange + " deltaIP: " + innerProductChange + |
| 400 |
"\n\n"); |
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
} |
| 407 |
|
| 408 |
constant = 0.5 * (logDetChange - innerProductChange) - LOG_SQRT_2_PI * (dimensionChange) - |
| 409 |
nuggetDensity; |
| 410 |
|
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
unwrap(precision, partials, partialsOffset + numFactors); |
| 415 |
|
| 416 |
if (STORE_VARIANCE) { |
| 417 |
safeInvert(precision, variance, true); |
| 418 |
unwrap(variance, partials, partialsOffset + numFactors + numFactors * numFactors); |
| 419 |
} |
| 420 |
|
| 421 |
normalizationConstants[taxon] = constant; |
| 422 |
|
| 423 |
partialsOffset += dimPartial; |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
private static final boolean STORE_VARIANCE = false; |
| 428 |
private static final boolean DEBUG = false; |
| 429 |
|
| 430 |
private void checkStatistics() { |
| 431 |
if (!statisticsKnown) { |
| 432 |
setupStatistics(); |
| 433 |
statisticsKnown = true; |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
private static double[][] setupObservedIndicators(List<Integer> missingIndices, int nTaxa, int dimTrait) { |
| 438 |
double[][] observed = new double[nTaxa][dimTrait]; |
| 439 |
|
| 440 |
for (double[] v : observed) { |
| 441 |
Arrays.fill(v, 1.0); |
| 442 |
} |
| 443 |
|
| 444 |
if (missingIndices != null) { |
| 445 |
for (Integer idx : missingIndices) { |
| 446 |
int taxon = idx / dimTrait; |
| 447 |
int trait = idx % dimTrait; |
| 448 |
observed[taxon][trait] = 0.0; |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
return observed; |
| 453 |
} |
| 454 |
|
| 455 |
private static int[] setupObservedDimensions(double[][] observed) { |
| 456 |
int length = observed.length; |
| 457 |
int[] dimensions = new int[length]; |
| 458 |
|
| 459 |
for (int i = 0; i < length; ++i) { |
| 460 |
double sum = 0; |
| 461 |
for (double x : observed[i]) { |
| 462 |
sum += x; |
| 463 |
} |
| 464 |
dimensions[i] = (int) sum; |
| 465 |
} |
| 466 |
|
| 467 |
return dimensions; |
| 468 |
} |
| 469 |
|
| 470 |
private boolean likelihoodKnown = false; |
| 471 |
private boolean storedLikelihoodKnow; |
| 472 |
private boolean statisticsKnown = false; |
| 473 |
private boolean storedStatisticsKnown; |
| 474 |
private double logLikelihood; |
| 475 |
private double storedLogLikelihood; |
| 476 |
|
| 477 |
private double[] partials; |
| 478 |
private double[] storedPartials; |
| 479 |
|
| 480 |
private double[] normalizationConstants; |
| 481 |
private double[] storedNormalizationConstants; |
| 482 |
|
| 483 |
private final int numTaxa; |
| 484 |
private final int dimTrait; |
| 485 |
private final int dimPartial; |
| 486 |
private final int numFactors; |
| 487 |
private final CompoundParameter traitParameter; |
| 488 |
private final MatrixParameterInterface loadings; |
| 489 |
private final Parameter traitPrecision; |
| 490 |
private final List<Integer> missingFactorIndices; |
| 491 |
|
| 492 |
private final double[][] observedIndicators; |
| 493 |
private final int[] observedDimensions; |
| 494 |
|
| 495 |
private static double LOG_SQRT_2_PI = 0.5 * Math.log(2 * Math.PI); |
| 496 |
|
| 497 |
|
| 498 |
public static AbstractXMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 499 |
@Override |
| 500 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 501 |
|
| 502 |
MutableTreeModel treeModel = (MutableTreeModel) xo.getChild(MutableTreeModel.class); |
| 503 |
TreeTraitParserUtilities utilities = new TreeTraitParserUtilities(); |
| 504 |
|
| 505 |
TreeTraitParserUtilities.TraitsAndMissingIndices returnValue = |
| 506 |
utilities.parseTraitsFromTaxonAttributes(xo, TreeTraitParserUtilities.DEFAULT_TRAIT_NAME, |
| 507 |
treeModel, true); |
| 508 |
CompoundParameter traitParameter = returnValue.traitParameter; |
| 509 |
List<Integer> missingIndices = returnValue.missingIndices; |
| 510 |
|
| 511 |
MatrixParameterInterface loadings = (MatrixParameterInterface) xo.getElementFirstChild(LOADINGS); |
| 512 |
Parameter traitPrecision = (Parameter) xo.getElementFirstChild(PRECISION); |
| 513 |
|
| 514 |
double nugget = xo.getAttribute(NUGGET, 0.0); |
| 515 |
|
| 516 |
return new IntegratedFactorAnalysisLikelihood(xo.getId(), traitParameter, missingIndices, |
| 517 |
loadings, traitPrecision, nugget); |
| 518 |
} |
| 519 |
|
| 520 |
@Override |
| 521 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 522 |
return rules; |
| 523 |
} |
| 524 |
|
| 525 |
@Override |
| 526 |
public String getParserDescription() { |
| 527 |
return null; |
| 528 |
} |
| 529 |
|
| 530 |
@Override |
| 531 |
public Class getReturnType() { |
| 532 |
return IntegratedFactorAnalysisLikelihood.class; |
| 533 |
} |
| 534 |
|
| 535 |
@Override |
| 536 |
public String getParserName() { |
| 537 |
return INTEGRATED_FACTOR_Model; |
| 538 |
} |
| 539 |
}; |
| 540 |
|
| 541 |
private static final String INTEGRATED_FACTOR_Model = "integratedFactorModel"; |
| 542 |
private static final String LOADINGS = "loadings"; |
| 543 |
private static final String PRECISION = "precision"; |
| 544 |
private static final String NUGGET = "nugget"; |
| 545 |
|
| 546 |
private final static XMLSyntaxRule[] rules = new XMLSyntaxRule[] { |
| 547 |
new ElementRule(LOADINGS, new XMLSyntaxRule[] { |
| 548 |
new ElementRule(MatrixParameterInterface.class), |
| 549 |
}), |
| 550 |
new ElementRule(PRECISION, new XMLSyntaxRule[] { |
| 551 |
new ElementRule(Parameter.class), |
| 552 |
}), |
| 553 |
|
| 554 |
new ElementRule(MutableTreeModel.class), |
| 555 |
AttributeRule.newStringRule(TreeTraitParserUtilities.TRAIT_NAME), |
| 556 |
new ElementRule(TreeTraitParserUtilities.TRAIT_PARAMETER, new XMLSyntaxRule[]{ |
| 557 |
new ElementRule(Parameter.class) |
| 558 |
}), |
| 559 |
new ElementRule(TreeTraitParserUtilities.MISSING, new XMLSyntaxRule[]{ |
| 560 |
new ElementRule(Parameter.class) |
| 561 |
}, true), |
| 562 |
AttributeRule.newDoubleRule(NUGGET, true), |
| 563 |
|
| 564 |
}; |
| 565 |
|
| 566 |
public void setLikelihoodDelegate(ContinuousDataLikelihoodDelegate delegate) { |
| 567 |
this.delegate = delegate; |
| 568 |
} |
| 569 |
|
| 570 |
private ContinuousDataLikelihoodDelegate delegate = null; |
| 571 |
|
| 572 |
private static Matrix buildDiagonalMatrix(double[] diagonals) { |
| 573 |
Matrix mat = new Matrix(diagonals.length, diagonals.length); |
| 574 |
for (int i = 0; i < diagonals.length; ++i) { |
| 575 |
mat.set(i, i, diagonals[i]); |
| 576 |
} |
| 577 |
return mat; |
| 578 |
} |
| 579 |
|
| 580 |
@Override |
| 581 |
public String getReport() { |
| 582 |
|
| 583 |
StringBuilder sb = new StringBuilder(); |
| 584 |
|
| 585 |
double logComponents = 0; |
| 586 |
|
| 587 |
if (delegate != null) { |
| 588 |
|
| 589 |
final Tree tree = delegate.getCallbackLikelihood().getTree(); |
| 590 |
sb.append(tree.toString()); |
| 591 |
sb.append("\n\n"); |
| 592 |
|
| 593 |
final double normalization = delegate.getRateTransformation().getNormalization(); |
| 594 |
final double priorSampleSize = delegate.getRootProcessDelegate().getPseudoObservations(); |
| 595 |
|
| 596 |
double[][] treeStructure = MultivariateTraitDebugUtilities.getTreeVariance(tree, 1.0, Double.POSITIVE_INFINITY); |
| 597 |
sb.append("Tree structure:\n"); |
| 598 |
sb.append(new Matrix(treeStructure)); |
| 599 |
sb.append("\n\n"); |
| 600 |
|
| 601 |
double[][] treeVariance = MultivariateTraitDebugUtilities.getTreeVariance(tree, normalization, priorSampleSize); |
| 602 |
|
| 603 |
Matrix treeV = new Matrix(treeVariance); |
| 604 |
Matrix treeP = treeV.inverse(); |
| 605 |
|
| 606 |
sb.append("Tree variance:\n"); |
| 607 |
sb.append(treeV); |
| 608 |
sb.append("Tree precision:\n"); |
| 609 |
sb.append(treeP); |
| 610 |
sb.append("\n\n"); |
| 611 |
|
| 612 |
Matrix Lt = new Matrix(loadings.getParameterAsMatrix()); |
| 613 |
sb.append("Loadings:\n"); |
| 614 |
sb.append(Lt); |
| 615 |
sb.append("\n\n"); |
| 616 |
Matrix loadingsVariance = null; |
| 617 |
try { |
| 618 |
loadingsVariance = Lt.product(Lt.transpose()); |
| 619 |
} catch (IllegalDimension illegalDimension) { |
| 620 |
illegalDimension.printStackTrace(); |
| 621 |
} |
| 622 |
sb.append("Loadings variance:\n"); |
| 623 |
sb.append(loadingsVariance); |
| 624 |
sb.append("\n\n"); |
| 625 |
|
| 626 |
Matrix gamma = buildDiagonalMatrix(traitPrecision.getParameterValues()); |
| 627 |
sb.append("Trait precision:\n"); |
| 628 |
sb.append(gamma); |
| 629 |
sb.append("\n\n"); |
| 630 |
Matrix gammaVariance = gamma.inverse(); |
| 631 |
|
| 632 |
double[] tmp = new double[tree.getExternalNodeCount()]; |
| 633 |
Arrays.fill(tmp, 1.0); |
| 634 |
Matrix identity = buildDiagonalMatrix(tmp); |
| 635 |
assert loadingsVariance != null; |
| 636 |
Matrix loadingsFactorsVariance = new Matrix(KroneckerOperation.product(treeVariance, loadingsVariance.toComponents())); |
| 637 |
Matrix errorVariance = new Matrix(KroneckerOperation.product(identity.toComponents(), gammaVariance.toComponents())); |
| 638 |
|
| 639 |
sb.append("Loadings-factors variance:\n"); |
| 640 |
sb.append(loadingsFactorsVariance); |
| 641 |
sb.append("\n\n"); |
| 642 |
|
| 643 |
sb.append("Error variance\n"); |
| 644 |
sb.append(errorVariance); |
| 645 |
sb.append("\n\n"); |
| 646 |
|
| 647 |
Matrix totalVariance = null; |
| 648 |
try { |
| 649 |
totalVariance = loadingsFactorsVariance.add(errorVariance); |
| 650 |
} catch (IllegalDimension illegalDimension) { |
| 651 |
illegalDimension.printStackTrace(); |
| 652 |
} |
| 653 |
|
| 654 |
double[] allData = getParameter().getParameterValues(); |
| 655 |
|
| 656 |
List<Integer> notMissing = new ArrayList<Integer>(); |
| 657 |
for (int taxon = 0; taxon < numTaxa; ++taxon) { |
| 658 |
double[] observed = observedIndicators[taxon]; |
| 659 |
for (int trait = 0; trait < dimTrait; ++trait) { |
| 660 |
if (observed[trait] == 0.0) { |
| 661 |
System.err.println("Missing taxon " + taxon + " trait " + trait); |
| 662 |
} else { |
| 663 |
notMissing.add(taxon * dimTrait + trait); |
| 664 |
} |
| 665 |
} |
| 666 |
} |
| 667 |
|
| 668 |
int[] notMissingIndices = new int[notMissing.size()]; |
| 669 |
double[] data = new double[notMissing.size()]; |
| 670 |
for (int i = 0; i < notMissing.size(); ++i) { |
| 671 |
notMissingIndices[i] = notMissing.get(i); |
| 672 |
data[i] = allData[notMissing.get(i)]; |
| 673 |
} |
| 674 |
|
| 675 |
if (totalVariance != null) { |
| 676 |
totalVariance = new Matrix(Matrix.gatherRowsAndColumns(totalVariance.toComponents(), notMissingIndices, notMissingIndices)); |
| 677 |
} |
| 678 |
Matrix totalPrecision = null; |
| 679 |
if (totalVariance != null) { |
| 680 |
totalPrecision = totalVariance.inverse(); |
| 681 |
} |
| 682 |
|
| 683 |
sb.append("Total variance:\n"); |
| 684 |
sb.append(totalVariance); |
| 685 |
sb.append("\n\n"); |
| 686 |
sb.append("Total precision:\n"); |
| 687 |
sb.append(totalPrecision); |
| 688 |
sb.append("\n\n"); |
| 689 |
|
| 690 |
sb.append("Data:\n"); |
| 691 |
sb.append(new Vector(data)); |
| 692 |
sb.append("\n\n"); |
| 693 |
|
| 694 |
MultivariateNormalDistribution mvn = null; |
| 695 |
if (totalPrecision != null) { |
| 696 |
mvn = new MultivariateNormalDistribution(new double[data.length], |
| 697 |
totalPrecision.toComponents()); |
| 698 |
} |
| 699 |
|
| 700 |
double logDensity = 0; |
| 701 |
if (mvn != null) { |
| 702 |
logDensity = mvn.logPdf(data); |
| 703 |
} |
| 704 |
sb.append("logMultiVariateNormalDensity = ").append(logDensity).append("\n\n"); |
| 705 |
|
| 706 |
double logInc = delegate.getCallbackLikelihood().getLogLikelihood(); |
| 707 |
sb.append("traitDataLikelihood = ").append(logInc).append("\n"); |
| 708 |
logComponents += logInc; |
| 709 |
} |
| 710 |
|
| 711 |
sb.append("logLikelihood = ").append(getLogLikelihood()).append("\n"); |
| 712 |
|
| 713 |
if (logComponents != 0.0) { |
| 714 |
sb.append("total likelihood = ").append((getLogLikelihood() + logComponents)).append("\n"); |
| 715 |
} |
| 716 |
|
| 717 |
return sb.toString(); |
| 718 |
} |
| 719 |
} |