View Javadoc
1   /*
2    * Licensed to the Hipparchus project 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 Hipparchus project 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  package org.hipparchus.distribution;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertTrue;
21  import static org.junit.Assert.fail;
22  
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import org.hipparchus.exception.MathIllegalArgumentException;
27  import org.hipparchus.util.MathArrays;
28  import org.hipparchus.util.Pair;
29  import org.junit.Test;
30  
31  public class EnumeratedDistributionTest {
32  
33      @Test
34      public void testCheckAndNormalizeBadArguments() {
35          double[] bad = new double[] {-1, 0, 1, 1};
36          try {
37              EnumeratedDistribution.checkAndNormalize(bad);
38              fail("Expecting IAE - negative probability");
39          } catch (MathIllegalArgumentException ex) {
40              // expected
41          }
42          bad = new double[] {0, Double.NaN, 1, 1};
43          try {
44              EnumeratedDistribution.checkAndNormalize(bad);
45              fail("Expecting IAE - NaN probability");
46          } catch (MathIllegalArgumentException ex) {
47              // expected
48          }
49          bad = new double[] {0, Double.POSITIVE_INFINITY, 1, 1};
50          try {
51              EnumeratedDistribution.checkAndNormalize(bad);
52              fail("Expecting IAE - infinite probability");
53          } catch (MathIllegalArgumentException ex) {
54              // expected
55          }
56          bad = new double[] {0, 0, 0, 0};
57          try {
58              EnumeratedDistribution.checkAndNormalize(bad);
59              fail("Expecting IAE - no positive probabilities");
60          } catch (MathIllegalArgumentException ex) {
61              // expected
62          }
63          bad = new double[] {};
64          try {
65              EnumeratedDistribution.checkAndNormalize(bad);
66              fail("Expecting IAE - empty probability array");
67          } catch (MathIllegalArgumentException ex) {
68              // expected
69          }
70          bad = null;
71          try {
72              EnumeratedDistribution.checkAndNormalize(bad);
73              fail("Expecting IAE - empty probability array");
74          } catch (MathIllegalArgumentException ex) {
75              // expected
76          }
77      }
78  
79      @Test
80      public void testCheckAndNormalize() {
81          double[] p = new double[] {0, 2, 2, 1};
82          double[] normP = EnumeratedDistribution.checkAndNormalize(p);
83          assertEquals(0, normP[0], 0);
84          assertEquals(0.4, normP[1], 0);
85          assertEquals(0.4, normP[2], 0);
86          assertEquals(0.2, normP[3], 0);
87          p = new double[] {0.2, 0.2, 0.4, 0.2};
88          assertTrue(MathArrays.equals(p, EnumeratedDistribution.checkAndNormalize(p)));
89      }
90  
91      @Test
92      public void testNullValues() {
93          final List<Pair<String, Double>> pmf = new ArrayList<>();
94          pmf.add(new Pair<>("a", 0.5));
95          pmf.add(new Pair<>(null, 0.5));
96          final EnumeratedDistribution<String> dist = new EnumeratedDistribution<>(pmf);
97          assertEquals(0.5, dist.probability(null), 0);
98          assertEquals(0.5, dist.probability("a"), 0);
99          assertEquals(0, dist.probability("b"), 0);
100     }
101 
102     @Test
103     public void testRepeatedValues() {
104         final List<Pair<String, Double>> pmf = new ArrayList<>();
105         pmf.add(new Pair<>("a", 0.5));
106         pmf.add(new Pair<>("a", 0.5));
107         pmf.add(new Pair<>("b", 0.0));
108         final EnumeratedDistribution<String> dist = new EnumeratedDistribution<>(pmf);
109         assertEquals(0, dist.probability(null), 0);
110         assertEquals(1, dist.probability("a"), 0);
111         assertEquals(0, dist.probability("b"), 0);
112         assertEquals(0, dist.probability("c"), 0);
113     }
114 }