Java Code Examples for org.apache.commons.math.stat.StatUtils

The following examples show how to use org.apache.commons.math.stat.StatUtils. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: astor   Source File: SemiVarianceTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testSample() {
    final double[] values = { -2.0d, 2.0d, 4.0d, -2.0d, 22.0d, 11.0d, 3.0d, 14.0d, 5.0d };
    final int length = values.length;
    final double mean = StatUtils.mean(values); // 6.333...
    final SemiVariance sv = new SemiVariance();  // Default bias correction is true
    final double downsideSemiVariance = sv.evaluate(values); // Downside is the default
    Assert.assertEquals(TestUtils.sumSquareDev(new double[] {-2d, 2d, 4d, -2d, 3d, 5d}, mean) / (length - 1),
            downsideSemiVariance, 1E-14);

    sv.setVarianceDirection(SemiVariance.UPSIDE_VARIANCE);
    final double upsideSemiVariance = sv.evaluate(values);
    Assert.assertEquals(TestUtils.sumSquareDev(new double[] {22d, 11d, 14d}, mean) / (length - 1),
            upsideSemiVariance, 1E-14);

    // Verify that upper + lower semivariance against the mean sum to variance
    Assert.assertEquals(StatUtils.variance(values), downsideSemiVariance + upsideSemiVariance, 10e-12);
}
 
Example 2
public void testMinMax() {
    da.addElement(2.0);
    da.addElement(22.0);
    da.addElement(-2.0);
    da.addElement(21.0);
    da.addElement(22.0);
    da.addElement(42.0);
    da.addElement(62.0);
    da.addElement(22.0);
    da.addElement(122.0);
    da.addElement(1212.0);

    assertEquals("Min should be -2.0", -2.0, StatUtils.min(da.getElements()), Double.MIN_VALUE);
    assertEquals(
        "Max should be 1212.0",
        1212.0,
        StatUtils.max(da.getElements()),
        Double.MIN_VALUE);
}
 
Example 3
Source Project: Juicebox   Source File: RealMatrixWrapper.java    License: MIT License 6 votes vote down vote up
private void computePercentiles() {

        // Statistics, other attributes
        DoubleArrayList flattenedDataList = new DoubleArrayList(matrix.getColumnDimension() * matrix.getRowDimension());
        double min = 1;
        double max = -1;
        for (int i = 0; i < matrix.getRowDimension(); i++) {
            for (int j = 0; j < matrix.getColumnDimension(); j++) {
                double value = matrix.getEntry(i, j);
                if (!Double.isNaN(value) && value != 1) {
                    min = value < min ? value : min;
                    max = value > max ? value : max;
                    flattenedDataList.add(value);
                }
            }
        }

        // Stats
        double[] flattenedData = flattenedDataList.toArray();
        lowerValue = (float) StatUtils.percentile(flattenedData, 5);
        upperValue = (float) StatUtils.percentile(flattenedData, 95);
        System.out.println(lowerValue + "  " + upperValue);

    }
 
Example 4
Source Project: astor   Source File: SemiVarianceTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testSample() {
    final double[] values = { -2.0d, 2.0d, 4.0d, -2.0d, 22.0d, 11.0d, 3.0d, 14.0d, 5.0d };
    final int length = values.length;
    final double mean = StatUtils.mean(values); // 6.333...
    final SemiVariance sv = new SemiVariance();  // Default bias correction is true
    final double downsideSemiVariance = sv.evaluate(values); // Downside is the default
    Assert.assertEquals(TestUtils.sumSquareDev(new double[] {-2d, 2d, 4d, -2d, 3d, 5d}, mean) / (length - 1),
            downsideSemiVariance, 1E-14);

    sv.setVarianceDirection(SemiVariance.UPSIDE_VARIANCE);
    final double upsideSemiVariance = sv.evaluate(values);
    Assert.assertEquals(TestUtils.sumSquareDev(new double[] {22d, 11d, 14d}, mean) / (length - 1),
            upsideSemiVariance, 1E-14);

    // Verify that upper + lower semivariance against the mean sum to variance
    Assert.assertEquals(StatUtils.variance(values), downsideSemiVariance + upsideSemiVariance, 10e-12);
}
 
Example 5
@Test
public void testMinMax() {
    da.addElement(2.0);
    da.addElement(22.0);
    da.addElement(-2.0);
    da.addElement(21.0);
    da.addElement(22.0);
    da.addElement(42.0);
    da.addElement(62.0);
    da.addElement(22.0);
    da.addElement(122.0);
    da.addElement(1212.0);

    Assert.assertEquals("Min should be -2.0", -2.0, StatUtils.min(da.getElements()), Double.MIN_VALUE);
    Assert.assertEquals(
        "Max should be 1212.0",
        1212.0,
        StatUtils.max(da.getElements()),
        Double.MIN_VALUE);
}
 
Example 6
public void testMinMax() {
    da.addElement(2.0);
    da.addElement(22.0);
    da.addElement(-2.0);
    da.addElement(21.0);
    da.addElement(22.0);
    da.addElement(42.0);
    da.addElement(62.0);
    da.addElement(22.0);
    da.addElement(122.0);
    da.addElement(1212.0);

    assertEquals("Min should be -2.0", -2.0, StatUtils.min(da.getElements()), Double.MIN_VALUE);
    assertEquals(
        "Max should be 1212.0",
        1212.0,
        StatUtils.max(da.getElements()),
        Double.MIN_VALUE);
}
 
Example 7
public void testMinMax() {
    da.addElement(2.0);
    da.addElement(22.0);
    da.addElement(-2.0);
    da.addElement(21.0);
    da.addElement(22.0);
    da.addElement(42.0);
    da.addElement(62.0);
    da.addElement(22.0);
    da.addElement(122.0);
    da.addElement(1212.0);

    assertEquals("Min should be -2.0", -2.0, StatUtils.min(da.getElements()), Double.MIN_VALUE);
    assertEquals(
        "Max should be 1212.0",
        1212.0,
        StatUtils.max(da.getElements()),
        Double.MIN_VALUE);
}
 
Example 8
Source Project: astor   Source File: SemiVarianceTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
public void testSample() {
    final double[] values = { -2.0d, 2.0d, 4.0d, -2.0d, 22.0d, 11.0d, 3.0d, 14.0d, 5.0d };
    final int length = values.length;
    final double mean = StatUtils.mean(values); // 6.333...
    final SemiVariance sv = new SemiVariance();  // Default bias correction is true
    final double downsideSemiVariance = sv.evaluate(values); // Downside is the default
    assertEquals(TestUtils.sumSquareDev(new double[] {-2d, 2d, 4d, -2d, 3d, 5d}, mean) / (length - 1),
            downsideSemiVariance, 1E-14);

    sv.setVarianceDirection(SemiVariance.UPSIDE_VARIANCE);
    final double upsideSemiVariance = sv.evaluate(values);
    assertEquals(TestUtils.sumSquareDev(new double[] {22d, 11d, 14d}, mean) / (length - 1),
            upsideSemiVariance, 1E-14);

    // Verify that upper + lower semivariance against the mean sum to variance
    assertEquals(StatUtils.variance(values), downsideSemiVariance + upsideSemiVariance, 10e-12);
}
 
Example 9
public void testMinMax() {
    da.addElement(2.0);
    da.addElement(22.0);
    da.addElement(-2.0);
    da.addElement(21.0);
    da.addElement(22.0);
    da.addElement(42.0);
    da.addElement(62.0);
    da.addElement(22.0);
    da.addElement(122.0);
    da.addElement(1212.0);

    assertEquals("Min should be -2.0", -2.0, StatUtils.min(da.getElements()), Double.MIN_VALUE);
    assertEquals(
        "Max should be 1212.0",
        1212.0,
        StatUtils.max(da.getElements()),
        Double.MIN_VALUE);
}
 
Example 10
Source Project: astor   Source File: SemiVarianceTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
public void testSample() {
    final double[] values = { -2.0d, 2.0d, 4.0d, -2.0d, 22.0d, 11.0d, 3.0d, 14.0d, 5.0d };
    final int length = values.length;
    final double mean = StatUtils.mean(values); // 6.333...
    final SemiVariance sv = new SemiVariance();  // Default bias correction is true
    final double downsideSemiVariance = sv.evaluate(values); // Downside is the default
    assertEquals(TestUtils.sumSquareDev(new double[] {-2d, 2d, 4d, -2d, 3d, 5d}, mean) / (length - 1),
            downsideSemiVariance, 1E-14);

    sv.setVarianceDirection(SemiVariance.UPSIDE_VARIANCE);
    final double upsideSemiVariance = sv.evaluate(values);
    assertEquals(TestUtils.sumSquareDev(new double[] {22d, 11d, 14d}, mean) / (length - 1),
            upsideSemiVariance, 1E-14);

    // Verify that upper + lower semivariance against the mean sum to variance
    assertEquals(StatUtils.variance(values), downsideSemiVariance + upsideSemiVariance, 10e-12);
}
 
Example 11
public void testMinMax() {
    da.addElement(2.0);
    da.addElement(22.0);
    da.addElement(-2.0);
    da.addElement(21.0);
    da.addElement(22.0);
    da.addElement(42.0);
    da.addElement(62.0);
    da.addElement(22.0);
    da.addElement(122.0);
    da.addElement(1212.0);

    assertEquals("Min should be -2.0", -2.0, StatUtils.min(da.getElements()), Double.MIN_VALUE);
    assertEquals(
        "Max should be 1212.0",
        1212.0,
        StatUtils.max(da.getElements()),
        Double.MIN_VALUE);
}
 
Example 12
Source Project: astor   Source File: SemiVarianceTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
public void testSample() {
    final double[] values = { -2.0d, 2.0d, 4.0d, -2.0d, 22.0d, 11.0d, 3.0d, 14.0d, 5.0d };
    final int length = values.length;
    final double mean = StatUtils.mean(values); // 6.333...
    final SemiVariance sv = new SemiVariance();  // Default bias correction is true
    final double downsideSemiVariance = sv.evaluate(values); // Downside is the default
    assertEquals(TestUtils.sumSquareDev(new double[] {-2d, 2d, 4d, -2d, 3d, 5d}, mean) / (length - 1),
            downsideSemiVariance, 1E-14);

    sv.setVarianceDirection(SemiVariance.UPSIDE_VARIANCE);
    final double upsideSemiVariance = sv.evaluate(values);
    assertEquals(TestUtils.sumSquareDev(new double[] {22d, 11d, 14d}, mean) / (length - 1),
            upsideSemiVariance, 1E-14);

    // Verify that upper + lower semivariance against the mean sum to variance
    assertEquals(StatUtils.variance(values), downsideSemiVariance + upsideSemiVariance, 10e-12);
}
 
Example 13
Source Project: astor   Source File: SemiVarianceTest.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Check that the lower + upper semivariance against the mean sum to the
 * variance.
 */
public void testVarianceDecompMeanCutoff() {
    double[] values = { -2.0d, 2.0d, 4.0d, -2.0d, 22.0d, 11.0d, 3.0d, 14.0d, 5.0d };
    double variance = StatUtils.variance(values);
    SemiVariance sv = new SemiVariance(true); // Bias corrected
    sv.setVarianceDirection(SemiVariance.DOWNSIDE_VARIANCE);
    final double lower = sv.evaluate(values);
    sv.setVarianceDirection(SemiVariance.UPSIDE_VARIANCE);
    final double upper = sv.evaluate(values);
    assertEquals(variance, lower + upper, 10e-12);
}
 
Example 14
/** test stats */
public void testSerialization() {
    
    double[] values = {35d, 23d, 42d};
    
    DescriptiveStatistics u = new BeanListUnivariateImpl( patientList, "age" ); 
    assertEquals("total count",3,u.getN(),tolerance);
    assertEquals("mean", StatUtils.mean(values), u.getMean(), tolerance);
    assertEquals("min", StatUtils.min(values), u.getMin(), tolerance);
    assertEquals("max", StatUtils.max(values), u.getMax(), tolerance);
    assertEquals("var", StatUtils.variance(values), u.getVariance(), tolerance);   
    
    
    DescriptiveStatistics u2 = (DescriptiveStatistics)TestUtils.serializeAndRecover(u); 
    assertEquals("total count",3,u2.getN(),tolerance);
    assertEquals("mean", StatUtils.mean(values), u2.getMean(), tolerance);
    assertEquals("min", StatUtils.min(values), u2.getMin(), tolerance);
    assertEquals("max", StatUtils.max(values), u2.getMax(), tolerance);
    assertEquals("var", StatUtils.variance(values), u2.getVariance(), tolerance);   

    u.clear();
    assertEquals("total count",0,u.getN(),tolerance);    
    
    u2.clear();
    assertEquals("total count",0,u2.getN(),tolerance);
        
}
 
Example 15
public void testMeanAndStandardDeviation() {
    RandomGenerator rg = new JDKRandomGenerator();
    rg.setSeed(17399225432l);
    GaussianRandomGenerator generator = new GaussianRandomGenerator(rg);
    double[] sample = new double[10000];
    for (int i = 0; i < sample.length; ++i) {
        sample[i] = generator.nextNormalizedDouble();
    }
    assertEquals(0.0, StatUtils.mean(sample), 0.012);
    assertEquals(1.0, StatUtils.variance(sample), 0.01);
}
 
Example 16
public void testMeanAndStandardDeviation() {
    RandomGenerator rg = new JDKRandomGenerator();
    rg.setSeed(17399225432l);
    UniformRandomGenerator generator = new UniformRandomGenerator(rg);
    double[] sample = new double[10000];
    for (int i = 0; i < sample.length; ++i) {
        sample[i] = generator.nextNormalizedDouble();
    }
    assertEquals(0.0, StatUtils.mean(sample), 0.07);
    assertEquals(1.0, StatUtils.variance(sample), 0.02);
}
 
Example 17
public void testMeanAndStandardDeviation() {
    RandomGenerator rg = new JDKRandomGenerator();
    rg.setSeed(17399225432l);
    GaussianRandomGenerator generator = new GaussianRandomGenerator(rg);
    double[] sample = new double[10000];
    for (int i = 0; i < sample.length; ++i) {
        sample[i] = generator.nextNormalizedDouble();
    }
    assertEquals(0.0, StatUtils.mean(sample), 0.012);
    assertEquals(1.0, StatUtils.variance(sample), 0.01);
}
 
Example 18
/** test stats */
public void testSerialization() {
    
    double[] values = {35d, 23d, 42d};
    
    DescriptiveStatistics u = new BeanListUnivariateImpl( patientList, "age" ); 
    assertEquals("total count",3,u.getN(),tolerance);
    assertEquals("mean", StatUtils.mean(values), u.getMean(), tolerance);
    assertEquals("min", StatUtils.min(values), u.getMin(), tolerance);
    assertEquals("max", StatUtils.max(values), u.getMax(), tolerance);
    assertEquals("var", StatUtils.variance(values), u.getVariance(), tolerance);   
    
    
    DescriptiveStatistics u2 = (DescriptiveStatistics)TestUtils.serializeAndRecover(u); 
    assertEquals("total count",3,u2.getN(),tolerance);
    assertEquals("mean", StatUtils.mean(values), u2.getMean(), tolerance);
    assertEquals("min", StatUtils.min(values), u2.getMin(), tolerance);
    assertEquals("max", StatUtils.max(values), u2.getMax(), tolerance);
    assertEquals("var", StatUtils.variance(values), u2.getVariance(), tolerance);   

    u.clear();
    assertEquals("total count",0,u.getN(),tolerance);    
    
    u2.clear();
    assertEquals("total count",0,u2.getN(),tolerance);
        
}
 
Example 19
public void testMeanAndStandardDeviation() {
    RandomGenerator rg = new JDKRandomGenerator();
    rg.setSeed(17399225432l);
    GaussianRandomGenerator generator = new GaussianRandomGenerator(rg);
    double[] sample = new double[10000];
    for (int i = 0; i < sample.length; ++i) {
        sample[i] = generator.nextNormalizedDouble();
    }
    assertEquals(0.0, StatUtils.mean(sample), 0.012);
    assertEquals(1.0, StatUtils.variance(sample), 0.01);
}
 
Example 20
/** test stats */
public void testStats() {	
    DescriptiveStatistics u = new BeanListUnivariateImpl( patientList, "age" ); 
    double[] values = {35d, 23d, 42d};
    assertEquals("total count",3,u.getN(),tolerance);
    assertEquals("mean", StatUtils.mean(values), u.getMean(), tolerance);
    assertEquals("min", StatUtils.min(values), u.getMin(), tolerance);
    assertEquals("max", StatUtils.max(values), u.getMax(), tolerance);
    assertEquals("var", StatUtils.variance(values), u.getVariance(), tolerance);       
    u.clear();
    assertEquals("total count",0,u.getN(),tolerance);    
}
 
Example 21
Source Project: Juicebox   Source File: InMemoryMatrix.java    License: MIT License 5 votes vote down vote up
private void computeBounds() {
    DoubleArrayList tmpList = new DoubleArrayList(data.length);
    for (float datum : data) {
        if (!Float.isNaN(datum)) tmpList.add(datum);
    }
    double[] tmp = tmpList.toArray();
    lowerValue = (float) StatUtils.percentile(tmp, 5);
    upperValue = (float) StatUtils.percentile(tmp, 95);
}
 
Example 22
public void testAddElementRolling() {
    ra.addElement(0.5);
    ra.addElement(1.0);
    ra.addElement(1.0);
    ra.addElement(1.0);
    ra.addElement(1.0);
    ra.addElement(1.0);
    ra.addElementRolling(2.0);

    assertEquals(
        "There should be 6 elements in the eda",
        6,
        ra.getNumElements());
    assertEquals(
        "The max element should be 2.0",
        2.0,
        StatUtils.max(ra.getElements()),
        Double.MIN_VALUE);
    assertEquals(
        "The min element should be 1.0",
        1.0,
        StatUtils.min(ra.getElements()),
        Double.MIN_VALUE);

    for (int i = 0; i < 1024; i++) {
        ra.addElementRolling(i);
    }

    assertEquals(
        "We just inserted 1024 rolling elements, num elements should still be 6",
        6,
        ra.getNumElements());
}
 
Example 23
public void testAddElementRolling() {
    ra.addElement(0.5);
    ra.addElement(1.0);
    ra.addElement(1.0);
    ra.addElement(1.0);
    ra.addElement(1.0);
    ra.addElement(1.0);
    ra.addElementRolling(2.0);

    assertEquals(
        "There should be 6 elements in the eda",
        6,
        ra.getNumElements());
    assertEquals(
        "The max element should be 2.0",
        2.0,
        StatUtils.max(ra.getElements()),
        Double.MIN_VALUE);
    assertEquals(
        "The min element should be 1.0",
        1.0,
        StatUtils.min(ra.getElements()),
        Double.MIN_VALUE);

    for (int i = 0; i < 1024; i++) {
        ra.addElementRolling(i);
    }

    assertEquals(
        "We just inserted 1024 rolling elements, num elements should still be 6",
        6,
        ra.getNumElements());
}
 
Example 24
Source Project: astor   Source File: SemiVarianceTest.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Check that the lower + upper semivariance against the mean sum to the
 * variance.
 */
@Test
public void testVarianceDecompMeanCutoff() {
    double[] values = { -2.0d, 2.0d, 4.0d, -2.0d, 22.0d, 11.0d, 3.0d, 14.0d, 5.0d };
    double variance = StatUtils.variance(values);
    SemiVariance sv = new SemiVariance(true); // Bias corrected
    sv.setVarianceDirection(SemiVariance.DOWNSIDE_VARIANCE);
    final double lower = sv.evaluate(values);
    sv.setVarianceDirection(SemiVariance.UPSIDE_VARIANCE);
    final double upper = sv.evaluate(values);
    Assert.assertEquals(variance, lower + upper, 10e-12);
}
 
Example 25
/** test stats */
public void testSerialization() {
    
    double[] values = {35d, 23d, 42d};
    
    DescriptiveStatistics u = new BeanListUnivariateImpl( patientList, "age" ); 
    assertEquals("total count",3,u.getN(),tolerance);
    assertEquals("mean", StatUtils.mean(values), u.getMean(), tolerance);
    assertEquals("min", StatUtils.min(values), u.getMin(), tolerance);
    assertEquals("max", StatUtils.max(values), u.getMax(), tolerance);
    assertEquals("var", StatUtils.variance(values), u.getVariance(), tolerance);   
    
    
    DescriptiveStatistics u2 = (DescriptiveStatistics)TestUtils.serializeAndRecover(u); 
    assertEquals("total count",3,u2.getN(),tolerance);
    assertEquals("mean", StatUtils.mean(values), u2.getMean(), tolerance);
    assertEquals("min", StatUtils.min(values), u2.getMin(), tolerance);
    assertEquals("max", StatUtils.max(values), u2.getMax(), tolerance);
    assertEquals("var", StatUtils.variance(values), u2.getVariance(), tolerance);   

    u.clear();
    assertEquals("total count",0,u.getN(),tolerance);    
    
    u2.clear();
    assertEquals("total count",0,u2.getN(),tolerance);
        
}
 
Example 26
/**
 * Verifies that calculateYVariance and calculateResidualVariance return consistent
 * values with direct variance computation from Y, residuals, respectively.
 */
protected void checkVarianceConsistency(OLSMultipleLinearRegression model) throws Exception {
    // Check Y variance consistency
    TestUtils.assertEquals(StatUtils.variance(model.Y.getData()), model.calculateYVariance(), 0);
    
    // Check residual variance consistency
    double[] residuals = model.calculateResiduals().getData();
    RealMatrix X = model.X;
    TestUtils.assertEquals(
            StatUtils.variance(model.calculateResiduals().getData()) * (residuals.length - 1),
            model.calculateErrorVariance() * (X.getRowDimension() - X.getColumnDimension()), 1E-20);
    
}
 
Example 27
Source Project: lesk-wsd-dsm   Source File: RevisedLesk.java    License: GNU General Public License v3.0 5 votes vote down vote up
/**
 *
 * @param list
 * @return
 */
public double getMean(List<SynsetOut> list) {
    double[] scores = new double[list.size()];
    int l = 0;
    for (SynsetOut out : list) {
        scores[l] = out.getScore();
        l++;
    }
    return StatUtils.mean(scores);
}
 
Example 28
public void testMeanAndStandardDeviation() {
    RandomGenerator rg = new JDKRandomGenerator();
    rg.setSeed(17399225432l);
    GaussianRandomGenerator generator = new GaussianRandomGenerator(rg);
    double[] sample = new double[10000];
    for (int i = 0; i < sample.length; ++i) {
        sample[i] = generator.nextNormalizedDouble();
    }
    assertEquals(0.0, StatUtils.mean(sample), 0.012);
    assertEquals(1.0, StatUtils.variance(sample), 0.01);
}
 
Example 29
Source Project: astor   Source File: SemiVarianceTest.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Check that the lower + upper semivariance against the mean sum to the
 * variance.
 */
public void testVarianceDecompMeanCutoff() {
    double[] values = { -2.0d, 2.0d, 4.0d, -2.0d, 22.0d, 11.0d, 3.0d, 14.0d, 5.0d };
    double variance = StatUtils.variance(values);
    SemiVariance sv = new SemiVariance(true); // Bias corrected
    sv.setVarianceDirection(SemiVariance.DOWNSIDE_VARIANCE);
    final double lower = sv.evaluate(values);
    sv.setVarianceDirection(SemiVariance.UPSIDE_VARIANCE);
    final double upper = sv.evaluate(values);
    assertEquals(variance, lower + upper, 10e-12);
}
 
Example 30
public void testMeanAndStandardDeviation() {
    RandomGenerator rg = new JDKRandomGenerator();
    rg.setSeed(17399225432l);
    UniformRandomGenerator generator = new UniformRandomGenerator(rg);
    double[] sample = new double[10000];
    for (int i = 0; i < sample.length; ++i) {
        sample[i] = generator.nextNormalizedDouble();
    }
    assertEquals(0.0, StatUtils.mean(sample), 0.07);
    assertEquals(1.0, StatUtils.variance(sample), 0.02);
}