Search in sources :

Example 1 with Statistic

use of dr.inference.model.Statistic in project beast-mcmc by beast-dev.

the class SpeciationLikelihoodParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    XMLObject cxo = xo.getChild(MODEL);
    final SpeciationModel specModel = (SpeciationModel) cxo.getChild(SpeciationModel.class);
    cxo = xo.getChild(TREE);
    final Tree tree = (Tree) cxo.getChild(Tree.class);
    Set<Taxon> excludeTaxa = null;
    if (xo.hasChildNamed(INCLUDE)) {
        excludeTaxa = new HashSet<Taxon>();
        for (int i = 0; i < tree.getTaxonCount(); i++) {
            excludeTaxa.add(tree.getTaxon(i));
        }
        cxo = xo.getChild(INCLUDE);
        for (int i = 0; i < cxo.getChildCount(); i++) {
            TaxonList taxonList = (TaxonList) cxo.getChild(i);
            for (int j = 0; j < taxonList.getTaxonCount(); j++) {
                excludeTaxa.remove(taxonList.getTaxon(j));
            }
        }
    }
    if (xo.hasChildNamed(EXCLUDE)) {
        excludeTaxa = new HashSet<Taxon>();
        cxo = xo.getChild(EXCLUDE);
        for (int i = 0; i < cxo.getChildCount(); i++) {
            TaxonList taxonList = (TaxonList) cxo.getChild(i);
            for (int j = 0; j < taxonList.getTaxonCount(); j++) {
                excludeTaxa.add(taxonList.getTaxon(j));
            }
        }
    }
    if (excludeTaxa != null) {
        Logger.getLogger("dr.evomodel").info("Speciation model excluding " + excludeTaxa.size() + " taxa from prior - " + (tree.getTaxonCount() - excludeTaxa.size()) + " taxa remaining.");
    }
    final XMLObject cal = xo.getChild(CALIBRATION);
    if (cal != null) {
        if (excludeTaxa != null) {
            throw new XMLParseException("Sorry, not implemented: internal calibration prior + excluded taxa");
        }
        List<Distribution> dists = new ArrayList<Distribution>();
        List<Taxa> taxa = new ArrayList<Taxa>();
        List<Boolean> forParent = new ArrayList<Boolean>();
        // (Statistic) cal.getChild(Statistic.class);
        Statistic userPDF = null;
        for (int k = 0; k < cal.getChildCount(); ++k) {
            final Object ck = cal.getChild(k);
            if (DistributionLikelihood.class.isInstance(ck)) {
                dists.add(((DistributionLikelihood) ck).getDistribution());
            } else if (Distribution.class.isInstance(ck)) {
                dists.add((Distribution) ck);
            } else if (Taxa.class.isInstance(ck)) {
                final Taxa tx = (Taxa) ck;
                taxa.add(tx);
                forParent.add(tx.getTaxonCount() == 1);
            } else if (Statistic.class.isInstance(ck)) {
                if (userPDF != null) {
                    throw new XMLParseException("more than one userPDF correction???");
                }
                userPDF = (Statistic) cal.getChild(Statistic.class);
            } else {
                XMLObject cko = (XMLObject) ck;
                assert cko.getChildCount() == 2;
                for (int i = 0; i < 2; ++i) {
                    final Object chi = cko.getChild(i);
                    if (DistributionLikelihood.class.isInstance(chi)) {
                        dists.add(((DistributionLikelihood) chi).getDistribution());
                    } else if (Distribution.class.isInstance(chi)) {
                        dists.add((Distribution) chi);
                    } else if (Taxa.class.isInstance(chi)) {
                        taxa.add((Taxa) chi);
                        boolean fp = ((Taxa) chi).getTaxonCount() == 1;
                        if (cko.hasAttribute(PARENT)) {
                            boolean ufp = cko.getBooleanAttribute(PARENT);
                            if (fp && !ufp) {
                                throw new XMLParseException("forParent==false for a single taxon?? (must be true)");
                            }
                            fp = ufp;
                        }
                        forParent.add(fp);
                    } else {
                        assert false;
                    }
                }
            }
        }
        if (dists.size() != taxa.size()) {
            throw new XMLParseException("Mismatch in number of distributions and taxa specs");
        }
        try {
            final String correction = cal.getAttribute(CORRECTION, EXACT);
            final CalibrationPoints.CorrectionType type = correction.equals(EXACT) ? CalibrationPoints.CorrectionType.EXACT : (correction.equals(APPROX) ? CalibrationPoints.CorrectionType.APPROXIMATED : (correction.equals(NONE) ? CalibrationPoints.CorrectionType.NONE : (correction.equals(PEXACT) ? CalibrationPoints.CorrectionType.PEXACT : null)));
            if (cal.hasAttribute(CORRECTION) && type == null) {
                throw new XMLParseException("correction type == " + correction + "???");
            }
            final CalibrationPoints calib = new CalibrationPoints(tree, specModel.isYule(), dists, taxa, forParent, userPDF, type);
            final SpeciationLikelihood speciationLikelihood = new SpeciationLikelihood(tree, specModel, null, calib);
            return speciationLikelihood;
        } catch (IllegalArgumentException e) {
            throw new XMLParseException(e.getMessage());
        }
    }
    return new SpeciationLikelihood(tree, specModel, excludeTaxa, null);
}
Also used : CalibrationPoints(dr.evomodel.speciation.CalibrationPoints) TaxonList(dr.evolution.util.TaxonList) Taxon(dr.evolution.util.Taxon) ArrayList(java.util.ArrayList) SpeciationModel(dr.evomodel.speciation.SpeciationModel) SpeciationLikelihood(dr.evomodel.speciation.SpeciationLikelihood) Taxa(dr.evolution.util.Taxa) Statistic(dr.inference.model.Statistic) Distribution(dr.math.distributions.Distribution) Tree(dr.evolution.tree.Tree) DistributionLikelihood(dr.inference.distribution.DistributionLikelihood)

Example 2 with Statistic

use of dr.inference.model.Statistic in project beast-mcmc by beast-dev.

the class EmpiricalDistributionLikelihoodParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    boolean splineInterpolation = xo.getAttribute(SPLINE_INTERPOLATION, false);
    // Default is cubic-spline
    int degree = xo.getAttribute(DEGREE, 3);
    boolean inverse = xo.getAttribute(INVERSE, false);
    EmpiricalDistributionLikelihood likelihood;
    if (xo.hasChildNamed(FILE_INFORMATION)) {
        String fileName = xo.getStringAttribute(FILE_NAME);
        boolean byColumn = xo.getAttribute(READ_BY_COLUMN, true);
        if (splineInterpolation) {
            if (degree < 1)
                throw new XMLParseException("Spline degree must be greater than zero!");
            likelihood = new SplineInterpolatedLikelihood(fileName, degree, inverse, byColumn);
        } else
            // likelihood = new EmpiricalDistributionLikelihood(fileName,inverse,byColumn);
            throw new XMLParseException("Only spline-interpolated empirical distributions are currently support");
    } else {
        List<EmpiricalDistributionData> dataList = new ArrayList<>();
        for (int i = 0; i < xo.getChildCount(); ++i) {
            XMLObject cxo = (XMLObject) xo.getChild(i);
            if (cxo.getName().equals(RAW_VALUES)) {
                Parameter thetaParameter = getRawValues(VALUES, cxo);
                Parameter likelihoodParameter = getRawValues(LIKELIHOOD, cxo);
                if (likelihoodParameter.getDimension() != thetaParameter.getDimension()) {
                    throw new XMLParseException("Unequal grid lengths");
                }
                boolean densityInLogSpace = cxo.getAttribute(DENSITY_IN_LOG_SPACE, true);
                dataList.add(new EmpiricalDistributionData(thetaParameter.getParameterValues(), likelihoodParameter.getParameterValues(), densityInLogSpace));
            }
        }
        likelihood = new SplineInterpolatedLikelihood(dataList, degree, inverse);
    }
    XMLObject cxo1 = xo.getChild(DATA);
    final int from = cxo1.getAttribute(FROM, -1);
    int to = cxo1.getAttribute(TO, -1);
    if (from >= 0 || to >= 0) {
        if (to < 0) {
            to = Integer.MAX_VALUE;
        }
        if (!(from >= 0 && to >= 0 && from < to)) {
            throw new XMLParseException("ill formed from-to");
        }
        likelihood.setRange(from, to);
    }
    for (int j = 0; j < cxo1.getChildCount(); j++) {
        if (cxo1.getChild(j) instanceof Statistic) {
            likelihood.addData((Statistic) cxo1.getChild(j));
        } else {
            throw new XMLParseException("illegal element in " + cxo1.getName() + " element");
        }
    }
    if (likelihood.getDistributionDimension() != 1 && (likelihood.getDistributionDimension() != likelihood.getDimension())) {
        throw new XMLParseException("Data dimension != distribution dimension");
    }
    double offset = cxo1.getAttribute(OFFSET, 0);
    likelihood.setOffset(offset);
    if (cxo1.hasAttribute(LOWER) || cxo1.hasAttribute(UPPER)) {
        likelihood.setBounds(cxo1.getAttribute(LOWER, Double.NEGATIVE_INFINITY), cxo1.getAttribute(UPPER, Double.POSITIVE_INFINITY));
    }
    return likelihood;
}
Also used : SplineInterpolatedLikelihood(dr.inference.distribution.SplineInterpolatedLikelihood) EmpiricalDistributionLikelihood(dr.inference.distribution.EmpiricalDistributionLikelihood) EmpiricalDistributionData(dr.inference.distribution.EmpiricalDistributionData) ArrayList(java.util.ArrayList) Statistic(dr.inference.model.Statistic) Parameter(dr.inference.model.Parameter)

Example 3 with Statistic

use of dr.inference.model.Statistic in project beast-mcmc by beast-dev.

the class ThresholdStatisticParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    double threshold = xo.getAttribute(THRESHOLD, 0.0);
    String name = THRESHOLD_STATISTIC;
    if (xo.hasAttribute(Statistic.NAME)) {
        name = xo.getAttribute(Statistic.NAME, xo.getId());
    } else if (xo.hasAttribute(XMLParser.ID)) {
        name = xo.getAttribute(XMLParser.ID, xo.getId());
    }
    final Statistic statistic = (Statistic) xo.getChild(Statistic.class);
    final ThresholdStatistic thresholdStatistic = new ThresholdStatistic(name, statistic, threshold);
    return thresholdStatistic;
}
Also used : Statistic(dr.inference.model.Statistic) SumStatistic(dr.inference.model.SumStatistic) ThresholdStatistic(dr.inference.model.ThresholdStatistic) ThresholdStatistic(dr.inference.model.ThresholdStatistic)

Example 4 with Statistic

use of dr.inference.model.Statistic in project beast-mcmc by beast-dev.

the class CoulombPriorParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    XMLObject cxo = xo;
    final double beta = xo.getDoubleAttribute(BETA);
    if (xo.hasChildNamed(DATA)) {
        cxo = xo.getChild(DATA);
    }
    CoulombPrior likelihood = new CoulombPrior(beta);
    for (int i = 0; i < cxo.getChildCount(); i++) {
        if (cxo.getChild(i) instanceof Statistic) {
            likelihood.addData((Statistic) cxo.getChild(i));
        }
    }
    return likelihood;
}
Also used : CoulombPrior(dr.inference.model.CoulombPrior) Statistic(dr.inference.model.Statistic)

Example 5 with Statistic

use of dr.inference.model.Statistic in project beast-mcmc by beast-dev.

the class ExponentialStatisticParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    ExponentialStatistic expStatistic = null;
    Object child = xo.getChild(0);
    if (child instanceof Statistic) {
        expStatistic = new ExponentialStatistic(EXPONENTIAL_STATISTIC, (Statistic) child);
    } else {
        throw new XMLParseException("Unknown element found in " + getParserName() + " element:" + child);
    }
    return expStatistic;
}
Also used : ExponentialStatistic(dr.inference.model.ExponentialStatistic) Statistic(dr.inference.model.Statistic) ExponentialStatistic(dr.inference.model.ExponentialStatistic)

Aggregations

Statistic (dr.inference.model.Statistic)21 Taxa (dr.evolution.util.Taxa)2 DistributionLikelihood (dr.inference.distribution.DistributionLikelihood)2 ParametricDistributionModel (dr.inference.distribution.ParametricDistributionModel)2 Parameter (dr.inference.model.Parameter)2 SumStatistic (dr.inference.model.SumStatistic)2 ArrayList (java.util.ArrayList)2 Tree (dr.evolution.tree.Tree)1 Taxon (dr.evolution.util.Taxon)1 TaxonList (dr.evolution.util.TaxonList)1 CalibrationPoints (dr.evomodel.speciation.CalibrationPoints)1 SpeciationLikelihood (dr.evomodel.speciation.SpeciationLikelihood)1 SpeciationModel (dr.evomodel.speciation.SpeciationModel)1 DateParser (dr.evoxml.DateParser)1 TaxaParser (dr.evoxml.TaxaParser)1 TaxonParser (dr.evoxml.TaxonParser)1 EmpiricalDistributionData (dr.inference.distribution.EmpiricalDistributionData)1 EmpiricalDistributionLikelihood (dr.inference.distribution.EmpiricalDistributionLikelihood)1 MixedDistributionLikelihood (dr.inference.distribution.MixedDistributionLikelihood)1 SplineInterpolatedLikelihood (dr.inference.distribution.SplineInterpolatedLikelihood)1