org.apache.commons.math3.stat.descriptive.summary.Sum Java Examples

The following examples show how to use org.apache.commons.math3.stat.descriptive.summary.Sum. 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: Mean.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the arithmetic mean of the entries in the specified portion of
 * the input array, or <code>Double.NaN</code> if the designated subarray
 * is empty.
 * <p>
 * Throws <code>IllegalArgumentException</code> if the array is null.</p>
 * <p>
 * See {@link Mean} for details on the computing algorithm.</p>
 *
 * @param values the input array
 * @param begin index of the first array element to include
 * @param length the number of elements to include
 * @return the mean of the values or Double.NaN if length = 0
 * @throws IllegalArgumentException if the array is null or the array index
 *  parameters are not valid
 */
@Override
public double evaluate(final double[] values,final int begin, final int length) {
    if (test(values, begin, length)) {
        Sum sum = new Sum();
        double sampleSize = length;

        // Compute initial estimate using definitional formula
        double xbar = sum.evaluate(values, begin, length) / sampleSize;

        // Compute correction factor in second pass
        double correction = 0;
        for (int i = begin; i < begin + length; i++) {
            correction += values[i] - xbar;
        }
        return xbar + (correction/sampleSize);
    }
    return Double.NaN;
}
 
Example #2
Source File: SummaryStatisticsTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testSetterInjection() {
    SummaryStatistics u = createSummaryStatistics();
    u.setMeanImpl(new Sum());
    u.setSumLogImpl(new Sum());
    u.addValue(1);
    u.addValue(3);
    Assert.assertEquals(4, u.getMean(), 1E-14);
    Assert.assertEquals(4, u.getSumOfLogs(), 1E-14);
    Assert.assertEquals(FastMath.exp(2), u.getGeometricMean(), 1E-14);
    u.clear();
    u.addValue(1);
    u.addValue(2);
    Assert.assertEquals(3, u.getMean(), 1E-14);
    u.clear();
    u.setMeanImpl(new Mean()); // OK after clear
}
 
Example #3
Source File: MultivariateSummaryStatistics.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Construct a MultivariateSummaryStatistics instance
 * @param k dimension of the data
 * @param isCovarianceBiasCorrected if true, the unbiased sample
 * covariance is computed, otherwise the biased population covariance
 * is computed
 */
public MultivariateSummaryStatistics(int k, boolean isCovarianceBiasCorrected) {
    this.k = k;

    sumImpl     = new StorelessUnivariateStatistic[k];
    sumSqImpl   = new StorelessUnivariateStatistic[k];
    minImpl     = new StorelessUnivariateStatistic[k];
    maxImpl     = new StorelessUnivariateStatistic[k];
    sumLogImpl  = new StorelessUnivariateStatistic[k];
    geoMeanImpl = new StorelessUnivariateStatistic[k];
    meanImpl    = new StorelessUnivariateStatistic[k];

    for (int i = 0; i < k; ++i) {
        sumImpl[i]     = new Sum();
        sumSqImpl[i]   = new SumOfSquares();
        minImpl[i]     = new Min();
        maxImpl[i]     = new Max();
        sumLogImpl[i]  = new SumOfLogs();
        geoMeanImpl[i] = new GeometricMean();
        meanImpl[i]    = new Mean();
    }

    covarianceImpl =
        new VectorialCovariance(k, isCovarianceBiasCorrected);

}
 
Example #4
Source File: Mean.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the arithmetic mean of the entries in the specified portion of
 * the input array, or <code>Double.NaN</code> if the designated subarray
 * is empty.
 * <p>
 * Throws <code>IllegalArgumentException</code> if the array is null.</p>
 * <p>
 * See {@link Mean} for details on the computing algorithm.</p>
 *
 * @param values the input array
 * @param begin index of the first array element to include
 * @param length the number of elements to include
 * @return the mean of the values or Double.NaN if length = 0
 * @throws MathIllegalArgumentException if the array is null or the array index
 *  parameters are not valid
 */
@Override
public double evaluate(final double[] values,final int begin, final int length)
throws MathIllegalArgumentException {
    if (test(values, begin, length)) {
        Sum sum = new Sum();
        double sampleSize = length;

        // Compute initial estimate using definitional formula
        double xbar = sum.evaluate(values, begin, length) / sampleSize;

        // Compute correction factor in second pass
        double correction = 0;
        for (int i = begin; i < begin + length; i++) {
            correction += values[i] - xbar;
        }
        return xbar + (correction/sampleSize);
    }
    return Double.NaN;
}
 
Example #5
Source File: Mean.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the arithmetic mean of the entries in the specified portion of
 * the input array, or <code>Double.NaN</code> if the designated subarray
 * is empty.
 * <p>
 * Throws <code>IllegalArgumentException</code> if the array is null.</p>
 * <p>
 * See {@link Mean} for details on the computing algorithm.</p>
 *
 * @param values the input array
 * @param begin index of the first array element to include
 * @param length the number of elements to include
 * @return the mean of the values or Double.NaN if length = 0
 * @throws MathIllegalArgumentException if the array is null or the array index
 *  parameters are not valid
 */
@Override
public double evaluate(final double[] values,final int begin, final int length)
throws MathIllegalArgumentException {
    if (test(values, begin, length)) {
        Sum sum = new Sum();
        double sampleSize = length;

        // Compute initial estimate using definitional formula
        double xbar = sum.evaluate(values, begin, length) / sampleSize;

        // Compute correction factor in second pass
        double correction = 0;
        for (int i = begin; i < begin + length; i++) {
            correction += values[i] - xbar;
        }
        return xbar + (correction/sampleSize);
    }
    return Double.NaN;
}
 
Example #6
Source File: GaugeITest.java    From hawkular-metrics with Apache License 2.0 6 votes vote down vote up
@Test
public void addAndFetchGaugeDataAggregates() throws Exception {
    DateTime start = now();
    DateTime end = start.plusMinutes(50);

    metricsService.createTenant(new Tenant(tenantId), false).toBlocking().lastOrDefault(null);

    Metric<Double> m1 = new Metric<>(new MetricId<>(tenantId, GAUGE, "m1"), asList(
            new DataPoint<>(start.getMillis(), 10.0),
            new DataPoint<>(start.plusMinutes(2).getMillis(), 20.0),
            new DataPoint<>(start.plusMinutes(4).getMillis(), 30.0),
            new DataPoint<>(end.getMillis(), 40.0)));

    Observable<Void> insertObservable = metricsService.addDataPoints(GAUGE, Observable.just(m1));
    insertObservable.toBlocking().lastOrDefault(null);

    @SuppressWarnings("unchecked")
    Observable<Double> observable = metricsService
            .findGaugeData(new MetricId<>(tenantId, GAUGE, "m1"), start.getMillis(), (end.getMillis() + 1000),
                    Aggregate.Min, Aggregate.Max, Aggregate.Average, Aggregate.Sum);
    List<Double> actual = toList(observable);
    List<Double> expected = asList(10.0, 40.0, 25.0, 100.0);

    assertEquals(actual, expected, "The data does not match the expected values");
    assertMetricIndexMatches(tenantId, GAUGE, singletonList(new Metric<>(m1.getMetricId(), m1.getDataPoints(), 7)));
}
 
Example #7
Source File: SummaryStatisticsTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testSetterInjection() {
    SummaryStatistics u = createSummaryStatistics();
    u.setMeanImpl(new Sum());
    u.setSumLogImpl(new Sum());
    u.addValue(1);
    u.addValue(3);
    Assert.assertEquals(4, u.getMean(), 1E-14);
    Assert.assertEquals(4, u.getSumOfLogs(), 1E-14);
    Assert.assertEquals(FastMath.exp(2), u.getGeometricMean(), 1E-14);
    u.clear();
    u.addValue(1);
    u.addValue(2);
    Assert.assertEquals(3, u.getMean(), 1E-14);
    u.clear();
    u.setMeanImpl(new Mean()); // OK after clear
}
 
Example #8
Source File: SummaryStatisticsTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testSetterInjection() {
    SummaryStatistics u = createSummaryStatistics();
    u.setMeanImpl(new Sum());
    u.setSumLogImpl(new Sum());
    u.addValue(1);
    u.addValue(3);
    Assert.assertEquals(4, u.getMean(), 1E-14);
    Assert.assertEquals(4, u.getSumOfLogs(), 1E-14);
    Assert.assertEquals(FastMath.exp(2), u.getGeometricMean(), 1E-14);
    u.clear();
    u.addValue(1);
    u.addValue(2);
    Assert.assertEquals(3, u.getMean(), 1E-14);
    u.clear();
    u.setMeanImpl(new Mean()); // OK after clear
}
 
Example #9
Source File: MultivariateSummaryStatistics.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Construct a MultivariateSummaryStatistics instance
 * @param k dimension of the data
 * @param isCovarianceBiasCorrected if true, the unbiased sample
 * covariance is computed, otherwise the biased population covariance
 * is computed
 */
public MultivariateSummaryStatistics(int k, boolean isCovarianceBiasCorrected) {
    this.k = k;

    sumImpl     = new StorelessUnivariateStatistic[k];
    sumSqImpl   = new StorelessUnivariateStatistic[k];
    minImpl     = new StorelessUnivariateStatistic[k];
    maxImpl     = new StorelessUnivariateStatistic[k];
    sumLogImpl  = new StorelessUnivariateStatistic[k];
    geoMeanImpl = new StorelessUnivariateStatistic[k];
    meanImpl    = new StorelessUnivariateStatistic[k];

    for (int i = 0; i < k; ++i) {
        sumImpl[i]     = new Sum();
        sumSqImpl[i]   = new SumOfSquares();
        minImpl[i]     = new Min();
        maxImpl[i]     = new Max();
        sumLogImpl[i]  = new SumOfLogs();
        geoMeanImpl[i] = new GeometricMean();
        meanImpl[i]    = new Mean();
    }

    covarianceImpl =
        new VectorialCovariance(k, isCovarianceBiasCorrected);

}
 
Example #10
Source File: MultivariateSummaryStatistics.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Construct a MultivariateSummaryStatistics instance
 * @param k dimension of the data
 * @param isCovarianceBiasCorrected if true, the unbiased sample
 * covariance is computed, otherwise the biased population covariance
 * is computed
 */
public MultivariateSummaryStatistics(int k, boolean isCovarianceBiasCorrected) {
    this.k = k;

    sumImpl     = new StorelessUnivariateStatistic[k];
    sumSqImpl   = new StorelessUnivariateStatistic[k];
    minImpl     = new StorelessUnivariateStatistic[k];
    maxImpl     = new StorelessUnivariateStatistic[k];
    sumLogImpl  = new StorelessUnivariateStatistic[k];
    geoMeanImpl = new StorelessUnivariateStatistic[k];
    meanImpl    = new StorelessUnivariateStatistic[k];

    for (int i = 0; i < k; ++i) {
        sumImpl[i]     = new Sum();
        sumSqImpl[i]   = new SumOfSquares();
        minImpl[i]     = new Min();
        maxImpl[i]     = new Max();
        sumLogImpl[i]  = new SumOfLogs();
        geoMeanImpl[i] = new GeometricMean();
        meanImpl[i]    = new Mean();
    }

    covarianceImpl =
        new VectorialCovariance(k, isCovarianceBiasCorrected);

}
 
Example #11
Source File: SummaryStatisticsTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testSetterInjection() {
    SummaryStatistics u = createSummaryStatistics();
    u.setMeanImpl(new Sum());
    u.setSumLogImpl(new Sum());
    u.addValue(1);
    u.addValue(3);
    Assert.assertEquals(4, u.getMean(), 1E-14);
    Assert.assertEquals(4, u.getSumOfLogs(), 1E-14);
    Assert.assertEquals(FastMath.exp(2), u.getGeometricMean(), 1E-14);
    u.clear();
    u.addValue(1);
    u.addValue(2);
    Assert.assertEquals(3, u.getMean(), 1E-14);
    u.clear();
    u.setMeanImpl(new Mean()); // OK after clear
}
 
Example #12
Source File: SumNumericBucketPointCollector.java    From hawkular-metrics with Apache License 2.0 6 votes vote down vote up
void increment(NumericBucketPoint bucketPoint) {
    if (!bucketPoint.isEmpty()) {
        min.increment(bucketPoint.getMin());
        average.increment(bucketPoint.getAvg());
        median.increment(bucketPoint.getMedian());
        max.increment(bucketPoint.getMax());
        sum.increment(bucketPoint.getSum());
        samples.increment(1);
        if (bucketPoint.getPercentiles() != null) {
            for (Percentile p : bucketPoint.getPercentiles()) {
                percentiles.computeIfAbsent(p.getOriginalQuantile(), k -> new Sum())
                        .increment(p.getValue());
            }
        }
    }

    start = bucketPoint.getStart();
    end = bucketPoint.getEnd();
}
 
Example #13
Source File: MultivariateSummaryStatistics.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Construct a MultivariateSummaryStatistics instance
 * @param k dimension of the data
 * @param isCovarianceBiasCorrected if true, the unbiased sample
 * covariance is computed, otherwise the biased population covariance
 * is computed
 */
public MultivariateSummaryStatistics(int k, boolean isCovarianceBiasCorrected) {
    this.k = k;

    sumImpl     = new StorelessUnivariateStatistic[k];
    sumSqImpl   = new StorelessUnivariateStatistic[k];
    minImpl     = new StorelessUnivariateStatistic[k];
    maxImpl     = new StorelessUnivariateStatistic[k];
    sumLogImpl  = new StorelessUnivariateStatistic[k];
    geoMeanImpl = new StorelessUnivariateStatistic[k];
    meanImpl    = new StorelessUnivariateStatistic[k];

    for (int i = 0; i < k; ++i) {
        sumImpl[i]     = new Sum();
        sumSqImpl[i]   = new SumOfSquares();
        minImpl[i]     = new Min();
        maxImpl[i]     = new Max();
        sumLogImpl[i]  = new SumOfLogs();
        geoMeanImpl[i] = new GeometricMean();
        meanImpl[i]    = new Mean();
    }

    covarianceImpl =
        new VectorialCovariance(k, isCovarianceBiasCorrected);

}
 
Example #14
Source File: MultivariateSummaryStatistics.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Construct a MultivariateSummaryStatistics instance
 * @param k dimension of the data
 * @param isCovarianceBiasCorrected if true, the unbiased sample
 * covariance is computed, otherwise the biased population covariance
 * is computed
 */
public MultivariateSummaryStatistics(int k, boolean isCovarianceBiasCorrected) {
    this.k = k;

    sumImpl     = new StorelessUnivariateStatistic[k];
    sumSqImpl   = new StorelessUnivariateStatistic[k];
    minImpl     = new StorelessUnivariateStatistic[k];
    maxImpl     = new StorelessUnivariateStatistic[k];
    sumLogImpl  = new StorelessUnivariateStatistic[k];
    geoMeanImpl = new StorelessUnivariateStatistic[k];
    meanImpl    = new StorelessUnivariateStatistic[k];

    for (int i = 0; i < k; ++i) {
        sumImpl[i]     = new Sum();
        sumSqImpl[i]   = new SumOfSquares();
        minImpl[i]     = new Min();
        maxImpl[i]     = new Max();
        sumLogImpl[i]  = new SumOfLogs();
        geoMeanImpl[i] = new GeometricMean();
        meanImpl[i]    = new Mean();
    }

    covarianceImpl =
        new VectorialCovariance(k, isCovarianceBiasCorrected);

}
 
Example #15
Source File: Mean.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the arithmetic mean of the entries in the specified portion of
 * the input array, or <code>Double.NaN</code> if the designated subarray
 * is empty.
 * <p>
 * Throws <code>IllegalArgumentException</code> if the array is null.</p>
 * <p>
 * See {@link Mean} for details on the computing algorithm.</p>
 *
 * @param values the input array
 * @param begin index of the first array element to include
 * @param length the number of elements to include
 * @return the mean of the values or Double.NaN if length = 0
 * @throws IllegalArgumentException if the array is null or the array index
 *  parameters are not valid
 */
@Override
public double evaluate(final double[] values,final int begin, final int length) {
    if (test(values, begin, length)) {
        Sum sum = new Sum();
        double sampleSize = length;

        // Compute initial estimate using definitional formula
        double xbar = sum.evaluate(values, begin, length) / sampleSize;

        // Compute correction factor in second pass
        double correction = 0;
        for (int i = begin; i < begin + length; i++) {
            correction += values[i] - xbar;
        }
        return xbar + (correction/sampleSize);
    }
    return Double.NaN;
}
 
Example #16
Source File: MultivariateSummaryStatistics.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Construct a MultivariateSummaryStatistics instance
 * @param k dimension of the data
 * @param isCovarianceBiasCorrected if true, the unbiased sample
 * covariance is computed, otherwise the biased population covariance
 * is computed
 */
public MultivariateSummaryStatistics(int k, boolean isCovarianceBiasCorrected) {
    this.k = k;

    sumImpl     = new StorelessUnivariateStatistic[k];
    sumSqImpl   = new StorelessUnivariateStatistic[k];
    minImpl     = new StorelessUnivariateStatistic[k];
    maxImpl     = new StorelessUnivariateStatistic[k];
    sumLogImpl  = new StorelessUnivariateStatistic[k];
    geoMeanImpl = new StorelessUnivariateStatistic[k];
    meanImpl    = new StorelessUnivariateStatistic[k];

    for (int i = 0; i < k; ++i) {
        sumImpl[i]     = new Sum();
        sumSqImpl[i]   = new SumOfSquares();
        minImpl[i]     = new Min();
        maxImpl[i]     = new Max();
        sumLogImpl[i]  = new SumOfLogs();
        geoMeanImpl[i] = new GeometricMean();
        meanImpl[i]    = new Mean();
    }

    covarianceImpl =
        new VectorialCovariance(k, isCovarianceBiasCorrected);

}
 
Example #17
Source File: Mean.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the arithmetic mean of the entries in the specified portion of
 * the input array, or <code>Double.NaN</code> if the designated subarray
 * is empty.
 * <p>
 * Throws <code>IllegalArgumentException</code> if the array is null.</p>
 * <p>
 * See {@link Mean} for details on the computing algorithm.</p>
 *
 * @param values the input array
 * @param begin index of the first array element to include
 * @param length the number of elements to include
 * @return the mean of the values or Double.NaN if length = 0
 * @throws MathIllegalArgumentException if the array is null or the array index
 *  parameters are not valid
 */
@Override
public double evaluate(final double[] values,final int begin, final int length)
throws MathIllegalArgumentException {
    if (test(values, begin, length)) {
        Sum sum = new Sum();
        double sampleSize = length;

        // Compute initial estimate using definitional formula
        double xbar = sum.evaluate(values, begin, length) / sampleSize;

        // Compute correction factor in second pass
        double correction = 0;
        for (int i = begin; i < begin + length; i++) {
            correction += values[i] - xbar;
        }
        return xbar + (correction/sampleSize);
    }
    return Double.NaN;
}
 
Example #18
Source File: SummaryStatisticsTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testSetterInjection() {
    SummaryStatistics u = createSummaryStatistics();
    u.setMeanImpl(new Sum());
    u.setSumLogImpl(new Sum());
    u.addValue(1);
    u.addValue(3);
    Assert.assertEquals(4, u.getMean(), 1E-14);
    Assert.assertEquals(4, u.getSumOfLogs(), 1E-14);
    Assert.assertEquals(FastMath.exp(2), u.getGeometricMean(), 1E-14);
    u.clear();
    u.addValue(1);
    u.addValue(2);
    Assert.assertEquals(3, u.getMean(), 1E-14);
    u.clear();
    u.setMeanImpl(new Mean()); // OK after clear
}
 
Example #19
Source File: MultivariateSummaryStatistics.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Construct a MultivariateSummaryStatistics instance
 * @param k dimension of the data
 * @param isCovarianceBiasCorrected if true, the unbiased sample
 * covariance is computed, otherwise the biased population covariance
 * is computed
 */
public MultivariateSummaryStatistics(int k, boolean isCovarianceBiasCorrected) {
    this.k = k;

    sumImpl     = new StorelessUnivariateStatistic[k];
    sumSqImpl   = new StorelessUnivariateStatistic[k];
    minImpl     = new StorelessUnivariateStatistic[k];
    maxImpl     = new StorelessUnivariateStatistic[k];
    sumLogImpl  = new StorelessUnivariateStatistic[k];
    geoMeanImpl = new StorelessUnivariateStatistic[k];
    meanImpl    = new StorelessUnivariateStatistic[k];

    for (int i = 0; i < k; ++i) {
        sumImpl[i]     = new Sum();
        sumSqImpl[i]   = new SumOfSquares();
        minImpl[i]     = new Min();
        maxImpl[i]     = new Max();
        sumLogImpl[i]  = new SumOfLogs();
        geoMeanImpl[i] = new GeometricMean();
        meanImpl[i]    = new Mean();
    }

    covarianceImpl =
        new VectorialCovariance(k, isCovarianceBiasCorrected);

}
 
Example #20
Source File: MultivariateSummaryStatistics.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Construct a MultivariateSummaryStatistics instance
 * @param k dimension of the data
 * @param isCovarianceBiasCorrected if true, the unbiased sample
 * covariance is computed, otherwise the biased population covariance
 * is computed
 */
public MultivariateSummaryStatistics(int k, boolean isCovarianceBiasCorrected) {
    this.k = k;

    sumImpl     = new StorelessUnivariateStatistic[k];
    sumSqImpl   = new StorelessUnivariateStatistic[k];
    minImpl     = new StorelessUnivariateStatistic[k];
    maxImpl     = new StorelessUnivariateStatistic[k];
    sumLogImpl  = new StorelessUnivariateStatistic[k];
    geoMeanImpl = new StorelessUnivariateStatistic[k];
    meanImpl    = new StorelessUnivariateStatistic[k];

    for (int i = 0; i < k; ++i) {
        sumImpl[i]     = new Sum();
        sumSqImpl[i]   = new SumOfSquares();
        minImpl[i]     = new Min();
        maxImpl[i]     = new Max();
        sumLogImpl[i]  = new SumOfLogs();
        geoMeanImpl[i] = new GeometricMean();
        meanImpl[i]    = new Mean();
    }

    covarianceImpl =
        new VectorialCovariance(k, isCovarianceBiasCorrected);

}
 
Example #21
Source File: SummaryStatisticsTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testSetterInjection() throws Exception {
    SummaryStatistics u = createSummaryStatistics();
    u.setMeanImpl(new Sum());
    u.setSumLogImpl(new Sum());
    u.addValue(1);
    u.addValue(3);
    Assert.assertEquals(4, u.getMean(), 1E-14);
    Assert.assertEquals(4, u.getSumOfLogs(), 1E-14);
    Assert.assertEquals(FastMath.exp(2), u.getGeometricMean(), 1E-14);
    u.clear();
    u.addValue(1);
    u.addValue(2);
    Assert.assertEquals(3, u.getMean(), 1E-14);
    u.clear();
    u.setMeanImpl(new Mean()); // OK after clear
}
 
Example #22
Source File: Mean.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the arithmetic mean of the entries in the specified portion of
 * the input array, or <code>Double.NaN</code> if the designated subarray
 * is empty.
 * <p>
 * Throws <code>IllegalArgumentException</code> if the array is null.</p>
 * <p>
 * See {@link Mean} for details on the computing algorithm.</p>
 *
 * @param values the input array
 * @param begin index of the first array element to include
 * @param length the number of elements to include
 * @return the mean of the values or Double.NaN if length = 0
 * @throws MathIllegalArgumentException if the array is null or the array index
 *  parameters are not valid
 */
@Override
public double evaluate(final double[] values,final int begin, final int length)
throws MathIllegalArgumentException {
    if (test(values, begin, length)) {
        Sum sum = new Sum();
        double sampleSize = length;

        // Compute initial estimate using definitional formula
        double xbar = sum.evaluate(values, begin, length) / sampleSize;

        // Compute correction factor in second pass
        double correction = 0;
        for (int i = begin; i < begin + length; i++) {
            correction += values[i] - xbar;
        }
        return xbar + (correction/sampleSize);
    }
    return Double.NaN;
}
 
Example #23
Source File: SummaryStatisticsTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testSetterInjection() {
    SummaryStatistics u = createSummaryStatistics();
    u.setMeanImpl(new Sum());
    u.setSumLogImpl(new Sum());
    u.addValue(1);
    u.addValue(3);
    Assert.assertEquals(4, u.getMean(), 1E-14);
    Assert.assertEquals(4, u.getSumOfLogs(), 1E-14);
    Assert.assertEquals(FastMath.exp(2), u.getGeometricMean(), 1E-14);
    u.clear();
    u.addValue(1);
    u.addValue(2);
    Assert.assertEquals(3, u.getMean(), 1E-14);
    u.clear();
    u.setMeanImpl(new Mean()); // OK after clear
}
 
Example #24
Source File: Mean.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the arithmetic mean of the entries in the specified portion of
 * the input array, or <code>Double.NaN</code> if the designated subarray
 * is empty.
 * <p>
 * Throws <code>IllegalArgumentException</code> if the array is null.</p>
 * <p>
 * See {@link Mean} for details on the computing algorithm.</p>
 *
 * @param values the input array
 * @param begin index of the first array element to include
 * @param length the number of elements to include
 * @return the mean of the values or Double.NaN if length = 0
 * @throws MathIllegalArgumentException if the array is null or the array index
 *  parameters are not valid
 */
@Override
public double evaluate(final double[] values,final int begin, final int length)
throws MathIllegalArgumentException {
    if (test(values, begin, length)) {
        Sum sum = new Sum();
        double sampleSize = length;

        // Compute initial estimate using definitional formula
        double xbar = sum.evaluate(values, begin, length) / sampleSize;

        // Compute correction factor in second pass
        double correction = 0;
        for (int i = begin; i < begin + length; i++) {
            correction += values[i] - xbar;
        }
        return xbar + (correction/sampleSize);
    }
    return Double.NaN;
}
 
Example #25
Source File: Mean.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns the arithmetic mean of the entries in the specified portion of
 * the input array, or <code>Double.NaN</code> if the designated subarray
 * is empty.
 * <p>
 * Throws <code>IllegalArgumentException</code> if the array is null.</p>
 * <p>
 * See {@link Mean} for details on the computing algorithm.</p>
 *
 * @param values the input array
 * @param begin index of the first array element to include
 * @param length the number of elements to include
 * @return the mean of the values or Double.NaN if length = 0
 * @throws MathIllegalArgumentException if the array is null or the array index
 *  parameters are not valid
 */
@Override
public double evaluate(final double[] values,final int begin, final int length)
throws MathIllegalArgumentException {
    if (test(values, begin, length)) {
        Sum sum = new Sum();
        double sampleSize = length;

        // Compute initial estimate using definitional formula
        double xbar = sum.evaluate(values, begin, length) / sampleSize;

        // Compute correction factor in second pass
        double correction = 0;
        for (int i = begin; i < begin + length; i++) {
            correction += values[i] - xbar;
        }
        return xbar + (correction/sampleSize);
    }
    return Double.NaN;
}
 
Example #26
Source File: SummaryStatisticsTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testSetterInjection() {
    SummaryStatistics u = createSummaryStatistics();
    u.setMeanImpl(new Sum());
    u.setSumLogImpl(new Sum());
    u.addValue(1);
    u.addValue(3);
    Assert.assertEquals(4, u.getMean(), 1E-14);
    Assert.assertEquals(4, u.getSumOfLogs(), 1E-14);
    Assert.assertEquals(FastMath.exp(2), u.getGeometricMean(), 1E-14);
    u.clear();
    u.addValue(1);
    u.addValue(2);
    Assert.assertEquals(3, u.getMean(), 1E-14);
    u.clear();
    u.setMeanImpl(new Mean()); // OK after clear
}
 
Example #27
Source File: SummaryStatisticsTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testCopy() {
    SummaryStatistics u = createSummaryStatistics();
    u.addValue(2d);
    u.addValue(1d);
    u.addValue(3d);
    u.addValue(4d);
    SummaryStatistics v = new SummaryStatistics(u);
    Assert.assertEquals(u, v);
    Assert.assertEquals(v, u);

    // Make sure both behave the same with additional values added
    u.addValue(7d);
    u.addValue(9d);
    u.addValue(11d);
    u.addValue(23d);
    v.addValue(7d);
    v.addValue(9d);
    v.addValue(11d);
    v.addValue(23d);
    Assert.assertEquals(u, v);
    Assert.assertEquals(v, u);

    // Check implementation pointers are preserved
    u.clear();
    u.setSumImpl(new Sum());
    SummaryStatistics.copy(u,v);
    Assert.assertEquals(u.getSumImpl(), v.getSumImpl());

}
 
Example #28
Source File: SummaryStatisticsTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testCopy() {
    SummaryStatistics u = createSummaryStatistics();
    u.addValue(2d);
    u.addValue(1d);
    u.addValue(3d);
    u.addValue(4d);
    SummaryStatistics v = new SummaryStatistics(u);
    Assert.assertEquals(u, v);
    Assert.assertEquals(v, u);

    // Make sure both behave the same with additional values added
    u.addValue(7d);
    u.addValue(9d);
    u.addValue(11d);
    u.addValue(23d);
    v.addValue(7d);
    v.addValue(9d);
    v.addValue(11d);
    v.addValue(23d);
    Assert.assertEquals(u, v);
    Assert.assertEquals(v, u);

    // Check implementation pointers are preserved
    u.clear();
    u.setSumImpl(new Sum());
    SummaryStatistics.copy(u,v);
    Assert.assertEquals(u.getSumImpl(), v.getSumImpl());

}
 
Example #29
Source File: DescriptiveStatisticsTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@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 #30
Source File: DescriptiveStatisticsTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@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);
    }
}