MATH-170. added SynchronizedDescriptiveStatistics class.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@590564 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
2e8332522e
commit
6afeedf161
|
@ -1,251 +1,39 @@
|
||||||
/*
|
/*
|
||||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||||
* contributor license agreements. See the NOTICE file distributed with
|
* contributor license agreements. See the NOTICE file distributed with this
|
||||||
* this work for additional information regarding copyright ownership.
|
* work for additional information regarding copyright ownership. The ASF
|
||||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
* licenses this file to You under the Apache License, Version 2.0 (the
|
||||||
* (the "License"); you may not use this file except in compliance with
|
* "License"); you may not use this file except in compliance with the License.
|
||||||
* the License. You may obtain a copy of the License at
|
* You may obtain a copy of the License at
|
||||||
*
|
* http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
* or agreed to in writing, software distributed under the License is
|
||||||
*
|
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
* KIND, either express or implied. See the License for the specific language
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
* governing permissions and limitations under the License.
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
*/
|
||||||
package org.apache.commons.math.stat.descriptive;
|
package org.apache.commons.math.stat.descriptive;
|
||||||
|
|
||||||
import junit.framework.Test;
|
import junit.framework.Test;
|
||||||
import junit.framework.TestCase;
|
|
||||||
import junit.framework.TestSuite;
|
import junit.framework.TestSuite;
|
||||||
|
|
||||||
import org.apache.commons.math.random.RandomData;
|
|
||||||
import org.apache.commons.math.random.RandomDataImpl;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test cases for the {@link Univariate} class.
|
* Test cases for the {@link Univariate} class.
|
||||||
*
|
* @version $Revision$ $Date: 2007-08-16 15:36:33 -0500 (Thu, 16 Aug
|
||||||
* @version $Revision$ $Date$
|
* 2007) $
|
||||||
*/
|
*/
|
||||||
|
public final class DescriptiveStatisticsImplTest extends DescriptiveStatisticsTest {
|
||||||
public final class DescriptiveStatisticsImplTest extends TestCase {
|
|
||||||
private double one = 1;
|
|
||||||
private float two = 2;
|
|
||||||
private int three = 3;
|
|
||||||
private double mean = 2;
|
|
||||||
private double sumSq = 18;
|
|
||||||
private double sum = 8;
|
|
||||||
private double var = 0.666666666666666666667;
|
|
||||||
private double std = Math.sqrt(var);
|
|
||||||
private double n = 4;
|
|
||||||
private double min = 1;
|
|
||||||
private double max = 3;
|
|
||||||
private double tolerance = 10E-15;
|
|
||||||
|
|
||||||
public DescriptiveStatisticsImplTest(String name) {
|
public DescriptiveStatisticsImplTest(String name) {
|
||||||
super(name);
|
super(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setUp() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Test suite() {
|
public static Test suite() {
|
||||||
TestSuite suite = new TestSuite(DescriptiveStatisticsImplTest.class);
|
TestSuite suite = new TestSuite(DescriptiveStatisticsImplTest.class);
|
||||||
suite.setName("DescriptiveStatistics Tests");
|
suite.setName("DescriptiveStatisticsImpl Tests");
|
||||||
return suite;
|
return suite;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** test stats */
|
|
||||||
public void testStats() {
|
|
||||||
DescriptiveStatistics u = DescriptiveStatistics.newInstance();
|
|
||||||
assertEquals("total count",0,u.getN(),tolerance);
|
|
||||||
u.addValue(one);
|
|
||||||
u.addValue(two);
|
|
||||||
u.addValue(two);
|
|
||||||
u.addValue(three);
|
|
||||||
assertEquals("N",n,u.getN(),tolerance);
|
|
||||||
assertEquals("sum",sum,u.getSum(),tolerance);
|
|
||||||
assertEquals("sumsq",sumSq,u.getSumsq(),tolerance);
|
|
||||||
assertEquals("var",var,u.getVariance(),tolerance);
|
|
||||||
assertEquals("std",std,u.getStandardDeviation(),tolerance);
|
|
||||||
assertEquals("mean",mean,u.getMean(),tolerance);
|
|
||||||
assertEquals("min",min,u.getMin(),tolerance);
|
|
||||||
assertEquals("max",max,u.getMax(),tolerance);
|
|
||||||
u.clear();
|
|
||||||
assertEquals("total count",0,u.getN(),tolerance);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void testN0andN1Conditions() throws Exception {
|
|
||||||
DescriptiveStatistics u = DescriptiveStatistics.newInstance();
|
|
||||||
|
|
||||||
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);
|
protected DescriptiveStatistics createDescriptiveStatistics() {
|
||||||
|
return new DescriptiveStatisticsImpl();
|
||||||
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() {
|
|
||||||
DescriptiveStatistics u = DescriptiveStatistics.newInstance();
|
|
||||||
|
|
||||||
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]);
|
|
||||||
}
|
|
||||||
|
|
||||||
assertEquals("mean", 12.40455, u.getMean(), 0.0001);
|
|
||||||
assertEquals("variance", 10.00236, u.getVariance(), 0.0001);
|
|
||||||
assertEquals("skewness", 1.437424, u.getSkewness(), 0.0001);
|
|
||||||
assertEquals("kurtosis", 2.37719, u.getKurtosis(), 0.0001);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void testProductAndGeometricMean() throws Exception {
|
|
||||||
DescriptiveStatistics u = DescriptiveStatistics.newInstance();
|
|
||||||
u.setWindowSize(10);
|
|
||||||
|
|
||||||
u.addValue( 1.0 );
|
|
||||||
u.addValue( 2.0 );
|
|
||||||
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 );
|
|
||||||
|
|
||||||
// Now test rolling - StorelessDescriptiveStatistics should discount the contribution
|
|
||||||
// of a discarded element
|
|
||||||
for( int i = 0; i < 10; i++ ) {
|
|
||||||
u.addValue( i + 2 );
|
|
||||||
}
|
|
||||||
// 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 );
|
|
||||||
}
|
|
||||||
|
|
||||||
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) {
|
|
||||||
DescriptiveStatistics u = DescriptiveStatistics.newInstance();
|
|
||||||
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};
|
|
||||||
DescriptiveStatistics u = DescriptiveStatistics.newInstance();
|
|
||||||
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 {
|
|
||||||
u.getPercentile(0);
|
|
||||||
fail("expecting IllegalArgumentException for getPercentile(0)");
|
|
||||||
} catch (IllegalArgumentException ex) {
|
|
||||||
;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
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)));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -29,122 +29,40 @@ import org.apache.commons.math.random.RandomDataImpl;
|
||||||
*
|
*
|
||||||
* @version $Revision$ $Date$
|
* @version $Revision$ $Date$
|
||||||
*/
|
*/
|
||||||
|
public abstract class DescriptiveStatisticsTest extends TestCase {
|
||||||
public final class DescriptiveStatisticsTest extends TestCase {
|
|
||||||
private double one = 1;
|
|
||||||
private float two = 2;
|
|
||||||
private int three = 3;
|
|
||||||
private double mean = 2;
|
|
||||||
private double sumSq = 18;
|
|
||||||
private double sum = 8;
|
|
||||||
private double var = 0.666666666666666666667;
|
private double var = 0.666666666666666666667;
|
||||||
private double std = Math.sqrt(var);
|
|
||||||
private double n = 4;
|
|
||||||
private double min = 1;
|
|
||||||
private double max = 3;
|
private double max = 3;
|
||||||
|
private double mean = 2;
|
||||||
|
private double min = 1;
|
||||||
|
private double n = 4;
|
||||||
|
private double one = 1;
|
||||||
|
private double std = Math.sqrt(var);
|
||||||
|
private double sum = 8;
|
||||||
|
private double sumSq = 18;
|
||||||
|
private int three = 3;
|
||||||
private double tolerance = 10E-15;
|
private double tolerance = 10E-15;
|
||||||
|
private float two = 2;
|
||||||
|
|
||||||
public DescriptiveStatisticsTest(String name) {
|
public DescriptiveStatisticsTest(String name) {
|
||||||
super(name);
|
super(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setUp() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Test suite() {
|
public static Test suite() {
|
||||||
TestSuite suite = new TestSuite(DescriptiveStatisticsTest.class);
|
TestSuite suite = new TestSuite(DescriptiveStatisticsTest.class);
|
||||||
suite.setName("Descriptive Statistics Tests");
|
suite.setName("Descriptive Statistics Tests");
|
||||||
return suite;
|
return suite;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** test stats */
|
protected abstract DescriptiveStatistics createDescriptiveStatistics();
|
||||||
public void testStats() {
|
|
||||||
DescriptiveStatistics u = DescriptiveStatistics.newInstance();
|
|
||||||
assertEquals("total count",0,u.getN(),tolerance);
|
|
||||||
u.addValue(one);
|
|
||||||
u.addValue(two);
|
|
||||||
u.addValue(two);
|
|
||||||
u.addValue(three);
|
|
||||||
assertEquals("N",n,u.getN(),tolerance);
|
|
||||||
assertEquals("sum",sum,u.getSum(),tolerance);
|
|
||||||
assertEquals("sumsq",sumSq,u.getSumsq(),tolerance);
|
|
||||||
assertEquals("var",var,u.getVariance(),tolerance);
|
|
||||||
assertEquals("std",std,u.getStandardDeviation(),tolerance);
|
|
||||||
assertEquals("mean",mean,u.getMean(),tolerance);
|
|
||||||
assertEquals("min",min,u.getMin(),tolerance);
|
|
||||||
assertEquals("max",max,u.getMax(),tolerance);
|
|
||||||
u.clear();
|
|
||||||
assertEquals("total count",0,u.getN(),tolerance);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void testN0andN1Conditions() throws Exception {
|
public void setUp() {
|
||||||
DescriptiveStatistics u = DescriptiveStatistics.newInstance();
|
|
||||||
|
|
||||||
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() {
|
|
||||||
DescriptiveStatistics u = DescriptiveStatistics.newInstance();
|
|
||||||
|
|
||||||
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]);
|
|
||||||
}
|
|
||||||
|
|
||||||
assertEquals("mean", 12.40455, u.getMean(), 0.0001);
|
|
||||||
assertEquals("variance", 10.00236, u.getVariance(), 0.0001);
|
|
||||||
assertEquals("skewness", 1.437424, u.getSkewness(), 0.0001);
|
|
||||||
assertEquals("kurtosis", 2.37719, u.getKurtosis(), 0.0001);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testProductAndGeometricMean() throws Exception {
|
|
||||||
DescriptiveStatistics u = DescriptiveStatistics.newInstance();
|
|
||||||
u.setWindowSize(10);
|
|
||||||
|
|
||||||
u.addValue( 1.0 );
|
|
||||||
u.addValue( 2.0 );
|
|
||||||
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 );
|
|
||||||
|
|
||||||
// Now test rolling - StorelessDescriptiveStatistics should discount the contribution
|
|
||||||
// of a discarded element
|
|
||||||
for( int i = 0; i < 10; i++ ) {
|
|
||||||
u.addValue( i + 2 );
|
|
||||||
}
|
|
||||||
// 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 );
|
|
||||||
}
|
|
||||||
|
|
||||||
public void testAddValue() {
|
public void testAddValue() {
|
||||||
double[] test1 = {5,4,3,2,1,0};
|
double[] test1 = {5,4,3,2,1,0};
|
||||||
double[] test2 = {5,2,1,0,4,3};
|
double[] test2 = {5,2,1,0,4,3};
|
||||||
|
|
||||||
DescriptiveStatistics stats = DescriptiveStatistics.newInstance();
|
DescriptiveStatistics stats = createDescriptiveStatistics();
|
||||||
stats.setWindowSize(12);
|
stats.setWindowSize(12);
|
||||||
|
|
||||||
for(int i = 0; i < test1.length; i++){
|
for(int i = 0; i < test1.length; i++){
|
||||||
|
@ -189,7 +107,7 @@ public final class DescriptiveStatisticsTest extends TestCase {
|
||||||
//System.out.println(test3[i] + " "+test2[i-6]);
|
//System.out.println(test3[i] + " "+test2[i-6]);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testGetSortedValues() {
|
public void testGetSortedValues() {
|
||||||
double[] test1 = {5,4,3,2,1};
|
double[] test1 = {5,4,3,2,1};
|
||||||
|
@ -219,28 +137,54 @@ public final class DescriptiveStatisticsTest extends TestCase {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testN0andN1Conditions() throws Exception {
|
||||||
|
DescriptiveStatistics u = createDescriptiveStatistics();
|
||||||
private void tstGetSortedValues(double[] test) {
|
|
||||||
DescriptiveStatistics u = DescriptiveStatistics.newInstance();
|
assertTrue("Mean of n = 0 set should be NaN",
|
||||||
u.setWindowSize(test.length);
|
Double.isNaN( u.getMean() ) );
|
||||||
for (int i = 0; i < test.length; i++) {
|
assertTrue("Standard Deviation of n = 0 set should be NaN",
|
||||||
u.addValue(test[i]);
|
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 testNewInstanceClassNull() {
|
||||||
|
try {
|
||||||
|
DescriptiveStatistics.newInstance((Class)null);
|
||||||
|
fail("null is not a valid descriptive statistics class");
|
||||||
|
} catch (NullPointerException ex) {
|
||||||
|
// success
|
||||||
|
} catch (Exception ex) {
|
||||||
|
fail();
|
||||||
}
|
}
|
||||||
double[] sorted = u.getSortedValues();
|
|
||||||
if (sorted.length != test.length) {
|
}
|
||||||
fail("wrong length for sorted values array");
|
|
||||||
}
|
public void testNewInstanceClassValid() {
|
||||||
for (int i = 0; i < sorted.length-1; i++) {
|
try {
|
||||||
if (sorted[i] > sorted[i+1]) {
|
DescriptiveStatistics u = DescriptiveStatistics.newInstance(
|
||||||
fail("sorted values out of sequence");
|
DescriptiveStatisticsImpl.class);
|
||||||
}
|
assertNotNull(u);
|
||||||
|
assertTrue(u instanceof DescriptiveStatisticsImpl);
|
||||||
|
} catch (InstantiationException ex) {
|
||||||
|
fail();
|
||||||
|
} catch (IllegalAccessException ex) {
|
||||||
|
fail();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testPercentiles() {
|
public void testPercentiles() {
|
||||||
double[] test = {5,4,3,2,1};
|
double[] test = {5,4,3,2,1};
|
||||||
DescriptiveStatistics u = DescriptiveStatistics.newInstance();
|
DescriptiveStatistics u = createDescriptiveStatistics();
|
||||||
u.setWindowSize(110);
|
u.setWindowSize(110);
|
||||||
for (int i = 0; i < test.length; i++) {
|
for (int i = 0; i < test.length; i++) {
|
||||||
u.addValue(test[i]);
|
u.addValue(test[i]);
|
||||||
|
@ -301,10 +245,39 @@ public final class DescriptiveStatisticsTest extends TestCase {
|
||||||
assertTrue("empty value set should return NaN",
|
assertTrue("empty value set should return NaN",
|
||||||
Double.isNaN(u.getPercentile(50)));
|
Double.isNaN(u.getPercentile(50)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public void testProductAndGeometricMean() throws Exception {
|
||||||
|
DescriptiveStatistics u = createDescriptiveStatistics();
|
||||||
|
u.setWindowSize(10);
|
||||||
|
|
||||||
|
u.addValue( 1.0 );
|
||||||
|
u.addValue( 2.0 );
|
||||||
|
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 );
|
||||||
|
|
||||||
|
// Now test rolling - StorelessDescriptiveStatistics should discount the contribution
|
||||||
|
// of a discarded element
|
||||||
|
for( int i = 0; i < 10; i++ ) {
|
||||||
|
u.addValue( i + 2 );
|
||||||
|
}
|
||||||
|
// 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 );
|
||||||
|
}
|
||||||
|
|
||||||
/** test stats */
|
/** test stats */
|
||||||
public void testSerialization() {
|
public void testSerialization() {
|
||||||
DescriptiveStatistics u = DescriptiveStatistics.newInstance();
|
DescriptiveStatistics u = createDescriptiveStatistics();
|
||||||
assertEquals("total count",0,u.getN(),tolerance);
|
assertEquals("total count",0,u.getN(),tolerance);
|
||||||
u.addValue(one);
|
u.addValue(one);
|
||||||
u.addValue(two);
|
u.addValue(two);
|
||||||
|
@ -325,51 +298,60 @@ public final class DescriptiveStatisticsTest extends TestCase {
|
||||||
|
|
||||||
u2.clear();
|
u2.clear();
|
||||||
assertEquals("total count",0,u2.getN(),tolerance);
|
assertEquals("total count",0,u2.getN(),tolerance);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testSkewAndKurtosis() {
|
||||||
|
DescriptiveStatistics u = createDescriptiveStatistics();
|
||||||
|
|
||||||
|
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]);
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEquals("mean", 12.40455, u.getMean(), 0.0001);
|
||||||
|
assertEquals("variance", 10.00236, u.getVariance(), 0.0001);
|
||||||
|
assertEquals("skewness", 1.437424, u.getSkewness(), 0.0001);
|
||||||
|
assertEquals("kurtosis", 2.37719, u.getKurtosis(), 0.0001);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testNewInstanceClassNull() {
|
/** test stats */
|
||||||
try {
|
public void testStats() {
|
||||||
DescriptiveStatistics.newInstance((Class)null);
|
DescriptiveStatistics u = createDescriptiveStatistics();
|
||||||
fail("null is not a valid descriptive statistics class");
|
assertEquals("total count",0,u.getN(),tolerance);
|
||||||
} catch (NullPointerException ex) {
|
u.addValue(one);
|
||||||
// success
|
u.addValue(two);
|
||||||
} catch (Exception ex) {
|
u.addValue(two);
|
||||||
fail();
|
u.addValue(three);
|
||||||
}
|
assertEquals("N",n,u.getN(),tolerance);
|
||||||
|
assertEquals("sum",sum,u.getSum(),tolerance);
|
||||||
|
assertEquals("sumsq",sumSq,u.getSumsq(),tolerance);
|
||||||
|
assertEquals("var",var,u.getVariance(),tolerance);
|
||||||
|
assertEquals("std",std,u.getStandardDeviation(),tolerance);
|
||||||
|
assertEquals("mean",mean,u.getMean(),tolerance);
|
||||||
|
assertEquals("min",min,u.getMin(),tolerance);
|
||||||
|
assertEquals("max",max,u.getMax(),tolerance);
|
||||||
|
u.clear();
|
||||||
|
assertEquals("total count",0,u.getN(),tolerance);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testNewInstanceClassValid() {
|
public void testToString() {
|
||||||
try {
|
DescriptiveStatistics u = createDescriptiveStatistics();
|
||||||
DescriptiveStatistics u = DescriptiveStatistics.newInstance(
|
assertTrue(u.toString().indexOf("NaN") > 0);
|
||||||
DescriptiveStatisticsImpl.class);
|
assertTrue(u.toString().startsWith("DescriptiveStatistics"));
|
||||||
assertNotNull(u);
|
double[] testArray =
|
||||||
assertTrue(u instanceof DescriptiveStatisticsImpl);
|
{ 12.5, 12, 11.8, 14.2, 14.9, 14.5, 21, 8.2, 10.3, 11.3, 14.1,
|
||||||
} catch (InstantiationException ex) {
|
9.9, 12.2, 12, 12.1, 11, 19.8, 11, 10, 8.8, 9, 12.3 };
|
||||||
fail();
|
for( int i = 0; i < testArray.length; i++) {
|
||||||
} catch (IllegalAccessException ex) {
|
u.addValue( testArray[i]);
|
||||||
fail();
|
}
|
||||||
}
|
assertTrue(u.toString().indexOf("NaN") == -1);
|
||||||
}
|
assertTrue(u.toString().startsWith("DescriptiveStatistics"));
|
||||||
|
|
||||||
public void testWindowSize() {
|
|
||||||
DescriptiveStatistics u = DescriptiveStatistics.newInstance();
|
|
||||||
u.setWindowSize(1234);
|
|
||||||
assertEquals(1234, u.getWindowSize());
|
|
||||||
|
|
||||||
u.addValue(1.0);
|
|
||||||
u.addValue(2.0);
|
|
||||||
u.addValue(3.0);
|
|
||||||
u.addValue(4.0);
|
|
||||||
u.addValue(5.0);
|
|
||||||
assertEquals(5, u.getN());
|
|
||||||
|
|
||||||
u.setWindowSize(DescriptiveStatistics.INFINITE_WINDOW);
|
|
||||||
assertEquals(5, u.getN());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testWindowing() {
|
public void testWindowing() {
|
||||||
DescriptiveStatistics u = DescriptiveStatistics.newInstance();
|
DescriptiveStatistics u = createDescriptiveStatistics();
|
||||||
u.setWindowSize(2);
|
u.setWindowSize(2);
|
||||||
|
|
||||||
u.addValue(1.0);
|
u.addValue(1.0);
|
||||||
|
@ -385,18 +367,37 @@ public final class DescriptiveStatisticsTest extends TestCase {
|
||||||
assertEquals(3.0, u.getMean(), tolerance);
|
assertEquals(3.0, u.getMean(), tolerance);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testToString() {
|
public void testWindowSize() {
|
||||||
DescriptiveStatistics u = DescriptiveStatistics.newInstance();
|
DescriptiveStatistics u = createDescriptiveStatistics();
|
||||||
assertTrue(u.toString().indexOf("NaN") > 0);
|
u.setWindowSize(1234);
|
||||||
assertTrue(u.toString().startsWith("DescriptiveStatistics"));
|
assertEquals(1234, u.getWindowSize());
|
||||||
double[] testArray =
|
|
||||||
{ 12.5, 12, 11.8, 14.2, 14.9, 14.5, 21, 8.2, 10.3, 11.3, 14.1,
|
u.addValue(1.0);
|
||||||
9.9, 12.2, 12, 12.1, 11, 19.8, 11, 10, 8.8, 9, 12.3 };
|
u.addValue(2.0);
|
||||||
for( int i = 0; i < testArray.length; i++) {
|
u.addValue(3.0);
|
||||||
u.addValue( testArray[i]);
|
u.addValue(4.0);
|
||||||
}
|
u.addValue(5.0);
|
||||||
assertTrue(u.toString().indexOf("NaN") == -1);
|
assertEquals(5, u.getN());
|
||||||
assertTrue(u.toString().startsWith("DescriptiveStatistics"));
|
|
||||||
|
u.setWindowSize(DescriptiveStatistics.INFINITE_WINDOW);
|
||||||
|
assertEquals(5, u.getN());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void tstGetSortedValues(double[] test) {
|
||||||
|
DescriptiveStatistics u = createDescriptiveStatistics();
|
||||||
|
u.setWindowSize(test.length);
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -95,6 +95,9 @@ Commons Math Release Notes</title>
|
||||||
MaxIterationsExceededException and return 0 or 1, resp. if the argument
|
MaxIterationsExceededException and return 0 or 1, resp. if the argument
|
||||||
is more than 20 standard deviations from the mean.
|
is more than 20 standard deviations from the mean.
|
||||||
</action>
|
</action>
|
||||||
|
<action dev="brentworden" type="update" issue="MATH-170" due-to="David J. M. Karlsen">
|
||||||
|
Added SynchronizedDescriptiveStatistics class.
|
||||||
|
</action>
|
||||||
</release>
|
</release>
|
||||||
<release version="1.1" date="2005-12-17"
|
<release version="1.1" date="2005-12-17"
|
||||||
description="This is a maintenance release containing bug fixes and enhancements.
|
description="This is a maintenance release containing bug fixes and enhancements.
|
||||||
|
|
|
@ -199,7 +199,16 @@ while (line != null) {
|
||||||
}
|
}
|
||||||
in.close();
|
in.close();
|
||||||
</source>
|
</source>
|
||||||
</dd>
|
</dd>
|
||||||
|
<dt>Compute statistics in a thread-safe manner</dt>
|
||||||
|
<br/>
|
||||||
|
<dd>Use a <code>ThreadSafeDescriptiveStatistics</code> instance
|
||||||
|
<source>
|
||||||
|
// Create a SynchronizedDescriptiveStatistics instance and
|
||||||
|
// use as any other DescriptiveStatistics instance
|
||||||
|
DescriptiveStatistics stats = DescriptiveStatistics.newInstance(SynchronizedDescriptiveStatistics.class);
|
||||||
|
</source>
|
||||||
|
</dd>
|
||||||
</dl>
|
</dl>
|
||||||
</p>
|
</p>
|
||||||
</subsection>
|
</subsection>
|
||||||
|
|
Loading…
Reference in New Issue