1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.math.distribution;
19
20
21
22
23
24
25
26
27 public class NormalDistributionTest extends ContinuousDistributionAbstractTest {
28
29
30
31
32
33 public NormalDistributionTest(String arg0) {
34 super(arg0);
35 }
36
37
38
39
40 public ContinuousDistribution makeDistribution() {
41 return new NormalDistributionImpl(2.1, 1.4);
42 }
43
44
45 public double[] makeCumulativeTestPoints() {
46
47 return new double[] {-2.226325d, -1.156887d, -0.6439496d, -0.2027951d, 0.3058278d,
48 6.426325d, 5.356887d, 4.84395d, 4.402795d, 3.894172d};
49 }
50
51
52 public double[] makeCumulativeTestValues() {
53 return new double[] {0.001d, 0.01d, 0.025d, 0.05d, 0.1d, 0.999d,
54 0.990d, 0.975d, 0.950d, 0.900d};
55 }
56
57
58 protected void setUp() throws Exception {
59 super.setUp();
60 setTolerance(1E-6);
61 }
62
63
64
65 private void verifyQuantiles() throws Exception {
66 NormalDistribution distribution = (NormalDistribution) getDistribution();
67 double mu = distribution.getMean();
68 double sigma = distribution.getStandardDeviation();
69 setCumulativeTestPoints( new double[] {mu - 2 *sigma, mu - sigma,
70 mu, mu + sigma, mu +2 * sigma, mu +3 * sigma, mu + 4 * sigma,
71 mu + 5 * sigma});
72
73 setCumulativeTestValues(new double[] {0.02275013, 0.1586553, 0.5, 0.8413447,
74 0.9772499, 0.9986501, 0.9999683, 0.9999997});
75 verifyCumulativeProbabilities();
76 }
77
78 public void testQuantiles() throws Exception {
79 verifyQuantiles();
80 setDistribution(new NormalDistributionImpl(0, 1));
81 verifyQuantiles();
82 setDistribution(new NormalDistributionImpl(0, 0.1));
83 verifyQuantiles();
84 }
85
86 public void testInverseCumulativeProbabilityExtremes() throws Exception {
87 setInverseCumulativeTestPoints(new double[] {0, 1});
88 setInverseCumulativeTestValues(
89 new double[] {Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY});
90 verifyInverseCumulativeProbabilities();
91 }
92
93 public void testGetMean() {
94 NormalDistribution distribution = (NormalDistribution) getDistribution();
95 assertEquals(2.1, distribution.getMean(), 0);
96 }
97
98 public void testSetMean() throws Exception {
99 double mu = Math.random();
100 NormalDistribution distribution = (NormalDistribution) getDistribution();
101 distribution.setMean(mu);
102 verifyQuantiles();
103 }
104
105 public void testGetStandardDeviation() {
106 NormalDistribution distribution = (NormalDistribution) getDistribution();
107 assertEquals(1.4, distribution.getStandardDeviation(), 0);
108 }
109
110 public void testSetStandardDeviation() throws Exception {
111 double sigma = 0.1d + Math.random();
112 NormalDistribution distribution = (NormalDistribution) getDistribution();
113 distribution.setStandardDeviation(sigma);
114 assertEquals(sigma, distribution.getStandardDeviation(), 0);
115 verifyQuantiles();
116 try {
117 distribution.setStandardDeviation(0);
118 fail("Expecting IllegalArgumentException for sd = 0");
119 } catch (IllegalArgumentException ex) {
120
121 }
122 }
123
124
125
126
127
128 public void testExtremeValues() throws Exception {
129 NormalDistribution distribution = (NormalDistribution) getDistribution();
130 distribution.setMean(0);
131 distribution.setStandardDeviation(1);
132 for (int i = 0; i < 100; i+=5) {
133 double lowerTail = distribution.cumulativeProbability((double)-i);
134 double upperTail = distribution.cumulativeProbability((double) i);
135 if (i < 10) {
136 assertTrue(lowerTail > 0.0d);
137 assertTrue(upperTail < 1.0d);
138 }
139 else {
140 assertTrue(lowerTail < 0.00001);
141 assertTrue(upperTail > 0.99999);
142 }
143 }
144 }
145 }