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

The following examples show how to use org.apache.commons.math3.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: 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 #2
Source File: MeanAndStd.java    From HMMRATAC with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Set the data across a specific region
 * @param node a TagNode representing a specific region for calculation
 * @throws IOException
 */
private void SetMeanAndStd2(TagNode node) throws IOException{
	BBFileReader wigReader = new BBFileReader(wigFile);
	String chrom = node.getChrom();
	int begin = node.getStart();
	int end = node.getStop();
	BigWigIterator iter = wigReader.getBigWigIterator(chrom, begin, chrom, end, false);
	Mean mu = new Mean();
	StandardDeviation dev = new StandardDeviation();
	while(iter.hasNext()){
		WigItem item = iter.next();
		int start = item.getStartBase();
		int stop = item.getEndBase();
		double value = item.getWigValue();
		for (int i = start; i < stop;i++){
			mu.increment(value);
			dev.increment(value);
		}
		
	}
	mean = mu.getResult();
	std = dev.getResult();
	wigReader=null;
}
 
Example #3
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 #4
Source File: MultivariateSummaryStatisticsTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testSetterInjection() {
    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 #5
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 #6
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 new MathIllegalArgumentException(
              LocalizedFormats.DIMENSIONS_MISMATCH_SIMPLE, length, yArray.length);
    } else if (length < 2) {
        throw new MathIllegalArgumentException(
              LocalizedFormats.INSUFFICIENT_OBSERVED_POINTS_IN_SAMPLE, 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 #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: MultivariateSummaryStatisticsTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testSetterInjection() {
    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 #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: 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 new MathIllegalArgumentException(
              LocalizedFormats.DIMENSIONS_MISMATCH_SIMPLE, length, yArray.length);
    } else if (length < 2) {
        throw new MathIllegalArgumentException(
              LocalizedFormats.INSUFFICIENT_OBSERVED_POINTS_IN_SAMPLE, 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 #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: 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  MathIllegalArgumentException if the arrays lengths do not match or
 * there is insufficient data
 */
public double covariance(final double[] xArray, final double[] yArray, boolean biasCorrected)
    throws MathIllegalArgumentException {
    Mean mean = new Mean();
    double result = 0d;
    int length = xArray.length;
    if (length != yArray.length) {
        throw new MathIllegalArgumentException(
              LocalizedFormats.DIMENSIONS_MISMATCH_SIMPLE, length, yArray.length);
    } else if (length < 2) {
        throw new MathIllegalArgumentException(
              LocalizedFormats.INSUFFICIENT_OBSERVED_POINTS_IN_SAMPLE, 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 #13
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 #14
Source File: MultivariateSummaryStatisticsTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testSetterInjection() {
    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 #15
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 #16
Source File: SummarizeUMIBaseQualities.java    From Drop-seq with MIT License 6 votes vote down vote up
/**
 * Summarize the phread scores of all the bases piles in the given pileup.
 * This calculates the mean quality score.
 * For bases that disagree, this adds in the error rate.
 * So, if you had AAT with qualities 30,30,10:
 * Mean of : 0.999,0.999,0.1=0.6993333
 * Thus an error rate of 1-0.6993333=0.3006667, which is a phread base score of ~ 5.
 * We want to penalize bases with disagreements.
 * @return
 */
public int getSummarizedPhreadScoreByMeanWithErrors () {
	byte commonBase = getMostCommonBase();

	Mean mean = new Mean();

	for (int i=0; i<this.bases.size(); i++) {
		byte base = this.bases.get(i);
		byte qual = this.qualities.get(i);
		double prob = LikelihoodUtils.getInstance().phredScoreToErrorProbability(qual);

		if (base==commonBase)
			mean.increment(prob);
		else
			mean.increment(1-prob);
	}
	double meanErrorProbability = mean.getResult();
	int phread = LikelihoodUtils.getInstance().errorProbabilityToPhredScore(meanErrorProbability);
	return phread;
}
 
Example #17
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 new MathIllegalArgumentException(
              LocalizedFormats.DIMENSIONS_MISMATCH_SIMPLE, length, yArray.length);
    } else if (length < 2) {
        throw new MathIllegalArgumentException(
              LocalizedFormats.INSUFFICIENT_OBSERVED_POINTS_IN_SAMPLE, 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 #18
Source File: SliceSamplerUnitTest.java    From gatk with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * 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 #19
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 #20
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 #21
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 #22
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  MathIllegalArgumentException if the arrays lengths do not match or
 * there is insufficient data
 */
public double covariance(final double[] xArray, final double[] yArray, boolean biasCorrected)
    throws MathIllegalArgumentException {
    Mean mean = new Mean();
    double result = 0d;
    int length = xArray.length;
    if (length != yArray.length) {
        throw new MathIllegalArgumentException(
              LocalizedFormats.DIMENSIONS_MISMATCH_SIMPLE, length, yArray.length);
    } else if (length < 2) {
        throw new MathIllegalArgumentException(
              LocalizedFormats.INSUFFICIENT_OBSERVED_POINTS_IN_SAMPLE, 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 #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: MultivariateSummaryStatisticsTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testSetterInjection() {
    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 #25
Source File: MinibatchSliceSamplerUnitTest.java    From gatk with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Tests slice sampling of a uniform prior with no data points.
 * Checks that mean and standard deviation are recovered from the samples
 * by 500 burn-in samples + 1000 samples to a relative error of 1% and 5%, respectively.
 */
@Test
public void testSliceSamplingOfUniformPriorWithNoData() {
    rng.setSeed(RANDOM_SEED);

    final double mean = 0.5;
    final double standardDeviation = 1. / Math.sqrt(12.);
    final List<Double> data = Collections.emptyList();

    final double xInitial = 0.5;
    final double xMin = 0.;
    final double xMax = 1.;
    final double width = 0.1;
    final int numBurnInSamples = 500;
    final int numSamples = 1500;
    final MinibatchSliceSampler<Double> uniformSampler = new MinibatchSliceSampler<>(
            rng, data, UNIFORM_LOG_PRIOR, (d, x) -> 0.,
            xMin, xMax, width, MINIBATCH_SIZE, APPROX_THRESHOLD);
    final double[] samples = Doubles.toArray(uniformSampler.sample(xInitial, numSamples).subList(numBurnInSamples, numSamples));

    final double sampleMean = new Mean().evaluate(samples);
    final double sampleStandardDeviation = new StandardDeviation().evaluate(samples);
    Assert.assertEquals(relativeError(sampleMean, mean), 0., 0.01);
    Assert.assertEquals(relativeError(sampleStandardDeviation, standardDeviation), 0., 0.05);
}
 
Example #26
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 #27
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 #28
Source File: MultivariateSummaryStatisticsTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testSetterInjection() {
    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 #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: 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  MathIllegalArgumentException if the arrays lengths do not match or
 * there is insufficient data
 */
public double covariance(final double[] xArray, final double[] yArray, boolean biasCorrected)
    throws MathIllegalArgumentException {
    Mean mean = new Mean();
    double result = 0d;
    int length = xArray.length;
    if (length != yArray.length) {
        throw new MathIllegalArgumentException(
              LocalizedFormats.DIMENSIONS_MISMATCH_SIMPLE, length, yArray.length);
    } else if (length < 2) {
        throw new MathIllegalArgumentException(
              LocalizedFormats.INSUFFICIENT_OBSERVED_POINTS_IN_SAMPLE, 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;
}