View Javadoc
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    *      https://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  
18  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  package org.hipparchus.distribution.discrete;
23  
24  import static org.junit.Assert.assertEquals;
25  
26  import java.util.HashMap;
27  import java.util.List;
28  import java.util.Map;
29  
30  import org.hipparchus.exception.MathIllegalArgumentException;
31  import org.hipparchus.exception.MathRuntimeException;
32  import org.hipparchus.util.Pair;
33  import org.junit.Assert;
34  import org.junit.Test;
35  
36  /**
37   * Test class for {@link EnumeratedIntegerDistribution}.
38   */
39  public class EnumeratedIntegerDistributionTest {
40  
41      /**
42       * The distribution object used for testing.
43       */
44      private final EnumeratedIntegerDistribution testDistribution;
45  
46      /**
47       * Creates the default distribution object used for testing.
48       */
49      public EnumeratedIntegerDistributionTest() {
50          // Non-sorted singleton array with duplicates should be allowed.
51          // Values with zero-probability do not extend the support.
52          testDistribution = new EnumeratedIntegerDistribution(
53                  new int[]{3, -1, 3, 7, -2, 8},
54                  new double[]{0.2, 0.2, 0.3, 0.3, 0.0, 0.0});
55      }
56  
57      /**
58       * Tests if the EnumeratedIntegerDistribution constructor throws
59       * exceptions for invalid data.
60       */
61      @Test
62      public void testExceptions() {
63          EnumeratedIntegerDistribution invalid = null;
64          try {
65              new EnumeratedIntegerDistribution(new int[]{1, 2}, new double[]{0.0});
66              Assert.fail("Expected MathIllegalArgumentException");
67          } catch (MathIllegalArgumentException e) {
68          }
69          try {
70              new EnumeratedIntegerDistribution(new int[]{1, 2}, new double[]{0.0, -1.0});
71              Assert.fail("Expected MathIllegalArgumentException");
72          } catch (MathIllegalArgumentException e) {
73          }
74          try {
75              new EnumeratedIntegerDistribution(new int[]{1, 2}, new double[]{0.0, 0.0});
76              Assert.fail("Expected MathRuntimeException");
77          } catch (MathRuntimeException e) {
78          }
79          try {
80            new EnumeratedIntegerDistribution(new int[]{1, 2}, new double[]{0.0, Double.NaN});
81              Assert.fail("Expected MathIllegalArgumentException");
82          } catch (MathIllegalArgumentException e) {
83          }
84          try {
85          new EnumeratedIntegerDistribution(new int[]{1, 2}, new double[]{0.0, Double.POSITIVE_INFINITY});
86              Assert.fail("Expected NotFiniteNumberException");
87          } catch (MathIllegalArgumentException e) {
88          }
89          Assert.assertNull("Expected non-initialized DiscreteRealDistribution", invalid);
90      }
91  
92      /**
93       * Tests if the distribution returns proper probability values.
94       */
95      @Test
96      public void testProbability() {
97          int[] points = new int[]{-2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8};
98          double[] results = new double[]{0, 0.2, 0, 0, 0, 0.5, 0, 0, 0, 0.3, 0};
99          for (int p = 0; p < points.length; p++) {
100             double probability = testDistribution.probability(points[p]);
101             Assert.assertEquals(results[p], probability, 0.0);
102         }
103     }
104 
105     /**
106      * Tests if the distribution returns proper cumulative probability values.
107      */
108     @Test
109     public void testCumulativeProbability() {
110         int[] points = new int[]{-2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8};
111         double[] results = new double[]{0, 0.2, 0.2, 0.2, 0.2, 0.7, 0.7, 0.7, 0.7, 1.0, 1.0};
112         for (int p = 0; p < points.length; p++) {
113             double probability = testDistribution.cumulativeProbability(points[p]);
114             Assert.assertEquals(results[p], probability, 1e-10);
115         }
116     }
117 
118     /**
119      * Tests if the distribution returns proper mean value.
120      */
121     @Test
122     public void testGetNumericalMean() {
123         Assert.assertEquals(3.4, testDistribution.getNumericalMean(), 1e-10);
124     }
125 
126     /**
127      * Tests if the distribution returns proper variance.
128      */
129     @Test
130     public void testGetNumericalVariance() {
131         Assert.assertEquals(7.84, testDistribution.getNumericalVariance(), 1e-10);
132     }
133 
134     /**
135      * Tests if the distribution returns proper lower bound.
136      */
137     @Test
138     public void testGetSupportLowerBound() {
139         Assert.assertEquals(-1, testDistribution.getSupportLowerBound());
140     }
141 
142     /**
143      * Tests if the distribution returns proper upper bound.
144      */
145     @Test
146     public void testGetSupportUpperBound() {
147         Assert.assertEquals(7, testDistribution.getSupportUpperBound());
148     }
149 
150     /**
151      * Tests if the distribution returns properly that the support is connected.
152      */
153     @Test
154     public void testIsSupportConnected() {
155         Assert.assertTrue(testDistribution.isSupportConnected());
156     }
157 
158     @Test
159     public void testCreateFromIntegers() {
160         final int[] data = new int[] {0, 1, 1, 2, 2, 2};
161         EnumeratedIntegerDistribution distribution = new EnumeratedIntegerDistribution(data);
162         assertEquals(0.5, distribution.probability(2), 0);
163         assertEquals(0.5, distribution.cumulativeProbability(1), 0);
164     }
165 
166     @Test
167     public void testGetPmf() {
168         final int[] values = new int[] {0,1,2,3,4};
169         final double[] masses = new double[] {0.2, 0.2, 0.4, 0.1, 0.1};
170         final EnumeratedIntegerDistribution distribution = new EnumeratedIntegerDistribution(values, masses);
171         final List<Pair<Integer, Double>> pmf = distribution.getPmf();
172         assertEquals(5, pmf.size());
173         final Map<Integer, Double> pmfMap = new HashMap<Integer, Double>();
174         for (int i = 0; i < 5; i++) {
175             pmfMap.put(i, masses[i]);
176         }
177         for (int i = 0; i < 5; i++) {
178             assertEquals(pmf.get(i).getSecond(), pmfMap.get(pmf.get(i).getFirst()), 0);
179         }
180     }
181 }