| 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.ComplexArray; |
| 29 |
import dr.math.FastFourierTransform; |
| 30 |
import dr.stats.DiscreteStatistics; |
| 31 |
import dr.util.HeapSort; |
| 32 |
|
| 33 |
import java.util.Random; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public class NormalKDEDistribution extends KernelDensityEstimatorDistribution { |
| 39 |
|
| 40 |
public static final int MINIMUM_GRID_SIZE = 512; |
| 41 |
|
| 42 |
public NormalKDEDistribution(Double[] sample) { |
| 43 |
this(sample, null, null, null); |
| 44 |
} |
| 45 |
|
| 46 |
public NormalKDEDistribution(Double[] sample, Double lowerBound, Double upperBound, Double bandWidth) { |
| 47 |
this(sample, lowerBound, upperBound, bandWidth, 3.0, MINIMUM_GRID_SIZE); |
| 48 |
} |
| 49 |
|
| 50 |
public NormalKDEDistribution(Double[] sample, Double lowerBound, Double upperBound, Double bandWidth, |
| 51 |
int n) { |
| 52 |
this(sample, lowerBound, upperBound, bandWidth, 3.0, n); |
| 53 |
} |
| 54 |
|
| 55 |
public NormalKDEDistribution(Double[] sample, Double lowerBound, Double upperBound, Double bandWidth, |
| 56 |
double cut, int n) { |
| 57 |
super(sample, lowerBound, upperBound, bandWidth); |
| 58 |
this.gridSize = Math.max(n, MINIMUM_GRID_SIZE); |
| 59 |
if (this.gridSize > MINIMUM_GRID_SIZE) { |
| 60 |
this.gridSize = (int) Math.pow(2, Math.ceil(Math.log(this.gridSize) / Math.log(2.0))); |
| 61 |
} |
| 62 |
this.cut = cut; |
| 63 |
|
| 64 |
from = DiscreteStatistics.min(super.sample) - this.cut * this.bandWidth; |
| 65 |
to = DiscreteStatistics.max(super.sample) + this.cut * this.bandWidth; |
| 66 |
|
| 67 |
lo = from - 4.0 * this.bandWidth; |
| 68 |
up = to + 4.0 * this.bandWidth; |
| 69 |
|
| 70 |
densityKnown = false; |
| 71 |
} |
| 72 |
|
| 73 |
public double getFromPoint() { |
| 74 |
return from; |
| 75 |
} |
| 76 |
|
| 77 |
public double getToPoint() { |
| 78 |
return to; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
private double linearApproximate(double[] x, double[] y, double pt, double low, double high) { |
| 91 |
|
| 92 |
int i = 0; |
| 93 |
int j = x.length - 1; |
| 94 |
|
| 95 |
if (pt < x[i]) { |
| 96 |
return low; |
| 97 |
} |
| 98 |
if (pt > x[j]) { |
| 99 |
return high; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
while (i < j - 1) { |
| 104 |
int ij = (i + j) / 2; |
| 105 |
if (pt < x[ij]) { |
| 106 |
j = ij; |
| 107 |
} else { |
| 108 |
i = ij; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
if (pt == x[j]) { |
| 113 |
return y[j]; |
| 114 |
} |
| 115 |
if (pt == x[i]) { |
| 116 |
return y[i]; |
| 117 |
} |
| 118 |
return y[i] + (y[j] - y[i]) * ((pt - x[i]) / (x[j] - x[i])); |
| 119 |
} |
| 120 |
|
| 121 |
private double[] rescaleAndTrim(double[] x) { |
| 122 |
final int length = x.length / 2; |
| 123 |
final double scale = 1.0 / x.length; |
| 124 |
double[] out = new double[length]; |
| 125 |
for (int i = 0; i < length; ++i) { |
| 126 |
out[i] = x[i] * scale; |
| 127 |
if (out[i] < 0) { |
| 128 |
out[i] = 0; |
| 129 |
} |
| 130 |
} |
| 131 |
return out; |
| 132 |
} |
| 133 |
|
| 134 |
private double[] massdist(double[] x, |
| 135 |
|
| 136 |
double xlow, double xhigh, int ny) { |
| 137 |
|
| 138 |
int nx = x.length; |
| 139 |
double[] y = new double[ny * 2]; |
| 140 |
|
| 141 |
final int ixmin = 0; |
| 142 |
final int ixmax = ny - 2; |
| 143 |
final double xdelta = (xhigh - xlow) / (ny - 1); |
| 144 |
|
| 145 |
for (int i = 0; i < ny; ++i) { |
| 146 |
y[i] = 0.0; |
| 147 |
} |
| 148 |
|
| 149 |
final double xmi = 1.0 / nx; |
| 150 |
for (int i = 0; i < nx; ++i) { |
| 151 |
final double xpos = (x[i] - xlow) / xdelta; |
| 152 |
final int ix = (int) Math.floor(xpos); |
| 153 |
final double fx = xpos - ix; |
| 154 |
|
| 155 |
|
| 156 |
if (ixmin <= ix && ix <= ixmax) { |
| 157 |
y[ix] += (1 - fx) * xmi; |
| 158 |
y[ix + 1] += fx * xmi; |
| 159 |
} else if (ix == -1) { |
| 160 |
y[0] += fx * xmi; |
| 161 |
} else if (ix == ixmax + 1) { |
| 162 |
y[ix] += (1 - fx) * xmi; |
| 163 |
} |
| 164 |
} |
| 165 |
return y; |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
protected void fillKernelOrdinates(ComplexArray ordinates, double bandWidth) { |
| 174 |
final int length = ordinates.length; |
| 175 |
final double a = 1.0 / (Math.sqrt(2.0 * Math.PI) * bandWidth); |
| 176 |
final double precision = -0.5 / (bandWidth * bandWidth); |
| 177 |
for (int i = 0; i < length; i++) { |
| 178 |
final double x = ordinates.real[i]; |
| 179 |
ordinates.real[i] = a * Math.exp(x * x * precision); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
protected void computeDensity() { |
| 184 |
makeOrdinates(); |
| 185 |
transformData(); |
| 186 |
densityKnown = true; |
| 187 |
} |
| 188 |
|
| 189 |
private void transformData() { |
| 190 |
ComplexArray Y = new ComplexArray(massdist(this.sample, lo, up, this.gridSize)); |
| 191 |
FastFourierTransform.fft(Y, false); |
| 192 |
|
| 193 |
ComplexArray product = Y.product(kOrdinates); |
| 194 |
FastFourierTransform.fft(product, true); |
| 195 |
|
| 196 |
densityPoints = rescaleAndTrim(product.real); |
| 197 |
} |
| 198 |
|
| 199 |
private void makeOrdinates() { |
| 200 |
|
| 201 |
final int length = 2 * gridSize; |
| 202 |
if (kOrdinates == null) { |
| 203 |
kOrdinates = new ComplexArray(new double[length]); |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
final double max = 2.0 * (up - lo); |
| 208 |
double value = 0; |
| 209 |
final double inc = max / (length - 1); |
| 210 |
for (int i = 0; i <= gridSize; i++) { |
| 211 |
kOrdinates.real[i] = value; |
| 212 |
value += inc; |
| 213 |
} |
| 214 |
for (int i = gridSize + 1; i < length; i++) { |
| 215 |
kOrdinates.real[i] = -kOrdinates.real[length - i]; |
| 216 |
} |
| 217 |
fillKernelOrdinates(kOrdinates, bandWidth); |
| 218 |
|
| 219 |
FastFourierTransform.fft(kOrdinates, false); |
| 220 |
kOrdinates.conjugate(); |
| 221 |
|
| 222 |
|
| 223 |
xPoints = new double[gridSize]; |
| 224 |
double x = lo; |
| 225 |
double delta = (up - lo) / (gridSize - 1); |
| 226 |
for (int i = 0; i < gridSize; i++) { |
| 227 |
xPoints[i] = x; |
| 228 |
x += delta; |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
@Override |
| 233 |
protected double evaluateKernel(double x) { |
| 234 |
if (!densityKnown) { |
| 235 |
computeDensity(); |
| 236 |
} |
| 237 |
return linearApproximate(xPoints, densityPoints, x, 0.0, 0.0); |
| 238 |
} |
| 239 |
|
| 240 |
@Override |
| 241 |
protected void processBounds(Double lowerBound, Double upperBound) { |
| 242 |
if ((lowerBound != null && lowerBound != Double.NEGATIVE_INFINITY) || |
| 243 |
(upperBound != null && upperBound != Double.POSITIVE_INFINITY)) { |
| 244 |
throw new RuntimeException("NormalKDEDistribution must be unbounded"); |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
@Override |
| 249 |
protected void setBandWidth(Double bandWidth) { |
| 250 |
if (bandWidth == null) { |
| 251 |
|
| 252 |
this.bandWidth = bandwidthNRD(sample); |
| 253 |
} else |
| 254 |
this.bandWidth = bandWidth; |
| 255 |
|
| 256 |
densityKnown = false; |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
public double bandwidthNRD(double[] x) { |
| 268 |
int[] indices = new int[x.length]; |
| 269 |
HeapSort.sort(x, indices); |
| 270 |
|
| 271 |
final double h = |
| 272 |
(DiscreteStatistics.quantile(0.75, x, indices) - DiscreteStatistics.quantile(0.25, x, indices)) / 1.34; |
| 273 |
return 1.06 * |
| 274 |
Math.min(Math.sqrt(DiscreteStatistics.variance(x)), h) * |
| 275 |
Math.pow(x.length, -0.2); |
| 276 |
} |
| 277 |
|
| 278 |
private ComplexArray kOrdinates; |
| 279 |
private double[] xPoints; |
| 280 |
private double[] densityPoints; |
| 281 |
|
| 282 |
private int gridSize; |
| 283 |
private double cut; |
| 284 |
private double from; |
| 285 |
private double to; |
| 286 |
private double lo; |
| 287 |
private double up; |
| 288 |
|
| 289 |
private boolean densityKnown = false; |
| 290 |
|
| 291 |
public static void main(String[] args) { |
| 292 |
|
| 293 |
long start = System.currentTimeMillis(); |
| 294 |
|
| 295 |
Random random = new Random(1234); |
| 296 |
|
| 297 |
Double[] samples = new Double[10000000]; |
| 298 |
for (int i = 0; i < samples.length; i++) { |
| 299 |
samples[i] = random.nextDouble(); |
| 300 |
} |
| 301 |
NormalKDEDistribution nKDE = new NormalKDEDistribution(samples); |
| 302 |
|
| 303 |
for (int i = 0; i < 100; i++) { |
| 304 |
nKDE.evaluateKernel(random.nextDouble()); |
| 305 |
} |
| 306 |
|
| 307 |
long end = System.currentTimeMillis(); |
| 308 |
|
| 309 |
System.out.println("Time: " + (end-start)); |
| 310 |
|
| 311 |
} |
| 312 |
|
| 313 |
} |