| 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 |
|
| 34 |
|
| 35 |
|
| 36 |
public class LogitTransformedNormalKDEDistribution extends KernelDensityEstimatorDistribution { |
| 37 |
|
| 38 |
public static final int MINIMUM_GRID_SIZE = 2048; |
| 39 |
public static final boolean DEBUG = false; |
| 40 |
|
| 41 |
|
| 42 |
public LogitTransformedNormalKDEDistribution(Double[] sample) { |
| 43 |
this(sample, 1.0, null, null, null); |
| 44 |
} |
| 45 |
|
| 46 |
public LogitTransformedNormalKDEDistribution(Double[] sample, Double upperLimit) { |
| 47 |
this(sample, upperLimit, null, null, null); |
| 48 |
} |
| 49 |
|
| 50 |
public LogitTransformedNormalKDEDistribution(Double[] sample, int n) { |
| 51 |
this(sample, 1.0, null, null, null, 3.0, n); |
| 52 |
} |
| 53 |
|
| 54 |
public LogitTransformedNormalKDEDistribution(Double[] sample, Double upperLimit, int n) { |
| 55 |
this(sample, upperLimit, null, null, null, 3.0, n); |
| 56 |
} |
| 57 |
|
| 58 |
public LogitTransformedNormalKDEDistribution(Double[] sample, Double upperLimit, Double lowerBound, Double upperBound, Double bandWidth) { |
| 59 |
this(sample, upperLimit, lowerBound, upperBound, bandWidth, 3.0, MINIMUM_GRID_SIZE); |
| 60 |
} |
| 61 |
|
| 62 |
public LogitTransformedNormalKDEDistribution(Double[] sample, Double upperLimit, Double lowerBound, Double upperBound, Double bandWidth, |
| 63 |
int n) { |
| 64 |
this(sample, upperLimit, lowerBound, upperBound, bandWidth, 3.0, n); |
| 65 |
} |
| 66 |
|
| 67 |
public LogitTransformedNormalKDEDistribution(Double[] sample, Double upperLimit, Double lowerBound, Double upperBound, Double bandWidth, double cut, int n) { |
| 68 |
|
| 69 |
super(sample, lowerBound, upperBound, bandWidth); |
| 70 |
|
| 71 |
this.upperLimit = upperLimit; |
| 72 |
if (DEBUG) { |
| 73 |
System.out.println("Creating the KDE in logit space"); |
| 74 |
System.out.println("lowerBound = " + lowerBound); |
| 75 |
System.out.println("upperBound = " + upperBound); |
| 76 |
System.out.println("upperlimit = " + upperLimit); |
| 77 |
} |
| 78 |
|
| 79 |
this.logitSample = new double[sample.length]; |
| 80 |
for (int i = 0; i < logitSample.length; i++) { |
| 81 |
|
| 82 |
this.logitSample[i] = Math.log(sample[i] / (upperLimit - sample[i])); |
| 83 |
} |
| 84 |
|
| 85 |
this.backupSample = new double[sample.length]; |
| 86 |
for (int i = 0; i < sample.length; i++) { |
| 87 |
this.backupSample[i] = sample[i]; |
| 88 |
} |
| 89 |
|
| 90 |
this.sample = logitSample; |
| 91 |
processBounds(lowerBound, upperBound); |
| 92 |
setBandWidth(bandWidth); |
| 93 |
|
| 94 |
this.gridSize = Math.max(n, MINIMUM_GRID_SIZE); |
| 95 |
if (this.gridSize > MINIMUM_GRID_SIZE) { |
| 96 |
this.gridSize = (int) Math.pow(2, Math.ceil(Math.log(this.gridSize) / Math.log(2.0))); |
| 97 |
} |
| 98 |
this.cut = cut; |
| 99 |
|
| 100 |
from = DiscreteStatistics.min(this.sample) - this.cut * this.bandWidth; |
| 101 |
to = DiscreteStatistics.max(this.sample) + this.cut * this.bandWidth; |
| 102 |
|
| 103 |
if (DEBUG) { |
| 104 |
System.out.println("bandWidth = " + this.bandWidth); |
| 105 |
System.out.println("cut = " + this.cut); |
| 106 |
System.out.println("from = " + from); |
| 107 |
System.out.println("to = " + to); |
| 108 |
} |
| 109 |
|
| 110 |
lo = from - 4.0 * this.bandWidth; |
| 111 |
up = to + 4.0 * this.bandWidth; |
| 112 |
|
| 113 |
if (DEBUG) { |
| 114 |
System.out.println("lo = " + lo); |
| 115 |
System.out.println("up = " + up); |
| 116 |
} |
| 117 |
|
| 118 |
densityKnown = false; |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
computeDensity(); |
| 123 |
|
| 124 |
} |
| 125 |
|
| 126 |
public double getFromPoint() { |
| 127 |
return from; |
| 128 |
} |
| 129 |
|
| 130 |
public double getToPoint() { |
| 131 |
return to; |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
private double linearApproximate(double[] x, double[] y, double pt, double low, double high) { |
| 144 |
|
| 145 |
int i = 0; |
| 146 |
int j = x.length - 1; |
| 147 |
|
| 148 |
if (pt < x[i]) { |
| 149 |
return low; |
| 150 |
} |
| 151 |
if (pt > x[j]) { |
| 152 |
return high; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
while (i < j - 1) { |
| 157 |
int ij = (i + j) / 2; |
| 158 |
if (pt < x[ij]) { |
| 159 |
j = ij; |
| 160 |
} else { |
| 161 |
i = ij; |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
if (pt == x[j]) { |
| 166 |
return y[j]; |
| 167 |
} |
| 168 |
if (pt == x[i]) { |
| 169 |
return y[i]; |
| 170 |
} |
| 171 |
|
| 172 |
return y[i] + (y[j] - y[i]) * ((pt - x[i]) / (x[j] - x[i])); |
| 173 |
} |
| 174 |
|
| 175 |
private double[] rescaleAndTrim(double[] x) { |
| 176 |
final int length = x.length / 2; |
| 177 |
final double scale = 1.0 / x.length; |
| 178 |
double[] out = new double[length]; |
| 179 |
for (int i = 0; i < length; ++i) { |
| 180 |
out[i] = x[i] * scale; |
| 181 |
if (out[i] < 0) { |
| 182 |
out[i] = 0; |
| 183 |
} |
| 184 |
} |
| 185 |
return out; |
| 186 |
} |
| 187 |
|
| 188 |
private double[] massdist(double[] x, double xlow, double xhigh, int ny) { |
| 189 |
|
| 190 |
int nx = x.length; |
| 191 |
double[] y = new double[ny * 2]; |
| 192 |
|
| 193 |
final int ixmin = 0; |
| 194 |
final int ixmax = ny - 2; |
| 195 |
final double xdelta = (xhigh - xlow) / (ny - 1); |
| 196 |
|
| 197 |
for (int i = 0; i < ny; ++i) { |
| 198 |
y[i] = 0.0; |
| 199 |
} |
| 200 |
|
| 201 |
final double xmi = 1.0 / nx; |
| 202 |
for (int i = 0; i < nx; ++i) { |
| 203 |
final double xpos = (x[i] - xlow) / xdelta; |
| 204 |
final int ix = (int) Math.floor(xpos); |
| 205 |
final double fx = xpos - ix; |
| 206 |
|
| 207 |
|
| 208 |
if (ixmin <= ix && ix <= ixmax) { |
| 209 |
y[ix] += (1 - fx) * xmi; |
| 210 |
y[ix + 1] += fx * xmi; |
| 211 |
} else if (ix == -1) { |
| 212 |
y[0] += fx * xmi; |
| 213 |
} else if (ix == ixmax + 1) { |
| 214 |
y[ix] += (1 - fx) * xmi; |
| 215 |
} |
| 216 |
} |
| 217 |
return y; |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
protected void fillKernelOrdinates(ComplexArray ordinates, double bandWidth) { |
| 226 |
final int length = ordinates.length; |
| 227 |
final double a = 1.0 / (Math.sqrt(2.0 * Math.PI) * bandWidth); |
| 228 |
final double precision = -0.5 / (bandWidth * bandWidth); |
| 229 |
for (int i = 0; i < length; i++) { |
| 230 |
final double x = ordinates.real[i]; |
| 231 |
ordinates.real[i] = a * Math.exp(x * x * precision); |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
protected void computeDensity() { |
| 236 |
|
| 237 |
makeOrdinates(); |
| 238 |
|
| 239 |
transformData(); |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
transformEstimator(); |
| 244 |
densityKnown = true; |
| 245 |
} |
| 246 |
|
| 247 |
private void transformEstimator() { |
| 248 |
|
| 249 |
if (DEBUG) { |
| 250 |
System.out.println("\nCreating the KDE in normal space"); |
| 251 |
System.out.println("lowerBound = " + lowerBound); |
| 252 |
System.out.println("upperBound = " + upperBound); |
| 253 |
System.out.println("upperlimit = " + upperLimit); |
| 254 |
} |
| 255 |
|
| 256 |
this.sample = backupSample; |
| 257 |
|
| 258 |
setBandWidth(null); |
| 259 |
|
| 260 |
from = DiscreteStatistics.min(this.sample) - this.cut * this.bandWidth; |
| 261 |
to = DiscreteStatistics.max(this.sample) + this.cut * this.bandWidth; |
| 262 |
|
| 263 |
if (DEBUG) { |
| 264 |
System.out.println("bandWidth = " + this.bandWidth); |
| 265 |
System.out.println("cut = " + this.cut); |
| 266 |
System.out.println("from = " + from); |
| 267 |
System.out.println("to = " + to); |
| 268 |
} |
| 269 |
|
| 270 |
lo = from - 4.0 * this.bandWidth; |
| 271 |
up = to + 4.0 * this.bandWidth; |
| 272 |
|
| 273 |
if (DEBUG) { |
| 274 |
System.out.println("lo = " + lo); |
| 275 |
System.out.println("up = " + up); |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
this.backupXPoints = new double[xPoints.length]; |
| 288 |
System.arraycopy(xPoints, 0, backupXPoints, 0, xPoints.length); |
| 289 |
makeOrdinates(); |
| 290 |
|
| 291 |
int numberOfNegatives = 0; |
| 292 |
if (DEBUG) { |
| 293 |
System.out.println("\nxPoints length = " + xPoints.length); |
| 294 |
} |
| 295 |
for (int i = 0; i < xPoints.length; i++) { |
| 296 |
if (xPoints[i] < 0.0) { |
| 297 |
numberOfNegatives++; |
| 298 |
} |
| 299 |
|
| 300 |
} |
| 301 |
if (DEBUG) { |
| 302 |
System.out.println("number of negative xPoints = " + numberOfNegatives); |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
this.finalXPoints = new double[xPoints.length - numberOfNegatives]; |
| 308 |
System.arraycopy(xPoints, numberOfNegatives, finalXPoints, 0, xPoints.length - numberOfNegatives); |
| 309 |
if (DEBUG) { |
| 310 |
for (int i = 0; i < xPoints.length; i++) { |
| 311 |
System.out.println(backupXPoints[i] + " : " + densityPoints[i]); |
| 312 |
} |
| 313 |
System.out.println("\nfinalXPoints length = " + finalXPoints.length); |
| 314 |
} |
| 315 |
|
| 316 |
this.finalDensityPoints = new double[densityPoints.length - numberOfNegatives]; |
| 317 |
|
| 318 |
for (int i = 0; i < finalXPoints.length; i++) { |
| 319 |
finalDensityPoints[i] = linearApproximate(backupXPoints, densityPoints, Math.log(finalXPoints[i]/(upperLimit - finalXPoints[i])), 0.0, 0.0)*(1.0/(finalXPoints[i]*(upperLimit-finalXPoints[i]))); |
| 320 |
if (DEBUG) { |
| 321 |
System.out.println(finalXPoints[i] + "\t" + finalDensityPoints[i]); |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
} |
| 328 |
|
| 329 |
private void transformData() { |
| 330 |
ComplexArray Y = new ComplexArray(massdist(this.logitSample, lo, up, this.gridSize)); |
| 331 |
FastFourierTransform.fft(Y, false); |
| 332 |
|
| 333 |
ComplexArray product = Y.product(kOrdinates); |
| 334 |
FastFourierTransform.fft(product, true); |
| 335 |
|
| 336 |
densityPoints = rescaleAndTrim(product.real); |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
} |
| 342 |
|
| 343 |
private void makeOrdinates() { |
| 344 |
|
| 345 |
final int length = 2 * gridSize; |
| 346 |
if (kOrdinates == null) { |
| 347 |
kOrdinates = new ComplexArray(new double[length]); |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
final double max = 2.0 * (up - lo); |
| 352 |
double value = 0; |
| 353 |
final double inc = max / (length - 1); |
| 354 |
for (int i = 0; i <= gridSize; i++) { |
| 355 |
kOrdinates.real[i] = value; |
| 356 |
value += inc; |
| 357 |
} |
| 358 |
for (int i = gridSize + 1; i < length; i++) { |
| 359 |
kOrdinates.real[i] = -kOrdinates.real[length - i]; |
| 360 |
} |
| 361 |
fillKernelOrdinates(kOrdinates, bandWidth); |
| 362 |
|
| 363 |
FastFourierTransform.fft(kOrdinates, false); |
| 364 |
kOrdinates.conjugate(); |
| 365 |
|
| 366 |
|
| 367 |
xPoints = new double[gridSize]; |
| 368 |
double x = lo; |
| 369 |
double delta = (up - lo) / (gridSize - 1); |
| 370 |
|
| 371 |
for (int i = 0; i < gridSize; i++) { |
| 372 |
xPoints[i] = x; |
| 373 |
x += delta; |
| 374 |
|
| 375 |
} |
| 376 |
|
| 377 |
} |
| 378 |
|
| 379 |
@Override |
| 380 |
protected double evaluateKernel(double x) { |
| 381 |
if (!densityKnown) { |
| 382 |
|
| 383 |
computeDensity(); |
| 384 |
} |
| 385 |
|
| 386 |
return linearApproximate(finalXPoints, finalDensityPoints, x, 0.0, 0.0); |
| 387 |
} |
| 388 |
|
| 389 |
@Override |
| 390 |
protected void processBounds(Double lowerBound, Double upperBound) { |
| 391 |
if ((lowerBound != null && lowerBound != Double.NEGATIVE_INFINITY) || |
| 392 |
(upperBound != null && upperBound != Double.POSITIVE_INFINITY)) { |
| 393 |
throw new RuntimeException("LogitTransformedNormalKDEDistribution must be unbounded"); |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
@Override |
| 398 |
protected void setBandWidth(Double bandWidth) { |
| 399 |
if (bandWidth == null) { |
| 400 |
|
| 401 |
this.bandWidth = bandwidthNRD(sample); |
| 402 |
} else |
| 403 |
this.bandWidth = bandWidth; |
| 404 |
|
| 405 |
densityKnown = false; |
| 406 |
} |
| 407 |
|
| 408 |
public double bandwidthNRD(double[] x) { |
| 409 |
int[] indices = new int[x.length]; |
| 410 |
HeapSort.sort(x, indices); |
| 411 |
|
| 412 |
final double h = |
| 413 |
(DiscreteStatistics.quantile(0.75, x, indices) - DiscreteStatistics.quantile(0.25, x, indices)) / 1.34; |
| 414 |
return 1.06 * |
| 415 |
Math.min(Math.sqrt(DiscreteStatistics.variance(x)), h) * |
| 416 |
Math.pow(x.length, -0.2); |
| 417 |
} |
| 418 |
|
| 419 |
private ComplexArray kOrdinates; |
| 420 |
private double[] xPoints, finalXPoints, backupXPoints; |
| 421 |
private double[] densityPoints, finalDensityPoints; |
| 422 |
private double[] backupSample, logitSample; |
| 423 |
|
| 424 |
private int gridSize; |
| 425 |
private double cut; |
| 426 |
private double from; |
| 427 |
private double to; |
| 428 |
private double lo; |
| 429 |
private double up; |
| 430 |
|
| 431 |
private double upperLimit; |
| 432 |
|
| 433 |
private boolean densityKnown = false; |
| 434 |
|
| 435 |
public static void main(String[] args) { |
| 436 |
|
| 437 |
double upperlimit = 3.0; |
| 438 |
|
| 439 |
|
| 440 |
Double[] samples = new Double[2001]; |
| 441 |
NormalDistribution dist = new NormalDistribution(0.5, 0.08); |
| 442 |
for (int i = 0; i < samples.length; i++) { |
| 443 |
samples[i] = upperlimit-Math.abs((Double)dist.nextRandom()-0.5); |
| 444 |
} |
| 445 |
|
| 446 |
|
| 447 |
System.out.print("par(mfrow=c(2,2))\n\nsamples <- c("); |
| 448 |
for (int i = 0; i < samples.length-1; i++) { |
| 449 |
System.out.print(samples[i] + ","); |
| 450 |
} |
| 451 |
System.out.println(samples[samples.length-1] + ")\n"); |
| 452 |
System.out.println("hist(samples,200,xlim=c(" + (upperlimit-0.5) + "," + (upperlimit+0.5) + "))\n"); |
| 453 |
System.out.println("plot(density(samples),xlim=c(" + (upperlimit-0.5) + "," + (upperlimit+0.5) + "))\n"); |
| 454 |
|
| 455 |
LogitTransformedNormalKDEDistribution ltn = new LogitTransformedNormalKDEDistribution(samples, upperlimit); |
| 456 |
NormalKDEDistribution nKDE = new NormalKDEDistribution(samples); |
| 457 |
|
| 458 |
System.out.print("normalKDE <- c("); |
| 459 |
for (int i = ((int)upperlimit-1)*1000; i < ((int)upperlimit+1)*1000; i++) { |
| 460 |
Double test = 0.0 + ((double)i)/(1000); |
| 461 |
System.out.print(nKDE.evaluateKernel(test) + ","); |
| 462 |
} |
| 463 |
System.out.println(nKDE.evaluateKernel((((int)upperlimit+1)*1000)/((upperlimit*1000))) + ")\n"); |
| 464 |
System.out.println("index <- seq(" + (upperlimit-1) + "," + (upperlimit+1) + ",by=0.001)"); |
| 465 |
System.out.println("plot(index,normalKDE,type=\"l\",xlim=c(" + (upperlimit-0.5) + "," + (upperlimit+0.5) + "))\n"); |
| 466 |
|
| 467 |
System.out.print("TransKDE <- c("); |
| 468 |
for (int i = ((int)upperlimit-1)*1000; i < ((int)upperlimit+1)*1000; i++) { |
| 469 |
Double test = 0.0 + ((double)i)/(1000); |
| 470 |
System.out.print(ltn.evaluateKernel(test) + ","); |
| 471 |
} |
| 472 |
System.out.println(ltn.evaluateKernel((((int)upperlimit+1)*1000)/((upperlimit*1000))) + ")\n"); |
| 473 |
System.out.println("plot(index,TransKDE,type=\"l\",xlim=c(" + (upperlimit-0.5) + "," + (upperlimit+0.5) + "))\n"); |
| 474 |
|
| 475 |
} |
| 476 |
|
| 477 |
} |