@Override public boolean execute(double[] point, double distance) { prob[0] += kernel.evaluate(Math.sqrt(distance) / getBandwidth()); count[0]++; return true; } });
@Override public double estimateProbability(double[] sample) { final double[] prob = new double[1]; final int[] count = new int[1]; tree.coordinateRadiusSearch(sample, kernel.getCutOff() * getBandwidth(), new TObjectDoubleProcedure<double[]>() { @Override public boolean execute(double[] point, double distance) { prob[0] += kernel.evaluate(Math.sqrt(distance) / getBandwidth()); count[0]++; return true; } }); return prob[0] / (getBandwidth() * count[0]); }
@Override public boolean execute(double[] a, double b) { support.add(ObjectDoublePair.pair(a, kernel.evaluate(Math.sqrt(b) / getBandwidth()))); return true; } });
@Override public double[] sample(Random rng) { final double[] pt = data[rng.nextInt(data.length)].clone(); for (int i = 0; i < pt.length; i++) { pt[i] = pt[i] + kernel.sample(rng) * this.getBandwidth(); } return pt; }
protected boolean computeMeanShift(double[] pt) { final List<ObjectDoublePair<double[]>> support = kde.getSupport(pt); if (support.size() == 1) { return true; } double sum = 0; final double[] out = new double[pt.length]; for (final ObjectDoublePair<double[]> p : support) { sum += p.second; for (int j = 0; j < out.length; j++) { out[j] += p.second * p.first[j]; } } double dist = 0; for (int j = 0; j < out.length; j++) { out[j] /= sum; dist += (pt[j] - out[j]) * (pt[j] - out[j]); } System.arraycopy(out, 0, pt, 0, out.length); return dist < 1e-3 * kde.getBandwidth(); } }
/** * Get the underlying points that support the KDE within the window around * the given point. Each point is returned together with its own density * estimate. * * @param sample * the point in the centre of the window * @return the points in the window */ public List<ObjectDoublePair<double[]>> getSupport(double[] sample) { final List<ObjectDoublePair<double[]>> support = new ArrayList<ObjectDoublePair<double[]>>(); tree.coordinateRadiusSearch(sample, kernel.getCutOff() * getBandwidth(), new TObjectDoubleProcedure<double[]>() { @Override public boolean execute(double[] a, double b) { support.add(ObjectDoublePair.pair(a, kernel.evaluate(Math.sqrt(b) / getBandwidth()))); return true; } }); return support; }