示例#1
0
  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);
        }
      }
    }
  }
示例#2
0
 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 double getLogLikelihood() {
   double logSum = Double.NEGATIVE_INFINITY;
   for (int i = 0; i < likelihoodList.size(); ++i) {
     double pi = mixtureWeights.getParameterValue(i);
     if (pi > 0.0) {
       logSum = LogTricks.logSum(logSum, Math.log(pi) + likelihoodList.get(i).getLogLikelihood());
     }
   }
   return logSum;
 }
示例#4
0
  // Here is a formula for the area of a polygon with vertices {(xk,yk): k = 1,...,n}:
  //   Area = 1/2 [(x1*y2 - x2*y1) + (x2*y3 - x3*y2) + ... + (xn*y1 - x1*yn)].
  //   This formula appears in an Article by Gil Strang of MIT
  //   on p. 253 of the March 1993 issue of The American Mathematical Monthly, with the note that it
  // is
  //   "known, but not well known". There is also a very brief discussion of proofs and other
  // references,
  //   including an article by Bart Braden of Northern Kentucky U., a known Mathematica enthusiast.
  public double calculateArea() {

    //        rescaleToPositiveCoordinates();

    double area = 0;
    // we can implement it like this because the polygon is closed (point2D.get(0) =
    // point2D.get(length + 1)
    for (int i = 0; i < length; i++) {
      area += (x[i] * y[i + 1] - x[i + 1] * y[i]);
    }

    return (Math.abs(area / 2));
  }
  public DistanceDependentCRPGibbsOperator(
      Parameter links,
      Parameter assignments,
      Parameter chiParameter,
      NPAntigenicLikelihood Likelihood,
      double weight) {

    this.links = links;
    this.assignments = assignments;
    this.modelLikelihood = Likelihood;
    this.chiParameter = chiParameter;
    this.depMatrix = Likelihood.getLogDepMatrix();

    for (int i = 0; i < links.getDimension(); i++) {
      links.setParameterValue(i, i);
    }

    setWeight(weight);

    // double[][] x=modelLikelihood.getData();
    // modelLikelihood.printInformtion(x[0][0]);

    this.m = new double[2];
    m[0] = modelLikelihood.priorMean.getParameterValue(0);
    m[1] = modelLikelihood.priorMean.getParameterValue(1);

    this.v0 = 2;

    this.k0 =
        modelLikelihood.priorPrec.getParameterValue(0)
            / modelLikelihood.clusterPrec.getParameterValue(0);

    this.T0Inv = new double[2][2];
    T0Inv[0][0] = v0 / modelLikelihood.clusterPrec.getParameterValue(0);
    T0Inv[1][1] = v0 / modelLikelihood.clusterPrec.getParameterValue(0);
    T0Inv[1][0] = 0.0;
    T0Inv[0][1] = 0.0;

    this.logDetT0 = -Math.log(T0Inv[0][0] * T0Inv[1][1]);
  }
  public static Transform[] parseListOfTransforms(XMLObject xo, int maxDim)
      throws XMLParseException {
    Transform[] transforms = null;

    boolean anyTransforms = false;
    for (int i = 0; i < xo.getChildCount(); ++i) {
      if (xo.getChild(i) instanceof Transform.ParsedTransform) {
        Transform.ParsedTransform t = (Transform.ParsedTransform) xo.getChild(i);
        if (transforms == null) {
          transforms = Transform.Util.getListOfNoTransforms(maxDim);
        }

        t.end = Math.max(t.end, maxDim);
        if (t.start < 0 || t.end < 0 || t.start > t.end) {
          throw new XMLParseException("Invalid bounds for transform in " + xo.getId());
        }
        for (int j = t.start; j < t.end; j += t.every) {
          transforms[j] = t.transform;
          anyTransforms = true;
        }
      }
    }
    if (anyTransforms) {
      StringBuilder sb =
          new StringBuilder("Using distributional transforms in " + xo.getId() + "\n");
      for (int i = 0; i < transforms.length; ++i) {
        if (transforms[i] != Transform.NONE) {
          sb.append("\t")
              .append(transforms[i].getTransformName())
              .append(" on index ")
              .append(i + 1)
              .append("\n");
        }
      }
      sb.append("Please cite:\n").append(Citable.Utils.getCitationString(Transform.LOG));
      Logger.getLogger("dr.utils.Transform").info(sb.toString());
    }
    return transforms;
  }
  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();
    }
  }
  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);
 }
 public double getCoercableParameter() {
   return Math.log(scaleFactor);
 }
示例#11
0
  private static LinkedList<Point2D> getCirclePoints(
      double centerLat, double centerLong, int numberOfPoints, double radius) {

    LinkedList<Point2D> Point2Ds = new LinkedList<Point2D>();

    double lat1, long1;
    double d_rad;
    double delta_pts;
    double radial, lat_rad, dlon_rad, lon_rad;

    // convert coordinates to radians
    lat1 = Math.toRadians(centerLat);
    long1 = Math.toRadians(centerLong);

    // radius is in meters
    d_rad = radius / 6378137;

    // loop through the array and write points
    for (int i = 0; i <= numberOfPoints; i++) {
      delta_pts = 360 / (double) numberOfPoints;
      radial = Math.toRadians((double) i * delta_pts);

      // This algorithm is limited to distances such that dlon < pi/2
      lat_rad =
          Math.asin(
              Math.sin(lat1) * Math.cos(d_rad)
                  + Math.cos(lat1) * Math.sin(d_rad) * Math.cos(radial));
      dlon_rad =
          Math.atan2(
              Math.sin(radial) * Math.sin(d_rad) * Math.cos(lat1),
              Math.cos(d_rad) - Math.sin(lat1) * Math.sin(lat_rad));
      lon_rad = ((long1 + dlon_rad + Math.PI) % (2 * Math.PI)) - Math.PI;

      Point2Ds.add(new Point2D.Double(Math.toDegrees(lat_rad), Math.toDegrees(lon_rad)));
    }
    return Point2Ds;
  }
  /** 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;
  }
 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;}
   }
 }
  public double getLogLik2Group(int group1, int group2) {
    double L = 0.0;

    int ngroup1 = 0;
    for (int i = 0; i < assignments.getDimension(); i++) {
      if ((int) assignments.getParameterValue(i) == group1) {
        ngroup1++;
      }
    }

    int ngroup2 = 0;
    for (int i = 0; i < assignments.getDimension(); i++) {
      if ((int) assignments.getParameterValue(i) == group2) {
        ngroup2++;
      }
    }

    int ngroup = (ngroup1 + ngroup2);

    if (ngroup != 0) {
      double[][] group = new double[ngroup][2];

      double mean[] = new double[2];

      int count = 0;
      for (int i = 0; i < assignments.getDimension(); i++) {
        if ((int) assignments.getParameterValue(i) == group1) {
          group[count][0] = modelLikelihood.getData()[i][0];
          group[count][1] = modelLikelihood.getData()[i][1];
          mean[0] += group[count][0];
          mean[1] += group[count][1];
          count += 1;
        }
      }

      for (int i = 0; i < assignments.getDimension(); i++) {
        if ((int) assignments.getParameterValue(i) == group2) {
          group[count][0] = modelLikelihood.getData()[i][0];
          group[count][1] = modelLikelihood.getData()[i][1];
          mean[0] += group[count][0];
          mean[1] += group[count][1];
          count += 1;
        }
      }

      mean[0] /= ngroup;
      mean[1] /= ngroup;

      double kn = k0 + ngroup;
      double vn = v0 + ngroup;

      double[][] sumdif = new double[2][2];

      for (int i = 0; i < ngroup; i++) {
        sumdif[0][0] += (group[i][0] - mean[0]) * (group[i][0] - mean[0]);
        sumdif[0][1] += (group[i][0] - mean[0]) * (group[i][1] - mean[1]);
        sumdif[1][0] += (group[i][0] - mean[0]) * (group[i][1] - mean[1]);
        sumdif[1][1] += (group[i][1] - mean[1]) * (group[i][1] - mean[1]);
      }

      double[][] TnInv = new double[2][2];
      TnInv[0][0] =
          T0Inv[0][0] + ngroup * (k0 / kn) * (mean[0] - m[0]) * (mean[0] - m[0]) + sumdif[0][0];
      TnInv[0][1] =
          T0Inv[0][1] + ngroup * (k0 / kn) * (mean[1] - m[1]) * (mean[0] - m[0]) + sumdif[0][1];
      TnInv[1][0] =
          T0Inv[1][0] + ngroup * (k0 / kn) * (mean[0] - m[0]) * (mean[1] - m[1]) + sumdif[1][0];
      TnInv[1][1] =
          T0Inv[1][1] + ngroup * (k0 / kn) * (mean[1] - m[1]) * (mean[1] - m[1]) + sumdif[1][1];

      double logDetTn = -Math.log(TnInv[0][0] * TnInv[1][1] - TnInv[0][1] * TnInv[1][0]);

      L += -(ngroup) * Math.log(Math.PI);
      L += Math.log(k0) - Math.log(kn);
      L += (vn / 2) * logDetTn - (v0 / 2) * logDetT0;
      L += GammaFunction.lnGamma(vn / 2) + GammaFunction.lnGamma((vn / 2) - 0.5);
      L += -GammaFunction.lnGamma(v0 / 2) - GammaFunction.lnGamma((v0 / 2) - 0.5);
    }

    return L;
  }