Debian Codegraph @

src/dr/inference/trace/KernelDensityEstimate.java

raw symbols SCIP
1 /*
2 * KernelDensityEstimate.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.inference.trace;
27
28 import dr.math.distributions.GammaKDEDistribution;
29 import dr.math.distributions.KernelDensityEstimatorDistribution;
30 import dr.math.distributions.NormalKDEDistribution;
31 import dr.stats.Variate;
32 import dr.util.FrequencyDistribution;
33
34 /**
35 * @author Marc A. Suchard
36 * @author Andrew Rambaut
37 * @version $Id$
38 */
39 public class KernelDensityEstimate extends DensityEstimate {
40
41 public KernelDensityEstimate(Double[] samples, int minimumBinCount) {
42 this(KernelDensityEstimatorDistribution.Type.GAUSSIAN, samples, minimumBinCount);
43 }
44
45 public KernelDensityEstimate(KernelDensityEstimatorDistribution.Type type, Double[] samples, int minimumBinCount) {
46 super(samples, minimumBinCount);
47
48 switch (type) {
49 case GAUSSIAN: this.kde = new NormalKDEDistribution(samples);
50 case GAMMA: this.kde = new GammaKDEDistribution(samples);
51 default:
52 throw new RuntimeException("Unknown type");
53 }
54 }
55
56 public KernelDensityEstimate(String type, Double[] samples, int minimumBinCount) {
57 super(samples, minimumBinCount);
58
59 this.kde = new NormalKDEDistribution(samples);
60
61 }
62
63 public KernelDensityEstimatorDistribution getKernelDensityEstimatorDistribution() {
64 return this.kde;
65 }
66
67 protected void calculateDensity(Variate data, int minimumBinCount) {
68
69 FrequencyDistribution frequency = calculateFrequencies(data, minimumBinCount);
70
71 xCoordinates = new Variate.D();
72 yCoordinates = new Variate.D();
73
74 double x = frequency.getLowerBound() - (frequency.getBinSize() / 2.0);
75 int extraEdgeCount = 0;
76 while (kde.pdf(x) > minDensity && x > lowerBoundary) {
77 x -= frequency.getBinSize();
78 extraEdgeCount += 1;
79 }
80 xCoordinates.add(x);
81 yCoordinates.add(0.0);
82 x += frequency.getBinSize();
83 int count = 0;
84 while (count < (frequency.getBinCount() + extraEdgeCount)) {// ||
85 // (kde.pdf(x) > minDensity && x < upperBoundary)) {
86 xCoordinates.add(x);
87 yCoordinates.add(kde.pdf(x));
88 x += frequency.getBinSize();
89 count++;
90 }
91 // System.err.println("kde = " + kde.pdf(x));
92 while (kde.pdf(x) > minDensity ) {
93 // System.err.println("add bit on end!!!");
94 xCoordinates.add(x);
95 yCoordinates.add(kde.pdf(x));
96 x += frequency.getBinSize();
97 }
98 xCoordinates.add(x);
99 yCoordinates.add(0.0);
100
101 //
102 //
103 // int extraBinsOnEdges = 5;
104 // double x = frequency.getLowerBound() - extraBinsOnEdges * frequency.getBinSize();
105 // for (int i = 0; i < frequency.getBinCount() + 2 * extraBinsOnEdges; i++) {
106 // double xMidPoint = x + (frequency.getBinSize() / 2.0);
107 // xData.add(xMidPoint);
108 // yData.add(kde.pdf(xMidPoint));
109 // x += frequency.getBinSize();
110 // }
111 }
112
113 private KernelDensityEstimatorDistribution kde;
114
115 private double lowerBoundary = 0;
116 private double upperBoundary = Double.POSITIVE_INFINITY;
117 private static final double minDensity = 10E-6;
118
119 public static void main(String[] args) {
120 Double[] samples = new Double[101];
121 for (int i = 0; i <= 100; i++) {
122 samples[i] = ((double)i)/100.0;
123 System.out.println(samples[i]);
124 }
125 KernelDensityEstimate kdeTest = new KernelDensityEstimate("Gaussian", samples, 512);
126 Variate.D samplesTwo = new Variate.D(samples);
127 //kdeTest.calculateDensitykde(samplesTwo, 512);
128 KernelDensityEstimatorDistribution kde = kdeTest.getKernelDensityEstimatorDistribution();
129 System.out.println("Bandwidth: " + kde.getBandWidth());
130 System.out.println();
131 }
132
133 }