| 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.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 |
|
| 36 |
|
| 37 |
|
| 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 |
|
| 86 |
xCoordinates.add(x); |
| 87 |
yCoordinates.add(kde.pdf(x)); |
| 88 |
x += frequency.getBinSize(); |
| 89 |
count++; |
| 90 |
} |
| 91 |
|
| 92 |
while (kde.pdf(x) > minDensity ) { |
| 93 |
|
| 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 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 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 |
|
| 128 |
KernelDensityEstimatorDistribution kde = kdeTest.getKernelDensityEstimatorDistribution(); |
| 129 |
System.out.println("Bandwidth: " + kde.getBandWidth()); |
| 130 |
System.out.println(); |
| 131 |
} |
| 132 |
|
| 133 |
} |