StatisticsTest.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. /* ===========================================================
  2. * JFreeChart : a free chart library for the Java(tm) platform
  3. * ===========================================================
  4. *
  5. * (C) Copyright 2000-2013, by Object Refinery Limited and Contributors.
  6. *
  7. * Project Info: http://www.jfree.org/jfreechart/index.html
  8. *
  9. * This library is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU Lesser General Public License as published by
  11. * the Free Software Foundation; either version 2.1 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  16. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  17. * License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  22. * USA.
  23. *
  24. * [Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  25. * Other names may be trademarks of their respective owners.]
  26. *
  27. * -------------------
  28. * StatisticsTest.java
  29. * -------------------
  30. * (C) Copyright 2004-2013, by Object Refinery Limited and Contributors.
  31. *
  32. * Original Author: David Gilbert (for Object Refinery Limited);
  33. * Contributor(s): -;
  34. *
  35. * Changes
  36. * -------
  37. * 25-Mar-2004 : Version 1 (DG);
  38. * 04-Oct-2004 : Eliminated NumberUtils usage (DG);
  39. *
  40. */
  41. package org.jfree.data.statistics;
  42. import static org.junit.Assert.assertEquals;
  43. import static org.junit.Assert.assertTrue;
  44. import java.util.ArrayList;
  45. import java.util.Collection;
  46. import java.util.Collections;
  47. import java.util.List;
  48. import org.junit.Test;
  49. /**
  50. * Tests for the {@link Statistics} class.
  51. */
  52. public class StatisticsTest {
  53. /**
  54. * Some checks for the calculateMean(Number[]) and
  55. * calculateMean(Number[], boolean) methods.
  56. */
  57. @Test
  58. public void testCalculateMean_Array() {
  59. // try null array
  60. boolean pass = false;
  61. try {
  62. Statistics.calculateMean((Number[]) null);
  63. }
  64. catch (IllegalArgumentException e) {
  65. pass = true;
  66. }
  67. assertTrue(pass);
  68. pass = false;
  69. try {
  70. Statistics.calculateMean((Number[]) null, false);
  71. }
  72. catch (IllegalArgumentException e) {
  73. pass = true;
  74. }
  75. assertTrue(pass);
  76. // try an array containing no items
  77. assertTrue(Double.isNaN(Statistics.calculateMean(new Number[0])));
  78. assertTrue(Double.isNaN(Statistics.calculateMean(new Number[0],
  79. false)));
  80. // try an array containing a single Number
  81. Number[] values = new Number[] {new Double(1.0)};
  82. assertEquals(1.0, Statistics.calculateMean(values), EPSILON);
  83. assertEquals(1.0, Statistics.calculateMean(values, true), EPSILON);
  84. assertEquals(1.0, Statistics.calculateMean(values, false), EPSILON);
  85. // try an array containing a single Number and a null
  86. values = new Number[] {new Double(1.0), null};
  87. assertTrue(Double.isNaN(Statistics.calculateMean(values)));
  88. assertTrue(Double.isNaN(Statistics.calculateMean(values, true)));
  89. assertEquals(1.0, Statistics.calculateMean(values, false), EPSILON);
  90. // try an array containing a single Number and a NaN
  91. values = new Number[] {new Double(1.0), new Double(Double.NaN)};
  92. assertTrue(Double.isNaN(Statistics.calculateMean(values)));
  93. assertTrue(Double.isNaN(Statistics.calculateMean(values, true)));
  94. assertEquals(1.0, Statistics.calculateMean(values, false), EPSILON);
  95. }
  96. /**
  97. * Some checks for the calculateMean(Collection) and
  98. * calculateMean(Collection, boolean) methods.
  99. */
  100. @Test
  101. public void testCalculateMean_Collection() {
  102. // try a null collection
  103. boolean pass = false;
  104. try {
  105. Statistics.calculateMean((Collection) null);
  106. }
  107. catch (IllegalArgumentException e) {
  108. pass = true;
  109. }
  110. assertTrue(pass);
  111. pass = false;
  112. try {
  113. Statistics.calculateMean((Collection) null, false);
  114. }
  115. catch (IllegalArgumentException e) {
  116. pass = true;
  117. }
  118. assertTrue(pass);
  119. // try an empty collection
  120. List values = new ArrayList();
  121. assertTrue(Double.isNaN(Statistics.calculateMean(values)));
  122. assertTrue(Double.isNaN(Statistics.calculateMean(values, true)));
  123. assertTrue(Double.isNaN(Statistics.calculateMean(values, false)));
  124. // try a collection with a single number
  125. values.add(new Double(9.0));
  126. assertEquals(9.0, Statistics.calculateMean(values), EPSILON);
  127. assertEquals(9.0, Statistics.calculateMean(values, true), EPSILON);
  128. assertEquals(9.0, Statistics.calculateMean(values, false), EPSILON);
  129. // try a collection with a single number plus a null
  130. values.add(null);
  131. assertTrue(Double.isNaN(Statistics.calculateMean(values)));
  132. assertTrue(Double.isNaN(Statistics.calculateMean(values, true)));
  133. assertEquals(9.0, Statistics.calculateMean(values, false), EPSILON);
  134. // try a collection with a single number plus a NaN
  135. values.clear();
  136. values.add(new Double(9.0));
  137. values.add(new Double(Double.NaN));
  138. assertTrue(Double.isNaN(Statistics.calculateMean(values)));
  139. assertTrue(Double.isNaN(Statistics.calculateMean(values, true)));
  140. assertEquals(9.0, Statistics.calculateMean(values, false), EPSILON);
  141. // try a collection with several numbers
  142. values = new ArrayList();
  143. values.add(new Double(9.0));
  144. values.add(new Double(3.0));
  145. values.add(new Double(2.0));
  146. values.add(new Double(2.0));
  147. double mean = Statistics.calculateMean(values);
  148. assertEquals(4.0, mean, EPSILON);
  149. // a Collection containing a NaN will return Double.NaN for the result
  150. values.add(new Double(Double.NaN));
  151. assertTrue(Double.isNaN(Statistics.calculateMean(values)));
  152. }
  153. static final double EPSILON = 0.0000000001;
  154. /**
  155. * Some checks for the calculateMedian(List, boolean) method.
  156. */
  157. @Test
  158. public void testCalculateMedian() {
  159. // check null list
  160. assertTrue(Double.isNaN(Statistics.calculateMedian(null, false)));
  161. assertTrue(Double.isNaN(Statistics.calculateMedian(null, true)));
  162. // check empty list
  163. List list = new ArrayList();
  164. assertTrue(Double.isNaN(Statistics.calculateMedian(list, false)));
  165. assertTrue(Double.isNaN(Statistics.calculateMedian(list, true)));
  166. // check list containing null
  167. list.add(null);
  168. boolean pass = false;
  169. try {
  170. Statistics.calculateMedian(list, false);
  171. }
  172. catch (NullPointerException e) {
  173. pass = true;
  174. }
  175. assertTrue(pass);
  176. pass = false;
  177. try {
  178. Statistics.calculateMedian(list, true);
  179. }
  180. catch (NullPointerException e) {
  181. pass = true;
  182. }
  183. assertTrue(pass);
  184. // check a list containing a non-Number object
  185. list.clear();
  186. list.add("Not a number");
  187. pass = false;
  188. try {
  189. Statistics.calculateMedian(list, false);
  190. }
  191. catch (ClassCastException e) {
  192. pass = true;
  193. }
  194. assertTrue(pass);
  195. pass = false;
  196. try {
  197. Statistics.calculateMedian(list, true);
  198. }
  199. catch (ClassCastException e) {
  200. pass = true;
  201. }
  202. assertTrue(pass);
  203. }
  204. /**
  205. * A test for the calculateMedian() method.
  206. */
  207. @Test
  208. public void testCalculateMedian1() {
  209. List values = new ArrayList();
  210. values.add(new Double(1.0));
  211. double median = Statistics.calculateMedian(values);
  212. assertEquals(1.0, median, 0.0000001);
  213. }
  214. /**
  215. * A test for the calculateMedian() method.
  216. */
  217. @Test
  218. public void testCalculateMedian2() {
  219. List values = new ArrayList();
  220. values.add(new Double(2.0));
  221. values.add(new Double(1.0));
  222. double median = Statistics.calculateMedian(values);
  223. assertEquals(1.5, median, 0.0000001);
  224. }
  225. /**
  226. * A test for the calculateMedian() method.
  227. */
  228. @Test
  229. public void testCalculateMedian3() {
  230. List values = new ArrayList();
  231. values.add(new Double(1.0));
  232. values.add(new Double(2.0));
  233. values.add(new Double(3.0));
  234. values.add(new Double(6.0));
  235. values.add(new Double(5.0));
  236. values.add(new Double(4.0));
  237. double median = Statistics.calculateMedian(values);
  238. assertEquals(3.5, median, 0.0000001);
  239. }
  240. /**
  241. * A test for the calculateMedian() method.
  242. */
  243. @Test
  244. public void testCalculateMedian4() {
  245. List values = new ArrayList();
  246. values.add(new Double(7.0));
  247. values.add(new Double(2.0));
  248. values.add(new Double(3.0));
  249. values.add(new Double(5.0));
  250. values.add(new Double(4.0));
  251. values.add(new Double(6.0));
  252. values.add(new Double(1.0));
  253. double median = Statistics.calculateMedian(values);
  254. assertEquals(4.0, median, 0.0000001);
  255. }
  256. /**
  257. * A test using some real data that caused a problem at one point.
  258. */
  259. @Test
  260. public void testCalculateMedian5() {
  261. List values = new ArrayList();
  262. values.add(new Double(11.228692993861783));
  263. values.add(new Double(11.30823353859889));
  264. values.add(new Double(11.75312904769314));
  265. values.add(new Double(11.825102897465314));
  266. values.add(new Double(10.184252778401783));
  267. values.add(new Double(12.207951828057766));
  268. values.add(new Double(10.68841994040566));
  269. values.add(new Double(12.099522004479438));
  270. values.add(new Double(11.508874945056881));
  271. values.add(new Double(12.052517729558513));
  272. values.add(new Double(12.401481645578734));
  273. values.add(new Double(12.185377793028543));
  274. values.add(new Double(10.666372951930315));
  275. values.add(new Double(11.680978041499548));
  276. values.add(new Double(11.06528277406718));
  277. values.add(new Double(11.36876492904596));
  278. values.add(new Double(11.927565516175939));
  279. values.add(new Double(11.39307785978655));
  280. values.add(new Double(11.989603679523857));
  281. values.add(new Double(12.009834360354864));
  282. values.add(new Double(10.653351822461559));
  283. values.add(new Double(11.851776254376754));
  284. values.add(new Double(11.045441544755946));
  285. values.add(new Double(11.993674040560624));
  286. values.add(new Double(12.898219965238944));
  287. values.add(new Double(11.97095782819647));
  288. values.add(new Double(11.73234406745488));
  289. values.add(new Double(11.649006017243991));
  290. values.add(new Double(12.20549704915365));
  291. values.add(new Double(11.799723639384919));
  292. values.add(new Double(11.896208658005628));
  293. values.add(new Double(12.164149111823424));
  294. values.add(new Double(12.042795103513766));
  295. values.add(new Double(12.114839532596426));
  296. values.add(new Double(12.166609097075824));
  297. values.add(new Double(12.183017546225935));
  298. values.add(new Double(11.622009125845342));
  299. values.add(new Double(11.289365786738633));
  300. values.add(new Double(12.462984323671568));
  301. values.add(new Double(11.573494921030598));
  302. values.add(new Double(10.862867940485804));
  303. values.add(new Double(12.018186939664872));
  304. values.add(new Double(10.418046849313018));
  305. values.add(new Double(11.326344465881341));
  306. double median = Statistics.calculateMedian(values, true);
  307. assertEquals(11.812413268425116, median, 0.000001);
  308. Collections.sort(values);
  309. double median2 = Statistics.calculateMedian(values, false);
  310. assertEquals(11.812413268425116, median2, 0.000001);
  311. }
  312. /**
  313. * A test for the calculateMedian() method.
  314. */
  315. @Test
  316. public void testCalculateMedian6() {
  317. List values = new ArrayList();
  318. values.add(new Double(7.0));
  319. values.add(new Double(2.0));
  320. values.add(new Double(3.0));
  321. values.add(new Double(5.0));
  322. values.add(new Double(4.0));
  323. values.add(new Double(6.0));
  324. values.add(new Double(1.0));
  325. double median = Statistics.calculateMedian(values, 0, 2);
  326. assertEquals(3.0, median, 0.0000001);
  327. }
  328. /**
  329. * A simple test for the correlation calculation.
  330. */
  331. @Test
  332. public void testCorrelation1() {
  333. Number[] data1 = new Number[3];
  334. data1[0] = new Double(1);
  335. data1[1] = new Double(2);
  336. data1[2] = new Double(3);
  337. Number[] data2 = new Number[3];
  338. data2[0] = new Double(1);
  339. data2[1] = new Double(2);
  340. data2[2] = new Double(3);
  341. double r = Statistics.getCorrelation(data1, data2);
  342. assertEquals(1.0, r, 0.00000001);
  343. }
  344. /**
  345. * A simple test for the correlation calculation.
  346. *
  347. * http://trochim.human.cornell.edu/kb/statcorr.htm
  348. */
  349. @Test
  350. public void testCorrelation2() {
  351. Number[] data1 = new Number[20];
  352. data1[0] = new Double(68);
  353. data1[1] = new Double(71);
  354. data1[2] = new Double(62);
  355. data1[3] = new Double(75);
  356. data1[4] = new Double(58);
  357. data1[5] = new Double(60);
  358. data1[6] = new Double(67);
  359. data1[7] = new Double(68);
  360. data1[8] = new Double(71);
  361. data1[9] = new Double(69);
  362. data1[10] = new Double(68);
  363. data1[11] = new Double(67);
  364. data1[12] = new Double(63);
  365. data1[13] = new Double(62);
  366. data1[14] = new Double(60);
  367. data1[15] = new Double(63);
  368. data1[16] = new Double(65);
  369. data1[17] = new Double(67);
  370. data1[18] = new Double(63);
  371. data1[19] = new Double(61);
  372. Number[] data2 = new Number[20];
  373. data2[0] = new Double(4.1);
  374. data2[1] = new Double(4.6);
  375. data2[2] = new Double(3.8);
  376. data2[3] = new Double(4.4);
  377. data2[4] = new Double(3.2);
  378. data2[5] = new Double(3.1);
  379. data2[6] = new Double(3.8);
  380. data2[7] = new Double(4.1);
  381. data2[8] = new Double(4.3);
  382. data2[9] = new Double(3.7);
  383. data2[10] = new Double(3.5);
  384. data2[11] = new Double(3.2);
  385. data2[12] = new Double(3.7);
  386. data2[13] = new Double(3.3);
  387. data2[14] = new Double(3.4);
  388. data2[15] = new Double(4.0);
  389. data2[16] = new Double(4.1);
  390. data2[17] = new Double(3.8);
  391. data2[18] = new Double(3.4);
  392. data2[19] = new Double(3.6);
  393. double r = Statistics.getCorrelation(data1, data2);
  394. assertEquals(0.7306356862792885, r, 0.000000000001);
  395. }
  396. /**
  397. * Some checks for the getStdDev() method.
  398. */
  399. @Test
  400. public void testGetStdDev() {
  401. // try null argument
  402. boolean pass = false;
  403. try {
  404. Statistics.getStdDev(null);
  405. }
  406. catch (IllegalArgumentException e) {
  407. pass = true;
  408. }
  409. assertTrue(pass);
  410. // try zero length array
  411. pass = false;
  412. try {
  413. Statistics.getStdDev(new Double[0]);
  414. }
  415. catch (IllegalArgumentException e) {
  416. pass = true;
  417. }
  418. assertTrue(pass);
  419. // try single value
  420. assertTrue(Double.isNaN(Statistics.getStdDev(new Double[]
  421. {new Double(1.0)})));
  422. }
  423. }