Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * KDENumericalDensityPlot.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.inference.trace.TraceDistribution;
29 import dr.math.distributions.GammaKDEDistribution;
30 import dr.math.distributions.KernelDensityEstimatorDistribution;
31 import dr.math.distributions.LogTransformedNormalKDEDistribution;
32 import dr.math.distributions.NormalKDEDistribution;
33 import dr.stats.Variate;
34 import dr.util.FrequencyDistribution;
35
36 import java.util.List;
37
38 /**
39 * @author Marc A. Suchard
40 */
41 public class KDENumericalDensityPlot extends NumericalDensityPlot {
42 private final static boolean DEBUG = false;
43
44 protected static final int DEFAULT_KDE_BINS = 5000;
45
46 public KDENumericalDensityPlot(List<Double> data) {
47 super(data, DEFAULT_KDE_BINS);
48 }
49
50 public KDENumericalDensityPlot(List<Double> data, int minimumBinCount) {
51 super(data, minimumBinCount);
52 }
53
54 private KernelDensityEstimatorDistribution getKDE(Double[] samples) {
55 // System.err.println("samples is null? " + (samples == null ? "yes" : "no"));
56 // System.err.println("type is null? " + (type == null ? "yes" : "no"));
57 type = KernelDensityEstimatorDistribution.Type.GAUSSIAN;
58 switch (type) {
59 case GAUSSIAN: return new NormalKDEDistribution(samples);
60 case GAMMA: return new GammaKDEDistribution(samples);
61 case LOG_TRANSFORMED_GAUSSIAN: return new LogTransformedNormalKDEDistribution(samples);
62 default:
63 throw new RuntimeException("Unknown type");
64 }
65 }
66
67 /**
68 * Set data
69 */
70 public void setData(Variate.D data, int minimumBinCount) {
71
72 setRawData(data);
73 Double[] samples = new Double[data.getCount()];
74 for (int i = 0; i < data.getCount(); i++) {
75 samples[i] = data.get(i);
76 }
77 kde = getKDE(samples);
78
79 FrequencyDistribution frequency = getFrequencyDistribution(data, minimumBinCount);
80
81 Variate.D xData = new Variate.D();
82 Variate.D yData = new Variate.D();
83
84 // double x = frequency.getLowerBound() - frequency.getBinSize();
85 // double maxDensity = 0.0;
86 // // TODO Compute KDE once
87 //// for (int i = 0; i < frequency.getBinCount(); i++) {
88 //// double density = frequency.getFrequency(i) / frequency.getBinSize() / data.getValuesSize();
89 //// if (density > maxDensity) maxDensity = density;
90 //// }
91 //
92 // xData.add(x + (frequency.getBinSize() / 2.0));
93 // yData.add(0.0);
94 // x += frequency.getBinSize();
95 //
96 // for (int i = 0; i < frequency.getBinCount(); i++) {
97 // double xPoint = x + (frequency.getBinSize() / 2.0);
98 // xData.add(xPoint);
99 //// double density = frequency.getFrequency(i) / frequency.getBinSize() / data.getValuesSize();
100 // double density = kde.pdf(xPoint);
101 // if (relativeDensity) {
102 // yData.add(density / maxDensity);
103 // } else {
104 // yData.add(density);
105 // }
106 // x += frequency.getBinSize();
107 // }
108 //
109 // xData.add(x + (frequency.getBinSize() / 2.0));
110 // yData.add(0.0);
111 double x = frequency.getLowerBound() - (frequency.getBinSize() / 2.0);
112 int extraEdgeCount = 0;
113 while (kde.pdf(x) > minDensity && x > lowerBoundary) {
114 x -= frequency.getBinSize();
115 extraEdgeCount += 1;
116 }
117 xData.add(x);
118 yData.add(0.0);
119 x += frequency.getBinSize();
120 int count = 0;
121 while (count < (frequency.getBinCount() + extraEdgeCount)) {// ||
122 // (kde.pdf(x) > minDensity && x < upperBoundary)) {
123 xData.add(x);
124 yData.add(kde.pdf(x));
125 x += frequency.getBinSize();
126 count++;
127 }
128 if (DEBUG) {
129 System.err.println("kde = " + kde.pdf(x));
130 }
131 while (kde.pdf(x) > minDensity ) {
132 if (DEBUG) {
133 System.err.println("add bit on end!!!");
134 }
135 xData.add(x);
136 yData.add(kde.pdf(x));
137 x += frequency.getBinSize();
138 }
139 xData.add(x);
140 yData.add(0.0);
141
142
143
144 //
145 //
146 // int extraBinsOnEdges = 5;
147 // double x = frequency.getLowerBound() - extraBinsOnEdges * frequency.getBinSize();
148 // for (int i = 0; i < frequency.getBinCount() + 2 * extraBinsOnEdges; i++) {
149 // double xMidPoint = x + (frequency.getBinSize() / 2.0);
150 // xData.add(xMidPoint);
151 // yData.add(kde.pdf(xMidPoint));
152 // x += frequency.getBinSize();
153 // }
154
155 setData(xData, yData);
156 }
157
158 protected Variate getXCoordinates(int numPoints) {
159 Double[] points = new Double[numPoints];
160 for (int i = 0; i < numPoints; i++) {
161 points[i] = (double) i;
162 }
163 return new Variate.D(points);
164 }
165
166 protected Variate getYCoordinates(Variate.D xData) {
167 final int length = xData.getCount();
168 Double[] points = new Double[length];
169 for (int i = 0; i < length; i++) {
170 points[i] = kde.pdf(xData.get(i));
171 }
172 return new Variate.D(points);
173 }
174
175 protected double getQuantile(double y) {
176 return kde.quantile(y);
177 }
178
179 protected double getDensity(double x) {
180 return kde.pdf(x);
181 }
182
183 private KernelDensityEstimatorDistribution kde;
184 private NumericalDensityPlot densityPlot;
185
186 private KernelDensityEstimatorDistribution.Type type;
187
188 private double lowerBoundary = 0;
189 private double upperBoundary = Double.POSITIVE_INFINITY;
190 private static final double minDensity = 10E-6;
191
192 // @Override
193 //
194 // protected void paintData(Graphics2D g2, Variate xData, Variate yData) {
195 //
196 // double x = transformX(xData.get(0));
197 // double y = transformY(yData.get(0));
198 //
199 // GeneralPath path = new GeneralPath();
200 // path.moveTo((float) x, (float) y);
201 //
202 // int n = xData.getValuesSize();
203 // boolean failed = false;
204 // for (int i = 1; i < n; i++) {
205 // x = transformX(xData.get(i));
206 // y = transformY(yData.get(i));
207 // if (x == Double.NEGATIVE_INFINITY || y == Double.NEGATIVE_INFINITY ||
208 // Double.isNaN(x) || Double.isNaN(y)) {
209 // failed = true;
210 // } else if (failed) {
211 // failed = false;
212 // path.moveTo((float) x, (float) y);
213 // } else {
214 // path.lineTo((float) x, (float) y);
215 // }
216 // }
217 //
218 // g2.setPaint(linePaint);
219 // g2.setStroke(lineStroke);
220 //
221 // g2.draw(path);
222 //
223 // }
224 }