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