| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.app.gui.chart; |
| 27 |
|
| 28 |
import dr.math.distributions.Distribution; |
| 29 |
import dr.stats.Variate; |
| 30 |
import org.apache.commons.math.MathException; |
| 31 |
import org.apache.commons.math.MathRuntimeException; |
| 32 |
|
| 33 |
import java.awt.*; |
| 34 |
|
| 35 |
public class PDFPlot extends Plot.AbstractPlot { |
| 36 |
|
| 37 |
private Distribution distribution = null; |
| 38 |
private double offset; |
| 39 |
private double xMax, xMin; |
| 40 |
private double yMax; |
| 41 |
private int stepCount = 100; |
| 42 |
private static final double headRoom = 0.1; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public PDFPlot(Distribution distribution, double offset) { |
| 48 |
this.distribution = distribution; |
| 49 |
this.offset = offset; |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public void setData(double[] xData, double[] yData) { |
| 56 |
throw new UnsupportedOperationException("Not available"); |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public void setData(Variate.N xData, Variate.N yData) { |
| 63 |
throw new UnsupportedOperationException("Not available"); |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public void setupAxis(Axis xAxis, Axis yAxis, Variate xData, Variate yData) { |
| 70 |
if (distribution == null) { |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
double quantile01 = distribution.quantile(0.01); |
| 75 |
double quantile99 = distribution.quantile(0.99); |
| 76 |
|
| 77 |
|
| 78 |
if (!Double.isInfinite(distribution.getProbabilityDensityFunction().getLowerBound())) { |
| 79 |
|
| 80 |
xMin = distribution.getProbabilityDensityFunction().getLowerBound(); |
| 81 |
double value = distribution.getProbabilityDensityFunction().evaluate(xMin); |
| 82 |
if (Double.isNaN(value) || Double.isInfinite(value)) { |
| 83 |
xMin = Math.max(quantile01, distribution.getProbabilityDensityFunction().getLowerBound() + 1E-100); |
| 84 |
} |
| 85 |
} else { |
| 86 |
xMin = quantile01; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
if (!Double.isInfinite(distribution.getProbabilityDensityFunction().getUpperBound())) { |
| 91 |
|
| 92 |
xMax = distribution.getProbabilityDensityFunction().getUpperBound(); |
| 93 |
double value = distribution.getProbabilityDensityFunction().evaluate(xMax); |
| 94 |
if (Double.isNaN(value) || Double.isInfinite(value)) { |
| 95 |
xMax = Math.min(quantile99, distribution.getProbabilityDensityFunction().getUpperBound() - 1E-100); |
| 96 |
} |
| 97 |
} else { |
| 98 |
xMax = quantile99; |
| 99 |
} |
| 100 |
|
| 101 |
if (Double.isNaN(xMin) || Double.isInfinite(xMin)) { |
| 102 |
xMin = 0.0; |
| 103 |
} |
| 104 |
if (Double.isNaN(xMax) || Double.isInfinite(xMax)) { |
| 105 |
xMax = 1.0; |
| 106 |
} |
| 107 |
if (xMin == xMax) xMax += 1; |
| 108 |
|
| 109 |
double x = xMin + offset; |
| 110 |
yMax = distribution.pdf(x - offset); |
| 111 |
double step = (xMax - xMin) / stepCount; |
| 112 |
|
| 113 |
for (int i = 1; i < stepCount; i++) { |
| 114 |
x += step; |
| 115 |
double y = distribution.pdf(x - offset); |
| 116 |
if (y > yMax) yMax = y; |
| 117 |
} |
| 118 |
|
| 119 |
if (xAxis instanceof LogAxis) { |
| 120 |
throw new IllegalArgumentException("Log axis are not compatible to PDFPlot"); |
| 121 |
} else { |
| 122 |
xAxis.setRange(offset + xMin, offset + xMax); |
| 123 |
} |
| 124 |
if (yAxis instanceof LogAxis) { |
| 125 |
throw new IllegalArgumentException("Log axis are not compatible to PDFPlot"); |
| 126 |
} else { |
| 127 |
yAxis.setRange(0.0, yMax * (1.0 + headRoom)); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
@Override |
| 135 |
public void paintPlot(Graphics2D g2, double xScale, double yScale, |
| 136 |
double xOffset, double yOffset, int plotNumber, int plotCount) { |
| 137 |
|
| 138 |
if (distribution == null) { |
| 139 |
return; |
| 140 |
} |
| 141 |
|
| 142 |
super.paintPlot(g2, xScale, yScale, xOffset, yOffset, plotNumber, plotCount); |
| 143 |
|
| 144 |
g2.setPaint(linePaint); |
| 145 |
g2.setStroke(lineStroke); |
| 146 |
|
| 147 |
double x1 = xMin + offset; |
| 148 |
double y1 = distribution.pdf(x1 - offset); |
| 149 |
|
| 150 |
double step = (xMax - xMin) / stepCount; |
| 151 |
for (int i = 1; i < stepCount; i++) { |
| 152 |
double x2 = x1 + step; |
| 153 |
double y2 = distribution.pdf(x2 - offset); |
| 154 |
drawLine(g2, x1, y1, x2, y2); |
| 155 |
x1 = x2; |
| 156 |
y1 = y2; |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
@Override |
| 164 |
protected void paintData(Graphics2D g2, Variate.N xData, Variate.N yData) { |
| 165 |
|
| 166 |
} |
| 167 |
} |