19
19
import junit.framework.TestCase;
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;
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) $
28
30
public final class StatUtilsTest extends TestCase {
98
100
StatUtils.sum(values, 2, 3);
99
assertTrue("Didn't throw exception", false);
100
} catch (Exception e) {
101
fail("Expected RuntimeException");
102
} catch (RuntimeException e) {
105
107
StatUtils.sum(values, -1, 2);
106
assertTrue("Didn't throw exception", false);
107
} catch (Exception e) {
108
fail("Expected RuntimeException");
109
} catch (RuntimeException e) {
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);
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);
215
217
public void testMean() {
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);
427
* Run the test with the values 50 and 100 and assume standardized values
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]);
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.
445
public void testNormalize2() {
446
// create an sample with 77 values
448
double sample[] = new double[length];
449
for (int i = 0; i < length; i++) {
450
sample[i] = Math.random();
452
// normalize this sample
453
double standardizedSample[] = StatUtils.normalize(sample);
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]);
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);