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