001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2006, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jfreechart/index.html
008: *
009: * This library is free software; you can redistribute it and/or modify it
010: * under the terms of the GNU Lesser General Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but
015: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017: * License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022: * USA.
023: *
024: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * --------------------
028: * StatisticsTests.java
029: * --------------------
030: * (C) Copyright 2004-2006, by Object Refinery Limited and Contributors.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: StatisticsTests.java,v 1.1.2.2 2006/11/16 11:19:47 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 25-Mar-2004 : Version 1 (DG);
040: * 04-Oct-2004 : Eliminated NumberUtils usage (DG);
041: *
042: */
043:
044: package org.jfree.data.statistics.junit;
045:
046: import java.util.ArrayList;
047: import java.util.Collection;
048: import java.util.Collections;
049: import java.util.List;
050:
051: import junit.framework.Test;
052: import junit.framework.TestCase;
053: import junit.framework.TestSuite;
054:
055: import org.jfree.data.statistics.Statistics;
056:
057: /**
058: * Tests for the {@link Statistics} class.
059: */
060: public class StatisticsTests extends TestCase {
061:
062: /**
063: * Returns the tests as a test suite.
064: *
065: * @return The test suite.
066: */
067: public static Test suite() {
068: return new TestSuite(StatisticsTests.class);
069: }
070:
071: /**
072: * Constructs a new set of tests.
073: *
074: * @param name the name of the tests.
075: */
076: public StatisticsTests(String name) {
077: super (name);
078: }
079:
080: /**
081: * Some checks for the calculateMean(Number[]) and
082: * calculateMean(Number[], boolean) methods.
083: */
084: public void testCalculateMean_Array() {
085:
086: // try null array
087: boolean pass = false;
088: try {
089: Statistics.calculateMean((Number[]) null);
090: } catch (IllegalArgumentException e) {
091: pass = true;
092: }
093: assertTrue(pass);
094:
095: pass = false;
096: try {
097: Statistics.calculateMean((Number[]) null, false);
098: } catch (IllegalArgumentException e) {
099: pass = true;
100: }
101: assertTrue(pass);
102:
103: // try an array containing no items
104: assertTrue(Double
105: .isNaN(Statistics.calculateMean(new Number[0])));
106: assertTrue(Double.isNaN(Statistics.calculateMean(new Number[0],
107: false)));
108:
109: // try an array containing a single Number
110: Number[] values = new Number[] { new Double(1.0) };
111: assertEquals(1.0, Statistics.calculateMean(values), EPSILON);
112: assertEquals(1.0, Statistics.calculateMean(values, true),
113: EPSILON);
114: assertEquals(1.0, Statistics.calculateMean(values, false),
115: EPSILON);
116:
117: // try an array containing a single Number and a null
118: values = new Number[] { new Double(1.0), null };
119: assertTrue(Double.isNaN(Statistics.calculateMean(values)));
120: assertTrue(Double.isNaN(Statistics.calculateMean(values, true)));
121: assertEquals(1.0, Statistics.calculateMean(values, false),
122: EPSILON);
123:
124: // try an array containing a single Number and a NaN
125: values = new Number[] { new Double(1.0), new Double(Double.NaN) };
126: assertTrue(Double.isNaN(Statistics.calculateMean(values)));
127: assertTrue(Double.isNaN(Statistics.calculateMean(values, true)));
128: assertEquals(1.0, Statistics.calculateMean(values, false),
129: EPSILON);
130: }
131:
132: /**
133: * Some checks for the calculateMean(Collection) and
134: * calculateMean(Collection, boolean) methods.
135: */
136: public void testCalculateMean_Collection() {
137:
138: // try a null collection
139: boolean pass = false;
140: try {
141: Statistics.calculateMean((Collection) null);
142: } catch (IllegalArgumentException e) {
143: pass = true;
144: }
145: assertTrue(pass);
146:
147: pass = false;
148: try {
149: Statistics.calculateMean((Collection) null, false);
150: } catch (IllegalArgumentException e) {
151: pass = true;
152: }
153: assertTrue(pass);
154:
155: // try an empty collection
156: List values = new ArrayList();
157: assertTrue(Double.isNaN(Statistics.calculateMean(values)));
158: assertTrue(Double.isNaN(Statistics.calculateMean(values, true)));
159: assertTrue(Double
160: .isNaN(Statistics.calculateMean(values, false)));
161:
162: // try a collection with a single number
163: values.add(new Double(9.0));
164: assertEquals(9.0, Statistics.calculateMean(values), EPSILON);
165: assertEquals(9.0, Statistics.calculateMean(values, true),
166: EPSILON);
167: assertEquals(9.0, Statistics.calculateMean(values, false),
168: EPSILON);
169:
170: // try a collection with a single number plus a null
171: values.add(null);
172: assertTrue(Double.isNaN(Statistics.calculateMean(values)));
173: assertTrue(Double.isNaN(Statistics.calculateMean(values, true)));
174: assertEquals(9.0, Statistics.calculateMean(values, false),
175: EPSILON);
176:
177: // try a collection with a single number plus a NaN
178: values.clear();
179: values.add(new Double(9.0));
180: values.add(new Double(Double.NaN));
181: assertTrue(Double.isNaN(Statistics.calculateMean(values)));
182: assertTrue(Double.isNaN(Statistics.calculateMean(values, true)));
183: assertEquals(9.0, Statistics.calculateMean(values, false),
184: EPSILON);
185:
186: // try a collection with several numbers
187: values = new ArrayList();
188: values.add(new Double(9.0));
189: values.add(new Double(3.0));
190: values.add(new Double(2.0));
191: values.add(new Double(2.0));
192: double mean = Statistics.calculateMean(values);
193: assertEquals(4.0, mean, EPSILON);
194:
195: // a Collection containing a NaN will return Double.NaN for the result
196: values.add(new Double(Double.NaN));
197: assertTrue(Double.isNaN(Statistics.calculateMean(values)));
198: }
199:
200: static final double EPSILON = 0.0000000001;
201:
202: /**
203: * Some checks for the calculateMedian(List, boolean) method.
204: */
205: public void testCalculateMedian() {
206:
207: // check null list
208: assertTrue(Double
209: .isNaN(Statistics.calculateMedian(null, false)));
210: assertTrue(Double.isNaN(Statistics.calculateMedian(null, true)));
211:
212: // check empty list
213: List list = new ArrayList();
214: assertTrue(Double
215: .isNaN(Statistics.calculateMedian(list, false)));
216: assertTrue(Double.isNaN(Statistics.calculateMedian(list, true)));
217:
218: // check list containing null
219: list.add(null);
220: boolean pass = false;
221: try {
222: Statistics.calculateMedian(list, false);
223: } catch (NullPointerException e) {
224: pass = true;
225: }
226: assertTrue(pass);
227:
228: pass = false;
229: try {
230: Statistics.calculateMedian(list, true);
231: } catch (NullPointerException e) {
232: pass = true;
233: }
234: assertTrue(pass);
235:
236: // check a list containing a non-Number object
237: list.clear();
238: list.add("Not a number");
239: pass = false;
240: try {
241: Statistics.calculateMedian(list, false);
242: } catch (ClassCastException e) {
243: pass = true;
244: }
245: assertTrue(pass);
246:
247: pass = false;
248: try {
249: Statistics.calculateMedian(list, true);
250: } catch (ClassCastException e) {
251: pass = true;
252: }
253: assertTrue(pass);
254:
255: }
256:
257: /**
258: * A test for the calculateMedian() method.
259: */
260: public void testCalculateMedian1() {
261: List values = new ArrayList();
262: values.add(new Double(1.0));
263: double median = Statistics.calculateMedian(values);
264: assertEquals(1.0, median, 0.0000001);
265: }
266:
267: /**
268: * A test for the calculateMedian() method.
269: */
270: public void testCalculateMedian2() {
271: List values = new ArrayList();
272: values.add(new Double(2.0));
273: values.add(new Double(1.0));
274: double median = Statistics.calculateMedian(values);
275: assertEquals(1.5, median, 0.0000001);
276: }
277:
278: /**
279: * A test for the calculateMedian() method.
280: */
281: public void testCalculateMedian3() {
282: List values = new ArrayList();
283: values.add(new Double(1.0));
284: values.add(new Double(2.0));
285: values.add(new Double(3.0));
286: values.add(new Double(6.0));
287: values.add(new Double(5.0));
288: values.add(new Double(4.0));
289: double median = Statistics.calculateMedian(values);
290: assertEquals(3.5, median, 0.0000001);
291: }
292:
293: /**
294: * A test for the calculateMedian() method.
295: */
296: public void testCalculateMedian4() {
297: List values = new ArrayList();
298: values.add(new Double(7.0));
299: values.add(new Double(2.0));
300: values.add(new Double(3.0));
301: values.add(new Double(5.0));
302: values.add(new Double(4.0));
303: values.add(new Double(6.0));
304: values.add(new Double(1.0));
305: double median = Statistics.calculateMedian(values);
306: assertEquals(4.0, median, 0.0000001);
307: }
308:
309: /**
310: * A test using some real data that caused a problem at one point.
311: */
312: public void testCalculateMedian5() {
313: List values = new ArrayList();
314: values.add(new Double(11.228692993861783));
315: values.add(new Double(11.30823353859889));
316: values.add(new Double(11.75312904769314));
317: values.add(new Double(11.825102897465314));
318: values.add(new Double(10.184252778401783));
319: values.add(new Double(12.207951828057766));
320: values.add(new Double(10.68841994040566));
321: values.add(new Double(12.099522004479438));
322: values.add(new Double(11.508874945056881));
323: values.add(new Double(12.052517729558513));
324: values.add(new Double(12.401481645578734));
325: values.add(new Double(12.185377793028543));
326: values.add(new Double(10.666372951930315));
327: values.add(new Double(11.680978041499548));
328: values.add(new Double(11.06528277406718));
329: values.add(new Double(11.36876492904596));
330: values.add(new Double(11.927565516175939));
331: values.add(new Double(11.39307785978655));
332: values.add(new Double(11.989603679523857));
333: values.add(new Double(12.009834360354864));
334: values.add(new Double(10.653351822461559));
335: values.add(new Double(11.851776254376754));
336: values.add(new Double(11.045441544755946));
337: values.add(new Double(11.993674040560624));
338: values.add(new Double(12.898219965238944));
339: values.add(new Double(11.97095782819647));
340: values.add(new Double(11.73234406745488));
341: values.add(new Double(11.649006017243991));
342: values.add(new Double(12.20549704915365));
343: values.add(new Double(11.799723639384919));
344: values.add(new Double(11.896208658005628));
345: values.add(new Double(12.164149111823424));
346: values.add(new Double(12.042795103513766));
347: values.add(new Double(12.114839532596426));
348: values.add(new Double(12.166609097075824));
349: values.add(new Double(12.183017546225935));
350: values.add(new Double(11.622009125845342));
351: values.add(new Double(11.289365786738633));
352: values.add(new Double(12.462984323671568));
353: values.add(new Double(11.573494921030598));
354: values.add(new Double(10.862867940485804));
355: values.add(new Double(12.018186939664872));
356: values.add(new Double(10.418046849313018));
357: values.add(new Double(11.326344465881341));
358: double median = Statistics.calculateMedian(values, true);
359: assertEquals(11.812413268425116, median, 0.000001);
360: Collections.sort(values);
361: double median2 = Statistics.calculateMedian(values, false);
362: assertEquals(11.812413268425116, median2, 0.000001);
363: }
364:
365: /**
366: * A test for the calculateMedian() method.
367: */
368: public void testCalculateMedian6() {
369: List values = new ArrayList();
370: values.add(new Double(7.0));
371: values.add(new Double(2.0));
372: values.add(new Double(3.0));
373: values.add(new Double(5.0));
374: values.add(new Double(4.0));
375: values.add(new Double(6.0));
376: values.add(new Double(1.0));
377: double median = Statistics.calculateMedian(values, 0, 2);
378: assertEquals(3.0, median, 0.0000001);
379: }
380:
381: /**
382: * A simple test for the correlation calculation.
383: */
384: public void testCorrelation1() {
385: Number[] data1 = new Number[3];
386: data1[0] = new Double(1);
387: data1[1] = new Double(2);
388: data1[2] = new Double(3);
389: Number[] data2 = new Number[3];
390: data2[0] = new Double(1);
391: data2[1] = new Double(2);
392: data2[2] = new Double(3);
393: double r = Statistics.getCorrelation(data1, data2);
394: assertEquals(1.0, r, 0.00000001);
395: }
396:
397: /**
398: * A simple test for the correlation calculation.
399: *
400: * http://trochim.human.cornell.edu/kb/statcorr.htm
401: */
402: public void testCorrelation2() {
403: Number[] data1 = new Number[20];
404: data1[0] = new Double(68);
405: data1[1] = new Double(71);
406: data1[2] = new Double(62);
407: data1[3] = new Double(75);
408: data1[4] = new Double(58);
409: data1[5] = new Double(60);
410: data1[6] = new Double(67);
411: data1[7] = new Double(68);
412: data1[8] = new Double(71);
413: data1[9] = new Double(69);
414: data1[10] = new Double(68);
415: data1[11] = new Double(67);
416: data1[12] = new Double(63);
417: data1[13] = new Double(62);
418: data1[14] = new Double(60);
419: data1[15] = new Double(63);
420: data1[16] = new Double(65);
421: data1[17] = new Double(67);
422: data1[18] = new Double(63);
423: data1[19] = new Double(61);
424: Number[] data2 = new Number[20];
425: data2[0] = new Double(4.1);
426: data2[1] = new Double(4.6);
427: data2[2] = new Double(3.8);
428: data2[3] = new Double(4.4);
429: data2[4] = new Double(3.2);
430: data2[5] = new Double(3.1);
431: data2[6] = new Double(3.8);
432: data2[7] = new Double(4.1);
433: data2[8] = new Double(4.3);
434: data2[9] = new Double(3.7);
435: data2[10] = new Double(3.5);
436: data2[11] = new Double(3.2);
437: data2[12] = new Double(3.7);
438: data2[13] = new Double(3.3);
439: data2[14] = new Double(3.4);
440: data2[15] = new Double(4.0);
441: data2[16] = new Double(4.1);
442: data2[17] = new Double(3.8);
443: data2[18] = new Double(3.4);
444: data2[19] = new Double(3.6);
445: double r = Statistics.getCorrelation(data1, data2);
446: assertEquals(0.7306356862792885, r, 0.000000000001);
447: }
448:
449: /**
450: * Some checks for the getStdDev() method.
451: */
452: public void testGetStdDev() {
453:
454: // try null argument
455: boolean pass = false;
456: try {
457: Statistics.getStdDev(null);
458: } catch (IllegalArgumentException e) {
459: pass = true;
460: }
461: assertTrue(pass);
462:
463: // try zero length array
464: pass = false;
465: try {
466: Statistics.getStdDev(new Double[0]);
467: } catch (IllegalArgumentException e) {
468: pass = true;
469: }
470: assertTrue(pass);
471:
472: // try single value
473: assertTrue(Double.isNaN(Statistics
474: .getStdDev(new Double[] { new Double(1.0) })));
475: }
476:
477: }
|