1 package org.djutils.stats;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertTrue;
5 import static org.junit.Assert.fail;
6
7 import org.junit.Test;
8
9
10
11
12
13
14 public class DistNormalTableTest
15 {
16
17
18
19
20
21 @Test
22 public void testSmoothness()
23 {
24 double prevValue = 0.5;
25 for (double value : DistNormalTable.CUMULATIVE_NORMAL_PROBABILITIES)
26 {
27 assertTrue("value are non-decreasing", prevValue <= value);
28 prevValue = value;
29 }
30 assertEquals("last value is 1.0", 1.0, prevValue, 0.0);
31 }
32
33
34
35
36
37 @Test
38 public void testCumulativeNormalProbabilities()
39 {
40 assertEquals(0.5, DistNormalTable.getCumulativeProbability(0.0, 1.0, 0.0), 0.001);
41 assertEquals(0.5 + 0.682689492137086 / 2.0, DistNormalTable.getCumulativeProbability(0.0, 1.0, 1.0), 1E-6);
42 assertEquals(0.5 + 0.954499736103642 / 2.0, DistNormalTable.getCumulativeProbability(0.0, 1.0, 2.0), 1E-6);
43 assertEquals(0.5 + 0.997300203936740 / 2.0, DistNormalTable.getCumulativeProbability(0.0, 1.0, 3.0), 1E-6);
44 assertEquals(1.0, DistNormalTable.getCumulativeProbability(0.0, 1.0, 100.0), 0.001);
45 assertEquals(1.0, DistNormalTable.getCumulativeProbability(0.0, 1.0, 10000.0), 0.001);
46 assertEquals(0.5 - 0.682689492137086 / 2.0, DistNormalTable.getCumulativeProbability(0.0, 1.0, -1.0), 1E-6);
47 assertEquals(0.5 - 0.954499736103642 / 2.0, DistNormalTable.getCumulativeProbability(0.0, 1.0, -2.0), 1E-6);
48 assertEquals(0.5 - 0.997300203936740 / 2.0, DistNormalTable.getCumulativeProbability(0.0, 1.0, -3.0), 1E-6);
49 assertEquals(0.0, DistNormalTable.getCumulativeProbability(0.0, 1.0, -100.0), 0.001);
50 assertEquals(0.0, DistNormalTable.getCumulativeProbability(0.0, 1.0, -10000.0), 0.001);
51
52 assertEquals(0.5, DistNormalTable.getCumulativeProbability(0.0, 3.0, 0.0), 0.001);
53 assertEquals(0.5 + 0.682689492137086 / 2.0, DistNormalTable.getCumulativeProbability(0.0, 3.0, 3.0), 1E-6);
54 assertEquals(0.5 + 0.954499736103642 / 2.0, DistNormalTable.getCumulativeProbability(0.0, 3.0, 6.0), 1E-6);
55 assertEquals(0.5 + 0.997300203936740 / 2.0, DistNormalTable.getCumulativeProbability(0.0, 3.0, 9.0), 1E-6);
56 assertEquals(1.0, DistNormalTable.getCumulativeProbability(0.0, 3.0, 100.0), 0.001);
57 assertEquals(1.0, DistNormalTable.getCumulativeProbability(0.0, 3.0, 10000.0), 0.001);
58 assertEquals(0.5 - 0.682689492137086 / 2.0, DistNormalTable.getCumulativeProbability(0.0, 3.0, -3.0), 1E-6);
59 assertEquals(0.5 - 0.954499736103642 / 2.0, DistNormalTable.getCumulativeProbability(0.0, 3.0, -6.0), 1E-6);
60 assertEquals(0.5 - 0.997300203936740 / 2.0, DistNormalTable.getCumulativeProbability(0.0, 3.0, -9.0), 1E-6);
61 assertEquals(0.0, DistNormalTable.getCumulativeProbability(0.0, 3.0, -100.0), 0.001);
62 assertEquals(0.0, DistNormalTable.getCumulativeProbability(0.0, 3.0, -10000.0), 0.001);
63
64 assertEquals(0.5, DistNormalTable.getCumulativeProbability(6.0, 2.0, 6.0), 0.001);
65 assertEquals(0.5 + 0.682689492137086 / 2.0, DistNormalTable.getCumulativeProbability(6.0, 2.0, 8.0), 1E-6);
66 assertEquals(0.5 + 0.954499736103642 / 2.0, DistNormalTable.getCumulativeProbability(6.0, 2.0, 10.0), 1E-6);
67 assertEquals(0.5 + 0.997300203936740 / 2.0, DistNormalTable.getCumulativeProbability(6.0, 2.0, 12.0), 1E-6);
68 assertEquals(1.0, DistNormalTable.getCumulativeProbability(6.0, 2.0, 100.0), 0.001);
69 assertEquals(1.0, DistNormalTable.getCumulativeProbability(6.0, 2.0, 10000.0), 0.001);
70 assertEquals(0.5 - 0.682689492137086 / 2.0, DistNormalTable.getCumulativeProbability(6.0, 2.0, 4.0), 1E-6);
71 assertEquals(0.5 - 0.954499736103642 / 2.0, DistNormalTable.getCumulativeProbability(6.0, 2.0, 2.0), 1E-6);
72 assertEquals(0.5 - 0.997300203936740 / 2.0, DistNormalTable.getCumulativeProbability(6.0, 2.0, 0.0), 1E-6);
73 assertEquals(0.0, DistNormalTable.getCumulativeProbability(6.0, 2.0, -100.0), 0.001);
74 assertEquals(0.0, DistNormalTable.getCumulativeProbability(6.0, 2.0, -10000.0), 0.001);
75
76
77 try
78 {
79 DistNormalTable.getCumulativeProbability(0.0, -1.0, 0.0);
80 fail("negative sigma should have thrown IllegalArgumentException");
81 }
82 catch (Exception exception)
83 {
84 assertTrue(exception instanceof IllegalArgumentException);
85 }
86
87 assertEquals(0.5, DistNormalTable.getCumulativeProbability(1.0, 0, 1.0), 0);
88 assertEquals(0.0, DistNormalTable.getCumulativeProbability(1.0, 0, 1.0 - Math.ulp(1.0)), 0);
89 assertEquals(1.0, DistNormalTable.getCumulativeProbability(1.0, 0, 1.0 + Math.ulp(1.0)), 0);
90 }
91
92
93
94
95
96 @Test
97 public void testInverseCumulativeNormalProbabilities()
98 {
99 assertEquals(0.0, DistNormalTable.getInverseCumulativeProbability(0.0, 1.0, 0.5), 1E-6);
100 assertEquals(1.0, DistNormalTable.getInverseCumulativeProbability(0.0, 1.0, 0.5 + 0.682689492137086 / 2.0), 1E-6);
101 assertEquals(2.0, DistNormalTable.getInverseCumulativeProbability(0.0, 1.0, 0.5 + 0.954499736103642 / 2.0), 1E-6);
102 assertEquals(3.0, DistNormalTable.getInverseCumulativeProbability(0.0, 1.0, 0.5 + 0.997300203936740 / 2.0), 1E-6);
103 assertTrue(DistNormalTable.getInverseCumulativeProbability(0.0, 1.0, 1.0) > 5.0);
104 assertEquals(-1.0, DistNormalTable.getInverseCumulativeProbability(0.0, 1.0, 0.5 - 0.682689492137086 / 2.0), 1E-6);
105 assertEquals(-2.0, DistNormalTable.getInverseCumulativeProbability(0.0, 1.0, 0.5 - 0.954499736103642 / 2.0), 1E-6);
106 assertEquals(-3.0, DistNormalTable.getInverseCumulativeProbability(0.0, 1.0, 0.5 - 0.997300203936740 / 2.0), 1E-6);
107 assertTrue(DistNormalTable.getInverseCumulativeProbability(0.0, 1.0, 0.0) < -5.0);
108
109 assertEquals(0.0, DistNormalTable.getInverseCumulativeProbability(0.0, 2.0, 0.5), 1E-6);
110 assertEquals(2.0, DistNormalTable.getInverseCumulativeProbability(0.0, 2.0, 0.5 + 0.682689492137086 / 2.0), 1E-6);
111 assertEquals(4.0, DistNormalTable.getInverseCumulativeProbability(0.0, 2.0, 0.5 + 0.954499736103642 / 2.0), 1E-6);
112 assertEquals(6.0, DistNormalTable.getInverseCumulativeProbability(0.0, 2.0, 0.5 + 0.997300203936740 / 2.0), 1E-6);
113 assertTrue(DistNormalTable.getInverseCumulativeProbability(0.0, 2.0, 1.0) > 10.0);
114 assertEquals(-2.0, DistNormalTable.getInverseCumulativeProbability(0.0, 2.0, 0.5 - 0.682689492137086 / 2.0), 1E-6);
115 assertEquals(-4.0, DistNormalTable.getInverseCumulativeProbability(0.0, 2.0, 0.5 - 0.954499736103642 / 2.0), 1E-6);
116 assertEquals(-6.0, DistNormalTable.getInverseCumulativeProbability(0.0, 2.0, 0.5 - 0.997300203936740 / 2.0), 1E-6);
117 assertTrue(DistNormalTable.getInverseCumulativeProbability(0.0, 2.0, 0.0) < -10.0);
118
119 assertEquals(6.0, DistNormalTable.getInverseCumulativeProbability(6.0, 2.0, 0.5), 1E-6);
120 assertEquals(8.0, DistNormalTable.getInverseCumulativeProbability(6.0, 2.0, 0.5 + 0.682689492137086 / 2.0), 1E-6);
121 assertEquals(10.0, DistNormalTable.getInverseCumulativeProbability(6.0, 2.0, 0.5 + 0.954499736103642 / 2.0), 1E-6);
122 assertEquals(12.0, DistNormalTable.getInverseCumulativeProbability(6.0, 2.0, 0.5 + 0.997300203936740 / 2.0), 1E-6);
123 assertTrue(DistNormalTable.getInverseCumulativeProbability(6.0, 2.0, 1.0) > 16.0);
124 assertEquals(4.0, DistNormalTable.getInverseCumulativeProbability(6.0, 2.0, 0.5 - 0.682689492137086 / 2.0), 1E-6);
125 assertEquals(2.0, DistNormalTable.getInverseCumulativeProbability(6.0, 2.0, 0.5 - 0.954499736103642 / 2.0), 1E-6);
126 assertEquals(0.0, DistNormalTable.getInverseCumulativeProbability(6.0, 2.0, 0.5 - 0.997300203936740 / 2.0), 1E-6);
127 assertTrue(DistNormalTable.getInverseCumulativeProbability(6.0, 2.0, 0.0) < -4.0);
128
129
130 try
131 {
132 DistNormalTable.getInverseCumulativeProbability(0.0, -1.0, 0.5);
133 fail("negative sigma should have thrown IllegalArgumentException");
134 }
135 catch (Exception exception)
136 {
137 assertTrue(exception instanceof IllegalArgumentException);
138 }
139
140
141 try
142 {
143 DistNormalTable.getInverseCumulativeProbability(0.0, 1.0, -0.1);
144 fail("negative cumulative probability should have thrown IllegalArgumentException");
145 }
146 catch (Exception exception)
147 {
148 assertTrue(exception instanceof IllegalArgumentException);
149 }
150
151 try
152 {
153 DistNormalTable.getInverseCumulativeProbability(0.0, 1.0, 1.1);
154 fail("cumulative probability > 1 should have thrown IllegalArgumentException");
155 }
156 catch (Exception exception)
157 {
158 assertTrue(exception instanceof IllegalArgumentException);
159 }
160
161 }
162
163 }