Java Code Examples for org.apache.commons.math.stat.descriptive.SummaryStatistics#newInstance()

The following examples show how to use org.apache.commons.math.stat.descriptive.SummaryStatistics#newInstance() . 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: CertifiedDataTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Test StorelessDescriptiveStatistics
*/
public void testUnivariateImpl() throws Exception {
    SummaryStatistics u = SummaryStatistics.newInstance(SummaryStatisticsImpl.class);
    loadStats("data/PiDigits.txt", u);
    assertEquals("PiDigits: std", std, u.getStandardDeviation(), .0000000000001);
    assertEquals("PiDigits: mean", mean, u.getMean(), .0000000000001);  

    loadStats("data/Mavro.txt", u);
    assertEquals("Mavro: std", std, u.getStandardDeviation(), .00000000000001);
    assertEquals("Mavro: mean", mean, u.getMean(), .00000000000001);
    
    //loadStats("data/Michelso.txt");
    //assertEquals("Michelso: std", std, u.getStandardDeviation(), .00000000000001);
    //assertEquals("Michelso: mean", mean, u.getMean(), .00000000000001);   
                                    
    loadStats("data/NumAcc1.txt", u);
    assertEquals("NumAcc1: std", std, u.getStandardDeviation(), .00000000000001);
    assertEquals("NumAcc1: mean", mean, u.getMean(), .00000000000001);
    
    //loadStats("data/NumAcc2.txt");
    //assertEquals("NumAcc2: std", std, u.getStandardDeviation(), .000000001);
    //assertEquals("NumAcc2: mean", mean, u.getMean(), .00000000000001);
}
 
Example 2
Source File: RandomDataTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** test failure modes and distribution of nextGaussian() */  
public void testNextGaussian() { 
    try {
        double x = randomData.nextGaussian(0,0);
        fail("zero sigma -- IllegalArgumentException expected");
    } catch (IllegalArgumentException ex) {
        ;
    }
    SummaryStatistics u = SummaryStatistics.newInstance();
    for (int i = 0; i<largeSampleSize; i++) {
        u.addValue(randomData.nextGaussian(0,1));
    }
    double xbar = u.getMean();
    double s = u.getStandardDeviation();
    double n = (double) u.getN(); 
    /* t-test at .001-level TODO: replace with externalized t-test, with
     * test statistic defined in TestStatistic
     */
    assertTrue(Math.abs(xbar)/(s/Math.sqrt(n))< 3.29);
}
 
Example 3
Source File: EmpiricalDistributionImpl.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Computes sampleStats
 * 
 * @throws IOException if an IOError occurs
 */
public void computeStats() throws IOException {
    String str = null;
    double val = 0.0;
    sampleStats = SummaryStatistics.newInstance();
    while ((str = inputStream.readLine()) != null) {
        val = new Double(str).doubleValue();
        sampleStats.addValue(val);
    }
    inputStream.close();
    inputStream = null;
}
 
Example 4
Source File: EmpiricalDistributionImpl.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Computes sampleStats
 * 
 * @throws IOException if an IO error occurs
 */
public void computeStats() throws IOException {
    sampleStats = SummaryStatistics.newInstance();
    for (int i = 0; i < inputArray.length; i++) {
        sampleStats.addValue(inputArray[i]);
    }
}
 
Example 5
Source File: CertifiedDataAbstractTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
protected void setUp() throws Exception {
    descriptives = DescriptiveStatistics.newInstance();
    summaries = SummaryStatistics.newInstance();
    certifiedValues = new HashMap();
    
    loadData();
}
 
Example 6
Source File: ValueServerTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** 
  * Generate 1000 random values and make sure they look OK.<br>
  * Note that there is a non-zero (but very small) probability that
  * these tests will fail even if the code is working as designed.
  */
public void testNextDigest() throws Exception{
    double next = 0.0;
    double tolerance = 0.1;
    vs.computeDistribution();
    assertTrue("empirical distribution property", 
        vs.getEmpiricalDistribution() != null);
    SummaryStatistics stats = SummaryStatistics.newInstance();
    for (int i = 1; i < 1000; i++) {
        next = vs.getNext();
        stats.addValue(next);
    }    
    assertEquals("mean", 5.069831575018909, stats.getMean(), tolerance);
    assertEquals
     ("std dev", 1.0173699343977738, stats.getStandardDeviation(), 
        tolerance);
    
    vs.computeDistribution(500);
    stats = SummaryStatistics.newInstance();
    for (int i = 1; i < 1000; i++) {
        next = vs.getNext();
        stats.addValue(next);
    }    
    assertEquals("mean", 5.069831575018909, stats.getMean(), tolerance);
    assertEquals
     ("std dev", 1.0173699343977738, stats.getStandardDeviation(), 
        tolerance);
    
}
 
Example 7
Source File: EmpiricalDistributionTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private void tstGen(double tolerance)throws Exception {
    empiricalDistribution.load(url);   
    SummaryStatistics stats = SummaryStatistics.newInstance();
    for (int i = 1; i < 1000; i++) {
        stats.addValue(empiricalDistribution.getNextValue());
    }
    assertEquals("mean", stats.getMean(),5.069831575018909,tolerance);
    assertEquals
     ("std dev", stats.getStandardDeviation(),1.0173699343977738,tolerance);
}
 
Example 8
Source File: EmpiricalDistributionTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private void tstDoubleGen(double tolerance)throws Exception {
    empiricalDistribution2.load(dataArray);   
    SummaryStatistics stats = SummaryStatistics.newInstance();
    for (int i = 1; i < 1000; i++) {
        stats.addValue(empiricalDistribution2.getNextValue());
    }
    assertEquals("mean", stats.getMean(),5.069831575018909,tolerance);
    assertEquals
     ("std dev", stats.getStandardDeviation(),1.0173699343977738,tolerance);
}
 
Example 9
Source File: EmpiricalDistributionImpl.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Fills binStats array (second pass through data file).
 * 
 * @param in object providing access to the data
 * @throws IOException  if an IO error occurs
 */
private void fillBinStats(Object in) throws IOException {
    // Load array of bin upper bounds -- evenly spaced from min - max
    double min = sampleStats.getMin();
    double max = sampleStats.getMax();
    double delta = (max - min)/(new Double(binCount)).doubleValue();
    double[] binUpperBounds = new double[binCount];
    binUpperBounds[0] = min + delta;
    for (int i = 1; i< binCount - 1; i++) {
        binUpperBounds[i] = binUpperBounds[i-1] + delta;
    }
    binUpperBounds[binCount -1] = max;

    // Initialize binStats ArrayList
    if (!binStats.isEmpty()) {
        binStats.clear();
    }
    for (int i = 0; i < binCount; i++) {
        SummaryStatistics stats = SummaryStatistics.newInstance();
        binStats.add(i,stats);
    }

    // Filling data in binStats Array
    DataAdapterFactory aFactory = new DataAdapterFactory();
    DataAdapter da = aFactory.getAdapter(in);
    try {
        da.computeBinStats(min, delta);
    } catch (Exception e) {
        if(e instanceof RuntimeException){
            throw new RuntimeException(e.getMessage());
        }else{
            throw new IOException(e.getMessage());
        }
    }

    // Assign upperBounds based on bin counts
    upperBounds = new double[binCount];
    upperBounds[0] =
    ((double)((SummaryStatistics)binStats.get(0)).getN())/
    (double)sampleStats.getN();
    for (int i = 1; i < binCount-1; i++) {
        upperBounds[i] = upperBounds[i-1] +
        ((double)((SummaryStatistics)binStats.get(i)).getN())/
        (double)sampleStats.getN();
    }
    upperBounds[binCount-1] = 1.0d;
}
 
Example 10
Source File: TTestTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void setUp() {
    tooShortStats = SummaryStatistics.newInstance();
    tooShortStats.addValue(0d);
}