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 GammaDistributionTest extends ContinuousDistributionAbstractTest {
28
29
30
31
32
33 public GammaDistributionTest(String name) {
34 super(name);
35 }
36
37
38
39
40 public ContinuousDistribution makeDistribution() {
41 return new GammaDistributionImpl(4d, 2d);
42 }
43
44
45 public double[] makeCumulativeTestPoints() {
46
47 return new double[] {0.8571048, 1.646497, 2.179731, 2.732637,
48 3.489539, 26.12448, 20.09024, 17.53455,
49 15.50731, 13.36157};
50 }
51
52
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 protected void setUp() throws Exception {
60 super.setUp();
61 setTolerance(6e-6);
62 }
63
64
65 public void testParameterAccessors() {
66 GammaDistribution distribution = (GammaDistribution) getDistribution();
67 assertEquals(4d, distribution.getAlpha(), 0);
68 distribution.setAlpha(3d);
69 assertEquals(3d, distribution.getAlpha(), 0);
70 assertEquals(2d, distribution.getBeta(), 0);
71 distribution.setBeta(4d);
72 assertEquals(4d, distribution.getBeta(), 0);
73 try {
74 distribution.setAlpha(0d);
75 fail("Expecting IllegalArgumentException for alpha = 0");
76 } catch (IllegalArgumentException ex) {
77
78 }
79 try {
80 distribution.setBeta(0d);
81 fail("Expecting IllegalArgumentException for beta = 0");
82 } catch (IllegalArgumentException ex) {
83
84 }
85 }
86
87 public void testProbabilities() throws Exception {
88 testProbability(-1.000, 4.0, 2.0, .0000);
89 testProbability(15.501, 4.0, 2.0, .9499);
90 testProbability(0.504, 4.0, 1.0, .0018);
91 testProbability(10.011, 1.0, 2.0, .9933);
92 testProbability(5.000, 2.0, 2.0, .7127);
93 }
94
95 public void testValues() throws Exception {
96 testValue(15.501, 4.0, 2.0, .9499);
97 testValue(0.504, 4.0, 1.0, .0018);
98 testValue(10.011, 1.0, 2.0, .9933);
99 testValue(5.000, 2.0, 2.0, .7127);
100 }
101
102 private void testProbability(double x, double a, double b, double expected) throws Exception {
103 GammaDistribution distribution = new GammaDistributionImpl( a, b );
104 double actual = distribution.cumulativeProbability(x);
105 assertEquals("probability for " + x, expected, actual, 10e-4);
106 }
107
108 private void testValue(double expected, double a, double b, double p) throws Exception {
109 GammaDistribution distribution = new GammaDistributionImpl( a, b );
110 double actual = distribution.inverseCumulativeProbability(p);
111 assertEquals("critical value for " + p, expected, actual, 10e-4);
112 }
113
114 public void testInverseCumulativeProbabilityExtremes() throws Exception {
115 setInverseCumulativeTestPoints(new double[] {0, 1});
116 setInverseCumulativeTestValues(new double[] {0, Double.POSITIVE_INFINITY});
117 verifyInverseCumulativeProbabilities();
118 }
119 }