KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > math > distribution > PoissonDistributionTest


1 /*
2  * Copyright 2004-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.math.distribution;
17
18 /**
19  * <code>PoissonDistributionTest</code>
20  *
21  * @version $Revision$ $Date: 2005-02-26 05:11:52 -0800 (Sat, 26 Feb 2005) $
22  */

23 public class PoissonDistributionTest extends IntegerDistributionAbstractTest {
24
25     /**
26      * Poisson parameter value for the test distribution.
27      */

28     private static final double DEFAULT_TEST_POISSON_PARAMETER = 4.0;
29
30     /**
31      * Constructor.
32      * @param name
33      */

34     public PoissonDistributionTest(String JavaDoc name) {
35         super(name);
36         setTolerance(1e-12);
37     }
38
39     /**
40      * Creates the default discrete distribution instance to use in tests.
41      */

42     public IntegerDistribution makeDistribution() {
43         return DistributionFactory.newInstance().createPoissonDistribution
44             (DEFAULT_TEST_POISSON_PARAMETER);
45     }
46
47     /**
48      * Creates the default probability density test input values.
49      */

50     public int[] makeDensityTestPoints() {
51         return new int[] { -1, 0, 1, 2, 3, 4, 5, 10, 20};
52     }
53
54     /**
55      * Creates the default probability density test expected values.
56      * These and all other test values are generated by R, version 1.8.1
57      */

58     public double[] makeDensityTestValues() {
59         return new double[] { 0d, 0.0183156388887d, 0.073262555555d,
60                 0.14652511111d, 0.195366814813d, 0.195366814813,
61                 0.156293451851d, 0.00529247667642d, 8.27746364655e-09};
62     }
63
64     /**
65      * Creates the default cumulative probability density test input values.
66      */

67     public int[] makeCumulativeTestPoints() {
68         return new int[] { -1, 0, 1, 2, 3, 4, 5, 10, 20 };
69     }
70
71     /**
72      * Creates the default cumulative probability density test expected values.
73      */

74     public double[] makeCumulativeTestValues() {
75         return new double[] { 0d, 0.0183156388887d, 0.0915781944437d,
76                 0.238103305554d, 0.433470120367d, 0.62883693518,
77                 0.78513038703d, 0.99716023388d, 0.999999998077 };
78     }
79
80     /**
81      * Creates the default inverse cumulative probability test input values.
82      * Increased 3rd and 7th values slightly as computed cumulative
83      * probabilities for corresponding values exceeds the target value (still
84      * within tolerance).
85      */

86     public double[] makeInverseCumulativeTestPoints() {
87         return new double[] { 0d, 0.018315638889d, 0.0915781944437d,
88                 0.238103305554d, 0.433470120367d, 0.62883693518,
89                 0.78513038704d, 0.99716023388d, 0.999999998077 };
90     }
91
92     /**
93      * Creates the default inverse cumulative probability density test expected values.
94      */

95     public int[] makeInverseCumulativeTestValues() {
96         return new int[] { -1, 0, 1, 2, 3, 4, 5, 10, 20};
97     }
98
99     /**
100      * Test the normal approximation of the Poisson distribution by
101      * calculating P(90 &le; X &le; 110) for X = Po(100) and
102      * P(9900 &le; X &le; 10200) for X = Po(10000)
103      */

104     public void testNormalApproximateProbability() throws Exception JavaDoc {
105         PoissonDistribution dist = DistributionFactory.newInstance().createPoissonDistribution(100);
106         double result = dist.normalApproximateProbability(110)
107                 - dist.normalApproximateProbability(89);
108         assertEquals(0.706281887248, result, 1E-10);
109         dist.setMean(10000);
110         result = dist.normalApproximateProbability(10200)
111         - dist.normalApproximateProbability(9899);
112         assertEquals(0.820070051552, result, 1E-10);
113     }
114
115     /**
116      * Test the degenerate cases of a 0.0 and 1.0 inverse cumulative probability.
117      * @throws Exception
118      */

119     public void testDegenerateInverseCumulativeProbability() throws Exception JavaDoc {
120         PoissonDistribution dist = DistributionFactory.newInstance().createPoissonDistribution(DEFAULT_TEST_POISSON_PARAMETER);
121         assertEquals(Integer.MAX_VALUE, dist.inverseCumulativeProbability(1.0d));
122         assertEquals(-1, dist.inverseCumulativeProbability(0d));
123     }
124     
125     public void testMean() {
126         PoissonDistribution dist = DistributionFactory.newInstance().createPoissonDistribution(DEFAULT_TEST_POISSON_PARAMETER);
127         try {
128             dist.setMean(-1);
129             fail("negative mean. IllegalArgumentException expected");
130         } catch(IllegalArgumentException JavaDoc ex) {
131         }
132         
133         dist.setMean(10.0);
134         assertEquals(10.0, dist.getMean(), 0.0);
135     }
136 }
Popular Tags