public void forwardIBD() { int numNodes = treeModel.getNodeCount(); int stateCount = substitutionModel.getStateCount(); getDiagonalRates(diag); for (int nodeId = 0; nodeId < numNodes; ++nodeId) { NodeRef node = treeModel.getNode(nodeId); NodeRef parent = treeModel.getParent(node); if (parent == null) { // handle the root } else if (treeModel.isExternal(node)) { // Handle the tip double branchTime = branchRateModel.getBranchRate(treeModel, node) * (treeModel.getNodeHeight(parent) - treeModel.getNodeHeight(node)); for (int state = 0; state < stateCount; ++state) { ibdForward[nodeId][state] = Math.exp(-diag[state] * branchTime); } } else { // Handle internal node double branchTime = branchRateModel.getBranchRate(treeModel, node) * (treeModel.getNodeHeight(parent) - treeModel.getNodeHeight(node)); int childCount = treeModel.getChildCount(node); for (int state = 0; state < stateCount; ++state) { ibdForward[nodeId][state] = 0; for (int child = 0; child < childCount; ++child) { int childNodeId = treeModel.getChild(node, child).getNumber(); ibdForward[nodeId][state] += ibdForward[childNodeId][state]; } ibdForward[nodeId][state] *= Math.exp(-diag[state] * branchTime); } } } }
public void backwardIBD(NodeRef node) { int stateCount = substitutionModel.getStateCount(); if (node == null) { node = treeModel.getRoot(); int nodeId = node.getNumber(); for (int state = 0; state < stateCount; ++state) { ibdBackward[nodeId][state] = 0; } } getDiagonalRates(diag); int childCount = treeModel.getChildCount(node); int nodeId = node.getNumber(); for (int child = 0; child < childCount; ++child) { NodeRef childNode = treeModel.getChild(node, child); int childNodeId = childNode.getNumber(); double branchTime = branchRateModel.getBranchRate(treeModel, childNode) * (treeModel.getNodeHeight(node) - treeModel.getNodeHeight(childNode)); for (int state = 0; state < stateCount; ++state) { ibdBackward[childNodeId][state] = ibdBackward[nodeId][state]; for (int sibling = 0; sibling < childCount; ++sibling) { if (sibling != child) { int siblingId = treeModel.getChild(node, sibling).getNumber(); ibdBackward[childNodeId][state] += ibdForward[siblingId][state]; } } ibdBackward[childNodeId][state] *= Math.exp(-diag[state] * branchTime); } } for (int child = 0; child < childCount; ++child) { NodeRef childNode = treeModel.getChild(node, child); backwardIBD(childNode); } }
public static void main(String[] args) { final double l1 = -10; final double l2 = -2; AbstractModelLikelihood like1 = new AbstractModelLikelihood("dummy") { public Model getModel() { return null; } public double getLogLikelihood() { return l1; } public void makeDirty() {} public String prettyName() { return null; } public boolean isUsed() { return false; } @Override protected void handleModelChangedEvent(Model model, Object object, int index) {} @Override protected void handleVariableChangedEvent( Variable variable, int index, Variable.ChangeType type) {} @Override protected void storeState() {} @Override protected void restoreState() {} @Override protected void acceptState() {} public void setUsed() {} public LogColumn[] getColumns() { return new LogColumn[0]; } public String getId() { return null; } public void setId(String id) {} }; AbstractModelLikelihood like2 = new AbstractModelLikelihood("dummy") { public Model getModel() { return null; } public double getLogLikelihood() { return l2; } public void makeDirty() {} public String prettyName() { return null; } public boolean isUsed() { return false; } @Override protected void handleModelChangedEvent(Model model, Object object, int index) {} @Override protected void handleVariableChangedEvent( Variable variable, int index, Variable.ChangeType type) {} @Override protected void storeState() {} @Override protected void restoreState() {} @Override protected void acceptState() {} public void setUsed() {} public LogColumn[] getColumns() { return new LogColumn[0]; } public String getId() { return null; } public void setId(String id) {} }; List<AbstractModelLikelihood> likelihoodList = new ArrayList<AbstractModelLikelihood>(); likelihoodList.add(like1); likelihoodList.add(like2); Parameter weights = new Parameter.Default(2); double p1 = 0.05; weights.setParameterValue(0, p1); weights.setParameterValue(1, 1.0 - p1); WeightedMixtureModel mixture = new WeightedMixtureModel(likelihoodList, weights); System.err.println("getLogLikelihood() = " + mixture.getLogLikelihood()); double test = Math.log(p1 * Math.exp(l1) + (1.0 - p1) * Math.exp(l2)); System.err.println("correct = " + test); }
public void setCoercableParameter(double value) { scaleFactor = Math.exp(value); }
private void exp(double[] logX) { for (int i = 0; i < logX.length; ++i) { logX[i] = Math.exp(logX[i]); // if(logX[i]<1E-5){logX[i]=0;} } }