public PopsIOSpeciesTreeModel( PopsIOSpeciesBindings piosb, Parameter popPriorScale, PriorComponent[] priorComponents) { super(PopsIOSpeciesTreeModelParser.PIO_SPECIES_TREE); this.piosb = piosb; this.popPriorScale = popPriorScale; addVariable(popPriorScale); popPriorScale.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1)); this.priorComponents = priorComponents; PopsIOSpeciesBindings.SpInfo[] species = piosb.getSpecies(); int nTaxa = species.length; int nNodes = 2 * nTaxa - 1; pionodes = new PopsIONode[nNodes]; for (int n = 0; n < nNodes; n++) { pionodes[n] = new PopsIONode(n); } ArrayList<Integer> tojoin = new ArrayList<Integer>(nTaxa); for (int n = 0; n < nTaxa; n++) { pionodes[n].setTaxon(species[n].name); pionodes[n].setHeight(0.0); pionodes[n].setUnion(piosb.tipUnionFromTaxon(pionodes[n].getTaxon())); tojoin.add(n); } double rate = 1.0; double treeheight = 0.0; for (int i = 0; i < nTaxa - 1; i++) { int numtojoin = tojoin.size(); int j = MathUtils.nextInt(numtojoin); Integer child0 = tojoin.get(j); tojoin.remove(j); int k = MathUtils.nextInt(numtojoin - 1); Integer child1 = tojoin.get(k); tojoin.remove(k); pionodes[nTaxa + i].addChildren(pionodes[child0], pionodes[child1]); pionodes[nTaxa + i].setHeight(treeheight + randomnodeheight(numtojoin * rate)); treeheight = pionodes[nTaxa + i].getHeight(); tojoin.add(nTaxa + i); } rootn = pionodes.length - 1; double scale = 0.99 * piosb.initialMinGeneNodeHeight() / pionodes[rootn].height; scaleAllHeights(scale); pionodes[rootn].fillinUnionsInSubtree(piosb.getSpecies().length); stree = makeSimpleTree(); Logger.getLogger("dr.evomodel.speciation.popsio") .info( "\tConstructing a PopsIO Species Tree Model, please cite:\n" + Citable.Utils.getCitationString(this)); }
public void drawTreeIndex() { // System.err.print("Drawing new tree, (old tree = " + currentTreeIndex); currentTreeIndex = MathUtils.nextInt(trees.length); // System.err.println(") new tree = " + currentTreeIndex); fireModelChanged(new TreeModel.TreeChangedEvent()); }
@Override public double doOperation() throws OperatorFailedException { apspnet.beginNetworkEdit(); double b = (1.0 - scalingFactor) * (1.0 - scalingFactor) / scalingFactor; double c = scalingFactor / (1.0 - scalingFactor); double y = MathUtils.nextDouble(); double s = b * (y + c) * (y + c); int i = MathUtils.nextInt(apspnet.getNumberOfTetraTrees()); apspnet.setOneHybPopValue(i, s * apspnet.getOneHybPopValue(i)); apspnet.endNetworkEdit(); return 0.0; // this way of scaling, with proposal proportional to x^-(1/2) has hastings ratio 1 }
/** change the parameter and return the hastings ratio. */ public double doOperation() throws OperatorFailedException { double[] mean = sccs.getMode(); double[] currentValue = parameter.getParameterValues(); double[] newValue = new double[dim]; Set<Integer> updateSet = new HashSet<Integer>(); if (setSizeMean != -1.0) { final int listLength = Poisson.nextPoisson(setSizeMean); while (updateSet.size() < listLength) { int newInt = MathUtils.nextInt(parameter.getDimension()); if (!updateSet.contains(newInt)) { updateSet.add(newInt); } } } else { for (int i = 0; i < dim; ++i) { updateSet.add(i); } } double logq = 0; for (Integer i : updateSet) { newValue[i] = mean[i] + scaleFactor * MathUtils.nextGaussian(); if (UPDATE_ALL) { parameter.setParameterValueQuietly(i, newValue[i]); } else { parameter.setParameterValue(i, newValue[i]); } logq += (NormalDistribution.logPdf(currentValue[i], mean[i], scaleFactor) - NormalDistribution.logPdf(newValue[i], mean[i], scaleFactor)); } // for (Integer i : updateSet) { // parameter.setParameterValueQuietly(i, newValue[i]); // } if (UPDATE_ALL) { parameter.setParameterValueNotifyChangedAll(0, parameter.getParameterValue(0)); } return logq; }
public void proposeTree() throws OperatorFailedException { TreeModel tree = c2cLikelihood.getTreeModel(); BranchMapModel branchMap = c2cLikelihood.getBranchMap(); NodeRef i; double oldMinAge, newMinAge, newRange, oldRange, newAge, q; // choose a random node avoiding root, and nodes that are ineligible for this move because they // have nowhere to // go final int nodeCount = tree.getNodeCount(); do { i = tree.getNode(MathUtils.nextInt(nodeCount)); } while (tree.getRoot() == i || !eligibleForMove(i, tree, branchMap)); final NodeRef iP = tree.getParent(i); // this one can go anywhere NodeRef j = tree.getNode(MathUtils.nextInt(tree.getNodeCount())); NodeRef k = tree.getParent(j); while ((k != null && tree.getNodeHeight(k) <= tree.getNodeHeight(i)) || (i == j)) { j = tree.getNode(MathUtils.nextInt(tree.getNodeCount())); k = tree.getParent(j); } if (iP == tree.getRoot() || j == tree.getRoot()) { throw new OperatorFailedException("Root changes not allowed!"); } if (k == iP || j == iP || k == i) throw new OperatorFailedException("move failed"); final NodeRef CiP = getOtherChild(tree, iP, i); NodeRef PiP = tree.getParent(iP); newMinAge = Math.max(tree.getNodeHeight(i), tree.getNodeHeight(j)); newRange = tree.getNodeHeight(k) - newMinAge; newAge = newMinAge + (MathUtils.nextDouble() * newRange); oldMinAge = Math.max(tree.getNodeHeight(i), tree.getNodeHeight(CiP)); oldRange = tree.getNodeHeight(PiP) - oldMinAge; q = newRange / Math.abs(oldRange); // need to account for the random repainting of iP if (branchMap.get(PiP.getNumber()) != branchMap.get(CiP.getNumber())) { q *= 0.5; } if (branchMap.get(k.getNumber()) != branchMap.get(j.getNumber())) { q *= 2; } tree.beginTreeEdit(); if (j == tree.getRoot()) { // 1. remove edges <iP, CiP> tree.removeChild(iP, CiP); tree.removeChild(PiP, iP); // 2. add edges <k, iP>, <iP, j>, <PiP, CiP> tree.addChild(iP, j); tree.addChild(PiP, CiP); // iP is the new root tree.setRoot(iP); } else if (iP == tree.getRoot()) { // 1. remove edges <k, j>, <iP, CiP>, <PiP, iP> tree.removeChild(k, j); tree.removeChild(iP, CiP); // 2. add edges <k, iP>, <iP, j>, <PiP, CiP> tree.addChild(iP, j); tree.addChild(k, iP); // CiP is the new root tree.setRoot(CiP); } else { // 1. remove edges <k, j>, <iP, CiP>, <PiP, iP> tree.removeChild(k, j); tree.removeChild(iP, CiP); tree.removeChild(PiP, iP); // 2. add edges <k, iP>, <iP, j>, <PiP, CiP> tree.addChild(iP, j); tree.addChild(k, iP); tree.addChild(PiP, CiP); } tree.setNodeHeight(iP, newAge); tree.endTreeEdit(); // logq = Math.log(q); // repaint the parent to match either its new parent or its new child (50% chance of each). if (MathUtils.nextInt(2) == 0) { branchMap.set(iP.getNumber(), branchMap.get(k.getNumber()), true); } else { branchMap.set(iP.getNumber(), branchMap.get(j.getNumber()), true); } if (DEBUG) { c2cLikelihood.checkPartitions(); } }
/** change the parameter and return the hastings ratio. */ public final double doOperation() { int index = MathUtils.nextInt(links.getDimension()); int oldGroup = (int) assignments.getParameterValue(index); /* * Set index customer link to index and all connected to it to a new assignment (min value empty) */ int minEmp = minEmpty(modelLikelihood.getLogLikelihoodsVector()); links.setParameterValue(index, index); int[] visited = connected(index, links); int ii = 0; while (visited[ii] != 0) { assignments.setParameterValue(visited[ii] - 1, minEmp); ii++; } /* * Adjust likvector for group separated */ modelLikelihood.setLogLikelihoodsVector(oldGroup, getLogLikGroup(oldGroup)); modelLikelihood.setLogLikelihoodsVector(minEmp, getLogLikGroup(minEmp)); int maxFull = maxFull(modelLikelihood.getLogLikelihoodsVector()); double[] liks = modelLikelihood.getLogLikelihoodsVector(); /* * computing likelihoods of joint groups */ double[] crossedLiks = new double[maxFull + 1]; for (int ll = 0; ll < maxFull + 1; ll++) { if (ll != minEmp) { crossedLiks[ll] = getLogLik2Group(ll, minEmp); } } /* * Add logPrior */ double[] logP = new double[links.getDimension()]; for (int jj = 0; jj < links.getDimension(); jj++) { logP[jj] += depMatrix[index][jj]; int n = (int) assignments.getParameterValue(jj); if (n != minEmp) { logP[jj] += crossedLiks[n] - liks[n] - liks[minEmp]; } } logP[index] = Math.log(chiParameter.getParameterValue(0)); /* * possibilidade de mandar p zero as probs muito pequenas */ /* * Gibbs sampling */ this.rescale(logP); // Improve numerical stability this.exp(logP); // Transform back to probability-scale int k = MathUtils.randomChoicePDF(logP); links.setParameterValue(index, k); int newGroup = (int) assignments.getParameterValue(k); ii = 0; while (visited[ii] != 0) { assignments.setParameterValue(visited[ii] - 1, newGroup); ii++; } /* * updating conditional likelihood vector */ modelLikelihood.setLogLikelihoodsVector(newGroup, getLogLikGroup(newGroup)); if (newGroup != minEmp) { modelLikelihood.setLogLikelihoodsVector(minEmp, 0); } sampleMeans(maxFull); return 0.0; }
public void operateOneNode(final double factor) throws OperatorFailedException { // #print "operate: tree", ut.treerep(t) // if( verbose) System.out.println(" Mau at start: " + tree.getSimpleTree()); final int count = multree.getExternalNodeCount(); assert count == species.nSpSeqs(); NodeRef[] order = new NodeRef[2 * count - 1]; boolean[] swapped = new boolean[count - 1]; mauCanonical(multree, order, swapped); // internal node to change // count-1 - number of internal nodes int which = MathUtils.nextInt(count - 1); FixedBitSet left = new FixedBitSet(count); FixedBitSet right = new FixedBitSet(count); for (int k = 0; k < 2 * which + 1; k += 2) { left.set(multree.speciesIndex(order[k])); } for (int k = 2 * (which + 1); k < 2 * count; k += 2) { right.set(multree.speciesIndex(order[k])); } double newHeight; if (factor > 0) { newHeight = multree.getNodeHeight(order[2 * which + 1]) * factor; } else { final double limit = species.speciationUpperBound(left, right); newHeight = MathUtils.nextDouble() * limit; } multree.beginTreeEdit(); multree.setPreorderIndices(preOrderIndexBefore); final NodeRef node = order[2 * which + 1]; multree.setNodeHeight(node, newHeight); mauReconstruct(multree, order, swapped); // restore pre-order of pops - { multree.setPreorderIndices(preOrderIndexAfter); double[] splitPopValues = null; for (int k = 0; k < preOrderIndexBefore.length; ++k) { final int b = preOrderIndexBefore[k]; if (b >= 0) { final int a = preOrderIndexAfter[k]; if (a != b) { // if( verbose) System.out.println("pops: " + a + " <- " + b); final Parameter p1 = multree.sppSplitPopulations; if (splitPopValues == null) { splitPopValues = p1.getParameterValues(); } if (multree.constPopulation()) { p1.setParameterValue(count + a, splitPopValues[count + b]); } else { for (int i = 0; i < 2; ++i) { p1.setParameterValue(count + 2 * a + i, splitPopValues[count + 2 * b + i]); } } } } } } multree.endTreeEdit(); }