예제 #1
0
        public Object parseXMLObject(XMLObject xo) throws XMLParseException {

          Parameter weights = (Parameter) xo.getChild(Parameter.class);
          List<AbstractModelLikelihood> likelihoodList = new ArrayList<AbstractModelLikelihood>();

          for (int i = 0; i < xo.getChildCount(); i++) {
            if (xo.getChild(i) instanceof Likelihood)
              likelihoodList.add((AbstractModelLikelihood) xo.getChild(i));
          }

          if (weights.getDimension() != likelihoodList.size()) {
            throw new XMLParseException(
                "Dim of " + weights.getId() + " does not match the number of likelihoods");
          }

          if (xo.hasAttribute(NORMALIZE)) {
            if (xo.getBooleanAttribute(NORMALIZE)) {
              double sum = 0;
              for (int i = 0; i < weights.getDimension(); i++) sum += weights.getParameterValue(i);
              for (int i = 0; i < weights.getDimension(); i++)
                weights.setParameterValue(i, weights.getParameterValue(i) / sum);
            }
          }

          if (!normalized(weights))
            throw new XMLParseException(
                "Parameter +" + weights.getId() + " must lie on the simplex");

          return new WeightedMixtureModel(likelihoodList, weights);
        }
예제 #2
0
  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);
  }