use of beast.evolution.likelihood.LikelihoodCore in project bacter by tgvaughan.
the class ACGLikelihood method traverseNoRecurse.
/**
* Traverse a marginal tree, computing partial likelihoods on the way.
* This version avoids potentially-expensive recursive function calls.
*
* @param root Tree node
* @param region region
*/
void traverseNoRecurse(MarginalNode root, Region region) {
computePostOrder(root);
LikelihoodCore lhc = likelihoodCores.get(region);
for (MarginalNode node : postOrderNodes) {
if (!node.isRoot()) {
lhc.setNodeMatrixForUpdate(node.getNr());
boolean cfEdge = node.cfNodeNr >= 0 && !acg.getNode(node.cfNodeNr).isRoot() && acg.getNode(node.cfNodeNr).getParent().getNr() == ((MarginalNode) node.getParent()).cfNodeNr;
if (!cfEdge) {
cacheMisses += 1;
for (int i = 0; i < siteModel.getCategoryCount(); i++) {
double jointBranchRate = siteModel.getRateForCategory(i, node) * branchRateModel.getRateForBranch(node);
double parentHeight = node.getParent().getHeight();
double nodeHeight = node.getHeight();
substitutionModel.getTransitionProbabilities(node, parentHeight, nodeHeight, jointBranchRate, probabilities);
lhc.setNodeMatrix(node.getNr(), i, probabilities);
}
} else {
cacheHits += 1;
for (int i = 0; i < siteModel.getCategoryCount(); i++) {
lhc.setNodeMatrix(node.getNr(), i, cfTransitionProbs[node.cfNodeNr][i]);
}
}
}
if (!node.isLeaf()) {
// LikelihoodCore only supports binary trees.
List<Node> children = node.getChildren();
lhc.setNodePartialsForUpdate(node.getNr());
lhc.setNodeStatesForUpdate(node.getNr());
lhc.calculatePartials(children.get(0).getNr(), children.get(1).getNr(), node.getNr());
if (node.isRoot()) {
double[] frequencies = substitutionModel.getFrequencies();
double[] proportions = siteModel.getCategoryProportions(node);
lhc.integratePartials(node.getNr(), proportions, rootPartials.get(region));
for (int idx : constantPatterns.get(region)) {
rootPartials.get(region)[idx] += siteModel.getProportionInvariant();
}
lhc.calculateLogLikelihoods(rootPartials.get(region), frequencies, patternLogLikelihoods.get(region));
}
}
}
}
use of beast.evolution.likelihood.LikelihoodCore in project bacter by tgvaughan.
the class ACGLikelihood method calculateLogP.
@Override
public double calculateLogP() {
while (true) {
doLogPCalculation();
if (logP > Double.NEGATIVE_INFINITY || scaleFactor >= maxScaleFactor)
return logP;
scaleFactor *= scaleFactorMultiplier;
Log.warning.println("Turning on scaling to prevent numeric instability. Scale factor: " + scaleFactor);
for (LikelihoodCore core : likelihoodCores.values()) core.setUseScaling(scaleFactor);
requiresRecalculation();
}
}
use of beast.evolution.likelihood.LikelihoodCore in project bacter by tgvaughan.
the class ACGLikelihood method updateCores.
/**
* Initialize likelihood cores.
*/
private void updateCores() {
List<Region> regionList = acg.getRegions(locus);
likelihoodCores.keySet().retainAll(regionList);
for (Region region : regionList) {
if (likelihoodCores.containsKey(region))
continue;
LikelihoodCore likelihoodCore;
if (nStates == 4)
likelihoodCore = new BeerLikelihoodCore4();
else
likelihoodCore = new BeerLikelihoodCore(nStates);
likelihoodCores.put(region, likelihoodCore);
likelihoodCore.initialize(acg.getNodeCount(), patterns.get(region).elementSet().size(), siteModel.getCategoryCount(), true, useAmbiguitiesInput.get());
if (scaleFactor > 1.0)
likelihoodCore.setUseScaling(scaleFactor);
if (useAmbiguitiesInput.get())
setPartials(likelihoodCore, patterns.get(region));
else
setStates(likelihoodCore, patterns.get(region));
int intNodeCount = acg.getNodeCount() / 2;
for (int i = 0; i < intNodeCount; i++) likelihoodCore.createNodePartials(intNodeCount + 1 + i);
}
}
Aggregations