You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by md...@apache.org on 2003/06/04 04:24:51 UTC
cvs commit: jakarta-commons-sandbox/math/src/test/org/apache/commons/math/stat StoreUnivariateImplTest.java
mdiggory 2003/06/03 19:24:51
Modified: math/src/test/org/apache/commons/math/stat
StoreUnivariateImplTest.java
Log:
PR: http://nagoya.apache.org/bugzilla/show_bug.cgi?id=20377
Submitted by: Phil Steitz
Revision Changes Path
1.2 +138 -19 jakarta-commons-sandbox/math/src/test/org/apache/commons/math/stat/StoreUnivariateImplTest.java
Index: StoreUnivariateImplTest.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/math/src/test/org/apache/commons/math/stat/StoreUnivariateImplTest.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- StoreUnivariateImplTest.java 29 May 2003 20:35:46 -0000 1.1
+++ StoreUnivariateImplTest.java 4 Jun 2003 02:24:51 -0000 1.2
@@ -56,6 +56,8 @@
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
+import org.apache.commons.math.RandomDataImpl;
+import org.apache.commons.math.RandomData;
/**
* Test cases for the {@link Univariate} class.
@@ -117,22 +119,29 @@
public void testN0andN1Conditions() throws Exception {
StoreUnivariate u = new StoreUnivariateImpl();
- assertTrue("Mean of n = 0 set should be NaN", Double.isNaN( u.getMean() ) );
- assertTrue("Standard Deviation of n = 0 set should be NaN", Double.isNaN( u.getStandardDeviation() ) );
- assertTrue("Variance of n = 0 set should be NaN", Double.isNaN(u.getVariance() ) );
-
- u.addValue(one);
-
- assertTrue( "Mean of n = 1 set should be value of single item n1", u.getMean() == one);
- assertTrue( "StdDev of n = 1 set should be zero, instead it is: " + u.getStandardDeviation(), u.getStandardDeviation() == 0);
- assertTrue( "Variance of n = 1 set should be zero", u.getVariance() == 0);
+ assertTrue("Mean of n = 0 set should be NaN",
+ Double.isNaN( u.getMean() ) );
+ assertTrue("Standard Deviation of n = 0 set should be NaN",
+ Double.isNaN( u.getStandardDeviation() ) );
+ assertTrue("Variance of n = 0 set should be NaN",
+ Double.isNaN(u.getVariance() ) );
+
+ u.addValue(one);
+
+ assertTrue( "Mean of n = 1 set should be value of single item n1",
+ u.getMean() == one);
+ assertTrue( "StdDev of n = 1 set should be zero, instead it is: "
+ + u.getStandardDeviation(), u.getStandardDeviation() == 0);
+ assertTrue( "Variance of n = 1 set should be zero",
+ u.getVariance() == 0);
}
public void testSkewAndKurtosis() {
StoreUnivariate u = new StoreUnivariateImpl();
- double[] testArray = { 12.5, 12, 11.8, 14.2, 14.9, 14.5, 21, 8.2, 10.3, 11.3, 14.1,
- 9.9, 12.2, 12, 12.1, 11, 19.8, 11, 10, 8.8, 9, 12.3 };
+ double[] testArray =
+ { 12.5, 12, 11.8, 14.2, 14.9, 14.5, 21, 8.2, 10.3, 11.3, 14.1,
+ 9.9, 12.2, 12, 12.1, 11, 19.8, 11, 10, 8.8, 9, 12.3 };
for( int i = 0; i < testArray.length; i++) {
u.addValue( testArray[i]);
}
@@ -152,8 +161,10 @@
u.addValue( 3.0 );
u.addValue( 4.0 );
- assertEquals( "Product not expected", 24.0, u.getProduct(), Double.MIN_VALUE );
- assertEquals( "Geometric mean not expected", 2.213364, u.getGeometricMean(), 0.00001 );
+ assertEquals( "Product not expected",
+ 24.0, u.getProduct(), Double.MIN_VALUE );
+ assertEquals( "Geometric mean not expected",
+ 2.213364, u.getGeometricMean(), 0.00001 );
// Now test rolling - UnivariateImpl should discount the contribution
// of a discarded element
@@ -162,11 +173,119 @@
}
// Values should be (2,3,4,5,6,7,8,9,10,11)
- assertEquals( "Product not expected", 39916800.0, u.getProduct(), 0.00001 );
- assertEquals( "Geometric mean not expected", 5.755931, u.getGeometricMean(), 0.00001 );
-
-
+ assertEquals( "Product not expected", 39916800.0,
+ u.getProduct(), 0.00001 );
+ assertEquals( "Geometric mean not expected", 5.755931,
+ u.getGeometricMean(), 0.00001 );
+ }
+
+ public void testGetSortedValues() {
+ double[] test1 = {5,4,3,2,1};
+ double[] test2 = {5,2,1,3,4,0};
+ double[] test3 = {1};
+ int[] testi = null;
+ double[] test4 = null;
+ RandomData rd = new RandomDataImpl();
+ tstGetSortedValues(test1);
+ tstGetSortedValues(test2);
+ tstGetSortedValues(test3);
+ for (int i = 0; i < 10; i++) {
+ testi = rd.nextPermutation(10,6);
+ test4 = new double[6];
+ for (int j = 0; j < testi.length; j++) {
+ test4[j] = (double) testi[j];
+ }
+ tstGetSortedValues(test4);
+ }
+ for (int i = 0; i < 10; i++) {
+ testi = rd.nextPermutation(10,5);
+ test4 = new double[5];
+ for (int j = 0; j < testi.length; j++) {
+ test4[j] = (double) testi[j];
+ }
+ tstGetSortedValues(test4);
+ }
}
-
+
+
+ private void tstGetSortedValues(double[] test) {
+ StoreUnivariateImpl u = new StoreUnivariateImpl();
+ for (int i = 0; i < test.length; i++) {
+ u.addValue(test[i]);
+ }
+ double[] sorted = u.getSortedValues();
+ if (sorted.length != test.length) {
+ fail("wrong length for sorted values array");
+ }
+ for (int i = 0; i < sorted.length-1; i++) {
+ if (sorted[i] > sorted[i+1]) {
+ fail("sorted values out of sequence");
+ }
+ }
+ }
+
+ public void testPercentiles() {
+ double[] test = {5,4,3,2,1};
+ StoreUnivariateImpl u = new StoreUnivariateImpl();
+ for (int i = 0; i < test.length; i++) {
+ u.addValue(test[i]);
+ }
+ assertEquals("expecting min",1,u.getPercentile(5),10E-12);
+ assertEquals("expecting max",5,u.getPercentile(99),10E-12);
+ assertEquals("expecting middle",3,u.getPercentile(50),10E-12);
+ try {
+ double x = u.getPercentile(0);
+ fail("expecting IllegalArgumentException for getPercentile(0)");
+ } catch (IllegalArgumentException ex) {
+ ;
+ }
+ try {
+ double x = u.getPercentile(120);
+ fail("expecting IllegalArgumentException for getPercentile(120)");
+ } catch (IllegalArgumentException ex) {
+ ;
+ }
+
+ u.clear();
+ double[] test2 = {1,2,3,4};
+ for (int i = 0; i < test2.length; i++) {
+ u.addValue(test2[i]);
+ }
+ assertEquals("Q1",1.25,u.getPercentile(25),10E-12);
+ assertEquals("Q3",3.75,u.getPercentile(75),10E-12);
+ assertEquals("Q2",2.5,u.getPercentile(50),10E-12);
+
+ u.clear();
+ double[] test3 = {1};
+ for (int i = 0; i < test3.length; i++) {
+ u.addValue(test3[i]);
+ }
+ assertEquals("Q1",1,u.getPercentile(25),10E-12);
+ assertEquals("Q3",1,u.getPercentile(75),10E-12);
+ assertEquals("Q2",1,u.getPercentile(50),10E-12);
+
+ u.clear();
+ RandomData rd = new RandomDataImpl();
+ int[] testi = rd.nextPermutation(100,100); // will contain 0-99
+ for (int j = 0; j < testi.length; j++) {
+ u.addValue((double) testi[j]); //OK, laugh at me for the cast
+ }
+ for (int i = 1; i < 100; i++) {
+ assertEquals("percentile " + i,
+ (double) i-1 + (double) i*(.01), u.getPercentile(i),10E-12);
+ }
+
+ u.clear();
+ double[] test4 = {1,2,3,4,100};
+ for (int i = 0; i < test4.length; i++) {
+ u.addValue(test4[i]);
+ }
+ assertEquals("80th",80.8,u.getPercentile(80),10E-12);
+
+ u.clear();
+ assertTrue("empty value set should return NaN",
+ Double.isNaN(u.getPercentile(50)));
+ }
+
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org