001: /*
002: * Copyright 2004-2005 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.math.distribution;
017:
018: import org.apache.commons.math.MathException;
019:
020: /**
021: * <code>PoissonDistributionTest</code>
022: *
023: * @version $Revision: 348888 $ $Date: 2005-11-24 23:21:25 -0700 (Thu, 24 Nov 2005) $
024: */
025: public class PoissonDistributionTest extends
026: IntegerDistributionAbstractTest {
027:
028: /**
029: * Poisson parameter value for the test distribution.
030: */
031: private static final double DEFAULT_TEST_POISSON_PARAMETER = 4.0;
032:
033: /**
034: * Constructor.
035: * @param name
036: */
037: public PoissonDistributionTest(String name) {
038: super (name);
039: setTolerance(1e-12);
040: }
041:
042: /**
043: * Creates the default discrete distribution instance to use in tests.
044: */
045: public IntegerDistribution makeDistribution() {
046: return DistributionFactory.newInstance()
047: .createPoissonDistribution(
048: DEFAULT_TEST_POISSON_PARAMETER);
049: }
050:
051: /**
052: * Creates the default probability density test input values.
053: */
054: public int[] makeDensityTestPoints() {
055: return new int[] { -1, 0, 1, 2, 3, 4, 5, 10, 20 };
056: }
057:
058: /**
059: * Creates the default probability density test expected values.
060: * These and all other test values are generated by R, version 1.8.1
061: */
062: public double[] makeDensityTestValues() {
063: return new double[] { 0d, 0.0183156388887d, 0.073262555555d,
064: 0.14652511111d, 0.195366814813d, 0.195366814813,
065: 0.156293451851d, 0.00529247667642d, 8.27746364655e-09 };
066: }
067:
068: /**
069: * Creates the default cumulative probability density test input values.
070: */
071: public int[] makeCumulativeTestPoints() {
072: return new int[] { -1, 0, 1, 2, 3, 4, 5, 10, 20 };
073: }
074:
075: /**
076: * Creates the default cumulative probability density test expected values.
077: */
078: public double[] makeCumulativeTestValues() {
079: return new double[] { 0d, 0.0183156388887d, 0.0915781944437d,
080: 0.238103305554d, 0.433470120367d, 0.62883693518,
081: 0.78513038703d, 0.99716023388d, 0.999999998077 };
082: }
083:
084: /**
085: * Creates the default inverse cumulative probability test input values.
086: * Increased 3rd and 7th values slightly as computed cumulative
087: * probabilities for corresponding values exceeds the target value (still
088: * within tolerance).
089: */
090: public double[] makeInverseCumulativeTestPoints() {
091: return new double[] { 0d, 0.018315638889d, 0.0915781944437d,
092: 0.238103305554d, 0.433470120367d, 0.62883693518,
093: 0.78513038704d, 0.99716023388d, 0.999999998077 };
094: }
095:
096: /**
097: * Creates the default inverse cumulative probability density test expected values.
098: */
099: public int[] makeInverseCumulativeTestValues() {
100: return new int[] { -1, 0, 1, 2, 3, 4, 5, 10, 20 };
101: }
102:
103: /**
104: * Test the normal approximation of the Poisson distribution by
105: * calculating P(90 ≤ X ≤ 110) for X = Po(100) and
106: * P(9900 ≤ X ≤ 10200) for X = Po(10000)
107: */
108: public void testNormalApproximateProbability() throws Exception {
109: PoissonDistribution dist = DistributionFactory.newInstance()
110: .createPoissonDistribution(100);
111: double result = dist.normalApproximateProbability(110)
112: - dist.normalApproximateProbability(89);
113: assertEquals(0.706281887248, result, 1E-10);
114: dist.setMean(10000);
115: result = dist.normalApproximateProbability(10200)
116: - dist.normalApproximateProbability(9899);
117: assertEquals(0.820070051552, result, 1E-10);
118: }
119:
120: /**
121: * Test the degenerate cases of a 0.0 and 1.0 inverse cumulative probability.
122: * @throws Exception
123: */
124: public void testDegenerateInverseCumulativeProbability()
125: throws Exception {
126: PoissonDistribution dist = DistributionFactory.newInstance()
127: .createPoissonDistribution(
128: DEFAULT_TEST_POISSON_PARAMETER);
129: assertEquals(Integer.MAX_VALUE, dist
130: .inverseCumulativeProbability(1.0d));
131: assertEquals(-1, dist.inverseCumulativeProbability(0d));
132: }
133:
134: public void testMean() {
135: PoissonDistribution dist = DistributionFactory.newInstance()
136: .createPoissonDistribution(
137: DEFAULT_TEST_POISSON_PARAMETER);
138: try {
139: dist.setMean(-1);
140: fail("negative mean. IllegalArgumentException expected");
141: } catch (IllegalArgumentException ex) {
142: }
143:
144: dist.setMean(10.0);
145: assertEquals(10.0, dist.getMean(), 0.0);
146: }
147:
148: public void testLargeMeanCumulativeProbability() {
149: PoissonDistribution dist = DistributionFactory.newInstance()
150: .createPoissonDistribution(1.0);
151: double mean = 1.0;
152: while (mean <= 10000000.0) {
153: dist.setMean(mean);
154:
155: double x = mean * 2.0;
156: double dx = x / 10.0;
157: while (x >= 0) {
158: try {
159: dist.cumulativeProbability(x);
160: } catch (MathException ex) {
161: fail("mean of " + mean + " and x of " + x
162: + " caused " + ex.getMessage());
163: }
164: x -= dx;
165: }
166:
167: mean *= 10.0;
168: }
169: }
170:
171: public void testLargeMeanInverseCumulativeProbability() {
172: PoissonDistribution dist = DistributionFactory.newInstance()
173: .createPoissonDistribution(1.0);
174: double mean = 1.0;
175: while (mean <= 10000000.0) {
176: dist.setMean(mean);
177:
178: double p = 0.1;
179: double dp = p;
180: while (p < 1.0) {
181: try {
182: dist.inverseCumulativeProbability(p);
183: } catch (MathException ex) {
184: fail("mean of " + mean + " and p of " + p
185: + " caused " + ex.getMessage());
186: }
187: p += dp;
188: }
189:
190: mean *= 10.0;
191: }
192: }
193: }
|