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); }
private boolean normalized(Parameter p) { double sum = 0; for (int i = 0; i < p.getDimension(); i++) sum += p.getParameterValue(i); return (sum == 1.0); }
public Object parseXMLObject(XMLObject xo) throws XMLParseException { double[] shape; double[] scale; if (xo.hasChildNamed(MVGAMMA_SHAPE)) { XMLObject cxo = xo.getChild(MVGAMMA_SHAPE); shape = ((Parameter) cxo.getChild(Parameter.class)).getParameterValues(); cxo = xo.getChild(MVGAMMA_SCALE); scale = ((Parameter) cxo.getChild(Parameter.class)).getParameterValues(); if (shape.length != scale.length) throw new XMLParseException( "Shape and scale have wrong dimensions in " + xo.getName() + " element"); } else { XMLObject cxo = xo.getChild(MVN_MEAN); double[] mean = ((Parameter) cxo.getChild(Parameter.class)).getParameterValues(); cxo = xo.getChild(MVN_CV); double[] cv = ((Parameter) cxo.getChild(Parameter.class)).getParameterValues(); if (mean.length != cv.length) throw new XMLParseException( "Mean and CV have wrong dimensions in " + xo.getName() + " element"); final int dim = mean.length; shape = new double[dim]; scale = new double[dim]; for (int i = 0; i < dim; i++) { double c2 = cv[i] * cv[i]; shape[i] = 1.0 / c2; scale[i] = c2 * mean[i]; } } MultivariateDistributionLikelihood likelihood = new MultivariateDistributionLikelihood( new MultivariateGammaDistribution(shape, scale)); XMLObject cxo = xo.getChild(DATA); for (int j = 0; j < cxo.getChildCount(); j++) { if (cxo.getChild(j) instanceof Parameter) { Parameter data = (Parameter) cxo.getChild(j); likelihood.addData(data); if (data.getDimension() != shape.length) throw new XMLParseException( "dim(" + data.getStatisticName() + ") != " + shape.length + " in " + xo.getName() + "element"); } else { throw new XMLParseException("illegal element in " + xo.getName() + " element"); } } return likelihood; }
public Object parseXMLObject(XMLObject xo) throws XMLParseException { XMLObject cxo = xo.getChild(MVN_MEAN); Parameter mean = (Parameter) cxo.getChild(Parameter.class); cxo = xo.getChild(MVN_PRECISION); MatrixParameter precision = (MatrixParameter) cxo.getChild(MatrixParameter.class); if (mean.getDimension() != precision.getRowDimension() || mean.getDimension() != precision.getColumnDimension()) throw new XMLParseException( "Mean and precision have wrong dimensions in " + xo.getName() + " element"); Transform[] transforms = parseListOfTransforms(xo, mean.getDimension()); MultivariateDistributionLikelihood likelihood = new MultivariateDistributionLikelihood( new MultivariateNormalDistribution( mean.getParameterValues(), precision.getParameterAsMatrix()), transforms); cxo = xo.getChild(DATA); if (cxo != null) { for (int j = 0; j < cxo.getChildCount(); j++) { if (cxo.getChild(j) instanceof Parameter) { Parameter data = (Parameter) cxo.getChild(j); if (data instanceof MatrixParameter) { MatrixParameter matrix = (MatrixParameter) data; if (matrix.getParameter(0).getDimension() != mean.getDimension()) throw new XMLParseException( "dim(" + data.getStatisticName() + ") = " + matrix.getParameter(0).getDimension() + " is not equal to dim(" + mean.getStatisticName() + ") = " + mean.getDimension() + " in " + xo.getName() + "element"); for (int i = 0; i < matrix.getParameterCount(); i++) { likelihood.addData(matrix.getParameter(i)); } } else { if (data.getDimension() != mean.getDimension()) throw new XMLParseException( "dim(" + data.getStatisticName() + ") = " + data.getDimension() + " is not equal to dim(" + mean.getStatisticName() + ") = " + mean.getDimension() + " in " + xo.getName() + "element"); likelihood.addData(data); } } else { throw new XMLParseException("illegal element in " + xo.getName() + " element"); } } } return likelihood; }
public Object parseXMLObject(XMLObject xo) throws XMLParseException { XMLObject cxo = xo.getChild(DistributionLikelihoodParser.DISTRIBUTION); ParametricMultivariateDistributionModel distribution = (ParametricMultivariateDistributionModel) cxo.getChild(ParametricMultivariateDistributionModel.class); // Parse transforms here int maxDim = distribution.getMean().length; Transform[] transforms = parseListOfTransforms(xo, maxDim); MultivariateDistributionLikelihood likelihood = new MultivariateDistributionLikelihood(xo.getId(), distribution, transforms); boolean dataAsMatrix = xo.getAttribute(DATA_AS_MATRIX, false); cxo = xo.getChild(DATA); if (cxo != null) { for (int j = 0; j < cxo.getChildCount(); j++) { if (cxo.getChild(j) instanceof Parameter) { Parameter data = (Parameter) cxo.getChild(j); if (data instanceof MatrixParameter) { MatrixParameter matrix = (MatrixParameter) data; if (dataAsMatrix) { likelihood.addData(matrix); } else { if (matrix.getParameter(0).getDimension() != distribution.getMean().length) throw new XMLParseException( "dim(" + data.getStatisticName() + ") = " + matrix.getParameter(0).getDimension() + " is not equal to dim(" + distribution.getType() + ") = " + distribution.getMean().length + " in " + xo.getName() + "element"); for (int i = 0; i < matrix.getParameterCount(); i++) { likelihood.addData(matrix.getParameter(i)); } } } else { if (data.getDimension() != distribution.getMean().length) throw new XMLParseException( "dim(" + data.getStatisticName() + ") = " + data.getDimension() + " is not equal to dim(" + distribution.getType() + ") = " + distribution.getMean().length + " in " + xo.getName() + "element"); likelihood.addData(data); } } else { throw new XMLParseException("illegal element in " + xo.getName() + " element"); } } } return likelihood; }
public MixtureModelBranchRates( TreeModel tree, Parameter rateCategoryQuantilesParameter, ParametricDistributionModel[] models, Parameter distributionIndexParameter, boolean useQuantilesForRates, boolean normalize, double normalizeBranchRateTo) { super(MixtureModelBranchRatesParser.MIXTURE_MODEL_BRANCH_RATES); this.useQuantilesForRates = useQuantilesForRates; this.rateCategoryQuantiles = new TreeParameterModel(tree, rateCategoryQuantilesParameter, false); rates = new double[tree.getNodeCount()]; this.normalize = normalize; this.treeModel = tree; this.distributionModels = models; this.normalizeBranchRateTo = normalizeBranchRateTo; this.tree = new SimpleTree(tree); this.distributionIndexParameter = distributionIndexParameter; addVariable(this.distributionIndexParameter); // Force the boundaries of rateCategoryParameter to match the category count // d Parameter.DefaultBounds bound = new Parameter.DefaultBounds(categoryCount - 1, 0, // rateCategoryParameter.getDimension()); // d rateCategoryParameter.addBounds(bound); // rateCategoryQuantilesParameter.; Parameter.DefaultBounds bound = new Parameter.DefaultBounds(1.0, 0.0, rateCategoryQuantilesParameter.getDimension()); rateCategoryQuantilesParameter.addBounds(bound); Parameter.DefaultBounds bound2 = new Parameter.DefaultBounds(models.length, 0.0, 1); distributionIndexParameter.addBounds(bound2); distributionIndexParameter.setParameterValue(0, 0); // Parameter distributionIndexParameter; for (ParametricDistributionModel distributionModel : distributionModels) { addModel(distributionModel); } // AR - commented out: changes to the tree are handled by model changed events fired by // rateCategories // addModel(tree); // d addModel(rateCategories); addModel(rateCategoryQuantiles); // addModel(treeModel); // Maybe // AR - commented out: changes to rateCategoryParameter are handled by model changed events // fired by rateCategories // addVariable(rateCategoryParameter); if (normalize) { tree.addModelListener( new ModelListener() { public void modelChangedEvent(Model model, Object object, int index) { computeFactor(); } public void modelRestored(Model model) { computeFactor(); } }); } setupRates(); }