| 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.math.distributions; |
| 27 |
|
| 28 |
import dr.math.GammaFunction; |
| 29 |
import dr.stats.DiscreteStatistics; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public class BetaKDEDistribution extends KernelDensityEstimatorDistribution { |
| 41 |
|
| 42 |
public BetaKDEDistribution(Double[] sample, Double lowerBound, Double upperBound, Double bandWidth) { |
| 43 |
super(sample, lowerBound, upperBound, bandWidth); |
| 44 |
|
| 45 |
} |
| 46 |
|
| 47 |
@Override |
| 48 |
public double getFromPoint() { |
| 49 |
throw new RuntimeException("Not yet implemented"); |
| 50 |
} |
| 51 |
|
| 52 |
@Override |
| 53 |
public double getToPoint() { |
| 54 |
throw new RuntimeException("Not yet implemented"); |
| 55 |
} |
| 56 |
|
| 57 |
protected void processBounds(Double lowerBound, Double upperBound) { |
| 58 |
if (lowerBound == null || upperBound == null || upperBound - lowerBound <= 0) { |
| 59 |
throw new RuntimeException("BetaKDEDistribution must be bounded"); |
| 60 |
} |
| 61 |
if (lowerBound > DiscreteStatistics.min(sample) || upperBound < DiscreteStatistics.max(sample)) |
| 62 |
throw new RuntimeException("Sample range outside bounds: "+DiscreteStatistics.min(sample)+" -> "+DiscreteStatistics.max(sample)); |
| 63 |
|
| 64 |
this.lowerBound = lowerBound; |
| 65 |
this.upperBound = upperBound; |
| 66 |
|
| 67 |
|
| 68 |
double[] oldSample = sample; |
| 69 |
sample = new double[sample.length]; |
| 70 |
|
| 71 |
range = upperBound - lowerBound; |
| 72 |
for(int i=0; i<N; i++) |
| 73 |
sample[i] = (oldSample[i] - this.lowerBound) / range; |
| 74 |
} |
| 75 |
|
| 76 |
protected void setBandWidth(Double bandWidth) { |
| 77 |
if (bandWidth == null) { |
| 78 |
|
| 79 |
double sigma = DiscreteStatistics.stdev(sample); |
| 80 |
this.bandWidth = sigma * Math.pow(N,-0.4); |
| 81 |
} else |
| 82 |
this.bandWidth = bandWidth; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
protected double evaluateKernel(double x) { |
| 90 |
|
| 91 |
double xPrime = (x - lowerBound) / range; |
| 92 |
double alphaMinus1 = xPrime/bandWidth - 1.0; |
| 93 |
double betaMinus1 = (1.0 - xPrime)/bandWidth - 1.0; |
| 94 |
|
| 95 |
if (xPrime < 2*bandWidth) |
| 96 |
alphaMinus1 = getRho(xPrime,bandWidth) - 1.0; |
| 97 |
else if (xPrime > 1 - 2*bandWidth) |
| 98 |
betaMinus1 = getRho(1.0-xPrime,bandWidth) - 1.0; |
| 99 |
|
| 100 |
double logK = GammaFunction.lnGamma(alphaMinus1+betaMinus1+2.0) - GammaFunction.lnGamma(alphaMinus1+1.0) - GammaFunction.lnGamma(betaMinus1+1.0); |
| 101 |
|
| 102 |
double pdf = 0; |
| 103 |
for(int i=0; i<N; i++) |
| 104 |
pdf += Math.pow(sample[i],alphaMinus1) * Math.pow(1.0-sample[i],betaMinus1); |
| 105 |
|
| 106 |
return pdf * Math.exp(logK) / (double)N / range; |
| 107 |
} |
| 108 |
|
| 109 |
private double getRho(double x, double bandWidth) { |
| 110 |
return 2*bandWidth*bandWidth + 2.5 - Math.sqrt(4*bandWidth*bandWidth*bandWidth*bandWidth |
| 111 |
+ 6*bandWidth*bandWidth |
| 112 |
+ 2.25 |
| 113 |
- x*x |
| 114 |
- x/bandWidth); |
| 115 |
} |
| 116 |
|
| 117 |
public double sampleMean() { return DiscreteStatistics.mean(sample); } |
| 118 |
|
| 119 |
private double range; |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
} |