PageRenderTime 23ms CodeModel.GetById 48ms RepoModel.GetById 2ms app.codeStats 0ms

/commons-math3-3.0-src/src/test/java/org/apache/commons/math3/random/EmpiricalDistributionTest.java

#
Java | 279 lines | 185 code | 30 blank | 64 comment | 10 complexity | 0e2ae494769674b6520770d7010ac5e7 MD5 | raw file
Possible License(s): Apache-2.0
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.commons.math3.random;
  18. import java.io.BufferedReader;
  19. import java.io.File;
  20. import java.io.IOException;
  21. import java.io.InputStreamReader;
  22. import java.net.URL;
  23. import java.util.ArrayList;
  24. import org.apache.commons.math3.TestUtils;
  25. import org.apache.commons.math3.exception.NullArgumentException;
  26. import org.apache.commons.math3.stat.descriptive.SummaryStatistics;
  27. import org.junit.Assert;
  28. import org.junit.Before;
  29. import org.junit.Test;
  30. /**
  31. * Test cases for the EmpiricalDistribution class
  32. *
  33. * @version $Id: EmpiricalDistributionTest.java 1244107 2012-02-14 16:17:55Z erans $
  34. */
  35. public final class EmpiricalDistributionTest {
  36. protected EmpiricalDistribution empiricalDistribution = null;
  37. protected EmpiricalDistribution empiricalDistribution2 = null;
  38. protected File file = null;
  39. protected URL url = null;
  40. protected double[] dataArray = null;
  41. @Before
  42. public void setUp() throws IOException {
  43. empiricalDistribution = new EmpiricalDistribution(100);
  44. url = getClass().getResource("testData.txt");
  45. empiricalDistribution2 = new EmpiricalDistribution(100);
  46. BufferedReader in =
  47. new BufferedReader(new InputStreamReader(
  48. url.openStream()));
  49. String str = null;
  50. ArrayList<Double> list = new ArrayList<Double>();
  51. while ((str = in.readLine()) != null) {
  52. list.add(Double.valueOf(str));
  53. }
  54. in.close();
  55. in = null;
  56. dataArray = new double[list.size()];
  57. int i = 0;
  58. for (Double data : list) {
  59. dataArray[i] = data.doubleValue();
  60. i++;
  61. }
  62. }
  63. /**
  64. * Test EmpiricalDistrbution.load() using sample data file.<br>
  65. * Check that the sampleCount, mu and sigma match data in
  66. * the sample data file.
  67. */
  68. @Test
  69. public void testLoad() throws Exception {
  70. empiricalDistribution.load(url);
  71. // testData File has 10000 values, with mean ~ 5.0, std dev ~ 1
  72. // Make sure that loaded distribution matches this
  73. Assert.assertEquals(empiricalDistribution.getSampleStats().getN(),1000,10E-7);
  74. //TODO: replace with statistical tests
  75. Assert.assertEquals(empiricalDistribution.getSampleStats().getMean(),
  76. 5.069831575018909,10E-7);
  77. Assert.assertEquals(empiricalDistribution.getSampleStats().getStandardDeviation(),
  78. 1.0173699343977738,10E-7);
  79. }
  80. /**
  81. * Test EmpiricalDistrbution.load(double[]) using data taken from
  82. * sample data file.<br>
  83. * Check that the sampleCount, mu and sigma match data in
  84. * the sample data file.
  85. */
  86. @Test
  87. public void testDoubleLoad() throws Exception {
  88. empiricalDistribution2.load(dataArray);
  89. // testData File has 10000 values, with mean ~ 5.0, std dev ~ 1
  90. // Make sure that loaded distribution matches this
  91. Assert.assertEquals(empiricalDistribution2.getSampleStats().getN(),1000,10E-7);
  92. //TODO: replace with statistical tests
  93. Assert.assertEquals(empiricalDistribution2.getSampleStats().getMean(),
  94. 5.069831575018909,10E-7);
  95. Assert.assertEquals(empiricalDistribution2.getSampleStats().getStandardDeviation(),
  96. 1.0173699343977738,10E-7);
  97. double[] bounds = empiricalDistribution2.getGeneratorUpperBounds();
  98. Assert.assertEquals(bounds.length, 100);
  99. Assert.assertEquals(bounds[99], 1.0, 10e-12);
  100. }
  101. /**
  102. * Generate 1000 random values and make sure they look OK.<br>
  103. * Note that there is a non-zero (but very small) probability that
  104. * these tests will fail even if the code is working as designed.
  105. */
  106. @Test
  107. public void testNext() throws Exception {
  108. tstGen(0.1);
  109. tstDoubleGen(0.1);
  110. }
  111. /**
  112. * Make sure exception thrown if digest getNext is attempted
  113. * before loading empiricalDistribution.
  114. */
  115. @Test
  116. public void testNexFail() {
  117. try {
  118. empiricalDistribution.getNextValue();
  119. empiricalDistribution2.getNextValue();
  120. Assert.fail("Expecting IllegalStateException");
  121. } catch (IllegalStateException ex) {
  122. // expected
  123. }
  124. }
  125. /**
  126. * Make sure we can handle a grid size that is too fine
  127. */
  128. @Test
  129. public void testGridTooFine() throws Exception {
  130. empiricalDistribution = new EmpiricalDistribution(1001);
  131. tstGen(0.1);
  132. empiricalDistribution2 = new EmpiricalDistribution(1001);
  133. tstDoubleGen(0.1);
  134. }
  135. /**
  136. * How about too fat?
  137. */
  138. @Test
  139. public void testGridTooFat() throws Exception {
  140. empiricalDistribution = new EmpiricalDistribution(1);
  141. tstGen(5); // ridiculous tolerance; but ridiculous grid size
  142. // really just checking to make sure we do not bomb
  143. empiricalDistribution2 = new EmpiricalDistribution(1);
  144. tstDoubleGen(5);
  145. }
  146. /**
  147. * Test bin index overflow problem (BZ 36450)
  148. */
  149. @Test
  150. public void testBinIndexOverflow() throws Exception {
  151. double[] x = new double[] {9474.94326071674, 2080107.8865462579};
  152. new EmpiricalDistribution().load(x);
  153. }
  154. @Test
  155. public void testSerialization() {
  156. // Empty
  157. EmpiricalDistribution dist = new EmpiricalDistribution();
  158. EmpiricalDistribution dist2 = (EmpiricalDistribution) TestUtils.serializeAndRecover(dist);
  159. verifySame(dist, dist2);
  160. // Loaded
  161. empiricalDistribution2.load(dataArray);
  162. dist2 = (EmpiricalDistribution) TestUtils.serializeAndRecover(empiricalDistribution2);
  163. verifySame(empiricalDistribution2, dist2);
  164. }
  165. @Test(expected=NullArgumentException.class)
  166. public void testLoadNullDoubleArray() {
  167. new EmpiricalDistribution().load((double[]) null);
  168. }
  169. @Test(expected=NullArgumentException.class)
  170. public void testLoadNullURL() throws Exception {
  171. new EmpiricalDistribution().load((URL) null);
  172. }
  173. @Test(expected=NullArgumentException.class)
  174. public void testLoadNullFile() throws Exception {
  175. new EmpiricalDistribution().load((File) null);
  176. }
  177. /**
  178. * MATH-298
  179. */
  180. @Test
  181. public void testGetBinUpperBounds() {
  182. double[] testData = {0, 1, 1, 2, 3, 4, 4, 5, 6, 7, 8, 9, 10};
  183. EmpiricalDistribution dist = new EmpiricalDistribution(5);
  184. dist.load(testData);
  185. double[] expectedBinUpperBounds = {2, 4, 6, 8, 10};
  186. double[] expectedGeneratorUpperBounds = {4d/13d, 7d/13d, 9d/13d, 11d/13d, 1};
  187. double tol = 10E-12;
  188. TestUtils.assertEquals(expectedBinUpperBounds, dist.getUpperBounds(), tol);
  189. TestUtils.assertEquals(expectedGeneratorUpperBounds, dist.getGeneratorUpperBounds(), tol);
  190. }
  191. @Test
  192. public void testGeneratorConfig() {
  193. double[] testData = {0, 1, 2, 3, 4};
  194. RandomGenerator generator = new RandomAdaptorTest.ConstantGenerator(0.5);
  195. EmpiricalDistribution dist = new EmpiricalDistribution(5, generator);
  196. dist.load(testData);
  197. for (int i = 0; i < 5; i++) {
  198. Assert.assertEquals(2.0, dist.getNextValue(), 0d);
  199. }
  200. // Verify no NPE with null generator argument
  201. dist = new EmpiricalDistribution(5, (RandomGenerator) null);
  202. dist.load(testData);
  203. dist.getNextValue();
  204. }
  205. @Test
  206. public void testReSeed() throws Exception {
  207. empiricalDistribution.load(url);
  208. empiricalDistribution.reSeed(100);
  209. final double [] values = new double[10];
  210. for (int i = 0; i < 10; i++) {
  211. values[i] = empiricalDistribution.getNextValue();
  212. }
  213. empiricalDistribution.reSeed(100);
  214. for (int i = 0; i < 10; i++) {
  215. Assert.assertEquals(values[i],empiricalDistribution.getNextValue(), 0d);
  216. }
  217. }
  218. private void verifySame(EmpiricalDistribution d1, EmpiricalDistribution d2) {
  219. Assert.assertEquals(d1.isLoaded(), d2.isLoaded());
  220. Assert.assertEquals(d1.getBinCount(), d2.getBinCount());
  221. Assert.assertEquals(d1.getSampleStats(), d2.getSampleStats());
  222. if (d1.isLoaded()) {
  223. for (int i = 0; i < d1.getUpperBounds().length; i++) {
  224. Assert.assertEquals(d1.getUpperBounds()[i], d2.getUpperBounds()[i], 0);
  225. }
  226. Assert.assertEquals(d1.getBinStats(), d2.getBinStats());
  227. }
  228. }
  229. private void tstGen(double tolerance)throws Exception {
  230. empiricalDistribution.load(url);
  231. empiricalDistribution.reSeed(1000);
  232. SummaryStatistics stats = new SummaryStatistics();
  233. for (int i = 1; i < 1000; i++) {
  234. stats.addValue(empiricalDistribution.getNextValue());
  235. }
  236. Assert.assertEquals("mean", 5.069831575018909, stats.getMean(),tolerance);
  237. Assert.assertEquals("std dev", 1.0173699343977738, stats.getStandardDeviation(),tolerance);
  238. }
  239. private void tstDoubleGen(double tolerance)throws Exception {
  240. empiricalDistribution2.load(dataArray);
  241. empiricalDistribution2.reSeed(1000);
  242. SummaryStatistics stats = new SummaryStatistics();
  243. for (int i = 1; i < 1000; i++) {
  244. stats.addValue(empiricalDistribution2.getNextValue());
  245. }
  246. Assert.assertEquals("mean", 5.069831575018909, stats.getMean(), tolerance);
  247. Assert.assertEquals("std dev", 1.0173699343977738, stats.getStandardDeviation(), tolerance);
  248. }
  249. }