| 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.geo; |
| 27 |
|
| 28 |
import cern.colt.list.DoubleArrayList; |
| 29 |
import cern.jet.stat.Descriptive; |
| 30 |
import dr.math.distributions.NormalDistribution; |
| 31 |
import dr.math.matrixAlgebra.Matrix; |
| 32 |
import dr.math.matrixAlgebra.Vector; |
| 33 |
import dr.stats.DiscreteStatistics; |
| 34 |
import dr.geo.contouring.ContourGenerator; |
| 35 |
import dr.geo.contouring.ContourAttrib; |
| 36 |
import dr.geo.contouring.ContourPath; |
| 37 |
import dr.geo.contouring.ContourMaker; |
| 38 |
|
| 39 |
import java.util.Arrays; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public class KernelDensityEstimator2D implements ContourMaker { |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public KernelDensityEstimator2D(final double[] x, final double[] y, final double[] h, final int n, final double[] lims) { |
| 79 |
this(x, y, h, n, lims, false); |
| 80 |
} |
| 81 |
|
| 82 |
public KernelDensityEstimator2D(final double[] x, final double[] y, final double[] h, final int n, final double[] lims, boolean bandwdithLimited) { |
| 83 |
this.x = x; |
| 84 |
this.y = y; |
| 85 |
if (x.length != y.length) |
| 86 |
throw new RuntimeException("data vectors must be the same length"); |
| 87 |
|
| 88 |
this.nx = x.length; |
| 89 |
|
| 90 |
if (n <= 0) |
| 91 |
throw new RuntimeException("must have a positive number of grid points"); |
| 92 |
this.n = n; |
| 93 |
|
| 94 |
if (lims != null) |
| 95 |
this.lims = lims; |
| 96 |
else |
| 97 |
setupLims(); |
| 98 |
|
| 99 |
this.limitBandwidth = bandwdithLimited; |
| 100 |
if (h != null) |
| 101 |
this.h = h; |
| 102 |
else |
| 103 |
setupH(); |
| 104 |
|
| 105 |
doKDE2D(); |
| 106 |
} |
| 107 |
|
| 108 |
public KernelDensityEstimator2D(final double[] x, final double[] y, boolean limitBandwidth) { |
| 109 |
this(x,y,null,50,null,limitBandwidth); |
| 110 |
} |
| 111 |
|
| 112 |
public KernelDensityEstimator2D(final double[] x, final double[] y) { |
| 113 |
this(x,y,null,50,null); |
| 114 |
} |
| 115 |
|
| 116 |
public KernelDensityEstimator2D(final double[] x, final double[] y, final int n) { |
| 117 |
this(x,y,null,n,null); |
| 118 |
} |
| 119 |
|
| 120 |
public void doKDE2D() { |
| 121 |
gx = makeSequence(lims[0], lims[1], n); |
| 122 |
gy = makeSequence(lims[2], lims[3], n); |
| 123 |
double[][] ax = outerMinusScaled(gx, x, h[0]); |
| 124 |
double[][] ay = outerMinusScaled(gy, y, h[1]); |
| 125 |
normalize(ax); |
| 126 |
normalize(ay); |
| 127 |
z = new double[n][n]; |
| 128 |
double scale = nx * h[0] * h[1]; |
| 129 |
for (int i = 0; i < n; i++) { |
| 130 |
for (int j = 0; j < n; j++) { |
| 131 |
double value = 0; |
| 132 |
for (int k = 0; k < nx; k++) { |
| 133 |
value += ax[i][k] * ay[j][k]; |
| 134 |
} |
| 135 |
z[i][j] = value / scale; |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
public double findLevelCorrespondingToMass(double probabilityMass) { |
| 141 |
double level = 0; |
| 142 |
double[] sz = new double[n*n]; |
| 143 |
double[] c1 = new double[n*n]; |
| 144 |
for(int i=0; i<n; i++) |
| 145 |
System.arraycopy(z[i],0,sz,i*n,n); |
| 146 |
Arrays.sort(sz); |
| 147 |
final double dx = gx[1] - gx[0]; |
| 148 |
final double dy = gy[1] - gy[0]; |
| 149 |
final double dxdy = dx * dy; |
| 150 |
c1[0] = sz[0] * dxdy; |
| 151 |
final double criticalValue = 1.0 - probabilityMass; |
| 152 |
if (criticalValue < c1[0] || criticalValue >= 1.0) |
| 153 |
throw new RuntimeException(); |
| 154 |
|
| 155 |
for(int i=1; i<n*n; i++) { |
| 156 |
c1[i] = sz[i] * dxdy + c1[i-1]; |
| 157 |
if (c1[i] > criticalValue) { |
| 158 |
final double diffC1 = c1[i] - c1[i-1]; |
| 159 |
final double diffSz = sz[i] - sz[i-1]; |
| 160 |
level = sz[i] - (c1[i]-criticalValue) / diffC1 * diffSz; |
| 161 |
break; |
| 162 |
} |
| 163 |
} |
| 164 |
return level; |
| 165 |
} |
| 166 |
|
| 167 |
public ContourPath[] getContourPaths(double hpdValue) { |
| 168 |
|
| 169 |
double thresholdDensity = findLevelCorrespondingToMass(hpdValue); |
| 170 |
ContourGenerator contour = new ContourGenerator(getXGrid(), getYGrid(), getKDE(), |
| 171 |
new ContourAttrib[]{new ContourAttrib(thresholdDensity)}); |
| 172 |
|
| 173 |
ContourPath[] paths = null; |
| 174 |
try { |
| 175 |
paths = contour.getContours(); |
| 176 |
} catch (InterruptedException e) { |
| 177 |
e.printStackTrace(); |
| 178 |
} |
| 179 |
return paths; |
| 180 |
} |
| 181 |
|
| 182 |
public double[][] getKDE() { |
| 183 |
return z; |
| 184 |
} |
| 185 |
|
| 186 |
public double[] getXGrid() { |
| 187 |
return gx; |
| 188 |
} |
| 189 |
|
| 190 |
public double[] getYGrid() { |
| 191 |
return gy; |
| 192 |
} |
| 193 |
|
| 194 |
public void normalize(double[][] X) { |
| 195 |
for (int i = 0; i < X.length; i++) { |
| 196 |
for (int j = 0; j < X[0].length; j++) |
| 197 |
X[i][j] = NormalDistribution.pdf(X[i][j], 0, 1); |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
public double[][] outerMinusScaled(double[] X, double[] Y, double scale) { |
| 202 |
double[][] A = new double[X.length][Y.length]; |
| 203 |
for (int indexX = 0; indexX < X.length; indexX++) { |
| 204 |
for (int indexY = 0; indexY < Y.length; indexY++) |
| 205 |
A[indexX][indexY] = (X[indexX] - Y[indexY]) / scale; |
| 206 |
} |
| 207 |
return A; |
| 208 |
} |
| 209 |
|
| 210 |
public double[] makeSequence(double start, double end, int length) { |
| 211 |
double[] seq = new double[length]; |
| 212 |
double by = (end - start) / (length - 1); |
| 213 |
double value = start; |
| 214 |
for (int i = 0; i < length; i++, value += by) { |
| 215 |
seq[i] = value; |
| 216 |
} |
| 217 |
return seq; |
| 218 |
} |
| 219 |
|
| 220 |
private double margin = 0.1; |
| 221 |
|
| 222 |
private void setupLims() { |
| 223 |
lims = new double[4]; |
| 224 |
lims[0] = DiscreteStatistics.min(x); |
| 225 |
lims[1] = DiscreteStatistics.max(x); |
| 226 |
lims[2] = DiscreteStatistics.min(y); |
| 227 |
lims[3] = DiscreteStatistics.max(y); |
| 228 |
|
| 229 |
double xDelta = (lims[1] - lims[0]) * margin; |
| 230 |
double yDelta = (lims[3] - lims[2]) * margin; |
| 231 |
lims[0] -= xDelta; |
| 232 |
lims[1] += xDelta; |
| 233 |
lims[2] -= yDelta; |
| 234 |
lims[3] += yDelta; |
| 235 |
} |
| 236 |
|
| 237 |
private void setupH() { |
| 238 |
h = new double[2]; |
| 239 |
h[0] = bandwidthNRD(x) / 4; |
| 240 |
h[1] = bandwidthNRD(y) / 4; |
| 241 |
|
| 242 |
if (limitBandwidth) { |
| 243 |
if (h[0] > 0.5) { |
| 244 |
h[0] = 0.5; |
| 245 |
} |
| 246 |
if (h[1] > 0.5) { |
| 247 |
h[1] = 0.5; |
| 248 |
} |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
public double bandwidthNRD(double[] in) { |
| 262 |
|
| 263 |
DoubleArrayList inList = new DoubleArrayList(in.length); |
| 264 |
for (double d : in) |
| 265 |
inList.add(d); |
| 266 |
inList.sort(); |
| 267 |
|
| 268 |
final double h = (Descriptive.quantile(inList, 0.75) - Descriptive.quantile(inList, 0.25)) / 1.34; |
| 269 |
|
| 270 |
return 4 * 1.06 * |
| 271 |
Math.min(Math.sqrt(DiscreteStatistics.variance(in)), h) * |
| 272 |
Math.pow(in.length, -0.2); |
| 273 |
} |
| 274 |
|
| 275 |
public static void main(String[] arg) { |
| 276 |
|
| 277 |
double[] x = {3.4, 1.2, 5.6, 2.2, 3.1}; |
| 278 |
double[] y = {1.0, 2.0, 1.0, 2.0, 1.0}; |
| 279 |
|
| 280 |
KernelDensityEstimator2D kde = new KernelDensityEstimator2D(x, y, 4); |
| 281 |
|
| 282 |
System.out.println(new Vector(kde.getXGrid())); |
| 283 |
System.out.println(new Vector(kde.getYGrid())); |
| 284 |
System.out.println(new Matrix(kde.getKDE())); |
| 285 |
System.exit(-1); |
| 286 |
|
| 287 |
} |
| 288 |
|
| 289 |
public double[] getLims() { return lims; } |
| 290 |
|
| 291 |
private final double[] x; |
| 292 |
private final double[] y; |
| 293 |
private double[] h; |
| 294 |
private final int n; |
| 295 |
private double[] lims; |
| 296 |
private int nx; |
| 297 |
private double[] gx; |
| 298 |
private double[] gy; |
| 299 |
private double[][] z; |
| 300 |
|
| 301 |
private final boolean limitBandwidth; |
| 302 |
|
| 303 |
} |