~ubuntu-branches/ubuntu/quantal/commons-math/quantal

« back to all changes in this revision

Viewing changes to src/test/java/org/apache/commons/math/stat/StatUtilsTest.java

  • Committer: Bazaar Package Importer
  • Author(s): Damien Raude-Morvan, Torsten Werner, Damien Raude-Morvan
  • Date: 2011-03-07 21:14:46 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20110307211446-4zea7og4eeyzhpai
Tags: 2.2-1
[ Torsten Werner ]
* Change maintainers into Maintainers.

[ Damien Raude-Morvan ]
* New upstream release (Closes: #617209).
* d/control: Bump Standards-Version to 3.9.1 (no changes needed).
* d/copyright: Refresh years, upgrade to DEP5 r166 and relicence my work
  under Apache-2.0.
* d/ant.properties: Set junit.jar to /usr/share/java/junit4.jar
  to ensure unit tests are launched.
* d/docs: Install upstream RELEASE-NOTES.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
import junit.framework.TestCase;
20
20
 
21
21
import org.apache.commons.math.TestUtils;
 
22
import org.apache.commons.math.stat.descriptive.DescriptiveStatistics;
 
23
import org.apache.commons.math.util.FastMath;
22
24
 
23
25
/**
24
26
 * Test cases for the {@link StatUtils} class.
25
 
 * @version $Revision: 902201 $ $Date: 2010-01-22 13:18:16 -0500 (Fri, 22 Jan 2010) $
 
27
 * @version $Revision: 1060124 $ $Date: 2011-01-18 00:00:52 +0100 (mar. 18 janv. 2011) $
26
28
 */
27
29
 
28
30
public final class StatUtilsTest extends TestCase {
96
98
 
97
99
        try {
98
100
            StatUtils.sum(values, 2, 3);
99
 
            assertTrue("Didn't throw exception", false);
100
 
        } catch (Exception e) {
101
 
            assertTrue(true);
 
101
            fail("Expected RuntimeException");
 
102
        } catch (RuntimeException e) {
 
103
            // expected
102
104
        }
103
105
 
104
106
        try {
105
107
            StatUtils.sum(values, -1, 2);
106
 
            assertTrue("Didn't throw exception", false);
107
 
        } catch (Exception e) {
108
 
            assertTrue(true);
 
108
            fail("Expected RuntimeException");
 
109
        } catch (RuntimeException e) {
 
110
            // expected
109
111
        }
110
112
 
111
113
    }
203
205
 
204
206
        // test one
205
207
        x = new double[] {two};
206
 
        TestUtils.assertEquals(Math.log(two), StatUtils.sumLog(x), tolerance);
207
 
        TestUtils.assertEquals(Math.log(two), StatUtils.sumLog(x, 0, 1), tolerance);
 
208
        TestUtils.assertEquals(FastMath.log(two), StatUtils.sumLog(x), tolerance);
 
209
        TestUtils.assertEquals(FastMath.log(two), StatUtils.sumLog(x, 0, 1), tolerance);
208
210
 
209
211
        // test many
210
212
        x = new double[] {one, two, two, three};
211
 
        TestUtils.assertEquals(Math.log(one) + 2.0 * Math.log(two) + Math.log(three), StatUtils.sumLog(x), tolerance);
212
 
        TestUtils.assertEquals(2.0 * Math.log(two), StatUtils.sumLog(x, 1, 2), tolerance);
 
213
        TestUtils.assertEquals(FastMath.log(one) + 2.0 * FastMath.log(two) + FastMath.log(three), StatUtils.sumLog(x), tolerance);
 
214
        TestUtils.assertEquals(2.0 * FastMath.log(two), StatUtils.sumLog(x, 1, 2), tolerance);
213
215
    }
214
216
 
215
217
    public void testMean() {
414
416
            // expected
415
417
        }
416
418
        test = new double[] {2, 4, 6, 8};
417
 
        assertEquals(Math.exp(0.25d * StatUtils.sumLog(test)),
 
419
        assertEquals(FastMath.exp(0.25d * StatUtils.sumLog(test)),
418
420
                StatUtils.geometricMean(test), Double.MIN_VALUE);
419
 
        assertEquals(Math.exp(0.5 * StatUtils.sumLog(test, 0, 2)),
 
421
        assertEquals(FastMath.exp(0.5 * StatUtils.sumLog(test, 0, 2)),
420
422
                StatUtils.geometricMean(test, 0, 2), Double.MIN_VALUE);
421
423
    }
 
424
    
 
425
    
 
426
    /**
 
427
     * Run the test with the values 50 and 100 and assume standardized values    
 
428
     */
 
429
 
 
430
    public void testNormalize1() {
 
431
        double sample[] = { 50, 100 };
 
432
        double expectedSample[] = { -25 / Math.sqrt(1250), 25 / Math.sqrt(1250) };
 
433
        double[] out = StatUtils.normalize(sample);
 
434
        for (int i = 0; i < out.length; i++) {
 
435
            assertEquals(out[i], expectedSample[i]);
 
436
        }
 
437
 
 
438
    }
 
439
 
 
440
    /**
 
441
     * Run with 77 random values, assuming that the outcome has a mean of 0 and a standard deviation of 1 with a
 
442
     * precision of 1E-10.
 
443
     */
 
444
 
 
445
    public void testNormalize2() {
 
446
        // create an sample with 77 values    
 
447
        int length = 77;
 
448
        double sample[] = new double[length];
 
449
        for (int i = 0; i < length; i++) {
 
450
            sample[i] = Math.random();
 
451
        }
 
452
        // normalize this sample
 
453
        double standardizedSample[] = StatUtils.normalize(sample);
 
454
 
 
455
        DescriptiveStatistics stats = new DescriptiveStatistics();
 
456
        // Add the data from the array
 
457
        for (int i = 0; i < length; i++) {
 
458
            stats.addValue(standardizedSample[i]);
 
459
        }
 
460
        // the calculations do have a limited precision    
 
461
        double distance = 1E-10;
 
462
        // check the mean an standard deviation
 
463
        assertEquals(0.0, stats.getMean(), distance);
 
464
        assertEquals(1.0, stats.getStandardDeviation(), distance);
 
465
 
 
466
    }
 
467
    
422
468
}