removed deprecated factory methods and classes

they were replaced by setter injection as of 1.2

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/branches/MATH_2_0@651230 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Luc Maisonobe 2008-04-24 12:04:33 +00:00
parent 5ae0852fe0
commit cffe7c6cc6
14 changed files with 207 additions and 891 deletions

View File

@ -20,7 +20,6 @@ import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import org.apache.commons.discovery.tools.DiscoverClass;
import org.apache.commons.math.stat.descriptive.moment.GeometricMean;
import org.apache.commons.math.stat.descriptive.moment.Kurtosis;
import org.apache.commons.math.stat.descriptive.moment.Mean;
@ -56,8 +55,8 @@ import org.apache.commons.math.util.ResizableDoubleArray;
public class DescriptiveStatistics implements StatisticalSummary, Serializable {
/** Serialization UID */
private static final long serialVersionUID = -2734185686570407433L;
private static final long serialVersionUID = 4133067267405273064L;
/** hold the window size **/
protected int windowSize = INFINITE_WINDOW;
@ -112,39 +111,6 @@ public class DescriptiveStatistics implements StatisticalSummary, Serializable {
setWindowSize(window);
}
/**
* Create an instance of a <code>DescriptiveStatistics</code>
* @param cls the type of <code>DescriptiveStatistics</code> object to
* create.
* @return a new instance.
* @throws InstantiationException is thrown if the object can not be
* created.
* @throws IllegalAccessException is thrown if the type's default
* constructor is not accessible.
* @deprecated to be removed in commons-math 2.0
*/
public static DescriptiveStatistics newInstance(Class cls) throws InstantiationException, IllegalAccessException {
return (DescriptiveStatistics)cls.newInstance();
}
/**
* Create an instance of a <code>DescriptiveStatistics</code>
* @return a new DescriptiveStatistics instance.
* @deprecated to be removed in commons-math 2.0
*/
public static DescriptiveStatistics newInstance() {
DescriptiveStatistics factory = null;
try {
DiscoverClass dc = new DiscoverClass();
factory = (DescriptiveStatistics) dc.newInstance(
DescriptiveStatistics.class,
"org.apache.commons.math.stat.descriptive.DescriptiveStatisticsImpl");
} catch(Throwable t) {
return new DescriptiveStatisticsImpl();
}
return factory;
}
/**
* Represents an infinite window size. When the {@link #getWindowSize()}
* returns this value, there is no limit to the number of data values

View File

@ -15,7 +15,6 @@ package org.apache.commons.math.stat.descriptive;
import java.io.Serializable;
import org.apache.commons.discovery.tools.DiscoverClass;
import org.apache.commons.math.stat.descriptive.moment.GeometricMean;
import org.apache.commons.math.stat.descriptive.moment.Mean;
import org.apache.commons.math.stat.descriptive.moment.SecondMoment;
@ -55,37 +54,7 @@ import org.apache.commons.math.util.MathUtils;
public class SummaryStatistics implements StatisticalSummary, Serializable {
/** Serialization UID */
private static final long serialVersionUID = -3346512372447011854L;
/**
* Create an instance of a <code>SummaryStatistics</code>
* @param cls the type of <code>SummaryStatistics</code> object to create.
* @return a new instance.
* @deprecated to be removed in commons-math 2.0
* @throws InstantiationException is thrown if the object can not be
* created.
* @throws IllegalAccessException is thrown if the type's default
* constructor is not accessible.
*/
public static SummaryStatistics newInstance(Class cls) throws InstantiationException, IllegalAccessException {
return (SummaryStatistics)cls.newInstance();
}
/**
* Create an instance of a <code>SummaryStatistics</code>
* @return a new SummaryStatistics instance.
* @deprecated to be removed in commons-math 2.0
*/
public static SummaryStatistics newInstance() {
SummaryStatistics instance = null;
try {
DiscoverClass dc = new DiscoverClass();
instance = (SummaryStatistics)dc.newInstance(SummaryStatistics.class, "org.apache.commons.math.stat.descriptive.SummaryStatisticsImpl");
} catch (Throwable t) {
return new SummaryStatisticsImpl();
}
return instance;
}
private static final long serialVersionUID = -2021321786743555871L;
/**
* Construct a SummaryStatistics instance

View File

@ -19,7 +19,6 @@ package org.apache.commons.math.stat.inference;
import org.apache.commons.math.MathException;
import org.apache.commons.math.distribution.ChiSquaredDistribution;
import org.apache.commons.math.distribution.ChiSquaredDistributionImpl;
import org.apache.commons.math.distribution.DistributionFactory;
/**
* Implements Chi-Square test statistics defined in the
@ -329,16 +328,6 @@ public class ChiSquareTestImpl implements UnknownDistributionChiSquareTest {
}
//--------------------- Protected methods ---------------------------------
/**
* Gets a DistributionFactory to use in creating ChiSquaredDistribution instances.
* @deprecated inject ChiSquaredDistribution instances directly instead of
* using a factory.
*/
protected DistributionFactory getDistributionFactory() {
return DistributionFactory.newInstance();
}
//--------------------- Private array methods -- should find a utility home for these
/**

View File

@ -17,7 +17,6 @@
package org.apache.commons.math.stat.inference;
import org.apache.commons.math.MathException;
import org.apache.commons.math.distribution.DistributionFactory;
import org.apache.commons.math.distribution.TDistribution;
import org.apache.commons.math.distribution.TDistributionImpl;
import org.apache.commons.math.stat.StatUtils;
@ -919,15 +918,6 @@ public class TTestImpl implements TTest {
//----------------------------------------------- Protected methods
/**
* Gets a DistributionFactory to use in creating TDistribution instances.
* @return a distribution factory.
* @deprecated inject TDistribution directly instead of using a factory.
*/
protected DistributionFactory getDistributionFactory() {
return DistributionFactory.newInstance();
}
/**
* Computes approximate degrees of freedom for 2-sample t-test.
*

View File

@ -1,67 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* 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.inference;
import org.apache.commons.discovery.tools.DiscoverClass;
/**
* Abstract factory to create inference test instances.
*
* @since 1.1
* @version $Revision$ $Date$
* @deprecated as of 1.2, pluggability of test instances is now provided through
* constructors and setters.
*/
public abstract class TestFactory {
/**
* Default constructor.
*/
protected TestFactory() {
super();
}
/**
* Create an instance of a <code>TestFactory</code>
*
* @return a new factory.
*/
public static TestFactory newInstance() {
TestFactory factory = null;
try {
DiscoverClass dc = new DiscoverClass();
factory = (TestFactory) dc.newInstance(
TestFactory.class,
"org.apache.commons.math.stat.inference.TestFactoryImpl");
} catch(Throwable t) {
return new TestFactoryImpl();
}
return factory;
}
/**
* Create a TTest instance.
*
* @return a new TTest instance
*/
public abstract TTest createTTest();
/**
* Create a ChiSquareTest instance.
*
* @return a new ChiSquareTest instance
*/
public abstract ChiSquareTest createChiSquareTest();
}

View File

@ -1,55 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* 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.inference;
/**
* A concrete inference test factory. This is the default factory used by
* Commons-Math.
*
* @deprecated as of 1.2, pluggability of test instances is now provided through
* constructors and setters.
* @since 1.1
* @version $Revision$ $Date$
*/
public class TestFactoryImpl extends TestFactory {
/**
* Default constructor.
*/
public TestFactoryImpl() {
super();
}
/**
* Create a TTest instance.
*
* @return a new TTest instance
*/
public TTest createTTest() {
return new TTestImpl();
}
/**
* Create a ChiSquareTest instance.
*
* @return a new ChiSquareTest instance
*/
public ChiSquareTest createChiSquareTest() {
return new ChiSquareTestImpl();
}
}

View File

@ -1,398 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* 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;
import junit.framework.TestCase;
import org.apache.commons.math.TestUtils;
import org.apache.commons.math.random.RandomData;
import org.apache.commons.math.random.RandomDataImpl;
/**
* Test cases for the DescriptiveStatistics implementations.
*
* @version $Revision$ $Date$
* @deprecated should be moved down into DescriptiveStatisticsTest
* when DescriptiveStatisticsImpl is removed in 2.0
*/
public abstract class DescriptiveStatisticsAbstractTest extends TestCase {
private double var = 0.666666666666666666667;
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 float two = 2;
public DescriptiveStatisticsAbstractTest(String name) {
super(name);
}
protected abstract DescriptiveStatistics createDescriptiveStatistics();
public void setUp() {
}
public void testAddValue() {
double[] test1 = {5,4,3,2,1,0};
double[] test2 = {5,2,1,0,4,3};
DescriptiveStatistics stats = createDescriptiveStatistics();
stats.setWindowSize(12);
for(int i = 0; i < test1.length; i++){
stats.addValue(test1[i]);
}
double[] test3 = stats.getValues();
for(int i = 0; i < 6; i++){
assertEquals( "Added value ["+i+"] not equal",
test3[i], test1[i],0.0);
//System.out.println(test3[i] + " "+test1[i]);
}
for(int i = 0; i < test2.length; i++){
stats.addValue(test2[i]);
}
test3 = stats.getValues();
for(int i = 6; i < 12; i++){
assertEquals( "Added value ["+i+"] not equal",
test3[i], test2[i-6],0.0);
//System.out.println(test3[i] + " "+test2[i-6]);
}
for(int i = 0; i < test2.length; i++){
stats.addValue(test2[i]);
}
test3 = stats.getValues();
for(int i = 0; i < 6; i++){
assertEquals( "Added value ["+i+"] not equal",
test3[i], test2[i],0.0);
//System.out.println(test3[i] + " "+test2[i]);
}
for(int i = 6; i < 12; i++){
assertEquals( "Added value ["+i+"] not equal",
test3[i], test2[i-6],0.0);
//System.out.println(test3[i] + " "+test2[i-6]);
}
}
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);
}
}
public void testN0andN1Conditions() throws Exception {
DescriptiveStatistics u = createDescriptiveStatistics();
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 testNewInstanceClassNull() {
try {
DescriptiveStatistics.newInstance((Class)null);
fail("null is not a valid descriptive statistics class");
} catch (NullPointerException ex) {
// success
} catch (Exception ex) {
fail();
}
}
public void testNewInstanceClassValid() {
try {
DescriptiveStatistics u = DescriptiveStatistics.newInstance(
DescriptiveStatisticsImpl.class);
assertNotNull(u);
assertTrue(u instanceof DescriptiveStatisticsImpl);
} catch (InstantiationException ex) {
fail();
} catch (IllegalAccessException ex) {
fail();
}
}
public void testPercentiles() {
double[] test = {5,4,3,2,1};
DescriptiveStatistics u = createDescriptiveStatistics();
u.setWindowSize(110);
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)));
}
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 */
public void testSerialization() {
DescriptiveStatistics u = createDescriptiveStatistics();
assertEquals("total count",0,u.getN(),tolerance);
u.addValue(one);
u.addValue(two);
DescriptiveStatistics u2 = (DescriptiveStatistics)TestUtils.serializeAndRecover(u);
u2.addValue(two);
u2.addValue(three);
assertEquals("N",n,u2.getN(),tolerance);
assertEquals("sum",sum,u2.getSum(),tolerance);
assertEquals("sumsq",sumSq,u2.getSumsq(),tolerance);
assertEquals("var",var,u2.getVariance(),tolerance);
assertEquals("std",std,u2.getStandardDeviation(),tolerance);
assertEquals("mean",mean,u2.getMean(),tolerance);
assertEquals("min",min,u2.getMin(),tolerance);
assertEquals("max",max,u2.getMax(),tolerance);
u2.clear();
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);
}
/** test stats */
public void testStats() {
DescriptiveStatistics u = createDescriptiveStatistics();
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 testToString() {
DescriptiveStatistics u = createDescriptiveStatistics();
assertTrue(u.toString().indexOf("NaN") > 0);
assertTrue(u.toString().startsWith("DescriptiveStatistics"));
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]);
}
assertTrue(u.toString().indexOf("NaN") == -1);
assertTrue(u.toString().startsWith("DescriptiveStatistics"));
}
public void testWindowing() {
DescriptiveStatistics u = createDescriptiveStatistics();
u.setWindowSize(2);
u.addValue(1.0);
assertEquals(1.0, u.getMean(), tolerance);
u.addValue(2.0);
assertEquals(1.5, u.getMean(), tolerance);
u.addValue(3.0);
assertEquals(2.5, u.getMean(), tolerance);
u.setWindowSize(1);
assertEquals(3.0, u.getMean(), tolerance);
}
public void testWindowSize() {
DescriptiveStatistics u = createDescriptiveStatistics();
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());
}
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");
}
}
}
}

View File

@ -1,40 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with this
* work for additional information regarding copyright ownership. The ASF
* licenses this file to You under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
* or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, 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;
import junit.framework.Test;
import junit.framework.TestSuite;
/**
* Test cases for the DescriptiveStatisticsImpl class.
* @deprecated - to be removed in 2.0 with DescriptiveStatisticsImpl
* @version $Revision$ $Date: 2007-08-16 15:36:33 -0500 (Thu, 16 Aug
* 2007) $
*/
public final class DescriptiveStatisticsImplTest extends DescriptiveStatisticsAbstractTest {
public DescriptiveStatisticsImplTest(String name) {
super(name);
}
public static Test suite() {
TestSuite suite = new TestSuite(DescriptiveStatisticsImplTest.class);
suite.setName("DescriptiveStatisticsImpl Tests");
return suite;
}
protected DescriptiveStatistics createDescriptiveStatistics() {
return new DescriptiveStatisticsImpl();
}
}

View File

@ -14,19 +14,18 @@
package org.apache.commons.math.stat.descriptive;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.apache.commons.math.stat.descriptive.rank.Percentile;
/**
* Test cases for the DescriptiveStatistics class.
* When DescriptiveStatisticsImpl is removed, this class should replace
* DescriptiveStatisticsAbstractTest
*
* @version $Revision: 592121 $ $Date: 2007-08-16 15:36:33 -0500 (Thu, 16 Aug
* 2007) $
*/
public final class DescriptiveStatisticsTest extends DescriptiveStatisticsAbstractTest {
public class DescriptiveStatisticsTest extends TestCase {
public DescriptiveStatisticsTest(String name) {
super(name);

View File

@ -1,228 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* 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;
import junit.framework.TestCase;
import org.apache.commons.math.TestUtils;
/**
* Test cases for the {@link SummaryStatisticsImpl} class.
*
* @version $Revision: 602305 $ $Date: 2007-12-08 03:51:23 +0100 (sam., 08 déc. 2007) $
* @deprecated should be moved down into SummaryStatisticsTest
* when SummaryStatisticsImpl is removed in 2.0
*/
public abstract class SummaryStatisticsAbstractTest extends TestCase {
private double one = 1;
private float twoF = 2;
private long twoL = 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 SummaryStatisticsAbstractTest(String name) {
super(name);
}
protected abstract SummaryStatistics createSummaryStatistics();
/** test stats */
public void testStats() {
SummaryStatistics u = createSummaryStatistics();
assertEquals("total count",0,u.getN(),tolerance);
u.addValue(one);
u.addValue(twoF);
u.addValue(twoL);
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 {
SummaryStatistics u = createSummaryStatistics();
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() ) );
/* n=1 */
u.addValue(one);
assertTrue("mean should be one (n = 1)",
u.getMean() == one);
assertTrue("geometric should be one (n = 1) instead it is " + u.getGeometricMean(),
u.getGeometricMean() == one);
assertTrue("Std should be zero (n = 1)",
u.getStandardDeviation() == 0.0);
assertTrue("variance should be zero (n = 1)",
u.getVariance() == 0.0);
/* n=2 */
u.addValue(twoF);
assertTrue("Std should not be zero (n = 2)",
u.getStandardDeviation() != 0.0);
assertTrue("variance should not be zero (n = 2)",
u.getVariance() != 0.0);
}
public void testProductAndGeometricMean() throws Exception {
SummaryStatistics u = createSummaryStatistics();
u.addValue( 1.0 );
u.addValue( 2.0 );
u.addValue( 3.0 );
u.addValue( 4.0 );
assertEquals( "Geometric mean not expected", 2.213364,
u.getGeometricMean(), 0.00001 );
}
public void testNaNContracts() {
SummaryStatistics u = createSummaryStatistics();
assertTrue("mean not NaN",Double.isNaN(u.getMean()));
assertTrue("min not NaN",Double.isNaN(u.getMin()));
assertTrue("std dev not NaN",Double.isNaN(u.getStandardDeviation()));
assertTrue("var not NaN",Double.isNaN(u.getVariance()));
assertTrue("geom mean not NaN",Double.isNaN(u.getGeometricMean()));
u.addValue(1.0);
assertEquals( "mean not expected", 1.0,
u.getMean(), Double.MIN_VALUE);
assertEquals( "variance not expected", 0.0,
u.getVariance(), Double.MIN_VALUE);
assertEquals( "geometric mean not expected", 1.0,
u.getGeometricMean(), Double.MIN_VALUE);
u.addValue(-1.0);
assertTrue("geom mean not NaN",Double.isNaN(u.getGeometricMean()));
u.addValue(0.0);
assertTrue("geom mean not NaN",Double.isNaN(u.getGeometricMean()));
//FiXME: test all other NaN contract specs
}
public void testGetSummary() {
SummaryStatistics u = createSummaryStatistics();
StatisticalSummary summary = u.getSummary();
verifySummary(u, summary);
u.addValue(1d);
summary = u.getSummary();
verifySummary(u, summary);
u.addValue(2d);
summary = u.getSummary();
verifySummary(u, summary);
u.addValue(2d);
summary = u.getSummary();
verifySummary(u, summary);
}
public void testSerialization() {
SummaryStatistics u = createSummaryStatistics();
// Empty test
TestUtils.checkSerializedEquality(u);
SummaryStatistics s = (SummaryStatistics) TestUtils.serializeAndRecover(u);
StatisticalSummary summary = s.getSummary();
verifySummary(u, summary);
// Add some data
u.addValue(2d);
u.addValue(1d);
u.addValue(3d);
u.addValue(4d);
u.addValue(5d);
// Test again
TestUtils.checkSerializedEquality(u);
s = (SummaryStatistics) TestUtils.serializeAndRecover(u);
summary = s.getSummary();
verifySummary(u, summary);
}
public void testEqualsAndHashCode() {
SummaryStatistics u = createSummaryStatistics();
SummaryStatistics t = null;
int emptyHash = u.hashCode();
assertTrue("reflexive", u.equals(u));
assertFalse("non-null compared to null", u.equals(t));
assertFalse("wrong type", u.equals(new Double(0)));
t = createSummaryStatistics();
assertTrue("empty instances should be equal", t.equals(u));
assertTrue("empty instances should be equal", u.equals(t));
assertEquals("empty hash code", emptyHash, t.hashCode());
// Add some data to u
u.addValue(2d);
u.addValue(1d);
u.addValue(3d);
u.addValue(4d);
assertFalse("different n's should make instances not equal", t.equals(u));
assertFalse("different n's should make instances not equal", u.equals(t));
assertTrue("different n's should make hashcodes different",
u.hashCode() != t.hashCode());
//Add data in same order to t
t.addValue(2d);
t.addValue(1d);
t.addValue(3d);
t.addValue(4d);
assertTrue("summaries based on same data should be equal", t.equals(u));
assertTrue("summaries based on same data should be equal", u.equals(t));
assertEquals("summaries based on same data should have same hashcodes",
u.hashCode(), t.hashCode());
// Clear and make sure summaries are indistinguishable from empty summary
u.clear();
t.clear();
assertTrue("empty instances should be equal", t.equals(u));
assertTrue("empty instances should be equal", u.equals(t));
assertEquals("empty hash code", emptyHash, t.hashCode());
assertEquals("empty hash code", emptyHash, u.hashCode());
}
private void verifySummary(SummaryStatistics u, StatisticalSummary s) {
assertEquals("N",s.getN(),u.getN());
TestUtils.assertEquals("sum",s.getSum(),u.getSum(),tolerance);
TestUtils.assertEquals("var",s.getVariance(),u.getVariance(),tolerance);
TestUtils.assertEquals("std",s.getStandardDeviation(),u.getStandardDeviation(),tolerance);
TestUtils.assertEquals("mean",s.getMean(),u.getMean(),tolerance);
TestUtils.assertEquals("min",s.getMin(),u.getMin(),tolerance);
TestUtils.assertEquals("max",s.getMax(),u.getMax(),tolerance);
}
}

View File

@ -25,7 +25,7 @@ import junit.framework.TestSuite;
* @version $Revision$ $Date$
*/
public final class SummaryStatisticsImplTest extends SummaryStatisticsAbstractTest {
public final class SummaryStatisticsImplTest extends SummaryStatisticsTest {
public SummaryStatisticsImplTest(String name) {
super(name);

View File

@ -18,34 +18,225 @@ package org.apache.commons.math.stat.descriptive;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.apache.commons.math.TestUtils;
import org.apache.commons.math.stat.descriptive.moment.Mean;
import org.apache.commons.math.stat.descriptive.summary.Sum;
/**
* Test cases for the {@link SummaryStatistics} class.
* When SummaryStatisticsImpl is removed in math 2.0, test cases from
* SummaryStatisticsImplTest should be merged into this class.
*
* @version $Revision: 566833 $ $Date: 2007-08-16 13:36:33 -0700 (Thu, 16 Aug 2007) $
*/
public final class SummaryStatisticsTest extends SummaryStatisticsAbstractTest {
public class SummaryStatisticsTest extends TestCase {
private double one = 1;
private float twoF = 2;
private long twoL = 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 SummaryStatisticsTest(String name) {
super(name);
}
public static Test suite() {
TestSuite suite = new TestSuite(SummaryStatisticsTest.class);
suite.setName("SummaryStatistics tests");
return suite;
}
public SummaryStatisticsTest(String name) {
super(name);
}
protected SummaryStatistics createSummaryStatistics() {
return new SummaryStatistics();
}
/** test stats */
public void testStats() {
SummaryStatistics u = createSummaryStatistics();
assertEquals("total count",0,u.getN(),tolerance);
u.addValue(one);
u.addValue(twoF);
u.addValue(twoL);
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 {
SummaryStatistics u = createSummaryStatistics();
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() ) );
/* n=1 */
u.addValue(one);
assertTrue("mean should be one (n = 1)",
u.getMean() == one);
assertTrue("geometric should be one (n = 1) instead it is " + u.getGeometricMean(),
u.getGeometricMean() == one);
assertTrue("Std should be zero (n = 1)",
u.getStandardDeviation() == 0.0);
assertTrue("variance should be zero (n = 1)",
u.getVariance() == 0.0);
/* n=2 */
u.addValue(twoF);
assertTrue("Std should not be zero (n = 2)",
u.getStandardDeviation() != 0.0);
assertTrue("variance should not be zero (n = 2)",
u.getVariance() != 0.0);
}
public void testProductAndGeometricMean() throws Exception {
SummaryStatistics u = createSummaryStatistics();
u.addValue( 1.0 );
u.addValue( 2.0 );
u.addValue( 3.0 );
u.addValue( 4.0 );
assertEquals( "Geometric mean not expected", 2.213364,
u.getGeometricMean(), 0.00001 );
}
public void testNaNContracts() {
SummaryStatistics u = createSummaryStatistics();
assertTrue("mean not NaN",Double.isNaN(u.getMean()));
assertTrue("min not NaN",Double.isNaN(u.getMin()));
assertTrue("std dev not NaN",Double.isNaN(u.getStandardDeviation()));
assertTrue("var not NaN",Double.isNaN(u.getVariance()));
assertTrue("geom mean not NaN",Double.isNaN(u.getGeometricMean()));
u.addValue(1.0);
assertEquals( "mean not expected", 1.0,
u.getMean(), Double.MIN_VALUE);
assertEquals( "variance not expected", 0.0,
u.getVariance(), Double.MIN_VALUE);
assertEquals( "geometric mean not expected", 1.0,
u.getGeometricMean(), Double.MIN_VALUE);
u.addValue(-1.0);
assertTrue("geom mean not NaN",Double.isNaN(u.getGeometricMean()));
u.addValue(0.0);
assertTrue("geom mean not NaN",Double.isNaN(u.getGeometricMean()));
//FiXME: test all other NaN contract specs
}
public void testGetSummary() {
SummaryStatistics u = createSummaryStatistics();
StatisticalSummary summary = u.getSummary();
verifySummary(u, summary);
u.addValue(1d);
summary = u.getSummary();
verifySummary(u, summary);
u.addValue(2d);
summary = u.getSummary();
verifySummary(u, summary);
u.addValue(2d);
summary = u.getSummary();
verifySummary(u, summary);
}
public void testSerialization() {
SummaryStatistics u = createSummaryStatistics();
// Empty test
TestUtils.checkSerializedEquality(u);
SummaryStatistics s = (SummaryStatistics) TestUtils.serializeAndRecover(u);
StatisticalSummary summary = s.getSummary();
verifySummary(u, summary);
// Add some data
u.addValue(2d);
u.addValue(1d);
u.addValue(3d);
u.addValue(4d);
u.addValue(5d);
// Test again
TestUtils.checkSerializedEquality(u);
s = (SummaryStatistics) TestUtils.serializeAndRecover(u);
summary = s.getSummary();
verifySummary(u, summary);
}
public void testEqualsAndHashCode() {
SummaryStatistics u = createSummaryStatistics();
SummaryStatistics t = null;
int emptyHash = u.hashCode();
assertTrue("reflexive", u.equals(u));
assertFalse("non-null compared to null", u.equals(t));
assertFalse("wrong type", u.equals(new Double(0)));
t = createSummaryStatistics();
assertTrue("empty instances should be equal", t.equals(u));
assertTrue("empty instances should be equal", u.equals(t));
assertEquals("empty hash code", emptyHash, t.hashCode());
// Add some data to u
u.addValue(2d);
u.addValue(1d);
u.addValue(3d);
u.addValue(4d);
assertFalse("different n's should make instances not equal", t.equals(u));
assertFalse("different n's should make instances not equal", u.equals(t));
assertTrue("different n's should make hashcodes different",
u.hashCode() != t.hashCode());
//Add data in same order to t
t.addValue(2d);
t.addValue(1d);
t.addValue(3d);
t.addValue(4d);
assertTrue("summaries based on same data should be equal", t.equals(u));
assertTrue("summaries based on same data should be equal", u.equals(t));
assertEquals("summaries based on same data should have same hashcodes",
u.hashCode(), t.hashCode());
// Clear and make sure summaries are indistinguishable from empty summary
u.clear();
t.clear();
assertTrue("empty instances should be equal", t.equals(u));
assertTrue("empty instances should be equal", u.equals(t));
assertEquals("empty hash code", emptyHash, t.hashCode());
assertEquals("empty hash code", emptyHash, u.hashCode());
}
private void verifySummary(SummaryStatistics u, StatisticalSummary s) {
assertEquals("N",s.getN(),u.getN());
TestUtils.assertEquals("sum",s.getSum(),u.getSum(),tolerance);
TestUtils.assertEquals("var",s.getVariance(),u.getVariance(),tolerance);
TestUtils.assertEquals("std",s.getStandardDeviation(),u.getStandardDeviation(),tolerance);
TestUtils.assertEquals("mean",s.getMean(),u.getMean(),tolerance);
TestUtils.assertEquals("min",s.getMin(),u.getMin(),tolerance);
TestUtils.assertEquals("max",s.getMax(),u.getMax(),tolerance);
}
public void testSetterInjection() throws Exception {
SummaryStatistics u = createSummaryStatistics();
u.setMeanImpl(new Sum());

View File

@ -21,7 +21,7 @@ import junit.framework.TestSuite;
* @version $Revision$ $Date: 2007-08-16 15:36:33 -0500 (Thu, 16 Aug
* 2007) $
*/
public final class SynchronizedDescriptiveStatisticsTest extends DescriptiveStatisticsAbstractTest {
public final class SynchronizedDescriptiveStatisticsTest extends DescriptiveStatisticsTest {
public SynchronizedDescriptiveStatisticsTest(String name) {
super(name);

View File

@ -21,7 +21,7 @@ import junit.framework.TestSuite;
* @version $Revision: 592121 $ $Date: 2007-08-16 15:36:33 -0500 (Thu, 16 Aug
* 2007) $
*/
public final class SynchronizedSummaryStatisticsTest extends SummaryStatisticsAbstractTest {
public final class SynchronizedSummaryStatisticsTest extends SummaryStatisticsTest {
public SynchronizedSummaryStatisticsTest(String name) {
super(name);