Java Code Examples for org.apache.commons.math3.stat.descriptive.moment.Variance#getResult()

The following examples show how to use org.apache.commons.math3.stat.descriptive.moment.Variance#getResult() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SumOfClusterVariances.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
public double score(final List<? extends Cluster<T>> clusters) {
    double varianceSum = 0.0;
    for (final Cluster<T> cluster : clusters) {
        if (!cluster.getPoints().isEmpty()) {

            final Clusterable center = centroidOf(cluster);

            // compute the distance variance of the current cluster
            final Variance stat = new Variance();
            for (final T point : cluster.getPoints()) {
                stat.increment(distance(point, center));
            }
            varianceSum += stat.getResult();

        }
    }
    return varianceSum;
}
 
Example 2
Source File: SumOfClusterVariances.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
public double score(final List<? extends Cluster<T>> clusters) {
    double varianceSum = 0.0;
    for (final Cluster<T> cluster : clusters) {
        if (!cluster.getPoints().isEmpty()) {

            final Clusterable center = centroidOf(cluster);

            // compute the distance variance of the current cluster
            final Variance stat = new Variance();
            for (final T point : cluster.getPoints()) {
                stat.increment(distance(point, center));
            }
            varianceSum += stat.getResult();

        }
    }
    return varianceSum;
}
 
Example 3
Source File: KMeansPlusPlusClusterer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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
 * @throws ConvergenceException if clusters are all empty
 */
private T getPointFromLargestVarianceCluster(final Collection<Cluster<T>> clusters)
throws ConvergenceException {

    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()));

}
 
Example 4
Source File: KMeansPlusPlusClusterer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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
 * @throws ConvergenceException if clusters are all empty
 */
private T getPointFromLargestVarianceCluster(final Collection<CentroidCluster<T>> clusters)
        throws ConvergenceException {

    double maxVariance = Double.NEGATIVE_INFINITY;
    Cluster<T> selected = null;
    for (final CentroidCluster<T> cluster : clusters) {
        if (!cluster.getPoints().isEmpty()) {

            // compute the distance variance of the current cluster
            final Clusterable center = cluster.getCenter();
            final Variance stat = new Variance();
            for (final T point : cluster.getPoints()) {
                stat.increment(distance(point, 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()));

}
 
Example 5
Source File: KMeansPlusPlusClusterer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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
 * @throws ConvergenceException if clusters are all empty
 */
private T getPointFromLargestVarianceCluster(final Collection<Cluster<T>> clusters)
throws ConvergenceException {

    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()));

}
 
Example 6
Source File: KMeansPlusPlusClusterer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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
 * @throws ConvergenceException if clusters are all empty
 */
private T getPointFromLargestVarianceCluster(final Collection<Cluster<T>> clusters)
throws ConvergenceException {

    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()));

}
 
Example 7
Source File: KMeansPlusPlusClusterer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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
 * @throws ConvergenceException if clusters are all empty
 */
private T getPointFromLargestVarianceCluster(final Collection<Cluster<T>> clusters)
throws ConvergenceException {

    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()));

}
 
Example 8
Source File: KMeansPlusPlusClusterer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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
 */
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()));

}
 
Example 9
Source File: KMeansPlusPlusClusterer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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
 * @throws ConvergenceException if clusters are all empty
 */
private T getPointFromLargestVarianceCluster(final Collection<CentroidCluster<T>> clusters)
        throws ConvergenceException {

    double maxVariance = Double.NEGATIVE_INFINITY;
    Cluster<T> selected = null;
    for (final CentroidCluster<T> cluster : clusters) {
        if (!cluster.getPoints().isEmpty()) {

            // compute the distance variance of the current cluster
            final Clusterable center = cluster.getCenter();
            final Variance stat = new Variance();
            for (final T point : cluster.getPoints()) {
                stat.increment(distance(point, 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()));

}
 
Example 10
Source File: KMeansPlusPlusClusterer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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
 */
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()));

}
 
Example 11
Source File: KMeansPlusPlusClusterer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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
 * @throws ConvergenceException if clusters are all empty
 */
private T getPointFromLargestVarianceCluster(final Collection<Cluster<T>> clusters)
throws ConvergenceException {

    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()));

}
 
Example 12
Source File: KMeansPlusPlusClusterer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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
 * @throws ConvergenceException if clusters are all empty
 */
private T getPointFromLargestVarianceCluster(final Collection<CentroidCluster<T>> clusters)
        throws ConvergenceException {

    double maxVariance = Double.NEGATIVE_INFINITY;
    Cluster<T> selected = null;
    for (final CentroidCluster<T> cluster : clusters) {
        if (!cluster.getPoints().isEmpty()) {

            // compute the distance variance of the current cluster
            final Clusterable center = cluster.getCenter();
            final Variance stat = new Variance();
            for (final T point : cluster.getPoints()) {
                stat.increment(distance(point, 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()));

}
 
Example 13
Source File: SummaryStatistics.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Returns the <a href="http://en.wikibooks.org/wiki/Statistics/Summary/Variance">
 * population variance</a> of the values that have been added.
 *
 * <p>Double.NaN is returned if no values have been added.</p>
 *
 * @return the population variance
 */
public double getPopulationVariance() {
    Variance populationVariance = new Variance(secondMoment);
    populationVariance.setBiasCorrected(false);
    return populationVariance.getResult();
}
 
Example 14
Source File: SummaryStatistics.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Returns the <a href="http://en.wikibooks.org/wiki/Statistics/Summary/Variance">
 * population variance</a> of the values that have been added.
 *
 * <p>Double.NaN is returned if no values have been added.</p>
 *
 * @return the population variance
 */
public double getPopulationVariance() {
    Variance populationVariance = new Variance(secondMoment);
    populationVariance.setBiasCorrected(false);
    return populationVariance.getResult();
}
 
Example 15
Source File: SummaryStatistics.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Returns the <a href="http://en.wikibooks.org/wiki/Statistics/Summary/Variance">
 * population variance</a> of the values that have been added.
 *
 * <p>Double.NaN is returned if no values have been added.</p>
 *
 * @return the population variance
 */
public double getPopulationVariance() {
    Variance populationVariance = new Variance(secondMoment);
    populationVariance.setBiasCorrected(false);
    return populationVariance.getResult();
}
 
Example 16
Source File: SummaryStatistics.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Returns the <a href="http://en.wikibooks.org/wiki/Statistics/Summary/Variance">
 * population variance</a> of the values that have been added.
 *
 * <p>Double.NaN is returned if no values have been added.</p>
 *
 * @return the population variance
 */
public double getPopulationVariance() {
    Variance populationVariance = new Variance(secondMoment);
    populationVariance.setBiasCorrected(false);
    return populationVariance.getResult();
}
 
Example 17
Source File: SummaryStatistics.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Returns the <a href="http://en.wikibooks.org/wiki/Statistics/Summary/Variance">
 * population variance</a> of the values that have been added.
 *
 * <p>Double.NaN is returned if no values have been added.</p>
 *
 * @return the population variance
 */
public double getPopulationVariance() {
    Variance populationVariance = new Variance(secondMoment);
    populationVariance.setBiasCorrected(false);
    return populationVariance.getResult();
}
 
Example 18
Source File: SummaryStatistics.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Returns the <a href="http://en.wikibooks.org/wiki/Statistics/Summary/Variance">
 * population variance</a> of the values that have been added.
 *
 * <p>Double.NaN is returned if no values have been added.</p>
 *
 * @return the population variance
 */
public double getPopulationVariance() {
    Variance populationVariance = new Variance(secondMoment);
    populationVariance.setBiasCorrected(false);
    return populationVariance.getResult();
}
 
Example 19
Source File: SummaryStatistics.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Returns the <a href="http://en.wikibooks.org/wiki/Statistics/Summary/Variance">
 * population variance</a> of the values that have been added.
 *
 * <p>Double.NaN is returned if no values have been added.</p>
 *
 * @return the population variance
 */
public double getPopulationVariance() {
    Variance populationVariance = new Variance(secondMoment);
    populationVariance.setBiasCorrected(false);
    return populationVariance.getResult();
}
 
Example 20
Source File: SummaryStatistics.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Returns the <a href="http://en.wikibooks.org/wiki/Statistics/Summary/Variance">
 * population variance</a> of the values that have been added.
 *
 * <p>Double.NaN is returned if no values have been added.</p>
 *
 * @return the population variance
 */
public double getPopulationVariance() {
    Variance populationVariance = new Variance(secondMoment);
    populationVariance.setBiasCorrected(false);
    return populationVariance.getResult();
}