1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
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
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
62 }
63 bad = new double[] {};
64 try {
65 EnumeratedDistribution.checkAndNormalize(bad);
66 fail("Expecting IAE - empty probability array");
67 } catch (MathIllegalArgumentException ex) {
68
69 }
70 bad = null;
71 try {
72 EnumeratedDistribution.checkAndNormalize(bad);
73 fail("Expecting IAE - empty probability array");
74 } catch (MathIllegalArgumentException ex) {
75
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 }