1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.hipparchus.distribution.continuous;
24
25 import org.hipparchus.distribution.RealDistribution;
26 import org.hipparchus.exception.MathIllegalArgumentException;
27 import org.junit.Assert;
28 import org.junit.Test;
29
30
31
32
33 public class NormalDistributionTest extends RealDistributionAbstractTest {
34
35
36
37
38 @Override
39 public NormalDistribution makeDistribution() {
40 return new NormalDistribution(2.1, 1.4);
41 }
42
43
44 @Override
45 public double[] makeCumulativeTestPoints() {
46
47 return new double[] {-2.226325228634938d, -1.156887023657177d, -0.643949578356075d, -0.2027950777320613d, 0.305827808237559d,
48 6.42632522863494d, 5.35688702365718d, 4.843949578356074d, 4.40279507773206d, 3.89417219176244d};
49 }
50
51
52 @Override
53 public double[] makeCumulativeTestValues() {
54 return new double[] {0.001d, 0.01d, 0.025d, 0.05d, 0.1d, 0.999d,
55 0.990d, 0.975d, 0.950d, 0.900d};
56 }
57
58
59 @Override
60 public double[] makeDensityTestValues() {
61 return new double[] {0.00240506434076, 0.0190372444310, 0.0417464784322, 0.0736683145538, 0.125355951380,
62 0.00240506434076, 0.0190372444310, 0.0417464784322, 0.0736683145538, 0.125355951380};
63 }
64
65
66 protected double defaultTolerance = 1e-9;
67 @Override
68 public void setUp() {
69 super.setUp();
70 setTolerance(defaultTolerance);
71 }
72
73
74
75 private void verifyQuantiles() {
76 NormalDistribution distribution = (NormalDistribution) getDistribution();
77 double mu = distribution.getMean();
78 double sigma = distribution.getStandardDeviation();
79 setCumulativeTestPoints( new double[] {mu - 2 *sigma, mu - sigma,
80 mu, mu + sigma, mu + 2 * sigma, mu + 3 * sigma, mu + 4 * sigma,
81 mu + 5 * sigma});
82
83 setCumulativeTestValues(new double[] {0.02275013194817921, 0.158655253931457, 0.5, 0.841344746068543,
84 0.977249868051821, 0.99865010196837, 0.999968328758167, 0.999999713348428});
85 verifyCumulativeProbabilities();
86 }
87
88 @Test
89 public void testQuantiles() {
90 setDensityTestValues(new double[] {0.0385649760808, 0.172836231799, 0.284958771715, 0.172836231799, 0.0385649760808,
91 0.00316560600853, 9.55930184035e-05, 1.06194251052e-06});
92 verifyQuantiles();
93 verifyDensities();
94
95 setDistribution(new NormalDistribution(0, 1));
96 setDensityTestValues(new double[] {0.0539909665132, 0.241970724519, 0.398942280401, 0.241970724519, 0.0539909665132,
97 0.00443184841194, 0.000133830225765, 1.48671951473e-06});
98 verifyQuantiles();
99 verifyDensities();
100
101 setDistribution(new NormalDistribution(0, 0.1));
102 setDensityTestValues(new double[] {0.539909665132, 2.41970724519, 3.98942280401, 2.41970724519,
103 0.539909665132, 0.0443184841194, 0.00133830225765, 1.48671951473e-05});
104 verifyQuantiles();
105 verifyDensities();
106 }
107
108 @Test
109 public void testInverseCumulativeProbabilityExtremes() {
110 setInverseCumulativeTestPoints(new double[] {0, 1});
111 setInverseCumulativeTestValues(
112 new double[] {Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY});
113 verifyInverseCumulativeProbabilities();
114 }
115
116
117 @Test
118 public void testCumulativeProbability() {
119 final RealDistribution dist = new NormalDistribution(0, 1);
120 double x = -10;
121 double expected = 7.61985e-24;
122 double v = dist.cumulativeProbability(x);
123 double tol = 1e-5;
124 Assert.assertEquals(1, v / expected, tol);
125 }
126
127 @Test
128 public void testGetMean() {
129 NormalDistribution distribution = (NormalDistribution) getDistribution();
130 Assert.assertEquals(2.1, distribution.getMean(), 0);
131 }
132
133 @Test
134 public void testGetStandardDeviation() {
135 NormalDistribution distribution = (NormalDistribution) getDistribution();
136 Assert.assertEquals(1.4, distribution.getStandardDeviation(), 0);
137 }
138
139 @Test(expected=MathIllegalArgumentException.class)
140 public void testPreconditions() {
141 new NormalDistribution(1, 0);
142 }
143
144 @Test
145 public void testDensity() {
146 double [] x = new double[]{-2, -1, 0, 1, 2};
147
148 checkDensity(0, 1, x, new double[]{0.05399096651, 0.24197072452, 0.39894228040, 0.24197072452, 0.05399096651});
149
150 checkDensity(1.1, 1, x, new double[]{0.003266819056,0.043983595980,0.217852177033,0.396952547477,0.266085249899});
151 }
152
153 private void checkDensity(double mean, double sd, double[] x, double[] expected) {
154 NormalDistribution d = new NormalDistribution(mean, sd);
155 for (int i = 0; i < x.length; i++) {
156 Assert.assertEquals(expected[i], d.density(x[i]), 1e-9);
157 }
158 }
159
160
161
162
163
164 @Test
165 public void testExtremeValues() {
166 NormalDistribution distribution = new NormalDistribution(0, 1);
167 for (int i = 0; i < 100; i++) {
168 double lowerTail = distribution.cumulativeProbability(-i);
169 double upperTail = distribution.cumulativeProbability(i);
170 if (i < 9) {
171
172
173 Assert.assertTrue(lowerTail > 0.0d);
174 Assert.assertTrue(upperTail < 1.0d);
175 }
176 else {
177 Assert.assertTrue(lowerTail < 0.00001);
178 Assert.assertTrue(upperTail > 0.99999);
179 }
180 }
181
182 Assert.assertEquals(distribution.cumulativeProbability(Double.MAX_VALUE), 1, 0);
183 Assert.assertEquals(distribution.cumulativeProbability(-Double.MAX_VALUE), 0, 0);
184 Assert.assertEquals(distribution.cumulativeProbability(Double.POSITIVE_INFINITY), 1, 0);
185 Assert.assertEquals(distribution.cumulativeProbability(Double.NEGATIVE_INFINITY), 0, 0);
186 }
187
188 @Test
189 public void testMath280() {
190 NormalDistribution normal = new NormalDistribution(0,1);
191 double result = normal.inverseCumulativeProbability(0.9986501019683698);
192 Assert.assertEquals(3.0, result, defaultTolerance);
193 result = normal.inverseCumulativeProbability(0.841344746068543);
194 Assert.assertEquals(1.0, result, defaultTolerance);
195 result = normal.inverseCumulativeProbability(0.9999683287581673);
196 Assert.assertEquals(4.0, result, defaultTolerance);
197 result = normal.inverseCumulativeProbability(0.9772498680518209);
198 Assert.assertEquals(2.0, result, defaultTolerance);
199 }
200
201 @Test
202 public void testMoments() {
203 final double tol = 1e-9;
204 NormalDistribution dist;
205
206 dist = new NormalDistribution(0, 1);
207 Assert.assertEquals(dist.getNumericalMean(), 0, tol);
208 Assert.assertEquals(dist.getNumericalVariance(), 1, tol);
209
210 dist = new NormalDistribution(2.2, 1.4);
211 Assert.assertEquals(dist.getNumericalMean(), 2.2, tol);
212 Assert.assertEquals(dist.getNumericalVariance(), 1.4 * 1.4, tol);
213
214 dist = new NormalDistribution(-2000.9, 10.4);
215 Assert.assertEquals(dist.getNumericalMean(), -2000.9, tol);
216 Assert.assertEquals(dist.getNumericalVariance(), 10.4 * 10.4, tol);
217 }
218 }