Java Code Examples for org.apache.commons.math3.stat.descriptive.moment.Variance

The following examples show how to use org.apache.commons.math3.stat.descriptive.moment.Variance. These examples are extracted from open source projects. 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
/**
 * Test slice sampling of a monotonic beta distribution as an example of sampling of a bounded random variable.
 * Checks that input mean and variance are recovered by 10000 samples to a relative error of 0.5% and 2%,
 * respectively.
 */
@Test
public void testSliceSamplingOfMonotonicBetaDistribution() {
    rng.setSeed(RANDOM_SEED);

    final double alpha = 10.;
    final double beta = 1.;
    final BetaDistribution betaDistribution = new BetaDistribution(alpha, beta);
    final Function<Double, Double> betaLogPDF = betaDistribution::logDensity;

    final double xInitial = 0.5;
    final double xMin = 0.;
    final double xMax = 1.;
    final double width = 0.1;
    final int numSamples = 10000;
    final SliceSampler betaSampler = new SliceSampler(rng, betaLogPDF, xMin, xMax, width);
    final double[] samples = Doubles.toArray(betaSampler.sample(xInitial, numSamples));

    final double mean = betaDistribution.getNumericalMean();
    final double variance = betaDistribution.getNumericalVariance();
    final double sampleMean = new Mean().evaluate(samples);
    final double sampleVariance = new Variance().evaluate(samples);
    Assert.assertEquals(relativeError(sampleMean, mean), 0., 0.005);
    Assert.assertEquals(relativeError(sampleVariance, variance), 0., 0.02);
}
 
Example 2
/**
 * Test slice sampling of a peaked beta distribution as an example of sampling of a bounded random variable.
 * Checks that input mean and variance are recovered by 10000 samples to a relative error of 0.5% and 2%,
 * respectively.
 */
@Test
public void testSliceSamplingOfPeakedBetaDistribution() {
    rng.setSeed(RANDOM_SEED);

    final double alpha = 10.;
    final double beta = 4.;
    final BetaDistribution betaDistribution = new BetaDistribution(alpha, beta);
    final Function<Double, Double> betaLogPDF = betaDistribution::logDensity;

    final double xInitial = 0.5;
    final double xMin = 0.;
    final double xMax = 1.;
    final double width = 0.1;
    final int numSamples = 10000;
    final SliceSampler betaSampler = new SliceSampler(rng, betaLogPDF, xMin, xMax, width);
    final double[] samples = Doubles.toArray(betaSampler.sample(xInitial, numSamples));

    final double mean = betaDistribution.getNumericalMean();
    final double variance = betaDistribution.getNumericalVariance();
    final double sampleMean = new Mean().evaluate(samples);
    final double sampleVariance = new Variance().evaluate(samples);
    Assert.assertEquals(relativeError(sampleMean, mean), 0., 0.005);
    Assert.assertEquals(relativeError(sampleVariance, variance), 0., 0.02);
}
 
Example 3
Source Project: astor   Source File: SumOfClusterVariances.java    License: 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 4
Source Project: astor   Source File: Covariance.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Compute a covariance matrix from a matrix whose columns represent
 * covariates.
 * @param matrix input matrix (must have at least one column and two rows)
 * @param biasCorrected determines whether or not covariance estimates are bias-corrected
 * @return covariance matrix
 * @throws MathIllegalArgumentException if the matrix does not contain sufficient data
 */
protected RealMatrix computeCovarianceMatrix(RealMatrix matrix, boolean biasCorrected)
throws MathIllegalArgumentException {
    int dimension = matrix.getColumnDimension();
    Variance variance = new Variance(biasCorrected);
    RealMatrix outMatrix = new BlockRealMatrix(dimension, dimension);
    for (int i = 0; i < dimension; i++) {
        for (int j = 0; j < i; j++) {
          double cov = covariance(matrix.getColumn(i), matrix.getColumn(j), biasCorrected);
          outMatrix.setEntry(i, j, cov);
          outMatrix.setEntry(j, i, cov);
        }
        outMatrix.setEntry(i, i, variance.evaluate(matrix.getColumn(i)));
    }
    return outMatrix;
}
 
Example 5
Source Project: astor   Source File: Covariance.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Compute a covariance matrix from a matrix whose columns represent
 * covariates.
 * @param matrix input matrix (must have at least one column and two rows)
 * @param biasCorrected determines whether or not covariance estimates are bias-corrected
 * @return covariance matrix
 * @throws MathIllegalArgumentException if the matrix does not contain sufficient data
 */
protected RealMatrix computeCovarianceMatrix(RealMatrix matrix, boolean biasCorrected)
throws MathIllegalArgumentException {
    int dimension = matrix.getColumnDimension();
    Variance variance = new Variance(biasCorrected);
    RealMatrix outMatrix = new BlockRealMatrix(dimension, dimension);
    for (int i = 0; i < dimension; i++) {
        for (int j = 0; j < i; j++) {
          double cov = covariance(matrix.getColumn(i), matrix.getColumn(j), biasCorrected);
          outMatrix.setEntry(i, j, cov);
          outMatrix.setEntry(j, i, cov);
        }
        outMatrix.setEntry(i, i, variance.evaluate(matrix.getColumn(i)));
    }
    return outMatrix;
}
 
Example 6
/**
 * Tests slice sampling of a monotonic beta distribution as an example of sampling of a bounded random variable.
 * Checks that input mean and variance are recovered by 10000 samples to a relative error of 0.5% and 2%,
 * respectively.
 */
@Test
public void testSliceSamplingOfMonotonicBetaDistribution() {
    rng.setSeed(RANDOM_SEED);

    final double alpha = 10.;
    final double beta = 1.;
    final BetaDistribution betaDistribution = new BetaDistribution(alpha, beta);
    final Function<Double, Double> betaLogPDF = betaDistribution::logDensity;
    final double mean = betaDistribution.getNumericalMean();
    final double variance = betaDistribution.getNumericalVariance();

    final double xInitial = 0.5;
    final double xMin = 0.;
    final double xMax = 1.;
    final double width = 0.1;
    final int numSamples = 10000;
    final SliceSampler betaSampler = new SliceSampler(rng, betaLogPDF, xMin, xMax, width);
    final double[] samples = Doubles.toArray(betaSampler.sample(xInitial, numSamples));

    final double sampleMean = new Mean().evaluate(samples);
    final double sampleVariance = new Variance().evaluate(samples);
    Assert.assertEquals(relativeError(sampleMean, mean), 0., 0.005);
    Assert.assertEquals(relativeError(sampleVariance, variance), 0., 0.02);
}
 
Example 7
Source Project: astor   Source File: Covariance.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Compute a covariance matrix from a matrix whose columns represent
 * covariates.
 * @param matrix input matrix (must have at least one column and two rows)
 * @param biasCorrected determines whether or not covariance estimates are bias-corrected
 * @return covariance matrix
 * @throws MathIllegalArgumentException if the matrix does not contain sufficient data
 */
protected RealMatrix computeCovarianceMatrix(RealMatrix matrix, boolean biasCorrected)
throws MathIllegalArgumentException {
    int dimension = matrix.getColumnDimension();
    Variance variance = new Variance(biasCorrected);
    RealMatrix outMatrix = new BlockRealMatrix(dimension, dimension);
    for (int i = 0; i < dimension; i++) {
        for (int j = 0; j < i; j++) {
          double cov = covariance(matrix.getColumn(i), matrix.getColumn(j), biasCorrected);
          outMatrix.setEntry(i, j, cov);
          outMatrix.setEntry(j, i, cov);
        }
        outMatrix.setEntry(i, i, variance.evaluate(matrix.getColumn(i)));
    }
    return outMatrix;
}
 
Example 8
/**
 * Tests slice sampling of a peaked beta distribution as an example of sampling of a bounded random variable.
 * Checks that input mean and variance are recovered by 10000 samples to a relative error of 0.5% and 2%,
 * respectively.
 */
@Test
public void testSliceSamplingOfPeakedBetaDistribution() {
    rng.setSeed(RANDOM_SEED);

    final double alpha = 10.;
    final double beta = 4.;
    final BetaDistribution betaDistribution = new BetaDistribution(alpha, beta);
    final Function<Double, Double> betaLogPDF = betaDistribution::logDensity;
    final double mean = betaDistribution.getNumericalMean();
    final double variance = betaDistribution.getNumericalVariance();

    final double xInitial = 0.5;
    final double xMin = 0.;
    final double xMax = 1.;
    final double width = 0.1;
    final int numSamples = 10000;
    final SliceSampler betaSampler = new SliceSampler(rng, betaLogPDF, xMin, xMax, width);
    final double[] samples = Doubles.toArray(betaSampler.sample(xInitial, numSamples));

    final double sampleMean = new Mean().evaluate(samples);
    final double sampleVariance = new Variance().evaluate(samples);
    Assert.assertEquals(relativeError(sampleMean, mean), 0., 0.005);
    Assert.assertEquals(relativeError(sampleVariance, variance), 0., 0.02);
}
 
Example 9
Source Project: astor   Source File: Covariance.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Compute a covariance matrix from a matrix whose columns represent
 * covariates.
 * @param matrix input matrix (must have at least one column and two rows)
 * @param biasCorrected determines whether or not covariance estimates are bias-corrected
 * @return covariance matrix
 * @throws MathIllegalArgumentException if the matrix does not contain sufficient data
 */
protected RealMatrix computeCovarianceMatrix(RealMatrix matrix, boolean biasCorrected)
throws MathIllegalArgumentException {
    int dimension = matrix.getColumnDimension();
    Variance variance = new Variance(biasCorrected);
    RealMatrix outMatrix = new BlockRealMatrix(dimension, dimension);
    for (int i = 0; i < dimension; i++) {
        for (int j = 0; j < i; j++) {
          double cov = covariance(matrix.getColumn(i), matrix.getColumn(j), biasCorrected);
          outMatrix.setEntry(i, j, cov);
          outMatrix.setEntry(j, i, cov);
        }
        outMatrix.setEntry(i, i, variance.evaluate(matrix.getColumn(i)));
    }
    return outMatrix;
}
 
Example 10
Source Project: presto   Source File: TestDoubleVariancePopAggregation.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected Number getExpectedValue(int start, int length)
{
    if (length == 0) {
        return null;
    }

    double[] values = new double[length];
    for (int i = 0; i < length; i++) {
        values[i] = start + i;
    }

    Variance variance = new Variance(false);
    return variance.evaluate(values);
}
 
Example 11
Source Project: presto   Source File: TestLongVariancePopAggregation.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected Number getExpectedValue(int start, int length)
{
    if (length == 0) {
        return null;
    }

    double[] values = new double[length];
    for (int i = 0; i < length; i++) {
        values[i] = start + i;
    }

    Variance variance = new Variance(false);
    return variance.evaluate(values);
}
 
Example 12
Source Project: presto   Source File: TestDoubleVarianceAggregation.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected Number getExpectedValue(int start, int length)
{
    if (length < 2) {
        return null;
    }

    double[] values = new double[length];
    for (int i = 0; i < length; i++) {
        values[i] = start + i;
    }

    Variance variance = new Variance();
    return variance.evaluate(values);
}
 
Example 13
Source Project: astor   Source File: SummaryStatisticsTest.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * JIRA: MATH-691
 */
@Test
public void testOverrideVarianceWithMathClass() throws Exception {
    double[] scores = {1, 2, 3, 4};
    SummaryStatistics stats = new SummaryStatistics();
    stats.setVarianceImpl(new Variance(false)); //use "population variance"
    for(double i : scores) {
      stats.addValue(i);
    }
    Assert.assertEquals((new Variance(false)).evaluate(scores),stats.getVariance(), 0); 
}
 
Example 14
Source Project: Java-Data-Analysis   Source File: CorrelationExample.java    License: MIT License 5 votes vote down vote up
static double rho(double[][] data) {
    Variance v = new Variance();
    double varX = v.evaluate(data[0]);
    double sigX = Math.sqrt(varX);
    double varY = v.evaluate(data[1]);
    double sigY = Math.sqrt(varY);
    Covariance c = new Covariance(data);
    double sigXY = c.covariance(data[0], data[1]);
    return sigXY/(sigX*sigY);
}
 
Example 15
/**
 * Calculates the standard deviation per row from a matrix.
 * @param matrix the input matrix.
 * @return never {@code null}, an array with as many positions as rows in {@code matrix}.
 * @throws IllegalArgumentException if {@code matrix} is {@code null}.
 */
public static double[] rowStdDevs(final RealMatrix matrix) {
    Utils.nonNull(matrix);
    final Variance varianceEvaluator = new Variance();
    return IntStream.range(0, matrix.getRowDimension())
            .mapToDouble(r -> Math.sqrt(varianceEvaluator.evaluate(matrix.getRow(r)))).toArray();
}
 
Example 16
/**
 * Calculates the standard deviation per column from a matrix.
 * @param matrix the input matrix.
 * @return never {@code null}, an array with as many positions as columns in {@code matrix}.
 * @throws IllegalArgumentException if {@code matrix} is {@code null}.
 */
public static double[] columnStdDevs(final RealMatrix matrix) {
    Utils.nonNull(matrix);
    final Variance varianceEvaluator = new Variance();
    return IntStream.range(0, matrix.getColumnDimension())
            .mapToDouble(c -> Math.sqrt(varianceEvaluator.evaluate(matrix.getColumn(c)))).toArray();
}
 
Example 17
/**
 * Attempt to get an idea of segment mean variance near copy neutral.
 *
 * @param segments Never {@code null}
 * @return variance of segment mean (in CR space) of segments that are "close enough" to copy neutral.
 *   Zero if no segments are "close enough"
 */
private double calculateVarianceOfCopyNeutralSegmentMeans(final List<ACNVModeledSegment> segments, final double meanBiasInCR) {
    Utils.nonNull(segments);

    // Only consider values "close enough" to copy neutral (CR == 1).
    final double neutralCR = 1 + meanBiasInCR;
    final double[] neutralSegmentMeans = segments.stream()
            .mapToDouble(ACNVModeledSegment::getSegmentMeanInCRSpace)
            .filter(m -> Math.abs(m - neutralCR) < CLOSE_ENOUGH_TO_COPY_NEUTRAL_IN_CR).toArray();
    return new Variance().evaluate(neutralSegmentMeans);
}
 
Example 18
public double getVarianceValue(Set<OWLClass> candidates, IRI referenceEntity) throws OwlSimVarianceEntityReferenceNotFoundException {
	// Reference entity does not exist - this should probably not happen.
	if (!refBasedStats.getReferenceStats().containsKey(referenceEntity)) {
		throw new OwlSimVarianceEntityReferenceNotFoundException(referenceEntity);
	}
	
	// Create IC list for candidates provided
	double[] icData = refBasedStats.retrieveCandidatesIC(candidates);
	// Return variance against the given reference concept
	return new Variance().evaluate(icData, refBasedStats.getReferenceStats().get(referenceEntity).getMean());
}
 
Example 19
/**
 * This implementation can be heavily optimized
 */
public Map<String, Double> getTopNVarianceValues(Set<OWLClass> candidates, int n) {
	double[] icData = refBasedStats.retrieveCandidatesIC(candidates);

	// Compute variance against all cached reference entities
	Map<IRI, Double> iResult = new LinkedHashMap<IRI, Double>();
	List<Double> list = new ArrayList<Double>();
	for (IRI reference : refBasedStats.getReferenceStats().keySet()) {
		double variance = new Variance().evaluate(icData, refBasedStats.getReferenceStats().get(reference).getMean());
		list.add(variance);
		iResult.put(reference, variance);
	}
	// Sort variances ascendantly
	Collections.sort(list);
	
	// Return top-N variances
	
	int iterSize = n < list.size() ? n : list.size();
	Map<String, Double> result = new LinkedHashMap<String, Double>();
	for (int i = 0 ;i < iterSize ; i++) {
		double varValue = list.get(i);
		List<String> refURIs = findRefURIs(varValue, iResult);
		for (String uri : refURIs) {
			result.put(uri, varValue);
		}
	}
	
	return result;
}
 
Example 20
Source Project: astor   Source File: SummaryStatisticsTest.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * JIRA: MATH-691
 */
@Test
public void testOverrideVarianceWithMathClass() {
    double[] scores = {1, 2, 3, 4};
    SummaryStatistics stats = new SummaryStatistics();
    stats.setVarianceImpl(new Variance(false)); //use "population variance"
    for(double i : scores) {
      stats.addValue(i);
    }
    Assert.assertEquals((new Variance(false)).evaluate(scores),stats.getVariance(), 0); 
}
 
Example 21
@Test
public void testSummaryConsistency() {
    final DescriptiveStatistics dstats = new DescriptiveStatistics();
    final SummaryStatistics sstats = new SummaryStatistics();
    final int windowSize = 5;
    dstats.setWindowSize(windowSize);
    final double tol = 1E-12;
    for (int i = 0; i < 20; i++) {
        dstats.addValue(i);
        sstats.clear();
        double[] values = dstats.getValues();
        for (int j = 0; j < values.length; j++) {
            sstats.addValue(values[j]);
        }
        TestUtils.assertEquals(dstats.getMean(), sstats.getMean(), tol);
        TestUtils.assertEquals(new Mean().evaluate(values), dstats.getMean(), tol);
        TestUtils.assertEquals(dstats.getMax(), sstats.getMax(), tol);
        TestUtils.assertEquals(new Max().evaluate(values), dstats.getMax(), tol);
        TestUtils.assertEquals(dstats.getGeometricMean(), sstats.getGeometricMean(), tol);
        TestUtils.assertEquals(new GeometricMean().evaluate(values), dstats.getGeometricMean(), tol);
        TestUtils.assertEquals(dstats.getMin(), sstats.getMin(), tol);
        TestUtils.assertEquals(new Min().evaluate(values), dstats.getMin(), tol);
        TestUtils.assertEquals(dstats.getStandardDeviation(), sstats.getStandardDeviation(), tol);
        TestUtils.assertEquals(dstats.getVariance(), sstats.getVariance(), tol);
        TestUtils.assertEquals(new Variance().evaluate(values), dstats.getVariance(), tol);
        TestUtils.assertEquals(dstats.getSum(), sstats.getSum(), tol);
        TestUtils.assertEquals(new Sum().evaluate(values), dstats.getSum(), tol);
        TestUtils.assertEquals(dstats.getSumsq(), sstats.getSumsq(), tol);
        TestUtils.assertEquals(new SumOfSquares().evaluate(values), dstats.getSumsq(), tol);
        TestUtils.assertEquals(dstats.getPopulationVariance(), sstats.getPopulationVariance(), tol);
        TestUtils.assertEquals(new Variance(false).evaluate(values), dstats.getPopulationVariance(), tol);
    }
}
 
Example 22
/**
 * 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 23
@Test
public void testSummaryConsistency() {
    final DescriptiveStatistics dstats = new DescriptiveStatistics();
    final SummaryStatistics sstats = new SummaryStatistics();
    final int windowSize = 5;
    dstats.setWindowSize(windowSize);
    final double tol = 1E-12;
    for (int i = 0; i < 20; i++) {
        dstats.addValue(i);
        sstats.clear();
        double[] values = dstats.getValues();
        for (int j = 0; j < values.length; j++) {
            sstats.addValue(values[j]);
        }
        TestUtils.assertEquals(dstats.getMean(), sstats.getMean(), tol);
        TestUtils.assertEquals(new Mean().evaluate(values), dstats.getMean(), tol);
        TestUtils.assertEquals(dstats.getMax(), sstats.getMax(), tol);
        TestUtils.assertEquals(new Max().evaluate(values), dstats.getMax(), tol);
        TestUtils.assertEquals(dstats.getGeometricMean(), sstats.getGeometricMean(), tol);
        TestUtils.assertEquals(new GeometricMean().evaluate(values), dstats.getGeometricMean(), tol);
        TestUtils.assertEquals(dstats.getMin(), sstats.getMin(), tol);
        TestUtils.assertEquals(new Min().evaluate(values), dstats.getMin(), tol);
        TestUtils.assertEquals(dstats.getStandardDeviation(), sstats.getStandardDeviation(), tol);
        TestUtils.assertEquals(dstats.getVariance(), sstats.getVariance(), tol);
        TestUtils.assertEquals(new Variance().evaluate(values), dstats.getVariance(), tol);
        TestUtils.assertEquals(dstats.getSum(), sstats.getSum(), tol);
        TestUtils.assertEquals(new Sum().evaluate(values), dstats.getSum(), tol);
        TestUtils.assertEquals(dstats.getSumsq(), sstats.getSumsq(), tol);
        TestUtils.assertEquals(new SumOfSquares().evaluate(values), dstats.getSumsq(), tol);
        TestUtils.assertEquals(dstats.getPopulationVariance(), sstats.getPopulationVariance(), tol);
        TestUtils.assertEquals(new Variance(false).evaluate(values), dstats.getPopulationVariance(), tol);
    }
}
 
Example 24
/**
 * 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 25
@Test
public void testSummaryConsistency() {
    final DescriptiveStatistics dstats = new DescriptiveStatistics();
    final SummaryStatistics sstats = new SummaryStatistics();
    final int windowSize = 5;
    dstats.setWindowSize(windowSize);
    final double tol = 1E-12;
    for (int i = 0; i < 20; i++) {
        dstats.addValue(i);
        sstats.clear();
        double[] values = dstats.getValues();
        for (int j = 0; j < values.length; j++) {
            sstats.addValue(values[j]);
        }
        TestUtils.assertEquals(dstats.getMean(), sstats.getMean(), tol);
        TestUtils.assertEquals(new Mean().evaluate(values), dstats.getMean(), tol);
        TestUtils.assertEquals(dstats.getMax(), sstats.getMax(), tol);
        TestUtils.assertEquals(new Max().evaluate(values), dstats.getMax(), tol);
        TestUtils.assertEquals(dstats.getGeometricMean(), sstats.getGeometricMean(), tol);
        TestUtils.assertEquals(new GeometricMean().evaluate(values), dstats.getGeometricMean(), tol);
        TestUtils.assertEquals(dstats.getMin(), sstats.getMin(), tol);
        TestUtils.assertEquals(new Min().evaluate(values), dstats.getMin(), tol);
        TestUtils.assertEquals(dstats.getStandardDeviation(), sstats.getStandardDeviation(), tol);
        TestUtils.assertEquals(dstats.getVariance(), sstats.getVariance(), tol);
        TestUtils.assertEquals(new Variance().evaluate(values), dstats.getVariance(), tol);
        TestUtils.assertEquals(dstats.getSum(), sstats.getSum(), tol);
        TestUtils.assertEquals(new Sum().evaluate(values), dstats.getSum(), tol);
        TestUtils.assertEquals(dstats.getSumsq(), sstats.getSumsq(), tol);
        TestUtils.assertEquals(new SumOfSquares().evaluate(values), dstats.getSumsq(), tol);
        TestUtils.assertEquals(dstats.getPopulationVariance(), sstats.getPopulationVariance(), tol);
        TestUtils.assertEquals(new Variance(false).evaluate(values), dstats.getPopulationVariance(), tol);
    }
}
 
Example 26
Source Project: astor   Source File: SummaryStatisticsTest.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * JIRA: MATH-691
 */
@Test
public void testOverrideVarianceWithMathClass() {
    double[] scores = {1, 2, 3, 4};
    SummaryStatistics stats = new SummaryStatistics();
    stats.setVarianceImpl(new Variance(false)); //use "population variance"
    for(double i : scores) {
      stats.addValue(i);
    }
    Assert.assertEquals((new Variance(false)).evaluate(scores),stats.getVariance(), 0); 
}
 
Example 27
@Test
public void testSummaryConsistency() {
    final DescriptiveStatistics dstats = new DescriptiveStatistics();
    final SummaryStatistics sstats = new SummaryStatistics();
    final int windowSize = 5;
    dstats.setWindowSize(windowSize);
    final double tol = 1E-12;
    for (int i = 0; i < 20; i++) {
        dstats.addValue(i);
        sstats.clear();
        double[] values = dstats.getValues();
        for (int j = 0; j < values.length; j++) {
            sstats.addValue(values[j]);
        }
        TestUtils.assertEquals(dstats.getMean(), sstats.getMean(), tol);
        TestUtils.assertEquals(new Mean().evaluate(values), dstats.getMean(), tol);
        TestUtils.assertEquals(dstats.getMax(), sstats.getMax(), tol);
        TestUtils.assertEquals(new Max().evaluate(values), dstats.getMax(), tol);
        TestUtils.assertEquals(dstats.getGeometricMean(), sstats.getGeometricMean(), tol);
        TestUtils.assertEquals(new GeometricMean().evaluate(values), dstats.getGeometricMean(), tol);
        TestUtils.assertEquals(dstats.getMin(), sstats.getMin(), tol);
        TestUtils.assertEquals(new Min().evaluate(values), dstats.getMin(), tol);
        TestUtils.assertEquals(dstats.getStandardDeviation(), sstats.getStandardDeviation(), tol);
        TestUtils.assertEquals(dstats.getVariance(), sstats.getVariance(), tol);
        TestUtils.assertEquals(new Variance().evaluate(values), dstats.getVariance(), tol);
        TestUtils.assertEquals(dstats.getSum(), sstats.getSum(), tol);
        TestUtils.assertEquals(new Sum().evaluate(values), dstats.getSum(), tol);
        TestUtils.assertEquals(dstats.getSumsq(), sstats.getSumsq(), tol);
        TestUtils.assertEquals(new SumOfSquares().evaluate(values), dstats.getSumsq(), tol);
        TestUtils.assertEquals(dstats.getPopulationVariance(), sstats.getPopulationVariance(), tol);
        TestUtils.assertEquals(new Variance(false).evaluate(values), dstats.getPopulationVariance(), tol);
    }
}
 
Example 28
@Test
public void testSummaryConsistency() {
    final DescriptiveStatistics dstats = new DescriptiveStatistics();
    final SummaryStatistics sstats = new SummaryStatistics();
    final int windowSize = 5;
    dstats.setWindowSize(windowSize);
    final double tol = 1E-12;
    for (int i = 0; i < 20; i++) {
        dstats.addValue(i);
        sstats.clear();
        double[] values = dstats.getValues();
        for (int j = 0; j < values.length; j++) {
            sstats.addValue(values[j]);
        }
        TestUtils.assertEquals(dstats.getMean(), sstats.getMean(), tol);
        TestUtils.assertEquals(new Mean().evaluate(values), dstats.getMean(), tol);
        TestUtils.assertEquals(dstats.getMax(), sstats.getMax(), tol);
        TestUtils.assertEquals(new Max().evaluate(values), dstats.getMax(), tol);
        TestUtils.assertEquals(dstats.getGeometricMean(), sstats.getGeometricMean(), tol);
        TestUtils.assertEquals(new GeometricMean().evaluate(values), dstats.getGeometricMean(), tol);
        TestUtils.assertEquals(dstats.getMin(), sstats.getMin(), tol);
        TestUtils.assertEquals(new Min().evaluate(values), dstats.getMin(), tol);
        TestUtils.assertEquals(dstats.getStandardDeviation(), sstats.getStandardDeviation(), tol);
        TestUtils.assertEquals(dstats.getVariance(), sstats.getVariance(), tol);
        TestUtils.assertEquals(new Variance().evaluate(values), dstats.getVariance(), tol);
        TestUtils.assertEquals(dstats.getSum(), sstats.getSum(), tol);
        TestUtils.assertEquals(new Sum().evaluate(values), dstats.getSum(), tol);
        TestUtils.assertEquals(dstats.getSumsq(), sstats.getSumsq(), tol);
        TestUtils.assertEquals(new SumOfSquares().evaluate(values), dstats.getSumsq(), tol);
        TestUtils.assertEquals(dstats.getPopulationVariance(), sstats.getPopulationVariance(), tol);
        TestUtils.assertEquals(new Variance(false).evaluate(values), dstats.getPopulationVariance(), tol);
    }
}
 
Example 29
/**
 * 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 30
/**
 * 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()));

}