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 java.util.ArrayList;
019: import java.util.List;
020:
021: import org.apache.commons.math.TestUtils;
022:
023: import junit.framework.Test;
024: import junit.framework.TestCase;
025: import junit.framework.TestSuite;
026:
027: /**
028: * Test cases for the {@link Univariate} class.
029: *
030: * @version $Revision: 201916 $ $Date: 2005-06-26 15:25:41 -0700 (Sun, 26 Jun 2005) $
031: */
032:
033: public final class ListUnivariateImplTest extends TestCase {
034:
035: private double one = 1;
036: private float two = 2;
037: private int three = 3;
038:
039: private double mean = 2;
040: private double sumSq = 18;
041: private double sum = 8;
042: private double var = 0.666666666666666666667;
043: private double std = Math.sqrt(var);
044: private double n = 4;
045: private double min = 1;
046: private double max = 3;
047: private double skewness = 0;
048: private double kurtosis = 0.5;
049: private double tolerance = 10E-15;
050:
051: public ListUnivariateImplTest(String name) {
052: super (name);
053: }
054:
055: public void setUp() {
056: }
057:
058: public static Test suite() {
059: TestSuite suite = new TestSuite(ListUnivariateImplTest.class);
060: suite.setName("Frequency Tests");
061: return suite;
062: }
063:
064: /** test stats */
065: public void testStats() {
066: List externalList = new ArrayList();
067:
068: DescriptiveStatistics u = new ListUnivariateImpl(externalList);
069:
070: assertEquals("total count", 0, u.getN(), tolerance);
071: u.addValue(one);
072: u.addValue(two);
073: u.addValue(two);
074: u.addValue(three);
075: assertEquals("N", n, u.getN(), tolerance);
076: assertEquals("sum", sum, u.getSum(), tolerance);
077: assertEquals("sumsq", sumSq, u.getSumsq(), tolerance);
078: assertEquals("var", var, u.getVariance(), tolerance);
079: assertEquals("std", std, u.getStandardDeviation(), tolerance);
080: assertEquals("mean", mean, u.getMean(), tolerance);
081: assertEquals("min", min, u.getMin(), tolerance);
082: assertEquals("max", max, u.getMax(), tolerance);
083: u.clear();
084: assertEquals("total count", 0, u.getN(), tolerance);
085: }
086:
087: public void testN0andN1Conditions() throws Exception {
088: List list = new ArrayList();
089:
090: DescriptiveStatistics u = new ListUnivariateImpl(list);
091:
092: assertTrue("Mean of n = 0 set should be NaN", Double.isNaN(u
093: .getMean()));
094: assertTrue("Standard Deviation of n = 0 set should be NaN",
095: Double.isNaN(u.getStandardDeviation()));
096: assertTrue("Variance of n = 0 set should be NaN", Double
097: .isNaN(u.getVariance()));
098:
099: list.add(new Double(one));
100:
101: assertTrue(
102: "Mean of n = 1 set should be value of single item n1",
103: u.getMean() == one);
104: assertTrue(
105: "StdDev of n = 1 set should be zero, instead it is: "
106: + u.getStandardDeviation(), u
107: .getStandardDeviation() == 0);
108: assertTrue("Variance of n = 1 set should be zero", u
109: .getVariance() == 0);
110: }
111:
112: public void testSkewAndKurtosis() {
113: DescriptiveStatistics u = DescriptiveStatistics.newInstance();
114:
115: double[] testArray = { 12.5, 12, 11.8, 14.2, 14.9, 14.5, 21,
116: 8.2, 10.3, 11.3, 14.1, 9.9, 12.2, 12, 12.1, 11, 19.8,
117: 11, 10, 8.8, 9, 12.3 };
118: for (int i = 0; i < testArray.length; i++) {
119: u.addValue(testArray[i]);
120: }
121:
122: assertEquals("mean", 12.40455, u.getMean(), 0.0001);
123: assertEquals("variance", 10.00236, u.getVariance(), 0.0001);
124: assertEquals("skewness", 1.437424, u.getSkewness(), 0.0001);
125: assertEquals("kurtosis", 2.37719, u.getKurtosis(), 0.0001);
126: }
127:
128: public void testProductAndGeometricMean() throws Exception {
129: ListUnivariateImpl u = new ListUnivariateImpl(new ArrayList());
130: u.setWindowSize(10);
131:
132: u.addValue(1.0);
133: u.addValue(2.0);
134: u.addValue(3.0);
135: u.addValue(4.0);
136:
137: assertEquals("Geometric mean not expected", 2.213364, u
138: .getGeometricMean(), 0.00001);
139:
140: // Now test rolling - StorelessDescriptiveStatistics should discount the contribution
141: // of a discarded element
142: for (int i = 0; i < 10; i++) {
143: u.addValue(i + 2);
144: }
145: // Values should be (2,3,4,5,6,7,8,9,10,11)
146:
147: assertEquals("Geometric mean not expected", 5.755931, u
148: .getGeometricMean(), 0.00001);
149:
150: }
151:
152: /** test stats */
153: public void testSerialization() {
154:
155: DescriptiveStatistics u = null;
156:
157: try {
158: u = DescriptiveStatistics
159: .newInstance(ListUnivariateImpl.class);
160: } catch (InstantiationException e) {
161: fail(e.getMessage());
162: } catch (IllegalAccessException e) {
163: fail(e.getMessage());
164: }
165:
166: assertEquals("total count", 0, u.getN(), tolerance);
167: u.addValue(one);
168: u.addValue(two);
169:
170: DescriptiveStatistics u2 = (DescriptiveStatistics) TestUtils
171: .serializeAndRecover(u);
172:
173: u2.addValue(two);
174: u2.addValue(three);
175:
176: assertEquals("N", n, u2.getN(), tolerance);
177: assertEquals("sum", sum, u2.getSum(), tolerance);
178: assertEquals("sumsq", sumSq, u2.getSumsq(), tolerance);
179: assertEquals("var", var, u2.getVariance(), tolerance);
180: assertEquals("std", std, u2.getStandardDeviation(), tolerance);
181: assertEquals("mean", mean, u2.getMean(), tolerance);
182: assertEquals("min", min, u2.getMin(), tolerance);
183: assertEquals("max", max, u2.getMax(), tolerance);
184:
185: u2.clear();
186: assertEquals("total count", 0, u2.getN(), tolerance);
187: }
188: }
|