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.random.RandomData;
023: import org.apache.commons.math.random.RandomDataImpl;
024:
025: /**
026: * Test cases for the {@link Univariate} class.
027: *
028: * @version $Revision: 201916 $ $Date: 2005-06-26 15:25:41 -0700 (Sun, 26 Jun 2005) $
029: */
030:
031: public final class DescriptiveStatisticsImplTest extends TestCase {
032: private double one = 1;
033: private float two = 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 skewness = 0;
044: private double kurtosis = 0.5;
045: private double tolerance = 10E-15;
046:
047: public DescriptiveStatisticsImplTest(String name) {
048: super (name);
049: }
050:
051: public void setUp() {
052: }
053:
054: public static Test suite() {
055: TestSuite suite = new TestSuite(
056: DescriptiveStatisticsImplTest.class);
057: suite.setName("DescriptiveStatistics Tests");
058: return suite;
059: }
060:
061: /** test stats */
062: public void testStats() {
063: DescriptiveStatistics u = DescriptiveStatistics.newInstance();
064: assertEquals("total count", 0, u.getN(), tolerance);
065: u.addValue(one);
066: u.addValue(two);
067: u.addValue(two);
068: u.addValue(three);
069: assertEquals("N", n, u.getN(), tolerance);
070: assertEquals("sum", sum, u.getSum(), tolerance);
071: assertEquals("sumsq", sumSq, u.getSumsq(), tolerance);
072: assertEquals("var", var, u.getVariance(), tolerance);
073: assertEquals("std", std, u.getStandardDeviation(), tolerance);
074: assertEquals("mean", mean, u.getMean(), tolerance);
075: assertEquals("min", min, u.getMin(), tolerance);
076: assertEquals("max", max, u.getMax(), tolerance);
077: u.clear();
078: assertEquals("total count", 0, u.getN(), tolerance);
079: }
080:
081: public void testN0andN1Conditions() throws Exception {
082: DescriptiveStatistics u = DescriptiveStatistics.newInstance();
083:
084: assertTrue("Mean of n = 0 set should be NaN", Double.isNaN(u
085: .getMean()));
086: assertTrue("Standard Deviation of n = 0 set should be NaN",
087: Double.isNaN(u.getStandardDeviation()));
088: assertTrue("Variance of n = 0 set should be NaN", Double
089: .isNaN(u.getVariance()));
090:
091: u.addValue(one);
092:
093: assertTrue(
094: "Mean of n = 1 set should be value of single item n1",
095: u.getMean() == one);
096: assertTrue(
097: "StdDev of n = 1 set should be zero, instead it is: "
098: + u.getStandardDeviation(), u
099: .getStandardDeviation() == 0);
100: assertTrue("Variance of n = 1 set should be zero", u
101: .getVariance() == 0);
102: }
103:
104: public void testSkewAndKurtosis() {
105: DescriptiveStatistics u = DescriptiveStatistics.newInstance();
106:
107: double[] testArray = { 12.5, 12, 11.8, 14.2, 14.9, 14.5, 21,
108: 8.2, 10.3, 11.3, 14.1, 9.9, 12.2, 12, 12.1, 11, 19.8,
109: 11, 10, 8.8, 9, 12.3 };
110: for (int i = 0; i < testArray.length; i++) {
111: u.addValue(testArray[i]);
112: }
113:
114: assertEquals("mean", 12.40455, u.getMean(), 0.0001);
115: assertEquals("variance", 10.00236, u.getVariance(), 0.0001);
116: assertEquals("skewness", 1.437424, u.getSkewness(), 0.0001);
117: assertEquals("kurtosis", 2.37719, u.getKurtosis(), 0.0001);
118: }
119:
120: public void testProductAndGeometricMean() throws Exception {
121: DescriptiveStatistics u = DescriptiveStatistics.newInstance();
122: u.setWindowSize(10);
123:
124: u.addValue(1.0);
125: u.addValue(2.0);
126: u.addValue(3.0);
127: u.addValue(4.0);
128:
129: //assertEquals( "Product not expected",
130: // 24.0, u.getProduct(), Double.MIN_VALUE );
131: assertEquals("Geometric mean not expected", 2.213364, u
132: .getGeometricMean(), 0.00001);
133:
134: // Now test rolling - StorelessDescriptiveStatistics should discount the contribution
135: // of a discarded element
136: for (int i = 0; i < 10; i++) {
137: u.addValue(i + 2);
138: }
139: // Values should be (2,3,4,5,6,7,8,9,10,11)
140:
141: //assertEquals( "Product not expected", 39916800.0,
142: // u.getProduct(), 0.00001 );
143: assertEquals("Geometric mean not expected", 5.755931, u
144: .getGeometricMean(), 0.00001);
145: }
146:
147: public void testGetSortedValues() {
148: double[] test1 = { 5, 4, 3, 2, 1 };
149: double[] test2 = { 5, 2, 1, 3, 4, 0 };
150: double[] test3 = { 1 };
151: int[] testi = null;
152: double[] test4 = null;
153: RandomData rd = new RandomDataImpl();
154: tstGetSortedValues(test1);
155: tstGetSortedValues(test2);
156: tstGetSortedValues(test3);
157: for (int i = 0; i < 10; i++) {
158: testi = rd.nextPermutation(10, 6);
159: test4 = new double[6];
160: for (int j = 0; j < testi.length; j++) {
161: test4[j] = (double) testi[j];
162: }
163: tstGetSortedValues(test4);
164: }
165: for (int i = 0; i < 10; i++) {
166: testi = rd.nextPermutation(10, 5);
167: test4 = new double[5];
168: for (int j = 0; j < testi.length; j++) {
169: test4[j] = (double) testi[j];
170: }
171: tstGetSortedValues(test4);
172: }
173: }
174:
175: private void tstGetSortedValues(double[] test) {
176: DescriptiveStatistics u = DescriptiveStatistics.newInstance();
177: for (int i = 0; i < test.length; i++) {
178: u.addValue(test[i]);
179: }
180: double[] sorted = u.getSortedValues();
181: if (sorted.length != test.length) {
182: fail("wrong length for sorted values array");
183: }
184: for (int i = 0; i < sorted.length - 1; i++) {
185: if (sorted[i] > sorted[i + 1]) {
186: fail("sorted values out of sequence");
187: }
188: }
189: }
190:
191: public void testPercentiles() {
192: double[] test = { 5, 4, 3, 2, 1 };
193: DescriptiveStatistics u = DescriptiveStatistics.newInstance();
194: for (int i = 0; i < test.length; i++) {
195: u.addValue(test[i]);
196: }
197: assertEquals("expecting min", 1, u.getPercentile(5), 10E-12);
198: assertEquals("expecting max", 5, u.getPercentile(99), 10E-12);
199: assertEquals("expecting middle", 3, u.getPercentile(50), 10E-12);
200: try {
201: double x = u.getPercentile(0);
202: fail("expecting IllegalArgumentException for getPercentile(0)");
203: } catch (IllegalArgumentException ex) {
204: ;
205: }
206: try {
207: double x = u.getPercentile(120);
208: fail("expecting IllegalArgumentException for getPercentile(120)");
209: } catch (IllegalArgumentException ex) {
210: ;
211: }
212:
213: u.clear();
214: double[] test2 = { 1, 2, 3, 4 };
215: for (int i = 0; i < test2.length; i++) {
216: u.addValue(test2[i]);
217: }
218: assertEquals("Q1", 1.25, u.getPercentile(25), 10E-12);
219: assertEquals("Q3", 3.75, u.getPercentile(75), 10E-12);
220: assertEquals("Q2", 2.5, u.getPercentile(50), 10E-12);
221:
222: u.clear();
223: double[] test3 = { 1 };
224: for (int i = 0; i < test3.length; i++) {
225: u.addValue(test3[i]);
226: }
227: assertEquals("Q1", 1, u.getPercentile(25), 10E-12);
228: assertEquals("Q3", 1, u.getPercentile(75), 10E-12);
229: assertEquals("Q2", 1, u.getPercentile(50), 10E-12);
230:
231: u.clear();
232: RandomData rd = new RandomDataImpl();
233: int[] testi = rd.nextPermutation(100, 100); // will contain 0-99
234: for (int j = 0; j < testi.length; j++) {
235: u.addValue((double) testi[j]); //OK, laugh at me for the cast
236: }
237: for (int i = 1; i < 100; i++) {
238: assertEquals("percentile " + i, (double) i - 1 + (double) i
239: * (.01), u.getPercentile(i), 10E-12);
240: }
241:
242: u.clear();
243: double[] test4 = { 1, 2, 3, 4, 100 };
244: for (int i = 0; i < test4.length; i++) {
245: u.addValue(test4[i]);
246: }
247: assertEquals("80th", 80.8, u.getPercentile(80), 10E-12);
248:
249: u.clear();
250: assertTrue("empty value set should return NaN", Double.isNaN(u
251: .getPercentile(50)));
252: }
253:
254: }
|