public double[][] simulateLocations() {
    NodeRef root = m_tree.getRoot();

    // assume uniform
    double[][] latLongs = new double[m_tree.getNodeCount()][2];
    double rootLat = MathUtils.nextDouble() * (maxLat - minLat) + minLat;
    double rootLong = MathUtils.nextDouble() * (maxLong - minLong) + minLong;
    int rootNum = root.getNumber();
    latLongs[rootNum][LATITUDE_INDEX] = rootLat;
    latLongs[rootNum][LONGITUDE_INDEX] = rootLong;
    traverse(root, latLongs[rootNum], latLongs);

    return latLongs;
  }
Ejemplo n.º 2
0
 @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
 }
Ejemplo n.º 3
0
  public static void checkTree(TreeModel treeModel) {

    // todo Should only be run if there exists a zero-length interval

    //        TreeModel treeModel = (TreeModel) tree;
    for (int i = 0; i < treeModel.getInternalNodeCount(); i++) {
      NodeRef node = treeModel.getInternalNode(i);
      if (node != treeModel.getRoot()) {
        double parentHeight = treeModel.getNodeHeight(treeModel.getParent(node));
        double childHeight0 = treeModel.getNodeHeight(treeModel.getChild(node, 0));
        double childHeight1 = treeModel.getNodeHeight(treeModel.getChild(node, 1));
        double maxChild = childHeight0;
        if (childHeight1 > maxChild) maxChild = childHeight1;
        double newHeight = maxChild + MathUtils.nextDouble() * (parentHeight - maxChild);
        treeModel.setNodeHeight(node, newHeight);
      }
    }
    treeModel.pushTreeChangedEvent();
  }
  /** change the parameter and return the hastings ratio. */
  public final double doOperation() throws OperatorFailedException {

    final double scale =
        (scaleFactor + (MathUtils.nextDouble() * ((1.0 / scaleFactor) - scaleFactor)));
    int goingUp = 0, goingDown = 0;

    if (upParameter != null) {
      for (Scalable.Default up : upParameter) {
        goingUp += up.scaleAllAndNotify(scale, -1);
      }
    }

    if (downParameter != null) {
      for (Scalable.Default dn : downParameter) {
        goingDown += dn.scaleAllAndNotify(1.0 / scale, -1);
      }
    }

    return (goingUp - goingDown - 2) * Math.log(scale);
  }
Ejemplo n.º 5
0
  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();
    }
  }
Ejemplo n.º 6
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();
  }