use of dr.util.FrequencyDistribution in project beast-mcmc by beast-dev.
the class DensityEstimate method calculateFrequencies.
protected FrequencyDistribution calculateFrequencies(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);
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) {
while (binCount < minimumBinCount) {
majorTickCount++;
axis.setPrefNumTicks(majorTickCount, 4);
binSize = axis.getMinorTickSpacing();
// should +2, otherwise the last bar will lose
binCount = (int) ((axis.getMaxAxis() - axis.getMinAxis()) / binSize) + 2;
}
}
FrequencyDistribution frequency = new FrequencyDistribution(axis.getMinAxis(), binCount, binSize);
for (int i = 0; i < data.getCount(); i++) {
frequency.addValue((Double) data.get(i));
}
return frequency;
}
use of dr.util.FrequencyDistribution in project beast-mcmc by beast-dev.
the class KernelDensityEstimate method calculateDensity.
protected void calculateDensity(Variate data, int minimumBinCount) {
FrequencyDistribution frequency = calculateFrequencies(data, minimumBinCount);
xCoordinates = new Variate.D();
yCoordinates = new Variate.D();
double x = frequency.getLowerBound() - (frequency.getBinSize() / 2.0);
int extraEdgeCount = 0;
while (kde.pdf(x) > minDensity && x > lowerBoundary) {
x -= frequency.getBinSize();
extraEdgeCount += 1;
}
xCoordinates.add(x);
yCoordinates.add(0.0);
x += frequency.getBinSize();
int count = 0;
while (count < (frequency.getBinCount() + extraEdgeCount)) {
// ||
// (kde.pdf(x) > minDensity && x < upperBoundary)) {
xCoordinates.add(x);
yCoordinates.add(kde.pdf(x));
x += frequency.getBinSize();
count++;
}
// System.err.println("kde = " + kde.pdf(x));
while (kde.pdf(x) > minDensity) {
// System.err.println("add bit on end!!!");
xCoordinates.add(x);
yCoordinates.add(kde.pdf(x));
x += frequency.getBinSize();
}
xCoordinates.add(x);
yCoordinates.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();
// }
}
Aggregations