Search in sources :

Example 1 with Distribution

use of dr.math.distributions.Distribution in project beast-mcmc by beast-dev.

the class HierarchicalPriorDialog method setupChart.

private void setupChart() {
    for (int i = 0; i < 2; ++i) {
        chart[i].removeAllPlots();
        double offset = 0.0;
        Distribution distribution = null;
        switch(i) {
            case 0:
                distribution = optionsPanels.get(PriorType.NORMAL_PRIOR).getDistribution();
                break;
            case 1:
                distribution = optionsPanels.get(PriorType.GAMMA_PRIOR).getDistribution();
                break;
        }
        chart[i].addPlot(new PDFPlot(distribution, offset));
        if (distribution != null) {
            quantileText[i].setText(formatter.format(distribution.quantile(0.025)) + "\n" + formatter.format(distribution.quantile(0.05)) + "\n" + formatter.format(distribution.quantile(0.5)) + "\n" + formatter.format(distribution.quantile(0.95)) + "\n" + formatter.format(distribution.quantile(0.975)));
        }
    }
}
Also used : GammaDistribution(dr.math.distributions.GammaDistribution) Distribution(dr.math.distributions.Distribution) OffsetPositiveDistribution(dr.math.distributions.OffsetPositiveDistribution) NormalDistribution(dr.math.distributions.NormalDistribution) PDFPlot(dr.app.gui.chart.PDFPlot)

Example 2 with Distribution

use of dr.math.distributions.Distribution 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 3 with Distribution

use of dr.math.distributions.Distribution in project beast-mcmc by beast-dev.

the class ParameterParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    double[] values = null;
    double[] uppers;
    double[] lowers;
    if (xo.hasAttribute(DIMENSION)) {
        values = new double[xo.getIntegerAttribute(DIMENSION)];
    }
    if (xo.hasAttribute(FILENAME)) {
        // read samples from a file (used for a reference prior) and calculate the mean
        String fileName = xo.getStringAttribute(FILENAME);
        File file = new File(fileName);
        fileName = file.getName();
        String parent = file.getParent();
        if (!file.isAbsolute()) {
            parent = System.getProperty("user.dir");
        }
        file = new File(parent, fileName);
        fileName = file.getAbsolutePath();
        String columnName = "";
        if (xo.hasAttribute(PARAMETERCOLUMN)) {
            columnName = xo.getStringAttribute(PARAMETERCOLUMN);
        } else {
            throw new XMLParseException("when providing a file name you must provide a parameter column as well");
        }
        // if a number for the burnin is not given, use 0 ...
        int burnin;
        if (xo.hasAttribute(BURNIN)) {
            burnin = xo.getIntegerAttribute(BURNIN);
        } else {
            burnin = 0;
        }
        LogFileTraces traces = new LogFileTraces(fileName, file);
        List parameterSamples = null;
        try {
            traces.loadTraces();
            traces.setBurnIn(burnin);
            int traceIndexParameter = -1;
            for (int i = 0; i < traces.getTraceCount(); i++) {
                String traceName = traces.getTraceName(i);
                if (traceName.trim().equals(columnName)) {
                    traceIndexParameter = i;
                }
            }
            if (traceIndexParameter == -1) {
                throw new XMLParseException("Column '" + columnName + "' can not be found for " + getParserName() + " element.");
            }
            parameterSamples = traces.getValues(traceIndexParameter);
        } catch (TraceException e) {
            throw new XMLParseException(e.getMessage());
        } catch (IOException ioe) {
            throw new XMLParseException(ioe.getMessage());
        }
        values = new double[1];
        for (int i = 0, stop = parameterSamples.size(); i < stop; i++) {
            values[0] += ((Double) parameterSamples.get(i)) / ((double) stop);
        }
        System.out.println("Number of samples: " + parameterSamples.size());
        System.out.println("Parameter mean: " + values[0]);
    } else if (xo.hasAttribute(VALUE)) {
        if (values == null) {
            values = xo.getDoubleArrayAttribute(VALUE);
        } else {
            double[] v = xo.getDoubleArrayAttribute(VALUE);
            if (v.length == values.length) {
                System.arraycopy(v, 0, values, 0, v.length);
            } else if (v.length == 1) {
                for (int i = 0; i < values.length; i++) {
                    values[i] = v[0];
                }
            } else {
                throw new XMLParseException("value string must have 1 value or dimension values");
            }
        }
    } else {
        if (xo.hasAttribute(DIMENSION)) {
            values = new double[xo.getIntegerAttribute(DIMENSION)];
        } else {
            // parameter dimension will get set correctly by TreeModel presumably.
            if (!xo.hasChildNamed(RANDOMIZE)) {
                return new Parameter.Default(1);
            }
            values = new double[1];
            values[0] = 1.0;
        }
    }
    uppers = new double[values.length];
    for (int i = 0; i < values.length; i++) {
        uppers[i] = Double.POSITIVE_INFINITY;
    }
    lowers = new double[values.length];
    for (int i = 0; i < values.length; i++) {
        lowers[i] = Double.NEGATIVE_INFINITY;
    }
    if (xo.hasAttribute(UPPER)) {
        double[] v = xo.getDoubleArrayAttribute(UPPER);
        if (v.length == uppers.length) {
            System.arraycopy(v, 0, uppers, 0, v.length);
        } else if (v.length == 1) {
            for (int i = 0; i < uppers.length; i++) {
                uppers[i] = v[0];
            }
        } else {
            throw new XMLParseException("uppers string must have 1 value or dimension values");
        }
    }
    if (xo.hasAttribute(LOWER)) {
        double[] v = xo.getDoubleArrayAttribute(LOWER);
        if (v.length == lowers.length) {
            System.arraycopy(v, 0, lowers, 0, v.length);
        } else if (v.length == 1) {
            for (int i = 0; i < lowers.length; i++) {
                lowers[i] = v[0];
            }
        } else {
            throw new XMLParseException("lowers string must have 1 value or dimension values");
        }
    }
    if ((uppers.length != values.length)) {
        throw new XMLParseException("value and upper limit strings have different dimension, in parameter");
    }
    if ((lowers.length != values.length)) {
        throw new XMLParseException("value and lower limit strings have different dimension, in parameter");
    }
    // check if uppers and lowers are consistent
    for (int i = 0; i < values.length; i++) {
        if (uppers[i] < lowers[i]) {
            throw new XMLParseException("upper is lower than lower, in parameter");
        }
    }
    if (xo.hasChildNamed(RANDOMIZE)) {
        Distribution distribution = (Distribution) xo.getChild(RANDOMIZE).getChild(Distribution.class);
        for (int i = 0; i < values.length; i++) {
            do {
                // Not an efficient way to draw random variables, but this is currently the only general interface
                values[i] = distribution.quantile(MathUtils.nextDouble());
            } while (values[i] < lowers[i] || values[i] > uppers[i]);
        }
    } else {
        // make values consistent with bounds
        for (int i = 0; i < values.length; i++) {
            if (uppers[i] < values[i])
                values[i] = uppers[i];
        }
        for (int i = 0; i < values.length; i++) {
            if (lowers[i] > values[i])
                values[i] = lowers[i];
        }
    }
    Parameter param = new Parameter.Default(values);
    // for(int i = 0; i < values.length; i++) {
    // param.setParameterValue(i, values[i]);
    // }
    param.addBounds(new Parameter.DefaultBounds(uppers, lowers));
    return param;
}
Also used : IOException(java.io.IOException) TraceException(dr.inference.trace.TraceException) Distribution(dr.math.distributions.Distribution) LogFileTraces(dr.inference.trace.LogFileTraces) List(java.util.List) File(java.io.File)

Example 4 with Distribution

use of dr.math.distributions.Distribution in project beast-mcmc by beast-dev.

the class TwoPartsDistributionLikelihoodParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    DistributionLikelihood priorLikelihood = (DistributionLikelihood) xo.getElementFirstChild(PRIOR);
    DistributionLikelihood pseudoPriorLikelihood = (DistributionLikelihood) xo.getElementFirstChild(PSEUDO_PRIOR);
    Distribution prior = priorLikelihood.getDistribution();
    Distribution pseudoPrior = pseudoPriorLikelihood.getDistribution();
    Parameter bitVector = (Parameter) xo.getElementFirstChild(PARAMETER_VECTOR);
    int paramIndex = xo.getIntegerAttribute(PARAMETER_INDEX);
    Parameter selectedVariable = (Parameter) xo.getElementFirstChild(SELECTED_VARIABLE);
    TwoPartsDistributionLikelihood likelihood = new TwoPartsDistributionLikelihood(prior, pseudoPrior, bitVector, paramIndex);
    likelihood.addData(selectedVariable);
    return likelihood;
}
Also used : Distribution(dr.math.distributions.Distribution) Parameter(dr.inference.model.Parameter) TwoPartsDistributionLikelihood(dr.inference.distribution.TwoPartsDistributionLikelihood) TwoPartsDistributionLikelihood(dr.inference.distribution.TwoPartsDistributionLikelihood) DistributionLikelihood(dr.inference.distribution.DistributionLikelihood)

Example 5 with Distribution

use of dr.math.distributions.Distribution in project beast-mcmc by beast-dev.

the class HierarchicalPriorDialog method setupChart.

private void setupChart() {
    for (int i = 0; i < 2; ++i) {
        chart[i].removeAllPlots();
        double offset = 0.0;
        Distribution distribution = null;
        switch(i) {
            case 0:
                distribution = optionsPanels.get(PriorType.NORMAL_PRIOR).getDistribution();
                break;
            case 1:
                distribution = optionsPanels.get(PriorType.GAMMA_PRIOR).getDistribution();
                break;
        }
        chart[i].addPlot(new PDFPlot(distribution, offset));
        if (distribution != null) {
            quantileText[i].setText(formatter.format(distribution.quantile(0.025)) + "\n" + formatter.format(distribution.quantile(0.05)) + "\n" + formatter.format(distribution.quantile(0.5)) + "\n" + formatter.format(distribution.quantile(0.95)) + "\n" + formatter.format(distribution.quantile(0.975)));
        }
    }
}
Also used : GammaDistribution(dr.math.distributions.GammaDistribution) Distribution(dr.math.distributions.Distribution) OffsetPositiveDistribution(dr.math.distributions.OffsetPositiveDistribution) NormalDistribution(dr.math.distributions.NormalDistribution) PDFPlot(dr.app.gui.chart.PDFPlot)

Aggregations

Distribution (dr.math.distributions.Distribution)10 PDFPlot (dr.app.gui.chart.PDFPlot)4 DistributionLikelihood (dr.inference.distribution.DistributionLikelihood)3 Parameter (dr.inference.model.Parameter)3 PriorType (dr.app.beauti.types.PriorType)2 GammaDistribution (dr.math.distributions.GammaDistribution)2 NormalDistribution (dr.math.distributions.NormalDistribution)2 OffsetPositiveDistribution (dr.math.distributions.OffsetPositiveDistribution)2 Tree (dr.evolution.tree.Tree)1 Taxa (dr.evolution.util.Taxa)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 ModelSpecificPseudoPriorLikelihood (dr.inference.distribution.ModelSpecificPseudoPriorLikelihood)1 TwoPartsDistributionLikelihood (dr.inference.distribution.TwoPartsDistributionLikelihood)1 TwoPieceLocationScaleDistributionModel (dr.inference.distribution.TwoPieceLocationScaleDistributionModel)1 Statistic (dr.inference.model.Statistic)1 LogFileTraces (dr.inference.trace.LogFileTraces)1