List of usage examples for org.apache.commons.math.stat.descriptive.moment Variance increment
@Override public void increment(final double d)
If all values are available, it is more accurate to use #evaluate(double[]) rather than adding values one at a time using this method and then executing #getResult , since evaluate
leverages the fact that is has the full list of values together to execute a two-pass algorithm.
From source file:org.basketball.MyKMeansPlusPlusClusterer.java
/** * Get a random point from the {@link Cluster} with the largest distance variance. * * @param clusters the {@link Cluster}s to search * @return a random point from the selected cluster *///from w w w . ja v a 2s.c o m private T getPointFromLargestVarianceCluster(final Collection<Cluster<T>> clusters) { double maxVariance = Double.NEGATIVE_INFINITY; Cluster<T> selected = null; for (final Cluster<T> cluster : clusters) { if (!cluster.getPoints().isEmpty()) { // compute the distance variance of the current cluster final T center = cluster.getCenter(); final Variance stat = new Variance(); for (final T point : cluster.getPoints()) { stat.increment(point.distanceFrom(center)); } final double variance = stat.getResult(); // select the cluster with the largest variance if (variance > maxVariance) { maxVariance = variance; selected = cluster; } } } // did we find at least one non-empty cluster ? if (selected == null) { throw new ConvergenceException(LocalizedFormats.EMPTY_CLUSTER_IN_K_MEANS); } // extract a random point from the cluster final List<T> selectedPoints = selected.getPoints(); return selectedPoints.remove(random.nextInt(selectedPoints.size())); }