@Override public boolean execute(double[] point, double distance) { prob[0] += kernel.evaluate(Math.sqrt(distance) / getBandwidth()); count[0]++; return true; } });
@Override public double[] estimateLogProbability(double[][] x) { final double[] lps = new double[x.length]; for (int i = 0; i < x.length; i++) lps[i] = estimateLogProbability(x[i]); return lps; } }
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(); } }
protected void performMeanShift() { final double[][] data = kde.getData(); final double[][] modePerPoint = new double[data.length][]; // perform the MS procedure on each point for (int i = 0; i < data.length; i++) { final double[] point = data[i].clone(); for (int iter = 0; iter < maxIter; iter++) { if (computeMeanShift(point)) break; } modePerPoint[i] = point; } // now need to merge modes that are <bandwidth away mergeModes(modePerPoint); }
final double[] point = modePerPoint[i]; tree.radiusSearch(modePerPoint[i], kde.getScaledBandwidth(), new TIntObjectProcedure<double[]>() { @Override public boolean execute(int a, double[] b) {
@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; }
/** * 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; }