org.apache.commons.math.stat.descriptive.moment.Mean Java Examples

The following examples show how to use org.apache.commons.math.stat.descriptive.moment.Mean. 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: SummaryStatistics.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Add a value to the data
 * @param value the value to add
 */
public void addValue(double value) {
    sumImpl.increment(value);
    sumsqImpl.increment(value);
    minImpl.increment(value);
    maxImpl.increment(value);
    sumLogImpl.increment(value);
    secondMoment.increment(value);
    // If mean, variance or geomean have been overridden,
    // need to increment these
    if (!(meanImpl instanceof Mean)) {
        meanImpl.increment(value);
    }
    if (!(varianceImpl instanceof Variance)) {
        varianceImpl.increment(value);
    }
    if (!(geoMeanImpl instanceof GeometricMean)) {
        geoMeanImpl.increment(value);
    }
    n++;
}
 
Example #2
Source File: InteractionTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testInteraction() {

        FourthMoment m4 = new FourthMoment();
        Mean m = new Mean(m4);
        Variance v = new Variance(m4);
        Skewness s= new Skewness(m4);
        Kurtosis k = new Kurtosis(m4);

        for (int i = 0; i < testArray.length; i++){
            m4.increment(testArray[i]);
        }

        assertEquals(mean,m.getResult(),tolerance);
        assertEquals(var,v.getResult(),tolerance);
        assertEquals(skew ,s.getResult(),tolerance);
        assertEquals(kurt,k.getResult(),tolerance);

    }
 
Example #3
Source File: SummaryStatisticsTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testSetterInjection() throws Exception {
    SummaryStatistics u = createSummaryStatistics();
    u.setMeanImpl(new Sum());
    u.setSumLogImpl(new Sum());
    u.addValue(1);
    u.addValue(3);
    assertEquals(4, u.getMean(), 1E-14);
    assertEquals(4, u.getSumOfLogs(), 1E-14);
    assertEquals(Math.exp(2), u.getGeometricMean(), 1E-14);
    u.clear();
    u.addValue(1);
    u.addValue(2);
    assertEquals(3, u.getMean(), 1E-14);
    u.clear();
    u.setMeanImpl(new Mean()); // OK after clear
}
 
Example #4
Source File: Covariance.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Computes the covariance between the two arrays.
 *
 * <p>Array lengths must match and the common length must be at least 2.</p>
 *
 * @param xArray first data array
 * @param yArray second data array
 * @param biasCorrected if true, returned value will be bias-corrected
 * @return returns the covariance for the two arrays
 * @throws  IllegalArgumentException if the arrays lengths do not match or
 * there is insufficient data
 */
public double covariance(final double[] xArray, final double[] yArray, boolean biasCorrected)
    throws IllegalArgumentException {
    Mean mean = new Mean();
    double result = 0d;
    int length = xArray.length;
    if(length == yArray.length && length > 1) {
        double xMean = mean.evaluate(xArray);
        double yMean = mean.evaluate(yArray);
        for (int i = 0; i < length; i++) {
            double xDev = xArray[i] - xMean;
            double yDev = yArray[i] - yMean;
            result += (xDev * yDev - result) / (i + 1);
        }
    }
    else {
        throw MathRuntimeException.createIllegalArgumentException(
           "arrays must have the same length and both must have at " +
           "least two elements. xArray has size {0}, yArray has {1} elements",
                length, yArray.length);
    }
    return biasCorrected ? result * ((double) length / (double)(length - 1)) : result;
}
 
Example #5
Source File: MultivariateSummaryStatisticsTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testSetterInjection() throws Exception {
    MultivariateSummaryStatistics u = createMultivariateSummaryStatistics(2, true);
    u.setMeanImpl(new StorelessUnivariateStatistic[] {
                    new sumMean(), new sumMean()
                  });
    u.addValue(new double[] { 1, 2 });
    u.addValue(new double[] { 3, 4 });
    assertEquals(4, u.getMean()[0], 1E-14);
    assertEquals(6, u.getMean()[1], 1E-14);
    u.clear();
    u.addValue(new double[] { 1, 2 });
    u.addValue(new double[] { 3, 4 });
    assertEquals(4, u.getMean()[0], 1E-14);
    assertEquals(6, u.getMean()[1], 1E-14);
    u.clear();
    u.setMeanImpl(new StorelessUnivariateStatistic[] {
                    new Mean(), new Mean()
                  }); // OK after clear
    u.addValue(new double[] { 1, 2 });
    u.addValue(new double[] { 3, 4 });
    assertEquals(2, u.getMean()[0], 1E-14);
    assertEquals(3, u.getMean()[1], 1E-14);
    assertEquals(2, u.getDimension());
}
 
Example #6
Source File: MultivariateSummaryStatisticsTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testSetterInjection() throws Exception {
    MultivariateSummaryStatistics u = createMultivariateSummaryStatistics(2, true);
    u.setMeanImpl(new StorelessUnivariateStatistic[] {
                    new sumMean(), new sumMean()
                  });
    u.addValue(new double[] { 1, 2 });
    u.addValue(new double[] { 3, 4 });
    assertEquals(4, u.getMean()[0], 1E-14);
    assertEquals(6, u.getMean()[1], 1E-14);
    u.clear();
    u.addValue(new double[] { 1, 2 });
    u.addValue(new double[] { 3, 4 });
    assertEquals(4, u.getMean()[0], 1E-14);
    assertEquals(6, u.getMean()[1], 1E-14);
    u.clear();
    u.setMeanImpl(new StorelessUnivariateStatistic[] {
                    new Mean(), new Mean()
                  }); // OK after clear
    u.addValue(new double[] { 1, 2 });
    u.addValue(new double[] { 3, 4 });
    assertEquals(2, u.getMean()[0], 1E-14);
    assertEquals(3, u.getMean()[1], 1E-14);
    assertEquals(2, u.getDimension());
}
 
Example #7
Source File: SummaryStatistics.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Add a value to the data
 * @param value the value to add
 */
public void addValue(double value) {
    sumImpl.increment(value);
    sumsqImpl.increment(value);
    minImpl.increment(value);
    maxImpl.increment(value);
    sumLogImpl.increment(value);
    secondMoment.increment(value);
    // If mean, variance or geomean have been overridden,
    // need to increment these
    if (!(meanImpl instanceof Mean)) {
        meanImpl.increment(value);
    }
    if (!(varianceImpl instanceof Variance)) {
        varianceImpl.increment(value);
    }
    if (!(geoMeanImpl instanceof GeometricMean)) {
        geoMeanImpl.increment(value);
    }
    n++;
}
 
Example #8
Source File: SummaryStatistics.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Add a value to the data
 * @param value the value to add
 */
public void addValue(double value) {
    sumImpl.increment(value);
    sumsqImpl.increment(value);
    minImpl.increment(value);
    maxImpl.increment(value);
    sumLogImpl.increment(value);
    secondMoment.increment(value);
    // If mean, variance or geomean have been overridden,
    // need to increment these
    if (!(meanImpl instanceof Mean)) {
        meanImpl.increment(value);
    }
    if (!(varianceImpl instanceof Variance)) {
        varianceImpl.increment(value);
    }
    if (!(geoMeanImpl instanceof GeometricMean)) {
        geoMeanImpl.increment(value);
    }
    n++;
}
 
Example #9
Source File: MultivariateSummaryStatisticsTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testSetterInjection() throws Exception {
    MultivariateSummaryStatistics u = createMultivariateSummaryStatistics(2, true);
    u.setMeanImpl(new StorelessUnivariateStatistic[] {
                    new sumMean(), new sumMean()
                  });
    u.addValue(new double[] { 1, 2 });
    u.addValue(new double[] { 3, 4 });
    assertEquals(4, u.getMean()[0], 1E-14);
    assertEquals(6, u.getMean()[1], 1E-14);
    u.clear();
    u.addValue(new double[] { 1, 2 });
    u.addValue(new double[] { 3, 4 });
    assertEquals(4, u.getMean()[0], 1E-14);
    assertEquals(6, u.getMean()[1], 1E-14);
    u.clear();
    u.setMeanImpl(new StorelessUnivariateStatistic[] {
                    new Mean(), new Mean()
                  }); // OK after clear
    u.addValue(new double[] { 1, 2 });
    u.addValue(new double[] { 3, 4 });
    assertEquals(2, u.getMean()[0], 1E-14);
    assertEquals(3, u.getMean()[1], 1E-14);
    assertEquals(2, u.getDimension());
}
 
Example #10
Source File: SummaryStatistics.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Add a value to the data
 * @param value the value to add
 */
public void addValue(double value) {
    sumImpl.increment(value);
    sumsqImpl.increment(value);
    minImpl.increment(value);
    maxImpl.increment(value);
    sumLogImpl.increment(value);
    secondMoment.increment(value);
    // If mean, variance or geomean have been overridden,
    // need to increment these
    if (!(meanImpl instanceof Mean)) {
        meanImpl.increment(value);
    }
    if (!(varianceImpl instanceof Variance)) {
        varianceImpl.increment(value);
    }
    if (!(geoMeanImpl instanceof GeometricMean)) {
        geoMeanImpl.increment(value);
    }
    n++;
}
 
Example #11
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 #12
Source File: Covariance.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Computes the covariance between the two arrays.
 *
 * <p>Array lengths must match and the common length must be at least 2.</p>
 *
 * @param xArray first data array
 * @param yArray second data array
 * @param biasCorrected if true, returned value will be bias-corrected
 * @return returns the covariance for the two arrays
 * @throws  IllegalArgumentException if the arrays lengths do not match or
 * there is insufficient data
 */
public double covariance(final double[] xArray, final double[] yArray, boolean biasCorrected)
    throws IllegalArgumentException {
    Mean mean = new Mean();
    double result = 0d;
    int length = xArray.length;
    if(length == yArray.length && length > 1) {
        double xMean = mean.evaluate(xArray);
        double yMean = mean.evaluate(yArray);
        for (int i = 0; i < length; i++) {
            double xDev = xArray[i] - xMean;
            double yDev = yArray[i] - yMean;
            result += (xDev * yDev - result) / (i + 1);
        }
    }
    else {
        throw MathRuntimeException.createIllegalArgumentException(
           "arrays must have the same length and both must have at " +
           "least two elements. xArray has size {0}, yArray has {1} elements",
                length, yArray.length);
    }
    return biasCorrected ? result * ((double) length / (double)(length - 1)) : result;
}
 
Example #13
Source File: Covariance.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Computes the covariance between the two arrays.
 *
 * <p>Array lengths must match and the common length must be at least 2.</p>
 *
 * @param xArray first data array
 * @param yArray second data array
 * @param biasCorrected if true, returned value will be bias-corrected
 * @return returns the covariance for the two arrays
 * @throws  IllegalArgumentException if the arrays lengths do not match or
 * there is insufficient data
 */
public double covariance(final double[] xArray, final double[] yArray, boolean biasCorrected)
    throws IllegalArgumentException {
    Mean mean = new Mean();
    double result = 0d;
    int length = xArray.length;
    if (length != yArray.length) {
        throw MathRuntimeException.createIllegalArgumentException(
              LocalizedFormats.DIMENSIONS_MISMATCH_SIMPLE, length, yArray.length);
    } else if (length < 2) {
        throw MathRuntimeException.createIllegalArgumentException(
              LocalizedFormats.INSUFFICIENT_DIMENSION, length, 2);
    } else {
        double xMean = mean.evaluate(xArray);
        double yMean = mean.evaluate(yArray);
        for (int i = 0; i < length; i++) {
            double xDev = xArray[i] - xMean;
            double yDev = yArray[i] - yMean;
            result += (xDev * yDev - result) / (i + 1);
        }
    }
    return biasCorrected ? result * ((double) length / (double)(length - 1)) : result;
}
 
Example #14
Source File: SummaryStatistics.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Add a value to the data
 * @param value the value to add
 */
public void addValue(double value) {
    sumImpl.increment(value);
    sumsqImpl.increment(value);
    minImpl.increment(value);
    maxImpl.increment(value);
    sumLogImpl.increment(value);
    secondMoment.increment(value);
    // If mean, variance or geomean have been overridden,
    // need to increment these
    if (!(meanImpl instanceof Mean)) {
        meanImpl.increment(value);
    }
    if (!(varianceImpl instanceof Variance)) {
        varianceImpl.increment(value);
    }
    if (!(geoMeanImpl instanceof GeometricMean)) {
        geoMeanImpl.increment(value);
    }
    n++;
}
 
Example #15
Source File: MultivariateSummaryStatisticsTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testSetterInjection() throws Exception {
    MultivariateSummaryStatistics u = createMultivariateSummaryStatistics(2, true);
    u.setMeanImpl(new StorelessUnivariateStatistic[] {
                    new sumMean(), new sumMean()
                  });
    u.addValue(new double[] { 1, 2 });
    u.addValue(new double[] { 3, 4 });
    assertEquals(4, u.getMean()[0], 1E-14);
    assertEquals(6, u.getMean()[1], 1E-14);
    u.clear();
    u.addValue(new double[] { 1, 2 });
    u.addValue(new double[] { 3, 4 });
    assertEquals(4, u.getMean()[0], 1E-14);
    assertEquals(6, u.getMean()[1], 1E-14);
    u.clear();
    u.setMeanImpl(new StorelessUnivariateStatistic[] {
                    new Mean(), new Mean()
                  }); // OK after clear
    u.addValue(new double[] { 1, 2 });
    u.addValue(new double[] { 3, 4 });
    assertEquals(2, u.getMean()[0], 1E-14);
    assertEquals(3, u.getMean()[1], 1E-14);
    assertEquals(2, u.getDimension());
}
 
Example #16
Source File: SummaryStatisticsTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testSetterInjection() throws Exception {
    SummaryStatistics u = createSummaryStatistics();
    u.setMeanImpl(new Sum());
    u.setSumLogImpl(new Sum());
    u.addValue(1);
    u.addValue(3);
    assertEquals(4, u.getMean(), 1E-14);
    assertEquals(4, u.getSumOfLogs(), 1E-14);
    assertEquals(Math.exp(2), u.getGeometricMean(), 1E-14);
    u.clear();
    u.addValue(1);
    u.addValue(2);
    assertEquals(3, u.getMean(), 1E-14);
    u.clear();
    u.setMeanImpl(new Mean()); // OK after clear
}
 
Example #17
Source File: InteractionTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testInteraction() {

        FourthMoment m4 = new FourthMoment();
        Mean m = new Mean(m4);
        Variance v = new Variance(m4);
        Skewness s= new Skewness(m4);
        Kurtosis k = new Kurtosis(m4);

        for (int i = 0; i < testArray.length; i++){
            m4.increment(testArray[i]);
        }

        assertEquals(mean,m.getResult(),tolerance);
        assertEquals(var,v.getResult(),tolerance);
        assertEquals(skew ,s.getResult(),tolerance);
        assertEquals(kurt,k.getResult(),tolerance);

    }
 
Example #18
Source File: Covariance.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Computes the covariance between the two arrays.
 *
 * <p>Array lengths must match and the common length must be at least 2.</p>
 *
 * @param xArray first data array
 * @param yArray second data array
 * @param biasCorrected if true, returned value will be bias-corrected
 * @return returns the covariance for the two arrays
 * @throws  IllegalArgumentException if the arrays lengths do not match or
 * there is insufficient data
 */
public double covariance(final double[] xArray, final double[] yArray, boolean biasCorrected)
    throws IllegalArgumentException {
    Mean mean = new Mean();
    double result = 0d;
    int length = xArray.length;
    if(length == yArray.length && length > 1) {
        double xMean = mean.evaluate(xArray);
        double yMean = mean.evaluate(yArray);
        for (int i = 0; i < length; i++) {
            double xDev = xArray[i] - xMean;
            double yDev = yArray[i] - yMean;
            result += (xDev * yDev - result) / (i + 1);
        }
    }
    else {
        throw MathRuntimeException.createIllegalArgumentException(
           "arrays must have the same length and both must have at " +
           "least two elements. xArray has size {0}, yArray has {1} elements",
                length, yArray.length);
    }
    return biasCorrected ? result * ((double) length / (double)(length - 1)) : result;
}
 
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: SummaryStatistics.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Add a value to the data
 * @param value the value to add
 */
public void addValue(double value) {
    sumImpl.increment(value);
    sumsqImpl.increment(value);
    minImpl.increment(value);
    maxImpl.increment(value);
    sumLogImpl.increment(value);
    secondMoment.increment(value);
    // If mean, variance or geomean have been overridden,
    // need to increment these
    if (!(meanImpl instanceof Mean)) {
        meanImpl.increment(value);
    }
    if (!(varianceImpl instanceof Variance)) {
        varianceImpl.increment(value);
    }
    if (!(geoMeanImpl instanceof GeometricMean)) {
        geoMeanImpl.increment(value);
    }
    n++;
}
 
Example #21
Source File: SummaryStatisticsTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testSetterInjection() throws Exception {
    SummaryStatistics u = createSummaryStatistics();
    u.setMeanImpl(new Sum());
    u.setSumLogImpl(new Sum());
    u.addValue(1);
    u.addValue(3);
    assertEquals(4, u.getMean(), 1E-14);
    assertEquals(4, u.getSumOfLogs(), 1E-14);
    assertEquals(Math.exp(2), u.getGeometricMean(), 1E-14);
    u.clear();
    u.addValue(1);
    u.addValue(2);
    assertEquals(3, u.getMean(), 1E-14);
    u.clear();
    u.setMeanImpl(new Mean()); // OK after clear
}
 
Example #22
Source File: SummaryStatisticsTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testSetterInjection() throws Exception {
    SummaryStatistics u = createSummaryStatistics();
    u.setMeanImpl(new Sum());
    u.setSumLogImpl(new Sum());
    u.addValue(1);
    u.addValue(3);
    assertEquals(4, u.getMean(), 1E-14);
    assertEquals(4, u.getSumOfLogs(), 1E-14);
    assertEquals(Math.exp(2), u.getGeometricMean(), 1E-14);
    u.clear();
    u.addValue(1);
    u.addValue(2);
    assertEquals(3, u.getMean(), 1E-14);
    u.clear();
    u.setMeanImpl(new Mean()); // OK after clear
}
 
Example #23
Source File: SummaryStatisticsTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testSetterInjection() throws Exception {
    SummaryStatistics u = createSummaryStatistics();
    u.setMeanImpl(new Sum());
    u.setSumLogImpl(new Sum());
    u.addValue(1);
    u.addValue(3);
    assertEquals(4, u.getMean(), 1E-14);
    assertEquals(4, u.getSumOfLogs(), 1E-14);
    assertEquals(Math.exp(2), u.getGeometricMean(), 1E-14);
    u.clear();
    u.addValue(1);
    u.addValue(2);
    assertEquals(3, u.getMean(), 1E-14);
    u.clear();
    u.setMeanImpl(new Mean()); // OK after clear
}
 
Example #24
Source File: InteractionTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testInteraction() {

        FourthMoment m4 = new FourthMoment();
        Mean m = new Mean(m4);
        Variance v = new Variance(m4);
        Skewness s= new Skewness(m4);
        Kurtosis k = new Kurtosis(m4);

        for (int i = 0; i < testArray.length; i++){
            m4.increment(testArray[i]);
        }

        assertEquals(mean,m.getResult(),tolerance);
        assertEquals(var,v.getResult(),tolerance);
        assertEquals(skew ,s.getResult(),tolerance);
        assertEquals(kurt,k.getResult(),tolerance);

    }
 
Example #25
Source File: SummaryStatistics.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Add a value to the data
 * @param value the value to add
 */
public void addValue(double value) {
    sumImpl.increment(value);
    sumsqImpl.increment(value);
    minImpl.increment(value);
    maxImpl.increment(value);
    sumLogImpl.increment(value);
    secondMoment.increment(value);
    // If mean, variance or geomean have been overridden,
    // need to increment these
    if (!(meanImpl instanceof Mean)) {
        meanImpl.increment(value);
    }
    if (!(varianceImpl instanceof Variance)) {
        varianceImpl.increment(value);
    }
    if (!(geoMeanImpl instanceof GeometricMean)) {
        geoMeanImpl.increment(value);
    }
    n++;
}
 
Example #26
Source File: MultivariateSummaryStatisticsTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testSetterInjection() throws Exception {
    MultivariateSummaryStatistics u = createMultivariateSummaryStatistics(2, true);
    u.setMeanImpl(new StorelessUnivariateStatistic[] {
                    new sumMean(), new sumMean()
                  });
    u.addValue(new double[] { 1, 2 });
    u.addValue(new double[] { 3, 4 });
    Assert.assertEquals(4, u.getMean()[0], 1E-14);
    Assert.assertEquals(6, u.getMean()[1], 1E-14);
    u.clear();
    u.addValue(new double[] { 1, 2 });
    u.addValue(new double[] { 3, 4 });
    Assert.assertEquals(4, u.getMean()[0], 1E-14);
    Assert.assertEquals(6, u.getMean()[1], 1E-14);
    u.clear();
    u.setMeanImpl(new StorelessUnivariateStatistic[] {
                    new Mean(), new Mean()
                  }); // OK after clear
    u.addValue(new double[] { 1, 2 });
    u.addValue(new double[] { 3, 4 });
    Assert.assertEquals(2, u.getMean()[0], 1E-14);
    Assert.assertEquals(3, u.getMean()[1], 1E-14);
    Assert.assertEquals(2, u.getDimension());
}
 
Example #27
Source File: Covariance.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Computes the covariance between the two arrays.
 * 
 * <p>Array lengths must match and the common length must be at least 2.</p>
 *
 * @param xArray first data array
 * @param yArray second data array
 * @param biasCorrected if true, returned value will be bias-corrected 
 * @return returns the covariance for the two arrays 
 * @throws  IllegalArgumentException if the arrays lengths do not match or
 * there is insufficient data
 */
public double covariance(final double[] xArray, final double[] yArray, boolean biasCorrected) 
    throws IllegalArgumentException {
    Mean mean = new Mean();
    double result = 0d;
    int length = xArray.length;
    if(length == yArray.length && length > 1) {
        double xMean = mean.evaluate(xArray);
        double yMean = mean.evaluate(yArray);
        for (int i = 0; i < length; i++) {
            double xDev = xArray[i] - xMean;
            double yDev = yArray[i] - yMean;
            result += (xDev * yDev - result) / (i + 1);
        }
    }
    else {
        throw MathRuntimeException.createIllegalArgumentException(
           "arrays must have the same length and both must have at " +
           "least two elements. xArray has size {0}, yArray has {1} elements",
                length, yArray.length);
    }
    return biasCorrected ? result * ((double) length / (double)(length - 1)) : result;
}
 
Example #28
Source File: Covariance.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Computes the covariance between the two arrays.
 *
 * <p>Array lengths must match and the common length must be at least 2.</p>
 *
 * @param xArray first data array
 * @param yArray second data array
 * @param biasCorrected if true, returned value will be bias-corrected
 * @return returns the covariance for the two arrays
 * @throws  IllegalArgumentException if the arrays lengths do not match or
 * there is insufficient data
 */
public double covariance(final double[] xArray, final double[] yArray, boolean biasCorrected)
    throws IllegalArgumentException {
    Mean mean = new Mean();
    double result = 0d;
    int length = xArray.length;
    if (length != yArray.length) {
        throw MathRuntimeException.createIllegalArgumentException(
              LocalizedFormats.DIMENSIONS_MISMATCH_SIMPLE, length, yArray.length);
    } else if (length < 2) {
        throw MathRuntimeException.createIllegalArgumentException(
              LocalizedFormats.INSUFFICIENT_DIMENSION, length, 2);
    } else {
        double xMean = mean.evaluate(xArray);
        double yMean = mean.evaluate(yArray);
        for (int i = 0; i < length; i++) {
            double xDev = xArray[i] - xMean;
            double yDev = yArray[i] - yMean;
            result += (xDev * yDev - result) / (i + 1);
        }
    }
    return biasCorrected ? result * ((double) length / (double)(length - 1)) : result;
}
 
Example #29
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 #30
Source File: AvroMixedMapReduce.java    From hiped2 with Apache License 2.0 6 votes vote down vote up
public void reduce(Text key,
                   Iterator<DoubleWritable> values,
                   OutputCollector<AvroWrapper<StockAvg>,
                       NullWritable> output,
                   Reporter reporter) throws IOException {

  Mean mean = new Mean();
  while (values.hasNext()) {
    mean.increment(values.next().get());
  }
  StockAvg avg = new StockAvg();
  avg.setSymbol(key.toString());
  avg.setAvg(mean.getResult());
  output.collect(new AvroWrapper<StockAvg>(avg),
      NullWritable.get());
}