Search in sources :

Example 1 with FrequencyDistribution

use of dr.util.FrequencyDistribution in project beast-mcmc by beast-dev.

the class CategoryDensityPlot method setData.

/**
     * Set data, all integers
     */
public void setData(Variate.D data) {
    setRawData(data);
    FrequencyDistribution frequency = getFrequencyDistribution(data, -1);
    Variate.D xData = new Variate.D();
    Variate.D yData = new Variate.D();
    double x = frequency.getLowerBound();
    for (int i = 0; i < frequency.getBinCount(); i++) {
        xData.add(x);
        yData.add(0.0);
        x += frequency.getBinSize();
        xData.add(x);
        yData.add(frequency.getProbability(i));
    }
    setData(xData, yData);
}
Also used : Variate(dr.stats.Variate) FrequencyDistribution(dr.util.FrequencyDistribution)

Example 2 with FrequencyDistribution

use of dr.util.FrequencyDistribution in project beast-mcmc by beast-dev.

the class FrequencyPlot method setData.

/**
     * Set data
     */
public void setData(Variate.D data, int minimumBinCount) {
    setRawData(data);
    FrequencyDistribution frequency = getFrequencyDistribution(data, minimumBinCount);
    Variate.D xData = new Variate.D();
    Variate.D yData = new Variate.D();
    double x = frequency.getLowerBound();
    for (int i = 0; i < frequency.getBinCount(); i++) {
        xData.add(x);
        yData.add(0.0);
        x += frequency.getBinSize();
        xData.add(x);
        yData.add((double) frequency.getFrequency(i));
    }
    setData(xData, yData);
}
Also used : Variate(dr.stats.Variate) FrequencyDistribution(dr.util.FrequencyDistribution)

Example 3 with FrequencyDistribution

use of dr.util.FrequencyDistribution in project beast-mcmc by beast-dev.

the class KDENumericalDensityPlot method setData.

/**
     * Set data
     */
public void setData(Variate.D data, int minimumBinCount) {
    setRawData(data);
    Double[] samples = new Double[data.getCount()];
    for (int i = 0; i < data.getCount(); i++) {
        samples[i] = data.get(i);
    }
    kde = getKDE(samples);
    FrequencyDistribution frequency = getFrequencyDistribution(data, minimumBinCount);
    Variate.D xData = new Variate.D();
    Variate.D yData = new Variate.D();
    //        double x = frequency.getLowerBound() - frequency.getBinSize();
    //        double maxDensity = 0.0;
    //        // TODO Compute KDE once
    ////        for (int i = 0; i < frequency.getBinCount(); i++) {
    ////            double density = frequency.getFrequency(i) / frequency.getBinSize() / data.getValuesSize();
    ////            if (density > maxDensity) maxDensity = density;
    ////        }
    //
    //        xData.add(x + (frequency.getBinSize() / 2.0));
    //        yData.add(0.0);
    //        x += frequency.getBinSize();
    //
    //        for (int i = 0; i < frequency.getBinCount(); i++) {
    //            double xPoint = x + (frequency.getBinSize() / 2.0);
    //            xData.add(xPoint);
    ////            double density = frequency.getFrequency(i) / frequency.getBinSize() / data.getValuesSize();
    //            double density = kde.pdf(xPoint);
    //            if (relativeDensity) {
    //                yData.add(density / maxDensity);
    //            } else {
    //                yData.add(density);
    //            }
    //            x += frequency.getBinSize();
    //        }
    //
    //        xData.add(x + (frequency.getBinSize() / 2.0));
    //        yData.add(0.0);
    double x = frequency.getLowerBound() - (frequency.getBinSize() / 2.0);
    int extraEdgeCount = 0;
    while (kde.pdf(x) > minDensity && x > lowerBoundary) {
        x -= frequency.getBinSize();
        extraEdgeCount += 1;
    }
    xData.add(x);
    yData.add(0.0);
    x += frequency.getBinSize();
    int count = 0;
    while (count < (frequency.getBinCount() + extraEdgeCount)) {
        // ||
        //                (kde.pdf(x) > minDensity && x < upperBoundary)) {
        xData.add(x);
        yData.add(kde.pdf(x));
        x += frequency.getBinSize();
        count++;
    }
    if (DEBUG) {
        System.err.println("kde = " + kde.pdf(x));
    }
    while (kde.pdf(x) > minDensity) {
        if (DEBUG) {
            System.err.println("add bit on end!!!");
        }
        xData.add(x);
        yData.add(kde.pdf(x));
        x += frequency.getBinSize();
    }
    xData.add(x);
    yData.add(0.0);
    //
    //
    //        int extraBinsOnEdges = 5;
    //        double x = frequency.getLowerBound() - extraBinsOnEdges * frequency.getBinSize();
    //        for (int i = 0; i < frequency.getBinCount() + 2 * extraBinsOnEdges; i++) {
    //            double xMidPoint = x + (frequency.getBinSize() / 2.0);
    //            xData.add(xMidPoint);
    //            yData.add(kde.pdf(xMidPoint));
    //            x += frequency.getBinSize();
    //        }
    setData(xData, yData);
}
Also used : Variate(dr.stats.Variate) FrequencyDistribution(dr.util.FrequencyDistribution)

Example 4 with FrequencyDistribution

use of dr.util.FrequencyDistribution in project beast-mcmc by beast-dev.

the class NumericalDensityPlot method setData.

/**
     * Set data
     */
public void setData(Variate.D data, int minimumBinCount) {
    setRawData(data);
    FrequencyDistribution frequency = getFrequencyDistribution(data, minimumBinCount);
    Variate.D xData = new Variate.D();
    Variate.D yData = new Variate.D();
    double x = frequency.getLowerBound() - frequency.getBinSize();
    double maxDensity = 0.0;
    for (int i = 0; i < frequency.getBinCount(); i++) {
        double density = frequency.getFrequency(i) / frequency.getBinSize() / data.getCount();
        if (density > maxDensity)
            maxDensity = density;
    }
    xData.add(x + (frequency.getBinSize() / 2.0));
    yData.add(0.0);
    x += frequency.getBinSize();
    for (int i = 0; i < frequency.getBinCount(); i++) {
        xData.add(x + (frequency.getBinSize() / 2.0));
        double density = frequency.getFrequency(i) / frequency.getBinSize() / data.getCount();
        if (relativeDensity) {
            yData.add(density / maxDensity);
        } else {
            yData.add(density);
        }
        x += frequency.getBinSize();
    }
    xData.add(x + (frequency.getBinSize() / 2.0));
    yData.add(0.0);
    setData(xData, yData);
}
Also used : Rectangle2D(java.awt.geom.Rectangle2D) Variate(dr.stats.Variate) FrequencyDistribution(dr.util.FrequencyDistribution)

Example 5 with FrequencyDistribution

use of dr.util.FrequencyDistribution in project beast-mcmc by beast-dev.

the class FrequencyPlot method getFrequencyDistribution.

/**
     * Get the FrequencyDistribution object
     */
protected FrequencyDistribution getFrequencyDistribution(Variate data, int minimumBinCount) {
    double min = (Double) data.getMin();
    double max = (Double) data.getMax();
    if (min == max) {
        if (min == 0) {
            min = -1.0;
        } else {
            min -= Math.abs(min / 10.0);
        }
        if (max == 0) {
            max = 1.0;
        } else {
            max += Math.abs(max / 10.0);
        }
    }
    Axis axis = new LinearAxis(Axis.AT_MAJOR_TICK, Axis.AT_MAJOR_TICK);
    if (minimumBinCount <= 0)
        axis = new LinearAxis(Axis.AT_MAJOR_TICK_PLUS, Axis.AT_MAJOR_TICK_PLUS);
    axis.setRange(min, max);
    int majorTickCount = axis.getMajorTickCount();
    axis.setPrefNumTicks(majorTickCount, 4);
    double binSize = axis.getMinorTickSpacing();
    int binCount = (int) ((axis.getMaxAxis() - axis.getMinAxis()) / binSize) + 2;
    if (minimumBinCount > 0) {
        // avoid dead loop
        while (binCount < minimumBinCount && majorTickCount < 1000) {
            majorTickCount++;
            axis.setPrefNumTicks(majorTickCount, 4);
            binSize = axis.getMinorTickSpacing();
            // should +2, otherwise the last bar will lose
            binCount = (int) ((axis.getMaxAxis() - axis.getMinAxis()) / binSize) + 2;
        }
    } else if (binSize > 1) {
        // getMinorTickSpacing() returns 1.25, if the min integer slightly bigger than 0
        binSize = 0.5;
        binCount = (int) ((axis.getMaxAxis() - axis.getMinAxis()) / binSize) + 2;
    }
    double start = axis.getMinAxis();
    if (minimumBinCount < 0)
        // to convert x-axis into integer
        start = Math.round(start);
    FrequencyDistribution frequency = new FrequencyDistribution(start, binCount, binSize);
    for (int i = 0; i < raw.getCount(); i++) {
        frequency.addValue((Double) raw.get(i));
    }
    return frequency;
}
Also used : FrequencyDistribution(dr.util.FrequencyDistribution)

Aggregations

FrequencyDistribution (dr.util.FrequencyDistribution)7 Variate (dr.stats.Variate)5 Axis (dr.app.gui.chart.Axis)1 LinearAxis (dr.app.gui.chart.LinearAxis)1 Rectangle2D (java.awt.geom.Rectangle2D)1