Debian Codegraph @

src/dr/app/gui/chart/PDFPlot.java

raw symbols SCIP
1 /*
2 * PDFPlot.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
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 * Constructor
46 */
47 public PDFPlot(Distribution distribution, double offset) {
48 this.distribution = distribution;
49 this.offset = offset;
50 }
51
52 /**
53 * Set data
54 */
55 public void setData(double[] xData, double[] yData) {
56 throw new UnsupportedOperationException("Not available");
57 }
58
59 /**
60 * Set data
61 */
62 public void setData(Variate.N xData, Variate.N yData) {
63 throw new UnsupportedOperationException("Not available");
64 }
65
66 /**
67 * Set up the axis with some data
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 // if the distribution has a bound then use it, otherwise find a range using a small quantile
78 if (!Double.isInfinite(distribution.getProbabilityDensityFunction().getLowerBound())) {
79 // the actual bound may be undefined so come just inside it...
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 // if the distribution has a bound then use it, otherwise find a range using a small quantile
90 if (!Double.isInfinite(distribution.getProbabilityDensityFunction().getUpperBound())) {
91 // the actual bound may be undefined so come just inside it...
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 * Paint actual plot
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 * Paint data series
162 */
163 @Override
164 protected void paintData(Graphics2D g2, Variate.N xData, Variate.N yData) {
165 // do nothing because paintPlot is overridden
166 }
167 }