001: /*
002: * Copyright 2003-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.math.stat.descriptive;
017:
018: import junit.framework.Test;
019: import junit.framework.TestCase;
020: import junit.framework.TestSuite;
021:
022: import org.apache.commons.math.TestUtils;
023:
024: /**
025: * Test cases for the {@link DescriptiveStatistics} class.
026: *
027: * @version $Revision: 201916 $ $Date: 2005-06-26 15:25:41 -0700 (Sun, 26 Jun 2005) $
028: */
029:
030: public final class SummaryStatisticsImplTest extends TestCase {
031: private double one = 1;
032: private float twoF = 2;
033: private long twoL = 2;
034: private int three = 3;
035: private double mean = 2;
036: private double sumSq = 18;
037: private double sum = 8;
038: private double var = 0.666666666666666666667;
039: private double std = Math.sqrt(var);
040: private double n = 4;
041: private double min = 1;
042: private double max = 3;
043: private double tolerance = 10E-15;
044:
045: protected SummaryStatistics u = null;
046:
047: public SummaryStatisticsImplTest(String name) {
048: super (name);
049: }
050:
051: public void setUp() {
052: u = SummaryStatistics.newInstance();
053: }
054:
055: public static Test suite() {
056: TestSuite suite = new TestSuite(SummaryStatisticsImplTest.class);
057: suite.setName("Frequency Tests");
058: return suite;
059: }
060:
061: /** test stats */
062: public void testStats() {
063: assertEquals("total count", 0, u.getN(), tolerance);
064: u.addValue(one);
065: u.addValue(twoF);
066: u.addValue(twoL);
067: u.addValue(three);
068: assertEquals("N", n, u.getN(), tolerance);
069: assertEquals("sum", sum, u.getSum(), tolerance);
070: assertEquals("sumsq", sumSq, u.getSumsq(), tolerance);
071: assertEquals("var", var, u.getVariance(), tolerance);
072: assertEquals("std", std, u.getStandardDeviation(), tolerance);
073: assertEquals("mean", mean, u.getMean(), tolerance);
074: assertEquals("min", min, u.getMin(), tolerance);
075: assertEquals("max", max, u.getMax(), tolerance);
076: u.clear();
077: assertEquals("total count", 0, u.getN(), tolerance);
078: }
079:
080: public void testN0andN1Conditions() throws Exception {
081: assertTrue("Mean of n = 0 set should be NaN", Double.isNaN(u
082: .getMean()));
083: assertTrue("Standard Deviation of n = 0 set should be NaN",
084: Double.isNaN(u.getStandardDeviation()));
085: assertTrue("Variance of n = 0 set should be NaN", Double
086: .isNaN(u.getVariance()));
087:
088: /* n=1 */
089: u.addValue(one);
090: assertTrue("mean should be one (n = 1)", u.getMean() == one);
091: assertTrue("geometric should be one (n = 1) instead it is "
092: + u.getGeometricMean(), u.getGeometricMean() == one);
093: assertTrue("Std should be zero (n = 1)", u
094: .getStandardDeviation() == 0.0);
095: assertTrue("variance should be zero (n = 1)",
096: u.getVariance() == 0.0);
097:
098: /* n=2 */
099: u.addValue(twoF);
100: assertTrue("Std should not be zero (n = 2)", u
101: .getStandardDeviation() != 0.0);
102: assertTrue("variance should not be zero (n = 2)", u
103: .getVariance() != 0.0);
104:
105: }
106:
107: public void testProductAndGeometricMean() throws Exception {
108: u.addValue(1.0);
109: u.addValue(2.0);
110: u.addValue(3.0);
111: u.addValue(4.0);
112:
113: assertEquals("Geometric mean not expected", 2.213364, u
114: .getGeometricMean(), 0.00001);
115: }
116:
117: public void testNaNContracts() {
118: double nan = Double.NaN;
119: assertTrue("mean not NaN", Double.isNaN(u.getMean()));
120: assertTrue("min not NaN", Double.isNaN(u.getMin()));
121: assertTrue("std dev not NaN", Double.isNaN(u
122: .getStandardDeviation()));
123: assertTrue("var not NaN", Double.isNaN(u.getVariance()));
124: assertTrue("geom mean not NaN", Double.isNaN(u
125: .getGeometricMean()));
126:
127: u.addValue(1.0);
128:
129: assertEquals("mean not expected", 1.0, u.getMean(),
130: Double.MIN_VALUE);
131: assertEquals("variance not expected", 0.0, u.getVariance(),
132: Double.MIN_VALUE);
133: assertEquals("geometric mean not expected", 1.0, u
134: .getGeometricMean(), Double.MIN_VALUE);
135:
136: u.addValue(-1.0);
137:
138: assertTrue("geom mean not NaN", Double.isNaN(u
139: .getGeometricMean()));
140:
141: u.addValue(0.0);
142:
143: assertTrue("geom mean not NaN", Double.isNaN(u
144: .getGeometricMean()));
145:
146: //FiXME: test all other NaN contract specs
147: }
148:
149: public void testGetSummary() {
150: StatisticalSummary summary = u.getSummary();
151: verifySummary(summary);
152: u.addValue(1d);
153: summary = u.getSummary();
154: verifySummary(summary);
155: u.addValue(2d);
156: summary = u.getSummary();
157: verifySummary(summary);
158: u.addValue(2d);
159: summary = u.getSummary();
160: verifySummary(summary);
161: }
162:
163: public void testSerialization() {
164: // Empty test
165: TestUtils.checkSerializedEquality(u);
166: SummaryStatistics s = (SummaryStatistics) TestUtils
167: .serializeAndRecover(u);
168: StatisticalSummary summary = s.getSummary();
169: verifySummary(summary);
170:
171: // Add some data
172: u.addValue(2d);
173: u.addValue(1d);
174: u.addValue(3d);
175: u.addValue(4d);
176: u.addValue(5d);
177:
178: // Test again
179: TestUtils.checkSerializedEquality(u);
180: s = (SummaryStatistics) TestUtils.serializeAndRecover(u);
181: summary = s.getSummary();
182: verifySummary(summary);
183:
184: }
185:
186: public void testEqualsAndHashCode() {
187: SummaryStatistics t = null;
188: int emptyHash = u.hashCode();
189: assertTrue("reflexive", u.equals(u));
190: assertFalse("non-null compared to null", u.equals(t));
191: assertFalse("wrong type", u.equals(new Double(0)));
192: t = SummaryStatistics.newInstance();
193: assertTrue("empty instances should be equal", t.equals(u));
194: assertTrue("empty instances should be equal", u.equals(t));
195: assertEquals("empty hash code", emptyHash, t.hashCode());
196:
197: // Add some data to u
198: u.addValue(2d);
199: u.addValue(1d);
200: u.addValue(3d);
201: u.addValue(4d);
202: assertFalse("different n's should make instances not equal", t
203: .equals(u));
204: assertFalse("different n's should make instances not equal", u
205: .equals(t));
206: assertTrue("different n's should make hashcodes different", u
207: .hashCode() != t.hashCode());
208:
209: //Add data in different order to t, should not affect identity or hashcode
210: t.addValue(4d);
211: t.addValue(2d);
212: t.addValue(3d);
213: t.addValue(1d);
214: assertTrue("summaries based on same data should be equal", t
215: .equals(u));
216: assertTrue("summaries based on same data should be equal", u
217: .equals(t));
218: assertEquals(
219: "summaries based on same data should have same hashcodes",
220: u.hashCode(), t.hashCode());
221:
222: // Clear and make sure summaries are indistinguishable from empty summary
223: u.clear();
224: t.clear();
225: assertTrue("empty instances should be equal", t.equals(u));
226: assertTrue("empty instances should be equal", u.equals(t));
227: assertEquals("empty hash code", emptyHash, t.hashCode());
228: assertEquals("empty hash code", emptyHash, u.hashCode());
229: }
230:
231: private void verifySummary(StatisticalSummary s) {
232: assertEquals("N", s.getN(), u.getN());
233: TestUtils
234: .assertEquals("sum", s.getSum(), u.getSum(), tolerance);
235: TestUtils.assertEquals("var", s.getVariance(), u.getVariance(),
236: tolerance);
237: TestUtils.assertEquals("std", s.getStandardDeviation(), u
238: .getStandardDeviation(), tolerance);
239: TestUtils.assertEquals("mean", s.getMean(), u.getMean(),
240: tolerance);
241: TestUtils
242: .assertEquals("min", s.getMin(), u.getMin(), tolerance);
243: TestUtils
244: .assertEquals("max", s.getMax(), u.getMax(), tolerance);
245: }
246: }
|